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(((const struct bt_event_class *) (_ec)), \
38 "Event class", ": %!+E", (_ec))
41 void destroy_event_class(struct bt_object
*obj
)
43 struct bt_event_class
*event_class
= (void *) obj
;
45 BT_LIB_LOGD("Destroying event class: %!+E", event_class
);
46 BT_OBJECT_PUT_REF_AND_RESET(event_class
->user_attributes
);
48 if (event_class
->name
.str
) {
49 g_string_free(event_class
->name
.str
, TRUE
);
50 event_class
->name
.str
= NULL
;
53 if (event_class
->emf_uri
.str
) {
54 g_string_free(event_class
->emf_uri
.str
, TRUE
);
55 event_class
->emf_uri
.str
= NULL
;
58 BT_LOGD_STR("Putting context field class.");
59 BT_OBJECT_PUT_REF_AND_RESET(event_class
->specific_context_fc
);
60 BT_LOGD_STR("Putting payload field class.");
61 BT_OBJECT_PUT_REF_AND_RESET(event_class
->payload_fc
);
62 bt_object_pool_finalize(&event_class
->event_pool
);
67 void free_event(struct bt_event
*event
,
68 struct bt_event_class
*event_class
)
70 bt_event_destroy(event
);
74 bool event_class_id_is_unique(const struct bt_stream_class
*stream_class
,
78 bool is_unique
= true;
80 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
81 const struct bt_event_class
*ec
=
82 stream_class
->event_classes
->pdata
[i
];
95 struct bt_event_class
*create_event_class_with_id(
96 struct bt_stream_class
*stream_class
, uint64_t id
)
99 struct bt_event_class
*event_class
;
101 BT_ASSERT(stream_class
);
102 BT_ASSERT_PRE(event_class_id_is_unique(stream_class
, id
),
103 "Duplicate event class ID: %![sc-]+S, id=%" PRIu64
,
105 BT_LIB_LOGD("Creating event class object: %![sc-]+S, id=%" PRIu64
,
107 event_class
= g_new0(struct bt_event_class
, 1);
109 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one event class.");
113 bt_object_init_shared_with_parent(&event_class
->base
,
114 destroy_event_class
);
115 event_class
->user_attributes
= bt_value_map_create();
116 if (!event_class
->user_attributes
) {
117 BT_LIB_LOGE_APPEND_CAUSE(
118 "Failed to create a map value object.");
122 event_class
->id
= id
;
123 bt_property_uint_init(&event_class
->log_level
,
124 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
, 0);
125 event_class
->name
.str
= g_string_new(NULL
);
126 if (!event_class
->name
.str
) {
127 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
131 event_class
->emf_uri
.str
= g_string_new(NULL
);
132 if (!event_class
->emf_uri
.str
) {
133 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
137 ret
= bt_object_pool_initialize(&event_class
->event_pool
,
138 (bt_object_pool_new_object_func
) bt_event_new
,
139 (bt_object_pool_destroy_object_func
) free_event
,
142 BT_LIB_LOGE_APPEND_CAUSE(
143 "Failed to initialize event pool: ret=%d",
148 bt_object_set_parent(&event_class
->base
, &stream_class
->base
);
149 g_ptr_array_add(stream_class
->event_classes
, event_class
);
150 bt_stream_class_freeze(stream_class
);
151 BT_LIB_LOGD("Created event class object: %!+E", event_class
);
155 BT_OBJECT_PUT_REF_AND_RESET(event_class
);
161 struct bt_event_class
*bt_event_class_create(
162 struct bt_stream_class
*stream_class
)
164 BT_ASSERT_PRE_NO_ERROR();
165 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
166 BT_ASSERT_PRE(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
);
173 struct bt_event_class
*bt_event_class_create_with_id(
174 struct bt_stream_class
*stream_class
, uint64_t id
)
176 BT_ASSERT_PRE_NO_ERROR();
177 BT_ASSERT_PRE(!stream_class
->assigns_automatic_event_class_id
,
178 "Stream class automatically assigns event class IDs: "
179 "%![sc-]+S", stream_class
);
180 return create_event_class_with_id(stream_class
, id
);
183 const char *bt_event_class_get_name(const struct bt_event_class
*event_class
)
185 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
186 return event_class
->name
.value
;
189 enum bt_event_class_set_name_status
bt_event_class_set_name(
190 struct bt_event_class
*event_class
, const char *name
)
192 BT_ASSERT_PRE_NO_ERROR();
193 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
194 BT_ASSERT_PRE_NAME_NON_NULL(name
);
195 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
196 g_string_assign(event_class
->name
.str
, name
);
197 event_class
->name
.value
= event_class
->name
.str
->str
;
198 BT_LIB_LOGD("Set event class's name: %!+E", event_class
);
199 return BT_FUNC_STATUS_OK
;
202 uint64_t bt_event_class_get_id(const struct bt_event_class
*event_class
)
204 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
205 return event_class
->id
;
208 enum bt_property_availability
bt_event_class_get_log_level(
209 const struct bt_event_class
*event_class
,
210 enum bt_event_class_log_level
*log_level
)
212 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
213 BT_ASSERT_PRE_DEV_NON_NULL(log_level
, "Log level (output)");
214 *log_level
= (enum bt_event_class_log_level
)
215 event_class
->log_level
.value
;
216 return event_class
->log_level
.base
.avail
;
219 void bt_event_class_set_log_level(
220 struct bt_event_class
*event_class
,
221 enum bt_event_class_log_level log_level
)
223 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
224 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
225 bt_property_uint_set(&event_class
->log_level
,
226 (uint64_t) log_level
);
227 BT_LIB_LOGD("Set event class's log level: %!+E", event_class
);
230 const char *bt_event_class_get_emf_uri(const struct bt_event_class
*event_class
)
232 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
233 return event_class
->emf_uri
.value
;
236 enum bt_event_class_set_emf_uri_status
bt_event_class_set_emf_uri(
237 struct bt_event_class
*event_class
,
240 BT_ASSERT_PRE_NO_ERROR();
241 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
242 BT_ASSERT_PRE_NON_NULL(emf_uri
, "EMF URI");
243 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
244 g_string_assign(event_class
->emf_uri
.str
, emf_uri
);
245 event_class
->emf_uri
.value
= event_class
->emf_uri
.str
->str
;
246 BT_LIB_LOGD("Set event class's EMF URI: %!+E", event_class
);
247 return BT_FUNC_STATUS_OK
;
250 struct bt_stream_class
*bt_event_class_borrow_stream_class(
251 struct bt_event_class
*event_class
)
253 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
254 return bt_event_class_borrow_stream_class_inline(event_class
);
257 const struct bt_stream_class
*
258 bt_event_class_borrow_stream_class_const(
259 const struct bt_event_class
*event_class
)
261 return bt_event_class_borrow_stream_class((void *) event_class
);
264 const struct bt_field_class
*
265 bt_event_class_borrow_specific_context_field_class_const(
266 const struct bt_event_class
*event_class
)
268 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
269 return event_class
->specific_context_fc
;
272 struct bt_field_class
*
273 bt_event_class_borrow_specific_context_field_class(
274 struct bt_event_class
*event_class
)
276 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
277 return event_class
->specific_context_fc
;
280 enum bt_event_class_set_field_class_status
281 bt_event_class_set_specific_context_field_class(
282 struct bt_event_class
*event_class
,
283 struct bt_field_class
*field_class
)
286 struct bt_stream_class
*stream_class
;
287 struct bt_resolve_field_path_context resolve_ctx
= {
288 .packet_context
= NULL
,
289 .event_common_context
= NULL
,
290 .event_specific_context
= field_class
,
291 .event_payload
= NULL
,
294 BT_ASSERT_PRE_NO_ERROR();
295 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
296 BT_ASSERT_PRE_FC_NON_NULL(field_class
);
297 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
298 BT_ASSERT_PRE_FC_IS_STRUCT(field_class
, "Specific context field class");
299 stream_class
= bt_event_class_borrow_stream_class_inline(
301 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
302 resolve_ctx
.event_common_context
=
303 stream_class
->event_common_context_fc
;
305 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
308 * This is the only reason for which
309 * bt_resolve_field_paths() can fail: anything else
310 * would be because a precondition is not satisfied.
312 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
316 bt_field_class_make_part_of_trace_class(field_class
);
317 bt_object_put_ref(event_class
->specific_context_fc
);
318 event_class
->specific_context_fc
= field_class
;
319 bt_object_get_ref_no_null_check(event_class
->specific_context_fc
);
320 bt_field_class_freeze(field_class
);
321 BT_LIB_LOGD("Set event class's specific context field class: %!+E",
328 const struct bt_field_class
*bt_event_class_borrow_payload_field_class_const(
329 const struct bt_event_class
*event_class
)
331 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
332 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
;
342 enum bt_event_class_set_field_class_status
343 bt_event_class_set_payload_field_class(
344 struct bt_event_class
*event_class
,
345 struct bt_field_class
*field_class
)
348 struct bt_stream_class
*stream_class
;
349 struct bt_resolve_field_path_context resolve_ctx
= {
350 .packet_context
= NULL
,
351 .event_common_context
= NULL
,
352 .event_specific_context
= NULL
,
353 .event_payload
= field_class
,
356 BT_ASSERT_PRE_NO_ERROR();
357 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
358 BT_ASSERT_PRE_FC_NON_NULL(field_class
);
359 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
360 BT_ASSERT_PRE_FC_IS_STRUCT(field_class
, "Payload field class");
361 stream_class
= bt_event_class_borrow_stream_class_inline(
363 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
364 resolve_ctx
.event_common_context
=
365 stream_class
->event_common_context_fc
;
366 resolve_ctx
.event_specific_context
= event_class
->specific_context_fc
;
368 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
371 * This is the only reason for which
372 * bt_resolve_field_paths() can fail: anything else
373 * would be because a precondition is not satisfied.
375 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
379 bt_field_class_make_part_of_trace_class(field_class
);
380 bt_object_put_ref(event_class
->payload_fc
);
381 event_class
->payload_fc
= field_class
;
382 bt_object_get_ref_no_null_check(event_class
->payload_fc
);
383 bt_field_class_freeze(field_class
);
384 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;
402 const struct bt_value
*bt_event_class_borrow_user_attributes_const(
403 const struct bt_event_class
*event_class
)
405 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
406 return event_class
->user_attributes
;
409 struct bt_value
*bt_event_class_borrow_user_attributes(
410 struct bt_event_class
*event_class
)
412 return (void *) bt_event_class_borrow_user_attributes_const(
413 (void *) event_class
);
416 void bt_event_class_set_user_attributes(
417 struct bt_event_class
*event_class
,
418 const struct bt_value
*user_attributes
)
420 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
421 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
422 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
423 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
424 bt_object_put_ref_no_null_check(event_class
->user_attributes
);
425 event_class
->user_attributes
= (void *) user_attributes
;
426 bt_object_get_ref_no_null_check(event_class
->user_attributes
);
429 void bt_event_class_get_ref(const struct bt_event_class
*event_class
)
431 bt_object_get_ref(event_class
);
434 void bt_event_class_put_ref(const struct bt_event_class
*event_class
)
436 bt_object_put_ref(event_class
);