2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #define BT_LOG_TAG "LIB/EVENT-CLASS"
9 #include "lib/logging.h"
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"
24 #include "event-class.h"
26 #include "field-class.h"
27 #include "resolve-field-path.h"
28 #include "stream-class.h"
29 #include "lib/func-status.h"
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))
37 void destroy_event_class(struct bt_object
*obj
)
39 struct bt_event_class
*event_class
= (void *) obj
;
41 BT_LIB_LOGD("Destroying event class: %!+E", event_class
);
42 BT_OBJECT_PUT_REF_AND_RESET(event_class
->user_attributes
);
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
);
57 void free_event(struct bt_event
*event
,
58 struct bt_event_class
*event_class
__attribute__((unused
)))
60 bt_event_destroy(event
);
64 bool event_class_id_is_unique(const struct bt_stream_class
*stream_class
,
68 bool is_unique
= true;
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
];
85 struct bt_event_class
*create_event_class_with_id(
86 struct bt_stream_class
*stream_class
, uint64_t id
)
89 struct bt_event_class
*event_class
;
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
,
96 BT_LIB_LOGD("Creating event class object: %![sc-]+S, id=%" PRIu64
,
98 event_class
= g_new0(struct bt_event_class
, 1);
100 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one event class.");
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.");
113 event_class
->id
= id
;
114 bt_property_uint_init(&event_class
->log_level
,
115 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
, 0);
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
,
122 BT_LIB_LOGE_APPEND_CAUSE(
123 "Failed to initialize event pool: ret=%d",
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
);
135 BT_OBJECT_PUT_REF_AND_RESET(event_class
);
142 struct bt_event_class
*bt_event_class_create(
143 struct bt_stream_class
*stream_class
)
145 BT_ASSERT_PRE_NO_ERROR();
146 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
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
);
157 struct bt_event_class
*bt_event_class_create_with_id(
158 struct bt_stream_class
*stream_class
, uint64_t id
)
160 BT_ASSERT_PRE_NO_ERROR();
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
);
170 const char *bt_event_class_get_namespace(const struct bt_event_class
*event_class
)
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
;
178 enum bt_event_class_set_namespace_status
bt_event_class_set_namespace(
179 struct bt_event_class
*event_class
, const char *ns
)
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
;
193 const char *bt_event_class_get_name(const struct bt_event_class
*event_class
)
195 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
196 return event_class
->name
;
200 enum bt_event_class_set_name_status
bt_event_class_set_name(
201 struct bt_event_class
*event_class
, const char *name
)
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
;
214 const char *bt_event_class_get_uid(const struct bt_event_class
*event_class
)
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
;
222 enum bt_event_class_set_uid_status
bt_event_class_set_uid(
223 struct bt_event_class
*event_class
, const char *uid
)
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
;
237 uint64_t bt_event_class_get_id(const struct bt_event_class
*event_class
)
239 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
240 return event_class
->id
;
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
)
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
;
257 void bt_event_class_set_log_level(
258 struct bt_event_class
*event_class
,
259 enum bt_event_class_log_level log_level
)
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
);
269 const char *bt_event_class_get_emf_uri(const struct bt_event_class
*event_class
)
271 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
272 return event_class
->emf_uri
;
276 enum bt_event_class_set_emf_uri_status
bt_event_class_set_emf_uri(
277 struct bt_event_class
*event_class
,
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
;
291 struct bt_stream_class
*bt_event_class_borrow_stream_class(
292 struct bt_event_class
*event_class
)
294 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
295 return bt_event_class_borrow_stream_class_inline(event_class
);
299 const struct bt_stream_class
*
300 bt_event_class_borrow_stream_class_const(
301 const struct bt_event_class
*event_class
)
303 return bt_event_class_borrow_stream_class((void *) event_class
);
307 const struct bt_field_class
*
308 bt_event_class_borrow_specific_context_field_class_const(
309 const struct bt_event_class
*event_class
)
311 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
312 return event_class
->specific_context_fc
;
316 struct bt_field_class
*
317 bt_event_class_borrow_specific_context_field_class(
318 struct bt_event_class
*event_class
)
320 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
321 return event_class
->specific_context_fc
;
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
)
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
,
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(
348 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
349 resolve_ctx
.event_common_context
=
350 stream_class
->event_common_context_fc
;
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
;
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",
366 status
= BT_EVENT_CLASS_SET_FIELD_CLASS_STATUS_OK
;
373 const struct bt_field_class
*bt_event_class_borrow_payload_field_class_const(
374 const struct bt_event_class
*event_class
)
376 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
377 return event_class
->payload_fc
;
381 struct bt_field_class
*bt_event_class_borrow_payload_field_class(
382 struct bt_event_class
*event_class
)
384 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
385 return event_class
->payload_fc
;
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
)
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
,
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(
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
;
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
;
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
);
429 status
= BT_EVENT_CLASS_SET_FIELD_CLASS_STATUS_OK
;
435 void _bt_event_class_freeze(const struct bt_event_class
*event_class
)
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;
447 const struct bt_value
*bt_event_class_borrow_user_attributes_const(
448 const struct bt_event_class
*event_class
)
450 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
451 return event_class
->user_attributes
;
455 struct bt_value
*bt_event_class_borrow_user_attributes(
456 struct bt_event_class
*event_class
)
458 return (void *) bt_event_class_borrow_user_attributes_const(
459 (void *) event_class
);
463 void bt_event_class_set_user_attributes(
464 struct bt_event_class
*event_class
,
465 const struct bt_value
*user_attributes
)
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
);
477 void bt_event_class_get_ref(const struct bt_event_class
*event_class
)
479 bt_object_get_ref(event_class
);
483 void bt_event_class_put_ref(const struct bt_event_class
*event_class
)
485 bt_object_put_ref(event_class
);