2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2017-2018 Philippe Proulx <pproulx@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 "CTF-WRITER-TRACE"
25 #include <babeltrace/lib-logging-internal.h>
27 #include <babeltrace/ctf-ir/trace-internal.h>
28 #include <babeltrace/ctf-ir/clock-class.h>
29 #include <babeltrace/ctf-writer/clock-internal.h>
30 #include <babeltrace/ctf-writer/trace-internal.h>
31 #include <babeltrace/ctf-writer/stream-class-internal.h>
32 #include <babeltrace/ctf-writer/writer-internal.h>
33 #include <babeltrace/ctf-writer/field-types-internal.h>
35 #define DEFAULT_IDENTIFIER_SIZE 128
36 #define DEFAULT_METADATA_STRING_SIZE 4096
39 void bt_ctf_trace_destroy(struct bt_object
*obj
)
41 struct bt_ctf_trace
*trace
= (void *) obj
;
43 BT_LOGD("Destroying CTF writer trace object: addr=%p, name=\"%s\"",
44 trace
, bt_ctf_trace_get_name(trace
));
45 bt_trace_common_finalize(BT_TO_COMMON(trace
));
50 struct bt_ctf_trace
*bt_ctf_trace_create(void)
52 struct bt_ctf_trace
*trace
= NULL
;
55 BT_LOGD_STR("Creating CTF writer trace object.");
56 trace
= g_new0(struct bt_ctf_trace
, 1);
58 BT_LOGE_STR("Failed to allocate one CTF writer trace.");
62 ret
= bt_trace_common_initialize(BT_TO_COMMON(trace
),
63 bt_ctf_trace_destroy
);
65 /* bt_trace_common_initialize() logs errors */
69 BT_LOGD("Created CTF writer trace object: addr=%p", trace
);
77 const unsigned char *bt_ctf_trace_get_uuid(struct bt_ctf_trace
*trace
)
79 return bt_trace_common_get_uuid(BT_TO_COMMON(trace
));
82 int bt_ctf_trace_set_uuid(struct bt_ctf_trace
*trace
,
83 const unsigned char *uuid
)
85 return bt_trace_common_set_uuid(BT_TO_COMMON(trace
), uuid
);
88 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace
*trace
,
89 const char *name
, struct bt_value
*value
)
91 return bt_trace_common_set_environment_field(BT_TO_COMMON(trace
),
95 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace
*trace
,
96 const char *name
, const char *value
)
98 return bt_trace_common_set_environment_field_string(BT_TO_COMMON(trace
),
102 int bt_ctf_trace_set_environment_field_integer(
103 struct bt_ctf_trace
*trace
, const char *name
, int64_t value
)
105 return bt_trace_common_set_environment_field_integer(
106 BT_TO_COMMON(trace
), name
, value
);
109 int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace
*trace
)
111 return bt_trace_common_get_environment_field_count(BT_TO_COMMON(trace
));
115 bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace
*trace
,
118 return bt_trace_common_get_environment_field_name_by_index(
119 BT_TO_COMMON(trace
), index
);
122 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_index(
123 struct bt_ctf_trace
*trace
, uint64_t index
)
125 return bt_trace_common_get_environment_field_value_by_index(
126 BT_TO_COMMON(trace
), index
);
129 struct bt_value
*bt_ctf_trace_get_environment_field_value_by_name(
130 struct bt_ctf_trace
*trace
, const char *name
)
132 return bt_trace_common_get_environment_field_value_by_name(
133 BT_TO_COMMON(trace
), name
);
136 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace
*trace
,
137 struct bt_ctf_clock_class
*clock_class
)
139 return bt_trace_common_add_clock_class(BT_TO_COMMON(trace
),
140 (void *) clock_class
);
143 int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace
*trace
)
145 return bt_trace_common_get_clock_class_count(BT_TO_COMMON(trace
));
148 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_index(
149 struct bt_ctf_trace
*trace
, uint64_t index
)
151 return (void *) bt_trace_common_get_clock_class_by_index(
152 BT_TO_COMMON(trace
), index
);
156 int map_clock_classes_func(struct bt_stream_class_common
*stream_class
,
157 struct bt_field_type_common
*packet_context_type
,
158 struct bt_field_type_common
*event_header_type
)
160 int ret
= bt_ctf_stream_class_map_clock_class(
161 BT_FROM_COMMON(stream_class
),
162 BT_FROM_COMMON(packet_context_type
),
163 BT_FROM_COMMON(event_header_type
));
166 BT_LOGW_STR("Cannot automatically map selected stream class's field types to stream class's clock's class.");
172 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace
*trace
,
173 struct bt_ctf_stream_class
*stream_class
)
176 struct bt_clock_class
*expected_clock_class
= NULL
;
179 BT_LOGW_STR("Invalid parameter: trace is NULL.");
185 BT_LOGW_STR("Invalid parameter: stream class is NULL.");
190 if (stream_class
->clock
) {
191 struct bt_ctf_clock_class
*stream_clock_class
=
192 stream_class
->clock
->clock_class
;
195 * Make sure this clock was also added to the
196 * trace (potentially through its CTF writer
201 for (i
= 0; i
< trace
->common
.clock_classes
->len
; i
++) {
202 if (trace
->common
.clock_classes
->pdata
[i
] ==
203 stream_clock_class
) {
209 if (i
== trace
->common
.clock_classes
->len
) {
211 BT_LOGW("Stream class's clock's class is not part of the trace: "
212 "clock-class-addr=%p, clock-class-name=\"%s\"",
214 bt_clock_class_get_name(
215 BT_TO_COMMON(stream_clock_class
)));
220 if (stream_class
->common
.clock_class
&&
221 stream_class
->common
.clock_class
!=
222 BT_TO_COMMON(stream_class
->clock
->clock_class
)) {
224 * Stream class already has an expected clock
225 * class, but it does not match its clock's
228 BT_LOGW("Invalid parameter: stream class's clock's "
229 "class does not match stream class's "
230 "expected clock class: "
231 "stream-class-addr=%p, "
232 "stream-class-id=%" PRId64
", "
233 "stream-class-name=\"%s\", "
234 "expected-clock-class-addr=%p, "
235 "expected-clock-class-name=\"%s\"",
237 bt_ctf_stream_class_get_id(stream_class
),
238 bt_ctf_stream_class_get_name(stream_class
),
239 stream_class
->common
.clock_class
,
240 bt_clock_class_get_name(stream_class
->common
.clock_class
));
241 } else if (!stream_class
->common
.clock_class
) {
243 * Set expected clock class to stream class's
246 expected_clock_class
=
247 BT_TO_COMMON(stream_class
->clock
->clock_class
);
252 ret
= bt_trace_common_add_stream_class(BT_TO_COMMON(trace
),
253 BT_TO_COMMON(stream_class
),
254 (bt_validation_flag_copy_field_type_func
) bt_ctf_field_type_copy
,
255 expected_clock_class
, map_clock_classes_func
,
262 int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace
*trace
)
264 return bt_trace_common_get_stream_count(BT_TO_COMMON(trace
));
267 struct bt_ctf_stream
*bt_ctf_trace_get_stream_by_index(
268 struct bt_ctf_trace
*trace
, uint64_t index
)
270 return BT_FROM_COMMON(bt_trace_common_get_stream_by_index(
271 BT_TO_COMMON(trace
), index
));
274 int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace
*trace
)
276 return bt_trace_common_get_stream_class_count(BT_TO_COMMON(trace
));
279 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_index(
280 struct bt_ctf_trace
*trace
, uint64_t index
)
282 return BT_FROM_COMMON(bt_trace_common_get_stream_class_by_index(
283 BT_TO_COMMON(trace
), index
));
286 struct bt_ctf_stream_class
*bt_ctf_trace_get_stream_class_by_id(
287 struct bt_ctf_trace
*trace
, uint64_t id
)
289 return BT_FROM_COMMON(
290 bt_trace_common_get_stream_class_by_id(
291 BT_TO_COMMON(trace
), id
));
294 struct bt_ctf_clock_class
*bt_ctf_trace_get_clock_class_by_name(
295 struct bt_ctf_trace
*trace
, const char *name
)
297 return BT_FROM_COMMON(
298 bt_trace_common_get_clock_class_by_name(BT_TO_COMMON(trace
),
303 int append_trace_metadata(struct bt_ctf_trace
*trace
,
304 struct metadata_context
*context
)
306 unsigned char *uuid
= trace
->common
.uuid
;
309 if (trace
->common
.native_byte_order
== BT_BYTE_ORDER_NATIVE
||
310 trace
->common
.native_byte_order
== BT_BYTE_ORDER_UNSPECIFIED
) {
311 BT_LOGW("Invalid parameter: trace's byte order cannot be BT_CTF_BYTE_ORDER_NATIVE or BT_CTF_BYTE_ORDER_UNSPECIFIED at this point; "
312 "set it with bt_ctf_trace_set_native_byte_order(): "
313 "addr=%p, name=\"%s\"",
314 trace
, bt_ctf_trace_get_name(trace
));
319 g_string_append(context
->string
, "trace {\n");
320 g_string_append(context
->string
, "\tmajor = 1;\n");
321 g_string_append(context
->string
, "\tminor = 8;\n");
322 BT_ASSERT(trace
->common
.native_byte_order
== BT_BYTE_ORDER_LITTLE_ENDIAN
||
323 trace
->common
.native_byte_order
== BT_BYTE_ORDER_BIG_ENDIAN
||
324 trace
->common
.native_byte_order
== BT_BYTE_ORDER_NETWORK
);
326 if (trace
->common
.uuid_set
) {
327 g_string_append_printf(context
->string
,
328 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
329 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
330 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
331 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
332 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
335 g_string_append_printf(context
->string
, "\tbyte_order = %s;\n",
336 get_byte_order_string(trace
->common
.native_byte_order
));
338 if (trace
->common
.packet_header_field_type
) {
339 g_string_append(context
->string
, "\tpacket.header := ");
340 context
->current_indentation_level
++;
341 g_string_assign(context
->field_name
, "");
342 BT_LOGD_STR("Serializing trace's packet header field type's metadata.");
343 ret
= bt_ctf_field_type_serialize_recursive(
344 (void *) trace
->common
.packet_header_field_type
,
349 context
->current_indentation_level
--;
352 g_string_append(context
->string
, ";\n};\n\n");
358 void append_env_metadata(struct bt_ctf_trace
*trace
,
359 struct metadata_context
*context
)
364 env_size
= bt_attributes_get_count(trace
->common
.environment
);
369 g_string_append(context
->string
, "env {\n");
371 for (i
= 0; i
< env_size
; i
++) {
372 struct bt_value
*env_field_value_obj
= NULL
;
373 const char *entry_name
;
375 entry_name
= bt_attributes_get_field_name(
376 trace
->common
.environment
, i
);
377 env_field_value_obj
= bt_attributes_get_field_value(
378 trace
->common
.environment
, i
);
380 BT_ASSERT(entry_name
);
381 BT_ASSERT(env_field_value_obj
);
383 switch (bt_value_get_type(env_field_value_obj
)) {
384 case BT_VALUE_TYPE_INTEGER
:
389 ret
= bt_value_integer_get(env_field_value_obj
,
392 g_string_append_printf(context
->string
,
393 "\t%s = %" PRId64
";\n", entry_name
,
397 case BT_VALUE_TYPE_STRING
:
400 const char *str_value
;
401 char *escaped_str
= NULL
;
403 ret
= bt_value_string_get(env_field_value_obj
,
406 escaped_str
= g_strescape(str_value
, NULL
);
408 BT_LOGE("Cannot escape string: string=\"%s\"",
413 g_string_append_printf(context
->string
,
414 "\t%s = \"%s\";\n", entry_name
, escaped_str
);
423 BT_PUT(env_field_value_obj
);
426 g_string_append(context
->string
, "};\n\n");
429 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace
*trace
)
431 char *metadata
= NULL
;
432 struct metadata_context
*context
= NULL
;
437 BT_LOGW_STR("Invalid parameter: trace is NULL.");
441 context
= g_new0(struct metadata_context
, 1);
443 BT_LOGE_STR("Failed to allocate one metadata context.");
447 context
->field_name
= g_string_sized_new(DEFAULT_IDENTIFIER_SIZE
);
448 context
->string
= g_string_sized_new(DEFAULT_METADATA_STRING_SIZE
);
449 g_string_append(context
->string
, "/* CTF 1.8 */\n\n");
450 if (append_trace_metadata(trace
, context
)) {
451 /* append_trace_metadata() logs errors */
454 append_env_metadata(trace
, context
);
455 g_ptr_array_foreach(trace
->common
.clock_classes
,
456 (GFunc
) bt_ctf_clock_class_serialize
, context
);
458 for (i
= 0; i
< trace
->common
.stream_classes
->len
; i
++) {
459 /* bt_stream_class_serialize() logs details */
460 err
= bt_ctf_stream_class_serialize(
461 trace
->common
.stream_classes
->pdata
[i
], context
);
463 /* bt_stream_class_serialize() logs errors */
468 metadata
= context
->string
->str
;
471 g_string_free(context
->string
, err
? TRUE
: FALSE
);
472 g_string_free(context
->field_name
, TRUE
);
479 enum bt_ctf_byte_order
bt_ctf_trace_get_native_byte_order(
480 struct bt_ctf_trace
*trace
)
482 return (int) bt_trace_common_get_native_byte_order(BT_TO_COMMON(trace
));
485 int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace
*trace
,
486 enum bt_ctf_byte_order byte_order
)
488 return bt_trace_common_set_native_byte_order(BT_TO_COMMON(trace
),
489 (int) byte_order
, false);
492 struct bt_ctf_field_type
*bt_ctf_trace_get_packet_header_field_type(
493 struct bt_ctf_trace
*trace
)
495 return BT_FROM_COMMON(bt_trace_common_get_packet_header_field_type(
496 BT_TO_COMMON(trace
)));
499 int bt_ctf_trace_set_packet_header_field_type(struct bt_ctf_trace
*trace
,
500 struct bt_ctf_field_type
*packet_header_type
)
502 return bt_trace_common_set_packet_header_field_type(BT_TO_COMMON(trace
),
503 (void *) packet_header_type
);
506 const char *bt_ctf_trace_get_name(struct bt_ctf_trace
*trace
)
508 return bt_trace_common_get_name(BT_TO_COMMON(trace
));