2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #define BT_LOG_TAG "LIB/TRACE-CLASS"
9 #include "lib/logging.h"
11 #include "lib/assert-cond.h"
12 #include <babeltrace2/trace-ir/trace-class.h>
13 #include <babeltrace2/trace-ir/event-class.h>
14 #include "compat/compiler.h"
15 #include <babeltrace2/value.h>
16 #include "lib/value.h"
17 #include <babeltrace2/types.h>
18 #include "compat/endian.h"
19 #include "common/assert.h"
26 #include "trace-class.h"
27 #include "stream-class.h"
29 #include "lib/value.h"
30 #include "lib/func-status.h"
32 struct bt_trace_class_destruction_listener_elem
{
33 bt_trace_class_destruction_listener_func func
;
37 #define BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(_tc) \
38 BT_ASSERT_PRE_DEV_HOT("trace-class", (_tc), "Trace class", \
41 #define DESTRUCTION_LISTENER_FUNC_NAME "bt_trace_destruction_listener_func"
44 void destroy_trace_class(struct bt_object
*obj
)
46 struct bt_trace_class
*tc
= (void *) obj
;
48 BT_LIB_LOGD("Destroying trace class object: %!+T", tc
);
49 BT_OBJECT_PUT_REF_AND_RESET(tc
->user_attributes
);
52 * Call destruction listener functions so that everything else
53 * still exists in the trace class.
55 if (tc
->destruction_listeners
) {
57 const struct bt_error
*saved_error
;
59 BT_LIB_LOGD("Calling trace class destruction listener(s): %!+T", tc
);
62 * The trace class' reference count is 0 if we're here. Increment
63 * it to avoid a double-destroy (possibly infinitely recursive).
64 * This could happen for example if a destruction listener did
65 * bt_object_get_ref() (or anything that causes
66 * bt_object_get_ref() to be called) on the trace class (ref.
67 * count goes from 0 to 1), and then bt_object_put_ref(): the
68 * reference count would go from 1 to 0 again and this function
69 * would be called again.
73 saved_error
= bt_current_thread_take_error();
75 /* Call all the trace class destruction listeners */
76 for (i
= 0; i
< tc
->destruction_listeners
->len
; i
++) {
77 struct bt_trace_class_destruction_listener_elem elem
=
78 bt_g_array_index(tc
->destruction_listeners
,
79 struct bt_trace_class_destruction_listener_elem
, i
);
82 elem
.func(tc
, elem
.data
);
83 BT_ASSERT_POST_NO_ERROR(
84 DESTRUCTION_LISTENER_FUNC_NAME
);
88 * The destruction listener should not have kept a
89 * reference to the trace class.
91 BT_ASSERT_POST(DESTRUCTION_LISTENER_FUNC_NAME
,
92 "trace-class-reference-count-not-changed",
93 tc
->base
.ref_count
== 1,
94 "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T",
97 g_array_free(tc
->destruction_listeners
, TRUE
);
98 tc
->destruction_listeners
= NULL
;
101 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
105 if (tc
->stream_classes
) {
106 BT_LOGD_STR("Destroying stream classes.");
107 g_ptr_array_free(tc
->stream_classes
, TRUE
);
108 tc
->stream_classes
= NULL
;
115 struct bt_trace_class
*bt_trace_class_create(bt_self_component
*self_comp
)
117 struct bt_trace_class
*tc
= NULL
;
119 BT_ASSERT_PRE_NO_ERROR();
120 BT_ASSERT_PRE_COMP_NON_NULL(self_comp
);
121 BT_LOGD_STR("Creating default trace class object.");
122 tc
= g_new0(struct bt_trace_class
, 1);
124 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace class.");
128 bt_object_init_shared_with_parent(&tc
->base
, destroy_trace_class
);
129 tc
->user_attributes
= bt_value_map_create();
130 if (!tc
->user_attributes
) {
131 BT_LIB_LOGE_APPEND_CAUSE(
132 "Failed to create a map value object.");
136 tc
->stream_classes
= g_ptr_array_new_with_free_func(
137 (GDestroyNotify
) bt_object_try_spec_release
);
138 if (!tc
->stream_classes
) {
139 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
143 tc
->destruction_listeners
= g_array_new(FALSE
, TRUE
,
144 sizeof(struct bt_trace_class_destruction_listener_elem
));
145 if (!tc
->destruction_listeners
) {
146 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
150 tc
->assigns_automatic_stream_class_id
= true;
151 BT_LIB_LOGD("Created trace class object: %!+T", tc
);
155 BT_OBJECT_PUT_REF_AND_RESET(tc
);
162 enum bt_trace_class_add_listener_status
bt_trace_class_add_destruction_listener(
163 const struct bt_trace_class
*_tc
,
164 bt_trace_class_destruction_listener_func listener
,
165 void *data
, bt_listener_id
*listener_id
)
167 struct bt_trace_class
*tc
= (void *) _tc
;
169 struct bt_trace_class_destruction_listener_elem new_elem
= {
174 BT_ASSERT_PRE_NO_ERROR();
175 BT_ASSERT_PRE_TC_NON_NULL(tc
);
176 BT_ASSERT_PRE_LISTENER_FUNC_NON_NULL(listener
);
178 /* Find the next available spot */
179 for (i
= 0; i
< tc
->destruction_listeners
->len
; i
++) {
180 struct bt_trace_class_destruction_listener_elem elem
=
181 bt_g_array_index(tc
->destruction_listeners
,
182 struct bt_trace_class_destruction_listener_elem
, i
);
189 if (i
== tc
->destruction_listeners
->len
) {
190 g_array_append_val(tc
->destruction_listeners
, new_elem
);
192 g_array_insert_val(tc
->destruction_listeners
, i
, new_elem
);
199 BT_LIB_LOGD("Added trace class destruction listener: %![tc-]+T, "
200 "listener-id=%" PRIu64
, tc
, i
);
201 return BT_FUNC_STATUS_OK
;
205 bool has_listener_id(const struct bt_trace_class
*tc
, uint64_t listener_id
)
207 BT_ASSERT(listener_id
< tc
->destruction_listeners
->len
);
208 return (&bt_g_array_index(tc
->destruction_listeners
,
209 struct bt_trace_class_destruction_listener_elem
,
214 enum bt_trace_class_remove_listener_status
bt_trace_class_remove_destruction_listener(
215 const struct bt_trace_class
*_tc
, bt_listener_id listener_id
)
217 struct bt_trace_class
*tc
= (void *) _tc
;
218 struct bt_trace_class_destruction_listener_elem
*elem
;
220 BT_ASSERT_PRE_NO_ERROR();
221 BT_ASSERT_PRE_TC_NON_NULL(tc
);
222 BT_ASSERT_PRE("listener-id-exists",
223 has_listener_id(tc
, listener_id
),
224 "Trace class has no such trace class destruction listener ID: "
225 "%![tc-]+T, %" PRIu64
, tc
, listener_id
);
226 elem
= &bt_g_array_index(tc
->destruction_listeners
,
227 struct bt_trace_class_destruction_listener_elem
,
229 BT_ASSERT(elem
->func
);
233 BT_LIB_LOGD("Removed trace class destruction listener: "
234 "%![tc-]+T, listener-id=%" PRIu64
,
236 return BT_FUNC_STATUS_OK
;
240 uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class
*tc
)
242 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc
);
243 return (uint64_t) tc
->stream_classes
->len
;
247 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_index(
248 struct bt_trace_class
*tc
, uint64_t index
)
250 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc
);
251 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, tc
->stream_classes
->len
);
252 return g_ptr_array_index(tc
->stream_classes
, index
);
256 const struct bt_stream_class
*
257 bt_trace_class_borrow_stream_class_by_index_const(
258 const struct bt_trace_class
*tc
, uint64_t index
)
260 return bt_trace_class_borrow_stream_class_by_index(
265 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_id(
266 struct bt_trace_class
*tc
, uint64_t id
)
268 struct bt_stream_class
*stream_class
= NULL
;
271 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc
);
273 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
274 struct bt_stream_class
*stream_class_candidate
=
275 g_ptr_array_index(tc
->stream_classes
, i
);
277 if (stream_class_candidate
->id
== id
) {
278 stream_class
= stream_class_candidate
;
288 const struct bt_stream_class
*
289 bt_trace_class_borrow_stream_class_by_id_const(
290 const struct bt_trace_class
*tc
, uint64_t id
)
292 return bt_trace_class_borrow_stream_class_by_id((void *) tc
, id
);
295 void _bt_trace_class_freeze(const struct bt_trace_class
*tc
)
298 BT_LIB_LOGD("Freezing trace class: %!+T", tc
);
299 ((struct bt_trace_class
*) tc
)->frozen
= true;
303 bt_bool
bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class
*tc
)
305 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc
);
306 return (bt_bool
) tc
->assigns_automatic_stream_class_id
;
310 void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class
*tc
,
313 BT_ASSERT_PRE_TC_NON_NULL(tc
);
314 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(tc
);
315 tc
->assigns_automatic_stream_class_id
= (bool) value
;
316 BT_LIB_LOGD("Set trace class's automatic stream class ID "
317 "assignment property: %!+T", tc
);
321 const struct bt_value
*bt_trace_class_borrow_user_attributes_const(
322 const struct bt_trace_class
*trace_class
)
324 BT_ASSERT_PRE_DEV_TC_NON_NULL(trace_class
);
325 return trace_class
->user_attributes
;
329 struct bt_value
*bt_trace_class_borrow_user_attributes(
330 struct bt_trace_class
*trace_class
)
332 return (void *) bt_trace_class_borrow_user_attributes_const(
333 (void *) trace_class
);
337 void bt_trace_class_set_user_attributes(struct bt_trace_class
*trace_class
,
338 const struct bt_value
*user_attributes
)
340 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
341 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
342 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
343 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(trace_class
);
344 bt_object_put_ref_no_null_check(trace_class
->user_attributes
);
345 trace_class
->user_attributes
= (void *) user_attributes
;
346 bt_object_get_ref_no_null_check(trace_class
->user_attributes
);
350 void bt_trace_class_get_ref(const struct bt_trace_class
*trace_class
)
352 bt_object_get_ref(trace_class
);
356 void bt_trace_class_put_ref(const struct bt_trace_class
*trace_class
)
358 bt_object_put_ref(trace_class
);