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 "TRACE"
25 #include <babeltrace/lib-logging-internal.h>
27 #include <babeltrace/assert-pre-internal.h>
28 #include <babeltrace/trace-ir/trace-class.h>
29 #include <babeltrace/trace-ir/trace-class-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-class-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/value.h>
46 #include <babeltrace/value-const.h>
47 #include <babeltrace/value-internal.h>
48 #include <babeltrace/types.h>
49 #include <babeltrace/endian-internal.h>
50 #include <babeltrace/assert-internal.h>
51 #include <babeltrace/compat/glib-internal.h>
57 struct bt_trace_class_destruction_listener_elem
{
58 bt_trace_class_destruction_listener_func func
;
62 #define BT_ASSERT_PRE_TRACE_CLASS_HOT(_tc) \
63 BT_ASSERT_PRE_HOT((_tc), "Trace class", ": %!+T", (_tc))
66 void destroy_trace_class(struct bt_object
*obj
)
68 struct bt_trace_class
*tc
= (void *) obj
;
70 BT_LIB_LOGD("Destroying trace class object: %!+T", tc
);
72 * Call destruction listener functions so that everything else
73 * still exists in the trace class.
75 if (tc
->destruction_listeners
) {
77 BT_LIB_LOGV("Calling trace class destruction listener(s): %!+T", tc
);
78 /* Call all the trace class destruction listeners */
79 for (i
= 0; i
< tc
->destruction_listeners
->len
; i
++) {
80 struct bt_trace_class_destruction_listener_elem elem
=
81 g_array_index(tc
->destruction_listeners
,
82 struct bt_trace_class_destruction_listener_elem
, i
);
85 elem
.func(tc
, elem
.data
);
88 g_array_free(tc
->destruction_listeners
, TRUE
);
89 tc
->destruction_listeners
= NULL
;
92 if (tc
->environment
) {
93 BT_LOGD_STR("Destroying environment attributes.");
94 bt_attributes_destroy(tc
->environment
);
95 tc
->environment
= NULL
;
99 g_string_free(tc
->name
.str
, TRUE
);
101 tc
->name
.value
= NULL
;
104 if (tc
->stream_classes
) {
105 BT_LOGD_STR("Destroying stream classes.");
106 g_ptr_array_free(tc
->stream_classes
, TRUE
);
107 tc
->stream_classes
= NULL
;
113 struct bt_trace_class
*bt_trace_class_create(bt_self_component
*self_comp
)
115 struct bt_trace_class
*tc
= NULL
;
117 BT_ASSERT_PRE_NON_NULL(self_comp
, "Self component");
118 BT_LOGD_STR("Creating default trace class object.");
119 tc
= g_new0(struct bt_trace_class
, 1);
121 BT_LOGE_STR("Failed to allocate one trace class.");
125 bt_object_init_shared_with_parent(&tc
->base
, destroy_trace_class
);
127 tc
->stream_classes
= g_ptr_array_new_with_free_func(
128 (GDestroyNotify
) bt_object_try_spec_release
);
129 if (!tc
->stream_classes
) {
130 BT_LOGE_STR("Failed to allocate one GPtrArray.");
134 tc
->name
.str
= g_string_new(NULL
);
136 BT_LOGE_STR("Failed to allocate one GString.");
140 tc
->environment
= bt_attributes_create();
141 if (!tc
->environment
) {
142 BT_LOGE_STR("Cannot create empty attributes object.");
146 tc
->destruction_listeners
= g_array_new(FALSE
, TRUE
,
147 sizeof(struct bt_trace_class_destruction_listener_elem
));
148 if (!tc
->destruction_listeners
) {
149 BT_LOGE_STR("Failed to allocate one GArray.");
153 tc
->assigns_automatic_stream_class_id
= true;
154 BT_LIB_LOGD("Created trace class object: %!+T", tc
);
158 BT_OBJECT_PUT_REF_AND_RESET(tc
);
164 const char *bt_trace_class_get_name(const struct bt_trace_class
*tc
)
166 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
167 return tc
->name
.value
;
170 enum bt_trace_class_status
bt_trace_class_set_name(
171 struct bt_trace_class
*tc
, const char *name
)
173 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
174 BT_ASSERT_PRE_NON_NULL(name
, "Name");
175 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc
);
176 g_string_assign(tc
->name
.str
, name
);
177 tc
->name
.value
= tc
->name
.str
->str
;
178 BT_LIB_LOGV("Set trace class's name: %!+T", tc
);
179 return BT_TRACE_CLASS_STATUS_OK
;
182 bt_uuid
bt_trace_class_get_uuid(const struct bt_trace_class
*tc
)
184 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
185 return tc
->uuid
.value
;
188 void bt_trace_class_set_uuid(struct bt_trace_class
*tc
, bt_uuid uuid
)
190 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
191 BT_ASSERT_PRE_NON_NULL(uuid
, "UUID");
192 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc
);
193 memcpy(tc
->uuid
.uuid
, uuid
, BABELTRACE_UUID_LEN
);
194 tc
->uuid
.value
= tc
->uuid
.uuid
;
195 BT_LIB_LOGV("Set trace class's UUID: %!+T", tc
);
198 enum bt_trace_class_status
bt_trace_class_add_destruction_listener(
199 const struct bt_trace_class
*_tc
,
200 bt_trace_class_destruction_listener_func listener
,
201 void *data
, uint64_t *listener_id
)
203 struct bt_trace_class
*tc
= (void *) _tc
;
205 struct bt_trace_class_destruction_listener_elem new_elem
= {
210 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
211 BT_ASSERT_PRE_NON_NULL(listener
, "Listener");
213 /* Find the next available spot */
214 for (i
= 0; i
< tc
->destruction_listeners
->len
; i
++) {
215 struct bt_trace_class_destruction_listener_elem elem
=
216 g_array_index(tc
->destruction_listeners
,
217 struct bt_trace_class_destruction_listener_elem
, i
);
224 if (i
== tc
->destruction_listeners
->len
) {
225 g_array_append_val(tc
->destruction_listeners
, new_elem
);
227 g_array_insert_val(tc
->destruction_listeners
, i
, new_elem
);
234 BT_LIB_LOGV("Added trace class destruction listener: %![tc-]+T, "
235 "listener-id=%" PRIu64
, tc
, i
);
236 return BT_TRACE_CLASS_STATUS_OK
;
241 bool has_listener_id(const struct bt_trace_class
*tc
, uint64_t listener_id
)
243 BT_ASSERT(listener_id
< tc
->destruction_listeners
->len
);
244 return (&g_array_index(tc
->destruction_listeners
,
245 struct bt_trace_class_destruction_listener_elem
,
246 listener_id
))->func
!= NULL
;
249 enum bt_trace_class_status
bt_trace_class_remove_destruction_listener(
250 const struct bt_trace_class
*_tc
, uint64_t listener_id
)
252 struct bt_trace_class
*tc
= (void *) _tc
;
253 struct bt_trace_class_destruction_listener_elem
*elem
;
255 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
256 BT_ASSERT_PRE(has_listener_id(tc
, listener_id
),
257 "Trace class has no such trace class destruction listener ID: "
258 "%![tc-]+T, %" PRIu64
, tc
, listener_id
);
259 elem
= &g_array_index(tc
->destruction_listeners
,
260 struct bt_trace_class_destruction_listener_elem
,
262 BT_ASSERT(elem
->func
);
266 BT_LIB_LOGV("Removed trace class destruction listener: "
267 "%![tc-]+T, listener-id=%" PRIu64
,
269 return BT_TRACE_CLASS_STATUS_OK
;
274 bool trace_has_environment_entry(const struct bt_trace_class
*tc
, const char *name
)
278 return bt_attributes_borrow_field_value_by_name(
279 tc
->environment
, name
) != NULL
;
283 enum bt_trace_class_status
set_environment_entry(struct bt_trace_class
*tc
,
284 const char *name
, struct bt_value
*value
)
291 BT_ASSERT_PRE(!tc
->frozen
||
292 !trace_has_environment_entry(tc
, name
),
293 "Trace class is frozen: cannot replace environment entry: "
294 "%![tc-]+T, entry-name=\"%s\"", tc
, name
);
295 ret
= bt_attributes_set_field_value(tc
->environment
, name
,
298 ret
= BT_TRACE_CLASS_STATUS_NOMEM
;
299 BT_LIB_LOGE("Cannot set trace class's environment entry: "
300 "%![tc-]+T, entry-name=\"%s\"", tc
, name
);
302 bt_value_freeze(value
);
303 BT_LIB_LOGV("Set trace class's environment entry: "
304 "%![tc-]+T, entry-name=\"%s\"", tc
, name
);
310 enum bt_trace_class_status
bt_trace_class_set_environment_entry_string(
311 struct bt_trace_class
*tc
, const char *name
, const char *value
)
314 struct bt_value
*value_obj
;
315 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
316 BT_ASSERT_PRE_NON_NULL(name
, "Name");
317 BT_ASSERT_PRE_NON_NULL(value
, "Value");
318 value_obj
= bt_value_string_create_init(value
);
320 BT_LOGE_STR("Cannot create a string value object.");
325 /* set_environment_entry() logs errors */
326 ret
= set_environment_entry(tc
, name
, value_obj
);
329 bt_object_put_ref(value_obj
);
333 enum bt_trace_class_status
bt_trace_class_set_environment_entry_integer(
334 struct bt_trace_class
*tc
, const char *name
, int64_t value
)
337 struct bt_value
*value_obj
;
338 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
339 BT_ASSERT_PRE_NON_NULL(name
, "Name");
340 value_obj
= bt_value_signed_integer_create_init(value
);
342 BT_LOGE_STR("Cannot create an integer value object.");
343 ret
= BT_TRACE_CLASS_STATUS_NOMEM
;
347 /* set_environment_entry() logs errors */
348 ret
= set_environment_entry(tc
, name
, value_obj
);
351 bt_object_put_ref(value_obj
);
355 uint64_t bt_trace_class_get_environment_entry_count(const struct bt_trace_class
*tc
)
359 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
360 ret
= bt_attributes_get_count(tc
->environment
);
362 return (uint64_t) ret
;
365 void bt_trace_class_borrow_environment_entry_by_index_const(
366 const struct bt_trace_class
*tc
, uint64_t index
,
367 const char **name
, const struct bt_value
**value
)
369 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
370 BT_ASSERT_PRE_NON_NULL(name
, "Name");
371 BT_ASSERT_PRE_NON_NULL(value
, "Value");
372 BT_ASSERT_PRE_VALID_INDEX(index
,
373 bt_attributes_get_count(tc
->environment
));
374 *value
= bt_attributes_borrow_field_value(tc
->environment
, index
);
376 *name
= bt_attributes_get_field_name(tc
->environment
, index
);
380 const struct bt_value
*bt_trace_class_borrow_environment_entry_value_by_name_const(
381 const struct bt_trace_class
*tc
, const char *name
)
383 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
384 BT_ASSERT_PRE_NON_NULL(name
, "Name");
385 return bt_attributes_borrow_field_value_by_name(tc
->environment
,
389 uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class
*tc
)
391 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
392 return (uint64_t) tc
->stream_classes
->len
;
395 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_index(
396 struct bt_trace_class
*tc
, uint64_t index
)
398 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
399 BT_ASSERT_PRE_VALID_INDEX(index
, tc
->stream_classes
->len
);
400 return g_ptr_array_index(tc
->stream_classes
, index
);
403 const struct bt_stream_class
*
404 bt_trace_class_borrow_stream_class_by_index_const(
405 const struct bt_trace_class
*tc
, uint64_t index
)
407 return bt_trace_class_borrow_stream_class_by_index(
411 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_id(
412 struct bt_trace_class
*tc
, uint64_t id
)
414 struct bt_stream_class
*stream_class
= NULL
;
417 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
419 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
420 struct bt_stream_class
*stream_class_candidate
=
421 g_ptr_array_index(tc
->stream_classes
, i
);
423 if (stream_class_candidate
->id
== id
) {
424 stream_class
= stream_class_candidate
;
433 const struct bt_stream_class
*
434 bt_trace_class_borrow_stream_class_by_id_const(
435 const struct bt_trace_class
*tc
, uint64_t id
)
437 return bt_trace_class_borrow_stream_class_by_id((void *) tc
, id
);
441 void _bt_trace_class_freeze(const struct bt_trace_class
*tc
)
444 BT_LIB_LOGD("Freezing trace class: %!+T", tc
);
445 ((struct bt_trace_class
*) tc
)->frozen
= true;
448 bt_bool
bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class
*tc
)
450 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
451 return (bt_bool
) tc
->assigns_automatic_stream_class_id
;
454 void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class
*tc
,
457 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
458 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc
);
459 tc
->assigns_automatic_stream_class_id
= (bool) value
;
460 BT_LIB_LOGV("Set trace class's automatic stream class ID "
461 "assignment property: %!+T", tc
);
464 void bt_trace_class_get_ref(const struct bt_trace_class
*trace_class
)
466 bt_object_get_ref(trace_class
);
469 void bt_trace_class_put_ref(const struct bt_trace_class
*trace_class
)
471 bt_object_put_ref(trace_class
);