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 "ctf-writer/functor.h"
15 #include "ctf-writer/clock.h"
16 #include "compat/compiler.h"
17 #include <babeltrace2/value.h>
18 #include "lib/value.h"
19 #include <babeltrace2/types.h>
20 #include "compat/endian.h"
21 #include "common/assert.h"
22 #include "compat/glib.h"
29 #include "clock-class.h"
30 #include "event-class.h"
32 #include "field-class.h"
33 #include "field-wrapper.h"
34 #include "resolve-field-path.h"
35 #include "stream-class.h"
39 #include "lib/value.h"
40 #include "lib/func-status.h"
42 struct bt_trace_class_destruction_listener_elem
{
43 bt_trace_class_destruction_listener_func func
;
47 #define BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(_tc) \
48 BT_ASSERT_PRE_DEV_HOT("trace-class", (_tc), "Trace class", \
51 #define DESTRUCTION_LISTENER_FUNC_NAME "bt_trace_destruction_listener_func"
54 void destroy_trace_class(struct bt_object
*obj
)
56 struct bt_trace_class
*tc
= (void *) obj
;
58 BT_LIB_LOGD("Destroying trace class object: %!+T", tc
);
59 BT_OBJECT_PUT_REF_AND_RESET(tc
->user_attributes
);
62 * Call destruction listener functions so that everything else
63 * still exists in the trace class.
65 if (tc
->destruction_listeners
) {
67 const struct bt_error
*saved_error
;
69 BT_LIB_LOGD("Calling trace class destruction listener(s): %!+T", tc
);
72 * The trace class' reference count is 0 if we're here. Increment
73 * it to avoid a double-destroy (possibly infinitely recursive).
74 * This could happen for example if a destruction listener did
75 * bt_object_get_ref() (or anything that causes
76 * bt_object_get_ref() to be called) on the trace class (ref.
77 * count goes from 0 to 1), and then bt_object_put_ref(): the
78 * reference count would go from 1 to 0 again and this function
79 * would be called again.
83 saved_error
= bt_current_thread_take_error();
85 /* Call all the trace class destruction listeners */
86 for (i
= 0; i
< tc
->destruction_listeners
->len
; i
++) {
87 struct bt_trace_class_destruction_listener_elem elem
=
88 g_array_index(tc
->destruction_listeners
,
89 struct bt_trace_class_destruction_listener_elem
, i
);
92 elem
.func(tc
, elem
.data
);
93 BT_ASSERT_POST_NO_ERROR(
94 DESTRUCTION_LISTENER_FUNC_NAME
);
98 * The destruction listener should not have kept a
99 * reference to the trace class.
101 BT_ASSERT_POST(DESTRUCTION_LISTENER_FUNC_NAME
,
102 "trace-class-reference-count-not-changed",
103 tc
->base
.ref_count
== 1,
104 "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T",
107 g_array_free(tc
->destruction_listeners
, TRUE
);
108 tc
->destruction_listeners
= NULL
;
111 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
115 if (tc
->stream_classes
) {
116 BT_LOGD_STR("Destroying stream classes.");
117 g_ptr_array_free(tc
->stream_classes
, TRUE
);
118 tc
->stream_classes
= NULL
;
124 struct bt_trace_class
*bt_trace_class_create(bt_self_component
*self_comp
)
126 struct bt_trace_class
*tc
= NULL
;
128 BT_ASSERT_PRE_NO_ERROR();
129 BT_ASSERT_PRE_COMP_NON_NULL(self_comp
);
130 BT_LOGD_STR("Creating default trace class object.");
131 tc
= g_new0(struct bt_trace_class
, 1);
133 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace class.");
137 bt_object_init_shared_with_parent(&tc
->base
, destroy_trace_class
);
138 tc
->user_attributes
= bt_value_map_create();
139 if (!tc
->user_attributes
) {
140 BT_LIB_LOGE_APPEND_CAUSE(
141 "Failed to create a map value object.");
145 tc
->stream_classes
= g_ptr_array_new_with_free_func(
146 (GDestroyNotify
) bt_object_try_spec_release
);
147 if (!tc
->stream_classes
) {
148 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
152 tc
->destruction_listeners
= g_array_new(FALSE
, TRUE
,
153 sizeof(struct bt_trace_class_destruction_listener_elem
));
154 if (!tc
->destruction_listeners
) {
155 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
159 tc
->assigns_automatic_stream_class_id
= true;
160 BT_LIB_LOGD("Created trace class object: %!+T", tc
);
164 BT_OBJECT_PUT_REF_AND_RESET(tc
);
170 enum bt_trace_class_add_listener_status
bt_trace_class_add_destruction_listener(
171 const struct bt_trace_class
*_tc
,
172 bt_trace_class_destruction_listener_func listener
,
173 void *data
, bt_listener_id
*listener_id
)
175 struct bt_trace_class
*tc
= (void *) _tc
;
177 struct bt_trace_class_destruction_listener_elem new_elem
= {
182 BT_ASSERT_PRE_NO_ERROR();
183 BT_ASSERT_PRE_TC_NON_NULL(tc
);
184 BT_ASSERT_PRE_LISTENER_FUNC_NON_NULL(listener
);
186 /* Find the next available spot */
187 for (i
= 0; i
< tc
->destruction_listeners
->len
; i
++) {
188 struct bt_trace_class_destruction_listener_elem elem
=
189 g_array_index(tc
->destruction_listeners
,
190 struct bt_trace_class_destruction_listener_elem
, i
);
197 if (i
== tc
->destruction_listeners
->len
) {
198 g_array_append_val(tc
->destruction_listeners
, new_elem
);
200 g_array_insert_val(tc
->destruction_listeners
, i
, new_elem
);
207 BT_LIB_LOGD("Added trace class destruction listener: %![tc-]+T, "
208 "listener-id=%" PRIu64
, tc
, i
);
209 return BT_FUNC_STATUS_OK
;
213 bool has_listener_id(const struct bt_trace_class
*tc
, uint64_t listener_id
)
215 BT_ASSERT(listener_id
< tc
->destruction_listeners
->len
);
216 return (&g_array_index(tc
->destruction_listeners
,
217 struct bt_trace_class_destruction_listener_elem
,
221 enum bt_trace_class_remove_listener_status
bt_trace_class_remove_destruction_listener(
222 const struct bt_trace_class
*_tc
, bt_listener_id listener_id
)
224 struct bt_trace_class
*tc
= (void *) _tc
;
225 struct bt_trace_class_destruction_listener_elem
*elem
;
227 BT_ASSERT_PRE_NO_ERROR();
228 BT_ASSERT_PRE_TC_NON_NULL(tc
);
229 BT_ASSERT_PRE("listener-id-exists",
230 has_listener_id(tc
, listener_id
),
231 "Trace class has no such trace class destruction listener ID: "
232 "%![tc-]+T, %" PRIu64
, tc
, listener_id
);
233 elem
= &g_array_index(tc
->destruction_listeners
,
234 struct bt_trace_class_destruction_listener_elem
,
236 BT_ASSERT(elem
->func
);
240 BT_LIB_LOGD("Removed trace class destruction listener: "
241 "%![tc-]+T, listener-id=%" PRIu64
,
243 return BT_FUNC_STATUS_OK
;
246 uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class
*tc
)
248 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc
);
249 return (uint64_t) tc
->stream_classes
->len
;
252 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_index(
253 struct bt_trace_class
*tc
, uint64_t index
)
255 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc
);
256 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, tc
->stream_classes
->len
);
257 return g_ptr_array_index(tc
->stream_classes
, index
);
260 const struct bt_stream_class
*
261 bt_trace_class_borrow_stream_class_by_index_const(
262 const struct bt_trace_class
*tc
, uint64_t index
)
264 return bt_trace_class_borrow_stream_class_by_index(
268 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_id(
269 struct bt_trace_class
*tc
, uint64_t id
)
271 struct bt_stream_class
*stream_class
= NULL
;
274 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc
);
276 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
277 struct bt_stream_class
*stream_class_candidate
=
278 g_ptr_array_index(tc
->stream_classes
, i
);
280 if (stream_class_candidate
->id
== id
) {
281 stream_class
= stream_class_candidate
;
290 const struct bt_stream_class
*
291 bt_trace_class_borrow_stream_class_by_id_const(
292 const struct bt_trace_class
*tc
, uint64_t id
)
294 return bt_trace_class_borrow_stream_class_by_id((void *) tc
, id
);
298 void _bt_trace_class_freeze(const struct bt_trace_class
*tc
)
301 BT_LIB_LOGD("Freezing trace class: %!+T", tc
);
302 ((struct bt_trace_class
*) tc
)->frozen
= true;
305 bt_bool
bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class
*tc
)
307 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc
);
308 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
);
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
;
328 struct bt_value
*bt_trace_class_borrow_user_attributes(
329 struct bt_trace_class
*trace_class
)
331 return (void *) bt_trace_class_borrow_user_attributes_const(
332 (void *) trace_class
);
335 void bt_trace_class_set_user_attributes(struct bt_trace_class
*trace_class
,
336 const struct bt_value
*user_attributes
)
338 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
339 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
340 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
341 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(trace_class
);
342 bt_object_put_ref_no_null_check(trace_class
->user_attributes
);
343 trace_class
->user_attributes
= (void *) user_attributes
;
344 bt_object_get_ref_no_null_check(trace_class
->user_attributes
);
347 void bt_trace_class_get_ref(const struct bt_trace_class
*trace_class
)
349 bt_object_get_ref(trace_class
);
352 void bt_trace_class_put_ref(const struct bt_trace_class
*trace_class
)
354 bt_object_put_ref(trace_class
);