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