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 <babeltrace2/trace-ir/trace.h>
29 #include <babeltrace2/trace-ir/trace-const.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 <babeltrace2/value-const.h>
36 #include "lib/value.h"
37 #include <babeltrace2/types.h>
38 #include "compat/endian.h"
39 #include "common/assert.h"
40 #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 BT_LIB_LOGD("Calling trace destruction listener(s): %!+t", trace
);
86 * The trace's reference count is 0 if we're here. Increment
87 * it to avoid a double-destroy (possibly infinitely recursive).
88 * This could happen for example if a destruction listener did
89 * bt_object_get_ref() (or anything that causes
90 * bt_object_get_ref() to be called) on the trace (ref.
91 * count goes from 0 to 1), and then bt_object_put_ref(): the
92 * reference count would go from 1 to 0 again and this function
93 * would be called again.
95 trace
->base
.ref_count
++;
97 /* Call all the trace destruction listeners */
98 for (i
= 0; i
< trace
->destruction_listeners
->len
; i
++) {
99 struct bt_trace_destruction_listener_elem elem
=
100 g_array_index(trace
->destruction_listeners
,
101 struct bt_trace_destruction_listener_elem
, i
);
104 elem
.func(trace
, elem
.data
);
108 * The destruction listener should not have kept a
109 * reference to the trace.
111 BT_ASSERT_PRE(trace
->base
.ref_count
== 1, "Destruction listener kept a reference to the trace being destroyed: %![trace-]+t", trace
);
113 g_array_free(trace
->destruction_listeners
, TRUE
);
114 trace
->destruction_listeners
= NULL
;
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_LIB_LOGD("Creating trace object: %![tc-]+T", tc
);
151 trace
= g_new0(struct bt_trace
, 1);
153 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one trace.");
157 bt_object_init_shared(&trace
->base
, destroy_trace
);
158 trace
->user_attributes
= bt_value_map_create();
159 if (!trace
->user_attributes
) {
160 BT_LIB_LOGE_APPEND_CAUSE(
161 "Failed to create a map value object.");
165 trace
->streams
= g_ptr_array_new_with_free_func(
166 (GDestroyNotify
) bt_object_try_spec_release
);
167 if (!trace
->streams
) {
168 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray.");
172 trace
->stream_classes_stream_count
= g_hash_table_new(g_direct_hash
,
174 if (!trace
->stream_classes_stream_count
) {
175 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GHashTable.");
179 trace
->name
.str
= g_string_new(NULL
);
180 if (!trace
->name
.str
) {
181 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
185 trace
->environment
= bt_attributes_create();
186 if (!trace
->environment
) {
187 BT_LIB_LOGE_APPEND_CAUSE("Cannot create empty attributes object.");
191 trace
->destruction_listeners
= g_array_new(FALSE
, TRUE
,
192 sizeof(struct bt_trace_destruction_listener_elem
));
193 if (!trace
->destruction_listeners
) {
194 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GArray.");
199 bt_object_get_no_null_check(trace
->class);
200 BT_LIB_LOGD("Created trace object: %!+t", trace
);
204 BT_OBJECT_PUT_REF_AND_RESET(trace
);
210 const char *bt_trace_get_name(const struct bt_trace
*trace
)
212 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
213 return trace
->name
.value
;
216 enum bt_trace_set_name_status
bt_trace_set_name(struct bt_trace
*trace
,
219 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
220 BT_ASSERT_PRE_NON_NULL(name
, "Name");
221 BT_ASSERT_PRE_DEV_TRACE_HOT(trace
);
222 g_string_assign(trace
->name
.str
, name
);
223 trace
->name
.value
= trace
->name
.str
->str
;
224 BT_LIB_LOGD("Set trace's name: %!+t", trace
);
225 return BT_FUNC_STATUS_OK
;
228 bt_uuid
bt_trace_get_uuid(const struct bt_trace
*trace
)
230 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
231 return trace
->uuid
.value
;
234 void bt_trace_set_uuid(struct bt_trace
*trace
, bt_uuid uuid
)
236 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
237 BT_ASSERT_PRE_NON_NULL(uuid
, "UUID");
238 BT_ASSERT_PRE_DEV_TRACE_HOT(trace
);
239 bt_uuid_copy(trace
->uuid
.uuid
, uuid
);
240 trace
->uuid
.value
= trace
->uuid
.uuid
;
241 BT_LIB_LOGD("Set trace's UUID: %!+t", trace
);
246 bool trace_has_environment_entry(const struct bt_trace
*trace
, const char *name
)
250 return bt_attributes_borrow_field_value_by_name(
251 trace
->environment
, name
);
255 enum bt_trace_set_environment_entry_status
set_environment_entry(
256 struct bt_trace
*trace
,
257 const char *name
, struct bt_value
*value
)
264 BT_ASSERT_PRE(!trace
->frozen
||
265 !trace_has_environment_entry(trace
, name
),
266 "Trace is frozen: cannot replace environment entry: "
267 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
268 ret
= bt_attributes_set_field_value(trace
->environment
, name
,
271 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
272 BT_LIB_LOGE_APPEND_CAUSE(
273 "Cannot set trace's environment entry: "
274 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
276 bt_value_freeze(value
);
277 BT_LIB_LOGD("Set trace's environment entry: "
278 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
284 enum bt_trace_set_environment_entry_status
285 bt_trace_set_environment_entry_string(
286 struct bt_trace
*trace
, const char *name
, const char *value
)
289 struct bt_value
*value_obj
;
290 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
291 BT_ASSERT_PRE_NON_NULL(name
, "Name");
292 BT_ASSERT_PRE_NON_NULL(value
, "Value");
293 value_obj
= bt_value_string_create_init(value
);
295 BT_LIB_LOGE_APPEND_CAUSE(
296 "Cannot create a string value object.");
301 /* set_environment_entry() logs errors */
302 ret
= set_environment_entry(trace
, name
, value_obj
);
305 bt_object_put_ref(value_obj
);
309 enum bt_trace_set_environment_entry_status
310 bt_trace_set_environment_entry_integer(
311 struct bt_trace
*trace
, const char *name
, int64_t value
)
314 struct bt_value
*value_obj
;
315 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
316 BT_ASSERT_PRE_NON_NULL(name
, "Name");
317 value_obj
= bt_value_integer_signed_create_init(value
);
319 BT_LIB_LOGE_APPEND_CAUSE(
320 "Cannot create an integer value object.");
321 ret
= BT_FUNC_STATUS_MEMORY_ERROR
;
325 /* set_environment_entry() logs errors */
326 ret
= set_environment_entry(trace
, name
, value_obj
);
329 bt_object_put_ref(value_obj
);
333 uint64_t bt_trace_get_environment_entry_count(const struct bt_trace
*trace
)
337 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
338 ret
= bt_attributes_get_count(trace
->environment
);
340 return (uint64_t) ret
;
343 void bt_trace_borrow_environment_entry_by_index_const(
344 const struct bt_trace
*trace
, uint64_t index
,
345 const char **name
, const struct bt_value
**value
)
347 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
348 BT_ASSERT_PRE_DEV_NON_NULL(name
, "Name");
349 BT_ASSERT_PRE_DEV_NON_NULL(value
, "Value");
350 BT_ASSERT_PRE_DEV_VALID_INDEX(index
,
351 bt_attributes_get_count(trace
->environment
));
352 *value
= bt_attributes_borrow_field_value(trace
->environment
, index
);
354 *name
= bt_attributes_get_field_name(trace
->environment
, index
);
358 const struct bt_value
*bt_trace_borrow_environment_entry_value_by_name_const(
359 const struct bt_trace
*trace
, const char *name
)
361 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
362 BT_ASSERT_PRE_DEV_NON_NULL(name
, "Name");
363 return bt_attributes_borrow_field_value_by_name(trace
->environment
,
367 uint64_t bt_trace_get_stream_count(const struct bt_trace
*trace
)
369 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
370 return (uint64_t) trace
->streams
->len
;
373 struct bt_stream
*bt_trace_borrow_stream_by_index(
374 struct bt_trace
*trace
, uint64_t index
)
376 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
377 BT_ASSERT_PRE_DEV_VALID_INDEX(index
, trace
->streams
->len
);
378 return g_ptr_array_index(trace
->streams
, index
);
381 const struct bt_stream
*bt_trace_borrow_stream_by_index_const(
382 const struct bt_trace
*trace
, uint64_t index
)
384 return bt_trace_borrow_stream_by_index((void *) trace
, index
);
387 struct bt_stream
*bt_trace_borrow_stream_by_id(struct bt_trace
*trace
,
390 struct bt_stream
*stream
= NULL
;
393 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
395 for (i
= 0; i
< trace
->streams
->len
; i
++) {
396 struct bt_stream
*stream_candidate
=
397 g_ptr_array_index(trace
->streams
, i
);
399 if (stream_candidate
->id
== id
) {
400 stream
= stream_candidate
;
409 const struct bt_stream
*bt_trace_borrow_stream_by_id_const(
410 const struct bt_trace
*trace
, uint64_t id
)
412 return bt_trace_borrow_stream_by_id((void *) trace
, id
);
415 enum bt_trace_add_listener_status
bt_trace_add_destruction_listener(
416 const struct bt_trace
*c_trace
,
417 bt_trace_destruction_listener_func listener
,
418 void *data
, bt_listener_id
*listener_id
)
420 struct bt_trace
*trace
= (void *) c_trace
;
422 struct bt_trace_destruction_listener_elem new_elem
= {
427 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
428 BT_ASSERT_PRE_NON_NULL(listener
, "Listener");
430 /* Find the next available spot */
431 for (i
= 0; i
< trace
->destruction_listeners
->len
; i
++) {
432 struct bt_trace_destruction_listener_elem elem
=
433 g_array_index(trace
->destruction_listeners
,
434 struct bt_trace_destruction_listener_elem
, i
);
441 if (i
== trace
->destruction_listeners
->len
) {
442 g_array_append_val(trace
->destruction_listeners
, new_elem
);
444 g_array_insert_val(trace
->destruction_listeners
, i
, new_elem
);
451 BT_LIB_LOGD("Added destruction listener: " "%![trace-]+t, "
452 "listener-id=%" PRIu64
, trace
, i
);
453 return BT_FUNC_STATUS_OK
;
457 bool has_listener_id(const struct bt_trace
*trace
, uint64_t listener_id
)
459 BT_ASSERT(listener_id
< trace
->destruction_listeners
->len
);
460 return (&g_array_index(trace
->destruction_listeners
,
461 struct bt_trace_destruction_listener_elem
,
465 enum bt_trace_remove_listener_status
bt_trace_remove_destruction_listener(
466 const struct bt_trace
*c_trace
, bt_listener_id listener_id
)
468 struct bt_trace
*trace
= (void *) c_trace
;
469 struct bt_trace_destruction_listener_elem
*elem
;
471 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
472 BT_ASSERT_PRE(has_listener_id(trace
, listener_id
),
473 "Trace has no such trace destruction listener ID: "
474 "%![trace-]+t, %" PRIu64
, trace
, listener_id
);
475 elem
= &g_array_index(trace
->destruction_listeners
,
476 struct bt_trace_destruction_listener_elem
,
478 BT_ASSERT(elem
->func
);
482 BT_LIB_LOGD("Removed \"trace destruction listener: "
483 "%![trace-]+t, listener-id=%" PRIu64
,
485 return BT_FUNC_STATUS_OK
;
489 void _bt_trace_freeze(const struct bt_trace
*trace
)
492 BT_LIB_LOGD("Freezing trace's class: %!+T", trace
->class);
493 bt_trace_class_freeze(trace
->class);
494 BT_LIB_LOGD("Freezing trace's user attributes: %!+v",
495 trace
->user_attributes
);
496 bt_value_freeze(trace
->user_attributes
);
497 BT_LIB_LOGD("Freezing trace: %!+t", trace
);
498 ((struct bt_trace
*) trace
)->frozen
= true;
502 void bt_trace_add_stream(struct bt_trace
*trace
, struct bt_stream
*stream
)
506 bt_object_set_parent(&stream
->base
, &trace
->base
);
507 g_ptr_array_add(trace
->streams
, stream
);
508 bt_trace_freeze(trace
);
510 if (bt_g_hash_table_contains(trace
->stream_classes_stream_count
,
512 count
= GPOINTER_TO_UINT(g_hash_table_lookup(
513 trace
->stream_classes_stream_count
, stream
->class));
516 g_hash_table_insert(trace
->stream_classes_stream_count
,
517 stream
->class, GUINT_TO_POINTER(count
+ 1));
521 uint64_t bt_trace_get_automatic_stream_id(const struct bt_trace
*trace
,
522 const struct bt_stream_class
*stream_class
)
528 BT_ASSERT(stream_class
);
530 if (g_hash_table_lookup_extended(trace
->stream_classes_stream_count
,
531 stream_class
, &orig_key
, &value
)) {
532 id
= (uint64_t) GPOINTER_TO_UINT(value
);
538 struct bt_trace_class
*bt_trace_borrow_class(struct bt_trace
*trace
)
540 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
544 const struct bt_trace_class
*bt_trace_borrow_class_const(
545 const struct bt_trace
*trace
)
547 return bt_trace_borrow_class((void *) trace
);
550 const struct bt_value
*bt_trace_borrow_user_attributes_const(
551 const struct bt_trace
*trace
)
553 BT_ASSERT_PRE_DEV_NON_NULL(trace
, "Trace");
554 return trace
->user_attributes
;
557 struct bt_value
*bt_trace_borrow_user_attributes(struct bt_trace
*trace
)
559 return (void *) bt_trace_borrow_user_attributes_const((void *) trace
);
562 void bt_trace_set_user_attributes(
563 struct bt_trace
*trace
,
564 const struct bt_value
*user_attributes
)
566 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
567 BT_ASSERT_PRE_NON_NULL(user_attributes
, "User attributes");
568 BT_ASSERT_PRE(user_attributes
->type
== BT_VALUE_TYPE_MAP
,
569 "User attributes object is not a map value object.");
570 BT_ASSERT_PRE_DEV_TRACE_HOT(trace
);
571 bt_object_put_no_null_check(trace
->user_attributes
);
572 trace
->user_attributes
= (void *) user_attributes
;
573 bt_object_get_no_null_check(trace
->user_attributes
);
576 void bt_trace_get_ref(const struct bt_trace
*trace
)
578 bt_object_get_ref(trace
);
581 void bt_trace_put_ref(const struct bt_trace
*trace
)
583 bt_object_put_ref(trace
);