Move to kernel style SPDX license identifiers
[babeltrace.git] / src / lib / trace-ir / event-class.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 */
7
8 #define BT_LOG_TAG "LIB/EVENT-CLASS"
9 #include "lib/logging.h"
10
11 #include "lib/assert-pre.h"
12 #include <babeltrace2/trace-ir/field-class.h>
13 #include <babeltrace2/trace-ir/event-class.h>
14 #include <babeltrace2/trace-ir/stream-class.h>
15 #include "compat/compiler.h"
16 #include "compat/endian.h"
17 #include <babeltrace2/types.h>
18 #include "lib/value.h"
19 #include "common/assert.h"
20 #include <inttypes.h>
21 #include <stdbool.h>
22 #include <stdlib.h>
23
24 #include "attributes.h"
25 #include "clock-snapshot.h"
26 #include "event-class.h"
27 #include "event.h"
28 #include "field-class.h"
29 #include "field.h"
30 #include "resolve-field-path.h"
31 #include "stream-class.h"
32 #include "trace.h"
33 #include "utils.h"
34 #include "lib/func-status.h"
35
36 #define BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(_ec) \
37 BT_ASSERT_PRE_DEV_HOT(((const struct bt_event_class *) (_ec)), \
38 "Event class", ": %!+E", (_ec))
39
40 static
41 void destroy_event_class(struct bt_object *obj)
42 {
43 struct bt_event_class *event_class = (void *) obj;
44
45 BT_LIB_LOGD("Destroying event class: %!+E", event_class);
46 BT_OBJECT_PUT_REF_AND_RESET(event_class->user_attributes);
47
48 if (event_class->name.str) {
49 g_string_free(event_class->name.str, TRUE);
50 event_class->name.str = NULL;
51 }
52
53 if (event_class->emf_uri.str) {
54 g_string_free(event_class->emf_uri.str, TRUE);
55 event_class->emf_uri.str = NULL;
56 }
57
58 BT_LOGD_STR("Putting context field class.");
59 BT_OBJECT_PUT_REF_AND_RESET(event_class->specific_context_fc);
60 BT_LOGD_STR("Putting payload field class.");
61 BT_OBJECT_PUT_REF_AND_RESET(event_class->payload_fc);
62 bt_object_pool_finalize(&event_class->event_pool);
63 g_free(obj);
64 }
65
66 static
67 void free_event(struct bt_event *event,
68 struct bt_event_class *event_class)
69 {
70 bt_event_destroy(event);
71 }
72
73 static
74 bool event_class_id_is_unique(const struct bt_stream_class *stream_class,
75 uint64_t id)
76 {
77 uint64_t i;
78 bool is_unique = true;
79
80 for (i = 0; i < stream_class->event_classes->len; i++) {
81 const struct bt_event_class *ec =
82 stream_class->event_classes->pdata[i];
83
84 if (ec->id == id) {
85 is_unique = false;
86 goto end;
87 }
88 }
89
90 end:
91 return is_unique;
92 }
93
94 static
95 struct bt_event_class *create_event_class_with_id(
96 struct bt_stream_class *stream_class, uint64_t id)
97 {
98 int ret;
99 struct bt_event_class *event_class;
100
101 BT_ASSERT(stream_class);
102 BT_ASSERT_PRE(event_class_id_is_unique(stream_class, id),
103 "Duplicate event class ID: %![sc-]+S, id=%" PRIu64,
104 stream_class, id);
105 BT_LIB_LOGD("Creating event class object: %![sc-]+S, id=%" PRIu64,
106 stream_class, id);
107 event_class = g_new0(struct bt_event_class, 1);
108 if (!event_class) {
109 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one event class.");
110 goto error;
111 }
112
113 bt_object_init_shared_with_parent(&event_class->base,
114 destroy_event_class);
115 event_class->user_attributes = bt_value_map_create();
116 if (!event_class->user_attributes) {
117 BT_LIB_LOGE_APPEND_CAUSE(
118 "Failed to create a map value object.");
119 goto error;
120 }
121
122 event_class->id = id;
123 bt_property_uint_init(&event_class->log_level,
124 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE, 0);
125 event_class->name.str = g_string_new(NULL);
126 if (!event_class->name.str) {
127 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
128 goto error;
129 }
130
131 event_class->emf_uri.str = g_string_new(NULL);
132 if (!event_class->emf_uri.str) {
133 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
134 goto error;
135 }
136
137 ret = bt_object_pool_initialize(&event_class->event_pool,
138 (bt_object_pool_new_object_func) bt_event_new,
139 (bt_object_pool_destroy_object_func) free_event,
140 event_class);
141 if (ret) {
142 BT_LIB_LOGE_APPEND_CAUSE(
143 "Failed to initialize event pool: ret=%d",
144 ret);
145 goto error;
146 }
147
148 bt_object_set_parent(&event_class->base, &stream_class->base);
149 g_ptr_array_add(stream_class->event_classes, event_class);
150 bt_stream_class_freeze(stream_class);
151 BT_LIB_LOGD("Created event class object: %!+E", event_class);
152 goto end;
153
154 error:
155 BT_OBJECT_PUT_REF_AND_RESET(event_class);
156
157 end:
158 return event_class;
159 }
160
161 struct bt_event_class *bt_event_class_create(
162 struct bt_stream_class *stream_class)
163 {
164 BT_ASSERT_PRE_NO_ERROR();
165 BT_ASSERT_PRE_NON_NULL(stream_class, "Stream class");
166 BT_ASSERT_PRE(stream_class->assigns_automatic_event_class_id,
167 "Stream class does not automatically assigns event class IDs: "
168 "%![sc-]+S", stream_class);
169 return create_event_class_with_id(stream_class,
170 (uint64_t) stream_class->event_classes->len);
171 }
172
173 struct bt_event_class *bt_event_class_create_with_id(
174 struct bt_stream_class *stream_class, uint64_t id)
175 {
176 BT_ASSERT_PRE_NO_ERROR();
177 BT_ASSERT_PRE(!stream_class->assigns_automatic_event_class_id,
178 "Stream class automatically assigns event class IDs: "
179 "%![sc-]+S", stream_class);
180 return create_event_class_with_id(stream_class, id);
181 }
182
183 const char *bt_event_class_get_name(const struct bt_event_class *event_class)
184 {
185 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
186 return event_class->name.value;
187 }
188
189 enum bt_event_class_set_name_status bt_event_class_set_name(
190 struct bt_event_class *event_class, const char *name)
191 {
192 BT_ASSERT_PRE_NO_ERROR();
193 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
194 BT_ASSERT_PRE_NON_NULL(name, "Name");
195 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
196 g_string_assign(event_class->name.str, name);
197 event_class->name.value = event_class->name.str->str;
198 BT_LIB_LOGD("Set event class's name: %!+E", event_class);
199 return BT_FUNC_STATUS_OK;
200 }
201
202 uint64_t bt_event_class_get_id(const struct bt_event_class *event_class)
203 {
204 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
205 return event_class->id;
206 }
207
208 enum bt_property_availability bt_event_class_get_log_level(
209 const struct bt_event_class *event_class,
210 enum bt_event_class_log_level *log_level)
211 {
212 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
213 BT_ASSERT_PRE_DEV_NON_NULL(log_level, "Log level (output)");
214 *log_level = (enum bt_event_class_log_level)
215 event_class->log_level.value;
216 return event_class->log_level.base.avail;
217 }
218
219 void bt_event_class_set_log_level(
220 struct bt_event_class *event_class,
221 enum bt_event_class_log_level log_level)
222 {
223 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
224 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
225 bt_property_uint_set(&event_class->log_level,
226 (uint64_t) log_level);
227 BT_LIB_LOGD("Set event class's log level: %!+E", event_class);
228 }
229
230 const char *bt_event_class_get_emf_uri(const struct bt_event_class *event_class)
231 {
232 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
233 return event_class->emf_uri.value;
234 }
235
236 enum bt_event_class_set_emf_uri_status bt_event_class_set_emf_uri(
237 struct bt_event_class *event_class,
238 const char *emf_uri)
239 {
240 BT_ASSERT_PRE_NO_ERROR();
241 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
242 BT_ASSERT_PRE_NON_NULL(emf_uri, "EMF URI");
243 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
244 g_string_assign(event_class->emf_uri.str, emf_uri);
245 event_class->emf_uri.value = event_class->emf_uri.str->str;
246 BT_LIB_LOGD("Set event class's EMF URI: %!+E", event_class);
247 return BT_FUNC_STATUS_OK;
248 }
249
250 struct bt_stream_class *bt_event_class_borrow_stream_class(
251 struct bt_event_class *event_class)
252 {
253 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
254 return bt_event_class_borrow_stream_class_inline(event_class);
255 }
256
257 const struct bt_stream_class *
258 bt_event_class_borrow_stream_class_const(
259 const struct bt_event_class *event_class)
260 {
261 return bt_event_class_borrow_stream_class((void *) event_class);
262 }
263
264 const struct bt_field_class *
265 bt_event_class_borrow_specific_context_field_class_const(
266 const struct bt_event_class *event_class)
267 {
268 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
269 return event_class->specific_context_fc;
270 }
271
272 struct bt_field_class *
273 bt_event_class_borrow_specific_context_field_class(
274 struct bt_event_class *event_class)
275 {
276 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
277 return event_class->specific_context_fc;
278 }
279
280 enum bt_event_class_set_field_class_status
281 bt_event_class_set_specific_context_field_class(
282 struct bt_event_class *event_class,
283 struct bt_field_class *field_class)
284 {
285 int ret;
286 struct bt_stream_class *stream_class;
287 struct bt_resolve_field_path_context resolve_ctx = {
288 .packet_context = NULL,
289 .event_common_context = NULL,
290 .event_specific_context = field_class,
291 .event_payload = NULL,
292 };
293
294 BT_ASSERT_PRE_NO_ERROR();
295 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
296 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
297 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
298 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
299 BT_FIELD_CLASS_TYPE_STRUCTURE,
300 "Specific context field class is not a structure field class: "
301 "%!+F", field_class);
302 stream_class = bt_event_class_borrow_stream_class_inline(
303 event_class);
304 resolve_ctx.packet_context = stream_class->packet_context_fc;
305 resolve_ctx.event_common_context =
306 stream_class->event_common_context_fc;
307
308 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
309 if (ret) {
310 /*
311 * This is the only reason for which
312 * bt_resolve_field_paths() can fail: anything else
313 * would be because a precondition is not satisfied.
314 */
315 ret = BT_FUNC_STATUS_MEMORY_ERROR;
316 goto end;
317 }
318
319 bt_field_class_make_part_of_trace_class(field_class);
320 bt_object_put_ref(event_class->specific_context_fc);
321 event_class->specific_context_fc = field_class;
322 bt_object_get_ref_no_null_check(event_class->specific_context_fc);
323 bt_field_class_freeze(field_class);
324 BT_LIB_LOGD("Set event class's specific context field class: %!+E",
325 event_class);
326
327 end:
328 return ret;
329 }
330
331 const struct bt_field_class *bt_event_class_borrow_payload_field_class_const(
332 const struct bt_event_class *event_class)
333 {
334 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
335 return event_class->payload_fc;
336 }
337
338 struct bt_field_class *bt_event_class_borrow_payload_field_class(
339 struct bt_event_class *event_class)
340 {
341 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
342 return event_class->payload_fc;
343 }
344
345 enum bt_event_class_set_field_class_status
346 bt_event_class_set_payload_field_class(
347 struct bt_event_class *event_class,
348 struct bt_field_class *field_class)
349 {
350 int ret;
351 struct bt_stream_class *stream_class;
352 struct bt_resolve_field_path_context resolve_ctx = {
353 .packet_context = NULL,
354 .event_common_context = NULL,
355 .event_specific_context = NULL,
356 .event_payload = field_class,
357 };
358
359 BT_ASSERT_PRE_NO_ERROR();
360 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
361 BT_ASSERT_PRE_NON_NULL(field_class, "Field class");
362 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
363 BT_ASSERT_PRE(bt_field_class_get_type(field_class) ==
364 BT_FIELD_CLASS_TYPE_STRUCTURE,
365 "Payload field class is not a structure field class: %!+F",
366 field_class);
367 stream_class = bt_event_class_borrow_stream_class_inline(
368 event_class);
369 resolve_ctx.packet_context = stream_class->packet_context_fc;
370 resolve_ctx.event_common_context =
371 stream_class->event_common_context_fc;
372 resolve_ctx.event_specific_context = event_class->specific_context_fc;
373
374 ret = bt_resolve_field_paths(field_class, &resolve_ctx);
375 if (ret) {
376 /*
377 * This is the only reason for which
378 * bt_resolve_field_paths() can fail: anything else
379 * would be because a precondition is not satisfied.
380 */
381 ret = BT_FUNC_STATUS_MEMORY_ERROR;
382 goto end;
383 }
384
385 bt_field_class_make_part_of_trace_class(field_class);
386 bt_object_put_ref(event_class->payload_fc);
387 event_class->payload_fc = field_class;
388 bt_object_get_ref_no_null_check(event_class->payload_fc);
389 bt_field_class_freeze(field_class);
390 BT_LIB_LOGD("Set event class's payload field class: %!+E", event_class);
391
392 end:
393 return ret;
394 }
395
396 BT_HIDDEN
397 void _bt_event_class_freeze(const struct bt_event_class *event_class)
398 {
399 /* The field classes are already frozen */
400 BT_ASSERT(event_class);
401 BT_LIB_LOGD("Freezing event class's user attributes: %!+v",
402 event_class->user_attributes);
403 bt_value_freeze(event_class->user_attributes);
404 BT_LIB_LOGD("Freezing event class: %!+E", event_class);
405 ((struct bt_event_class *) event_class)->frozen = true;
406 }
407
408 const struct bt_value *bt_event_class_borrow_user_attributes_const(
409 const struct bt_event_class *event_class)
410 {
411 BT_ASSERT_PRE_DEV_NON_NULL(event_class, "Event class");
412 return event_class->user_attributes;
413 }
414
415 struct bt_value *bt_event_class_borrow_user_attributes(
416 struct bt_event_class *event_class)
417 {
418 return (void *) bt_event_class_borrow_user_attributes_const(
419 (void *) event_class);
420 }
421
422 void bt_event_class_set_user_attributes(
423 struct bt_event_class *event_class,
424 const struct bt_value *user_attributes)
425 {
426 BT_ASSERT_PRE_NON_NULL(event_class, "Event class");
427 BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes");
428 BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP,
429 "User attributes object is not a map value object.");
430 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
431 bt_object_put_ref_no_null_check(event_class->user_attributes);
432 event_class->user_attributes = (void *) user_attributes;
433 bt_object_get_ref_no_null_check(event_class->user_attributes);
434 }
435
436 void bt_event_class_get_ref(const struct bt_event_class *event_class)
437 {
438 bt_object_get_ref(event_class);
439 }
440
441 void bt_event_class_put_ref(const struct bt_event_class *event_class)
442 {
443 bt_object_put_ref(event_class);
444 }
This page took 0.037604 seconds and 4 git commands to generate.