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