3a8892f9a8f2af75706ed99cd65186a97ad3b8f2
[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-cond.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 "event-class.h"
25 #include "event.h"
26 #include "field-class.h"
27 #include "resolve-field-path.h"
28 #include "stream-class.h"
29 #include "lib/func-status.h"
30
31 #define BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(_ec) \
32 BT_ASSERT_PRE_DEV_HOT("event-class", \
33 ((const struct bt_event_class *) (_ec)), \
34 "Event class", ": %!+E", (_ec))
35
36 static
37 void destroy_event_class(struct bt_object *obj)
38 {
39 struct bt_event_class *event_class = (void *) obj;
40
41 BT_LIB_LOGD("Destroying event class: %!+E", event_class);
42 BT_OBJECT_PUT_REF_AND_RESET(event_class->user_attributes);
43
44 g_free(event_class->ns);
45 g_free(event_class->name);
46 g_free(event_class->uid);
47 g_free(event_class->emf_uri);
48 BT_LOGD_STR("Putting context field class.");
49 BT_OBJECT_PUT_REF_AND_RESET(event_class->specific_context_fc);
50 BT_LOGD_STR("Putting payload field class.");
51 BT_OBJECT_PUT_REF_AND_RESET(event_class->payload_fc);
52 bt_object_pool_finalize(&event_class->event_pool);
53 g_free(obj);
54 }
55
56 static
57 void free_event(struct bt_event *event,
58 struct bt_event_class *event_class __attribute__((unused)))
59 {
60 bt_event_destroy(event);
61 }
62
63 static
64 bool event_class_id_is_unique(const struct bt_stream_class *stream_class,
65 uint64_t id)
66 {
67 uint64_t i;
68 bool is_unique = true;
69
70 for (i = 0; i < stream_class->event_classes->len; i++) {
71 const struct bt_event_class *ec =
72 stream_class->event_classes->pdata[i];
73
74 if (ec->id == id) {
75 is_unique = false;
76 goto end;
77 }
78 }
79
80 end:
81 return is_unique;
82 }
83
84 static
85 struct bt_event_class *create_event_class_with_id(
86 struct bt_stream_class *stream_class, uint64_t id)
87 {
88 int ret;
89 struct bt_event_class *event_class;
90
91 BT_ASSERT(stream_class);
92 BT_ASSERT_PRE("event-class-id-is-unique",
93 event_class_id_is_unique(stream_class, id),
94 "Duplicate event class ID: %![sc-]+S, id=%" PRIu64,
95 stream_class, id);
96 BT_LIB_LOGD("Creating event class object: %![sc-]+S, id=%" PRIu64,
97 stream_class, id);
98 event_class = g_new0(struct bt_event_class, 1);
99 if (!event_class) {
100 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one event class.");
101 goto error;
102 }
103
104 bt_object_init_shared_with_parent(&event_class->base,
105 destroy_event_class);
106 event_class->user_attributes = bt_value_map_create();
107 if (!event_class->user_attributes) {
108 BT_LIB_LOGE_APPEND_CAUSE(
109 "Failed to create a map value object.");
110 goto error;
111 }
112
113 event_class->id = id;
114 bt_property_uint_init(&event_class->log_level,
115 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE, 0);
116
117 ret = bt_object_pool_initialize(&event_class->event_pool,
118 (bt_object_pool_new_object_func) bt_event_new,
119 (bt_object_pool_destroy_object_func) free_event,
120 event_class);
121 if (ret) {
122 BT_LIB_LOGE_APPEND_CAUSE(
123 "Failed to initialize event pool: ret=%d",
124 ret);
125 goto error;
126 }
127
128 bt_object_set_parent(&event_class->base, &stream_class->base);
129 g_ptr_array_add(stream_class->event_classes, event_class);
130 bt_stream_class_freeze(stream_class);
131 BT_LIB_LOGD("Created event class object: %!+E", event_class);
132 goto end;
133
134 error:
135 BT_OBJECT_PUT_REF_AND_RESET(event_class);
136
137 end:
138 return event_class;
139 }
140
141 BT_EXPORT
142 struct bt_event_class *bt_event_class_create(
143 struct bt_stream_class *stream_class)
144 {
145 BT_ASSERT_PRE_NO_ERROR();
146 BT_ASSERT_PRE_SC_NON_NULL(stream_class);
147 BT_ASSERT_PRE(
148 "stream-class-automatically-assigns-event-class-ids",
149 stream_class->assigns_automatic_event_class_id,
150 "Stream class does not automatically assigns event class IDs: "
151 "%![sc-]+S", stream_class);
152 return create_event_class_with_id(stream_class,
153 (uint64_t) stream_class->event_classes->len);
154 }
155
156 BT_EXPORT
157 struct bt_event_class *bt_event_class_create_with_id(
158 struct bt_stream_class *stream_class, uint64_t id)
159 {
160 BT_ASSERT_PRE_NO_ERROR();
161 BT_ASSERT_PRE(
162 "stream-class-does-not-automatically-assigns-event-class-ids",
163 !stream_class->assigns_automatic_event_class_id,
164 "Stream class automatically assigns event class IDs: "
165 "%![sc-]+S", stream_class);
166 return create_event_class_with_id(stream_class, id);
167 }
168
169 BT_EXPORT
170 const char *bt_event_class_get_namespace(const struct bt_event_class *event_class)
171 {
172 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
173 BT_ASSERT_PRE_EC_MIP_VERSION_GE(event_class, 1);
174 return event_class->ns;
175 }
176
177 BT_EXPORT
178 enum bt_event_class_set_namespace_status bt_event_class_set_namespace(
179 struct bt_event_class *event_class, const char *ns)
180 {
181 BT_ASSERT_PRE_NO_ERROR();
182 BT_ASSERT_PRE_EC_NON_NULL(event_class);
183 BT_ASSERT_PRE_EC_MIP_VERSION_GE(event_class, 1);
184 BT_ASSERT_PRE_NAMESPACE_NON_NULL(ns);
185 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
186 g_free(event_class->ns);
187 event_class->ns = g_strdup(ns);
188 BT_LIB_LOGD("Set event class's namespace: %!+E", event_class);
189 return BT_FUNC_STATUS_OK;
190 }
191
192 BT_EXPORT
193 const char *bt_event_class_get_name(const struct bt_event_class *event_class)
194 {
195 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
196 return event_class->name;
197 }
198
199 BT_EXPORT
200 enum bt_event_class_set_name_status bt_event_class_set_name(
201 struct bt_event_class *event_class, const char *name)
202 {
203 BT_ASSERT_PRE_NO_ERROR();
204 BT_ASSERT_PRE_EC_NON_NULL(event_class);
205 BT_ASSERT_PRE_NAME_NON_NULL(name);
206 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
207 g_free(event_class->name);
208 event_class->name = g_strdup(name);
209 BT_LIB_LOGD("Set event class's name: %!+E", event_class);
210 return BT_FUNC_STATUS_OK;
211 }
212
213 BT_EXPORT
214 const char *bt_event_class_get_uid(const struct bt_event_class *event_class)
215 {
216 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
217 BT_ASSERT_PRE_EC_MIP_VERSION_GE(event_class, 1);
218 return event_class->uid;
219 }
220
221 BT_EXPORT
222 enum bt_event_class_set_uid_status bt_event_class_set_uid(
223 struct bt_event_class *event_class, const char *uid)
224 {
225 BT_ASSERT_PRE_NO_ERROR();
226 BT_ASSERT_PRE_EC_NON_NULL(event_class);
227 BT_ASSERT_PRE_EC_MIP_VERSION_GE(event_class, 1);
228 BT_ASSERT_PRE_NAME_NON_NULL(uid);
229 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
230 g_free(event_class->uid);
231 event_class->uid = g_strdup(uid);
232 BT_LIB_LOGD("Set event class's UID: %!+E", event_class);
233 return BT_FUNC_STATUS_OK;
234 }
235
236 BT_EXPORT
237 uint64_t bt_event_class_get_id(const struct bt_event_class *event_class)
238 {
239 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
240 return event_class->id;
241 }
242
243 BT_EXPORT
244 enum bt_property_availability bt_event_class_get_log_level(
245 const struct bt_event_class *event_class,
246 enum bt_event_class_log_level *log_level)
247 {
248 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
249 BT_ASSERT_PRE_DEV_NON_NULL("log-level-output", log_level,
250 "Log level (output)");
251 *log_level = (enum bt_event_class_log_level)
252 event_class->log_level.value;
253 return event_class->log_level.base.avail;
254 }
255
256 BT_EXPORT
257 void bt_event_class_set_log_level(
258 struct bt_event_class *event_class,
259 enum bt_event_class_log_level log_level)
260 {
261 BT_ASSERT_PRE_EC_NON_NULL(event_class);
262 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
263 bt_property_uint_set(&event_class->log_level,
264 (uint64_t) log_level);
265 BT_LIB_LOGD("Set event class's log level: %!+E", event_class);
266 }
267
268 BT_EXPORT
269 const char *bt_event_class_get_emf_uri(const struct bt_event_class *event_class)
270 {
271 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
272 return event_class->emf_uri;
273 }
274
275 BT_EXPORT
276 enum bt_event_class_set_emf_uri_status bt_event_class_set_emf_uri(
277 struct bt_event_class *event_class,
278 const char *emf_uri)
279 {
280 BT_ASSERT_PRE_NO_ERROR();
281 BT_ASSERT_PRE_EC_NON_NULL(event_class);
282 BT_ASSERT_PRE_NON_NULL("emf-uri", emf_uri, "EMF URI");
283 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
284 g_free(event_class->emf_uri);
285 event_class->emf_uri = g_strdup(emf_uri);
286 BT_LIB_LOGD("Set event class's EMF URI: %!+E", event_class);
287 return BT_FUNC_STATUS_OK;
288 }
289
290 BT_EXPORT
291 struct bt_stream_class *bt_event_class_borrow_stream_class(
292 struct bt_event_class *event_class)
293 {
294 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
295 return bt_event_class_borrow_stream_class_inline(event_class);
296 }
297
298 BT_EXPORT
299 const struct bt_stream_class *
300 bt_event_class_borrow_stream_class_const(
301 const struct bt_event_class *event_class)
302 {
303 return bt_event_class_borrow_stream_class((void *) event_class);
304 }
305
306 BT_EXPORT
307 const struct bt_field_class *
308 bt_event_class_borrow_specific_context_field_class_const(
309 const struct bt_event_class *event_class)
310 {
311 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
312 return event_class->specific_context_fc;
313 }
314
315 BT_EXPORT
316 struct bt_field_class *
317 bt_event_class_borrow_specific_context_field_class(
318 struct bt_event_class *event_class)
319 {
320 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
321 return event_class->specific_context_fc;
322 }
323
324 BT_EXPORT
325 enum bt_event_class_set_field_class_status
326 bt_event_class_set_specific_context_field_class(
327 struct bt_event_class *event_class,
328 struct bt_field_class *field_class)
329 {
330 enum bt_event_class_set_field_class_status status;
331 enum bt_resolve_field_xref_status resolve_status;
332 struct bt_stream_class *stream_class;
333 struct bt_resolve_field_xref_context resolve_ctx = {
334 .packet_context = NULL,
335 .event_common_context = NULL,
336 .event_specific_context = field_class,
337 .event_payload = NULL,
338 };
339
340 BT_ASSERT_PRE_NO_ERROR();
341 BT_ASSERT_PRE_EC_NON_NULL(event_class);
342 BT_ASSERT_PRE_FC_NON_NULL(field_class);
343 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
344 BT_ASSERT_PRE_FC_IS_STRUCT("specific-context", field_class,
345 "Specific context field class");
346 stream_class = bt_event_class_borrow_stream_class_inline(
347 event_class);
348 resolve_ctx.packet_context = stream_class->packet_context_fc;
349 resolve_ctx.event_common_context =
350 stream_class->event_common_context_fc;
351
352 resolve_status = bt_resolve_field_paths(field_class, &resolve_ctx, __func__);
353 if (resolve_status != BT_RESOLVE_FIELD_XREF_STATUS_OK) {
354 status = (int) resolve_status;
355 goto end;
356 }
357
358 bt_field_class_make_part_of_trace_class(field_class);
359 bt_object_put_ref(event_class->specific_context_fc);
360 event_class->specific_context_fc = field_class;
361 bt_object_get_ref_no_null_check(event_class->specific_context_fc);
362 bt_field_class_freeze(field_class);
363 BT_LIB_LOGD("Set event class's specific context field class: %!+E",
364 event_class);
365
366 status = BT_EVENT_CLASS_SET_FIELD_CLASS_STATUS_OK;
367
368 end:
369 return status;
370 }
371
372 BT_EXPORT
373 const struct bt_field_class *bt_event_class_borrow_payload_field_class_const(
374 const struct bt_event_class *event_class)
375 {
376 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
377 return event_class->payload_fc;
378 }
379
380 BT_EXPORT
381 struct bt_field_class *bt_event_class_borrow_payload_field_class(
382 struct bt_event_class *event_class)
383 {
384 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
385 return event_class->payload_fc;
386 }
387
388 BT_EXPORT
389 enum bt_event_class_set_field_class_status
390 bt_event_class_set_payload_field_class(
391 struct bt_event_class *event_class,
392 struct bt_field_class *field_class)
393 {
394 enum bt_event_class_set_field_class_status status;
395 enum bt_resolve_field_xref_status resolve_status;
396 struct bt_stream_class *stream_class;
397 struct bt_resolve_field_xref_context resolve_ctx = {
398 .packet_context = NULL,
399 .event_common_context = NULL,
400 .event_specific_context = NULL,
401 .event_payload = field_class,
402 };
403
404 BT_ASSERT_PRE_NO_ERROR();
405 BT_ASSERT_PRE_EC_NON_NULL(event_class);
406 BT_ASSERT_PRE_FC_NON_NULL(field_class);
407 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
408 BT_ASSERT_PRE_FC_IS_STRUCT("payload", field_class, "Payload field class");
409 stream_class = bt_event_class_borrow_stream_class_inline(
410 event_class);
411 resolve_ctx.packet_context = stream_class->packet_context_fc;
412 resolve_ctx.event_common_context =
413 stream_class->event_common_context_fc;
414 resolve_ctx.event_specific_context = event_class->specific_context_fc;
415
416 resolve_status = bt_resolve_field_paths(field_class, &resolve_ctx, __func__);
417 if (resolve_status != BT_RESOLVE_FIELD_XREF_STATUS_OK) {
418 status = (int) resolve_status;
419 goto end;
420 }
421
422 bt_field_class_make_part_of_trace_class(field_class);
423 bt_object_put_ref(event_class->payload_fc);
424 event_class->payload_fc = field_class;
425 bt_object_get_ref_no_null_check(event_class->payload_fc);
426 bt_field_class_freeze(field_class);
427 BT_LIB_LOGD("Set event class's payload field class: %!+E", event_class);
428
429 status = BT_EVENT_CLASS_SET_FIELD_CLASS_STATUS_OK;
430
431 end:
432 return status;
433 }
434
435 void _bt_event_class_freeze(const struct bt_event_class *event_class)
436 {
437 /* The field classes are already frozen */
438 BT_ASSERT(event_class);
439 BT_LIB_LOGD("Freezing event class's user attributes: %!+v",
440 event_class->user_attributes);
441 bt_value_freeze(event_class->user_attributes);
442 BT_LIB_LOGD("Freezing event class: %!+E", event_class);
443 ((struct bt_event_class *) event_class)->frozen = true;
444 }
445
446 BT_EXPORT
447 const struct bt_value *bt_event_class_borrow_user_attributes_const(
448 const struct bt_event_class *event_class)
449 {
450 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class);
451 return event_class->user_attributes;
452 }
453
454 BT_EXPORT
455 struct bt_value *bt_event_class_borrow_user_attributes(
456 struct bt_event_class *event_class)
457 {
458 return (void *) bt_event_class_borrow_user_attributes_const(
459 (void *) event_class);
460 }
461
462 BT_EXPORT
463 void bt_event_class_set_user_attributes(
464 struct bt_event_class *event_class,
465 const struct bt_value *user_attributes)
466 {
467 BT_ASSERT_PRE_EC_NON_NULL(event_class);
468 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class);
469 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes);
470 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes);
471 bt_object_put_ref_no_null_check(event_class->user_attributes);
472 event_class->user_attributes = (void *) user_attributes;
473 bt_object_get_ref_no_null_check(event_class->user_attributes);
474 }
475
476 BT_EXPORT
477 void bt_event_class_get_ref(const struct bt_event_class *event_class)
478 {
479 bt_object_get_ref(event_class);
480 }
481
482 BT_EXPORT
483 void bt_event_class_put_ref(const struct bt_event_class *event_class)
484 {
485 bt_object_put_ref(event_class);
486 }
This page took 0.038967 seconds and 5 git commands to generate.