2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "LIB/EVENT-CLASS"
25 #include "lib/logging.h"
27 #include "lib/assert-pre.h"
28 #include <babeltrace2/trace-ir/field-class.h>
29 #include <babeltrace2/trace-ir/event-class.h>
30 #include <babeltrace2/trace-ir/event-class-const.h>
31 #include <babeltrace2/trace-ir/stream-class.h>
32 #include "compat/compiler.h"
33 #include "compat/endian.h"
34 #include <babeltrace2/types.h>
35 #include "lib/value.h"
36 #include "common/assert.h"
40 #include "attributes.h"
41 #include "clock-snapshot.h"
42 #include "event-class.h"
44 #include "field-class.h"
46 #include "resolve-field-path.h"
47 #include "stream-class.h"
50 #include "lib/func-status.h"
52 #define BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(_ec) \
53 BT_ASSERT_PRE_DEV_HOT(((const struct bt_event_class *) (_ec)), \
54 "Event class", ": %!+E", (_ec))
57 void destroy_event_class(struct bt_object
*obj
)
59 struct bt_event_class
*event_class
= (void *) obj
;
61 BT_LIB_LOGD("Destroying event class: %!+E", event_class
);
62 BT_OBJECT_PUT_REF_AND_RESET(event_class
->user_attributes
);
64 if (event_class
->name
.str
) {
65 g_string_free(event_class
->name
.str
, TRUE
);
66 event_class
->name
.str
= NULL
;
69 if (event_class
->emf_uri
.str
) {
70 g_string_free(event_class
->emf_uri
.str
, TRUE
);
71 event_class
->emf_uri
.str
= NULL
;
74 BT_LOGD_STR("Putting context field class.");
75 BT_OBJECT_PUT_REF_AND_RESET(event_class
->specific_context_fc
);
76 BT_LOGD_STR("Putting payload field class.");
77 BT_OBJECT_PUT_REF_AND_RESET(event_class
->payload_fc
);
78 bt_object_pool_finalize(&event_class
->event_pool
);
83 void free_event(struct bt_event
*event
,
84 struct bt_event_class
*event_class
)
86 bt_event_destroy(event
);
90 bool event_class_id_is_unique(const struct bt_stream_class
*stream_class
,
94 bool is_unique
= true;
96 for (i
= 0; i
< stream_class
->event_classes
->len
; i
++) {
97 const struct bt_event_class
*ec
=
98 stream_class
->event_classes
->pdata
[i
];
111 struct bt_event_class
*create_event_class_with_id(
112 struct bt_stream_class
*stream_class
, uint64_t id
)
115 struct bt_event_class
*event_class
;
117 BT_ASSERT(stream_class
);
118 BT_ASSERT_PRE(event_class_id_is_unique(stream_class
, id
),
119 "Duplicate event class ID: %![sc-]+S, id=%" PRIu64
,
121 BT_LIB_LOGD("Creating event class object: %![sc-]+S, id=%" PRIu64
,
123 event_class
= g_new0(struct bt_event_class
, 1);
125 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one event class.");
129 bt_object_init_shared_with_parent(&event_class
->base
,
130 destroy_event_class
);
131 event_class
->user_attributes
= bt_value_map_create();
132 if (!event_class
->user_attributes
) {
133 BT_LIB_LOGE_APPEND_CAUSE(
134 "Failed to create a map value object.");
138 event_class
->id
= id
;
139 bt_property_uint_init(&event_class
->log_level
,
140 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
, 0);
141 event_class
->name
.str
= g_string_new(NULL
);
142 if (!event_class
->name
.str
) {
143 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
147 event_class
->emf_uri
.str
= g_string_new(NULL
);
148 if (!event_class
->emf_uri
.str
) {
149 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
153 ret
= bt_object_pool_initialize(&event_class
->event_pool
,
154 (bt_object_pool_new_object_func
) bt_event_new
,
155 (bt_object_pool_destroy_object_func
) free_event
,
158 BT_LIB_LOGE_APPEND_CAUSE(
159 "Failed to initialize event pool: ret=%d",
164 bt_object_set_parent(&event_class
->base
, &stream_class
->base
);
165 g_ptr_array_add(stream_class
->event_classes
, event_class
);
166 bt_stream_class_freeze(stream_class
);
167 BT_LIB_LOGD("Created event class object: %!+E", event_class
);
171 BT_OBJECT_PUT_REF_AND_RESET(event_class
);
177 struct bt_event_class
*bt_event_class_create(
178 struct bt_stream_class
*stream_class
)
180 BT_ASSERT_PRE_NON_NULL(stream_class
, "Stream class");
181 BT_ASSERT_PRE(stream_class
->assigns_automatic_event_class_id
,
182 "Stream class does not automatically assigns event class IDs: "
183 "%![sc-]+S", stream_class
);
184 return create_event_class_with_id(stream_class
,
185 (uint64_t) stream_class
->event_classes
->len
);
188 struct bt_event_class
*bt_event_class_create_with_id(
189 struct bt_stream_class
*stream_class
, uint64_t id
)
191 BT_ASSERT_PRE(!stream_class
->assigns_automatic_event_class_id
,
192 "Stream class automatically assigns event class IDs: "
193 "%![sc-]+S", stream_class
);
194 return create_event_class_with_id(stream_class
, id
);
197 const char *bt_event_class_get_name(const struct bt_event_class
*event_class
)
199 BT_ASSERT_PRE_DEV_NON_NULL(event_class
, "Event class");
200 return event_class
->name
.value
;
203 enum bt_event_class_set_name_status
bt_event_class_set_name(
204 struct bt_event_class
*event_class
, const char *name
)
206 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
207 BT_ASSERT_PRE_NON_NULL(name
, "Name");
208 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
209 g_string_assign(event_class
->name
.str
, name
);
210 event_class
->name
.value
= event_class
->name
.str
->str
;
211 BT_LIB_LOGD("Set event class's name: %!+E", event_class
);
212 return BT_FUNC_STATUS_OK
;
215 uint64_t bt_event_class_get_id(const struct bt_event_class
*event_class
)
217 BT_ASSERT_PRE_DEV_NON_NULL(event_class
, "Event class");
218 return event_class
->id
;
221 enum bt_property_availability
bt_event_class_get_log_level(
222 const struct bt_event_class
*event_class
,
223 enum bt_event_class_log_level
*log_level
)
225 BT_ASSERT_PRE_DEV_NON_NULL(event_class
, "Event class");
226 BT_ASSERT_PRE_DEV_NON_NULL(log_level
, "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
;
232 void bt_event_class_set_log_level(
233 struct bt_event_class
*event_class
,
234 enum bt_event_class_log_level log_level
)
236 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
237 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
238 bt_property_uint_set(&event_class
->log_level
,
239 (uint64_t) log_level
);
240 BT_LIB_LOGD("Set event class's log level: %!+E", event_class
);
243 const char *bt_event_class_get_emf_uri(const struct bt_event_class
*event_class
)
245 BT_ASSERT_PRE_DEV_NON_NULL(event_class
, "Event class");
246 return event_class
->emf_uri
.value
;
249 enum bt_event_class_set_emf_uri_status
bt_event_class_set_emf_uri(
250 struct bt_event_class
*event_class
,
253 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
254 BT_ASSERT_PRE_NON_NULL(emf_uri
, "EMF URI");
255 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
256 g_string_assign(event_class
->emf_uri
.str
, emf_uri
);
257 event_class
->emf_uri
.value
= event_class
->emf_uri
.str
->str
;
258 BT_LIB_LOGD("Set event class's EMF URI: %!+E", event_class
);
259 return BT_FUNC_STATUS_OK
;
262 struct bt_stream_class
*bt_event_class_borrow_stream_class(
263 struct bt_event_class
*event_class
)
265 BT_ASSERT_PRE_DEV_NON_NULL(event_class
, "Event class");
266 return bt_event_class_borrow_stream_class_inline(event_class
);
269 const struct bt_stream_class
*
270 bt_event_class_borrow_stream_class_const(
271 const struct bt_event_class
*event_class
)
273 return bt_event_class_borrow_stream_class((void *) event_class
);
276 const struct bt_field_class
*
277 bt_event_class_borrow_specific_context_field_class_const(
278 const struct bt_event_class
*event_class
)
280 BT_ASSERT_PRE_DEV_NON_NULL(event_class
, "Event class");
281 return event_class
->specific_context_fc
;
284 struct bt_field_class
*
285 bt_event_class_borrow_specific_context_field_class(
286 struct bt_event_class
*event_class
)
288 BT_ASSERT_PRE_DEV_NON_NULL(event_class
, "Event class");
289 return event_class
->specific_context_fc
;
292 enum bt_event_class_set_field_class_status
293 bt_event_class_set_specific_context_field_class(
294 struct bt_event_class
*event_class
,
295 struct bt_field_class
*field_class
)
298 struct bt_stream_class
*stream_class
;
299 struct bt_resolve_field_path_context resolve_ctx
= {
300 .packet_context
= NULL
,
301 .event_common_context
= NULL
,
302 .event_specific_context
= field_class
,
303 .event_payload
= NULL
,
306 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
307 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
308 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
309 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
310 BT_FIELD_CLASS_TYPE_STRUCTURE
,
311 "Specific context field class is not a structure field class: "
312 "%!+F", field_class
);
313 stream_class
= bt_event_class_borrow_stream_class_inline(
315 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
316 resolve_ctx
.event_common_context
=
317 stream_class
->event_common_context_fc
;
319 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
322 * This is the only reason for which
323 * bt_resolve_field_paths() can fail: anything else
324 * would be because a precondition is not satisfied.
326 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
330 bt_field_class_make_part_of_trace_class(field_class
);
331 bt_object_put_ref(event_class
->specific_context_fc
);
332 event_class
->specific_context_fc
= field_class
;
333 bt_object_get_ref_no_null_check(event_class
->specific_context_fc
);
334 bt_field_class_freeze(field_class
);
335 BT_LIB_LOGD("Set event class's specific context field class: %!+E",
342 const struct bt_field_class
*bt_event_class_borrow_payload_field_class_const(
343 const struct bt_event_class
*event_class
)
345 BT_ASSERT_PRE_DEV_NON_NULL(event_class
, "Event class");
346 return event_class
->payload_fc
;
349 struct bt_field_class
*bt_event_class_borrow_payload_field_class(
350 struct bt_event_class
*event_class
)
352 BT_ASSERT_PRE_DEV_NON_NULL(event_class
, "Event class");
353 return event_class
->payload_fc
;
356 enum bt_event_class_set_field_class_status
357 bt_event_class_set_payload_field_class(
358 struct bt_event_class
*event_class
,
359 struct bt_field_class
*field_class
)
362 struct bt_stream_class
*stream_class
;
363 struct bt_resolve_field_path_context resolve_ctx
= {
364 .packet_context
= NULL
,
365 .event_common_context
= NULL
,
366 .event_specific_context
= NULL
,
367 .event_payload
= field_class
,
370 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
371 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
372 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
373 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
374 BT_FIELD_CLASS_TYPE_STRUCTURE
,
375 "Payload field class is not a structure field class: %!+F",
377 stream_class
= bt_event_class_borrow_stream_class_inline(
379 resolve_ctx
.packet_context
= stream_class
->packet_context_fc
;
380 resolve_ctx
.event_common_context
=
381 stream_class
->event_common_context_fc
;
382 resolve_ctx
.event_specific_context
= event_class
->specific_context_fc
;
384 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
387 * This is the only reason for which
388 * bt_resolve_field_paths() can fail: anything else
389 * would be because a precondition is not satisfied.
391 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
395 bt_field_class_make_part_of_trace_class(field_class
);
396 bt_object_put_ref(event_class
->payload_fc
);
397 event_class
->payload_fc
= field_class
;
398 bt_object_get_ref_no_null_check(event_class
->payload_fc
);
399 bt_field_class_freeze(field_class
);
400 BT_LIB_LOGD("Set event class's payload field class: %!+E", event_class
);
407 void _bt_event_class_freeze(const struct bt_event_class
*event_class
)
409 /* The field classes are already frozen */
410 BT_ASSERT(event_class
);
411 BT_LIB_LOGD("Freezing event class's user attributes: %!+v",
412 event_class
->user_attributes
);
413 bt_value_freeze(event_class
->user_attributes
);
414 BT_LIB_LOGD("Freezing event class: %!+E", event_class
);
415 ((struct bt_event_class
*) event_class
)->frozen
= true;
418 const struct bt_value
*bt_event_class_borrow_user_attributes_const(
419 const struct bt_event_class
*event_class
)
421 BT_ASSERT_PRE_DEV_NON_NULL(event_class
, "Event class");
422 return event_class
->user_attributes
;
425 struct bt_value
*bt_event_class_borrow_user_attributes(
426 struct bt_event_class
*event_class
)
428 return (void *) bt_event_class_borrow_user_attributes_const(
429 (void *) event_class
);
432 void bt_event_class_set_user_attributes(
433 struct bt_event_class
*event_class
,
434 const struct bt_value
*user_attributes
)
436 BT_ASSERT_PRE_NON_NULL(event_class
, "Event class");
437 BT_ASSERT_PRE_NON_NULL(user_attributes
, "User attributes");
438 BT_ASSERT_PRE(user_attributes
->type
== BT_VALUE_TYPE_MAP
,
439 "User attributes object is not a map value object.");
440 BT_ASSERT_PRE_DEV_EVENT_CLASS_HOT(event_class
);
441 bt_object_put_ref_no_null_check(event_class
->user_attributes
);
442 event_class
->user_attributes
= (void *) user_attributes
;
443 bt_object_get_ref_no_null_check(event_class
->user_attributes
);
446 void bt_event_class_get_ref(const struct bt_event_class
*event_class
)
448 bt_object_get_ref(event_class
);
451 void bt_event_class_put_ref(const struct bt_event_class
*event_class
)
453 bt_object_put_ref(event_class
);