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 "TRACE"
25 #include <babeltrace/lib-logging-internal.h>
27 #include <babeltrace/assert-pre-internal.h>
28 #include <babeltrace/trace-ir/trace.h>
29 #include <babeltrace/trace-ir/trace-class-internal.h>
30 #include <babeltrace/trace-ir/trace-const.h>
31 #include <babeltrace/trace-ir/trace-internal.h>
32 #include <babeltrace/trace-ir/clock-class-internal.h>
33 #include <babeltrace/trace-ir/stream-internal.h>
34 #include <babeltrace/trace-ir/stream-class-internal.h>
35 #include <babeltrace/trace-ir/event-internal.h>
36 #include <babeltrace/trace-ir/event-class.h>
37 #include <babeltrace/trace-ir/event-class-internal.h>
38 #include <babeltrace/ctf-writer/functor-internal.h>
39 #include <babeltrace/ctf-writer/clock-internal.h>
40 #include <babeltrace/trace-ir/field-wrapper-internal.h>
41 #include <babeltrace/trace-ir/field-class-internal.h>
42 #include <babeltrace/trace-ir/attributes-internal.h>
43 #include <babeltrace/trace-ir/utils-internal.h>
44 #include <babeltrace/trace-ir/resolve-field-path-internal.h>
45 #include <babeltrace/compiler-internal.h>
46 #include <babeltrace/value.h>
47 #include <babeltrace/value-const.h>
48 #include <babeltrace/value-internal.h>
49 #include <babeltrace/types.h>
50 #include <babeltrace/endian-internal.h>
51 #include <babeltrace/assert-internal.h>
52 #include <babeltrace/compat/glib-internal.h>
58 struct bt_trace_destruction_listener_elem
{
59 bt_trace_destruction_listener_func func
;
63 #define BT_ASSERT_PRE_TRACE_HOT(_trace) \
64 BT_ASSERT_PRE_HOT((_trace), "Trace", ": %!+t", (_trace))
67 void destroy_trace(struct bt_object
*obj
)
69 struct bt_trace
*trace
= (void *) obj
;
71 BT_LIB_LOGD("Destroying trace object: %!+t", trace
);
74 * Call destruction listener functions so that everything else
75 * still exists in the trace.
77 if (trace
->destruction_listeners
) {
79 BT_LIB_LOGV("Calling trace destruction listener(s): %!+t", trace
);
82 * The trace's reference count is 0 if we're here. Increment
83 * it to avoid a double-destroy (possibly infinitely recursive).
84 * This could happen for example if a destruction listener did
85 * bt_object_get_ref() (or anything that causes
86 * bt_object_get_ref() to be called) on the trace (ref.
87 * count goes from 0 to 1), and then bt_object_put_ref(): the
88 * reference count would go from 1 to 0 again and this function
89 * would be called again.
91 trace
->base
.ref_count
++;
93 /* Call all the trace destruction listeners */
94 for (i
= 0; i
< trace
->destruction_listeners
->len
; i
++) {
95 struct bt_trace_destruction_listener_elem elem
=
96 g_array_index(trace
->destruction_listeners
,
97 struct bt_trace_destruction_listener_elem
, i
);
100 elem
.func(trace
, elem
.data
);
104 * The destruction listener should not have kept a
105 * reference to the trace.
107 BT_ASSERT_PRE(trace
->base
.ref_count
== 1, "Destruction listener kept a reference to the trace being destroyed: %![trace-]+t", trace
);
109 g_array_free(trace
->destruction_listeners
, TRUE
);
110 trace
->destruction_listeners
= NULL
;
113 if (trace
->name
.str
) {
114 g_string_free(trace
->name
.str
, TRUE
);
115 trace
->name
.str
= NULL
;
116 trace
->name
.value
= NULL
;
119 if (trace
->streams
) {
120 BT_LOGD_STR("Destroying streams.");
121 g_ptr_array_free(trace
->streams
, TRUE
);
122 trace
->streams
= NULL
;
125 if (trace
->stream_classes_stream_count
) {
126 g_hash_table_destroy(trace
->stream_classes_stream_count
);
127 trace
->stream_classes_stream_count
= NULL
;
130 BT_LOGD_STR("Putting trace's class.");
131 bt_object_put_ref(trace
->class);
136 struct bt_trace
*bt_trace_create(struct bt_trace_class
*tc
)
138 struct bt_trace
*trace
= NULL
;
140 BT_LIB_LOGD("Creating trace object: %![tc-]+T", tc
);
141 trace
= g_new0(struct bt_trace
, 1);
143 BT_LOGE_STR("Failed to allocate one trace.");
147 bt_object_init_shared(&trace
->base
, destroy_trace
);
148 trace
->streams
= g_ptr_array_new_with_free_func(
149 (GDestroyNotify
) bt_object_try_spec_release
);
150 if (!trace
->streams
) {
151 BT_LOGE_STR("Failed to allocate one GPtrArray.");
155 trace
->stream_classes_stream_count
= g_hash_table_new(g_direct_hash
,
157 if (!trace
->stream_classes_stream_count
) {
158 BT_LOGE_STR("Failed to allocate one GHashTable.");
162 trace
->name
.str
= g_string_new(NULL
);
163 if (!trace
->name
.str
) {
164 BT_LOGE_STR("Failed to allocate one GString.");
168 trace
->destruction_listeners
= g_array_new(FALSE
, TRUE
,
169 sizeof(struct bt_trace_destruction_listener_elem
));
170 if (!trace
->destruction_listeners
) {
171 BT_LOGE_STR("Failed to allocate one GArray.");
176 bt_object_get_no_null_check(trace
->class);
177 BT_LIB_LOGD("Created trace object: %!+t", trace
);
181 BT_OBJECT_PUT_REF_AND_RESET(trace
);
187 const char *bt_trace_get_name(const struct bt_trace
*trace
)
189 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
190 return trace
->name
.value
;
193 enum bt_trace_status
bt_trace_set_name(struct bt_trace
*trace
, const char *name
)
195 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
196 BT_ASSERT_PRE_NON_NULL(name
, "Name");
197 BT_ASSERT_PRE_TRACE_HOT(trace
);
198 g_string_assign(trace
->name
.str
, name
);
199 trace
->name
.value
= trace
->name
.str
->str
;
200 BT_LIB_LOGV("Set trace's name: %!+t", trace
);
201 return BT_TRACE_STATUS_OK
;
204 uint64_t bt_trace_get_stream_count(const struct bt_trace
*trace
)
206 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
207 return (uint64_t) trace
->streams
->len
;
210 struct bt_stream
*bt_trace_borrow_stream_by_index(
211 struct bt_trace
*trace
, uint64_t index
)
213 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
214 BT_ASSERT_PRE_VALID_INDEX(index
, trace
->streams
->len
);
215 return g_ptr_array_index(trace
->streams
, index
);
218 const struct bt_stream
*bt_trace_borrow_stream_by_index_const(
219 const struct bt_trace
*trace
, uint64_t index
)
221 return bt_trace_borrow_stream_by_index((void *) trace
, index
);
224 struct bt_stream
*bt_trace_borrow_stream_by_id(struct bt_trace
*trace
,
227 struct bt_stream
*stream
= NULL
;
230 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
232 for (i
= 0; i
< trace
->streams
->len
; i
++) {
233 struct bt_stream
*stream_candidate
=
234 g_ptr_array_index(trace
->streams
, i
);
236 if (stream_candidate
->id
== id
) {
237 stream
= stream_candidate
;
246 const struct bt_stream
*bt_trace_borrow_stream_by_id_const(
247 const struct bt_trace
*trace
, uint64_t id
)
249 return bt_trace_borrow_stream_by_id((void *) trace
, id
);
252 enum bt_trace_status
bt_trace_add_destruction_listener(
253 const struct bt_trace
*c_trace
,
254 bt_trace_destruction_listener_func listener
,
255 void *data
, uint64_t *listener_id
)
257 struct bt_trace
*trace
= (void *) c_trace
;
259 struct bt_trace_destruction_listener_elem new_elem
= {
264 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
265 BT_ASSERT_PRE_NON_NULL(listener
, "Listener");
267 /* Find the next available spot */
268 for (i
= 0; i
< trace
->destruction_listeners
->len
; i
++) {
269 struct bt_trace_destruction_listener_elem elem
=
270 g_array_index(trace
->destruction_listeners
,
271 struct bt_trace_destruction_listener_elem
, i
);
278 if (i
== trace
->destruction_listeners
->len
) {
279 g_array_append_val(trace
->destruction_listeners
, new_elem
);
281 g_array_insert_val(trace
->destruction_listeners
, i
, new_elem
);
288 BT_LIB_LOGV("Added destruction listener: " "%![trace-]+t, "
289 "listener-id=%" PRIu64
, trace
, i
);
290 return BT_TRACE_STATUS_OK
;
295 bool has_listener_id(const struct bt_trace
*trace
, uint64_t listener_id
)
297 BT_ASSERT(listener_id
< trace
->destruction_listeners
->len
);
298 return (&g_array_index(trace
->destruction_listeners
,
299 struct bt_trace_destruction_listener_elem
,
300 listener_id
))->func
!= NULL
;
303 enum bt_trace_status
bt_trace_remove_destruction_listener(
304 const struct bt_trace
*c_trace
, uint64_t listener_id
)
306 struct bt_trace
*trace
= (void *) c_trace
;
307 struct bt_trace_destruction_listener_elem
*elem
;
309 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
310 BT_ASSERT_PRE(has_listener_id(trace
, listener_id
),
311 "Trace has no such trace destruction listener ID: "
312 "%![trace-]+t, %" PRIu64
, trace
, listener_id
);
313 elem
= &g_array_index(trace
->destruction_listeners
,
314 struct bt_trace_destruction_listener_elem
,
316 BT_ASSERT(elem
->func
);
320 BT_LIB_LOGV("Removed \"trace destruction listener: "
321 "%![trace-]+t, listener-id=%" PRIu64
,
323 return BT_TRACE_STATUS_OK
;
327 void _bt_trace_freeze(const struct bt_trace
*trace
)
330 BT_LIB_LOGD("Freezing trace's class: %!+T", trace
->class);
331 bt_trace_class_freeze(trace
->class);
332 BT_LIB_LOGD("Freezing trace: %!+t", trace
);
333 ((struct bt_trace
*) trace
)->frozen
= true;
337 void bt_trace_add_stream(struct bt_trace
*trace
, struct bt_stream
*stream
)
341 bt_object_set_parent(&stream
->base
, &trace
->base
);
342 g_ptr_array_add(trace
->streams
, stream
);
343 bt_trace_freeze(trace
);
345 if (bt_g_hash_table_contains(trace
->stream_classes_stream_count
,
347 count
= GPOINTER_TO_UINT(g_hash_table_lookup(
348 trace
->stream_classes_stream_count
, stream
->class));
351 g_hash_table_insert(trace
->stream_classes_stream_count
,
352 stream
->class, GUINT_TO_POINTER(count
+ 1));
356 uint64_t bt_trace_get_automatic_stream_id(const struct bt_trace
*trace
,
357 const struct bt_stream_class
*stream_class
)
363 BT_ASSERT(stream_class
);
365 if (g_hash_table_lookup_extended(trace
->stream_classes_stream_count
,
366 stream_class
, &orig_key
, &value
)) {
367 id
= (uint64_t) GPOINTER_TO_UINT(value
);
373 struct bt_trace_class
*bt_trace_borrow_class(struct bt_trace
*trace
)
375 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
379 const struct bt_trace_class
*bt_trace_borrow_class_const(
380 const struct bt_trace
*trace
)
382 return bt_trace_borrow_class((void *) trace
);
385 void bt_trace_get_ref(const struct bt_trace
*trace
)
387 bt_object_get_ref(trace
);
390 void bt_trace_put_ref(const struct bt_trace
*trace
)
392 bt_object_put_ref(trace
);