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"
25 #include "lib/logging.h"
27 #include "lib/assert-pre.h"
28 #include "lib/assert-post.h"
29 #include <babeltrace2/trace-ir/trace.h>
30 #include <babeltrace2/trace-ir/event-class.h>
31 #include "ctf-writer/functor.h"
32 #include "ctf-writer/clock.h"
33 #include "compat/compiler.h"
34 #include <babeltrace2/value.h>
35 #include "lib/value.h"
36 #include <babeltrace2/types.h>
37 #include "compat/endian.h"
38 #include "common/assert.h"
39 #include "compat/glib.h"
46 #include "attributes.h"
47 #include "clock-class.h"
48 #include "event-class.h"
50 #include "field-class.h"
51 #include "field-wrapper.h"
52 #include "resolve-field-path.h"
53 #include "stream-class.h"
55 #include "trace-class.h"
58 #include "lib/value.h"
59 #include "lib/func-status.h"
61 struct bt_trace_destruction_listener_elem
{
62 bt_trace_destruction_listener_func func
;
66 #define BT_ASSERT_PRE_DEV_TRACE_HOT(_trace) \
67 BT_ASSERT_PRE_DEV_HOT((_trace), "Trace", ": %!+t", (_trace))
70 void destroy_trace(struct bt_object
*obj
)
72 struct bt_trace
*trace
= (void *) obj
;
74 BT_LIB_LOGD("Destroying trace object: %!+t", trace
);
75 BT_OBJECT_PUT_REF_AND_RESET(trace
->user_attributes
);
78 * Call destruction listener functions so that everything else
79 * still exists in the trace.
81 if (trace
->destruction_listeners
) {
83 const struct bt_error
*saved_error
;
85 BT_LIB_LOGD("Calling trace destruction listener(s): %!+t", trace
);
88 * The trace's reference count is 0 if we're here. Increment
89 * it to avoid a double-destroy (possibly infinitely recursive).
90 * This could happen for example if a destruction listener did
91 * bt_object_get_ref() (or anything that causes
92 * bt_object_get_ref() to be called) on the trace (ref.
93 * count goes from 0 to 1), and then bt_object_put_ref(): the
94 * reference count would go from 1 to 0 again and this function
95 * would be called again.
97 trace
->base
.ref_count
++;
99 saved_error
= bt_current_thread_take_error();
101 /* Call all the trace destruction listeners */
102 for (i
= 0; i
< trace
->destruction_listeners
->len
; i
++) {
103 struct bt_trace_destruction_listener_elem elem
=
104 g_array_index(trace
->destruction_listeners
,
105 struct bt_trace_destruction_listener_elem
, i
);
108 elem
.func(trace
, elem
.data
);
109 BT_ASSERT_POST_NO_ERROR();
113 * The destruction listener should not have kept a
114 * reference to the trace.
116 BT_ASSERT_POST(trace
->base
.ref_count
== 1, "Destruction listener kept a reference to the trace being destroyed: %![trace-]+t", trace
);
118 g_array_free(trace
->destruction_listeners
, TRUE
);
119 trace
->destruction_listeners
= NULL
;
122 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error
);
126 if (trace
->name
.str
) {
127 g_string_free(trace
->name
.str
, TRUE
);
128 trace
->name
.str
= NULL
;
129 trace
->name
.value
= NULL
;
132 if (trace
->environment
) {
133 BT_LOGD_STR("Destroying environment attributes.");
134 bt_attributes_destroy(trace
->environment
);
135 trace
->environment
= NULL
;
138 if (trace
->streams
) {
139 BT_LOGD_STR("Destroying streams.");
140 g_ptr_array_free(trace
->streams
, TRUE
);
141 trace
->streams
= NULL
;
144 if (trace
->stream_classes_stream_count
) {
145 g_hash_table_destroy(trace
->stream_classes_stream_count
);
146 trace
->stream_classes_stream_count
= NULL
;
149 BT_LOGD_STR("Putting trace's class.");
150 bt_object_put_ref(trace
->class);
155 struct bt_trace
*bt_trace_create(struct bt_trace_class
*tc
)
157 struct bt_trace
*trace
= NULL
;
159 BT_ASSERT_PRE_NO_ERROR();
161 BT_LIB_LOGD("Creating trace object: %![tc-]+T", tc
);
162 trace
= g_new0(struct bt_trace
, 1);
164 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace.");
168 bt_object_init_shared(&trace
->base
, destroy_trace
);
169 trace
->user_attributes
= bt_value_map_create();
170 if (!trace
->user_attributes
) {
171 BT_LIB_LOGE_APPEND_CAUSE(
172 "Failed to create a map value object.");
176 trace
->streams
= g_ptr_array_new_with_free_func(
177 (GDestroyNotify
) bt_object_try_spec_release
);
178 if (!trace
->streams
) {
179 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
183 trace
->stream_classes_stream_count
= g_hash_table_new(g_direct_hash
,
185 if (!trace
->stream_classes_stream_count
) {
186 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GHashTable.");
190 trace
->name
.str
= g_string_new(NULL
);
191 if (!trace
->name
.str
) {
192 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
196 trace
->environment
= bt_attributes_create();
197 if (!trace
->environment
) {
198 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty attributes object.");
202 trace
->destruction_listeners
= g_array_new(FALSE
, TRUE
,
203 sizeof(struct bt_trace_destruction_listener_elem
));
204 if (!trace
->destruction_listeners
) {
205 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
210 bt_object_get_ref_no_null_check(trace
->class);
211 BT_LIB_LOGD("Created trace object: %!+t", trace
);
215 BT_OBJECT_PUT_REF_AND_RESET(trace
);
221 const char *bt_trace_get_name(const struct bt_trace
*trace
)
223 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
224 return trace
->name
.value
;
227 enum bt_trace_set_name_status
bt_trace_set_name(struct bt_trace
*trace
,
230 BT_ASSERT_PRE_NO_ERROR();
231 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
232 BT_ASSERT_PRE_NON_NULL(name
, "Name");
233 BT_ASSERT_PRE_DEV_TRACE_HOT(trace
);
234 g_string_assign(trace
->name
.str
, name
);
235 trace
->name
.value
= trace
->name
.str
->str
;
236 BT_LIB_LOGD("Set trace's name: %!+t", trace
);
237 return BT_FUNC_STATUS_OK
;
240 bt_uuid
bt_trace_get_uuid(const struct bt_trace
*trace
)
242 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
243 return trace
->uuid
.value
;
246 void bt_trace_set_uuid(struct bt_trace
*trace
, bt_uuid uuid
)
248 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
249 BT_ASSERT_PRE_NON_NULL(uuid
, "UUID");
250 BT_ASSERT_PRE_DEV_TRACE_HOT(trace
);
251 bt_uuid_copy(trace
->uuid
.uuid
, uuid
);
252 trace
->uuid
.value
= trace
->uuid
.uuid
;
253 BT_LIB_LOGD("Set trace's UUID: %!+t", trace
);
257 bool trace_has_environment_entry(const struct bt_trace
*trace
, const char *name
)
261 return bt_attributes_borrow_field_value_by_name(
262 trace
->environment
, name
);
266 enum bt_trace_set_environment_entry_status
set_environment_entry(
267 struct bt_trace
*trace
,
268 const char *name
, struct bt_value
*value
)
275 BT_ASSERT_PRE(!trace
->frozen
||
276 !trace_has_environment_entry(trace
, name
),
277 "Trace is frozen: cannot replace environment entry: "
278 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
279 ret
= bt_attributes_set_field_value(trace
->environment
, name
,
282 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
283 BT_LIB_LOGE_APPEND_CAUSE(
284 "Cannot set trace's environment entry: "
285 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
287 bt_value_freeze(value
);
288 BT_LIB_LOGD("Set trace's environment entry: "
289 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
295 enum bt_trace_set_environment_entry_status
296 bt_trace_set_environment_entry_string(
297 struct bt_trace
*trace
, const char *name
, const char *value
)
300 struct bt_value
*value_obj
;
302 BT_ASSERT_PRE_NO_ERROR();
303 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
304 BT_ASSERT_PRE_NON_NULL(name
, "Name");
305 BT_ASSERT_PRE_NON_NULL(value
, "Value");
307 value_obj
= bt_value_string_create_init(value
);
309 BT_LIB_LOGE_APPEND_CAUSE(
310 "Cannot create a string value object.");
315 /* set_environment_entry() logs errors */
316 ret
= set_environment_entry(trace
, name
, value_obj
);
319 bt_object_put_ref(value_obj
);
323 enum bt_trace_set_environment_entry_status
324 bt_trace_set_environment_entry_integer(
325 struct bt_trace
*trace
, const char *name
, int64_t value
)
328 struct bt_value
*value_obj
;
330 BT_ASSERT_PRE_NO_ERROR();
331 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
332 BT_ASSERT_PRE_NON_NULL(name
, "Name");
334 value_obj
= bt_value_integer_signed_create_init(value
);
336 BT_LIB_LOGE_APPEND_CAUSE(
337 "Cannot create an integer value object.");
338 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
342 /* set_environment_entry() logs errors */
343 ret
= set_environment_entry(trace
, name
, value_obj
);
346 bt_object_put_ref(value_obj
);
350 uint64_t bt_trace_get_environment_entry_count(const struct bt_trace
*trace
)
352 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
353 return bt_attributes_get_count(trace
->environment
);
356 void bt_trace_borrow_environment_entry_by_index_const(
357 const struct bt_trace
*trace
, uint64_t index
,
358 const char **name
, const struct bt_value
**value
)
360 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
361 BT_ASSERT_PRE_DEV_NON_NULL(name
, "Name");
362 BT_ASSERT_PRE_DEV_NON_NULL(value
, "Value");
363 BT_ASSERT_PRE_DEV_VALID_INDEX(index
,
364 bt_attributes_get_count(trace
->environment
));
365 *value
= bt_attributes_borrow_field_value(trace
->environment
, index
);
367 *name
= bt_attributes_get_field_name(trace
->environment
, index
);
371 const struct bt_value
*bt_trace_borrow_environment_entry_value_by_name_const(
372 const struct bt_trace
*trace
, const char *name
)
374 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
375 BT_ASSERT_PRE_DEV_NON_NULL(name
, "Name");
376 return bt_attributes_borrow_field_value_by_name(trace
->environment
,
380 uint64_t bt_trace_get_stream_count(const struct bt_trace
*trace
)
382 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
383 return (uint64_t) trace
->streams
->len
;
386 struct bt_stream
*bt_trace_borrow_stream_by_index(
387 struct bt_trace
*trace
, uint64_t index
)
389 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
390 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, trace
->streams
->len
);
391 return g_ptr_array_index(trace
->streams
, index
);
394 const struct bt_stream
*bt_trace_borrow_stream_by_index_const(
395 const struct bt_trace
*trace
, uint64_t index
)
397 return bt_trace_borrow_stream_by_index((void *) trace
, index
);
400 struct bt_stream
*bt_trace_borrow_stream_by_id(struct bt_trace
*trace
,
403 struct bt_stream
*stream
= NULL
;
406 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
408 for (i
= 0; i
< trace
->streams
->len
; i
++) {
409 struct bt_stream
*stream_candidate
=
410 g_ptr_array_index(trace
->streams
, i
);
412 if (stream_candidate
->id
== id
) {
413 stream
= stream_candidate
;
422 const struct bt_stream
*bt_trace_borrow_stream_by_id_const(
423 const struct bt_trace
*trace
, uint64_t id
)
425 return bt_trace_borrow_stream_by_id((void *) trace
, id
);
428 enum bt_trace_add_listener_status
bt_trace_add_destruction_listener(
429 const struct bt_trace
*c_trace
,
430 bt_trace_destruction_listener_func listener
,
431 void *data
, bt_listener_id
*listener_id
)
433 struct bt_trace
*trace
= (void *) c_trace
;
435 struct bt_trace_destruction_listener_elem new_elem
= {
440 BT_ASSERT_PRE_NO_ERROR();
441 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
442 BT_ASSERT_PRE_NON_NULL(listener
, "Listener");
444 /* Find the next available spot */
445 for (i
= 0; i
< trace
->destruction_listeners
->len
; i
++) {
446 struct bt_trace_destruction_listener_elem elem
=
447 g_array_index(trace
->destruction_listeners
,
448 struct bt_trace_destruction_listener_elem
, i
);
455 if (i
== trace
->destruction_listeners
->len
) {
456 g_array_append_val(trace
->destruction_listeners
, new_elem
);
458 g_array_insert_val(trace
->destruction_listeners
, i
, new_elem
);
465 BT_LIB_LOGD("Added destruction listener: " "%![trace-]+t, "
466 "listener-id=%" PRIu64
, trace
, i
);
467 return BT_FUNC_STATUS_OK
;
471 bool has_listener_id(const struct bt_trace
*trace
, uint64_t listener_id
)
473 BT_ASSERT(listener_id
< trace
->destruction_listeners
->len
);
474 return (&g_array_index(trace
->destruction_listeners
,
475 struct bt_trace_destruction_listener_elem
,
479 enum bt_trace_remove_listener_status
bt_trace_remove_destruction_listener(
480 const struct bt_trace
*c_trace
, bt_listener_id listener_id
)
482 struct bt_trace
*trace
= (void *) c_trace
;
483 struct bt_trace_destruction_listener_elem
*elem
;
485 BT_ASSERT_PRE_NO_ERROR();
486 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
487 BT_ASSERT_PRE(has_listener_id(trace
, listener_id
),
488 "Trace has no such trace destruction listener ID: "
489 "%![trace-]+t, %" PRIu64
, trace
, listener_id
);
490 elem
= &g_array_index(trace
->destruction_listeners
,
491 struct bt_trace_destruction_listener_elem
,
493 BT_ASSERT(elem
->func
);
497 BT_LIB_LOGD("Removed \"trace destruction listener: "
498 "%![trace-]+t, listener-id=%" PRIu64
,
500 return BT_FUNC_STATUS_OK
;
504 void _bt_trace_freeze(const struct bt_trace
*trace
)
507 BT_LIB_LOGD("Freezing trace's class: %!+T", trace
->class);
508 bt_trace_class_freeze(trace
->class);
509 BT_LIB_LOGD("Freezing trace's user attributes: %!+v",
510 trace
->user_attributes
);
511 bt_value_freeze(trace
->user_attributes
);
512 BT_LIB_LOGD("Freezing trace: %!+t", trace
);
513 ((struct bt_trace
*) trace
)->frozen
= true;
517 void bt_trace_add_stream(struct bt_trace
*trace
, struct bt_stream
*stream
)
521 bt_object_set_parent(&stream
->base
, &trace
->base
);
522 g_ptr_array_add(trace
->streams
, stream
);
523 bt_trace_freeze(trace
);
525 if (bt_g_hash_table_contains(trace
->stream_classes_stream_count
,
527 count
= GPOINTER_TO_UINT(g_hash_table_lookup(
528 trace
->stream_classes_stream_count
, stream
->class));
531 g_hash_table_insert(trace
->stream_classes_stream_count
,
532 stream
->class, GUINT_TO_POINTER(count
+ 1));
536 uint64_t bt_trace_get_automatic_stream_id(const struct bt_trace
*trace
,
537 const struct bt_stream_class
*stream_class
)
543 BT_ASSERT(stream_class
);
545 if (g_hash_table_lookup_extended(trace
->stream_classes_stream_count
,
546 stream_class
, &orig_key
, &value
)) {
547 id
= (uint64_t) GPOINTER_TO_UINT(value
);
553 struct bt_trace_class
*bt_trace_borrow_class(struct bt_trace
*trace
)
555 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
559 const struct bt_trace_class
*bt_trace_borrow_class_const(
560 const struct bt_trace
*trace
)
562 return bt_trace_borrow_class((void *) trace
);
565 const struct bt_value
*bt_trace_borrow_user_attributes_const(
566 const struct bt_trace
*trace
)
568 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
569 return trace
->user_attributes
;
572 struct bt_value
*bt_trace_borrow_user_attributes(struct bt_trace
*trace
)
574 return (void *) bt_trace_borrow_user_attributes_const((void *) trace
);
577 void bt_trace_set_user_attributes(
578 struct bt_trace
*trace
,
579 const struct bt_value
*user_attributes
)
581 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
582 BT_ASSERT_PRE_NON_NULL(user_attributes
, "User attributes");
583 BT_ASSERT_PRE(user_attributes
->type
== BT_VALUE_TYPE_MAP
,
584 "User attributes object is not a map value object.");
585 BT_ASSERT_PRE_DEV_TRACE_HOT(trace
);
586 bt_object_put_ref_no_null_check(trace
->user_attributes
);
587 trace
->user_attributes
= (void *) user_attributes
;
588 bt_object_get_ref_no_null_check(trace
->user_attributes
);
591 void bt_trace_get_ref(const struct bt_trace
*trace
)
593 bt_object_get_ref(trace
);
596 void bt_trace_put_ref(const struct bt_trace
*trace
)
598 bt_object_put_ref(trace
);