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-pre.h"
12 #include "lib/assert-post.h"
13 #include <babeltrace2/trace-ir/trace-class.h>
14 #include <babeltrace2/trace-ir/event-class.h>
15 #include "ctf-writer/functor.h"
16 #include "ctf-writer/clock.h"
17 #include "compat/compiler.h"
18 #include <babeltrace2/value.h>
19 #include "lib/value.h"
20 #include <babeltrace2/types.h>
21 #include "compat/endian.h"
22 #include "common/assert.h"
23 #include "compat/glib.h"
30 #include "clock-class.h"
31 #include "event-class.h"
33 #include "field-class.h"
34 #include "field-wrapper.h"
35 #include "resolve-field-path.h"
36 #include "stream-class.h"
40 #include "lib/value.h"
41 #include "lib/func-status.h"
43 struct bt_trace_class_destruction_listener_elem
{
44 bt_trace_class_destruction_listener_func func
;
48 #define BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(_tc) \
49 BT_ASSERT_PRE_DEV_HOT((_tc), "Trace class", ": %!+T", (_tc))
52 void destroy_trace_class(struct bt_object
*obj
)
54 struct bt_trace_class
*tc
= (void *) obj
;
56 BT_LIB_LOGD("Destroying trace class object: %!+T", tc
);
57 BT_OBJECT_PUT_REF_AND_RESET(tc
->user_attributes
);
60 * Call destruction listener functions so that everything else
61 * still exists in the trace class.
63 if (tc
->destruction_listeners
) {
65 const struct bt_error
*saved_error
;
67 BT_LIB_LOGD("Calling trace class destruction listener(s): %!+T", tc
);
70 * The trace class' reference count is 0 if we're here. Increment
71 * it to avoid a double-destroy (possibly infinitely recursive).
72 * This could happen for example if a destruction listener did
73 * bt_object_get_ref() (or anything that causes
74 * bt_object_get_ref() to be called) on the trace class (ref.
75 * count goes from 0 to 1), and then bt_object_put_ref(): the
76 * reference count would go from 1 to 0 again and this function
77 * would be called again.
81 saved_error
= bt_current_thread_take_error();
83 /* Call all the trace class destruction listeners */
84 for (i
= 0; i
< tc
->destruction_listeners
->len
; i
++) {
85 struct bt_trace_class_destruction_listener_elem elem
=
86 g_array_index(tc
->destruction_listeners
,
87 struct bt_trace_class_destruction_listener_elem
, i
);
90 elem
.func(tc
, elem
.data
);
91 BT_ASSERT_POST_NO_ERROR();
95 * The destruction listener should not have kept a
96 * reference to the trace class.
98 BT_ASSERT_POST(tc
->base
.ref_count
== 1, "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T", tc
);
100 g_array_free(tc
->destruction_listeners
, TRUE
);
101 tc
->destruction_listeners
= NULL
;
104 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
108 if (tc
->stream_classes
) {
109 BT_LOGD_STR("Destroying stream classes.");
110 g_ptr_array_free(tc
->stream_classes
, TRUE
);
111 tc
->stream_classes
= NULL
;
117 struct bt_trace_class
*bt_trace_class_create(bt_self_component
*self_comp
)
119 struct bt_trace_class
*tc
= NULL
;
121 BT_ASSERT_PRE_NO_ERROR();
122 BT_ASSERT_PRE_NON_NULL(self_comp
, "Self component");
123 BT_LOGD_STR("Creating default trace class object.");
124 tc
= g_new0(struct bt_trace_class
, 1);
126 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace class.");
130 bt_object_init_shared_with_parent(&tc
->base
, destroy_trace_class
);
131 tc
->user_attributes
= bt_value_map_create();
132 if (!tc
->user_attributes
) {
133 BT_LIB_LOGE_APPEND_CAUSE(
134 "Failed to create a map value object.");
138 tc
->stream_classes
= g_ptr_array_new_with_free_func(
139 (GDestroyNotify
) bt_object_try_spec_release
);
140 if (!tc
->stream_classes
) {
141 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
145 tc
->destruction_listeners
= g_array_new(FALSE
, TRUE
,
146 sizeof(struct bt_trace_class_destruction_listener_elem
));
147 if (!tc
->destruction_listeners
) {
148 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
152 tc
->assigns_automatic_stream_class_id
= true;
153 BT_LIB_LOGD("Created trace class object: %!+T", tc
);
157 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_NON_NULL(tc
, "Trace class");
177 BT_ASSERT_PRE_NON_NULL(listener
, "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 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 (&g_array_index(tc
->destruction_listeners
,
210 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_NON_NULL(tc
, "Trace class");
222 BT_ASSERT_PRE(has_listener_id(tc
, listener_id
),
223 "Trace class has no such trace class destruction listener ID: "
224 "%![tc-]+T, %" PRIu64
, tc
, listener_id
);
225 elem
= &g_array_index(tc
->destruction_listeners
,
226 struct bt_trace_class_destruction_listener_elem
,
228 BT_ASSERT(elem
->func
);
232 BT_LIB_LOGD("Removed trace class destruction listener: "
233 "%![tc-]+T, listener-id=%" PRIu64
,
235 return BT_FUNC_STATUS_OK
;
238 uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class
*tc
)
240 BT_ASSERT_PRE_DEV_NON_NULL(tc
, "Trace class");
241 return (uint64_t) tc
->stream_classes
->len
;
244 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_index(
245 struct bt_trace_class
*tc
, uint64_t index
)
247 BT_ASSERT_PRE_DEV_NON_NULL(tc
, "Trace class");
248 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, tc
->stream_classes
->len
);
249 return g_ptr_array_index(tc
->stream_classes
, index
);
252 const struct bt_stream_class
*
253 bt_trace_class_borrow_stream_class_by_index_const(
254 const struct bt_trace_class
*tc
, uint64_t index
)
256 return bt_trace_class_borrow_stream_class_by_index(
260 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_id(
261 struct bt_trace_class
*tc
, uint64_t id
)
263 struct bt_stream_class
*stream_class
= NULL
;
266 BT_ASSERT_PRE_DEV_NON_NULL(tc
, "Trace class");
268 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
269 struct bt_stream_class
*stream_class_candidate
=
270 g_ptr_array_index(tc
->stream_classes
, i
);
272 if (stream_class_candidate
->id
== id
) {
273 stream_class
= stream_class_candidate
;
282 const struct bt_stream_class
*
283 bt_trace_class_borrow_stream_class_by_id_const(
284 const struct bt_trace_class
*tc
, uint64_t id
)
286 return bt_trace_class_borrow_stream_class_by_id((void *) tc
, id
);
290 void _bt_trace_class_freeze(const struct bt_trace_class
*tc
)
293 BT_LIB_LOGD("Freezing trace class: %!+T", tc
);
294 ((struct bt_trace_class
*) tc
)->frozen
= true;
297 bt_bool
bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class
*tc
)
299 BT_ASSERT_PRE_DEV_NON_NULL(tc
, "Trace class");
300 return (bt_bool
) tc
->assigns_automatic_stream_class_id
;
303 void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class
*tc
,
306 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
307 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(tc
);
308 tc
->assigns_automatic_stream_class_id
= (bool) value
;
309 BT_LIB_LOGD("Set trace class's automatic stream class ID "
310 "assignment property: %!+T", tc
);
313 const struct bt_value
*bt_trace_class_borrow_user_attributes_const(
314 const struct bt_trace_class
*trace_class
)
316 BT_ASSERT_PRE_DEV_NON_NULL(trace_class
, "Trace class");
317 return trace_class
->user_attributes
;
320 struct bt_value
*bt_trace_class_borrow_user_attributes(
321 struct bt_trace_class
*trace_class
)
323 return (void *) bt_trace_class_borrow_user_attributes_const(
324 (void *) trace_class
);
327 void bt_trace_class_set_user_attributes(struct bt_trace_class
*trace_class
,
328 const struct bt_value
*user_attributes
)
330 BT_ASSERT_PRE_NON_NULL(trace_class
, "Trace class");
331 BT_ASSERT_PRE_NON_NULL(user_attributes
, "User attributes");
332 BT_ASSERT_PRE(user_attributes
->type
== BT_VALUE_TYPE_MAP
,
333 "User attributes object is not a map value object.");
334 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(trace_class
);
335 bt_object_put_ref_no_null_check(trace_class
->user_attributes
);
336 trace_class
->user_attributes
= (void *) user_attributes
;
337 bt_object_get_ref_no_null_check(trace_class
->user_attributes
);
340 void bt_trace_class_get_ref(const struct bt_trace_class
*trace_class
)
342 bt_object_get_ref(trace_class
);
345 void bt_trace_class_put_ref(const struct bt_trace_class
*trace_class
)
347 bt_object_put_ref(trace_class
);