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
);
80 * The trace class' reference count is 0 if we're here. Increment
81 * it to avoid a double-destroy (possibly infinitely recursive).
82 * This could happen for example if a destruction listener did
83 * bt_object_get_ref() (or anything that causes
84 * bt_object_get_ref() to be called) on the trace class (ref.
85 * count goes from 0 to 1), and then bt_object_put_ref(): the
86 * reference count would go from 1 to 0 again and this function
87 * would be called again.
91 /* Call all the trace class destruction listeners */
92 for (i
= 0; i
< tc
->destruction_listeners
->len
; i
++) {
93 struct bt_trace_class_destruction_listener_elem elem
=
94 g_array_index(tc
->destruction_listeners
,
95 struct bt_trace_class_destruction_listener_elem
, i
);
98 elem
.func(tc
, elem
.data
);
102 * The destruction listener should not have kept a
103 * reference to the trace class.
105 BT_ASSERT_PRE(tc
->base
.ref_count
== 1, "Destruction listener kept a reference to the trace class being destroyed: %![tc-]+T", tc
);
107 g_array_free(tc
->destruction_listeners
, TRUE
);
108 tc
->destruction_listeners
= NULL
;
111 if (tc
->environment
) {
112 BT_LOGD_STR("Destroying environment attributes.");
113 bt_attributes_destroy(tc
->environment
);
114 tc
->environment
= NULL
;
118 g_string_free(tc
->name
.str
, TRUE
);
120 tc
->name
.value
= NULL
;
123 if (tc
->stream_classes
) {
124 BT_LOGD_STR("Destroying stream classes.");
125 g_ptr_array_free(tc
->stream_classes
, TRUE
);
126 tc
->stream_classes
= NULL
;
132 struct bt_trace_class
*bt_trace_class_create(bt_self_component
*self_comp
)
134 struct bt_trace_class
*tc
= NULL
;
136 BT_ASSERT_PRE_NON_NULL(self_comp
, "Self component");
137 BT_LOGD_STR("Creating default trace class object.");
138 tc
= g_new0(struct bt_trace_class
, 1);
140 BT_LOGE_STR("Failed to allocate one trace class.");
144 bt_object_init_shared_with_parent(&tc
->base
, destroy_trace_class
);
146 tc
->stream_classes
= g_ptr_array_new_with_free_func(
147 (GDestroyNotify
) bt_object_try_spec_release
);
148 if (!tc
->stream_classes
) {
149 BT_LOGE_STR("Failed to allocate one GPtrArray.");
153 tc
->name
.str
= g_string_new(NULL
);
155 BT_LOGE_STR("Failed to allocate one GString.");
159 tc
->environment
= bt_attributes_create();
160 if (!tc
->environment
) {
161 BT_LOGE_STR("Cannot create empty attributes object.");
165 tc
->destruction_listeners
= g_array_new(FALSE
, TRUE
,
166 sizeof(struct bt_trace_class_destruction_listener_elem
));
167 if (!tc
->destruction_listeners
) {
168 BT_LOGE_STR("Failed to allocate one GArray.");
172 tc
->assigns_automatic_stream_class_id
= true;
173 BT_LIB_LOGD("Created trace class object: %!+T", tc
);
177 BT_OBJECT_PUT_REF_AND_RESET(tc
);
183 const char *bt_trace_class_get_name(const struct bt_trace_class
*tc
)
185 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
186 return tc
->name
.value
;
189 enum bt_trace_class_status
bt_trace_class_set_name(
190 struct bt_trace_class
*tc
, const char *name
)
192 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
193 BT_ASSERT_PRE_NON_NULL(name
, "Name");
194 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc
);
195 g_string_assign(tc
->name
.str
, name
);
196 tc
->name
.value
= tc
->name
.str
->str
;
197 BT_LIB_LOGV("Set trace class's name: %!+T", tc
);
198 return BT_TRACE_CLASS_STATUS_OK
;
201 bt_uuid
bt_trace_class_get_uuid(const struct bt_trace_class
*tc
)
203 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
204 return tc
->uuid
.value
;
207 void bt_trace_class_set_uuid(struct bt_trace_class
*tc
, bt_uuid uuid
)
209 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
210 BT_ASSERT_PRE_NON_NULL(uuid
, "UUID");
211 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc
);
212 memcpy(tc
->uuid
.uuid
, uuid
, BABELTRACE_UUID_LEN
);
213 tc
->uuid
.value
= tc
->uuid
.uuid
;
214 BT_LIB_LOGV("Set trace class's UUID: %!+T", tc
);
217 enum bt_trace_class_status
bt_trace_class_add_destruction_listener(
218 const struct bt_trace_class
*_tc
,
219 bt_trace_class_destruction_listener_func listener
,
220 void *data
, uint64_t *listener_id
)
222 struct bt_trace_class
*tc
= (void *) _tc
;
224 struct bt_trace_class_destruction_listener_elem new_elem
= {
229 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
230 BT_ASSERT_PRE_NON_NULL(listener
, "Listener");
232 /* Find the next available spot */
233 for (i
= 0; i
< tc
->destruction_listeners
->len
; i
++) {
234 struct bt_trace_class_destruction_listener_elem elem
=
235 g_array_index(tc
->destruction_listeners
,
236 struct bt_trace_class_destruction_listener_elem
, i
);
243 if (i
== tc
->destruction_listeners
->len
) {
244 g_array_append_val(tc
->destruction_listeners
, new_elem
);
246 g_array_insert_val(tc
->destruction_listeners
, i
, new_elem
);
253 BT_LIB_LOGV("Added trace class destruction listener: %![tc-]+T, "
254 "listener-id=%" PRIu64
, tc
, i
);
255 return BT_TRACE_CLASS_STATUS_OK
;
260 bool has_listener_id(const struct bt_trace_class
*tc
, uint64_t listener_id
)
262 BT_ASSERT(listener_id
< tc
->destruction_listeners
->len
);
263 return (&g_array_index(tc
->destruction_listeners
,
264 struct bt_trace_class_destruction_listener_elem
,
265 listener_id
))->func
!= NULL
;
268 enum bt_trace_class_status
bt_trace_class_remove_destruction_listener(
269 const struct bt_trace_class
*_tc
, uint64_t listener_id
)
271 struct bt_trace_class
*tc
= (void *) _tc
;
272 struct bt_trace_class_destruction_listener_elem
*elem
;
274 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
275 BT_ASSERT_PRE(has_listener_id(tc
, listener_id
),
276 "Trace class has no such trace class destruction listener ID: "
277 "%![tc-]+T, %" PRIu64
, tc
, listener_id
);
278 elem
= &g_array_index(tc
->destruction_listeners
,
279 struct bt_trace_class_destruction_listener_elem
,
281 BT_ASSERT(elem
->func
);
285 BT_LIB_LOGV("Removed trace class destruction listener: "
286 "%![tc-]+T, listener-id=%" PRIu64
,
288 return BT_TRACE_CLASS_STATUS_OK
;
293 bool trace_has_environment_entry(const struct bt_trace_class
*tc
, const char *name
)
297 return bt_attributes_borrow_field_value_by_name(
298 tc
->environment
, name
) != NULL
;
302 enum bt_trace_class_status
set_environment_entry(struct bt_trace_class
*tc
,
303 const char *name
, struct bt_value
*value
)
310 BT_ASSERT_PRE(!tc
->frozen
||
311 !trace_has_environment_entry(tc
, name
),
312 "Trace class is frozen: cannot replace environment entry: "
313 "%![tc-]+T, entry-name=\"%s\"", tc
, name
);
314 ret
= bt_attributes_set_field_value(tc
->environment
, name
,
317 ret
= BT_TRACE_CLASS_STATUS_NOMEM
;
318 BT_LIB_LOGE("Cannot set trace class's environment entry: "
319 "%![tc-]+T, entry-name=\"%s\"", tc
, name
);
321 bt_value_freeze(value
);
322 BT_LIB_LOGV("Set trace class's environment entry: "
323 "%![tc-]+T, entry-name=\"%s\"", tc
, name
);
329 enum bt_trace_class_status
bt_trace_class_set_environment_entry_string(
330 struct bt_trace_class
*tc
, const char *name
, const char *value
)
333 struct bt_value
*value_obj
;
334 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
335 BT_ASSERT_PRE_NON_NULL(name
, "Name");
336 BT_ASSERT_PRE_NON_NULL(value
, "Value");
337 value_obj
= bt_value_string_create_init(value
);
339 BT_LOGE_STR("Cannot create a string value object.");
344 /* set_environment_entry() logs errors */
345 ret
= set_environment_entry(tc
, name
, value_obj
);
348 bt_object_put_ref(value_obj
);
352 enum bt_trace_class_status
bt_trace_class_set_environment_entry_integer(
353 struct bt_trace_class
*tc
, const char *name
, int64_t value
)
356 struct bt_value
*value_obj
;
357 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
358 BT_ASSERT_PRE_NON_NULL(name
, "Name");
359 value_obj
= bt_value_signed_integer_create_init(value
);
361 BT_LOGE_STR("Cannot create an integer value object.");
362 ret
= BT_TRACE_CLASS_STATUS_NOMEM
;
366 /* set_environment_entry() logs errors */
367 ret
= set_environment_entry(tc
, name
, value_obj
);
370 bt_object_put_ref(value_obj
);
374 uint64_t bt_trace_class_get_environment_entry_count(const struct bt_trace_class
*tc
)
378 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
379 ret
= bt_attributes_get_count(tc
->environment
);
381 return (uint64_t) ret
;
384 void bt_trace_class_borrow_environment_entry_by_index_const(
385 const struct bt_trace_class
*tc
, uint64_t index
,
386 const char **name
, const struct bt_value
**value
)
388 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
389 BT_ASSERT_PRE_NON_NULL(name
, "Name");
390 BT_ASSERT_PRE_NON_NULL(value
, "Value");
391 BT_ASSERT_PRE_VALID_INDEX(index
,
392 bt_attributes_get_count(tc
->environment
));
393 *value
= bt_attributes_borrow_field_value(tc
->environment
, index
);
395 *name
= bt_attributes_get_field_name(tc
->environment
, index
);
399 const struct bt_value
*bt_trace_class_borrow_environment_entry_value_by_name_const(
400 const struct bt_trace_class
*tc
, const char *name
)
402 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
403 BT_ASSERT_PRE_NON_NULL(name
, "Name");
404 return bt_attributes_borrow_field_value_by_name(tc
->environment
,
408 uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class
*tc
)
410 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
411 return (uint64_t) tc
->stream_classes
->len
;
414 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_index(
415 struct bt_trace_class
*tc
, uint64_t index
)
417 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
418 BT_ASSERT_PRE_VALID_INDEX(index
, tc
->stream_classes
->len
);
419 return g_ptr_array_index(tc
->stream_classes
, index
);
422 const struct bt_stream_class
*
423 bt_trace_class_borrow_stream_class_by_index_const(
424 const struct bt_trace_class
*tc
, uint64_t index
)
426 return bt_trace_class_borrow_stream_class_by_index(
430 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_id(
431 struct bt_trace_class
*tc
, uint64_t id
)
433 struct bt_stream_class
*stream_class
= NULL
;
436 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
438 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
439 struct bt_stream_class
*stream_class_candidate
=
440 g_ptr_array_index(tc
->stream_classes
, i
);
442 if (stream_class_candidate
->id
== id
) {
443 stream_class
= stream_class_candidate
;
452 const struct bt_stream_class
*
453 bt_trace_class_borrow_stream_class_by_id_const(
454 const struct bt_trace_class
*tc
, uint64_t id
)
456 return bt_trace_class_borrow_stream_class_by_id((void *) tc
, id
);
460 void _bt_trace_class_freeze(const struct bt_trace_class
*tc
)
463 BT_LIB_LOGD("Freezing trace class: %!+T", tc
);
464 ((struct bt_trace_class
*) tc
)->frozen
= true;
467 bt_bool
bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class
*tc
)
469 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
470 return (bt_bool
) tc
->assigns_automatic_stream_class_id
;
473 void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class
*tc
,
476 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
477 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc
);
478 tc
->assigns_automatic_stream_class_id
= (bool) value
;
479 BT_LIB_LOGV("Set trace class's automatic stream class ID "
480 "assignment property: %!+T", tc
);
483 void bt_trace_class_get_ref(const struct bt_trace_class
*trace_class
)
485 bt_object_get_ref(trace_class
);
488 void bt_trace_class_put_ref(const struct bt_trace_class
*trace_class
)
490 bt_object_put_ref(trace_class
);