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"
31 #include "lib/graph/component.h"
32 #include "lib/graph/graph.h"
34 struct bt_trace_class_destruction_listener_elem
{
35 bt_trace_class_destruction_listener_func func
;
39 #define BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(_tc) \
40 BT_ASSERT_PRE_DEV_HOT("trace-class", (_tc), "Trace class", \
43 #define DESTRUCTION_LISTENER_FUNC_NAME "bt_trace_destruction_listener_func"
46 void destroy_trace_class(struct bt_object
*obj
)
48 struct bt_trace_class
*tc
= (void *) obj
;
50 BT_LIB_LOGD("Destroying trace class object: %!+T", tc
);
51 BT_OBJECT_PUT_REF_AND_RESET(tc
->user_attributes
);
54 * Call destruction listener functions so that everything else
55 * still exists in the trace class.
57 if (tc
->destruction_listeners
) {
59 const struct bt_error
*saved_error
;
61 BT_LIB_LOGD("Calling trace class destruction listener(s): %!+T", tc
);
64 * The trace class' reference count is 0 if we're here. Increment
65 * it to avoid a double-destroy (possibly infinitely recursive).
66 * This could happen for example if a destruction listener did
67 * bt_object_get_ref() (or anything that causes
68 * bt_object_get_ref() to be called) on the trace class (ref.
69 * count goes from 0 to 1), and then bt_object_put_ref(): the
70 * reference count would go from 1 to 0 again and this function
71 * would be called again.
75 saved_error
= bt_current_thread_take_error();
77 /* Call all the trace class destruction listeners */
78 for (i
= 0; i
< tc
->destruction_listeners
->len
; i
++) {
79 struct bt_trace_class_destruction_listener_elem elem
=
80 bt_g_array_index(tc
->destruction_listeners
,
81 struct bt_trace_class_destruction_listener_elem
, i
);
84 elem
.func(tc
, elem
.data
);
85 BT_ASSERT_POST_NO_ERROR(
86 DESTRUCTION_LISTENER_FUNC_NAME
);
90 * The destruction listener should not have kept a
91 * reference to the trace class.
93 BT_ASSERT_POST(DESTRUCTION_LISTENER_FUNC_NAME
,
94 "trace-class-reference-count-not-changed",
95 tc
->base
.ref_count
== 1,
96 "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T",
99 g_array_free(tc
->destruction_listeners
, TRUE
);
100 tc
->destruction_listeners
= NULL
;
103 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
107 if (tc
->stream_classes
) {
108 BT_LOGD_STR("Destroying stream classes.");
109 g_ptr_array_free(tc
->stream_classes
, TRUE
);
110 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_COMP_NON_NULL(self_comp
);
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
->mip_version
= bt_component_borrow_graph((void *) self_comp
)->mip_version
;
133 tc
->user_attributes
= bt_value_map_create();
134 if (!tc
->user_attributes
) {
135 BT_LIB_LOGE_APPEND_CAUSE(
136 "Failed to create a map value object.");
140 tc
->stream_classes
= g_ptr_array_new_with_free_func(
141 (GDestroyNotify
) bt_object_try_spec_release
);
142 if (!tc
->stream_classes
) {
143 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
147 tc
->destruction_listeners
= g_array_new(FALSE
, TRUE
,
148 sizeof(struct bt_trace_class_destruction_listener_elem
));
149 if (!tc
->destruction_listeners
) {
150 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
154 tc
->assigns_automatic_stream_class_id
= true;
155 BT_LIB_LOGD("Created trace class object: %!+T", tc
);
159 BT_OBJECT_PUT_REF_AND_RESET(tc
);
166 enum bt_trace_class_add_listener_status
bt_trace_class_add_destruction_listener(
167 const struct bt_trace_class
*_tc
,
168 bt_trace_class_destruction_listener_func listener
,
169 void *data
, bt_listener_id
*listener_id
)
171 struct bt_trace_class
*tc
= (void *) _tc
;
173 struct bt_trace_class_destruction_listener_elem new_elem
= {
178 BT_ASSERT_PRE_NO_ERROR();
179 BT_ASSERT_PRE_TC_NON_NULL(tc
);
180 BT_ASSERT_PRE_LISTENER_FUNC_NON_NULL(listener
);
182 /* Find the next available spot */
183 for (i
= 0; i
< tc
->destruction_listeners
->len
; i
++) {
184 struct bt_trace_class_destruction_listener_elem elem
=
185 bt_g_array_index(tc
->destruction_listeners
,
186 struct bt_trace_class_destruction_listener_elem
, i
);
193 if (i
== tc
->destruction_listeners
->len
) {
194 g_array_append_val(tc
->destruction_listeners
, new_elem
);
196 g_array_insert_val(tc
->destruction_listeners
, i
, new_elem
);
203 BT_LIB_LOGD("Added trace class destruction listener: %![tc-]+T, "
204 "listener-id=%" PRIu64
, tc
, i
);
205 return BT_FUNC_STATUS_OK
;
209 bool has_listener_id(const struct bt_trace_class
*tc
, uint64_t listener_id
)
211 BT_ASSERT(listener_id
< tc
->destruction_listeners
->len
);
212 return (&bt_g_array_index(tc
->destruction_listeners
,
213 struct bt_trace_class_destruction_listener_elem
,
218 enum bt_trace_class_remove_listener_status
bt_trace_class_remove_destruction_listener(
219 const struct bt_trace_class
*_tc
, bt_listener_id listener_id
)
221 struct bt_trace_class
*tc
= (void *) _tc
;
222 struct bt_trace_class_destruction_listener_elem
*elem
;
224 BT_ASSERT_PRE_NO_ERROR();
225 BT_ASSERT_PRE_TC_NON_NULL(tc
);
226 BT_ASSERT_PRE("listener-id-exists",
227 has_listener_id(tc
, listener_id
),
228 "Trace class has no such trace class destruction listener ID: "
229 "%![tc-]+T, %" PRIu64
, tc
, listener_id
);
230 elem
= &bt_g_array_index(tc
->destruction_listeners
,
231 struct bt_trace_class_destruction_listener_elem
,
233 BT_ASSERT(elem
->func
);
237 BT_LIB_LOGD("Removed trace class destruction listener: "
238 "%![tc-]+T, listener-id=%" PRIu64
,
240 return BT_FUNC_STATUS_OK
;
244 uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class
*tc
)
246 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc
);
247 return (uint64_t) tc
->stream_classes
->len
;
251 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_index(
252 struct bt_trace_class
*tc
, uint64_t index
)
254 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc
);
255 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, tc
->stream_classes
->len
);
256 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(
269 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_id(
270 struct bt_trace_class
*tc
, uint64_t id
)
272 struct bt_stream_class
*stream_class
= NULL
;
275 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc
);
277 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
278 struct bt_stream_class
*stream_class_candidate
=
279 g_ptr_array_index(tc
->stream_classes
, i
);
281 if (stream_class_candidate
->id
== id
) {
282 stream_class
= stream_class_candidate
;
292 const struct bt_stream_class
*
293 bt_trace_class_borrow_stream_class_by_id_const(
294 const struct bt_trace_class
*tc
, uint64_t id
)
296 return bt_trace_class_borrow_stream_class_by_id((void *) tc
, id
);
299 void _bt_trace_class_freeze(const struct bt_trace_class
*tc
)
302 BT_LIB_LOGD("Freezing trace class: %!+T", tc
);
303 ((struct bt_trace_class
*) tc
)->frozen
= true;
307 bt_bool
bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class
*tc
)
309 BT_ASSERT_PRE_DEV_TC_NON_NULL(tc
);
310 return (bt_bool
) tc
->assigns_automatic_stream_class_id
;
314 void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class
*tc
,
317 BT_ASSERT_PRE_TC_NON_NULL(tc
);
318 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(tc
);
319 tc
->assigns_automatic_stream_class_id
= (bool) value
;
320 BT_LIB_LOGD("Set trace class's automatic stream class ID "
321 "assignment property: %!+T", tc
);
325 const struct bt_value
*bt_trace_class_borrow_user_attributes_const(
326 const struct bt_trace_class
*trace_class
)
328 BT_ASSERT_PRE_DEV_TC_NON_NULL(trace_class
);
329 return trace_class
->user_attributes
;
333 uint64_t bt_trace_class_get_graph_mip_version(
334 const bt_trace_class
*trace_class
)
336 BT_ASSERT_PRE_DEV_TC_NON_NULL(trace_class
);
337 return trace_class
->mip_version
;
341 struct bt_value
*bt_trace_class_borrow_user_attributes(
342 struct bt_trace_class
*trace_class
)
344 return (void *) bt_trace_class_borrow_user_attributes_const(
345 (void *) trace_class
);
349 void bt_trace_class_set_user_attributes(struct bt_trace_class
*trace_class
,
350 const struct bt_value
*user_attributes
)
352 BT_ASSERT_PRE_TC_NON_NULL(trace_class
);
353 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
354 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
355 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(trace_class
);
356 bt_object_put_ref_no_null_check(trace_class
->user_attributes
);
357 trace_class
->user_attributes
= (void *) user_attributes
;
358 bt_object_get_ref_no_null_check(trace_class
->user_attributes
);
362 void bt_trace_class_get_ref(const struct bt_trace_class
*trace_class
)
364 bt_object_get_ref(trace_class
);
368 void bt_trace_class_put_ref(const struct bt_trace_class
*trace_class
)
370 bt_object_put_ref(trace_class
);