Do not use `bool` type; use new `bt_bool` instead
[babeltrace.git] / lib / ctf-ir / trace.c
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-class-internal.h>
31 #include <babeltrace/ctf-ir/stream-internal.h>
32 #include <babeltrace/ctf-ir/stream-class-internal.h>
33 #include <babeltrace/ctf-ir/event-internal.h>
34 #include <babeltrace/ctf-ir/event-class.h>
35 #include <babeltrace/ctf-ir/event-class-internal.h>
36 #include <babeltrace/ctf-writer/functor-internal.h>
37 #include <babeltrace/ctf-writer/clock-internal.h>
38 #include <babeltrace/ctf-ir/field-types-internal.h>
39 #include <babeltrace/ctf-ir/attributes-internal.h>
40 #include <babeltrace/ctf-ir/validation-internal.h>
41 #include <babeltrace/ctf-ir/visitor-internal.h>
42 #include <babeltrace/ctf-ir/utils.h>
43 #include <babeltrace/compiler-internal.h>
44 #include <babeltrace/values.h>
45 #include <babeltrace/ref.h>
46 #include <babeltrace/types.h>
47 #include <babeltrace/endian-internal.h>
48 #include <inttypes.h>
49 #include <stdint.h>
50 #include <string.h>
51
52 #define DEFAULT_IDENTIFIER_SIZE 128
53 #define DEFAULT_METADATA_STRING_SIZE 4096
54
55 struct listener_wrapper {
56 bt_ctf_listener_cb listener;
57 void *data;
58 };
59
60 static
61 void bt_ctf_trace_destroy(struct bt_object *obj);
62 static
63 void bt_ctf_trace_freeze(struct bt_ctf_trace *trace);
64
65 static
66 const unsigned int field_type_aliases_alignments[] = {
67 [FIELD_TYPE_ALIAS_UINT5_T] = 1,
68 [FIELD_TYPE_ALIAS_UINT8_T ... FIELD_TYPE_ALIAS_UINT16_T] = 8,
69 [FIELD_TYPE_ALIAS_UINT27_T] = 1,
70 [FIELD_TYPE_ALIAS_UINT32_T ... FIELD_TYPE_ALIAS_UINT64_T] = 8,
71 };
72
73 static
74 const unsigned int field_type_aliases_sizes[] = {
75 [FIELD_TYPE_ALIAS_UINT5_T] = 5,
76 [FIELD_TYPE_ALIAS_UINT8_T] = 8,
77 [FIELD_TYPE_ALIAS_UINT16_T] = 16,
78 [FIELD_TYPE_ALIAS_UINT27_T] = 27,
79 [FIELD_TYPE_ALIAS_UINT32_T] = 32,
80 [FIELD_TYPE_ALIAS_UINT64_T] = 64,
81 };
82
83 struct bt_ctf_trace *bt_ctf_trace_create(void)
84 {
85 struct bt_ctf_trace *trace = NULL;
86 struct bt_ctf_field_type *packet_header_type = NULL;
87
88 trace = g_new0(struct bt_ctf_trace, 1);
89 if (!trace) {
90 goto error;
91 }
92
93 trace->native_byte_order = BT_CTF_BYTE_ORDER_NATIVE;
94 bt_object_init(trace, bt_ctf_trace_destroy);
95 trace->clocks = g_ptr_array_new_with_free_func(
96 (GDestroyNotify) bt_put);
97 trace->streams = g_ptr_array_new_with_free_func(
98 (GDestroyNotify) bt_object_release);
99 trace->stream_classes = g_ptr_array_new_with_free_func(
100 (GDestroyNotify) bt_object_release);
101 if (!trace->clocks || !trace->stream_classes || !trace->streams) {
102 goto error;
103 }
104
105 packet_header_type = bt_ctf_field_type_structure_create();
106 if (!packet_header_type) {
107 goto error;
108 }
109
110 BT_MOVE(trace->packet_header_type, packet_header_type);
111
112 /* Create the environment array object */
113 trace->environment = bt_ctf_attributes_create();
114 if (!trace->environment) {
115 goto error;
116 }
117
118 trace->listeners = g_ptr_array_new_with_free_func(
119 (GDestroyNotify) g_free);
120 if (!trace->listeners) {
121 goto error;
122 }
123
124 return trace;
125
126 error:
127 BT_PUT(trace);
128 bt_put(packet_header_type);
129 return trace;
130 }
131
132 const char *bt_ctf_trace_get_name(struct bt_ctf_trace *trace)
133 {
134 const char *name = NULL;
135
136 if (!trace || !trace->name) {
137 goto end;
138 }
139
140 name = trace->name->str;
141 end:
142 return name;
143 }
144
145 int bt_ctf_trace_set_name(struct bt_ctf_trace *trace, const char *name)
146 {
147 int ret = 0;
148
149 if (!trace || !name || trace->frozen) {
150 ret = -1;
151 goto end;
152 }
153
154 trace->name = trace->name ? g_string_assign(trace->name, name) :
155 g_string_new(name);
156 if (!trace->name) {
157 ret = -1;
158 goto end;
159 }
160 end:
161 return ret;
162 }
163
164 const unsigned char *bt_ctf_trace_get_uuid(struct bt_ctf_trace *trace)
165 {
166 return trace && trace->uuid_set ? trace->uuid : NULL;
167 }
168
169 int bt_ctf_trace_set_uuid(struct bt_ctf_trace *trace, const unsigned char *uuid)
170 {
171 int ret = 0;
172
173 if (!trace || !uuid || trace->frozen) {
174 ret = -1;
175 goto end;
176 }
177
178 memcpy(trace->uuid, uuid, sizeof(uuid_t));
179 trace->uuid_set = BT_TRUE;
180
181 end:
182 return ret;
183 }
184
185 void bt_ctf_trace_destroy(struct bt_object *obj)
186 {
187 struct bt_ctf_trace *trace;
188
189 trace = container_of(obj, struct bt_ctf_trace, base);
190 if (trace->environment) {
191 bt_ctf_attributes_destroy(trace->environment);
192 }
193
194 if (trace->name) {
195 g_string_free(trace->name, TRUE);
196 }
197
198 if (trace->clocks) {
199 g_ptr_array_free(trace->clocks, TRUE);
200 }
201
202 if (trace->streams) {
203 g_ptr_array_free(trace->streams, TRUE);
204 }
205
206 if (trace->stream_classes) {
207 g_ptr_array_free(trace->stream_classes, TRUE);
208 }
209
210 if (trace->listeners) {
211 g_ptr_array_free(trace->listeners, TRUE);
212 }
213
214 bt_put(trace->packet_header_type);
215 g_free(trace);
216 }
217
218 int bt_ctf_trace_set_environment_field(struct bt_ctf_trace *trace,
219 const char *name, struct bt_value *value)
220 {
221 int ret = 0;
222
223 if (!trace || !name || !value ||
224 bt_ctf_validate_identifier(name) ||
225 !(bt_value_is_integer(value) || bt_value_is_string(value))) {
226 ret = -1;
227 goto end;
228 }
229
230 if (strchr(name, ' ')) {
231 ret = -1;
232 goto end;
233 }
234
235 if (trace->frozen) {
236 /*
237 * New environment fields may be added to a frozen trace,
238 * but existing fields may not be changed.
239 *
240 * The object passed is frozen like all other attributes.
241 */
242 struct bt_value *attribute =
243 bt_ctf_attributes_get_field_value_by_name(
244 trace->environment, name);
245
246 if (attribute) {
247 BT_PUT(attribute);
248 ret = -1;
249 goto end;
250 }
251
252 bt_value_freeze(value);
253 }
254
255 ret = bt_ctf_attributes_set_field_value(trace->environment, name,
256 value);
257
258 end:
259 return ret;
260 }
261
262 int bt_ctf_trace_set_environment_field_string(struct bt_ctf_trace *trace,
263 const char *name, const char *value)
264 {
265 int ret = 0;
266 struct bt_value *env_value_string_obj = NULL;
267
268 if (!trace || !name || !value) {
269 ret = -1;
270 goto end;
271 }
272
273 if (trace->frozen) {
274 /*
275 * New environment fields may be added to a frozen trace,
276 * but existing fields may not be changed.
277 */
278 struct bt_value *attribute =
279 bt_ctf_attributes_get_field_value_by_name(
280 trace->environment, name);
281
282 if (attribute) {
283 BT_PUT(attribute);
284 ret = -1;
285 goto end;
286 }
287 }
288
289 env_value_string_obj = bt_value_string_create_init(value);
290
291 if (!env_value_string_obj) {
292 ret = -1;
293 goto end;
294 }
295
296 if (trace->frozen) {
297 bt_value_freeze(env_value_string_obj);
298 }
299 ret = bt_ctf_trace_set_environment_field(trace, name,
300 env_value_string_obj);
301
302 end:
303 BT_PUT(env_value_string_obj);
304 return ret;
305 }
306
307 int bt_ctf_trace_set_environment_field_integer(struct bt_ctf_trace *trace,
308 const char *name, int64_t value)
309 {
310 int ret = 0;
311 struct bt_value *env_value_integer_obj = NULL;
312
313 if (!trace || !name) {
314 ret = -1;
315 goto end;
316 }
317
318 if (trace->frozen) {
319 /*
320 * New environment fields may be added to a frozen trace,
321 * but existing fields may not be changed.
322 */
323 struct bt_value *attribute =
324 bt_ctf_attributes_get_field_value_by_name(
325 trace->environment, name);
326
327 if (attribute) {
328 BT_PUT(attribute);
329 ret = -1;
330 goto end;
331 }
332 }
333
334 env_value_integer_obj = bt_value_integer_create_init(value);
335 if (!env_value_integer_obj) {
336 ret = -1;
337 goto end;
338 }
339
340 ret = bt_ctf_trace_set_environment_field(trace, name,
341 env_value_integer_obj);
342 if (trace->frozen) {
343 bt_value_freeze(env_value_integer_obj);
344 }
345 end:
346 BT_PUT(env_value_integer_obj);
347 return ret;
348 }
349
350 int64_t bt_ctf_trace_get_environment_field_count(struct bt_ctf_trace *trace)
351 {
352 int64_t ret = 0;
353
354 if (!trace) {
355 ret = (int64_t) -1;
356 goto end;
357 }
358
359 ret = bt_ctf_attributes_get_count(trace->environment);
360
361 end:
362 return ret;
363 }
364
365 const char *
366 bt_ctf_trace_get_environment_field_name_by_index(struct bt_ctf_trace *trace,
367 uint64_t index)
368 {
369 const char *ret = NULL;
370
371 if (!trace) {
372 goto end;
373 }
374
375 ret = bt_ctf_attributes_get_field_name(trace->environment, index);
376
377 end:
378 return ret;
379 }
380
381 struct bt_value *bt_ctf_trace_get_environment_field_value_by_index(
382 struct bt_ctf_trace *trace, uint64_t index)
383 {
384 struct bt_value *ret = NULL;
385
386 if (!trace) {
387 goto end;
388 }
389
390 ret = bt_ctf_attributes_get_field_value(trace->environment, index);
391
392 end:
393 return ret;
394 }
395
396 struct bt_value *bt_ctf_trace_get_environment_field_value_by_name(
397 struct bt_ctf_trace *trace, const char *name)
398 {
399 struct bt_value *ret = NULL;
400
401 if (!trace || !name) {
402 goto end;
403 }
404
405 ret = bt_ctf_attributes_get_field_value_by_name(trace->environment,
406 name);
407
408 end:
409 return ret;
410 }
411
412 int bt_ctf_trace_add_clock_class(struct bt_ctf_trace *trace,
413 struct bt_ctf_clock_class *clock_class)
414 {
415 int ret = 0;
416
417 if (!trace || trace->is_static ||
418 !bt_ctf_clock_class_is_valid(clock_class)) {
419 ret = -1;
420 goto end;
421 }
422
423 /* Check for duplicate clock classes */
424 if (bt_ctf_trace_has_clock_class(trace, clock_class)) {
425 ret = -1;
426 goto end;
427 }
428
429 bt_get(clock_class);
430 g_ptr_array_add(trace->clocks, clock_class);
431
432 if (trace->frozen) {
433 bt_ctf_clock_class_freeze(clock_class);
434 }
435 end:
436 return ret;
437 }
438
439 int64_t bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace)
440 {
441 int64_t ret = (int64_t) -1;
442
443 if (!trace) {
444 goto end;
445 }
446
447 ret = trace->clocks->len;
448 end:
449 return ret;
450 }
451
452 struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_index(
453 struct bt_ctf_trace *trace, uint64_t index)
454 {
455 struct bt_ctf_clock_class *clock_class = NULL;
456
457 if (!trace || index >= trace->clocks->len) {
458 goto end;
459 }
460
461 clock_class = g_ptr_array_index(trace->clocks, index);
462 bt_get(clock_class);
463 end:
464 return clock_class;
465 }
466
467 int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace,
468 struct bt_ctf_stream_class *stream_class)
469 {
470 int ret;
471 int64_t i;
472 int64_t stream_id;
473 struct bt_ctf_validation_output trace_sc_validation_output = { 0 };
474 struct bt_ctf_validation_output *ec_validation_outputs = NULL;
475 const enum bt_ctf_validation_flag trace_sc_validation_flags =
476 BT_CTF_VALIDATION_FLAG_TRACE |
477 BT_CTF_VALIDATION_FLAG_STREAM;
478 const enum bt_ctf_validation_flag ec_validation_flags =
479 BT_CTF_VALIDATION_FLAG_EVENT;
480 struct bt_ctf_field_type *packet_header_type = NULL;
481 struct bt_ctf_field_type *packet_context_type = NULL;
482 struct bt_ctf_field_type *event_header_type = NULL;
483 struct bt_ctf_field_type *stream_event_ctx_type = NULL;
484 int64_t event_class_count;
485 struct bt_ctf_trace *current_parent_trace = NULL;
486
487 if (!trace || !stream_class || trace->is_static) {
488 ret = -1;
489 goto end;
490 }
491
492 /*
493 * At the end of this function we freeze the trace, so its
494 * native byte order must NOT be BT_CTF_BYTE_ORDER_NATIVE.
495 */
496 if (trace->native_byte_order == BT_CTF_BYTE_ORDER_NATIVE) {
497 ret = -1;
498 goto end;
499 }
500
501 current_parent_trace = bt_ctf_stream_class_get_trace(stream_class);
502 if (current_parent_trace) {
503 /* Stream class is already associated to a trace, abort. */
504 ret = -1;
505 goto end;
506 }
507
508 event_class_count =
509 bt_ctf_stream_class_get_event_class_count(stream_class);
510 assert(event_class_count >= 0);
511
512 /* Check for duplicate stream classes */
513 for (i = 0; i < trace->stream_classes->len; i++) {
514 if (trace->stream_classes->pdata[i] == stream_class) {
515 /* Stream class already registered to the trace */
516 ret = -1;
517 goto end;
518 }
519 }
520
521 if (stream_class->clock) {
522 struct bt_ctf_clock_class *stream_clock_class =
523 stream_class->clock->clock_class;
524
525 if (trace->is_created_by_writer) {
526 /*
527 * Make sure this clock was also added to the
528 * trace (potentially through its CTF writer
529 * owner).
530 */
531 size_t i;
532
533 for (i = 0; i < trace->clocks->len; i++) {
534 if (trace->clocks->pdata[i] ==
535 stream_clock_class) {
536 /* Found! */
537 break;
538 }
539 }
540
541 if (i == trace->clocks->len) {
542 /* Not found */
543 ret = -1;
544 goto end;
545 }
546 } else {
547 /*
548 * This trace was NOT created by a CTF writer,
549 * thus do not allow the stream class to add to
550 * have a clock at all. Those are two
551 * independent APIs (non-writer and writer
552 * APIs), and isolating them simplifies things.
553 */
554 ret = -1;
555 goto end;
556 }
557 }
558
559 /*
560 * We're about to freeze both the trace and the stream class.
561 * Also, each event class contained in this stream class are
562 * already frozen.
563 *
564 * This trace, this stream class, and all its event classes
565 * should be valid at this point.
566 *
567 * Validate trace and stream class first, then each event
568 * class of this stream class can be validated individually.
569 */
570 packet_header_type =
571 bt_ctf_trace_get_packet_header_type(trace);
572 packet_context_type =
573 bt_ctf_stream_class_get_packet_context_type(stream_class);
574 event_header_type =
575 bt_ctf_stream_class_get_event_header_type(stream_class);
576 stream_event_ctx_type =
577 bt_ctf_stream_class_get_event_context_type(stream_class);
578 ret = bt_ctf_validate_class_types(trace->environment,
579 packet_header_type, packet_context_type, event_header_type,
580 stream_event_ctx_type, NULL, NULL, trace->valid,
581 stream_class->valid, 1, &trace_sc_validation_output,
582 trace_sc_validation_flags);
583 BT_PUT(packet_header_type);
584 BT_PUT(packet_context_type);
585 BT_PUT(event_header_type);
586 BT_PUT(stream_event_ctx_type);
587
588 if (ret) {
589 /*
590 * This means something went wrong during the validation
591 * process, not that the objects are invalid.
592 */
593 goto end;
594 }
595
596 if ((trace_sc_validation_output.valid_flags &
597 trace_sc_validation_flags) !=
598 trace_sc_validation_flags) {
599 /* Invalid trace/stream class */
600 ret = -1;
601 goto end;
602 }
603
604 if (event_class_count > 0) {
605 ec_validation_outputs = g_new0(struct bt_ctf_validation_output,
606 event_class_count);
607 if (!ec_validation_outputs) {
608 ret = -1;
609 goto end;
610 }
611 }
612
613 /* Validate each event class individually */
614 for (i = 0; i < event_class_count; i++) {
615 struct bt_ctf_event_class *event_class =
616 bt_ctf_stream_class_get_event_class_by_index(
617 stream_class, i);
618 struct bt_ctf_field_type *event_context_type = NULL;
619 struct bt_ctf_field_type *event_payload_type = NULL;
620
621 event_context_type =
622 bt_ctf_event_class_get_context_type(event_class);
623 event_payload_type =
624 bt_ctf_event_class_get_payload_type(event_class);
625
626 /*
627 * It is important to use the field types returned by
628 * the previous trace and stream class validation here
629 * because copies could have been made.
630 */
631 ret = bt_ctf_validate_class_types(trace->environment,
632 trace_sc_validation_output.packet_header_type,
633 trace_sc_validation_output.packet_context_type,
634 trace_sc_validation_output.event_header_type,
635 trace_sc_validation_output.stream_event_ctx_type,
636 event_context_type, event_payload_type,
637 1, 1, event_class->valid, &ec_validation_outputs[i],
638 ec_validation_flags);
639 BT_PUT(event_context_type);
640 BT_PUT(event_payload_type);
641 BT_PUT(event_class);
642
643 if (ret) {
644 goto end;
645 }
646
647 if ((ec_validation_outputs[i].valid_flags &
648 ec_validation_flags) != ec_validation_flags) {
649 /* Invalid event class */
650 ret = -1;
651 goto end;
652 }
653 }
654
655 stream_id = bt_ctf_stream_class_get_id(stream_class);
656 if (stream_id < 0) {
657 stream_id = trace->next_stream_id++;
658 if (stream_id < 0) {
659 ret = -1;
660 goto end;
661 }
662
663 /* Try to assign a new stream id */
664 for (i = 0; i < trace->stream_classes->len; i++) {
665 if (stream_id == bt_ctf_stream_class_get_id(
666 trace->stream_classes->pdata[i])) {
667 /* Duplicate stream id found */
668 ret = -1;
669 goto end;
670 }
671 }
672
673 if (bt_ctf_stream_class_set_id_no_check(stream_class,
674 stream_id)) {
675 /* TODO Should retry with a different stream id */
676 ret = -1;
677 goto end;
678 }
679 }
680
681 bt_object_set_parent(stream_class, trace);
682 g_ptr_array_add(trace->stream_classes, stream_class);
683
684 /*
685 * At this point we know that the function will be successful.
686 * Therefore we can replace the trace and stream class field
687 * types with what's in their validation output structure and
688 * mark them as valid. We can also replace the field types of
689 * all the event classes of the stream class and mark them as
690 * valid.
691 */
692 bt_ctf_validation_replace_types(trace, stream_class, NULL,
693 &trace_sc_validation_output, trace_sc_validation_flags);
694 trace->valid = 1;
695 stream_class->valid = 1;
696
697 /*
698 * Put what was not moved in bt_ctf_validation_replace_types().
699 */
700 bt_ctf_validation_output_put_types(&trace_sc_validation_output);
701
702 for (i = 0; i < event_class_count; i++) {
703 struct bt_ctf_event_class *event_class =
704 bt_ctf_stream_class_get_event_class_by_index(
705 stream_class, i);
706
707 bt_ctf_validation_replace_types(NULL, NULL, event_class,
708 &ec_validation_outputs[i], ec_validation_flags);
709 event_class->valid = 1;
710 BT_PUT(event_class);
711
712 /*
713 * Put what was not moved in
714 * bt_ctf_validation_replace_types().
715 */
716 bt_ctf_validation_output_put_types(&ec_validation_outputs[i]);
717 }
718
719 /*
720 * Freeze the trace and the stream class.
721 */
722 bt_ctf_stream_class_freeze(stream_class);
723 bt_ctf_trace_freeze(trace);
724
725 /* Notifiy listeners of the trace's schema modification. */
726 bt_ctf_stream_class_visit(stream_class,
727 bt_ctf_trace_object_modification, trace);
728 end:
729 if (ret) {
730 bt_object_set_parent(stream_class, NULL);
731
732 if (ec_validation_outputs) {
733 for (i = 0; i < event_class_count; i++) {
734 bt_ctf_validation_output_put_types(
735 &ec_validation_outputs[i]);
736 }
737 }
738 }
739
740 g_free(ec_validation_outputs);
741 bt_ctf_validation_output_put_types(&trace_sc_validation_output);
742 bt_put(current_parent_trace);
743 assert(!packet_header_type);
744 assert(!packet_context_type);
745 assert(!event_header_type);
746 assert(!stream_event_ctx_type);
747
748 return ret;
749 }
750
751 int64_t bt_ctf_trace_get_stream_count(struct bt_ctf_trace *trace)
752 {
753 int64_t ret;
754
755 if (!trace) {
756 ret = (int64_t) -1;
757 goto end;
758 }
759
760 ret = (int64_t) trace->streams->len;
761
762 end:
763 return ret;
764 }
765
766 struct bt_ctf_stream *bt_ctf_trace_get_stream_by_index(
767 struct bt_ctf_trace *trace,
768 uint64_t index)
769 {
770 struct bt_ctf_stream *stream = NULL;
771
772 if (!trace || index >= trace->streams->len) {
773 goto end;
774 }
775
776 stream = bt_get(g_ptr_array_index(trace->streams, index));
777
778 end:
779 return stream;
780 }
781
782 int64_t bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace)
783 {
784 int64_t ret;
785
786 if (!trace) {
787 ret = (int64_t) -1;
788 goto end;
789 }
790
791 ret = (int64_t) trace->stream_classes->len;
792 end:
793 return ret;
794 }
795
796 struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_index(
797 struct bt_ctf_trace *trace, uint64_t index)
798 {
799 struct bt_ctf_stream_class *stream_class = NULL;
800
801 if (!trace || index >= trace->stream_classes->len) {
802 goto end;
803 }
804
805 stream_class = g_ptr_array_index(trace->stream_classes, index);
806 bt_get(stream_class);
807 end:
808 return stream_class;
809 }
810
811 struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id(
812 struct bt_ctf_trace *trace, uint64_t id_param)
813 {
814 int i;
815 struct bt_ctf_stream_class *stream_class = NULL;
816 int64_t id = (int64_t) id_param;
817
818 if (!trace || id < 0) {
819 goto end;
820 }
821
822 for (i = 0; i < trace->stream_classes->len; i++) {
823 struct bt_ctf_stream_class *stream_class_candidate;
824
825 stream_class_candidate =
826 g_ptr_array_index(trace->stream_classes, i);
827
828 if (bt_ctf_stream_class_get_id(stream_class_candidate) ==
829 (int64_t) id) {
830 stream_class = stream_class_candidate;
831 bt_get(stream_class);
832 goto end;
833 }
834 }
835
836 end:
837 return stream_class;
838 }
839
840 struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name(
841 struct bt_ctf_trace *trace, const char *name)
842 {
843 size_t i;
844 struct bt_ctf_clock_class *clock_class = NULL;
845
846 if (!trace || !name) {
847 goto end;
848 }
849
850 for (i = 0; i < trace->clocks->len; i++) {
851 struct bt_ctf_clock_class *cur_clk =
852 g_ptr_array_index(trace->clocks, i);
853 const char *cur_clk_name = bt_ctf_clock_class_get_name(cur_clk);
854
855 if (!cur_clk_name) {
856 goto end;
857 }
858
859 if (!strcmp(cur_clk_name, name)) {
860 clock_class = cur_clk;
861 bt_get(clock_class);
862 goto end;
863 }
864 }
865
866 end:
867 return clock_class;
868 }
869
870 BT_HIDDEN
871 bt_bool bt_ctf_trace_has_clock_class(struct bt_ctf_trace *trace,
872 struct bt_ctf_clock_class *clock_class)
873 {
874 struct search_query query = { .value = clock_class, .found = 0 };
875
876 assert(trace);
877 assert(clock_class);
878
879 g_ptr_array_foreach(trace->clocks, value_exists, &query);
880 return query.found;
881 }
882
883 BT_HIDDEN
884 const char *get_byte_order_string(enum bt_ctf_byte_order byte_order)
885 {
886 const char *string;
887
888 switch (byte_order) {
889 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
890 string = "le";
891 break;
892 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
893 string = "be";
894 break;
895 case BT_CTF_BYTE_ORDER_NATIVE:
896 string = "native";
897 break;
898 default:
899 assert(BT_FALSE);
900 }
901
902 return string;
903 }
904
905 static
906 int append_trace_metadata(struct bt_ctf_trace *trace,
907 struct metadata_context *context)
908 {
909 unsigned char *uuid = trace->uuid;
910 int ret;
911
912 if (trace->native_byte_order == BT_CTF_BYTE_ORDER_NATIVE) {
913 ret = -1;
914 goto end;
915 }
916
917 g_string_append(context->string, "trace {\n");
918 g_string_append(context->string, "\tmajor = 1;\n");
919 g_string_append(context->string, "\tminor = 8;\n");
920 assert(trace->native_byte_order == BT_CTF_BYTE_ORDER_LITTLE_ENDIAN ||
921 trace->native_byte_order == BT_CTF_BYTE_ORDER_BIG_ENDIAN ||
922 trace->native_byte_order == BT_CTF_BYTE_ORDER_NETWORK);
923
924 if (trace->uuid_set) {
925 g_string_append_printf(context->string,
926 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
927 uuid[0], uuid[1], uuid[2], uuid[3],
928 uuid[4], uuid[5], uuid[6], uuid[7],
929 uuid[8], uuid[9], uuid[10], uuid[11],
930 uuid[12], uuid[13], uuid[14], uuid[15]);
931 }
932
933 g_string_append_printf(context->string, "\tbyte_order = %s;\n",
934 get_byte_order_string(trace->native_byte_order));
935
936 g_string_append(context->string, "\tpacket.header := ");
937 context->current_indentation_level++;
938 g_string_assign(context->field_name, "");
939 ret = bt_ctf_field_type_serialize(trace->packet_header_type,
940 context);
941 if (ret) {
942 goto end;
943 }
944 context->current_indentation_level--;
945
946 g_string_append(context->string, ";\n};\n\n");
947 end:
948 return ret;
949 }
950
951 static
952 void append_env_metadata(struct bt_ctf_trace *trace,
953 struct metadata_context *context)
954 {
955 int64_t i;
956 int64_t env_size;
957
958 env_size = bt_ctf_attributes_get_count(trace->environment);
959
960 if (env_size <= 0) {
961 return;
962 }
963
964 g_string_append(context->string, "env {\n");
965
966 for (i = 0; i < env_size; i++) {
967 struct bt_value *env_field_value_obj = NULL;
968 const char *entry_name;
969
970 entry_name = bt_ctf_attributes_get_field_name(
971 trace->environment, i);
972 env_field_value_obj = bt_ctf_attributes_get_field_value(
973 trace->environment, i);
974
975 if (!entry_name || !env_field_value_obj) {
976 goto loop_next;
977 }
978
979 switch (bt_value_get_type(env_field_value_obj)) {
980 case BT_VALUE_TYPE_INTEGER:
981 {
982 int ret;
983 int64_t int_value;
984
985 ret = bt_value_integer_get(env_field_value_obj,
986 &int_value);
987
988 if (ret) {
989 goto loop_next;
990 }
991
992 g_string_append_printf(context->string,
993 "\t%s = %" PRId64 ";\n", entry_name,
994 int_value);
995 break;
996 }
997 case BT_VALUE_TYPE_STRING:
998 {
999 int ret;
1000 const char *str_value;
1001 char *escaped_str = NULL;
1002
1003 ret = bt_value_string_get(env_field_value_obj,
1004 &str_value);
1005
1006 if (ret) {
1007 goto loop_next;
1008 }
1009
1010 escaped_str = g_strescape(str_value, NULL);
1011
1012 if (!escaped_str) {
1013 goto loop_next;
1014 }
1015
1016 g_string_append_printf(context->string,
1017 "\t%s = \"%s\";\n", entry_name, escaped_str);
1018 free(escaped_str);
1019 break;
1020 }
1021
1022 default:
1023 goto loop_next;
1024 }
1025
1026 loop_next:
1027 BT_PUT(env_field_value_obj);
1028 }
1029
1030 g_string_append(context->string, "};\n\n");
1031 }
1032
1033 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace)
1034 {
1035 char *metadata = NULL;
1036 struct metadata_context *context = NULL;
1037 int err = 0;
1038 size_t i;
1039
1040 if (!trace) {
1041 goto end;
1042 }
1043
1044 context = g_new0(struct metadata_context, 1);
1045 if (!context) {
1046 goto end;
1047 }
1048
1049 context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE);
1050 context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE);
1051 g_string_append(context->string, "/* CTF 1.8 */\n\n");
1052 if (append_trace_metadata(trace, context)) {
1053 goto error;
1054 }
1055 append_env_metadata(trace, context);
1056 g_ptr_array_foreach(trace->clocks,
1057 (GFunc)bt_ctf_clock_class_serialize, context);
1058
1059 for (i = 0; i < trace->stream_classes->len; i++) {
1060 err = bt_ctf_stream_class_serialize(
1061 trace->stream_classes->pdata[i], context);
1062 if (err) {
1063 goto error;
1064 }
1065 }
1066
1067 metadata = context->string->str;
1068 error:
1069 g_string_free(context->string, err ? TRUE : FALSE);
1070 g_string_free(context->field_name, TRUE);
1071 g_free(context);
1072 end:
1073 return metadata;
1074 }
1075
1076 enum bt_ctf_byte_order bt_ctf_trace_get_native_byte_order(
1077 struct bt_ctf_trace *trace)
1078 {
1079 enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN;
1080
1081 if (!trace) {
1082 goto end;
1083 }
1084
1085 ret = trace->native_byte_order;
1086
1087 end:
1088 return ret;
1089 }
1090
1091 int bt_ctf_trace_set_native_byte_order(struct bt_ctf_trace *trace,
1092 enum bt_ctf_byte_order byte_order)
1093 {
1094 int ret = 0;
1095
1096 if (!trace || trace->frozen) {
1097 ret = -1;
1098 goto end;
1099 }
1100
1101 if (byte_order != BT_CTF_BYTE_ORDER_LITTLE_ENDIAN &&
1102 byte_order != BT_CTF_BYTE_ORDER_BIG_ENDIAN &&
1103 byte_order != BT_CTF_BYTE_ORDER_NETWORK) {
1104 ret = -1;
1105 goto end;
1106 }
1107
1108 trace->native_byte_order = byte_order;
1109
1110 end:
1111 return ret;
1112 }
1113
1114 struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type(
1115 struct bt_ctf_trace *trace)
1116 {
1117 struct bt_ctf_field_type *field_type = NULL;
1118
1119 if (!trace) {
1120 goto end;
1121 }
1122
1123 bt_get(trace->packet_header_type);
1124 field_type = trace->packet_header_type;
1125 end:
1126 return field_type;
1127 }
1128
1129 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace,
1130 struct bt_ctf_field_type *packet_header_type)
1131 {
1132 int ret = 0;
1133
1134 if (!trace || trace->frozen) {
1135 ret = -1;
1136 goto end;
1137 }
1138
1139 /* packet_header_type must be a structure. */
1140 if (packet_header_type &&
1141 bt_ctf_field_type_get_type_id(packet_header_type) !=
1142 BT_CTF_FIELD_TYPE_ID_STRUCT) {
1143 ret = -1;
1144 goto end;
1145 }
1146
1147 bt_put(trace->packet_header_type);
1148 trace->packet_header_type = bt_get(packet_header_type);
1149 end:
1150 return ret;
1151 }
1152
1153 static
1154 int64_t get_stream_class_count(void *element)
1155 {
1156 return bt_ctf_trace_get_stream_class_count(
1157 (struct bt_ctf_trace *) element);
1158 }
1159
1160 static
1161 void *get_stream_class(void *element, int i)
1162 {
1163 return bt_ctf_trace_get_stream_class_by_index(
1164 (struct bt_ctf_trace *) element, i);
1165 }
1166
1167 static
1168 int visit_stream_class(void *object, bt_ctf_visitor visitor,void *data)
1169 {
1170 return bt_ctf_stream_class_visit(object, visitor, data);
1171 }
1172
1173 int bt_ctf_trace_visit(struct bt_ctf_trace *trace,
1174 bt_ctf_visitor visitor, void *data)
1175 {
1176 int ret;
1177 struct bt_ctf_object obj =
1178 { .object = trace, .type = BT_CTF_OBJECT_TYPE_TRACE };
1179
1180 if (!trace || !visitor) {
1181 ret = -1;
1182 goto end;
1183 }
1184
1185 ret = visitor_helper(&obj, get_stream_class_count,
1186 get_stream_class, visit_stream_class, visitor, data);
1187 end:
1188 return ret;
1189 }
1190
1191 static
1192 int invoke_listener(struct bt_ctf_object *object, void *data)
1193 {
1194 struct listener_wrapper *listener_wrapper = data;
1195
1196 listener_wrapper->listener(object, listener_wrapper->data);
1197 return 0;
1198 }
1199
1200 int bt_ctf_trace_add_listener(struct bt_ctf_trace *trace,
1201 bt_ctf_listener_cb listener, void *listener_data)
1202 {
1203 int ret = 0;
1204 struct listener_wrapper *listener_wrapper =
1205 g_new0(struct listener_wrapper, 1);
1206
1207 if (!trace || !listener || !listener_wrapper) {
1208 ret = -1;
1209 goto error;
1210 }
1211
1212 listener_wrapper->listener = listener;
1213 listener_wrapper->data = listener_data;
1214
1215 /* Visit the current schema. */
1216 ret = bt_ctf_trace_visit(trace, invoke_listener, listener_wrapper);
1217 if (ret) {
1218 goto error;
1219 }
1220
1221 /*
1222 * Add listener to the array of callbacks which will be invoked on
1223 * schema changes.
1224 */
1225 g_ptr_array_add(trace->listeners, listener_wrapper);
1226 return ret;
1227 error:
1228 g_free(listener_wrapper);
1229 return ret;
1230 }
1231
1232 BT_HIDDEN
1233 int bt_ctf_trace_object_modification(struct bt_ctf_object *object,
1234 void *trace_ptr)
1235 {
1236 size_t i;
1237 struct bt_ctf_trace *trace = trace_ptr;
1238
1239 assert(trace);
1240 assert(object);
1241
1242 if (trace->listeners->len == 0) {
1243 goto end;
1244 }
1245
1246 for (i = 0; i < trace->listeners->len; i++) {
1247 struct listener_wrapper *listener =
1248 g_ptr_array_index(trace->listeners, i);
1249
1250 listener->listener(object, listener->data);
1251 }
1252 end:
1253 return 0;
1254 }
1255
1256 BT_HIDDEN
1257 struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
1258 {
1259 int ret;
1260 unsigned int alignment, size;
1261 struct bt_ctf_field_type *field_type = NULL;
1262
1263 if (alias >= NR_FIELD_TYPE_ALIAS) {
1264 goto end;
1265 }
1266
1267 alignment = field_type_aliases_alignments[alias];
1268 size = field_type_aliases_sizes[alias];
1269 field_type = bt_ctf_field_type_integer_create(size);
1270 ret = bt_ctf_field_type_set_alignment(field_type, alignment);
1271 if (ret) {
1272 BT_PUT(field_type);
1273 }
1274 end:
1275 return field_type;
1276 }
1277
1278 static
1279 void bt_ctf_trace_freeze(struct bt_ctf_trace *trace)
1280 {
1281 int i;
1282
1283 bt_ctf_field_type_freeze(trace->packet_header_type);
1284 bt_ctf_attributes_freeze(trace->environment);
1285
1286 for (i = 0; i < trace->clocks->len; i++) {
1287 struct bt_ctf_clock_class *clock_class =
1288 g_ptr_array_index(trace->clocks, i);
1289
1290 bt_ctf_clock_class_freeze(clock_class);
1291 }
1292
1293 trace->frozen = 1;
1294 }
1295
1296 bt_bool bt_ctf_trace_is_static(struct bt_ctf_trace *trace)
1297 {
1298 bt_bool is_static = BT_FALSE;
1299
1300 if (!trace) {
1301 goto end;
1302 }
1303
1304 is_static = trace->is_static;
1305
1306 end:
1307 return is_static;
1308 }
1309
1310 int bt_ctf_trace_set_is_static(struct bt_ctf_trace *trace)
1311 {
1312 int ret = 0;
1313
1314 if (!trace) {
1315 ret = -1;
1316 goto end;
1317 }
1318
1319 trace->is_static = BT_TRUE;
1320 bt_ctf_trace_freeze(trace);
1321
1322 end:
1323 return ret;
1324 }
This page took 0.056133 seconds and 5 git commands to generate.