4 * Babeltrace trace IR - Trace
6 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define BT_LOG_TAG "TRACE"
30 #include <babeltrace/lib-logging-internal.h>
32 #include <babeltrace/assert-pre-internal.h>
33 #include <babeltrace/trace-ir/trace-internal.h>
34 #include <babeltrace/trace-ir/clock-class-internal.h>
35 #include <babeltrace/trace-ir/stream-internal.h>
36 #include <babeltrace/trace-ir/stream-class-internal.h>
37 #include <babeltrace/trace-ir/event-internal.h>
38 #include <babeltrace/trace-ir/event-class.h>
39 #include <babeltrace/trace-ir/event-class-internal.h>
40 #include <babeltrace/ctf-writer/functor-internal.h>
41 #include <babeltrace/ctf-writer/clock-internal.h>
42 #include <babeltrace/trace-ir/field-wrapper-internal.h>
43 #include <babeltrace/trace-ir/field-classes-internal.h>
44 #include <babeltrace/trace-ir/attributes-internal.h>
45 #include <babeltrace/trace-ir/utils-internal.h>
46 #include <babeltrace/trace-ir/resolve-field-path-internal.h>
47 #include <babeltrace/compiler-internal.h>
48 #include <babeltrace/values.h>
49 #include <babeltrace/values-internal.h>
50 #include <babeltrace/ref.h>
51 #include <babeltrace/types.h>
52 #include <babeltrace/endian-internal.h>
53 #include <babeltrace/assert-internal.h>
54 #include <babeltrace/compat/glib-internal.h>
60 struct bt_trace_is_static_listener_elem
{
61 bt_trace_is_static_listener func
;
62 bt_trace_listener_removed removed
;
66 #define BT_ASSERT_PRE_TRACE_HOT(_trace) \
67 BT_ASSERT_PRE_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
);
77 * Call remove listeners first so that everything else still
78 * exists in the trace.
80 if (trace
->is_static_listeners
) {
83 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
84 struct bt_trace_is_static_listener_elem elem
=
85 g_array_index(trace
->is_static_listeners
,
86 struct bt_trace_is_static_listener_elem
, i
);
89 elem
.removed(trace
, elem
.data
);
93 g_array_free(trace
->is_static_listeners
, TRUE
);
96 bt_object_pool_finalize(&trace
->packet_header_field_pool
);
98 if (trace
->environment
) {
99 BT_LOGD_STR("Destroying environment attributes.");
100 bt_attributes_destroy(trace
->environment
);
103 if (trace
->name
.str
) {
104 g_string_free(trace
->name
.str
, TRUE
);
107 if (trace
->streams
) {
108 BT_LOGD_STR("Destroying streams.");
109 g_ptr_array_free(trace
->streams
, TRUE
);
112 if (trace
->stream_classes
) {
113 BT_LOGD_STR("Destroying stream classes.");
114 g_ptr_array_free(trace
->stream_classes
, TRUE
);
117 if (trace
->stream_classes_stream_count
) {
118 g_hash_table_destroy(trace
->stream_classes_stream_count
);
121 BT_LOGD_STR("Putting packet header field classe.");
122 bt_put(trace
->packet_header_fc
);
127 void free_packet_header_field(struct bt_field_wrapper
*field_wrapper
,
128 struct bt_trace
*trace
)
130 bt_field_wrapper_destroy(field_wrapper
);
133 struct bt_trace
*bt_trace_create(void)
135 struct bt_trace
*trace
= NULL
;
138 BT_LOGD_STR("Creating default trace object.");
139 trace
= g_new0(struct bt_trace
, 1);
141 BT_LOGE_STR("Failed to allocate one trace.");
145 bt_object_init_shared_with_parent(&trace
->base
, destroy_trace
);
146 trace
->streams
= g_ptr_array_new_with_free_func(
147 (GDestroyNotify
) bt_object_try_spec_release
);
148 if (!trace
->streams
) {
149 BT_LOGE_STR("Failed to allocate one GPtrArray.");
153 trace
->stream_classes
= g_ptr_array_new_with_free_func(
154 (GDestroyNotify
) bt_object_try_spec_release
);
155 if (!trace
->stream_classes
) {
156 BT_LOGE_STR("Failed to allocate one GPtrArray.");
160 trace
->stream_classes_stream_count
= g_hash_table_new(g_direct_hash
,
162 if (!trace
->stream_classes_stream_count
) {
163 BT_LOGE_STR("Failed to allocate one GHashTable.");
167 trace
->name
.str
= g_string_new(NULL
);
168 if (!trace
->name
.str
) {
169 BT_LOGE_STR("Failed to allocate one GString.");
173 trace
->environment
= bt_attributes_create();
174 if (!trace
->environment
) {
175 BT_LOGE_STR("Cannot create empty attributes object.");
179 trace
->is_static_listeners
= g_array_new(FALSE
, TRUE
,
180 sizeof(struct bt_trace_is_static_listener_elem
));
181 if (!trace
->is_static_listeners
) {
182 BT_LOGE_STR("Failed to allocate one GArray.");
186 trace
->assigns_automatic_stream_class_id
= true;
187 ret
= bt_object_pool_initialize(&trace
->packet_header_field_pool
,
188 (bt_object_pool_new_object_func
) bt_field_wrapper_new
,
189 (bt_object_pool_destroy_object_func
) free_packet_header_field
,
192 BT_LOGE("Failed to initialize packet header field pool: ret=%d",
197 BT_LIB_LOGD("Created trace object: %!+t", trace
);
207 const char *bt_trace_get_name(struct bt_trace
*trace
)
209 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
210 return trace
->name
.value
;
213 int bt_trace_set_name(struct bt_trace
*trace
, const char *name
)
215 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
216 BT_ASSERT_PRE_NON_NULL(name
, "Name");
217 BT_ASSERT_PRE_TRACE_HOT(trace
);
218 g_string_assign(trace
->name
.str
, name
);
219 trace
->name
.value
= trace
->name
.str
->str
;
220 BT_LIB_LOGV("Set trace's name: %!+t", trace
);
224 bt_uuid
bt_trace_get_uuid(struct bt_trace
*trace
)
226 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
227 return trace
->uuid
.value
;
230 int bt_trace_set_uuid(struct bt_trace
*trace
, bt_uuid uuid
)
232 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
233 BT_ASSERT_PRE_NON_NULL(uuid
, "UUID");
234 BT_ASSERT_PRE_TRACE_HOT(trace
);
235 memcpy(trace
->uuid
.uuid
, uuid
, BABELTRACE_UUID_LEN
);
236 trace
->uuid
.value
= trace
->uuid
.uuid
;
237 BT_LIB_LOGV("Set trace's UUID: %!+t", trace
);
243 bool trace_has_environment_entry(struct bt_trace
*trace
, const char *name
)
245 struct bt_value
*attribute
;
249 attribute
= bt_attributes_borrow_field_value_by_name(
250 trace
->environment
, name
);
251 return attribute
!= NULL
;
255 int set_environment_entry(struct bt_trace
*trace
, const char *name
,
256 struct bt_value
*value
)
263 BT_ASSERT_PRE(!trace
->frozen
||
264 !trace_has_environment_entry(trace
, name
),
265 "Trace is frozen: cannot replace environment entry: "
266 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
267 ret
= bt_attributes_set_field_value(trace
->environment
, name
,
269 bt_value_freeze(value
);
271 BT_LIB_LOGE("Cannot set trace's environment entry: "
272 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
274 BT_LIB_LOGV("Set trace's environment entry: "
275 "%![trace-]+t, entry-name=\"%s\"", trace
, name
);
281 int bt_trace_set_environment_entry_string(struct bt_trace
*trace
,
282 const char *name
, const char *value
)
285 struct bt_value
*value_obj
;
287 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
288 BT_ASSERT_PRE_NON_NULL(name
, "Name");
289 BT_ASSERT_PRE_NON_NULL(value
, "Value");
290 value_obj
= bt_value_string_create_init(value
);
292 BT_LOGE_STR("Cannot create a string value object.");
297 /* set_environment_entry() logs errors */
298 ret
= set_environment_entry(trace
, name
, value_obj
);
305 int bt_trace_set_environment_entry_integer(
306 struct bt_trace
*trace
, const char *name
, int64_t value
)
309 struct bt_value
*value_obj
;
311 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
312 BT_ASSERT_PRE_NON_NULL(name
, "Name");
313 value_obj
= bt_value_integer_create_init(value
);
315 BT_LOGE_STR("Cannot create an integer value object.");
320 /* set_environment_entry() logs errors */
321 ret
= set_environment_entry(trace
, name
, value_obj
);
328 uint64_t bt_trace_get_environment_entry_count(struct bt_trace
*trace
)
332 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
333 ret
= bt_attributes_get_count(trace
->environment
);
335 return (uint64_t) ret
;
338 void bt_trace_borrow_environment_entry_by_index(
339 struct bt_trace
*trace
, uint64_t index
,
340 const char **name
, struct bt_value
**value
)
342 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
343 BT_ASSERT_PRE_NON_NULL(name
, "Name");
344 BT_ASSERT_PRE_NON_NULL(value
, "Value");
345 BT_ASSERT_PRE_VALID_INDEX(index
,
346 bt_attributes_get_count(trace
->environment
));
347 *value
= bt_attributes_borrow_field_value(trace
->environment
, index
);
349 *name
= bt_attributes_get_field_name(trace
->environment
, index
);
353 struct bt_value
*bt_trace_borrow_environment_entry_value_by_name(
354 struct bt_trace
*trace
, const char *name
)
356 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
357 BT_ASSERT_PRE_NON_NULL(name
, "Name");
358 return bt_attributes_borrow_field_value_by_name(trace
->environment
,
362 uint64_t bt_trace_get_stream_count(struct bt_trace
*trace
)
364 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
365 return (uint64_t) trace
->streams
->len
;
368 struct bt_stream
*bt_trace_borrow_stream_by_index(
369 struct bt_trace
*trace
, uint64_t index
)
371 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
372 BT_ASSERT_PRE_VALID_INDEX(index
, trace
->streams
->len
);
373 return g_ptr_array_index(trace
->streams
, index
);
376 struct bt_stream
*bt_trace_borrow_stream_by_id(
377 struct bt_trace
*trace
, uint64_t id
)
379 struct bt_stream
*stream
= NULL
;
382 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
384 for (i
= 0; i
< trace
->streams
->len
; i
++) {
385 struct bt_stream
*stream_candidate
=
386 g_ptr_array_index(trace
->streams
, i
);
388 if (stream_candidate
->id
== id
) {
389 stream
= stream_candidate
;
398 uint64_t bt_trace_get_stream_class_count(struct bt_trace
*trace
)
400 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
401 return (uint64_t) trace
->stream_classes
->len
;
404 struct bt_stream_class
*bt_trace_borrow_stream_class_by_index(
405 struct bt_trace
*trace
, uint64_t index
)
407 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
408 BT_ASSERT_PRE_VALID_INDEX(index
, trace
->stream_classes
->len
);
409 return g_ptr_array_index(trace
->stream_classes
, index
);
412 struct bt_stream_class
*bt_trace_borrow_stream_class_by_id(
413 struct bt_trace
*trace
, uint64_t id
)
415 struct bt_stream_class
*stream_class
= NULL
;
418 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
420 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
421 struct bt_stream_class
*stream_class_candidate
=
422 g_ptr_array_index(trace
->stream_classes
, i
);
424 if (stream_class_candidate
->id
== id
) {
425 stream_class
= stream_class_candidate
;
434 struct bt_field_class
*bt_trace_borrow_packet_header_field_class(
435 struct bt_trace
*trace
)
437 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
438 return trace
->packet_header_fc
;
441 int bt_trace_set_packet_header_field_class(struct bt_trace
*trace
,
442 struct bt_field_class
*field_class
)
445 struct bt_resolve_field_path_context resolve_ctx
= {
446 .packet_header
= field_class
,
447 .packet_context
= NULL
,
448 .event_header
= NULL
,
449 .event_common_context
= NULL
,
450 .event_specific_context
= NULL
,
451 .event_payload
= NULL
,
454 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
455 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
456 BT_ASSERT_PRE_TRACE_HOT(trace
);
457 BT_ASSERT_PRE(bt_field_class_get_id(field_class
) ==
458 BT_FIELD_CLASS_ID_STRUCTURE
,
459 "Packet header field classe is not a structure field classe: %!+F",
461 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
466 bt_field_class_make_part_of_trace(field_class
);
467 bt_put(trace
->packet_header_fc
);
468 trace
->packet_header_fc
= bt_get(field_class
);
469 bt_field_class_freeze(field_class
);
470 BT_LIB_LOGV("Set trace's packet header field classe: %!+t", trace
);
476 bt_bool
bt_trace_is_static(struct bt_trace
*trace
)
478 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
479 return (bt_bool
) trace
->is_static
;
482 int bt_trace_make_static(struct bt_trace
*trace
)
486 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
487 trace
->is_static
= true;
488 bt_trace_freeze(trace
);
489 BT_LIB_LOGV("Trace is now static: %!+t", trace
);
491 /* Call all the "trace is static" listeners */
492 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
493 struct bt_trace_is_static_listener_elem elem
=
494 g_array_index(trace
->is_static_listeners
,
495 struct bt_trace_is_static_listener_elem
, i
);
498 elem
.func(trace
, elem
.data
);
505 int bt_trace_add_is_static_listener(struct bt_trace
*trace
,
506 bt_trace_is_static_listener listener
,
507 bt_trace_listener_removed listener_removed
, void *data
,
508 uint64_t *listener_id
)
511 struct bt_trace_is_static_listener_elem new_elem
= {
513 .removed
= listener_removed
,
517 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
518 BT_ASSERT_PRE_NON_NULL(listener
, "Listener");
519 BT_ASSERT_PRE(!trace
->is_static
,
520 "Trace is already static: %!+t", trace
);
521 BT_ASSERT_PRE(trace
->in_remove_listener
,
522 "Cannot call this function while executing a "
523 "remove listener: %!+t", trace
);
525 /* Find the next available spot */
526 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
527 struct bt_trace_is_static_listener_elem elem
=
528 g_array_index(trace
->is_static_listeners
,
529 struct bt_trace_is_static_listener_elem
, i
);
536 if (i
== trace
->is_static_listeners
->len
) {
537 g_array_append_val(trace
->is_static_listeners
, new_elem
);
539 g_array_insert_val(trace
->is_static_listeners
, i
, new_elem
);
546 BT_LIB_LOGV("Added \"trace is static\" listener: "
547 "%![trace-]+t, listener-id=%" PRIu64
, trace
, i
);
553 bool has_listener_id(struct bt_trace
*trace
, uint64_t listener_id
)
555 BT_ASSERT(listener_id
< trace
->is_static_listeners
->len
);
556 return (&g_array_index(trace
->is_static_listeners
,
557 struct bt_trace_is_static_listener_elem
,
558 listener_id
))->func
!= NULL
;
561 int bt_trace_remove_is_static_listener(
562 struct bt_trace
*trace
, uint64_t listener_id
)
564 struct bt_trace_is_static_listener_elem
*elem
;
566 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
567 BT_ASSERT_PRE(!trace
->is_static
,
568 "Trace is already static: %!+t", trace
);
569 BT_ASSERT_PRE(trace
->in_remove_listener
,
570 "Cannot call this function while executing a "
571 "remove listener: %!+t", trace
);
572 BT_ASSERT_PRE(has_listener_id(trace
, listener_id
),
573 "Trace has no such \"trace is static\" listener ID: "
574 "%![trace-]+t, %" PRIu64
, trace
, listener_id
);
575 elem
= &g_array_index(trace
->is_static_listeners
,
576 struct bt_trace_is_static_listener_elem
,
578 BT_ASSERT(elem
->func
);
581 /* Call remove listener */
582 BT_LIB_LOGV("Calling remove listener: "
583 "%![trace-]+t, listener-id=%" PRIu64
,
585 trace
->in_remove_listener
= true;
586 elem
->removed(trace
, elem
->data
);
587 trace
->in_remove_listener
= false;
591 elem
->removed
= NULL
;
593 BT_LIB_LOGV("Removed \"trace is static\" listener: "
594 "%![trace-]+t, listener-id=%" PRIu64
,
600 void _bt_trace_freeze(struct bt_trace
*trace
)
602 /* The packet header field classe is already frozen */
604 BT_LIB_LOGD("Freezing trace: %!+t", trace
);
605 trace
->frozen
= true;
608 bt_bool
bt_trace_assigns_automatic_stream_class_id(struct bt_trace
*trace
)
610 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
611 return (bt_bool
) trace
->assigns_automatic_stream_class_id
;
614 int bt_trace_set_assigns_automatic_stream_class_id(
615 struct bt_trace
*trace
, bt_bool value
)
617 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
618 BT_ASSERT_PRE_TRACE_HOT(trace
);
619 trace
->assigns_automatic_stream_class_id
= (bool) value
;
620 BT_LIB_LOGV("Set trace's automatic stream class ID "
621 "assignment property: %!+t", trace
);
626 void bt_trace_add_stream(struct bt_trace
*trace
, struct bt_stream
*stream
)
630 bt_object_set_parent(&stream
->base
, &trace
->base
);
631 g_ptr_array_add(trace
->streams
, stream
);
632 bt_trace_freeze(trace
);
634 if (bt_g_hash_table_contains(trace
->stream_classes_stream_count
,
636 count
= GPOINTER_TO_UINT(g_hash_table_lookup(
637 trace
->stream_classes_stream_count
, stream
->class));
640 g_hash_table_insert(trace
->stream_classes_stream_count
,
641 stream
->class, GUINT_TO_POINTER(count
+ 1));
645 uint64_t bt_trace_get_automatic_stream_id(struct bt_trace
*trace
,
646 struct bt_stream_class
*stream_class
)
652 BT_ASSERT(stream_class
);
654 if (g_hash_table_lookup_extended(trace
->stream_classes_stream_count
,
655 stream_class
, &orig_key
, &value
)) {
656 id
= (uint64_t) GPOINTER_TO_UINT(value
);