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
);
163 struct bt_event_class
*bt_event_class_create(
164 struct bt_stream_class
*stream_class
)
166 BT_ASSERT_PRE_NO_ERROR();
167 BT_ASSERT_PRE_SC_NON_NULL(stream_class
);
169 "stream-class-automatically-assigns-event-class-ids",
170 stream_class
->assigns_automatic_event_class_id
,
171 "Stream class does not automatically assigns event class IDs: "
172 "%![sc-]+S", stream_class
);
173 return create_event_class_with_id(stream_class
,
174 (uint64_t) stream_class
->event_classes
->len
);
177 struct bt_event_class
*bt_event_class_create_with_id(
178 struct bt_stream_class
*stream_class
, uint64_t id
)
180 BT_ASSERT_PRE_NO_ERROR();
182 "stream-class-does-not-automatically-assigns-event-class-ids",
183 !stream_class
->assigns_automatic_event_class_id
,
184 "Stream class automatically assigns event class IDs: "
185 "%![sc-]+S", stream_class
);
186 return create_event_class_with_id(stream_class
, id
);
189 const char *bt_event_class_get_name(const struct bt_event_class
*event_class
)
191 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
192 return event_class
->name
.value
;
195 enum bt_event_class_set_name_status
bt_event_class_set_name(
196 struct bt_event_class
*event_class
, const char *name
)
198 BT_ASSERT_PRE_NO_ERROR();
199 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
200 BT_ASSERT_PRE_NAME_NON_NULL(name
);
201 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
202 g_string_assign(event_class
->name
.str
, name
);
203 event_class
->name
.value
= event_class
->name
.str
->str
;
204 BT_LIB_LOGD("Set event class's name: %!+E", event_class
);
205 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
;
214 enum bt_property_availability
bt_event_class_get_log_level(
215 const struct bt_event_class
*event_class
,
216 enum bt_event_class_log_level
*log_level
)
218 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
219 BT_ASSERT_PRE_DEV_NON_NULL("log-level-output", log_level
,
220 "Log level (output)");
221 *log_level
= (enum bt_event_class_log_level
)
222 event_class
->log_level
.value
;
223 return event_class
->log_level
.base
.avail
;
226 void bt_event_class_set_log_level(
227 struct bt_event_class
*event_class
,
228 enum bt_event_class_log_level log_level
)
230 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
231 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
232 bt_property_uint_set(&event_class
->log_level
,
233 (uint64_t) log_level
);
234 BT_LIB_LOGD("Set event class's log level: %!+E", event_class
);
237 const char *bt_event_class_get_emf_uri(const struct bt_event_class
*event_class
)
239 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
240 return event_class
->emf_uri
.value
;
243 enum bt_event_class_set_emf_uri_status
bt_event_class_set_emf_uri(
244 struct bt_event_class
*event_class
,
247 BT_ASSERT_PRE_NO_ERROR();
248 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
249 BT_ASSERT_PRE_NON_NULL("emf-uri", emf_uri
, "EMF URI");
250 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
251 g_string_assign(event_class
->emf_uri
.str
, emf_uri
);
252 event_class
->emf_uri
.value
= event_class
->emf_uri
.str
->str
;
253 BT_LIB_LOGD("Set event class's EMF URI: %!+E", event_class
);
254 return BT_FUNC_STATUS_OK
;
257 struct bt_stream_class
*bt_event_class_borrow_stream_class(
258 struct bt_event_class
*event_class
)
260 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
261 return bt_event_class_borrow_stream_class_inline(event_class
);
264 const struct bt_stream_class
*
265 bt_event_class_borrow_stream_class_const(
266 const struct bt_event_class
*event_class
)
268 return bt_event_class_borrow_stream_class((void *) event_class
);
271 const struct bt_field_class
*
272 bt_event_class_borrow_specific_context_field_class_const(
273 const struct bt_event_class
*event_class
)
275 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
276 return event_class
->specific_context_fc
;
279 struct bt_field_class
*
280 bt_event_class_borrow_specific_context_field_class(
281 struct bt_event_class
*event_class
)
283 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
284 return event_class
->specific_context_fc
;
287 enum bt_event_class_set_field_class_status
288 bt_event_class_set_specific_context_field_class(
289 struct bt_event_class
*event_class
,
290 struct bt_field_class
*field_class
)
293 struct bt_stream_class
*stream_class
;
294 struct bt_resolve_field_path_context resolve_ctx
= {
295 .packet_context
= NULL
,
296 .event_common_context
= NULL
,
297 .event_specific_context
= field_class
,
298 .event_payload
= NULL
,
301 BT_ASSERT_PRE_NO_ERROR();
302 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
303 BT_ASSERT_PRE_FC_NON_NULL(field_class
);
304 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
305 BT_ASSERT_PRE_FC_IS_STRUCT("specific-context", field_class
,
306 "Specific context field class");
307 stream_class
= bt_event_class_borrow_stream_class_inline(
309 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
310 resolve_ctx
.event_common_context
=
311 stream_class
->event_common_context_fc
;
313 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
, __func__
);
316 * This is the only reason for which
317 * bt_resolve_field_paths() can fail: anything else
318 * would be because a precondition is not satisfied.
320 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
324 bt_field_class_make_part_of_trace_class(field_class
);
325 bt_object_put_ref(event_class
->specific_context_fc
);
326 event_class
->specific_context_fc
= field_class
;
327 bt_object_get_ref_no_null_check(event_class
->specific_context_fc
);
328 bt_field_class_freeze(field_class
);
329 BT_LIB_LOGD("Set event class's specific context field class: %!+E",
336 const struct bt_field_class
*bt_event_class_borrow_payload_field_class_const(
337 const struct bt_event_class
*event_class
)
339 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
340 return event_class
->payload_fc
;
343 struct bt_field_class
*bt_event_class_borrow_payload_field_class(
344 struct bt_event_class
*event_class
)
346 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
347 return event_class
->payload_fc
;
350 enum bt_event_class_set_field_class_status
351 bt_event_class_set_payload_field_class(
352 struct bt_event_class
*event_class
,
353 struct bt_field_class
*field_class
)
356 struct bt_stream_class
*stream_class
;
357 struct bt_resolve_field_path_context resolve_ctx
= {
358 .packet_context
= NULL
,
359 .event_common_context
= NULL
,
360 .event_specific_context
= NULL
,
361 .event_payload
= field_class
,
364 BT_ASSERT_PRE_NO_ERROR();
365 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
366 BT_ASSERT_PRE_FC_NON_NULL(field_class
);
367 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
368 BT_ASSERT_PRE_FC_IS_STRUCT("payload", field_class
, "Payload field class");
369 stream_class
= bt_event_class_borrow_stream_class_inline(
371 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
372 resolve_ctx
.event_common_context
=
373 stream_class
->event_common_context_fc
;
374 resolve_ctx
.event_specific_context
= event_class
->specific_context_fc
;
376 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
, __func__
);
379 * This is the only reason for which
380 * bt_resolve_field_paths() can fail: anything else
381 * would be because a precondition is not satisfied.
383 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
387 bt_field_class_make_part_of_trace_class(field_class
);
388 bt_object_put_ref(event_class
->payload_fc
);
389 event_class
->payload_fc
= field_class
;
390 bt_object_get_ref_no_null_check(event_class
->payload_fc
);
391 bt_field_class_freeze(field_class
);
392 BT_LIB_LOGD("Set event class's payload field class: %!+E", event_class
);
399 void _bt_event_class_freeze(const struct bt_event_class
*event_class
)
401 /* The field classes are already frozen */
402 BT_ASSERT(event_class
);
403 BT_LIB_LOGD("Freezing event class's user attributes: %!+v",
404 event_class
->user_attributes
);
405 bt_value_freeze(event_class
->user_attributes
);
406 BT_LIB_LOGD("Freezing event class: %!+E", event_class
);
407 ((struct bt_event_class
*) event_class
)->frozen
= true;
410 const struct bt_value
*bt_event_class_borrow_user_attributes_const(
411 const struct bt_event_class
*event_class
)
413 BT_ASSERT_PRE_DEV_EC_NON_NULL(event_class
);
414 return event_class
->user_attributes
;
417 struct bt_value
*bt_event_class_borrow_user_attributes(
418 struct bt_event_class
*event_class
)
420 return (void *) bt_event_class_borrow_user_attributes_const(
421 (void *) event_class
);
424 void bt_event_class_set_user_attributes(
425 struct bt_event_class
*event_class
,
426 const struct bt_value
*user_attributes
)
428 BT_ASSERT_PRE_EC_NON_NULL(event_class
);
429 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
430 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
431 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
432 bt_object_put_ref_no_null_check(event_class
->user_attributes
);
433 event_class
->user_attributes
= (void *) user_attributes
;
434 bt_object_get_ref_no_null_check(event_class
->user_attributes
);
437 void bt_event_class_get_ref(const struct bt_event_class
*event_class
)
439 bt_object_get_ref(event_class
);
442 void bt_event_class_put_ref(const struct bt_event_class
*event_class
)
444 bt_object_put_ref(event_class
);