Fix: unchecked return value of bt_ctf_field_type_variant_get_field
[babeltrace.git] / formats / ctf / ir / trace.c
CommitLineData
bc37ae52
JG
1/*
2 * trace.c
3 *
4 * Babeltrace CTF IR - Trace
5 *
6 * Copyright 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29#include <babeltrace/ctf-ir/trace-internal.h>
30#include <babeltrace/ctf-ir/clock-internal.h>
31#include <babeltrace/ctf-ir/stream-internal.h>
32#include <babeltrace/ctf-ir/stream-class-internal.h>
09840de5 33#include <babeltrace/ctf-ir/event-internal.h>
272df73e
PP
34#include <babeltrace/ctf-ir/event-class.h>
35#include <babeltrace/ctf-ir/event-class-internal.h>
bc37ae52 36#include <babeltrace/ctf-writer/functor-internal.h>
2e33ac5a 37#include <babeltrace/ctf-ir/field-types-internal.h>
44e0a4f5 38#include <babeltrace/ctf-ir/attributes-internal.h>
09840de5 39#include <babeltrace/ctf-ir/validation-internal.h>
654c1444 40#include <babeltrace/ctf-ir/utils.h>
bc37ae52 41#include <babeltrace/compiler.h>
dac5c838 42#include <babeltrace/values.h>
83509119 43#include <babeltrace/ref.h>
a0b720b2 44#include <babeltrace/endian.h>
bc37ae52
JG
45
46#define DEFAULT_IDENTIFIER_SIZE 128
47#define DEFAULT_METADATA_STRING_SIZE 4096
48
bc37ae52 49static
83509119 50void bt_ctf_trace_destroy(struct bt_object *obj);
bc37ae52
JG
51static
52int init_trace_packet_header(struct bt_ctf_trace *trace);
f67f3a6e 53static
09840de5 54void bt_ctf_trace_freeze(struct bt_ctf_trace *trace);
bc37ae52 55
bc37ae52
JG
56static
57const unsigned int field_type_aliases_alignments[] = {
58 [FIELD_TYPE_ALIAS_UINT5_T] = 1,
59 [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8,
60 [FIELD_TYPE_ALIAS_UINT27_T] = 1,
61 [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8,
62};
63
64static
65const unsigned int field_type_aliases_sizes[] = {
66 [FIELD_TYPE_ALIAS_UINT5_T] = 5,
67 [FIELD_TYPE_ALIAS_UINT8_T] = 8,
68 [FIELD_TYPE_ALIAS_UINT16_T] = 16,
69 [FIELD_TYPE_ALIAS_UINT27_T] = 27,
70 [FIELD_TYPE_ALIAS_UINT32_T] = 32,
71 [FIELD_TYPE_ALIAS_UINT64_T] = 64,
72};
73
bc37ae52
JG
74struct bt_ctf_trace *bt_ctf_trace_create(void)
75{
76 struct bt_ctf_trace *trace = NULL;
77
78 trace = g_new0(struct bt_ctf_trace, 1);
79 if (!trace) {
80 goto error;
81 }
82
83 bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE);
83509119 84 bt_object_init(trace, bt_ctf_trace_destroy);
bc37ae52 85 trace->clocks = g_ptr_array_new_with_free_func(
83509119 86 (GDestroyNotify) bt_put);
bc37ae52 87 trace->streams = g_ptr_array_new_with_free_func(
e6a8e8e4 88 (GDestroyNotify) bt_object_release);
bc37ae52 89 trace->stream_classes = g_ptr_array_new_with_free_func(
e6a8e8e4 90 (GDestroyNotify) bt_object_release);
7f800dc7 91 if (!trace->clocks || !trace->stream_classes || !trace->streams) {
83509119 92 goto error;
bc37ae52
JG
93 }
94
95 /* Generate a trace UUID */
96 uuid_generate(trace->uuid);
97 if (init_trace_packet_header(trace)) {
83509119 98 goto error;
bc37ae52
JG
99 }
100
7f800dc7
PP
101 /* Create the environment array object */
102 trace->environment = bt_ctf_attributes_create();
103 if (!trace->environment) {
83509119 104 goto error;
7f800dc7
PP
105 }
106
bc37ae52
JG
107 return trace;
108
bc37ae52 109error:
83509119 110 BT_PUT(trace);
bc37ae52
JG
111 return trace;
112}
113
83509119 114void bt_ctf_trace_destroy(struct bt_object *obj)
bc37ae52
JG
115{
116 struct bt_ctf_trace *trace;
bc37ae52 117
83509119 118 trace = container_of(obj, struct bt_ctf_trace, base);
bc37ae52 119 if (trace->environment) {
7f800dc7 120 bt_ctf_attributes_destroy(trace->environment);
bc37ae52
JG
121 }
122
123 if (trace->clocks) {
124 g_ptr_array_free(trace->clocks, TRUE);
125 }
126
127 if (trace->streams) {
128 g_ptr_array_free(trace->streams, TRUE);
129 }
130
131 if (trace->stream_classes) {
132 g_ptr_array_free(trace->stream_classes, TRUE);
133 }
134
83509119 135 bt_put(trace->packet_header_type);
bc37ae52
JG
136 g_free(trace);
137}
138
7f800dc7 139int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace,
dac5c838 140 const char *name, struct bt_value *value)
bc37ae52 141{
bc37ae52
JG
142 int ret = 0;
143
a0d12916 144 if (!trace || !name || !value ||
7f800dc7 145 bt_ctf_validate_identifier(name) ||
dac5c838 146 !(bt_value_is_integer(value) || bt_value_is_string(value))) {
bc37ae52 147 ret = -1;
7f800dc7 148 goto end;
bc37ae52
JG
149 }
150
151 if (strchr(name, ' ')) {
152 ret = -1;
7f800dc7 153 goto end;
bc37ae52
JG
154 }
155
a0d12916
JG
156 if (trace->frozen) {
157 /*
158 * New environment fields may be added to a frozen trace,
159 * but existing fields may not be changed.
160 *
161 * The object passed is frozen like all other attributes.
162 */
dac5c838 163 struct bt_value *attribute =
a0d12916
JG
164 bt_ctf_attributes_get_field_value_by_name(
165 trace->environment, name);
166
167 if (attribute) {
83509119 168 BT_PUT(attribute);
a0d12916
JG
169 ret = -1;
170 goto end;
171 }
172
dac5c838 173 bt_value_freeze(value);
a0d12916
JG
174 }
175
7f800dc7
PP
176 ret = bt_ctf_attributes_set_field_value(trace->environment, name,
177 value);
bc37ae52 178
7f800dc7
PP
179end:
180 return ret;
181}
bc37ae52 182
7f800dc7
PP
183int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace,
184 const char *name, const char *value)
185{
186 int ret = 0;
dac5c838 187 struct bt_value *env_value_string_obj = NULL;
7f800dc7
PP
188
189 if (!trace || !name || !value) {
bc37ae52 190 ret = -1;
7f800dc7 191 goto end;
bc37ae52
JG
192 }
193
a0d12916
JG
194 if (trace->frozen) {
195 /*
196 * New environment fields may be added to a frozen trace,
197 * but existing fields may not be changed.
198 */
dac5c838 199 struct bt_value *attribute =
a0d12916
JG
200 bt_ctf_attributes_get_field_value_by_name(
201 trace->environment, name);
202
203 if (attribute) {
83509119 204 BT_PUT(attribute);
a0d12916
JG
205 ret = -1;
206 goto end;
207 }
208 }
209
dac5c838 210 env_value_string_obj = bt_value_string_create_init(value);
bc37ae52 211
7f800dc7
PP
212 if (!env_value_string_obj) {
213 ret = -1;
214 goto end;
bc37ae52
JG
215 }
216
a0d12916 217 if (trace->frozen) {
dac5c838 218 bt_value_freeze(env_value_string_obj);
a0d12916 219 }
7f800dc7
PP
220 ret = bt_ctf_trace_set_environment_field(trace, name,
221 env_value_string_obj);
222
223end:
83509119 224 BT_PUT(env_value_string_obj);
3487c9f3
JG
225 return ret;
226}
227
7f800dc7
PP
228int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace,
229 const char *name, int64_t value)
3487c9f3 230{
3487c9f3 231 int ret = 0;
dac5c838 232 struct bt_value *env_value_integer_obj = NULL;
3487c9f3
JG
233
234 if (!trace || !name) {
235 ret = -1;
7f800dc7 236 goto end;
3487c9f3
JG
237 }
238
a0d12916
JG
239 if (trace->frozen) {
240 /*
241 * New environment fields may be added to a frozen trace,
242 * but existing fields may not be changed.
243 */
dac5c838 244 struct bt_value *attribute =
a0d12916
JG
245 bt_ctf_attributes_get_field_value_by_name(
246 trace->environment, name);
247
248 if (attribute) {
83509119 249 BT_PUT(attribute);
a0d12916
JG
250 ret = -1;
251 goto end;
252 }
253 }
254
dac5c838 255 env_value_integer_obj = bt_value_integer_create_init(value);
7f800dc7 256 if (!env_value_integer_obj) {
3487c9f3 257 ret = -1;
7f800dc7 258 goto end;
3487c9f3
JG
259 }
260
7f800dc7
PP
261 ret = bt_ctf_trace_set_environment_field(trace, name,
262 env_value_integer_obj);
a0d12916 263 if (trace->frozen) {
dac5c838 264 bt_value_freeze(env_value_integer_obj);
a0d12916 265 }
7f800dc7 266end:
83509119 267 BT_PUT(env_value_integer_obj);
bc37ae52
JG
268 return ret;
269}
270
e6fa2160
JG
271int bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace)
272{
273 int ret = 0;
274
275 if (!trace) {
276 ret = -1;
277 goto end;
278 }
279
7f800dc7 280 ret = bt_ctf_attributes_get_count(trace->environment);
e6fa2160 281
e6fa2160 282end:
7f800dc7 283 return ret;
e6fa2160
JG
284}
285
286const char *
287bt_ctf_trace_get_environment_field_name(struct bt_ctf_trace *trace,
288 int index)
289{
e6fa2160
JG
290 const char *ret = NULL;
291
7f800dc7 292 if (!trace) {
e6fa2160
JG
293 goto end;
294 }
295
7f800dc7
PP
296 ret = bt_ctf_attributes_get_field_name(trace->environment, index);
297
e6fa2160
JG
298end:
299 return ret;
300}
301
dac5c838 302struct bt_value *bt_ctf_trace_get_environment_field_value(
7f800dc7 303 struct bt_ctf_trace *trace, int index)
e6fa2160 304{
dac5c838 305 struct bt_value *ret = NULL;
e6fa2160 306
7f800dc7 307 if (!trace) {
e6fa2160
JG
308 goto end;
309 }
310
7f800dc7
PP
311 ret = bt_ctf_attributes_get_field_value(trace->environment, index);
312
e6fa2160
JG
313end:
314 return ret;
315}
316
dac5c838 317struct bt_value *bt_ctf_trace_get_environment_field_value_by_name(
7f800dc7 318 struct bt_ctf_trace *trace, const char *name)
e6fa2160 319{
dac5c838 320 struct bt_value *ret = NULL;
e6fa2160 321
7f800dc7 322 if (!trace || !name) {
e6fa2160
JG
323 goto end;
324 }
325
7f800dc7
PP
326 ret = bt_ctf_attributes_get_field_value_by_name(trace->environment,
327 name);
328
e6fa2160
JG
329end:
330 return ret;
331}
332
bc37ae52
JG
333int bt_ctf_trace_add_clock(struct bt_ctf_trace *trace,
334 struct bt_ctf_clock *clock)
335{
336 int ret = 0;
337 struct search_query query = { .value = clock, .found = 0 };
338
339 if (!trace || !clock) {
340 ret = -1;
341 goto end;
342 }
343
344 /* Check for duplicate clocks */
345 g_ptr_array_foreach(trace->clocks, value_exists, &query);
346 if (query.found) {
347 ret = -1;
348 goto end;
349 }
350
83509119 351 bt_get(clock);
bc37ae52 352 g_ptr_array_add(trace->clocks, clock);
6474e016 353
cfeb617e
PP
354 if (!trace->is_created_by_writer) {
355 /*
356 * Non-writer mode trace: disable clock value functions
357 * because clock values are per-stream in that
358 * situation.
359 */
360 clock->has_value = 0;
361 }
362
6474e016
PP
363 if (trace->frozen) {
364 bt_ctf_clock_freeze(clock);
365 }
bc37ae52
JG
366end:
367 return ret;
368}
369
884cd6c3
JG
370int bt_ctf_trace_get_clock_count(struct bt_ctf_trace *trace)
371{
372 int ret = -1;
373
374 if (!trace) {
375 goto end;
376 }
377
378 ret = trace->clocks->len;
379end:
380 return ret;
381}
382
383struct bt_ctf_clock *bt_ctf_trace_get_clock(struct bt_ctf_trace *trace,
384 int index)
385{
386 struct bt_ctf_clock *clock = NULL;
387
388 if (!trace || index < 0 || index >= trace->clocks->len) {
389 goto end;
390 }
391
392 clock = g_ptr_array_index(trace->clocks, index);
83509119 393 bt_get(clock);
884cd6c3
JG
394end:
395 return clock;
396}
397
ef0c4a15
JG
398int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
399 struct bt_ctf_stream_class *stream_class)
400{
401 int ret, i;
402 int64_t stream_id;
09840de5
PP
403 struct bt_ctf_validation_output trace_sc_validation_output = { 0 };
404 struct bt_ctf_validation_output *ec_validation_outputs = NULL;
405 const enum bt_ctf_validation_flag trace_sc_validation_flags =
406 BT_CTF_VALIDATION_FLAG_TRACE |
407 BT_CTF_VALIDATION_FLAG_STREAM;
408 const enum bt_ctf_validation_flag ec_validation_flags =
409 BT_CTF_VALIDATION_FLAG_EVENT;
410 struct bt_ctf_field_type *packet_header_type = NULL;
411 struct bt_ctf_field_type *packet_context_type = NULL;
412 struct bt_ctf_field_type *event_header_type = NULL;
413 struct bt_ctf_field_type *stream_event_ctx_type = NULL;
414 int event_class_count;
fe13b5c0 415 struct bt_ctf_clock *clock_to_add_to_trace = NULL;
ef0c4a15
JG
416
417 if (!trace || !stream_class) {
418 ret = -1;
419 goto end;
420 }
421
09840de5
PP
422 event_class_count =
423 bt_ctf_stream_class_get_event_class_count(stream_class);
424 assert(event_class_count >= 0);
425
426 /* Check for duplicate stream classes */
ef0c4a15
JG
427 for (i = 0; i < trace->stream_classes->len; i++) {
428 if (trace->stream_classes->pdata[i] == stream_class) {
09840de5 429 /* Stream class already registered to the trace */
ef0c4a15
JG
430 ret = -1;
431 goto end;
432 }
433 }
434
fe13b5c0
PP
435 /*
436 * If the stream class has a clock, register this clock to this
437 * trace if not already done.
438 */
439 if (stream_class->clock) {
440 const char *clock_name =
441 bt_ctf_clock_get_name(stream_class->clock);
442 struct bt_ctf_clock *trace_clock;
443
444 assert(clock_name);
445 trace_clock = bt_ctf_trace_get_clock_by_name(trace, clock_name);
446 bt_put(trace_clock);
447 if (trace_clock) {
448 if (trace_clock != stream_class->clock) {
449 /*
450 * Error: two different clocks in the
451 * trace would share the same name.
452 */
453 ret = -1;
454 goto end;
455 }
456 } else {
457 clock_to_add_to_trace = bt_get(stream_class->clock);
458 }
459 }
460
09840de5
PP
461 /*
462 * We're about to freeze both the trace and the stream class.
463 * Also, each event class contained in this stream class are
464 * already frozen.
465 *
466 * This trace, this stream class, and all its event classes
467 * should be valid at this point.
468 *
469 * Validate trace and stream class first, then each event
470 * class of this stream class can be validated individually.
471 */
472 packet_header_type =
473 bt_ctf_trace_get_packet_header_type(trace);
474 packet_context_type =
475 bt_ctf_stream_class_get_packet_context_type(stream_class);
476 event_header_type =
477 bt_ctf_stream_class_get_event_header_type(stream_class);
478 stream_event_ctx_type =
479 bt_ctf_stream_class_get_event_context_type(stream_class);
480 ret = bt_ctf_validate_class_types(trace->environment,
481 packet_header_type, packet_context_type, event_header_type,
482 stream_event_ctx_type, NULL, NULL, trace->valid,
483 stream_class->valid, 1, &trace_sc_validation_output,
484 trace_sc_validation_flags);
485 BT_PUT(packet_header_type);
486 BT_PUT(packet_context_type);
487 BT_PUT(event_header_type);
488 BT_PUT(stream_event_ctx_type);
489
2bd7f864 490 if (ret) {
09840de5
PP
491 /*
492 * This means something went wrong during the validation
493 * process, not that the objects are invalid.
494 */
2bd7f864
JG
495 goto end;
496 }
497
09840de5
PP
498 if ((trace_sc_validation_output.valid_flags &
499 trace_sc_validation_flags) !=
500 trace_sc_validation_flags) {
501 /* Invalid trace/stream class */
502 ret = -1;
503 goto end;
504 }
505
506 if (event_class_count > 0) {
507 ec_validation_outputs = g_new0(struct bt_ctf_validation_output,
508 event_class_count);
509 if (!ec_validation_outputs) {
510 ret = -1;
511 goto end;
512 }
513 }
514
515 /* Validate each event class individually */
6474e016 516 for (i = 0; i < event_class_count; i++) {
09840de5
PP
517 struct bt_ctf_event_class *event_class =
518 bt_ctf_stream_class_get_event_class(stream_class, i);
519 struct bt_ctf_field_type *event_context_type = NULL;
520 struct bt_ctf_field_type *event_payload_type = NULL;
521
522 event_context_type =
523 bt_ctf_event_class_get_context_type(event_class);
524 event_payload_type =
525 bt_ctf_event_class_get_payload_type(event_class);
526
527 /*
528 * It is important to use the field types returned by
529 * the previous trace and stream class validation here
530 * because copies could have been made.
531 */
532 ret = bt_ctf_validate_class_types(trace->environment,
533 trace_sc_validation_output.packet_header_type,
534 trace_sc_validation_output.packet_context_type,
535 trace_sc_validation_output.event_header_type,
536 trace_sc_validation_output.stream_event_ctx_type,
537 event_context_type, event_payload_type,
538 1, 1, event_class->valid, &ec_validation_outputs[i],
539 ec_validation_flags);
540 BT_PUT(event_context_type);
541 BT_PUT(event_payload_type);
542 BT_PUT(event_class);
543
544 if (ret) {
545 goto end;
546 }
547
548 if ((ec_validation_outputs[i].valid_flags &
549 ec_validation_flags) != ec_validation_flags) {
550 /* Invalid event class */
551 ret = -1;
552 goto end;
553 }
554 }
555
ef0c4a15
JG
556 stream_id = bt_ctf_stream_class_get_id(stream_class);
557 if (stream_id < 0) {
558 stream_id = trace->next_stream_id++;
559
560 /* Try to assign a new stream id */
561 for (i = 0; i < trace->stream_classes->len; i++) {
562 if (stream_id == bt_ctf_stream_class_get_id(
563 trace->stream_classes->pdata[i])) {
564 /* Duplicate stream id found */
565 ret = -1;
566 goto end;
567 }
568 }
569
0ddffae5
JG
570 if (bt_ctf_stream_class_set_id_no_check(stream_class,
571 stream_id)) {
ef0c4a15
JG
572 /* TODO Should retry with a different stream id */
573 ret = -1;
574 goto end;
575 }
576 }
577
e6a8e8e4 578 bt_object_set_parent(stream_class, trace);
ef0c4a15
JG
579 g_ptr_array_add(trace->stream_classes, stream_class);
580
581 /*
09840de5
PP
582 * At this point we know that the function will be successful.
583 * Therefore we can replace the trace and stream class field
584 * types with what's in their validation output structure and
585 * mark them as valid. We can also replace the field types of
586 * all the event classes of the stream class and mark them as
587 * valid.
588 */
589 bt_ctf_validation_replace_types(trace, stream_class, NULL,
590 &trace_sc_validation_output, trace_sc_validation_flags);
591 trace->valid = 1;
592 stream_class->valid = 1;
593
594 /*
595 * Put what was not moved in bt_ctf_validation_replace_types().
596 */
597 bt_ctf_validation_output_put_types(&trace_sc_validation_output);
598
6474e016 599 for (i = 0; i < event_class_count; i++) {
09840de5
PP
600 struct bt_ctf_event_class *event_class =
601 bt_ctf_stream_class_get_event_class(stream_class, i);
602
603 bt_ctf_validation_replace_types(NULL, NULL, event_class,
604 &ec_validation_outputs[i], ec_validation_flags);
605 event_class->valid = 1;
606 BT_PUT(event_class);
607
608 /*
609 * Put what was not moved in
610 * bt_ctf_validation_replace_types().
611 */
612 bt_ctf_validation_output_put_types(&ec_validation_outputs[i]);
613 }
614
615 /*
ef0c4a15
JG
616 * All field type byte orders set as "native" byte ordering can now be
617 * safely set to trace's own endianness, including the stream class'.
618 */
619 bt_ctf_field_type_set_native_byte_order(trace->packet_header_type,
620 trace->byte_order);
445c3471 621 bt_ctf_stream_class_set_byte_order(stream_class, trace->byte_order);
ef0c4a15 622
fe13b5c0
PP
623 /* Add stream class's clock if it exists */
624 if (clock_to_add_to_trace) {
625 int add_clock_ret =
626 bt_ctf_trace_add_clock(trace, clock_to_add_to_trace);
627 assert(add_clock_ret == 0);
628 }
629
09840de5
PP
630 /*
631 * Freeze the trace and the stream class.
632 */
f67f3a6e 633 bt_ctf_stream_class_freeze(stream_class);
09840de5
PP
634 bt_ctf_trace_freeze(trace);
635
ef0c4a15 636end:
d3814b54 637 if (ret) {
e6a8e8e4 638 bt_object_set_parent(stream_class, NULL);
09840de5
PP
639
640 if (ec_validation_outputs) {
6474e016 641 for (i = 0; i < event_class_count; i++) {
09840de5
PP
642 bt_ctf_validation_output_put_types(
643 &ec_validation_outputs[i]);
644 }
645 }
d3814b54 646 }
09840de5
PP
647
648 g_free(ec_validation_outputs);
649 bt_ctf_validation_output_put_types(&trace_sc_validation_output);
fe13b5c0 650 BT_PUT(clock_to_add_to_trace);
09840de5
PP
651 assert(!packet_header_type);
652 assert(!packet_context_type);
653 assert(!event_header_type);
654 assert(!stream_event_ctx_type);
655
ef0c4a15
JG
656 return ret;
657}
658
659int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace)
660{
661 int ret;
662
663 if (!trace) {
664 ret = -1;
665 goto end;
666 }
667
668 ret = trace->stream_classes->len;
669end:
670 return ret;
671}
672
673struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class(
674 struct bt_ctf_trace *trace, int index)
675{
676 struct bt_ctf_stream_class *stream_class = NULL;
677
678 if (!trace || index < 0 || index >= trace->stream_classes->len) {
679 goto end;
680 }
681
682 stream_class = g_ptr_array_index(trace->stream_classes, index);
83509119 683 bt_get(stream_class);
ef0c4a15
JG
684end:
685 return stream_class;
686}
687
4841ccc1
PP
688struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id(
689 struct bt_ctf_trace *trace, uint32_t id)
690{
691 int i;
692 struct bt_ctf_stream_class *stream_class = NULL;
693
694 if (!trace) {
695 goto end;
696 }
697
6474e016 698 for (i = 0; i < trace->stream_classes->len; i++) {
4841ccc1
PP
699 struct bt_ctf_stream_class *stream_class_candidate;
700
701 stream_class_candidate =
702 g_ptr_array_index(trace->stream_classes, i);
703
704 if (bt_ctf_stream_class_get_id(stream_class_candidate) ==
705 (int64_t) id) {
706 stream_class = stream_class_candidate;
83509119 707 bt_get(stream_class);
4841ccc1
PP
708 goto end;
709 }
710 }
711
712end:
713 return stream_class;
714}
715
7e4347a3
PP
716struct bt_ctf_clock *bt_ctf_trace_get_clock_by_name(
717 struct bt_ctf_trace *trace, const char *name)
718{
719 size_t i;
720 struct bt_ctf_clock *clock = NULL;
721
722 if (!trace || !name) {
723 goto end;
724 }
725
6474e016 726 for (i = 0; i < trace->clocks->len; i++) {
7e4347a3
PP
727 struct bt_ctf_clock *cur_clk =
728 g_ptr_array_index(trace->clocks, i);
729 const char *cur_clk_name = bt_ctf_clock_get_name(cur_clk);
730
731 if (!cur_clk_name) {
732 goto end;
733 }
734
735 if (!strcmp(cur_clk_name, name)) {
736 clock = cur_clk;
83509119 737 bt_get(clock);
7e4347a3
PP
738 goto end;
739 }
740 }
741
742end:
743 return clock;
744}
745
bc37ae52
JG
746BT_HIDDEN
747const char *get_byte_order_string(int byte_order)
748{
749 const char *string;
750
751 switch (byte_order) {
752 case LITTLE_ENDIAN:
753 string = "le";
754 break;
755 case BIG_ENDIAN:
756 string = "be";
757 break;
758 default:
759 string = "unknown";
760 break;
761 }
762
763 return string;
764}
765
766static
767int append_trace_metadata(struct bt_ctf_trace *trace,
768 struct metadata_context *context)
769{
770 unsigned char *uuid = trace->uuid;
771 int ret;
772
773 g_string_append(context->string, "trace {\n");
774
775 g_string_append(context->string, "\tmajor = 1;\n");
776 g_string_append(context->string, "\tminor = 8;\n");
777
778 g_string_append_printf(context->string,
779 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
780 uuid[0], uuid[1], uuid[2], uuid[3],
781 uuid[4], uuid[5], uuid[6], uuid[7],
782 uuid[8], uuid[9], uuid[10], uuid[11],
783 uuid[12], uuid[13], uuid[14], uuid[15]);
784 g_string_append_printf(context->string, "\tbyte_order = %s;\n",
785 get_byte_order_string(trace->byte_order));
786
787 g_string_append(context->string, "\tpacket.header := ");
788 context->current_indentation_level++;
789 g_string_assign(context->field_name, "");
d246b111 790 ret = bt_ctf_field_type_serialize(trace->packet_header_type,
bc37ae52
JG
791 context);
792 if (ret) {
793 goto end;
794 }
795 context->current_indentation_level--;
796
797 g_string_append(context->string, ";\n};\n\n");
798end:
799 return ret;
800}
801
bc37ae52
JG
802static
803void append_env_metadata(struct bt_ctf_trace *trace,
804 struct metadata_context *context)
805{
7f800dc7
PP
806 int i;
807 int env_size;
808
809 env_size = bt_ctf_attributes_get_count(trace->environment);
810
811 if (env_size <= 0) {
bc37ae52
JG
812 return;
813 }
814
815 g_string_append(context->string, "env {\n");
7f800dc7 816
6474e016 817 for (i = 0; i < env_size; i++) {
dac5c838 818 struct bt_value *env_field_value_obj = NULL;
7f800dc7 819 const char *entry_name;
7f800dc7
PP
820
821 entry_name = bt_ctf_attributes_get_field_name(
822 trace->environment, i);
823 env_field_value_obj = bt_ctf_attributes_get_field_value(
824 trace->environment, i);
825
826 if (!entry_name || !env_field_value_obj) {
827 goto loop_next;
828 }
829
dac5c838
PP
830 switch (bt_value_get_type(env_field_value_obj)) {
831 case BT_VALUE_TYPE_INTEGER:
b8248cc0
PP
832 {
833 int ret;
834 int64_t int_value;
835
dac5c838 836 ret = bt_value_integer_get(env_field_value_obj,
7f800dc7
PP
837 &int_value);
838
839 if (ret) {
840 goto loop_next;
841 }
842
843 g_string_append_printf(context->string,
844 "\t%s = %" PRId64 ";\n", entry_name,
845 int_value);
846 break;
b8248cc0 847 }
dac5c838 848 case BT_VALUE_TYPE_STRING:
7f800dc7
PP
849 {
850 int ret;
851 const char *str_value;
852 char *escaped_str = NULL;
853
dac5c838 854 ret = bt_value_string_get(env_field_value_obj,
7f800dc7
PP
855 &str_value);
856
857 if (ret) {
858 goto loop_next;
859 }
860
861 escaped_str = g_strescape(str_value, NULL);
862
863 if (!escaped_str) {
864 goto loop_next;
865 }
866
867 g_string_append_printf(context->string,
868 "\t%s = \"%s\";\n", entry_name, escaped_str);
869 free(escaped_str);
870 break;
871 }
872
873 default:
874 goto loop_next;
875 }
876
877loop_next:
83509119 878 BT_PUT(env_field_value_obj);
7f800dc7
PP
879 }
880
bc37ae52
JG
881 g_string_append(context->string, "};\n\n");
882}
883
884char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace)
885{
886 char *metadata = NULL;
887 struct metadata_context *context = NULL;
888 int err = 0;
889 size_t i;
890
891 if (!trace) {
892 goto end;
893 }
894
895 context = g_new0(struct metadata_context, 1);
896 if (!context) {
897 goto end;
898 }
899
900 context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE);
901 context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE);
902 g_string_append(context->string, "/* CTF 1.8 */\n\n");
903 if (append_trace_metadata(trace, context)) {
904 goto error;
905 }
906 append_env_metadata(trace, context);
907 g_ptr_array_foreach(trace->clocks,
908 (GFunc)bt_ctf_clock_serialize, context);
909
910 for (i = 0; i < trace->stream_classes->len; i++) {
911 err = bt_ctf_stream_class_serialize(
912 trace->stream_classes->pdata[i], context);
913 if (err) {
914 goto error;
915 }
916 }
917
918 metadata = context->string->str;
919error:
920 g_string_free(context->string, err ? TRUE : FALSE);
921 g_string_free(context->field_name, TRUE);
922 g_free(context);
923end:
924 return metadata;
925}
926
4ed90fb3
JG
927enum bt_ctf_byte_order bt_ctf_trace_get_byte_order(struct bt_ctf_trace *trace)
928{
929 enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN;
930
931 if (!trace) {
932 goto end;
933 }
934
935 switch (trace->byte_order) {
936 case BIG_ENDIAN:
937 ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN;
938 break;
939 case LITTLE_ENDIAN:
940 ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN;
941 break;
942 default:
943 break;
944 }
945end:
946 return ret;
947}
948
bc37ae52
JG
949int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace,
950 enum bt_ctf_byte_order byte_order)
951{
952 int ret = 0;
953 int internal_byte_order;
954
955 if (!trace || trace->frozen) {
956 ret = -1;
957 goto end;
958 }
959
960 switch (byte_order) {
961 case BT_CTF_BYTE_ORDER_NATIVE:
c35a1669
JG
962 /*
963 * This doesn't make sense since the CTF specification defines
964 * the "native" byte order as "the byte order described in the
965 * trace description". However, this behavior had been
966 * implemented as part of v1.2 and is kept to maintain
967 * compatibility.
968 *
969 * This may be changed on a major version bump only.
970 */
971 internal_byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN) ?
bc37ae52
JG
972 LITTLE_ENDIAN : BIG_ENDIAN;
973 break;
974 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
975 internal_byte_order = LITTLE_ENDIAN;
976 break;
977 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
978 case BT_CTF_BYTE_ORDER_NETWORK:
979 internal_byte_order = BIG_ENDIAN;
980 break;
981 default:
982 ret = -1;
983 goto end;
984 }
985
986 trace->byte_order = internal_byte_order;
bc37ae52
JG
987end:
988 return ret;
989}
990
d246b111
JG
991struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type(
992 struct bt_ctf_trace *trace)
993{
994 struct bt_ctf_field_type *field_type = NULL;
995
996 if (!trace) {
997 goto end;
998 }
999
83509119 1000 bt_get(trace->packet_header_type);
d246b111
JG
1001 field_type = trace->packet_header_type;
1002end:
1003 return field_type;
1004}
1005
1006int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace,
1007 struct bt_ctf_field_type *packet_header_type)
1008{
1009 int ret = 0;
1010
1011 if (!trace || !packet_header_type || trace->frozen) {
1012 ret = -1;
1013 goto end;
1014 }
1015
1016 /* packet_header_type must be a structure */
1017 if (bt_ctf_field_type_get_type_id(packet_header_type) !=
9a19a512 1018 BT_CTF_TYPE_ID_STRUCT) {
d246b111
JG
1019 ret = -1;
1020 goto end;
1021 }
1022
83509119
JG
1023 bt_get(packet_header_type);
1024 bt_put(trace->packet_header_type);
d246b111
JG
1025 trace->packet_header_type = packet_header_type;
1026end:
1027 return ret;
1028}
1029
bc37ae52
JG
1030BT_HIDDEN
1031struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
1032{
1033 unsigned int alignment, size;
8bdcb82e 1034 struct bt_ctf_field_type *field_type = NULL;
bc37ae52
JG
1035
1036 if (alias >= NR_FIELD_TYPE_ALIAS) {
8bdcb82e 1037 goto end;
bc37ae52
JG
1038 }
1039
1040 alignment = field_type_aliases_alignments[alias];
1041 size = field_type_aliases_sizes[alias];
1042 field_type = bt_ctf_field_type_integer_create(size);
1043 bt_ctf_field_type_set_alignment(field_type, alignment);
8bdcb82e 1044end:
bc37ae52
JG
1045 return field_type;
1046}
1047
f67f3a6e 1048static
09840de5 1049void bt_ctf_trace_freeze(struct bt_ctf_trace *trace)
f67f3a6e 1050{
6474e016
PP
1051 int i;
1052
09840de5 1053 bt_ctf_field_type_freeze(trace->packet_header_type);
f67f3a6e 1054 bt_ctf_attributes_freeze(trace->environment);
6474e016
PP
1055
1056 for (i = 0; i < trace->clocks->len; i++) {
1057 struct bt_ctf_clock *clock =
1058 g_ptr_array_index(trace->clocks, i);
1059
1060 bt_ctf_clock_freeze(clock);
1061 }
1062
f67f3a6e
JG
1063 trace->frozen = 1;
1064}
1065
bc37ae52
JG
1066static
1067int init_trace_packet_header(struct bt_ctf_trace *trace)
1068{
bc37ae52 1069 int ret = 0;
d246b111 1070 struct bt_ctf_field *magic = NULL, *uuid_array = NULL;
bc37ae52
JG
1071 struct bt_ctf_field_type *_uint32_t =
1072 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
1073 struct bt_ctf_field_type *_uint8_t =
1074 get_field_type(FIELD_TYPE_ALIAS_UINT8_T);
1075 struct bt_ctf_field_type *trace_packet_header_type =
1076 bt_ctf_field_type_structure_create();
1077 struct bt_ctf_field_type *uuid_array_type =
1078 bt_ctf_field_type_array_create(_uint8_t, 16);
1079
1080 if (!trace_packet_header_type || !uuid_array_type) {
1081 ret = -1;
1082 goto end;
1083 }
1084
bc37ae52
JG
1085 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
1086 _uint32_t, "magic");
1087 if (ret) {
1088 goto end;
1089 }
1090
1091 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
1092 uuid_array_type, "uuid");
1093 if (ret) {
1094 goto end;
1095 }
1096
1097 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
1098 _uint32_t, "stream_id");
1099 if (ret) {
1100 goto end;
1101 }
1102
662e778c
JG
1103 ret = bt_ctf_trace_set_packet_header_type(trace,
1104 trace_packet_header_type);
1105 if (ret) {
1106 goto end;
1107 }
bc37ae52 1108end:
83509119
JG
1109 bt_put(uuid_array_type);
1110 bt_put(_uint32_t);
1111 bt_put(_uint8_t);
1112 bt_put(magic);
1113 bt_put(uuid_array);
1114 bt_put(trace_packet_header_type);
bc37ae52
JG
1115 return ret;
1116}
This page took 0.076763 seconds and 4 git commands to generate.