2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "LIB/TRACE-CLASS"
25 #include "lib/logging.h"
27 #include "lib/assert-pre.h"
28 #include "lib/assert-post.h"
29 #include <babeltrace2/trace-ir/trace-class.h>
30 #include <babeltrace2/trace-ir/trace-class-const.h>
31 #include <babeltrace2/trace-ir/event-class.h>
32 #include "ctf-writer/functor.h"
33 #include "ctf-writer/clock.h"
34 #include "compat/compiler.h"
35 #include <babeltrace2/value.h>
36 #include <babeltrace2/value-const.h>
37 #include "lib/value.h"
38 #include <babeltrace2/types.h>
39 #include "compat/endian.h"
40 #include "common/assert.h"
41 #include "compat/glib.h"
48 #include "clock-class.h"
49 #include "event-class.h"
51 #include "field-class.h"
52 #include "field-wrapper.h"
53 #include "resolve-field-path.h"
54 #include "stream-class.h"
58 #include "lib/value.h"
59 #include "lib/func-status.h"
61 struct bt_trace_class_destruction_listener_elem
{
62 bt_trace_class_destruction_listener_func func
;
66 #define BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(_tc) \
67 BT_ASSERT_PRE_DEV_HOT((_tc), "Trace class", ": %!+T", (_tc))
70 void destroy_trace_class(struct bt_object
*obj
)
72 struct bt_trace_class
*tc
= (void *) obj
;
74 BT_LIB_LOGD("Destroying trace class object: %!+T", tc
);
75 BT_OBJECT_PUT_REF_AND_RESET(tc
->user_attributes
);
78 * Call destruction listener functions so that everything else
79 * still exists in the trace class.
81 if (tc
->destruction_listeners
) {
83 BT_LIB_LOGD("Calling trace class destruction listener(s): %!+T", tc
);
86 * The trace class' reference count is 0 if we're here. Increment
87 * it to avoid a double-destroy (possibly infinitely recursive).
88 * This could happen for example if a destruction listener did
89 * bt_object_get_ref() (or anything that causes
90 * bt_object_get_ref() to be called) on the trace class (ref.
91 * count goes from 0 to 1), and then bt_object_put_ref(): the
92 * reference count would go from 1 to 0 again and this function
93 * would be called again.
97 /* Call all the trace class destruction listeners */
98 for (i
= 0; i
< tc
->destruction_listeners
->len
; i
++) {
99 struct bt_trace_class_destruction_listener_elem elem
=
100 g_array_index(tc
->destruction_listeners
,
101 struct bt_trace_class_destruction_listener_elem
, i
);
104 elem
.func(tc
, elem
.data
);
105 BT_ASSERT_POST_NO_ERROR();
109 * The destruction listener should not have kept a
110 * reference to the trace class.
112 BT_ASSERT_POST(tc
->base
.ref_count
== 1, "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T", tc
);
114 g_array_free(tc
->destruction_listeners
, TRUE
);
115 tc
->destruction_listeners
= NULL
;
118 if (tc
->stream_classes
) {
119 BT_LOGD_STR("Destroying stream classes.");
120 g_ptr_array_free(tc
->stream_classes
, TRUE
);
121 tc
->stream_classes
= NULL
;
127 struct bt_trace_class
*bt_trace_class_create(bt_self_component
*self_comp
)
129 struct bt_trace_class
*tc
= NULL
;
131 BT_ASSERT_PRE_NO_ERROR();
132 BT_ASSERT_PRE_NON_NULL(self_comp
, "Self component");
133 BT_LOGD_STR("Creating default trace class object.");
134 tc
= g_new0(struct bt_trace_class
, 1);
136 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace class.");
140 bt_object_init_shared_with_parent(&tc
->base
, destroy_trace_class
);
141 tc
->user_attributes
= bt_value_map_create();
142 if (!tc
->user_attributes
) {
143 BT_LIB_LOGE_APPEND_CAUSE(
144 "Failed to create a map value object.");
148 tc
->stream_classes
= g_ptr_array_new_with_free_func(
149 (GDestroyNotify
) bt_object_try_spec_release
);
150 if (!tc
->stream_classes
) {
151 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
155 tc
->destruction_listeners
= g_array_new(FALSE
, TRUE
,
156 sizeof(struct bt_trace_class_destruction_listener_elem
));
157 if (!tc
->destruction_listeners
) {
158 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
162 tc
->assigns_automatic_stream_class_id
= true;
163 BT_LIB_LOGD("Created trace class object: %!+T", tc
);
167 BT_OBJECT_PUT_REF_AND_RESET(tc
);
173 enum bt_trace_class_add_listener_status
bt_trace_class_add_destruction_listener(
174 const struct bt_trace_class
*_tc
,
175 bt_trace_class_destruction_listener_func listener
,
176 void *data
, bt_listener_id
*listener_id
)
178 struct bt_trace_class
*tc
= (void *) _tc
;
180 struct bt_trace_class_destruction_listener_elem new_elem
= {
185 BT_ASSERT_PRE_NO_ERROR();
186 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
187 BT_ASSERT_PRE_NON_NULL(listener
, "Listener");
189 /* Find the next available spot */
190 for (i
= 0; i
< tc
->destruction_listeners
->len
; i
++) {
191 struct bt_trace_class_destruction_listener_elem elem
=
192 g_array_index(tc
->destruction_listeners
,
193 struct bt_trace_class_destruction_listener_elem
, i
);
200 if (i
== tc
->destruction_listeners
->len
) {
201 g_array_append_val(tc
->destruction_listeners
, new_elem
);
203 g_array_insert_val(tc
->destruction_listeners
, i
, new_elem
);
210 BT_LIB_LOGD("Added trace class destruction listener: %![tc-]+T, "
211 "listener-id=%" PRIu64
, tc
, i
);
212 return BT_FUNC_STATUS_OK
;
216 bool has_listener_id(const struct bt_trace_class
*tc
, uint64_t listener_id
)
218 BT_ASSERT(listener_id
< tc
->destruction_listeners
->len
);
219 return (&g_array_index(tc
->destruction_listeners
,
220 struct bt_trace_class_destruction_listener_elem
,
224 enum bt_trace_class_remove_listener_status
bt_trace_class_remove_destruction_listener(
225 const struct bt_trace_class
*_tc
, bt_listener_id listener_id
)
227 struct bt_trace_class
*tc
= (void *) _tc
;
228 struct bt_trace_class_destruction_listener_elem
*elem
;
230 BT_ASSERT_PRE_NO_ERROR();
231 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
232 BT_ASSERT_PRE(has_listener_id(tc
, listener_id
),
233 "Trace class has no such trace class destruction listener ID: "
234 "%![tc-]+T, %" PRIu64
, tc
, listener_id
);
235 elem
= &g_array_index(tc
->destruction_listeners
,
236 struct bt_trace_class_destruction_listener_elem
,
238 BT_ASSERT(elem
->func
);
242 BT_LIB_LOGD("Removed trace class destruction listener: "
243 "%![tc-]+T, listener-id=%" PRIu64
,
245 return BT_FUNC_STATUS_OK
;
248 uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class
*tc
)
250 BT_ASSERT_PRE_DEV_NON_NULL(tc
, "Trace class");
251 return (uint64_t) tc
->stream_classes
->len
;
254 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_index(
255 struct bt_trace_class
*tc
, uint64_t index
)
257 BT_ASSERT_PRE_DEV_NON_NULL(tc
, "Trace class");
258 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, tc
->stream_classes
->len
);
259 return g_ptr_array_index(tc
->stream_classes
, index
);
262 const struct bt_stream_class
*
263 bt_trace_class_borrow_stream_class_by_index_const(
264 const struct bt_trace_class
*tc
, uint64_t index
)
266 return bt_trace_class_borrow_stream_class_by_index(
270 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_id(
271 struct bt_trace_class
*tc
, uint64_t id
)
273 struct bt_stream_class
*stream_class
= NULL
;
276 BT_ASSERT_PRE_DEV_NON_NULL(tc
, "Trace class");
278 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
279 struct bt_stream_class
*stream_class_candidate
=
280 g_ptr_array_index(tc
->stream_classes
, i
);
282 if (stream_class_candidate
->id
== id
) {
283 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
);
300 void _bt_trace_class_freeze(const struct bt_trace_class
*tc
)
303 BT_LIB_LOGD("Freezing trace class: %!+T", tc
);
304 ((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_NON_NULL(tc
, "Trace class");
310 return (bt_bool
) tc
->assigns_automatic_stream_class_id
;
313 void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class
*tc
,
316 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
317 BT_ASSERT_PRE_DEV_TRACE_CLASS_HOT(tc
);
318 tc
->assigns_automatic_stream_class_id
= (bool) value
;
319 BT_LIB_LOGD("Set trace class's automatic stream class ID "
320 "assignment property: %!+T", tc
);
323 const struct bt_value
*bt_trace_class_borrow_user_attributes_const(
324 const struct bt_trace_class
*trace_class
)
326 BT_ASSERT_PRE_DEV_NON_NULL(trace_class
, "Trace class");
327 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
);
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_NON_NULL(trace_class
, "Trace class");
341 BT_ASSERT_PRE_NON_NULL(user_attributes
, "User attributes");
342 BT_ASSERT_PRE(user_attributes
->type
== BT_VALUE_TYPE_MAP
,
343 "User attributes object is not a map value object.");
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
);
350 void bt_trace_class_get_ref(const struct bt_trace_class
*trace_class
)
352 bt_object_get_ref(trace_class
);
355 void bt_trace_class_put_ref(const struct bt_trace_class
*trace_class
)
357 bt_object_put_ref(trace_class
);