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 "attributes.h"
25 #include "clock-snapshot.h"
26 #include "event-class.h"
28 #include "field-class.h"
30 #include "resolve-field-path.h"
31 #include "stream-class.h"
34 #include "lib/func-status.h"
36 #define BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(_ec) \
37 BT_ASSERT_PRE_DEV_HOT("event-class", \
38 ((const struct bt_event_class *) (_ec)), \
39 "Event class", ": %!+E", (_ec))
42 void destroy_event_class(struct bt_object
*obj
)
44 struct bt_event_class
*event_class
= (void *) obj
;
46 BT_LIB_LOGD("Destroying event class: %!+E", event_class
);
47 BT_OBJECT_PUT_REF_AND_RESET(event_class
->user_attributes
);
49 if (event_class
->name
.str
) {
50 g_string_free(event_class
->name
.str
, TRUE
);
51 event_class
->name
.str
= NULL
;
54 if (event_class
->emf_uri
.str
) {
55 g_string_free(event_class
->emf_uri
.str
, TRUE
);
56 event_class
->emf_uri
.str
= NULL
;
59 BT_LOGD_STR("Putting context field class.");
60 BT_OBJECT_PUT_REF_AND_RESET(event_class
->specific_context_fc
);
61 BT_LOGD_STR("Putting payload field class.");
62 BT_OBJECT_PUT_REF_AND_RESET(event_class
->payload_fc
);
63 bt_object_pool_finalize(&event_class
->event_pool
);
68 void free_event(struct bt_event
*event
,
69 struct bt_event_class
*event_class
)
71 bt_event_destroy(event
);
75 bool event_class_id_is_unique(const struct bt_stream_class
*stream_class
,
79 bool is_unique
= true;
81 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
82 const struct bt_event_class
*ec
=
83 stream_class
->event_classes
->pdata
[i
];
96 struct bt_event_class
*create_event_class_with_id(
97 struct bt_stream_class
*stream_class
, uint64_t id
)
100 struct bt_event_class
*event_class
;
102 BT_ASSERT(stream_class
);
103 BT_ASSERT_PRE("event-class-id-is-unique",
104 event_class_id_is_unique(stream_class
, id
),
105 "Duplicate event class ID: %![sc-]+S, id=%" PRIu64
,
107 BT_LIB_LOGD("Creating event class object: %![sc-]+S, id=%" PRIu64
,
109 event_class
= g_new0(struct bt_event_class
, 1);
111 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one event class.");
115 bt_object_init_shared_with_parent(&event_class
->base
,
116 destroy_event_class
);
117 event_class
->user_attributes
= bt_value_map_create();
118 if (!event_class
->user_attributes
) {
119 BT_LIB_LOGE_APPEND_CAUSE(
120 "Failed to create a map value object.");
124 event_class
->id
= id
;
125 bt_property_uint_init(&event_class
->log_level
,
126 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
, 0);
127 event_class
->name
.str
= g_string_new(NULL
);
128 if (!event_class
->name
.str
) {
129 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
133 event_class
->emf_uri
.str
= g_string_new(NULL
);
134 if (!event_class
->emf_uri
.str
) {
135 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
139 ret
= bt_object_pool_initialize(&event_class
->event_pool
,
140 (bt_object_pool_new_object_func
) bt_event_new
,
141 (bt_object_pool_destroy_object_func
) free_event
,
144 BT_LIB_LOGE_APPEND_CAUSE(
145 "Failed to initialize event pool: ret=%d",
150 bt_object_set_parent(&event_class
->base
, &stream_class
->base
);
151 g_ptr_array_add(stream_class
->event_classes
, event_class
);
152 bt_stream_class_freeze(stream_class
);
153 BT_LIB_LOGD("Created event class object: %!+E", event_class
);
157 BT_OBJECT_PUT_REF_AND_RESET(event_class
);
164 struct bt_event_class
*bt_event_class_create(
165 struct bt_stream_class
*stream_class
)
167 BT_ASSERT_PRE_NO_ERROR();
168 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
170 "stream-class-automatically-assigns-event-class-ids",
171 stream_class
->assigns_automatic_event_class_id
,
172 "Stream class does not automatically assigns event class IDs: "
173 "%![sc-]+S", stream_class
);
174 return create_event_class_with_id(stream_class
,
175 (uint64_t) stream_class
->event_classes
->len
);
179 struct bt_event_class
*bt_event_class_create_with_id(
180 struct bt_stream_class
*stream_class
, uint64_t id
)
182 BT_ASSERT_PRE_NO_ERROR();
184 "stream-class-does-not-automatically-assigns-event-class-ids",
185 !stream_class
->assigns_automatic_event_class_id
,
186 "Stream class automatically assigns event class IDs: "
187 "%![sc-]+S", stream_class
);
188 return create_event_class_with_id(stream_class
, id
);
192 const char *bt_event_class_get_name(const struct bt_event_class
*event_class
)
194 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
195 return event_class
->name
.value
;
199 enum bt_event_class_set_name_status
bt_event_class_set_name(
200 struct bt_event_class
*event_class
, const char *name
)
202 BT_ASSERT_PRE_NO_ERROR();
203 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
204 BT_ASSERT_PRE_NAME_NON_NULL(name
);
205 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
206 g_string_assign(event_class
->name
.str
, name
);
207 event_class
->name
.value
= event_class
->name
.str
->str
;
208 BT_LIB_LOGD("Set event class's name: %!+E", event_class
);
209 return BT_FUNC_STATUS_OK
;
213 uint64_t bt_event_class_get_id(const struct bt_event_class
*event_class
)
215 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
216 return event_class
->id
;
220 enum bt_property_availability
bt_event_class_get_log_level(
221 const struct bt_event_class
*event_class
,
222 enum bt_event_class_log_level
*log_level
)
224 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
225 BT_ASSERT_PRE_DEV_NON_NULL("log-level-output", log_level
,
226 "Log level (output)");
227 *log_level
= (enum bt_event_class_log_level
)
228 event_class
->log_level
.value
;
229 return event_class
->log_level
.base
.avail
;
233 void bt_event_class_set_log_level(
234 struct bt_event_class
*event_class
,
235 enum bt_event_class_log_level log_level
)
237 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
238 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
239 bt_property_uint_set(&event_class
->log_level
,
240 (uint64_t) log_level
);
241 BT_LIB_LOGD("Set event class's log level: %!+E", event_class
);
245 const char *bt_event_class_get_emf_uri(const struct bt_event_class
*event_class
)
247 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
248 return event_class
->emf_uri
.value
;
252 enum bt_event_class_set_emf_uri_status
bt_event_class_set_emf_uri(
253 struct bt_event_class
*event_class
,
256 BT_ASSERT_PRE_NO_ERROR();
257 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
258 BT_ASSERT_PRE_NON_NULL("emf-uri", emf_uri
, "EMF URI");
259 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
260 g_string_assign(event_class
->emf_uri
.str
, emf_uri
);
261 event_class
->emf_uri
.value
= event_class
->emf_uri
.str
->str
;
262 BT_LIB_LOGD("Set event class's EMF URI: %!+E", event_class
);
263 return BT_FUNC_STATUS_OK
;
267 struct bt_stream_class
*bt_event_class_borrow_stream_class(
268 struct bt_event_class
*event_class
)
270 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
271 return bt_event_class_borrow_stream_class_inline(event_class
);
275 const struct bt_stream_class
*
276 bt_event_class_borrow_stream_class_const(
277 const struct bt_event_class
*event_class
)
279 return bt_event_class_borrow_stream_class((void *) event_class
);
283 const struct bt_field_class
*
284 bt_event_class_borrow_specific_context_field_class_const(
285 const struct bt_event_class
*event_class
)
287 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
288 return event_class
->specific_context_fc
;
292 struct bt_field_class
*
293 bt_event_class_borrow_specific_context_field_class(
294 struct bt_event_class
*event_class
)
296 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
297 return event_class
->specific_context_fc
;
301 enum bt_event_class_set_field_class_status
302 bt_event_class_set_specific_context_field_class(
303 struct bt_event_class
*event_class
,
304 struct bt_field_class
*field_class
)
307 struct bt_stream_class
*stream_class
;
308 struct bt_resolve_field_path_context resolve_ctx
= {
309 .packet_context
= NULL
,
310 .event_common_context
= NULL
,
311 .event_specific_context
= field_class
,
312 .event_payload
= NULL
,
315 BT_ASSERT_PRE_NO_ERROR();
316 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
317 BT_ASSERT_PRE_FC_NON_NULL(field_class
);
318 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
319 BT_ASSERT_PRE_FC_IS_STRUCT("specific-context", field_class
,
320 "Specific context field class");
321 stream_class
= bt_event_class_borrow_stream_class_inline(
323 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
324 resolve_ctx
.event_common_context
=
325 stream_class
->event_common_context_fc
;
327 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
, __func__
);
330 * This is the only reason for which
331 * bt_resolve_field_paths() can fail: anything else
332 * would be because a precondition is not satisfied.
334 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
338 bt_field_class_make_part_of_trace_class(field_class
);
339 bt_object_put_ref(event_class
->specific_context_fc
);
340 event_class
->specific_context_fc
= field_class
;
341 bt_object_get_ref_no_null_check(event_class
->specific_context_fc
);
342 bt_field_class_freeze(field_class
);
343 BT_LIB_LOGD("Set event class's specific context field class: %!+E",
351 const struct bt_field_class
*bt_event_class_borrow_payload_field_class_const(
352 const struct bt_event_class
*event_class
)
354 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
355 return event_class
->payload_fc
;
359 struct bt_field_class
*bt_event_class_borrow_payload_field_class(
360 struct bt_event_class
*event_class
)
362 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
363 return event_class
->payload_fc
;
367 enum bt_event_class_set_field_class_status
368 bt_event_class_set_payload_field_class(
369 struct bt_event_class
*event_class
,
370 struct bt_field_class
*field_class
)
373 struct bt_stream_class
*stream_class
;
374 struct bt_resolve_field_path_context resolve_ctx
= {
375 .packet_context
= NULL
,
376 .event_common_context
= NULL
,
377 .event_specific_context
= NULL
,
378 .event_payload
= field_class
,
381 BT_ASSERT_PRE_NO_ERROR();
382 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
383 BT_ASSERT_PRE_FC_NON_NULL(field_class
);
384 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
385 BT_ASSERT_PRE_FC_IS_STRUCT("payload", field_class
, "Payload field class");
386 stream_class
= bt_event_class_borrow_stream_class_inline(
388 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
389 resolve_ctx
.event_common_context
=
390 stream_class
->event_common_context_fc
;
391 resolve_ctx
.event_specific_context
= event_class
->specific_context_fc
;
393 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
, __func__
);
396 * This is the only reason for which
397 * bt_resolve_field_paths() can fail: anything else
398 * would be because a precondition is not satisfied.
400 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
404 bt_field_class_make_part_of_trace_class(field_class
);
405 bt_object_put_ref(event_class
->payload_fc
);
406 event_class
->payload_fc
= field_class
;
407 bt_object_get_ref_no_null_check(event_class
->payload_fc
);
408 bt_field_class_freeze(field_class
);
409 BT_LIB_LOGD("Set event class's payload field class: %!+E", event_class
);
415 void _bt_event_class_freeze(const struct bt_event_class
*event_class
)
417 /* The field classes are already frozen */
418 BT_ASSERT(event_class
);
419 BT_LIB_LOGD("Freezing event class's user attributes: %!+v",
420 event_class
->user_attributes
);
421 bt_value_freeze(event_class
->user_attributes
);
422 BT_LIB_LOGD("Freezing event class: %!+E", event_class
);
423 ((struct bt_event_class
*) event_class
)->frozen
= true;
427 const struct bt_value
*bt_event_class_borrow_user_attributes_const(
428 const struct bt_event_class
*event_class
)
430 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
431 return event_class
->user_attributes
;
435 struct bt_value
*bt_event_class_borrow_user_attributes(
436 struct bt_event_class
*event_class
)
438 return (void *) bt_event_class_borrow_user_attributes_const(
439 (void *) event_class
);
443 void bt_event_class_set_user_attributes(
444 struct bt_event_class
*event_class
,
445 const struct bt_value
*user_attributes
)
447 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
448 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
449 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
450 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
451 bt_object_put_ref_no_null_check(event_class
->user_attributes
);
452 event_class
->user_attributes
= (void *) user_attributes
;
453 bt_object_get_ref_no_null_check(event_class
->user_attributes
);
457 void bt_event_class_get_ref(const struct bt_event_class
*event_class
)
459 bt_object_get_ref(event_class
);
463 void bt_event_class_put_ref(const struct bt_event_class
*event_class
)
465 bt_object_put_ref(event_class
);