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"
9 #include "lib/logging.h"
11 #include "lib/assert-pre.h"
12 #include "lib/assert-post.h"
13 #include <babeltrace2/trace-ir/trace.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 "attributes.h"
31 #include "clock-class.h"
32 #include "event-class.h"
34 #include "field-class.h"
35 #include "field-wrapper.h"
36 #include "resolve-field-path.h"
37 #include "stream-class.h"
39 #include "trace-class.h"
42 #include "lib/value.h"
43 #include "lib/func-status.h"
45 struct bt_trace_destruction_listener_elem
{
46 bt_trace_destruction_listener_func func
;
50 #define BT_ASSERT_PRE_DEV_TRACE_HOT(_trace) \
51 BT_ASSERT_PRE_DEV_HOT((_trace), "Trace", ": %!+t", (_trace))
54 void destroy_trace(struct bt_object
*obj
)
56 struct bt_trace
*trace
= (void *) obj
;
58 BT_LIB_LOGD("Destroying trace object: %!+t", trace
);
59 BT_OBJECT_PUT_REF_AND_RESET(trace
->user_attributes
);
62 * Call destruction listener functions so that everything else
63 * still exists in the trace.
65 if (trace
->destruction_listeners
) {
67 const struct bt_error
*saved_error
;
69 BT_LIB_LOGD("Calling trace destruction listener(s): %!+t", trace
);
72 * The trace's 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 (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.
81 trace
->base
.ref_count
++;
83 saved_error
= bt_current_thread_take_error();
85 /* Call all the trace destruction listeners */
86 for (i
= 0; i
< trace
->destruction_listeners
->len
; i
++) {
87 struct bt_trace_destruction_listener_elem elem
=
88 g_array_index(trace
->destruction_listeners
,
89 struct bt_trace_destruction_listener_elem
, i
);
92 elem
.func(trace
, elem
.data
);
93 BT_ASSERT_POST_NO_ERROR();
97 * The destruction listener should not have kept a
98 * reference to the trace.
100 BT_ASSERT_POST(trace
->base
.ref_count
== 1, "Destruction listener kept a reference to the trace being destroyed: %![trace-]+t", trace
);
102 g_array_free(trace
->destruction_listeners
, TRUE
);
103 trace
->destruction_listeners
= NULL
;
106 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
110 if (trace
->name
.str
) {
111 g_string_free(trace
->name
.str
, TRUE
);
112 trace
->name
.str
= NULL
;
113 trace
->name
.value
= NULL
;
116 if (trace
->environment
) {
117 BT_LOGD_STR("Destroying environment attributes.");
118 bt_attributes_destroy(trace
->environment
);
119 trace
->environment
= NULL
;
122 if (trace
->streams
) {
123 BT_LOGD_STR("Destroying streams.");
124 g_ptr_array_free(trace
->streams
, TRUE
);
125 trace
->streams
= NULL
;
128 if (trace
->stream_classes_stream_count
) {
129 g_hash_table_destroy(trace
->stream_classes_stream_count
);
130 trace
->stream_classes_stream_count
= NULL
;
133 BT_LOGD_STR("Putting trace's class.");
134 bt_object_put_ref(trace
->class);
139 struct bt_trace
*bt_trace_create(struct bt_trace_class
*tc
)
141 struct bt_trace
*trace
= NULL
;
143 BT_ASSERT_PRE_NO_ERROR();
145 BT_LIB_LOGD("Creating trace object: %![tc-]+T", tc
);
146 trace
= g_new0(struct bt_trace
, 1);
148 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace.");
152 bt_object_init_shared(&trace
->base
, destroy_trace
);
153 trace
->user_attributes
= bt_value_map_create();
154 if (!trace
->user_attributes
) {
155 BT_LIB_LOGE_APPEND_CAUSE(
156 "Failed to create a map value object.");
160 trace
->streams
= g_ptr_array_new_with_free_func(
161 (GDestroyNotify
) bt_object_try_spec_release
);
162 if (!trace
->streams
) {
163 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
167 trace
->stream_classes_stream_count
= g_hash_table_new(g_direct_hash
,
169 if (!trace
->stream_classes_stream_count
) {
170 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GHashTable.");
174 trace
->name
.str
= g_string_new(NULL
);
175 if (!trace
->name
.str
) {
176 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
180 trace
->environment
= bt_attributes_create();
181 if (!trace
->environment
) {
182 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty attributes object.");
186 trace
->destruction_listeners
= g_array_new(FALSE
, TRUE
,
187 sizeof(struct bt_trace_destruction_listener_elem
));
188 if (!trace
->destruction_listeners
) {
189 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
194 bt_object_get_ref_no_null_check(trace
->class);
195 BT_LIB_LOGD("Created trace object: %!+t", trace
);
199 BT_OBJECT_PUT_REF_AND_RESET(trace
);
205 const char *bt_trace_get_name(const struct bt_trace
*trace
)
207 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
208 return trace
->name
.value
;
211 enum bt_trace_set_name_status
bt_trace_set_name(struct bt_trace
*trace
,
214 BT_ASSERT_PRE_NO_ERROR();
215 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
216 BT_ASSERT_PRE_NON_NULL(name
, "Name");
217 BT_ASSERT_PRE_DEV_TRACE_HOT(trace
);
218 g_string_assign(trace
->name
.str
, name
);
219 trace
->name
.value
= trace
->name
.str
->str
;
220 BT_LIB_LOGD("Set trace's name: %!+t", trace
);
221 return BT_FUNC_STATUS_OK
;
224 bt_uuid
bt_trace_get_uuid(const struct bt_trace
*trace
)
226 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
227 return trace
->uuid
.value
;
230 void bt_trace_set_uuid(struct bt_trace
*trace
, bt_uuid uuid
)
232 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
233 BT_ASSERT_PRE_NON_NULL(uuid
, "UUID");
234 BT_ASSERT_PRE_DEV_TRACE_HOT(trace
);
235 bt_uuid_copy(trace
->uuid
.uuid
, uuid
);
236 trace
->uuid
.value
= trace
->uuid
.uuid
;
237 BT_LIB_LOGD("Set trace's UUID: %!+t", trace
);
241 bool trace_has_environment_entry(const struct bt_trace
*trace
, const char *name
)
245 return bt_attributes_borrow_field_value_by_name(
246 trace
->environment
, name
);
250 enum bt_trace_set_environment_entry_status
set_environment_entry(
251 struct bt_trace
*trace
,
252 const char *name
, struct bt_value
*value
)
259 BT_ASSERT_PRE(!trace
->frozen
||
260 !trace_has_environment_entry(trace
, name
),
261 "Trace is frozen: cannot replace environment entry: "
262 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
263 ret
= bt_attributes_set_field_value(trace
->environment
, name
,
266 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
267 BT_LIB_LOGE_APPEND_CAUSE(
268 "Cannot set trace's environment entry: "
269 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
271 bt_value_freeze(value
);
272 BT_LIB_LOGD("Set trace's environment entry: "
273 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
279 enum bt_trace_set_environment_entry_status
280 bt_trace_set_environment_entry_string(
281 struct bt_trace
*trace
, const char *name
, const char *value
)
284 struct bt_value
*value_obj
;
286 BT_ASSERT_PRE_NO_ERROR();
287 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
288 BT_ASSERT_PRE_NON_NULL(name
, "Name");
289 BT_ASSERT_PRE_NON_NULL(value
, "Value");
291 value_obj
= bt_value_string_create_init(value
);
293 BT_LIB_LOGE_APPEND_CAUSE(
294 "Cannot create a string value object.");
299 /* set_environment_entry() logs errors */
300 ret
= set_environment_entry(trace
, name
, value_obj
);
303 bt_object_put_ref(value_obj
);
307 enum bt_trace_set_environment_entry_status
308 bt_trace_set_environment_entry_integer(
309 struct bt_trace
*trace
, const char *name
, int64_t value
)
312 struct bt_value
*value_obj
;
314 BT_ASSERT_PRE_NO_ERROR();
315 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
316 BT_ASSERT_PRE_NON_NULL(name
, "Name");
318 value_obj
= bt_value_integer_signed_create_init(value
);
320 BT_LIB_LOGE_APPEND_CAUSE(
321 "Cannot create an integer value object.");
322 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
326 /* set_environment_entry() logs errors */
327 ret
= set_environment_entry(trace
, name
, value_obj
);
330 bt_object_put_ref(value_obj
);
334 uint64_t bt_trace_get_environment_entry_count(const struct bt_trace
*trace
)
336 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
337 return bt_attributes_get_count(trace
->environment
);
340 void bt_trace_borrow_environment_entry_by_index_const(
341 const struct bt_trace
*trace
, uint64_t index
,
342 const char **name
, const struct bt_value
**value
)
344 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
345 BT_ASSERT_PRE_DEV_NON_NULL(name
, "Name");
346 BT_ASSERT_PRE_DEV_NON_NULL(value
, "Value");
347 BT_ASSERT_PRE_DEV_VALID_INDEX(index
,
348 bt_attributes_get_count(trace
->environment
));
349 *value
= bt_attributes_borrow_field_value(trace
->environment
, index
);
351 *name
= bt_attributes_get_field_name(trace
->environment
, index
);
355 const struct bt_value
*bt_trace_borrow_environment_entry_value_by_name_const(
356 const struct bt_trace
*trace
, const char *name
)
358 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
359 BT_ASSERT_PRE_DEV_NON_NULL(name
, "Name");
360 return bt_attributes_borrow_field_value_by_name(trace
->environment
,
364 uint64_t bt_trace_get_stream_count(const struct bt_trace
*trace
)
366 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
367 return (uint64_t) trace
->streams
->len
;
370 struct bt_stream
*bt_trace_borrow_stream_by_index(
371 struct bt_trace
*trace
, uint64_t index
)
373 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
374 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, trace
->streams
->len
);
375 return g_ptr_array_index(trace
->streams
, index
);
378 const struct bt_stream
*bt_trace_borrow_stream_by_index_const(
379 const struct bt_trace
*trace
, uint64_t index
)
381 return bt_trace_borrow_stream_by_index((void *) trace
, index
);
384 struct bt_stream
*bt_trace_borrow_stream_by_id(struct bt_trace
*trace
,
387 struct bt_stream
*stream
= NULL
;
390 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
392 for (i
= 0; i
< trace
->streams
->len
; i
++) {
393 struct bt_stream
*stream_candidate
=
394 g_ptr_array_index(trace
->streams
, i
);
396 if (stream_candidate
->id
== id
) {
397 stream
= stream_candidate
;
406 const struct bt_stream
*bt_trace_borrow_stream_by_id_const(
407 const struct bt_trace
*trace
, uint64_t id
)
409 return bt_trace_borrow_stream_by_id((void *) trace
, id
);
412 enum bt_trace_add_listener_status
bt_trace_add_destruction_listener(
413 const struct bt_trace
*c_trace
,
414 bt_trace_destruction_listener_func listener
,
415 void *data
, bt_listener_id
*listener_id
)
417 struct bt_trace
*trace
= (void *) c_trace
;
419 struct bt_trace_destruction_listener_elem new_elem
= {
424 BT_ASSERT_PRE_NO_ERROR();
425 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
426 BT_ASSERT_PRE_NON_NULL(listener
, "Listener");
428 /* Find the next available spot */
429 for (i
= 0; i
< trace
->destruction_listeners
->len
; i
++) {
430 struct bt_trace_destruction_listener_elem elem
=
431 g_array_index(trace
->destruction_listeners
,
432 struct bt_trace_destruction_listener_elem
, i
);
439 if (i
== trace
->destruction_listeners
->len
) {
440 g_array_append_val(trace
->destruction_listeners
, new_elem
);
442 g_array_insert_val(trace
->destruction_listeners
, i
, new_elem
);
449 BT_LIB_LOGD("Added destruction listener: " "%![trace-]+t, "
450 "listener-id=%" PRIu64
, trace
, i
);
451 return BT_FUNC_STATUS_OK
;
455 bool has_listener_id(const struct bt_trace
*trace
, uint64_t listener_id
)
457 BT_ASSERT(listener_id
< trace
->destruction_listeners
->len
);
458 return (&g_array_index(trace
->destruction_listeners
,
459 struct bt_trace_destruction_listener_elem
,
463 enum bt_trace_remove_listener_status
bt_trace_remove_destruction_listener(
464 const struct bt_trace
*c_trace
, bt_listener_id listener_id
)
466 struct bt_trace
*trace
= (void *) c_trace
;
467 struct bt_trace_destruction_listener_elem
*elem
;
469 BT_ASSERT_PRE_NO_ERROR();
470 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
471 BT_ASSERT_PRE(has_listener_id(trace
, listener_id
),
472 "Trace has no such trace destruction listener ID: "
473 "%![trace-]+t, %" PRIu64
, trace
, listener_id
);
474 elem
= &g_array_index(trace
->destruction_listeners
,
475 struct bt_trace_destruction_listener_elem
,
477 BT_ASSERT(elem
->func
);
481 BT_LIB_LOGD("Removed \"trace destruction listener: "
482 "%![trace-]+t, listener-id=%" PRIu64
,
484 return BT_FUNC_STATUS_OK
;
488 void _bt_trace_freeze(const struct bt_trace
*trace
)
491 BT_LIB_LOGD("Freezing trace's class: %!+T", trace
->class);
492 bt_trace_class_freeze(trace
->class);
493 BT_LIB_LOGD("Freezing trace's user attributes: %!+v",
494 trace
->user_attributes
);
495 bt_value_freeze(trace
->user_attributes
);
496 BT_LIB_LOGD("Freezing trace: %!+t", trace
);
497 ((struct bt_trace
*) trace
)->frozen
= true;
501 void bt_trace_add_stream(struct bt_trace
*trace
, struct bt_stream
*stream
)
505 bt_object_set_parent(&stream
->base
, &trace
->base
);
506 g_ptr_array_add(trace
->streams
, stream
);
507 bt_trace_freeze(trace
);
509 if (bt_g_hash_table_contains(trace
->stream_classes_stream_count
,
511 count
= GPOINTER_TO_UINT(g_hash_table_lookup(
512 trace
->stream_classes_stream_count
, stream
->class));
515 g_hash_table_insert(trace
->stream_classes_stream_count
,
516 stream
->class, GUINT_TO_POINTER(count
+ 1));
520 uint64_t bt_trace_get_automatic_stream_id(const struct bt_trace
*trace
,
521 const struct bt_stream_class
*stream_class
)
527 BT_ASSERT(stream_class
);
529 if (g_hash_table_lookup_extended(trace
->stream_classes_stream_count
,
530 stream_class
, &orig_key
, &value
)) {
531 id
= (uint64_t) GPOINTER_TO_UINT(value
);
537 struct bt_trace_class
*bt_trace_borrow_class(struct bt_trace
*trace
)
539 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
543 const struct bt_trace_class
*bt_trace_borrow_class_const(
544 const struct bt_trace
*trace
)
546 return bt_trace_borrow_class((void *) trace
);
549 const struct bt_value
*bt_trace_borrow_user_attributes_const(
550 const struct bt_trace
*trace
)
552 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
553 return trace
->user_attributes
;
556 struct bt_value
*bt_trace_borrow_user_attributes(struct bt_trace
*trace
)
558 return (void *) bt_trace_borrow_user_attributes_const((void *) trace
);
561 void bt_trace_set_user_attributes(
562 struct bt_trace
*trace
,
563 const struct bt_value
*user_attributes
)
565 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
566 BT_ASSERT_PRE_NON_NULL(user_attributes
, "User attributes");
567 BT_ASSERT_PRE(user_attributes
->type
== BT_VALUE_TYPE_MAP
,
568 "User attributes object is not a map value object.");
569 BT_ASSERT_PRE_DEV_TRACE_HOT(trace
);
570 bt_object_put_ref_no_null_check(trace
->user_attributes
);
571 trace
->user_attributes
= (void *) user_attributes
;
572 bt_object_get_ref_no_null_check(trace
->user_attributes
);
575 void bt_trace_get_ref(const struct bt_trace
*trace
)
577 bt_object_get_ref(trace
);
580 void bt_trace_put_ref(const struct bt_trace
*trace
)
582 bt_object_put_ref(trace
);