2 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "TRACE"
26 #include <babeltrace/lib-logging-internal.h>
28 #include <babeltrace/assert-pre-internal.h>
29 #include <babeltrace/trace-ir/trace-const.h>
30 #include <babeltrace/trace-ir/trace-internal.h>
31 #include <babeltrace/trace-ir/clock-class-internal.h>
32 #include <babeltrace/trace-ir/stream-internal.h>
33 #include <babeltrace/trace-ir/stream-class-internal.h>
34 #include <babeltrace/trace-ir/event-internal.h>
35 #include <babeltrace/trace-ir/event-class.h>
36 #include <babeltrace/trace-ir/event-class-internal.h>
37 #include <babeltrace/ctf-writer/functor-internal.h>
38 #include <babeltrace/ctf-writer/clock-internal.h>
39 #include <babeltrace/trace-ir/field-wrapper-internal.h>
40 #include <babeltrace/trace-ir/field-classes-internal.h>
41 #include <babeltrace/trace-ir/attributes-internal.h>
42 #include <babeltrace/trace-ir/utils-internal.h>
43 #include <babeltrace/trace-ir/resolve-field-path-internal.h>
44 #include <babeltrace/compiler-internal.h>
45 #include <babeltrace/values.h>
46 #include <babeltrace/values-const.h>
47 #include <babeltrace/values-internal.h>
48 #include <babeltrace/object.h>
49 #include <babeltrace/types.h>
50 #include <babeltrace/endian-internal.h>
51 #include <babeltrace/assert-internal.h>
52 #include <babeltrace/compat/glib-internal.h>
58 struct bt_trace_is_static_listener_elem
{
59 bt_trace_is_static_listener_func func
;
60 bt_trace_listener_removed_func removed
;
64 #define BT_ASSERT_PRE_TRACE_HOT(_trace) \
65 BT_ASSERT_PRE_HOT((_trace), "Trace", ": %!+t", (_trace))
68 void destroy_trace(struct bt_object
*obj
)
70 struct bt_trace
*trace
= (void *) obj
;
72 BT_LIB_LOGD("Destroying trace object: %!+t", trace
);
75 * Call remove listeners first so that everything else still
76 * exists in the trace.
78 if (trace
->is_static_listeners
) {
81 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
82 struct bt_trace_is_static_listener_elem elem
=
83 g_array_index(trace
->is_static_listeners
,
84 struct bt_trace_is_static_listener_elem
, i
);
87 elem
.removed((void *) trace
, elem
.data
);
91 g_array_free(trace
->is_static_listeners
, TRUE
);
92 trace
->is_static_listeners
= NULL
;
95 bt_object_pool_finalize(&trace
->packet_header_field_pool
);
97 if (trace
->environment
) {
98 BT_LOGD_STR("Destroying environment attributes.");
99 bt_attributes_destroy(trace
->environment
);
100 trace
->environment
= NULL
;
103 if (trace
->name
.str
) {
104 g_string_free(trace
->name
.str
, TRUE
);
105 trace
->name
.str
= NULL
;
106 trace
->name
.value
= NULL
;
109 if (trace
->streams
) {
110 BT_LOGD_STR("Destroying streams.");
111 g_ptr_array_free(trace
->streams
, TRUE
);
112 trace
->streams
= NULL
;
115 if (trace
->stream_classes
) {
116 BT_LOGD_STR("Destroying stream classes.");
117 g_ptr_array_free(trace
->stream_classes
, TRUE
);
118 trace
->stream_classes
= NULL
;
121 if (trace
->stream_classes_stream_count
) {
122 g_hash_table_destroy(trace
->stream_classes_stream_count
);
123 trace
->stream_classes_stream_count
= NULL
;
126 BT_LOGD_STR("Putting packet header field classe.");
127 bt_object_put_ref(trace
->packet_header_fc
);
128 trace
->packet_header_fc
= NULL
;
133 void free_packet_header_field(struct bt_field_wrapper
*field_wrapper
,
134 struct bt_trace
*trace
)
136 bt_field_wrapper_destroy(field_wrapper
);
139 struct bt_trace
*bt_trace_create(void)
141 struct bt_trace
*trace
= NULL
;
144 BT_LOGD_STR("Creating default trace object.");
145 trace
= g_new0(struct bt_trace
, 1);
147 BT_LOGE_STR("Failed to allocate one trace.");
151 bt_object_init_shared_with_parent(&trace
->base
, destroy_trace
);
152 trace
->streams
= g_ptr_array_new_with_free_func(
153 (GDestroyNotify
) bt_object_try_spec_release
);
154 if (!trace
->streams
) {
155 BT_LOGE_STR("Failed to allocate one GPtrArray.");
159 trace
->stream_classes
= g_ptr_array_new_with_free_func(
160 (GDestroyNotify
) bt_object_try_spec_release
);
161 if (!trace
->stream_classes
) {
162 BT_LOGE_STR("Failed to allocate one GPtrArray.");
166 trace
->stream_classes_stream_count
= g_hash_table_new(g_direct_hash
,
168 if (!trace
->stream_classes_stream_count
) {
169 BT_LOGE_STR("Failed to allocate one GHashTable.");
173 trace
->name
.str
= g_string_new(NULL
);
174 if (!trace
->name
.str
) {
175 BT_LOGE_STR("Failed to allocate one GString.");
179 trace
->environment
= bt_attributes_create();
180 if (!trace
->environment
) {
181 BT_LOGE_STR("Cannot create empty attributes object.");
185 trace
->is_static_listeners
= g_array_new(FALSE
, TRUE
,
186 sizeof(struct bt_trace_is_static_listener_elem
));
187 if (!trace
->is_static_listeners
) {
188 BT_LOGE_STR("Failed to allocate one GArray.");
192 trace
->assigns_automatic_stream_class_id
= true;
193 ret
= bt_object_pool_initialize(&trace
->packet_header_field_pool
,
194 (bt_object_pool_new_object_func
) bt_field_wrapper_new
,
195 (bt_object_pool_destroy_object_func
) free_packet_header_field
,
198 BT_LOGE("Failed to initialize packet header field pool: ret=%d",
203 BT_LIB_LOGD("Created trace object: %!+t", trace
);
207 BT_OBJECT_PUT_REF_AND_RESET(trace
);
210 return (void *) trace
;
213 const char *bt_trace_get_name(const struct bt_trace
*trace
)
215 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
216 return trace
->name
.value
;
219 int bt_trace_set_name(struct bt_trace
*trace
, const char *name
)
221 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
222 BT_ASSERT_PRE_NON_NULL(name
, "Name");
223 BT_ASSERT_PRE_TRACE_HOT(trace
);
224 g_string_assign(trace
->name
.str
, name
);
225 trace
->name
.value
= trace
->name
.str
->str
;
226 BT_LIB_LOGV("Set trace's name: %!+t", trace
);
230 bt_uuid
bt_trace_get_uuid(const struct bt_trace
*trace
)
232 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
233 return trace
->uuid
.value
;
236 void bt_trace_set_uuid(struct bt_trace
*trace
, bt_uuid uuid
)
238 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
239 BT_ASSERT_PRE_NON_NULL(uuid
, "UUID");
240 BT_ASSERT_PRE_TRACE_HOT(trace
);
241 memcpy(trace
->uuid
.uuid
, uuid
, BABELTRACE_UUID_LEN
);
242 trace
->uuid
.value
= trace
->uuid
.uuid
;
243 BT_LIB_LOGV("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
) != NULL
;
257 int set_environment_entry(struct bt_trace
*trace
, const char *name
,
258 struct bt_value
*value
)
265 BT_ASSERT_PRE(!trace
->frozen
||
266 !trace_has_environment_entry(trace
, name
),
267 "Trace is frozen: cannot replace environment entry: "
268 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
269 ret
= bt_attributes_set_field_value(trace
->environment
, name
,
271 bt_value_freeze(value
);
273 BT_LIB_LOGE("Cannot set trace's environment entry: "
274 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
276 BT_LIB_LOGV("Set trace's environment entry: "
277 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
283 int bt_trace_set_environment_entry_string(
284 struct bt_trace
*trace
,
285 const char *name
, const char *value
)
288 struct bt_value
*value_obj
;
289 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
290 BT_ASSERT_PRE_NON_NULL(name
, "Name");
291 BT_ASSERT_PRE_NON_NULL(value
, "Value");
292 value_obj
= bt_value_string_create_init(value
);
294 BT_LOGE_STR("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 int bt_trace_set_environment_entry_integer(struct bt_trace
*trace
,
308 const char *name
, int64_t value
)
311 struct bt_value
*value_obj
;
312 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
313 BT_ASSERT_PRE_NON_NULL(name
, "Name");
314 value_obj
= bt_value_integer_create_init(value
);
316 BT_LOGE_STR("Cannot create an integer value object.");
321 /* set_environment_entry() logs errors */
322 ret
= set_environment_entry(trace
, name
, value_obj
);
325 bt_object_put_ref(value_obj
);
329 uint64_t bt_trace_get_environment_entry_count(const struct bt_trace
*trace
)
333 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
334 ret
= bt_attributes_get_count(trace
->environment
);
336 return (uint64_t) ret
;
339 void bt_trace_borrow_environment_entry_by_index_const(
340 const struct bt_trace
*trace
, uint64_t index
,
341 const char **name
, const struct bt_value
**value
)
343 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
344 BT_ASSERT_PRE_NON_NULL(name
, "Name");
345 BT_ASSERT_PRE_NON_NULL(value
, "Value");
346 BT_ASSERT_PRE_VALID_INDEX(index
,
347 bt_attributes_get_count(trace
->environment
));
348 *value
= bt_attributes_borrow_field_value(trace
->environment
, index
);
350 *name
= bt_attributes_get_field_name(trace
->environment
, index
);
354 const struct bt_value
*bt_trace_borrow_environment_entry_value_by_name_const(
355 const struct bt_trace
*trace
, const char *name
)
357 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
358 BT_ASSERT_PRE_NON_NULL(name
, "Name");
359 return bt_attributes_borrow_field_value_by_name(trace
->environment
,
363 uint64_t bt_trace_get_stream_count(const struct bt_trace
*trace
)
365 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
366 return (uint64_t) trace
->streams
->len
;
369 struct bt_stream
*bt_trace_borrow_stream_by_index(
370 struct bt_trace
*trace
, uint64_t index
)
372 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
373 BT_ASSERT_PRE_VALID_INDEX(index
, trace
->streams
->len
);
374 return g_ptr_array_index(trace
->streams
, index
);
377 const struct bt_stream
*bt_trace_borrow_stream_by_index_const(
378 const struct bt_trace
*trace
, uint64_t index
)
380 return bt_trace_borrow_stream_by_index((void *) trace
, index
);
383 struct bt_stream
*bt_trace_borrow_stream_by_id(struct bt_trace
*trace
,
386 struct bt_stream
*stream
= NULL
;
389 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
391 for (i
= 0; i
< trace
->streams
->len
; i
++) {
392 struct bt_stream
*stream_candidate
=
393 g_ptr_array_index(trace
->streams
, i
);
395 if (stream_candidate
->id
== id
) {
396 stream
= stream_candidate
;
405 const struct bt_stream
*bt_trace_borrow_stream_by_id_const(
406 const struct bt_trace
*trace
, uint64_t id
)
408 return bt_trace_borrow_stream_by_id((void *) trace
, id
);
411 uint64_t bt_trace_get_stream_class_count(const struct bt_trace
*trace
)
413 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
414 return (uint64_t) trace
->stream_classes
->len
;
417 struct bt_stream_class
*bt_trace_borrow_stream_class_by_index(
418 struct bt_trace
*trace
, uint64_t index
)
420 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
421 BT_ASSERT_PRE_VALID_INDEX(index
, trace
->stream_classes
->len
);
422 return g_ptr_array_index(trace
->stream_classes
, index
);
425 const struct bt_stream_class
*
426 bt_trace_borrow_stream_class_by_index_const(
427 const struct bt_trace
*trace
, uint64_t index
)
429 return bt_trace_borrow_stream_class_by_index(
430 (void *) trace
, index
);
433 struct bt_stream_class
*bt_trace_borrow_stream_class_by_id(
434 struct bt_trace
*trace
, uint64_t id
)
436 struct bt_stream_class
*stream_class
= NULL
;
439 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
441 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
442 struct bt_stream_class
*stream_class_candidate
=
443 g_ptr_array_index(trace
->stream_classes
, i
);
445 if (stream_class_candidate
->id
== id
) {
446 stream_class
= stream_class_candidate
;
455 const struct bt_stream_class
*
456 bt_trace_borrow_stream_class_by_id_const(
457 const struct bt_trace
*trace
, uint64_t id
)
459 return bt_trace_borrow_stream_class_by_id((void *) trace
, id
);
462 const struct bt_field_class
*bt_trace_borrow_packet_header_field_class_const(
463 const struct bt_trace
*trace
)
465 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
466 return trace
->packet_header_fc
;
469 int bt_trace_set_packet_header_field_class(
470 struct bt_trace
*trace
,
471 struct bt_field_class
*field_class
)
474 struct bt_resolve_field_path_context resolve_ctx
= {
475 .packet_header
= field_class
,
476 .packet_context
= NULL
,
477 .event_header
= NULL
,
478 .event_common_context
= NULL
,
479 .event_specific_context
= NULL
,
480 .event_payload
= NULL
,
483 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
484 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
485 BT_ASSERT_PRE_TRACE_HOT(trace
);
486 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
487 BT_FIELD_CLASS_TYPE_STRUCTURE
,
488 "Packet header field classe is not a structure field classe: %!+F",
490 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
495 bt_field_class_make_part_of_trace(field_class
);
496 bt_object_put_ref(trace
->packet_header_fc
);
497 trace
->packet_header_fc
= field_class
;
498 bt_object_get_no_null_check(trace
->packet_header_fc
);
499 bt_field_class_freeze(field_class
);
500 BT_LIB_LOGV("Set trace's packet header field classe: %!+t", trace
);
506 bt_bool
bt_trace_is_static(const struct bt_trace
*trace
)
508 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
509 return (bt_bool
) trace
->is_static
;
512 int bt_trace_make_static(struct bt_trace
*trace
)
515 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
516 trace
->is_static
= true;
517 bt_trace_freeze(trace
);
518 BT_LIB_LOGV("Trace is now static: %!+t", trace
);
520 /* Call all the "trace is static" listeners */
521 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
522 struct bt_trace_is_static_listener_elem elem
=
523 g_array_index(trace
->is_static_listeners
,
524 struct bt_trace_is_static_listener_elem
, i
);
527 elem
.func((void *) trace
, elem
.data
);
534 int bt_trace_add_is_static_listener(
535 const struct bt_trace
*c_trace
,
536 bt_trace_is_static_listener_func listener
,
537 bt_trace_listener_removed_func listener_removed
, void *data
,
538 uint64_t *listener_id
)
540 struct bt_trace
*trace
= (void *) c_trace
;
542 struct bt_trace_is_static_listener_elem new_elem
= {
544 .removed
= listener_removed
,
548 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
549 BT_ASSERT_PRE_NON_NULL(listener
, "Listener");
550 BT_ASSERT_PRE(!trace
->is_static
,
551 "Trace is already static: %!+t", trace
);
552 BT_ASSERT_PRE(trace
->in_remove_listener
,
553 "Cannot call this function while executing a "
554 "remove listener: %!+t", trace
);
556 /* Find the next available spot */
557 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
558 struct bt_trace_is_static_listener_elem elem
=
559 g_array_index(trace
->is_static_listeners
,
560 struct bt_trace_is_static_listener_elem
, i
);
567 if (i
== trace
->is_static_listeners
->len
) {
568 g_array_append_val(trace
->is_static_listeners
, new_elem
);
570 g_array_insert_val(trace
->is_static_listeners
, i
, new_elem
);
577 BT_LIB_LOGV("Added \"trace is static\" listener: "
578 "%![trace-]+t, listener-id=%" PRIu64
, trace
, i
);
584 bool has_listener_id(const struct bt_trace
*trace
, uint64_t listener_id
)
586 BT_ASSERT(listener_id
< trace
->is_static_listeners
->len
);
587 return (&g_array_index(trace
->is_static_listeners
,
588 struct bt_trace_is_static_listener_elem
,
589 listener_id
))->func
!= NULL
;
592 int bt_trace_remove_is_static_listener(const struct bt_trace
*c_trace
,
593 uint64_t listener_id
)
595 struct bt_trace
*trace
= (void *) c_trace
;
596 struct bt_trace_is_static_listener_elem
*elem
;
598 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
599 BT_ASSERT_PRE(!trace
->is_static
,
600 "Trace is already static: %!+t", trace
);
601 BT_ASSERT_PRE(trace
->in_remove_listener
,
602 "Cannot call this function while executing a "
603 "remove listener: %!+t", trace
);
604 BT_ASSERT_PRE(has_listener_id(trace
, listener_id
),
605 "Trace has no such \"trace is static\" listener ID: "
606 "%![trace-]+t, %" PRIu64
, trace
, listener_id
);
607 elem
= &g_array_index(trace
->is_static_listeners
,
608 struct bt_trace_is_static_listener_elem
,
610 BT_ASSERT(elem
->func
);
613 /* Call remove listener */
614 BT_LIB_LOGV("Calling remove listener: "
615 "%![trace-]+t, listener-id=%" PRIu64
,
617 trace
->in_remove_listener
= true;
618 elem
->removed((void *) trace
, elem
->data
);
619 trace
->in_remove_listener
= false;
623 elem
->removed
= NULL
;
625 BT_LIB_LOGV("Removed \"trace is static\" listener: "
626 "%![trace-]+t, listener-id=%" PRIu64
,
632 void _bt_trace_freeze(const struct bt_trace
*trace
)
634 /* The packet header field classe is already frozen */
636 BT_LIB_LOGD("Freezing trace: %!+t", trace
);
637 ((struct bt_trace
*) trace
)->frozen
= true;
640 bt_bool
bt_trace_assigns_automatic_stream_class_id(const struct bt_trace
*trace
)
642 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
643 return (bt_bool
) trace
->assigns_automatic_stream_class_id
;
646 void bt_trace_set_assigns_automatic_stream_class_id(struct bt_trace
*trace
,
649 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
650 BT_ASSERT_PRE_TRACE_HOT(trace
);
651 trace
->assigns_automatic_stream_class_id
= (bool) value
;
652 BT_LIB_LOGV("Set trace's automatic stream class ID "
653 "assignment property: %!+t", trace
);
657 void bt_trace_add_stream(struct bt_trace
*trace
, struct bt_stream
*stream
)
661 bt_object_set_parent(&stream
->base
, &trace
->base
);
662 g_ptr_array_add(trace
->streams
, stream
);
663 bt_trace_freeze(trace
);
665 if (bt_g_hash_table_contains(trace
->stream_classes_stream_count
,
667 count
= GPOINTER_TO_UINT(g_hash_table_lookup(
668 trace
->stream_classes_stream_count
, stream
->class));
671 g_hash_table_insert(trace
->stream_classes_stream_count
,
672 stream
->class, GUINT_TO_POINTER(count
+ 1));
676 uint64_t bt_trace_get_automatic_stream_id(const struct bt_trace
*trace
,
677 const struct bt_stream_class
*stream_class
)
683 BT_ASSERT(stream_class
);
685 if (g_hash_table_lookup_extended(trace
->stream_classes_stream_count
,
686 stream_class
, &orig_key
, &value
)) {
687 id
= (uint64_t) GPOINTER_TO_UINT(value
);