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 if (event_class
->name
.str
) {
45 g_string_free(event_class
->name
.str
, TRUE
);
46 event_class
->name
.str
= NULL
;
49 if (event_class
->emf_uri
.str
) {
50 g_string_free(event_class
->emf_uri
.str
, TRUE
);
51 event_class
->emf_uri
.str
= NULL
;
54 BT_LOGD_STR("Putting context field class.");
55 BT_OBJECT_PUT_REF_AND_RESET(event_class
->specific_context_fc
);
56 BT_LOGD_STR("Putting payload field class.");
57 BT_OBJECT_PUT_REF_AND_RESET(event_class
->payload_fc
);
58 bt_object_pool_finalize(&event_class
->event_pool
);
63 void free_event(struct bt_event
*event
,
64 struct bt_event_class
*event_class
__attribute__((unused
)))
66 bt_event_destroy(event
);
70 bool event_class_id_is_unique(const struct bt_stream_class
*stream_class
,
74 bool is_unique
= true;
76 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
77 const struct bt_event_class
*ec
=
78 stream_class
->event_classes
->pdata
[i
];
91 struct bt_event_class
*create_event_class_with_id(
92 struct bt_stream_class
*stream_class
, uint64_t id
)
95 struct bt_event_class
*event_class
;
97 BT_ASSERT(stream_class
);
98 BT_ASSERT_PRE("event-class-id-is-unique",
99 event_class_id_is_unique(stream_class
, id
),
100 "Duplicate event class ID: %![sc-]+S, id=%" PRIu64
,
102 BT_LIB_LOGD("Creating event class object: %![sc-]+S, id=%" PRIu64
,
104 event_class
= g_new0(struct bt_event_class
, 1);
106 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one event class.");
110 bt_object_init_shared_with_parent(&event_class
->base
,
111 destroy_event_class
);
112 event_class
->user_attributes
= bt_value_map_create();
113 if (!event_class
->user_attributes
) {
114 BT_LIB_LOGE_APPEND_CAUSE(
115 "Failed to create a map value object.");
119 event_class
->id
= id
;
120 bt_property_uint_init(&event_class
->log_level
,
121 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
, 0);
122 event_class
->name
.str
= g_string_new(NULL
);
123 if (!event_class
->name
.str
) {
124 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
128 event_class
->emf_uri
.str
= g_string_new(NULL
);
129 if (!event_class
->emf_uri
.str
) {
130 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
134 ret
= bt_object_pool_initialize(&event_class
->event_pool
,
135 (bt_object_pool_new_object_func
) bt_event_new
,
136 (bt_object_pool_destroy_object_func
) free_event
,
139 BT_LIB_LOGE_APPEND_CAUSE(
140 "Failed to initialize event pool: ret=%d",
145 bt_object_set_parent(&event_class
->base
, &stream_class
->base
);
146 g_ptr_array_add(stream_class
->event_classes
, event_class
);
147 bt_stream_class_freeze(stream_class
);
148 BT_LIB_LOGD("Created event class object: %!+E", event_class
);
152 BT_OBJECT_PUT_REF_AND_RESET(event_class
);
159 struct bt_event_class
*bt_event_class_create(
160 struct bt_stream_class
*stream_class
)
162 BT_ASSERT_PRE_NO_ERROR();
163 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
165 "stream-class-automatically-assigns-event-class-ids",
166 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
);
174 struct bt_event_class
*bt_event_class_create_with_id(
175 struct bt_stream_class
*stream_class
, uint64_t id
)
177 BT_ASSERT_PRE_NO_ERROR();
179 "stream-class-does-not-automatically-assigns-event-class-ids",
180 !stream_class
->assigns_automatic_event_class_id
,
181 "Stream class automatically assigns event class IDs: "
182 "%![sc-]+S", stream_class
);
183 return create_event_class_with_id(stream_class
, id
);
187 const char *bt_event_class_get_name(const struct bt_event_class
*event_class
)
189 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
190 return event_class
->name
.value
;
194 enum bt_event_class_set_name_status
bt_event_class_set_name(
195 struct bt_event_class
*event_class
, const char *name
)
197 BT_ASSERT_PRE_NO_ERROR();
198 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
199 BT_ASSERT_PRE_NAME_NON_NULL(name
);
200 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
201 g_string_assign(event_class
->name
.str
, name
);
202 event_class
->name
.value
= event_class
->name
.str
->str
;
203 BT_LIB_LOGD("Set event class's name: %!+E", event_class
);
204 return BT_FUNC_STATUS_OK
;
208 uint64_t bt_event_class_get_id(const struct bt_event_class
*event_class
)
210 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
211 return event_class
->id
;
215 enum bt_property_availability
bt_event_class_get_log_level(
216 const struct bt_event_class
*event_class
,
217 enum bt_event_class_log_level
*log_level
)
219 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
220 BT_ASSERT_PRE_DEV_NON_NULL("log-level-output", log_level
,
221 "Log level (output)");
222 *log_level
= (enum bt_event_class_log_level
)
223 event_class
->log_level
.value
;
224 return event_class
->log_level
.base
.avail
;
228 void bt_event_class_set_log_level(
229 struct bt_event_class
*event_class
,
230 enum bt_event_class_log_level log_level
)
232 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
233 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
234 bt_property_uint_set(&event_class
->log_level
,
235 (uint64_t) log_level
);
236 BT_LIB_LOGD("Set event class's log level: %!+E", event_class
);
240 const char *bt_event_class_get_emf_uri(const struct bt_event_class
*event_class
)
242 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
243 return event_class
->emf_uri
.value
;
247 enum bt_event_class_set_emf_uri_status
bt_event_class_set_emf_uri(
248 struct bt_event_class
*event_class
,
251 BT_ASSERT_PRE_NO_ERROR();
252 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
253 BT_ASSERT_PRE_NON_NULL("emf-uri", emf_uri
, "EMF URI");
254 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
255 g_string_assign(event_class
->emf_uri
.str
, emf_uri
);
256 event_class
->emf_uri
.value
= event_class
->emf_uri
.str
->str
;
257 BT_LIB_LOGD("Set event class's EMF URI: %!+E", event_class
);
258 return BT_FUNC_STATUS_OK
;
262 struct bt_stream_class
*bt_event_class_borrow_stream_class(
263 struct bt_event_class
*event_class
)
265 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
266 return bt_event_class_borrow_stream_class_inline(event_class
);
270 const struct bt_stream_class
*
271 bt_event_class_borrow_stream_class_const(
272 const struct bt_event_class
*event_class
)
274 return bt_event_class_borrow_stream_class((void *) event_class
);
278 const struct bt_field_class
*
279 bt_event_class_borrow_specific_context_field_class_const(
280 const struct bt_event_class
*event_class
)
282 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
283 return event_class
->specific_context_fc
;
287 struct bt_field_class
*
288 bt_event_class_borrow_specific_context_field_class(
289 struct bt_event_class
*event_class
)
291 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
292 return event_class
->specific_context_fc
;
296 enum bt_event_class_set_field_class_status
297 bt_event_class_set_specific_context_field_class(
298 struct bt_event_class
*event_class
,
299 struct bt_field_class
*field_class
)
302 struct bt_stream_class
*stream_class
;
303 struct bt_resolve_field_path_context resolve_ctx
= {
304 .packet_context
= NULL
,
305 .event_common_context
= NULL
,
306 .event_specific_context
= field_class
,
307 .event_payload
= NULL
,
310 BT_ASSERT_PRE_NO_ERROR();
311 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
312 BT_ASSERT_PRE_FC_NON_NULL(field_class
);
313 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
314 BT_ASSERT_PRE_FC_IS_STRUCT("specific-context", field_class
,
315 "Specific context field class");
316 stream_class
= bt_event_class_borrow_stream_class_inline(
318 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
319 resolve_ctx
.event_common_context
=
320 stream_class
->event_common_context_fc
;
322 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
, __func__
);
325 * This is the only reason for which
326 * bt_resolve_field_paths() can fail: anything else
327 * would be because a precondition is not satisfied.
329 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
333 bt_field_class_make_part_of_trace_class(field_class
);
334 bt_object_put_ref(event_class
->specific_context_fc
);
335 event_class
->specific_context_fc
= field_class
;
336 bt_object_get_ref_no_null_check(event_class
->specific_context_fc
);
337 bt_field_class_freeze(field_class
);
338 BT_LIB_LOGD("Set event class's specific context field class: %!+E",
346 const struct bt_field_class
*bt_event_class_borrow_payload_field_class_const(
347 const struct bt_event_class
*event_class
)
349 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
350 return event_class
->payload_fc
;
354 struct bt_field_class
*bt_event_class_borrow_payload_field_class(
355 struct bt_event_class
*event_class
)
357 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
358 return event_class
->payload_fc
;
362 enum bt_event_class_set_field_class_status
363 bt_event_class_set_payload_field_class(
364 struct bt_event_class
*event_class
,
365 struct bt_field_class
*field_class
)
368 struct bt_stream_class
*stream_class
;
369 struct bt_resolve_field_path_context resolve_ctx
= {
370 .packet_context
= NULL
,
371 .event_common_context
= NULL
,
372 .event_specific_context
= NULL
,
373 .event_payload
= field_class
,
376 BT_ASSERT_PRE_NO_ERROR();
377 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
378 BT_ASSERT_PRE_FC_NON_NULL(field_class
);
379 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
380 BT_ASSERT_PRE_FC_IS_STRUCT("payload", field_class
, "Payload field class");
381 stream_class
= bt_event_class_borrow_stream_class_inline(
383 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
384 resolve_ctx
.event_common_context
=
385 stream_class
->event_common_context_fc
;
386 resolve_ctx
.event_specific_context
= event_class
->specific_context_fc
;
388 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
, __func__
);
391 * This is the only reason for which
392 * bt_resolve_field_paths() can fail: anything else
393 * would be because a precondition is not satisfied.
395 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
399 bt_field_class_make_part_of_trace_class(field_class
);
400 bt_object_put_ref(event_class
->payload_fc
);
401 event_class
->payload_fc
= field_class
;
402 bt_object_get_ref_no_null_check(event_class
->payload_fc
);
403 bt_field_class_freeze(field_class
);
404 BT_LIB_LOGD("Set event class's payload field class: %!+E", event_class
);
410 void _bt_event_class_freeze(const struct bt_event_class
*event_class
)
412 /* The field classes are already frozen */
413 BT_ASSERT(event_class
);
414 BT_LIB_LOGD("Freezing event class's user attributes: %!+v",
415 event_class
->user_attributes
);
416 bt_value_freeze(event_class
->user_attributes
);
417 BT_LIB_LOGD("Freezing event class: %!+E", event_class
);
418 ((struct bt_event_class
*) event_class
)->frozen
= true;
422 const struct bt_value
*bt_event_class_borrow_user_attributes_const(
423 const struct bt_event_class
*event_class
)
425 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
426 return event_class
->user_attributes
;
430 struct bt_value
*bt_event_class_borrow_user_attributes(
431 struct bt_event_class
*event_class
)
433 return (void *) bt_event_class_borrow_user_attributes_const(
434 (void *) event_class
);
438 void bt_event_class_set_user_attributes(
439 struct bt_event_class
*event_class
,
440 const struct bt_value
*user_attributes
)
442 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
443 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
444 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
445 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
446 bt_object_put_ref_no_null_check(event_class
->user_attributes
);
447 event_class
->user_attributes
= (void *) user_attributes
;
448 bt_object_get_ref_no_null_check(event_class
->user_attributes
);
452 void bt_event_class_get_ref(const struct bt_event_class
*event_class
)
454 bt_object_get_ref(event_class
);
458 void bt_event_class_put_ref(const struct bt_event_class
*event_class
)
460 bt_object_put_ref(event_class
);