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-pre.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_NON_NULL(stream_class
, "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_NON_NULL(event_class
, "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_NON_NULL(event_class
, "Event class");
194 BT_ASSERT_PRE_NON_NULL(name
, "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_NON_NULL(event_class
, "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_NON_NULL(event_class
, "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_NON_NULL(event_class
, "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_NON_NULL(event_class
, "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_NON_NULL(event_class
, "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_NON_NULL(event_class
, "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_NON_NULL(event_class
, "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_NON_NULL(event_class
, "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_NON_NULL(event_class
, "Event class");
296 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
297 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
298 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
299 BT_FIELD_CLASS_TYPE_STRUCTURE
,
300 "Specific context field class is not a structure field class: "
301 "%!+F", field_class
);
302 stream_class
= bt_event_class_borrow_stream_class_inline(
304 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
305 resolve_ctx
.event_common_context
=
306 stream_class
->event_common_context_fc
;
308 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
311 * This is the only reason for which
312 * bt_resolve_field_paths() can fail: anything else
313 * would be because a precondition is not satisfied.
315 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
319 bt_field_class_make_part_of_trace_class(field_class
);
320 bt_object_put_ref(event_class
->specific_context_fc
);
321 event_class
->specific_context_fc
= field_class
;
322 bt_object_get_ref_no_null_check(event_class
->specific_context_fc
);
323 bt_field_class_freeze(field_class
);
324 BT_LIB_LOGD("Set event class's specific context field class: %!+E",
331 const struct bt_field_class
*bt_event_class_borrow_payload_field_class_const(
332 const struct bt_event_class
*event_class
)
334 BT_ASSERT_PRE_DEV_NON_NULL(event_class
, "Event class");
335 return event_class
->payload_fc
;
338 struct bt_field_class
*bt_event_class_borrow_payload_field_class(
339 struct bt_event_class
*event_class
)
341 BT_ASSERT_PRE_DEV_NON_NULL(event_class
, "Event class");
342 return event_class
->payload_fc
;
345 enum bt_event_class_set_field_class_status
346 bt_event_class_set_payload_field_class(
347 struct bt_event_class
*event_class
,
348 struct bt_field_class
*field_class
)
351 struct bt_stream_class
*stream_class
;
352 struct bt_resolve_field_path_context resolve_ctx
= {
353 .packet_context
= NULL
,
354 .event_common_context
= NULL
,
355 .event_specific_context
= NULL
,
356 .event_payload
= field_class
,
359 BT_ASSERT_PRE_NO_ERROR();
360 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
361 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
362 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
363 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
364 BT_FIELD_CLASS_TYPE_STRUCTURE
,
365 "Payload field class is not a structure field class: %!+F",
367 stream_class
= bt_event_class_borrow_stream_class_inline(
369 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
370 resolve_ctx
.event_common_context
=
371 stream_class
->event_common_context_fc
;
372 resolve_ctx
.event_specific_context
= event_class
->specific_context_fc
;
374 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
377 * This is the only reason for which
378 * bt_resolve_field_paths() can fail: anything else
379 * would be because a precondition is not satisfied.
381 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
385 bt_field_class_make_part_of_trace_class(field_class
);
386 bt_object_put_ref(event_class
->payload_fc
);
387 event_class
->payload_fc
= field_class
;
388 bt_object_get_ref_no_null_check(event_class
->payload_fc
);
389 bt_field_class_freeze(field_class
);
390 BT_LIB_LOGD("Set event class's payload field class: %!+E", event_class
);
397 void _bt_event_class_freeze(const struct bt_event_class
*event_class
)
399 /* The field classes are already frozen */
400 BT_ASSERT(event_class
);
401 BT_LIB_LOGD("Freezing event class's user attributes: %!+v",
402 event_class
->user_attributes
);
403 bt_value_freeze(event_class
->user_attributes
);
404 BT_LIB_LOGD("Freezing event class: %!+E", event_class
);
405 ((struct bt_event_class
*) event_class
)->frozen
= true;
408 const struct bt_value
*bt_event_class_borrow_user_attributes_const(
409 const struct bt_event_class
*event_class
)
411 BT_ASSERT_PRE_DEV_NON_NULL(event_class
, "Event class");
412 return event_class
->user_attributes
;
415 struct bt_value
*bt_event_class_borrow_user_attributes(
416 struct bt_event_class
*event_class
)
418 return (void *) bt_event_class_borrow_user_attributes_const(
419 (void *) event_class
);
422 void bt_event_class_set_user_attributes(
423 struct bt_event_class
*event_class
,
424 const struct bt_value
*user_attributes
)
426 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
427 BT_ASSERT_PRE_NON_NULL(user_attributes
, "User attributes");
428 BT_ASSERT_PRE(user_attributes
->type
== BT_VALUE_TYPE_MAP
,
429 "User attributes object is not a map value object.");
430 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
431 bt_object_put_ref_no_null_check(event_class
->user_attributes
);
432 event_class
->user_attributes
= (void *) user_attributes
;
433 bt_object_get_ref_no_null_check(event_class
->user_attributes
);
436 void bt_event_class_get_ref(const struct bt_event_class
*event_class
)
438 bt_object_get_ref(event_class
);
441 void bt_event_class_put_ref(const struct bt_event_class
*event_class
)
443 bt_object_put_ref(event_class
);