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
->name
);
45 g_free(event_class
->emf_uri
);
46 BT_LOGD_STR("Putting context field class.");
47 BT_OBJECT_PUT_REF_AND_RESET(event_class
->specific_context_fc
);
48 BT_LOGD_STR("Putting payload field class.");
49 BT_OBJECT_PUT_REF_AND_RESET(event_class
->payload_fc
);
50 bt_object_pool_finalize(&event_class
->event_pool
);
55 void free_event(struct bt_event
*event
,
56 struct bt_event_class
*event_class
__attribute__((unused
)))
58 bt_event_destroy(event
);
62 bool event_class_id_is_unique(const struct bt_stream_class
*stream_class
,
66 bool is_unique
= true;
68 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
69 const struct bt_event_class
*ec
=
70 stream_class
->event_classes
->pdata
[i
];
83 struct bt_event_class
*create_event_class_with_id(
84 struct bt_stream_class
*stream_class
, uint64_t id
)
87 struct bt_event_class
*event_class
;
89 BT_ASSERT(stream_class
);
90 BT_ASSERT_PRE("event-class-id-is-unique",
91 event_class_id_is_unique(stream_class
, id
),
92 "Duplicate event class ID: %![sc-]+S, id=%" PRIu64
,
94 BT_LIB_LOGD("Creating event class object: %![sc-]+S, id=%" PRIu64
,
96 event_class
= g_new0(struct bt_event_class
, 1);
98 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one event class.");
102 bt_object_init_shared_with_parent(&event_class
->base
,
103 destroy_event_class
);
104 event_class
->user_attributes
= bt_value_map_create();
105 if (!event_class
->user_attributes
) {
106 BT_LIB_LOGE_APPEND_CAUSE(
107 "Failed to create a map value object.");
111 event_class
->id
= id
;
112 bt_property_uint_init(&event_class
->log_level
,
113 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
, 0);
115 ret
= bt_object_pool_initialize(&event_class
->event_pool
,
116 (bt_object_pool_new_object_func
) bt_event_new
,
117 (bt_object_pool_destroy_object_func
) free_event
,
120 BT_LIB_LOGE_APPEND_CAUSE(
121 "Failed to initialize event pool: ret=%d",
126 bt_object_set_parent(&event_class
->base
, &stream_class
->base
);
127 g_ptr_array_add(stream_class
->event_classes
, event_class
);
128 bt_stream_class_freeze(stream_class
);
129 BT_LIB_LOGD("Created event class object: %!+E", event_class
);
133 BT_OBJECT_PUT_REF_AND_RESET(event_class
);
140 struct bt_event_class
*bt_event_class_create(
141 struct bt_stream_class
*stream_class
)
143 BT_ASSERT_PRE_NO_ERROR();
144 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
146 "stream-class-automatically-assigns-event-class-ids",
147 stream_class
->assigns_automatic_event_class_id
,
148 "Stream class does not automatically assigns event class IDs: "
149 "%![sc-]+S", stream_class
);
150 return create_event_class_with_id(stream_class
,
151 (uint64_t) stream_class
->event_classes
->len
);
155 struct bt_event_class
*bt_event_class_create_with_id(
156 struct bt_stream_class
*stream_class
, uint64_t id
)
158 BT_ASSERT_PRE_NO_ERROR();
160 "stream-class-does-not-automatically-assigns-event-class-ids",
161 !stream_class
->assigns_automatic_event_class_id
,
162 "Stream class automatically assigns event class IDs: "
163 "%![sc-]+S", stream_class
);
164 return create_event_class_with_id(stream_class
, id
);
168 const char *bt_event_class_get_name(const struct bt_event_class
*event_class
)
170 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
171 return event_class
->name
;
175 enum bt_event_class_set_name_status
bt_event_class_set_name(
176 struct bt_event_class
*event_class
, const char *name
)
178 BT_ASSERT_PRE_NO_ERROR();
179 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
180 BT_ASSERT_PRE_NAME_NON_NULL(name
);
181 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
182 g_free(event_class
->name
);
183 event_class
->name
= g_strdup(name
);
184 BT_LIB_LOGD("Set event class's name: %!+E", event_class
);
185 return BT_FUNC_STATUS_OK
;
189 uint64_t bt_event_class_get_id(const struct bt_event_class
*event_class
)
191 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
192 return event_class
->id
;
196 enum bt_property_availability
bt_event_class_get_log_level(
197 const struct bt_event_class
*event_class
,
198 enum bt_event_class_log_level
*log_level
)
200 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
201 BT_ASSERT_PRE_DEV_NON_NULL("log-level-output", log_level
,
202 "Log level (output)");
203 *log_level
= (enum bt_event_class_log_level
)
204 event_class
->log_level
.value
;
205 return event_class
->log_level
.base
.avail
;
209 void bt_event_class_set_log_level(
210 struct bt_event_class
*event_class
,
211 enum bt_event_class_log_level log_level
)
213 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
214 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
215 bt_property_uint_set(&event_class
->log_level
,
216 (uint64_t) log_level
);
217 BT_LIB_LOGD("Set event class's log level: %!+E", event_class
);
221 const char *bt_event_class_get_emf_uri(const struct bt_event_class
*event_class
)
223 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
224 return event_class
->emf_uri
;
228 enum bt_event_class_set_emf_uri_status
bt_event_class_set_emf_uri(
229 struct bt_event_class
*event_class
,
232 BT_ASSERT_PRE_NO_ERROR();
233 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
234 BT_ASSERT_PRE_NON_NULL("emf-uri", emf_uri
, "EMF URI");
235 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
236 g_free(event_class
->emf_uri
);
237 event_class
->emf_uri
= g_strdup(emf_uri
);
238 BT_LIB_LOGD("Set event class's EMF URI: %!+E", event_class
);
239 return BT_FUNC_STATUS_OK
;
243 struct bt_stream_class
*bt_event_class_borrow_stream_class(
244 struct bt_event_class
*event_class
)
246 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
247 return bt_event_class_borrow_stream_class_inline(event_class
);
251 const struct bt_stream_class
*
252 bt_event_class_borrow_stream_class_const(
253 const struct bt_event_class
*event_class
)
255 return bt_event_class_borrow_stream_class((void *) event_class
);
259 const struct bt_field_class
*
260 bt_event_class_borrow_specific_context_field_class_const(
261 const struct bt_event_class
*event_class
)
263 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
264 return event_class
->specific_context_fc
;
268 struct bt_field_class
*
269 bt_event_class_borrow_specific_context_field_class(
270 struct bt_event_class
*event_class
)
272 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
273 return event_class
->specific_context_fc
;
277 enum bt_event_class_set_field_class_status
278 bt_event_class_set_specific_context_field_class(
279 struct bt_event_class
*event_class
,
280 struct bt_field_class
*field_class
)
283 struct bt_stream_class
*stream_class
;
284 struct bt_resolve_field_path_context resolve_ctx
= {
285 .packet_context
= NULL
,
286 .event_common_context
= NULL
,
287 .event_specific_context
= field_class
,
288 .event_payload
= NULL
,
291 BT_ASSERT_PRE_NO_ERROR();
292 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
293 BT_ASSERT_PRE_FC_NON_NULL(field_class
);
294 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
295 BT_ASSERT_PRE_FC_IS_STRUCT("specific-context", field_class
,
296 "Specific context field class");
297 stream_class
= bt_event_class_borrow_stream_class_inline(
299 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
300 resolve_ctx
.event_common_context
=
301 stream_class
->event_common_context_fc
;
303 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
, __func__
);
306 * This is the only reason for which
307 * bt_resolve_field_paths() can fail: anything else
308 * would be because a precondition is not satisfied.
310 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
314 bt_field_class_make_part_of_trace_class(field_class
);
315 bt_object_put_ref(event_class
->specific_context_fc
);
316 event_class
->specific_context_fc
= field_class
;
317 bt_object_get_ref_no_null_check(event_class
->specific_context_fc
);
318 bt_field_class_freeze(field_class
);
319 BT_LIB_LOGD("Set event class's specific context field class: %!+E",
327 const struct bt_field_class
*bt_event_class_borrow_payload_field_class_const(
328 const struct bt_event_class
*event_class
)
330 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
331 return event_class
->payload_fc
;
335 struct bt_field_class
*bt_event_class_borrow_payload_field_class(
336 struct bt_event_class
*event_class
)
338 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
339 return event_class
->payload_fc
;
343 enum bt_event_class_set_field_class_status
344 bt_event_class_set_payload_field_class(
345 struct bt_event_class
*event_class
,
346 struct bt_field_class
*field_class
)
349 struct bt_stream_class
*stream_class
;
350 struct bt_resolve_field_path_context resolve_ctx
= {
351 .packet_context
= NULL
,
352 .event_common_context
= NULL
,
353 .event_specific_context
= NULL
,
354 .event_payload
= field_class
,
357 BT_ASSERT_PRE_NO_ERROR();
358 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
359 BT_ASSERT_PRE_FC_NON_NULL(field_class
);
360 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
361 BT_ASSERT_PRE_FC_IS_STRUCT("payload", field_class
, "Payload field class");
362 stream_class
= bt_event_class_borrow_stream_class_inline(
364 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
365 resolve_ctx
.event_common_context
=
366 stream_class
->event_common_context_fc
;
367 resolve_ctx
.event_specific_context
= event_class
->specific_context_fc
;
369 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
, __func__
);
372 * This is the only reason for which
373 * bt_resolve_field_paths() can fail: anything else
374 * would be because a precondition is not satisfied.
376 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
380 bt_field_class_make_part_of_trace_class(field_class
);
381 bt_object_put_ref(event_class
->payload_fc
);
382 event_class
->payload_fc
= field_class
;
383 bt_object_get_ref_no_null_check(event_class
->payload_fc
);
384 bt_field_class_freeze(field_class
);
385 BT_LIB_LOGD("Set event class's payload field class: %!+E", event_class
);
391 void _bt_event_class_freeze(const struct bt_event_class
*event_class
)
393 /* The field classes are already frozen */
394 BT_ASSERT(event_class
);
395 BT_LIB_LOGD("Freezing event class's user attributes: %!+v",
396 event_class
->user_attributes
);
397 bt_value_freeze(event_class
->user_attributes
);
398 BT_LIB_LOGD("Freezing event class: %!+E", event_class
);
399 ((struct bt_event_class
*) event_class
)->frozen
= true;
403 const struct bt_value
*bt_event_class_borrow_user_attributes_const(
404 const struct bt_event_class
*event_class
)
406 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
407 return event_class
->user_attributes
;
411 struct bt_value
*bt_event_class_borrow_user_attributes(
412 struct bt_event_class
*event_class
)
414 return (void *) bt_event_class_borrow_user_attributes_const(
415 (void *) event_class
);
419 void bt_event_class_set_user_attributes(
420 struct bt_event_class
*event_class
,
421 const struct bt_value
*user_attributes
)
423 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
424 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
425 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
426 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
427 bt_object_put_ref_no_null_check(event_class
->user_attributes
);
428 event_class
->user_attributes
= (void *) user_attributes
;
429 bt_object_get_ref_no_null_check(event_class
->user_attributes
);
433 void bt_event_class_get_ref(const struct bt_event_class
*event_class
)
435 bt_object_get_ref(event_class
);
439 void bt_event_class_put_ref(const struct bt_event_class
*event_class
)
441 bt_object_put_ref(event_class
);