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"
20 #include "compat/glib.h"
27 #include "trace-class.h"
28 #include "stream-class.h"
30 #include "lib/value.h"
31 #include "lib/func-status.h"
33 struct bt_trace_class_destruction_listener_elem
{
34 bt_trace_class_destruction_listener_func func
;
38 #define BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(_tc) \
39 BT_ASSERT_PRE_DEV_HOT("trace-class", (_tc), "Trace class", \
42 #define DESTRUCTION_LISTENER_FUNC_NAME "bt_trace_destruction_listener_func"
45 void destroy_trace_class(struct bt_object
*obj
)
47 struct bt_trace_class
*tc
= (void *) obj
;
49 BT_LIB_LOGD("Destroying trace class object: %!+T", tc
);
50 BT_OBJECT_PUT_REF_AND_RESET(tc
->user_attributes
);
53 * Call destruction listener functions so that everything else
54 * still exists in the trace class.
56 if (tc
->destruction_listeners
) {
58 const struct bt_error
*saved_error
;
60 BT_LIB_LOGD("Calling trace class destruction listener(s): %!+T", tc
);
63 * The trace class' reference count is 0 if we're here. Increment
64 * it to avoid a double-destroy (possibly infinitely recursive).
65 * This could happen for example if a destruction listener did
66 * bt_object_get_ref() (or anything that causes
67 * bt_object_get_ref() to be called) on the trace class (ref.
68 * count goes from 0 to 1), and then bt_object_put_ref(): the
69 * reference count would go from 1 to 0 again and this function
70 * would be called again.
74 saved_error
= bt_current_thread_take_error();
76 /* Call all the trace class destruction listeners */
77 for (i
= 0; i
< tc
->destruction_listeners
->len
; i
++) {
78 struct bt_trace_class_destruction_listener_elem elem
=
79 bt_g_array_index(tc
->destruction_listeners
,
80 struct bt_trace_class_destruction_listener_elem
, i
);
83 elem
.func(tc
, elem
.data
);
84 BT_ASSERT_POST_NO_ERROR(
85 DESTRUCTION_LISTENER_FUNC_NAME
);
89 * The destruction listener should not have kept a
90 * reference to the trace class.
92 BT_ASSERT_POST(DESTRUCTION_LISTENER_FUNC_NAME
,
93 "trace-class-reference-count-not-changed",
94 tc
->base
.ref_count
== 1,
95 "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T",
98 g_array_free(tc
->destruction_listeners
, TRUE
);
99 tc
->destruction_listeners
= NULL
;
102 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
106 if (tc
->stream_classes
) {
107 BT_LOGD_STR("Destroying stream classes.");
108 g_ptr_array_free(tc
->stream_classes
, TRUE
);
109 tc
->stream_classes
= NULL
;
116 struct bt_trace_class
*bt_trace_class_create(bt_self_component
*self_comp
)
118 struct bt_trace_class
*tc
= NULL
;
120 BT_ASSERT_PRE_NO_ERROR();
121 BT_ASSERT_PRE_COMP_NON_NULL(self_comp
);
122 BT_LOGD_STR("Creating default trace class object.");
123 tc
= g_new0(struct bt_trace_class
, 1);
125 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace class.");
129 bt_object_init_shared_with_parent(&tc
->base
, destroy_trace_class
);
130 tc
->user_attributes
= bt_value_map_create();
131 if (!tc
->user_attributes
) {
132 BT_LIB_LOGE_APPEND_CAUSE(
133 "Failed to create a map value object.");
137 tc
->stream_classes
= g_ptr_array_new_with_free_func(
138 (GDestroyNotify
) bt_object_try_spec_release
);
139 if (!tc
->stream_classes
) {
140 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
144 tc
->destruction_listeners
= g_array_new(FALSE
, TRUE
,
145 sizeof(struct bt_trace_class_destruction_listener_elem
));
146 if (!tc
->destruction_listeners
) {
147 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
151 tc
->assigns_automatic_stream_class_id
= true;
152 BT_LIB_LOGD("Created trace class object: %!+T", tc
);
156 BT_OBJECT_PUT_REF_AND_RESET(tc
);
163 enum bt_trace_class_add_listener_status
bt_trace_class_add_destruction_listener(
164 const struct bt_trace_class
*_tc
,
165 bt_trace_class_destruction_listener_func listener
,
166 void *data
, bt_listener_id
*listener_id
)
168 struct bt_trace_class
*tc
= (void *) _tc
;
170 struct bt_trace_class_destruction_listener_elem new_elem
= {
175 BT_ASSERT_PRE_NO_ERROR();
176 BT_ASSERT_PRE_TC_NON_NULL(tc
);
177 BT_ASSERT_PRE_LISTENER_FUNC_NON_NULL(listener
);
179 /* Find the next available spot */
180 for (i
= 0; i
< tc
->destruction_listeners
->len
; i
++) {
181 struct bt_trace_class_destruction_listener_elem elem
=
182 bt_g_array_index(tc
->destruction_listeners
,
183 struct bt_trace_class_destruction_listener_elem
, i
);
190 if (i
== tc
->destruction_listeners
->len
) {
191 g_array_append_val(tc
->destruction_listeners
, new_elem
);
193 g_array_insert_val(tc
->destruction_listeners
, i
, new_elem
);
200 BT_LIB_LOGD("Added trace class destruction listener: %![tc-]+T, "
201 "listener-id=%" PRIu64
, tc
, i
);
202 return BT_FUNC_STATUS_OK
;
206 bool has_listener_id(const struct bt_trace_class
*tc
, uint64_t listener_id
)
208 BT_ASSERT(listener_id
< tc
->destruction_listeners
->len
);
209 return (&bt_g_array_index(tc
->destruction_listeners
,
210 struct bt_trace_class_destruction_listener_elem
,
215 enum bt_trace_class_remove_listener_status
bt_trace_class_remove_destruction_listener(
216 const struct bt_trace_class
*_tc
, bt_listener_id listener_id
)
218 struct bt_trace_class
*tc
= (void *) _tc
;
219 struct bt_trace_class_destruction_listener_elem
*elem
;
221 BT_ASSERT_PRE_NO_ERROR();
222 BT_ASSERT_PRE_TC_NON_NULL(tc
);
223 BT_ASSERT_PRE("listener-id-exists",
224 has_listener_id(tc
, listener_id
),
225 "Trace class has no such trace class destruction listener ID: "
226 "%![tc-]+T, %" PRIu64
, tc
, listener_id
);
227 elem
= &bt_g_array_index(tc
->destruction_listeners
,
228 struct bt_trace_class_destruction_listener_elem
,
230 BT_ASSERT(elem
->func
);
234 BT_LIB_LOGD("Removed trace class destruction listener: "
235 "%![tc-]+T, listener-id=%" PRIu64
,
237 return BT_FUNC_STATUS_OK
;
241 uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class
*tc
)
243 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc
);
244 return (uint64_t) tc
->stream_classes
->len
;
248 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_index(
249 struct bt_trace_class
*tc
, uint64_t index
)
251 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc
);
252 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, tc
->stream_classes
->len
);
253 return g_ptr_array_index(tc
->stream_classes
, index
);
257 const struct bt_stream_class
*
258 bt_trace_class_borrow_stream_class_by_index_const(
259 const struct bt_trace_class
*tc
, uint64_t index
)
261 return bt_trace_class_borrow_stream_class_by_index(
266 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_id(
267 struct bt_trace_class
*tc
, uint64_t id
)
269 struct bt_stream_class
*stream_class
= NULL
;
272 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc
);
274 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
275 struct bt_stream_class
*stream_class_candidate
=
276 g_ptr_array_index(tc
->stream_classes
, i
);
278 if (stream_class_candidate
->id
== id
) {
279 stream_class
= stream_class_candidate
;
289 const struct bt_stream_class
*
290 bt_trace_class_borrow_stream_class_by_id_const(
291 const struct bt_trace_class
*tc
, uint64_t id
)
293 return bt_trace_class_borrow_stream_class_by_id((void *) tc
, id
);
296 void _bt_trace_class_freeze(const struct bt_trace_class
*tc
)
299 BT_LIB_LOGD("Freezing trace class: %!+T", tc
);
300 ((struct bt_trace_class
*) tc
)->frozen
= true;
304 bt_bool
bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class
*tc
)
306 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc
);
307 return (bt_bool
) tc
->assigns_automatic_stream_class_id
;
311 void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class
*tc
,
314 BT_ASSERT_PRE_TC_NON_NULL(tc
);
315 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(tc
);
316 tc
->assigns_automatic_stream_class_id
= (bool) value
;
317 BT_LIB_LOGD("Set trace class's automatic stream class ID "
318 "assignment property: %!+T", tc
);
322 const struct bt_value
*bt_trace_class_borrow_user_attributes_const(
323 const struct bt_trace_class
*trace_class
)
325 BT_ASSERT_PRE_DEV_TC_NON_NULL(trace_class
);
326 return trace_class
->user_attributes
;
330 struct bt_value
*bt_trace_class_borrow_user_attributes(
331 struct bt_trace_class
*trace_class
)
333 return (void *) bt_trace_class_borrow_user_attributes_const(
334 (void *) trace_class
);
338 void bt_trace_class_set_user_attributes(struct bt_trace_class
*trace_class
,
339 const struct bt_value
*user_attributes
)
341 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
342 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
343 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
344 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(trace_class
);
345 bt_object_put_ref_no_null_check(trace_class
->user_attributes
);
346 trace_class
->user_attributes
= (void *) user_attributes
;
347 bt_object_get_ref_no_null_check(trace_class
->user_attributes
);
351 void bt_trace_class_get_ref(const struct bt_trace_class
*trace_class
)
353 bt_object_get_ref(trace_class
);
357 void bt_trace_class_put_ref(const struct bt_trace_class
*trace_class
)
359 bt_object_put_ref(trace_class
);