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 "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"
20 #include "compat/glib.h"
27 #include "attributes.h"
28 #include "stream-class.h"
30 #include "trace-class.h"
32 #include "lib/value.h"
33 #include "lib/func-status.h"
35 struct bt_trace_destruction_listener_elem
{
36 bt_trace_destruction_listener_func func
;
40 #define BT_ASSERT_PRE_DEV_TRACE_HOT(_trace) \
41 BT_ASSERT_PRE_DEV_HOT("trace", (_trace), "Trace", ": %!+t", (_trace))
43 #define DESTRUCTION_LISTENER_FUNC_NAME \
44 "bt_trace_class_destruction_listener_func"
47 void destroy_trace(struct bt_object
*obj
)
49 struct bt_trace
*trace
= (void *) obj
;
51 BT_LIB_LOGD("Destroying trace object: %!+t", trace
);
52 BT_OBJECT_PUT_REF_AND_RESET(trace
->user_attributes
);
55 * Call destruction listener functions so that everything else
56 * still exists in the trace.
58 if (trace
->destruction_listeners
) {
60 const struct bt_error
*saved_error
;
62 BT_LIB_LOGD("Calling trace destruction listener(s): %!+t", trace
);
65 * The trace's reference count is 0 if we're here. Increment
66 * it to avoid a double-destroy (possibly infinitely recursive).
67 * This could happen for example if a destruction listener did
68 * bt_object_get_ref() (or anything that causes
69 * bt_object_get_ref() to be called) on the trace (ref.
70 * count goes from 0 to 1), and then bt_object_put_ref(): the
71 * reference count would go from 1 to 0 again and this function
72 * would be called again.
74 trace
->base
.ref_count
++;
76 saved_error
= bt_current_thread_take_error();
78 /* Call all the trace destruction listeners */
79 for (i
= 0; i
< trace
->destruction_listeners
->len
; i
++) {
80 struct bt_trace_destruction_listener_elem elem
=
81 bt_g_array_index(trace
->destruction_listeners
,
82 struct bt_trace_destruction_listener_elem
, i
);
85 elem
.func(trace
, elem
.data
);
86 BT_ASSERT_POST_NO_ERROR(
87 DESTRUCTION_LISTENER_FUNC_NAME
);
91 * The destruction listener should not have kept a
92 * reference to the trace.
94 BT_ASSERT_POST(DESTRUCTION_LISTENER_FUNC_NAME
,
95 "trace-reference-count-not-changed",
96 trace
->base
.ref_count
== 1,
97 "Destruction listener kept a reference to the trace being destroyed: %![trace-]+t",
100 g_array_free(trace
->destruction_listeners
, TRUE
);
101 trace
->destruction_listeners
= NULL
;
104 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
111 if (trace
->class->mip_version
>= 1) {
112 g_free(trace
->uid_or_uuid
.uid
);
113 trace
->uid_or_uuid
.uid
= 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);
140 struct bt_trace
*bt_trace_create(struct bt_trace_class
*tc
)
142 struct bt_trace
*trace
= NULL
;
144 BT_ASSERT_PRE_NO_ERROR();
146 BT_LIB_LOGD("Creating trace object: %![tc-]+T", tc
);
147 trace
= g_new0(struct bt_trace
, 1);
149 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace.");
153 bt_object_init_shared(&trace
->base
, destroy_trace
);
154 trace
->user_attributes
= bt_value_map_create();
155 if (!trace
->user_attributes
) {
156 BT_LIB_LOGE_APPEND_CAUSE(
157 "Failed to create a map value object.");
161 trace
->streams
= g_ptr_array_new_with_free_func(
162 (GDestroyNotify
) bt_object_try_spec_release
);
163 if (!trace
->streams
) {
164 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
168 trace
->stream_classes_stream_count
= g_hash_table_new(g_direct_hash
,
170 if (!trace
->stream_classes_stream_count
) {
171 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GHashTable.");
175 trace
->environment
= bt_attributes_create();
176 if (!trace
->environment
) {
177 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty attributes object.");
181 trace
->destruction_listeners
= g_array_new(FALSE
, TRUE
,
182 sizeof(struct bt_trace_destruction_listener_elem
));
183 if (!trace
->destruction_listeners
) {
184 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
189 bt_object_get_ref_no_null_check(trace
->class);
190 BT_LIB_LOGD("Created trace object: %!+t", trace
);
194 BT_OBJECT_PUT_REF_AND_RESET(trace
);
201 const char *bt_trace_get_namespace(const struct bt_trace
*trace
)
203 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
204 BT_ASSERT_PRE_TRACE_MIP_VERSION_GE(trace
, 1);
209 enum bt_trace_set_namespace_status
bt_trace_set_namespace(
210 struct bt_trace
*trace
, const char *ns
)
212 BT_ASSERT_PRE_NO_ERROR();
213 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
214 BT_ASSERT_PRE_NAME_NON_NULL(ns
);
215 BT_ASSERT_PRE_DEV_TRACE_HOT(trace
);
216 BT_ASSERT_PRE_TRACE_MIP_VERSION_GE(trace
, 1);
218 trace
->ns
= g_strdup(ns
);
219 BT_LIB_LOGD("Set trace's namespace: %!+t", trace
);
220 return BT_FUNC_STATUS_OK
;
224 const char *bt_trace_get_name(const struct bt_trace
*trace
)
226 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
231 enum bt_trace_set_name_status
bt_trace_set_name(struct bt_trace
*trace
,
234 BT_ASSERT_PRE_NO_ERROR();
235 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
236 BT_ASSERT_PRE_NAME_NON_NULL(name
);
237 BT_ASSERT_PRE_DEV_TRACE_HOT(trace
);
239 trace
->name
= g_strdup(name
);
240 BT_LIB_LOGD("Set trace's name: %!+t", trace
);
241 return BT_FUNC_STATUS_OK
;
245 bt_uuid
bt_trace_get_uuid(const struct bt_trace
*trace
)
247 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
248 BT_ASSERT_PRE_TC_MIP_VERSION_EQ(trace
->class, 0);
249 return trace
->uid_or_uuid
.uuid
.value
;
253 const char *bt_trace_get_uid(const bt_trace
*trace
)
255 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
256 BT_ASSERT_PRE_TC_MIP_VERSION_GE(trace
->class, 1);
257 return trace
->uid_or_uuid
.uid
;
261 void bt_trace_set_uuid(struct bt_trace
*trace
, bt_uuid uuid
)
263 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
264 BT_ASSERT_PRE_UUID_NON_NULL(uuid
);
265 BT_ASSERT_PRE_DEV_TRACE_HOT(trace
);
266 BT_ASSERT_PRE_TC_MIP_VERSION_EQ(trace
->class, 0);
267 bt_uuid_copy(trace
->uid_or_uuid
.uuid
.uuid
, uuid
);
268 trace
->uid_or_uuid
.uuid
.value
= trace
->uid_or_uuid
.uuid
.uuid
;
269 BT_LIB_LOGD("Set trace's UUID: %!+t", trace
);
273 enum bt_trace_set_uid_status
bt_trace_set_uid(bt_trace
*trace
, const char *uid
)
275 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
276 BT_ASSERT_PRE_DEV_TRACE_HOT(trace
);
277 BT_ASSERT_PRE_TC_MIP_VERSION_GE(trace
->class, 1);
278 BT_ASSERT_PRE_UID_NON_NULL(uid
);
279 g_free(trace
->uid_or_uuid
.uid
);
280 trace
->uid_or_uuid
.uid
= g_strdup(uid
);
281 BT_LIB_LOGD("Set trace's UID: %!+t", trace
);
282 return BT_FUNC_STATUS_OK
;
286 bool trace_has_environment_entry(const struct bt_trace
*trace
, const char *name
)
290 return bt_attributes_borrow_field_value_by_name(
291 trace
->environment
, name
);
295 enum bt_trace_set_environment_entry_status
set_environment_entry(
296 struct bt_trace
*trace
,
297 const char *name
, struct bt_value
*value
)
304 BT_ASSERT_PRE("not-frozen:trace",
306 !trace_has_environment_entry(trace
, name
),
307 "Trace is frozen: cannot replace environment entry: "
308 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
309 ret
= bt_attributes_set_field_value(trace
->environment
, name
,
312 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
313 BT_LIB_LOGE_APPEND_CAUSE(
314 "Cannot set trace's environment entry: "
315 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
317 bt_value_freeze(value
);
318 BT_LIB_LOGD("Set trace's environment entry: "
319 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
326 enum bt_trace_set_environment_entry_status
327 bt_trace_set_environment_entry_string(
328 struct bt_trace
*trace
, const char *name
, const char *value
)
331 struct bt_value
*value_obj
;
333 BT_ASSERT_PRE_NO_ERROR();
334 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
335 BT_ASSERT_PRE_NAME_NON_NULL(name
);
336 BT_ASSERT_PRE_NON_NULL("value", value
, "Value");
338 value_obj
= bt_value_string_create_init(value
);
340 BT_LIB_LOGE_APPEND_CAUSE(
341 "Cannot create a string value object.");
346 /* set_environment_entry() logs errors */
347 ret
= set_environment_entry(trace
, name
, value_obj
);
350 bt_object_put_ref(value_obj
);
355 enum bt_trace_set_environment_entry_status
356 bt_trace_set_environment_entry_integer(
357 struct bt_trace
*trace
, const char *name
, int64_t value
)
360 struct bt_value
*value_obj
;
362 BT_ASSERT_PRE_NO_ERROR();
363 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
364 BT_ASSERT_PRE_NAME_NON_NULL(name
);
366 value_obj
= bt_value_integer_signed_create_init(value
);
368 BT_LIB_LOGE_APPEND_CAUSE(
369 "Cannot create an integer value object.");
370 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
374 /* set_environment_entry() logs errors */
375 ret
= set_environment_entry(trace
, name
, value_obj
);
378 bt_object_put_ref(value_obj
);
383 uint64_t bt_trace_get_environment_entry_count(const struct bt_trace
*trace
)
385 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
386 return bt_attributes_get_count(trace
->environment
);
390 void bt_trace_borrow_environment_entry_by_index_const(
391 const struct bt_trace
*trace
, uint64_t index
,
392 const char **name
, const struct bt_value
**value
)
394 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
395 BT_ASSERT_PRE_DEV_NAME_NON_NULL(name
);
396 BT_ASSERT_PRE_DEV_NON_NULL("value-object-output", value
,
397 "Value object (output)");
398 BT_ASSERT_PRE_DEV_VALID_INDEX(index
,
399 bt_attributes_get_count(trace
->environment
));
400 *value
= bt_attributes_borrow_field_value(trace
->environment
, index
);
402 *name
= bt_attributes_get_field_name(trace
->environment
, index
);
407 const struct bt_value
*bt_trace_borrow_environment_entry_value_by_name_const(
408 const struct bt_trace
*trace
, const char *name
)
410 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
411 BT_ASSERT_PRE_DEV_NAME_NON_NULL(name
);
412 return bt_attributes_borrow_field_value_by_name(trace
->environment
,
417 uint64_t bt_trace_get_stream_count(const struct bt_trace
*trace
)
419 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
420 return (uint64_t) trace
->streams
->len
;
424 struct bt_stream
*bt_trace_borrow_stream_by_index(
425 struct bt_trace
*trace
, uint64_t index
)
427 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
428 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, trace
->streams
->len
);
429 return g_ptr_array_index(trace
->streams
, index
);
433 const struct bt_stream
*bt_trace_borrow_stream_by_index_const(
434 const struct bt_trace
*trace
, uint64_t index
)
436 return bt_trace_borrow_stream_by_index((void *) trace
, index
);
440 struct bt_stream
*bt_trace_borrow_stream_by_id(struct bt_trace
*trace
,
443 struct bt_stream
*stream
= NULL
;
446 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
448 for (i
= 0; i
< trace
->streams
->len
; i
++) {
449 struct bt_stream
*stream_candidate
=
450 g_ptr_array_index(trace
->streams
, i
);
452 if (stream_candidate
->id
== id
) {
453 stream
= stream_candidate
;
463 const struct bt_stream
*bt_trace_borrow_stream_by_id_const(
464 const struct bt_trace
*trace
, uint64_t id
)
466 return bt_trace_borrow_stream_by_id((void *) trace
, id
);
470 enum bt_trace_add_listener_status
bt_trace_add_destruction_listener(
471 const struct bt_trace
*c_trace
,
472 bt_trace_destruction_listener_func listener
,
473 void *data
, bt_listener_id
*listener_id
)
475 struct bt_trace
*trace
= (void *) c_trace
;
477 struct bt_trace_destruction_listener_elem new_elem
= {
482 BT_ASSERT_PRE_NO_ERROR();
483 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
484 BT_ASSERT_PRE_LISTENER_FUNC_NON_NULL(listener
);
486 /* Find the next available spot */
487 for (i
= 0; i
< trace
->destruction_listeners
->len
; i
++) {
488 struct bt_trace_destruction_listener_elem elem
=
489 bt_g_array_index(trace
->destruction_listeners
,
490 struct bt_trace_destruction_listener_elem
, i
);
497 if (i
== trace
->destruction_listeners
->len
) {
498 g_array_append_val(trace
->destruction_listeners
, new_elem
);
500 g_array_insert_val(trace
->destruction_listeners
, i
, new_elem
);
507 BT_LIB_LOGD("Added destruction listener: " "%![trace-]+t, "
508 "listener-id=%" PRIu64
, trace
, i
);
509 return BT_FUNC_STATUS_OK
;
513 bool has_listener_id(const struct bt_trace
*trace
, uint64_t listener_id
)
515 BT_ASSERT(listener_id
< trace
->destruction_listeners
->len
);
516 return (&bt_g_array_index(trace
->destruction_listeners
,
517 struct bt_trace_destruction_listener_elem
,
522 enum bt_trace_remove_listener_status
bt_trace_remove_destruction_listener(
523 const struct bt_trace
*c_trace
, bt_listener_id listener_id
)
525 struct bt_trace
*trace
= (void *) c_trace
;
526 struct bt_trace_destruction_listener_elem
*elem
;
528 BT_ASSERT_PRE_NO_ERROR();
529 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
530 BT_ASSERT_PRE("listener-id-exists",
531 has_listener_id(trace
, listener_id
),
532 "Trace has no such trace destruction listener ID: "
533 "%![trace-]+t, %" PRIu64
, trace
, listener_id
);
534 elem
= &bt_g_array_index(trace
->destruction_listeners
,
535 struct bt_trace_destruction_listener_elem
,
537 BT_ASSERT(elem
->func
);
541 BT_LIB_LOGD("Removed \"trace destruction listener: "
542 "%![trace-]+t, listener-id=%" PRIu64
,
544 return BT_FUNC_STATUS_OK
;
547 void _bt_trace_freeze(const struct bt_trace
*trace
)
550 BT_LIB_LOGD("Freezing trace's class: %!+T", trace
->class);
551 bt_trace_class_freeze(trace
->class);
552 BT_LIB_LOGD("Freezing trace's user attributes: %!+v",
553 trace
->user_attributes
);
554 bt_value_freeze(trace
->user_attributes
);
555 BT_LIB_LOGD("Freezing trace: %!+t", trace
);
556 ((struct bt_trace
*) trace
)->frozen
= true;
559 void bt_trace_add_stream(struct bt_trace
*trace
, struct bt_stream
*stream
)
563 bt_object_set_parent(&stream
->base
, &trace
->base
);
564 g_ptr_array_add(trace
->streams
, stream
);
565 bt_trace_freeze(trace
);
567 if (bt_g_hash_table_contains(trace
->stream_classes_stream_count
,
569 count
= GPOINTER_TO_UINT(g_hash_table_lookup(
570 trace
->stream_classes_stream_count
, stream
->class));
573 g_hash_table_insert(trace
->stream_classes_stream_count
,
574 stream
->class, GUINT_TO_POINTER(count
+ 1));
577 uint64_t bt_trace_get_automatic_stream_id(const struct bt_trace
*trace
,
578 const struct bt_stream_class
*stream_class
)
584 BT_ASSERT(stream_class
);
586 if (g_hash_table_lookup_extended(trace
->stream_classes_stream_count
,
587 stream_class
, &orig_key
, &value
)) {
588 id
= (uint64_t) GPOINTER_TO_UINT(value
);
595 struct bt_trace_class
*bt_trace_borrow_class(struct bt_trace
*trace
)
597 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
602 const struct bt_trace_class
*bt_trace_borrow_class_const(
603 const struct bt_trace
*trace
)
605 return bt_trace_borrow_class((void *) trace
);
609 const struct bt_value
*bt_trace_borrow_user_attributes_const(
610 const struct bt_trace
*trace
)
612 BT_ASSERT_PRE_DEV_TRACE_NON_NULL(trace
);
613 return trace
->user_attributes
;
617 struct bt_value
*bt_trace_borrow_user_attributes(struct bt_trace
*trace
)
619 return (void *) bt_trace_borrow_user_attributes_const((void *) trace
);
623 void bt_trace_set_user_attributes(
624 struct bt_trace
*trace
,
625 const struct bt_value
*user_attributes
)
627 BT_ASSERT_PRE_TRACE_NON_NULL(trace
);
628 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
629 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
630 BT_ASSERT_PRE_DEV_TRACE_HOT(trace
);
631 bt_object_put_ref_no_null_check(trace
->user_attributes
);
632 trace
->user_attributes
= (void *) user_attributes
;
633 bt_object_get_ref_no_null_check(trace
->user_attributes
);
637 void bt_trace_get_ref(const struct bt_trace
*trace
)
639 bt_object_get_ref(trace
);
643 void bt_trace_put_ref(const struct bt_trace
*trace
)
645 bt_object_put_ref(trace
);