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/private-trace.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/private-values.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_private_trace_is_static_listener func
;
60 bt_private_trace_listener_removed 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
);
94 bt_object_pool_finalize(&trace
->packet_header_field_pool
);
96 if (trace
->environment
) {
97 BT_LOGD_STR("Destroying environment attributes.");
98 bt_attributes_destroy(trace
->environment
);
101 if (trace
->name
.str
) {
102 g_string_free(trace
->name
.str
, TRUE
);
105 if (trace
->streams
) {
106 BT_LOGD_STR("Destroying streams.");
107 g_ptr_array_free(trace
->streams
, TRUE
);
110 if (trace
->stream_classes
) {
111 BT_LOGD_STR("Destroying stream classes.");
112 g_ptr_array_free(trace
->stream_classes
, TRUE
);
115 if (trace
->stream_classes_stream_count
) {
116 g_hash_table_destroy(trace
->stream_classes_stream_count
);
119 BT_LOGD_STR("Putting packet header field classe.");
120 bt_object_put_ref(trace
->packet_header_fc
);
125 void free_packet_header_field(struct bt_field_wrapper
*field_wrapper
,
126 struct bt_trace
*trace
)
128 bt_field_wrapper_destroy(field_wrapper
);
131 struct bt_private_trace
*bt_private_trace_create(void)
133 struct bt_trace
*trace
= NULL
;
136 BT_LOGD_STR("Creating default trace object.");
137 trace
= g_new0(struct bt_trace
, 1);
139 BT_LOGE_STR("Failed to allocate one trace.");
143 bt_object_init_shared_with_parent(&trace
->base
, destroy_trace
);
144 trace
->streams
= g_ptr_array_new_with_free_func(
145 (GDestroyNotify
) bt_object_try_spec_release
);
146 if (!trace
->streams
) {
147 BT_LOGE_STR("Failed to allocate one GPtrArray.");
151 trace
->stream_classes
= g_ptr_array_new_with_free_func(
152 (GDestroyNotify
) bt_object_try_spec_release
);
153 if (!trace
->stream_classes
) {
154 BT_LOGE_STR("Failed to allocate one GPtrArray.");
158 trace
->stream_classes_stream_count
= g_hash_table_new(g_direct_hash
,
160 if (!trace
->stream_classes_stream_count
) {
161 BT_LOGE_STR("Failed to allocate one GHashTable.");
165 trace
->name
.str
= g_string_new(NULL
);
166 if (!trace
->name
.str
) {
167 BT_LOGE_STR("Failed to allocate one GString.");
171 trace
->environment
= bt_attributes_create();
172 if (!trace
->environment
) {
173 BT_LOGE_STR("Cannot create empty attributes object.");
177 trace
->is_static_listeners
= g_array_new(FALSE
, TRUE
,
178 sizeof(struct bt_trace_is_static_listener_elem
));
179 if (!trace
->is_static_listeners
) {
180 BT_LOGE_STR("Failed to allocate one GArray.");
184 trace
->assigns_automatic_stream_class_id
= true;
185 ret
= bt_object_pool_initialize(&trace
->packet_header_field_pool
,
186 (bt_object_pool_new_object_func
) bt_field_wrapper_new
,
187 (bt_object_pool_destroy_object_func
) free_packet_header_field
,
190 BT_LOGE("Failed to initialize packet header field pool: ret=%d",
195 BT_LIB_LOGD("Created trace object: %!+t", trace
);
199 BT_OBJECT_PUT_REF_AND_RESET(trace
);
202 return (void *) trace
;
205 const char *bt_trace_get_name(struct bt_trace
*trace
)
207 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
208 return trace
->name
.value
;
211 int bt_private_trace_set_name(struct bt_private_trace
*priv_trace
,
214 struct bt_trace
*trace
= (void *) priv_trace
;
216 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
217 BT_ASSERT_PRE_NON_NULL(name
, "Name");
218 BT_ASSERT_PRE_TRACE_HOT(trace
);
219 g_string_assign(trace
->name
.str
, name
);
220 trace
->name
.value
= trace
->name
.str
->str
;
221 BT_LIB_LOGV("Set trace's name: %!+t", trace
);
225 bt_uuid
bt_trace_get_uuid(struct bt_trace
*trace
)
227 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
228 return trace
->uuid
.value
;
231 int bt_private_trace_set_uuid(struct bt_private_trace
*priv_trace
, bt_uuid uuid
)
233 struct bt_trace
*trace
= (void *) priv_trace
;
235 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
236 BT_ASSERT_PRE_NON_NULL(uuid
, "UUID");
237 BT_ASSERT_PRE_TRACE_HOT(trace
);
238 memcpy(trace
->uuid
.uuid
, uuid
, BABELTRACE_UUID_LEN
);
239 trace
->uuid
.value
= trace
->uuid
.uuid
;
240 BT_LIB_LOGV("Set trace's UUID: %!+t", trace
);
246 bool trace_has_environment_entry(struct bt_trace
*trace
, const char *name
)
250 return bt_attributes_borrow_field_value_by_name(
251 trace
->environment
, name
) != NULL
;
255 int set_environment_entry(struct bt_trace
*trace
, const char *name
,
256 struct bt_private_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(bt_value_borrow_from_private(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_private_trace_set_private_environment_entry_string(
282 struct bt_private_trace
*priv_trace
,
283 const char *name
, const char *value
)
286 struct bt_private_value
*value_obj
;
287 struct bt_trace
*trace
= (void *) priv_trace
;
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_private_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_private_trace_set_private_environment_entry_integer(
308 struct bt_private_trace
*priv_trace
,
309 const char *name
, int64_t value
)
312 struct bt_private_value
*value_obj
;
313 struct bt_trace
*trace
= (void *) priv_trace
;
315 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
316 BT_ASSERT_PRE_NON_NULL(name
, "Name");
317 value_obj
= bt_private_value_integer_create_init(value
);
319 BT_LOGE_STR("Cannot create an integer value object.");
324 /* set_environment_entry() logs errors */
325 ret
= set_environment_entry(trace
, name
, value_obj
);
328 bt_object_put_ref(value_obj
);
332 uint64_t bt_trace_get_environment_entry_count(struct bt_trace
*trace
)
336 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
337 ret
= bt_attributes_get_count(trace
->environment
);
339 return (uint64_t) ret
;
342 void bt_trace_borrow_environment_entry_by_index(
343 struct bt_trace
*trace
, uint64_t index
,
344 const char **name
, struct bt_value
**value
)
346 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
347 BT_ASSERT_PRE_NON_NULL(name
, "Name");
348 BT_ASSERT_PRE_NON_NULL(value
, "Value");
349 BT_ASSERT_PRE_VALID_INDEX(index
,
350 bt_attributes_get_count(trace
->environment
));
351 *value
= bt_value_borrow_from_private(
352 bt_attributes_borrow_field_value(trace
->environment
, index
));
354 *name
= bt_attributes_get_field_name(trace
->environment
, index
);
358 void bt_private_trace_borrow_private_environment_entry_by_index(
359 struct bt_private_trace
*trace
, uint64_t index
,
360 const char **name
, struct bt_private_value
**value
)
362 bt_trace_borrow_environment_entry_by_index((void *) trace
,
363 index
, name
, (void *) value
);
366 struct bt_value
*bt_trace_borrow_environment_entry_value_by_name(
367 struct bt_trace
*trace
, const char *name
)
369 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
370 BT_ASSERT_PRE_NON_NULL(name
, "Name");
371 return bt_value_borrow_from_private(
372 bt_attributes_borrow_field_value_by_name(trace
->environment
,
376 struct bt_private_value
*
377 bt_private_trace_borrow_private_environment_entry_value_by_name(
378 struct bt_private_trace
*trace
, const char *name
)
380 return (void *) bt_trace_borrow_environment_entry_value_by_name(
381 (void *) trace
, name
);
384 uint64_t bt_trace_get_stream_count(struct bt_trace
*trace
)
386 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
387 return (uint64_t) trace
->streams
->len
;
390 struct bt_stream
*bt_trace_borrow_stream_by_index(
391 struct bt_trace
*trace
, uint64_t index
)
393 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
394 BT_ASSERT_PRE_VALID_INDEX(index
, trace
->streams
->len
);
395 return g_ptr_array_index(trace
->streams
, index
);
398 struct bt_private_stream
*bt_private_trace_borrow_private_stream_by_index(
399 struct bt_private_trace
*trace
, uint64_t index
)
401 return (void *) bt_trace_borrow_stream_by_index((void *) trace
, index
);
404 struct bt_stream
*bt_trace_borrow_stream_by_id(
405 struct bt_trace
*trace
, uint64_t id
)
407 struct bt_stream
*stream
= NULL
;
410 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
412 for (i
= 0; i
< trace
->streams
->len
; i
++) {
413 struct bt_stream
*stream_candidate
=
414 g_ptr_array_index(trace
->streams
, i
);
416 if (stream_candidate
->id
== id
) {
417 stream
= stream_candidate
;
426 struct bt_private_stream
*bt_private_trace_borrow_private_stream_by_id(
427 struct bt_private_trace
*trace
, uint64_t id
)
429 return (void *) bt_trace_borrow_stream_by_id((void *) trace
, id
);
432 uint64_t bt_trace_get_stream_class_count(struct bt_trace
*trace
)
434 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
435 return (uint64_t) trace
->stream_classes
->len
;
438 struct bt_stream_class
*bt_trace_borrow_stream_class_by_index(
439 struct bt_trace
*trace
, uint64_t index
)
441 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
442 BT_ASSERT_PRE_VALID_INDEX(index
, trace
->stream_classes
->len
);
443 return g_ptr_array_index(trace
->stream_classes
, index
);
446 struct bt_private_stream_class
*
447 bt_private_trace_borrow_private_stream_class_by_index(
448 struct bt_private_trace
*trace
, uint64_t index
)
450 return (void *) bt_trace_borrow_stream_class_by_index(
451 (void *) trace
, index
);
454 struct bt_stream_class
*bt_trace_borrow_stream_class_by_id(
455 struct bt_trace
*trace
, uint64_t id
)
457 struct bt_stream_class
*stream_class
= NULL
;
460 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
462 for (i
= 0; i
< trace
->stream_classes
->len
; i
++) {
463 struct bt_stream_class
*stream_class_candidate
=
464 g_ptr_array_index(trace
->stream_classes
, i
);
466 if (stream_class_candidate
->id
== id
) {
467 stream_class
= stream_class_candidate
;
476 struct bt_private_stream_class
*
477 bt_private_trace_borrow_private_stream_class_by_id(
478 struct bt_private_trace
*trace
, uint64_t id
)
480 return (void *) bt_trace_borrow_stream_class_by_id((void *) trace
, id
);
483 struct bt_field_class
*bt_trace_borrow_packet_header_field_class(
484 struct bt_trace
*trace
)
486 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
487 return trace
->packet_header_fc
;
490 int bt_private_trace_set_packet_header_private_field_class(
491 struct bt_private_trace
*priv_trace
,
492 struct bt_private_field_class
*priv_field_class
)
495 struct bt_trace
*trace
= (void *) priv_trace
;
496 struct bt_field_class
*field_class
= (void *) priv_field_class
;
497 struct bt_resolve_field_path_context resolve_ctx
= {
498 .packet_header
= field_class
,
499 .packet_context
= NULL
,
500 .event_header
= NULL
,
501 .event_common_context
= NULL
,
502 .event_specific_context
= NULL
,
503 .event_payload
= NULL
,
506 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
507 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
508 BT_ASSERT_PRE_TRACE_HOT(trace
);
509 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
510 BT_FIELD_CLASS_TYPE_STRUCTURE
,
511 "Packet header field classe is not a structure field classe: %!+F",
513 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
518 bt_field_class_make_part_of_trace(field_class
);
519 bt_object_put_ref(trace
->packet_header_fc
);
520 trace
->packet_header_fc
= bt_object_get_ref(field_class
);
521 bt_field_class_freeze(field_class
);
522 BT_LIB_LOGV("Set trace's packet header field classe: %!+t", trace
);
528 bt_bool
bt_trace_is_static(struct bt_trace
*trace
)
530 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
531 return (bt_bool
) trace
->is_static
;
534 int bt_private_trace_make_static(struct bt_private_trace
*priv_trace
)
536 struct bt_trace
*trace
= (void *) priv_trace
;
539 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
540 trace
->is_static
= true;
541 bt_trace_freeze(trace
);
542 BT_LIB_LOGV("Trace is now static: %!+t", trace
);
544 /* Call all the "trace is static" listeners */
545 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
546 struct bt_trace_is_static_listener_elem elem
=
547 g_array_index(trace
->is_static_listeners
,
548 struct bt_trace_is_static_listener_elem
, i
);
551 elem
.func((void *) trace
, elem
.data
);
558 int bt_private_trace_add_is_static_listener(
559 struct bt_private_trace
*priv_trace
,
560 bt_private_trace_is_static_listener listener
,
561 bt_private_trace_listener_removed listener_removed
, void *data
,
562 uint64_t *listener_id
)
564 struct bt_trace
*trace
= (void *) priv_trace
;
566 struct bt_trace_is_static_listener_elem new_elem
= {
568 .removed
= listener_removed
,
572 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
573 BT_ASSERT_PRE_NON_NULL(listener
, "Listener");
574 BT_ASSERT_PRE(!trace
->is_static
,
575 "Trace is already static: %!+t", trace
);
576 BT_ASSERT_PRE(trace
->in_remove_listener
,
577 "Cannot call this function while executing a "
578 "remove listener: %!+t", trace
);
580 /* Find the next available spot */
581 for (i
= 0; i
< trace
->is_static_listeners
->len
; i
++) {
582 struct bt_trace_is_static_listener_elem elem
=
583 g_array_index(trace
->is_static_listeners
,
584 struct bt_trace_is_static_listener_elem
, i
);
591 if (i
== trace
->is_static_listeners
->len
) {
592 g_array_append_val(trace
->is_static_listeners
, new_elem
);
594 g_array_insert_val(trace
->is_static_listeners
, i
, new_elem
);
601 BT_LIB_LOGV("Added \"trace is static\" listener: "
602 "%![trace-]+t, listener-id=%" PRIu64
, trace
, i
);
608 bool has_listener_id(struct bt_trace
*trace
, uint64_t listener_id
)
610 BT_ASSERT(listener_id
< trace
->is_static_listeners
->len
);
611 return (&g_array_index(trace
->is_static_listeners
,
612 struct bt_trace_is_static_listener_elem
,
613 listener_id
))->func
!= NULL
;
616 int bt_private_trace_remove_is_static_listener(
617 struct bt_private_trace
*priv_trace
, uint64_t listener_id
)
619 struct bt_trace
*trace
= (void *) priv_trace
;
620 struct bt_trace_is_static_listener_elem
*elem
;
622 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
623 BT_ASSERT_PRE(!trace
->is_static
,
624 "Trace is already static: %!+t", trace
);
625 BT_ASSERT_PRE(trace
->in_remove_listener
,
626 "Cannot call this function while executing a "
627 "remove listener: %!+t", trace
);
628 BT_ASSERT_PRE(has_listener_id(trace
, listener_id
),
629 "Trace has no such \"trace is static\" listener ID: "
630 "%![trace-]+t, %" PRIu64
, trace
, listener_id
);
631 elem
= &g_array_index(trace
->is_static_listeners
,
632 struct bt_trace_is_static_listener_elem
,
634 BT_ASSERT(elem
->func
);
637 /* Call remove listener */
638 BT_LIB_LOGV("Calling remove listener: "
639 "%![trace-]+t, listener-id=%" PRIu64
,
641 trace
->in_remove_listener
= true;
642 elem
->removed((void *) trace
, elem
->data
);
643 trace
->in_remove_listener
= false;
647 elem
->removed
= NULL
;
649 BT_LIB_LOGV("Removed \"trace is static\" listener: "
650 "%![trace-]+t, listener-id=%" PRIu64
,
656 void _bt_trace_freeze(struct bt_trace
*trace
)
658 /* The packet header field classe is already frozen */
660 BT_LIB_LOGD("Freezing trace: %!+t", trace
);
661 trace
->frozen
= true;
664 bt_bool
bt_trace_assigns_automatic_stream_class_id(struct bt_trace
*trace
)
666 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
667 return (bt_bool
) trace
->assigns_automatic_stream_class_id
;
670 int bt_private_trace_set_assigns_automatic_stream_class_id(
671 struct bt_private_trace
*priv_trace
, bt_bool value
)
673 struct bt_trace
*trace
= (void *) priv_trace
;
675 BT_ASSERT_PRE_NON_NULL(trace
, "Trace");
676 BT_ASSERT_PRE_TRACE_HOT(trace
);
677 trace
->assigns_automatic_stream_class_id
= (bool) value
;
678 BT_LIB_LOGV("Set trace's automatic stream class ID "
679 "assignment property: %!+t", trace
);
684 void bt_trace_add_stream(struct bt_trace
*trace
, struct bt_stream
*stream
)
688 bt_object_set_parent(&stream
->base
, &trace
->base
);
689 g_ptr_array_add(trace
->streams
, stream
);
690 bt_trace_freeze(trace
);
692 if (bt_g_hash_table_contains(trace
->stream_classes_stream_count
,
694 count
= GPOINTER_TO_UINT(g_hash_table_lookup(
695 trace
->stream_classes_stream_count
, stream
->class));
698 g_hash_table_insert(trace
->stream_classes_stream_count
,
699 stream
->class, GUINT_TO_POINTER(count
+ 1));
703 uint64_t bt_trace_get_automatic_stream_id(struct bt_trace
*trace
,
704 struct bt_stream_class
*stream_class
)
710 BT_ASSERT(stream_class
);
712 if (g_hash_table_lookup_extended(trace
->stream_classes_stream_count
,
713 stream_class
, &orig_key
, &value
)) {
714 id
= (uint64_t) GPOINTER_TO_UINT(value
);