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-cond.h"
12 #include <babeltrace2/trace-ir/trace.h>
13 #include <babeltrace2/trace-ir/event-class.h>
14 #include "ctf-writer/functor.h"
15 #include "ctf-writer/clock.h"
16 #include "compat/compiler.h"
17 #include <babeltrace2/value.h>
18 #include "lib/value.h"
19 #include <babeltrace2/types.h>
20 #include "compat/endian.h"
21 #include "common/assert.h"
22 #include "compat/glib.h"
29 #include "attributes.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"
38 #include "trace-class.h"
41 #include "lib/value.h"
42 #include "lib/func-status.h"
44 struct bt_trace_destruction_listener_elem
{
45 bt_trace_destruction_listener_func func
;
49 #define BT_ASSERT_PRE_DEV_TRACE_HOT(_trace) \
50 BT_ASSERT_PRE_DEV_HOT("trace", (_trace), "Trace", ": %!+t", (_trace))
52 #define DESTRUCTION_LISTENER_FUNC_NAME \
53 "bt_trace_class_destruction_listener_func"
56 void destroy_trace(struct bt_object
*obj
)
58 struct bt_trace
*trace
= (void *) obj
;
60 BT_LIB_LOGD("Destroying trace object: %!+t", trace
);
61 BT_OBJECT_PUT_REF_AND_RESET(trace
->user_attributes
);
64 * Call destruction listener functions so that everything else
65 * still exists in the trace.
67 if (trace
->destruction_listeners
) {
69 const struct bt_error
*saved_error
;
71 BT_LIB_LOGD("Calling trace destruction listener(s): %!+t", trace
);
74 * The trace's reference count is 0 if we're here. Increment
75 * it to avoid a double-destroy (possibly infinitely recursive).
76 * This could happen for example if a destruction listener did
77 * bt_object_get_ref() (or anything that causes
78 * bt_object_get_ref() to be called) on the trace (ref.
79 * count goes from 0 to 1), and then bt_object_put_ref(): the
80 * reference count would go from 1 to 0 again and this function
81 * would be called again.
83 trace
->base
.ref_count
++;
85 saved_error
= bt_current_thread_take_error();
87 /* Call all the trace destruction listeners */
88 for (i
= 0; i
< trace
->destruction_listeners
->len
; i
++) {
89 struct bt_trace_destruction_listener_elem elem
=
90 g_array_index(trace
->destruction_listeners
,
91 struct bt_trace_destruction_listener_elem
, i
);
94 elem
.func(trace
, elem
.data
);
95 BT_ASSERT_POST_NO_ERROR(
96 DESTRUCTION_LISTENER_FUNC_NAME
);
100 * The destruction listener should not have kept a
101 * reference to the trace.
103 BT_ASSERT_POST(DESTRUCTION_LISTENER_FUNC_NAME
,
104 "trace-reference-count-not-changed",
105 trace
->base
.ref_count
== 1,
106 "Destruction listener kept a reference to the trace being destroyed: %![trace-]+t",
109 g_array_free(trace
->destruction_listeners
, TRUE
);
110 trace
->destruction_listeners
= NULL
;
113 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
117 if (trace
->name
.str
) {
118 g_string_free(trace
->name
.str
, TRUE
);
119 trace
->name
.str
= NULL
;
120 trace
->name
.value
= NULL
;
123 if (trace
->environment
) {
124 BT_LOGD_STR("Destroying environment attributes.");
125 bt_attributes_destroy(trace
->environment
);
126 trace
->environment
= NULL
;
129 if (trace
->streams
) {
130 BT_LOGD_STR("Destroying streams.");
131 g_ptr_array_free(trace
->streams
, TRUE
);
132 trace
->streams
= NULL
;
135 if (trace
->stream_classes_stream_count
) {
136 g_hash_table_destroy(trace
->stream_classes_stream_count
);
137 trace
->stream_classes_stream_count
= NULL
;
140 BT_LOGD_STR("Putting trace's class.");
141 bt_object_put_ref(trace
->class);
146 struct bt_trace
*bt_trace_create(struct bt_trace_class
*tc
)
148 struct bt_trace
*trace
= NULL
;
150 BT_ASSERT_PRE_NO_ERROR();
152 BT_LIB_LOGD("Creating trace object: %![tc-]+T", tc
);
153 trace
= g_new0(struct bt_trace
, 1);
155 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace.");
159 bt_object_init_shared(&trace
->base
, destroy_trace
);
160 trace
->user_attributes
= bt_value_map_create();
161 if (!trace
->user_attributes
) {
162 BT_LIB_LOGE_APPEND_CAUSE(
163 "Failed to create a map value object.");
167 trace
->streams
= g_ptr_array_new_with_free_func(
168 (GDestroyNotify
) bt_object_try_spec_release
);
169 if (!trace
->streams
) {
170 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
174 trace
->stream_classes_stream_count
= g_hash_table_new(g_direct_hash
,
176 if (!trace
->stream_classes_stream_count
) {
177 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GHashTable.");
181 trace
->name
.str
= g_string_new(NULL
);
182 if (!trace
->name
.str
) {
183 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
187 trace
->environment
= bt_attributes_create();
188 if (!trace
->environment
) {
189 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty attributes object.");
193 trace
->destruction_listeners
= g_array_new(FALSE
, TRUE
,
194 sizeof(struct bt_trace_destruction_listener_elem
));
195 if (!trace
->destruction_listeners
) {
196 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
201 bt_object_get_ref_no_null_check(trace
->class);
202 BT_LIB_LOGD("Created trace object: %!+t", trace
);
206 BT_OBJECT_PUT_REF_AND_RESET(trace
);
212 const char *bt_trace_get_name(const struct bt_trace
*trace
)
214 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
215 return trace
->name
.value
;
218 enum bt_trace_set_name_status
bt_trace_set_name(struct bt_trace
*trace
,
221 BT_ASSERT_PRE_NO_ERROR();
222 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
223 BT_ASSERT_PRE_NAME_NON_NULL(name
);
224 BT_ASSERT_PRE_DEV_TRACE_HOT(trace
);
225 g_string_assign(trace
->name
.str
, name
);
226 trace
->name
.value
= trace
->name
.str
->str
;
227 BT_LIB_LOGD("Set trace's name: %!+t", trace
);
228 return BT_FUNC_STATUS_OK
;
231 bt_uuid
bt_trace_get_uuid(const struct bt_trace
*trace
)
233 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
234 return trace
->uuid
.value
;
237 void bt_trace_set_uuid(struct bt_trace
*trace
, bt_uuid uuid
)
239 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
240 BT_ASSERT_PRE_UUID_NON_NULL(uuid
);
241 BT_ASSERT_PRE_DEV_TRACE_HOT(trace
);
242 bt_uuid_copy(trace
->uuid
.uuid
, uuid
);
243 trace
->uuid
.value
= trace
->uuid
.uuid
;
244 BT_LIB_LOGD("Set trace's UUID: %!+t", trace
);
248 bool trace_has_environment_entry(const struct bt_trace
*trace
, const char *name
)
252 return bt_attributes_borrow_field_value_by_name(
253 trace
->environment
, name
);
257 enum bt_trace_set_environment_entry_status
set_environment_entry(
258 struct bt_trace
*trace
,
259 const char *name
, struct bt_value
*value
)
266 BT_ASSERT_PRE("not-frozen:trace",
268 !trace_has_environment_entry(trace
, name
),
269 "Trace is frozen: cannot replace environment entry: "
270 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
271 ret
= bt_attributes_set_field_value(trace
->environment
, name
,
274 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
275 BT_LIB_LOGE_APPEND_CAUSE(
276 "Cannot set trace's environment entry: "
277 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
279 bt_value_freeze(value
);
280 BT_LIB_LOGD("Set trace's environment entry: "
281 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
287 enum bt_trace_set_environment_entry_status
288 bt_trace_set_environment_entry_string(
289 struct bt_trace
*trace
, const char *name
, const char *value
)
292 struct bt_value
*value_obj
;
294 BT_ASSERT_PRE_NO_ERROR();
295 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
296 BT_ASSERT_PRE_NAME_NON_NULL(name
);
297 BT_ASSERT_PRE_NON_NULL("value", value
, "Value");
299 value_obj
= bt_value_string_create_init(value
);
301 BT_LIB_LOGE_APPEND_CAUSE(
302 "Cannot create a string value object.");
307 /* set_environment_entry() logs errors */
308 ret
= set_environment_entry(trace
, name
, value_obj
);
311 bt_object_put_ref(value_obj
);
315 enum bt_trace_set_environment_entry_status
316 bt_trace_set_environment_entry_integer(
317 struct bt_trace
*trace
, const char *name
, int64_t value
)
320 struct bt_value
*value_obj
;
322 BT_ASSERT_PRE_NO_ERROR();
323 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
324 BT_ASSERT_PRE_NAME_NON_NULL(name
);
326 value_obj
= bt_value_integer_signed_create_init(value
);
328 BT_LIB_LOGE_APPEND_CAUSE(
329 "Cannot create an integer value object.");
330 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
334 /* set_environment_entry() logs errors */
335 ret
= set_environment_entry(trace
, name
, value_obj
);
338 bt_object_put_ref(value_obj
);
342 uint64_t bt_trace_get_environment_entry_count(const struct bt_trace
*trace
)
344 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
345 return bt_attributes_get_count(trace
->environment
);
348 void bt_trace_borrow_environment_entry_by_index_const(
349 const struct bt_trace
*trace
, uint64_t index
,
350 const char **name
, const struct bt_value
**value
)
352 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
353 BT_ASSERT_PRE_DEV_NAME_NON_NULL(name
);
354 BT_ASSERT_PRE_DEV_NON_NULL("value-object-output", value
,
355 "Value object (output)");
356 BT_ASSERT_PRE_DEV_VALID_INDEX(index
,
357 bt_attributes_get_count(trace
->environment
));
358 *value
= bt_attributes_borrow_field_value(trace
->environment
, index
);
360 *name
= bt_attributes_get_field_name(trace
->environment
, index
);
364 const struct bt_value
*bt_trace_borrow_environment_entry_value_by_name_const(
365 const struct bt_trace
*trace
, const char *name
)
367 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
368 BT_ASSERT_PRE_DEV_NAME_NON_NULL(name
);
369 return bt_attributes_borrow_field_value_by_name(trace
->environment
,
373 uint64_t bt_trace_get_stream_count(const struct bt_trace
*trace
)
375 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
376 return (uint64_t) trace
->streams
->len
;
379 struct bt_stream
*bt_trace_borrow_stream_by_index(
380 struct bt_trace
*trace
, uint64_t index
)
382 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
383 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, trace
->streams
->len
);
384 return g_ptr_array_index(trace
->streams
, index
);
387 const struct bt_stream
*bt_trace_borrow_stream_by_index_const(
388 const struct bt_trace
*trace
, uint64_t index
)
390 return bt_trace_borrow_stream_by_index((void *) trace
, index
);
393 struct bt_stream
*bt_trace_borrow_stream_by_id(struct bt_trace
*trace
,
396 struct bt_stream
*stream
= NULL
;
399 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
401 for (i
= 0; i
< trace
->streams
->len
; i
++) {
402 struct bt_stream
*stream_candidate
=
403 g_ptr_array_index(trace
->streams
, i
);
405 if (stream_candidate
->id
== id
) {
406 stream
= stream_candidate
;
415 const struct bt_stream
*bt_trace_borrow_stream_by_id_const(
416 const struct bt_trace
*trace
, uint64_t id
)
418 return bt_trace_borrow_stream_by_id((void *) trace
, id
);
421 enum bt_trace_add_listener_status
bt_trace_add_destruction_listener(
422 const struct bt_trace
*c_trace
,
423 bt_trace_destruction_listener_func listener
,
424 void *data
, bt_listener_id
*listener_id
)
426 struct bt_trace
*trace
= (void *) c_trace
;
428 struct bt_trace_destruction_listener_elem new_elem
= {
433 BT_ASSERT_PRE_NO_ERROR();
434 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
435 BT_ASSERT_PRE_LISTENER_FUNC_NON_NULL(listener
);
437 /* Find the next available spot */
438 for (i
= 0; i
< trace
->destruction_listeners
->len
; i
++) {
439 struct bt_trace_destruction_listener_elem elem
=
440 g_array_index(trace
->destruction_listeners
,
441 struct bt_trace_destruction_listener_elem
, i
);
448 if (i
== trace
->destruction_listeners
->len
) {
449 g_array_append_val(trace
->destruction_listeners
, new_elem
);
451 g_array_insert_val(trace
->destruction_listeners
, i
, new_elem
);
458 BT_LIB_LOGD("Added destruction listener: " "%![trace-]+t, "
459 "listener-id=%" PRIu64
, trace
, i
);
460 return BT_FUNC_STATUS_OK
;
464 bool has_listener_id(const struct bt_trace
*trace
, uint64_t listener_id
)
466 BT_ASSERT(listener_id
< trace
->destruction_listeners
->len
);
467 return (&g_array_index(trace
->destruction_listeners
,
468 struct bt_trace_destruction_listener_elem
,
472 enum bt_trace_remove_listener_status
bt_trace_remove_destruction_listener(
473 const struct bt_trace
*c_trace
, bt_listener_id listener_id
)
475 struct bt_trace
*trace
= (void *) c_trace
;
476 struct bt_trace_destruction_listener_elem
*elem
;
478 BT_ASSERT_PRE_NO_ERROR();
479 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
480 BT_ASSERT_PRE("listener-id-exists",
481 has_listener_id(trace
, listener_id
),
482 "Trace has no such trace destruction listener ID: "
483 "%![trace-]+t, %" PRIu64
, trace
, listener_id
);
484 elem
= &g_array_index(trace
->destruction_listeners
,
485 struct bt_trace_destruction_listener_elem
,
487 BT_ASSERT(elem
->func
);
491 BT_LIB_LOGD("Removed \"trace destruction listener: "
492 "%![trace-]+t, listener-id=%" PRIu64
,
494 return BT_FUNC_STATUS_OK
;
498 void _bt_trace_freeze(const struct bt_trace
*trace
)
501 BT_LIB_LOGD("Freezing trace's class: %!+T", trace
->class);
502 bt_trace_class_freeze(trace
->class);
503 BT_LIB_LOGD("Freezing trace's user attributes: %!+v",
504 trace
->user_attributes
);
505 bt_value_freeze(trace
->user_attributes
);
506 BT_LIB_LOGD("Freezing trace: %!+t", trace
);
507 ((struct bt_trace
*) trace
)->frozen
= true;
511 void bt_trace_add_stream(struct bt_trace
*trace
, struct bt_stream
*stream
)
515 bt_object_set_parent(&stream
->base
, &trace
->base
);
516 g_ptr_array_add(trace
->streams
, stream
);
517 bt_trace_freeze(trace
);
519 if (bt_g_hash_table_contains(trace
->stream_classes_stream_count
,
521 count
= GPOINTER_TO_UINT(g_hash_table_lookup(
522 trace
->stream_classes_stream_count
, stream
->class));
525 g_hash_table_insert(trace
->stream_classes_stream_count
,
526 stream
->class, GUINT_TO_POINTER(count
+ 1));
530 uint64_t bt_trace_get_automatic_stream_id(const struct bt_trace
*trace
,
531 const struct bt_stream_class
*stream_class
)
537 BT_ASSERT(stream_class
);
539 if (g_hash_table_lookup_extended(trace
->stream_classes_stream_count
,
540 stream_class
, &orig_key
, &value
)) {
541 id
= (uint64_t) GPOINTER_TO_UINT(value
);
547 struct bt_trace_class
*bt_trace_borrow_class(struct bt_trace
*trace
)
549 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
553 const struct bt_trace_class
*bt_trace_borrow_class_const(
554 const struct bt_trace
*trace
)
556 return bt_trace_borrow_class((void *) trace
);
559 const struct bt_value
*bt_trace_borrow_user_attributes_const(
560 const struct bt_trace
*trace
)
562 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
563 return trace
->user_attributes
;
566 struct bt_value
*bt_trace_borrow_user_attributes(struct bt_trace
*trace
)
568 return (void *) bt_trace_borrow_user_attributes_const((void *) trace
);
571 void bt_trace_set_user_attributes(
572 struct bt_trace
*trace
,
573 const struct bt_value
*user_attributes
)
575 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
576 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
577 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
578 BT_ASSERT_PRE_DEV_TRACE_HOT(trace
);
579 bt_object_put_ref_no_null_check(trace
->user_attributes
);
580 trace
->user_attributes
= (void *) user_attributes
;
581 bt_object_get_ref_no_null_check(trace
->user_attributes
);
584 void bt_trace_get_ref(const struct bt_trace
*trace
)
586 bt_object_get_ref(trace
);
589 void bt_trace_put_ref(const struct bt_trace
*trace
)
591 bt_object_put_ref(trace
);