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/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 #define BT_ASSERT_PRE_TRACE_CLASS_HOT(_tc) \
59 BT_ASSERT_PRE_HOT((_tc), "Trace class", ": %!+T", (_tc))
62 void destroy_trace_class(struct bt_object
*obj
)
64 struct bt_trace_class
*tc
= (void *) obj
;
66 BT_LIB_LOGD("Destroying trace class object: %!+T", tc
);
67 bt_object_pool_finalize(&tc
->packet_header_field_pool
);
69 if (tc
->environment
) {
70 BT_LOGD_STR("Destroying environment attributes.");
71 bt_attributes_destroy(tc
->environment
);
72 tc
->environment
= NULL
;
76 g_string_free(tc
->name
.str
, TRUE
);
78 tc
->name
.value
= NULL
;
81 if (tc
->stream_classes
) {
82 BT_LOGD_STR("Destroying stream classes.");
83 g_ptr_array_free(tc
->stream_classes
, TRUE
);
84 tc
->stream_classes
= NULL
;
87 BT_LOGD_STR("Putting packet header field class.");
88 bt_object_put_ref(tc
->packet_header_fc
);
89 tc
->packet_header_fc
= NULL
;
94 void free_packet_header_field(struct bt_field_wrapper
*field_wrapper
,
95 struct bt_trace_class
*tc
)
97 bt_field_wrapper_destroy(field_wrapper
);
100 struct bt_trace_class
*bt_trace_class_create(void)
102 struct bt_trace_class
*tc
= NULL
;
105 BT_LOGD_STR("Creating default trace class object.");
106 tc
= g_new0(struct bt_trace_class
, 1);
108 BT_LOGE_STR("Failed to allocate one trace class.");
112 bt_object_init_shared_with_parent(&tc
->base
, destroy_trace_class
);
114 tc
->stream_classes
= g_ptr_array_new_with_free_func(
115 (GDestroyNotify
) bt_object_try_spec_release
);
116 if (!tc
->stream_classes
) {
117 BT_LOGE_STR("Failed to allocate one GPtrArray.");
121 tc
->name
.str
= g_string_new(NULL
);
123 BT_LOGE_STR("Failed to allocate one GString.");
127 tc
->environment
= bt_attributes_create();
128 if (!tc
->environment
) {
129 BT_LOGE_STR("Cannot create empty attributes object.");
133 tc
->assigns_automatic_stream_class_id
= true;
134 ret
= bt_object_pool_initialize(&tc
->packet_header_field_pool
,
135 (bt_object_pool_new_object_func
) bt_field_wrapper_new
,
136 (bt_object_pool_destroy_object_func
) free_packet_header_field
,
139 BT_LOGE("Failed to initialize packet header field pool: ret=%d",
144 BT_LIB_LOGD("Created trace class object: %!+T", tc
);
148 BT_OBJECT_PUT_REF_AND_RESET(tc
);
154 const char *bt_trace_class_get_name(const struct bt_trace_class
*tc
)
156 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
157 return tc
->name
.value
;
160 int bt_trace_class_set_name(struct bt_trace_class
*tc
, const char *name
)
162 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
163 BT_ASSERT_PRE_NON_NULL(name
, "Name");
164 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc
);
165 g_string_assign(tc
->name
.str
, name
);
166 tc
->name
.value
= tc
->name
.str
->str
;
167 BT_LIB_LOGV("Set trace class's name: %!+T", tc
);
171 bt_uuid
bt_trace_class_get_uuid(const struct bt_trace_class
*tc
)
173 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
174 return tc
->uuid
.value
;
177 void bt_trace_class_set_uuid(struct bt_trace_class
*tc
, bt_uuid uuid
)
179 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
180 BT_ASSERT_PRE_NON_NULL(uuid
, "UUID");
181 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc
);
182 memcpy(tc
->uuid
.uuid
, uuid
, BABELTRACE_UUID_LEN
);
183 tc
->uuid
.value
= tc
->uuid
.uuid
;
184 BT_LIB_LOGV("Set trace class's UUID: %!+T", tc
);
189 bool trace_has_environment_entry(const struct bt_trace_class
*tc
, const char *name
)
193 return bt_attributes_borrow_field_value_by_name(
194 tc
->environment
, name
) != NULL
;
198 int set_environment_entry(struct bt_trace_class
*tc
, const char *name
,
199 struct bt_value
*value
)
206 BT_ASSERT_PRE(!tc
->frozen
||
207 !trace_has_environment_entry(tc
, name
),
208 "Trace class is frozen: cannot replace environment entry: "
209 "%![tc-]+T, entry-name=\"%s\"", tc
, name
);
210 ret
= bt_attributes_set_field_value(tc
->environment
, name
,
212 bt_value_freeze(value
);
214 BT_LIB_LOGE("Cannot set trace class's environment entry: "
215 "%![tc-]+T, entry-name=\"%s\"", tc
, name
);
217 BT_LIB_LOGV("Set trace class's environment entry: "
218 "%![tc-]+T, entry-name=\"%s\"", tc
, name
);
224 int bt_trace_class_set_environment_entry_string(
225 struct bt_trace_class
*tc
, const char *name
, const char *value
)
228 struct bt_value
*value_obj
;
229 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
230 BT_ASSERT_PRE_NON_NULL(name
, "Name");
231 BT_ASSERT_PRE_NON_NULL(value
, "Value");
232 value_obj
= bt_value_string_create_init(value
);
234 BT_LOGE_STR("Cannot create a string value object.");
239 /* set_environment_entry() logs errors */
240 ret
= set_environment_entry(tc
, name
, value_obj
);
243 bt_object_put_ref(value_obj
);
247 int bt_trace_class_set_environment_entry_integer(struct bt_trace_class
*tc
,
248 const char *name
, int64_t value
)
251 struct bt_value
*value_obj
;
252 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
253 BT_ASSERT_PRE_NON_NULL(name
, "Name");
254 value_obj
= bt_value_integer_create_init(value
);
256 BT_LOGE_STR("Cannot create an integer value object.");
261 /* set_environment_entry() logs errors */
262 ret
= set_environment_entry(tc
, name
, value_obj
);
265 bt_object_put_ref(value_obj
);
269 uint64_t bt_trace_class_get_environment_entry_count(const struct bt_trace_class
*tc
)
273 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
274 ret
= bt_attributes_get_count(tc
->environment
);
276 return (uint64_t) ret
;
279 void bt_trace_class_borrow_environment_entry_by_index_const(
280 const struct bt_trace_class
*tc
, uint64_t index
,
281 const char **name
, const struct bt_value
**value
)
283 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
284 BT_ASSERT_PRE_NON_NULL(name
, "Name");
285 BT_ASSERT_PRE_NON_NULL(value
, "Value");
286 BT_ASSERT_PRE_VALID_INDEX(index
,
287 bt_attributes_get_count(tc
->environment
));
288 *value
= bt_attributes_borrow_field_value(tc
->environment
, index
);
290 *name
= bt_attributes_get_field_name(tc
->environment
, index
);
294 const struct bt_value
*bt_trace_class_borrow_environment_entry_value_by_name_const(
295 const struct bt_trace_class
*tc
, const char *name
)
297 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
298 BT_ASSERT_PRE_NON_NULL(name
, "Name");
299 return bt_attributes_borrow_field_value_by_name(tc
->environment
,
303 uint64_t bt_trace_class_get_stream_class_count(const struct bt_trace_class
*tc
)
305 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
306 return (uint64_t) tc
->stream_classes
->len
;
309 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_index(
310 struct bt_trace_class
*tc
, uint64_t index
)
312 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
313 BT_ASSERT_PRE_VALID_INDEX(index
, tc
->stream_classes
->len
);
314 return g_ptr_array_index(tc
->stream_classes
, index
);
317 const struct bt_stream_class
*
318 bt_trace_class_borrow_stream_class_by_index_const(
319 const struct bt_trace_class
*tc
, uint64_t index
)
321 return bt_trace_class_borrow_stream_class_by_index(
325 struct bt_stream_class
*bt_trace_class_borrow_stream_class_by_id(
326 struct bt_trace_class
*tc
, uint64_t id
)
328 struct bt_stream_class
*stream_class
= NULL
;
331 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
333 for (i
= 0; i
< tc
->stream_classes
->len
; i
++) {
334 struct bt_stream_class
*stream_class_candidate
=
335 g_ptr_array_index(tc
->stream_classes
, i
);
337 if (stream_class_candidate
->id
== id
) {
338 stream_class
= stream_class_candidate
;
347 const struct bt_stream_class
*
348 bt_trace_class_borrow_stream_class_by_id_const(
349 const struct bt_trace_class
*tc
, uint64_t id
)
351 return bt_trace_class_borrow_stream_class_by_id((void *) tc
, id
);
354 const struct bt_field_class
*bt_trace_class_borrow_packet_header_field_class_const(
355 const struct bt_trace_class
*tc
)
357 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
358 return tc
->packet_header_fc
;
361 int bt_trace_class_set_packet_header_field_class(
362 struct bt_trace_class
*tc
,
363 struct bt_field_class
*field_class
)
366 struct bt_resolve_field_path_context resolve_ctx
= {
367 .packet_header
= field_class
,
368 .packet_context
= NULL
,
369 .event_header
= NULL
,
370 .event_common_context
= NULL
,
371 .event_specific_context
= NULL
,
372 .event_payload
= NULL
,
375 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
376 BT_ASSERT_PRE_NON_NULL(field_class
, "Field class");
377 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc
);
378 BT_ASSERT_PRE(bt_field_class_get_type(field_class
) ==
379 BT_FIELD_CLASS_TYPE_STRUCTURE
,
380 "Packet header field class is not a structure field class: %!+F",
382 ret
= bt_resolve_field_paths(field_class
, &resolve_ctx
);
387 bt_field_class_make_part_of_trace_class(field_class
);
388 bt_object_put_ref(tc
->packet_header_fc
);
389 tc
->packet_header_fc
= field_class
;
390 bt_object_get_no_null_check(tc
->packet_header_fc
);
391 bt_field_class_freeze(field_class
);
392 BT_LIB_LOGV("Set trace class's packet header field class: %!+T", tc
);
399 void _bt_trace_class_freeze(const struct bt_trace_class
*tc
)
401 /* The packet header field class is already frozen */
403 BT_LIB_LOGD("Freezing trace class: %!+T", tc
);
404 ((struct bt_trace_class
*) tc
)->frozen
= true;
407 bt_bool
bt_trace_class_assigns_automatic_stream_class_id(const struct bt_trace_class
*tc
)
409 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
410 return (bt_bool
) tc
->assigns_automatic_stream_class_id
;
413 void bt_trace_class_set_assigns_automatic_stream_class_id(struct bt_trace_class
*tc
,
416 BT_ASSERT_PRE_NON_NULL(tc
, "Trace class");
417 BT_ASSERT_PRE_TRACE_CLASS_HOT(tc
);
418 tc
->assigns_automatic_stream_class_id
= (bool) value
;
419 BT_LIB_LOGV("Set trace class's automatic stream class ID "
420 "assignment property: %!+T", tc
);