32ceca636f813e44aae98da61df8a41de5e2c5c7
[babeltrace.git] / formats / 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/graph/notification-schema.h>
44 #include <babeltrace/compiler.h>
45 #include <babeltrace/values.h>
46 #include <babeltrace/ref.h>
47 #include <babeltrace/endian.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 bt_ctf_trace_set_byte_order(trace, 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 struct search_query query = { .value = clock_class, .found = 0 };
392
393 if (!trace || !bt_ctf_clock_class_is_valid(clock_class)) {
394 ret = -1;
395 goto end;
396 }
397
398 /* Check for duplicate clock classes */
399 g_ptr_array_foreach(trace->clocks, value_exists, &query);
400 if (query.found) {
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 int bt_ctf_trace_get_clock_class_count(struct bt_ctf_trace *trace)
416 {
417 int 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, i;
447 int64_t stream_id;
448 struct bt_ctf_validation_output trace_sc_validation_output = { 0 };
449 struct bt_ctf_validation_output *ec_validation_outputs = NULL;
450 const enum bt_ctf_validation_flag trace_sc_validation_flags =
451 BT_CTF_VALIDATION_FLAG_TRACE |
452 BT_CTF_VALIDATION_FLAG_STREAM;
453 const enum bt_ctf_validation_flag ec_validation_flags =
454 BT_CTF_VALIDATION_FLAG_EVENT;
455 struct bt_ctf_field_type *packet_header_type = NULL;
456 struct bt_ctf_field_type *packet_context_type = NULL;
457 struct bt_ctf_field_type *event_header_type = NULL;
458 struct bt_ctf_field_type *stream_event_ctx_type = NULL;
459 int event_class_count;
460 struct bt_ctf_trace *current_parent_trace = NULL;
461
462 if (!trace || !stream_class) {
463 ret = -1;
464 goto end;
465 }
466
467 current_parent_trace = bt_ctf_stream_class_get_trace(stream_class);
468 if (current_parent_trace) {
469 /* Stream class is already associated to a trace, abort. */
470 ret = -1;
471 goto end;
472 }
473
474 event_class_count =
475 bt_ctf_stream_class_get_event_class_count(stream_class);
476 assert(event_class_count >= 0);
477
478 /* Check for duplicate stream classes */
479 for (i = 0; i < trace->stream_classes->len; i++) {
480 if (trace->stream_classes->pdata[i] == stream_class) {
481 /* Stream class already registered to the trace */
482 ret = -1;
483 goto end;
484 }
485 }
486
487 if (stream_class->clock) {
488 struct bt_ctf_clock_class *stream_clock_class =
489 stream_class->clock->clock_class;
490
491 if (trace->is_created_by_writer) {
492 /*
493 * Make sure this clock was also added to the
494 * trace (potentially through its CTF writer
495 * owner).
496 */
497 size_t i;
498
499 for (i = 0; i < trace->clocks->len; i++) {
500 if (trace->clocks->pdata[i] ==
501 stream_clock_class) {
502 /* Found! */
503 break;
504 }
505 }
506
507 if (i == trace->clocks->len) {
508 /* Not found */
509 ret = -1;
510 goto end;
511 }
512 } else {
513 /*
514 * This trace was NOT created by a CTF writer,
515 * thus do not allow the stream class to add to
516 * have a clock at all. Those are two
517 * independent APIs (non-writer and writer
518 * APIs), and isolating them simplifies things.
519 */
520 ret = -1;
521 goto end;
522 }
523 }
524
525 /*
526 * We're about to freeze both the trace and the stream class.
527 * Also, each event class contained in this stream class are
528 * already frozen.
529 *
530 * This trace, this stream class, and all its event classes
531 * should be valid at this point.
532 *
533 * Validate trace and stream class first, then each event
534 * class of this stream class can be validated individually.
535 */
536 packet_header_type =
537 bt_ctf_trace_get_packet_header_type(trace);
538 packet_context_type =
539 bt_ctf_stream_class_get_packet_context_type(stream_class);
540 event_header_type =
541 bt_ctf_stream_class_get_event_header_type(stream_class);
542 stream_event_ctx_type =
543 bt_ctf_stream_class_get_event_context_type(stream_class);
544 ret = bt_ctf_validate_class_types(trace->environment,
545 packet_header_type, packet_context_type, event_header_type,
546 stream_event_ctx_type, NULL, NULL, trace->valid,
547 stream_class->valid, 1, &trace_sc_validation_output,
548 trace_sc_validation_flags);
549 BT_PUT(packet_header_type);
550 BT_PUT(packet_context_type);
551 BT_PUT(event_header_type);
552 BT_PUT(stream_event_ctx_type);
553
554 if (ret) {
555 /*
556 * This means something went wrong during the validation
557 * process, not that the objects are invalid.
558 */
559 goto end;
560 }
561
562 if ((trace_sc_validation_output.valid_flags &
563 trace_sc_validation_flags) !=
564 trace_sc_validation_flags) {
565 /* Invalid trace/stream class */
566 ret = -1;
567 goto end;
568 }
569
570 if (event_class_count > 0) {
571 ec_validation_outputs = g_new0(struct bt_ctf_validation_output,
572 event_class_count);
573 if (!ec_validation_outputs) {
574 ret = -1;
575 goto end;
576 }
577 }
578
579 /* Validate each event class individually */
580 for (i = 0; i < event_class_count; i++) {
581 struct bt_ctf_event_class *event_class =
582 bt_ctf_stream_class_get_event_class(stream_class, i);
583 struct bt_ctf_field_type *event_context_type = NULL;
584 struct bt_ctf_field_type *event_payload_type = NULL;
585
586 event_context_type =
587 bt_ctf_event_class_get_context_type(event_class);
588 event_payload_type =
589 bt_ctf_event_class_get_payload_type(event_class);
590
591 /*
592 * It is important to use the field types returned by
593 * the previous trace and stream class validation here
594 * because copies could have been made.
595 */
596 ret = bt_ctf_validate_class_types(trace->environment,
597 trace_sc_validation_output.packet_header_type,
598 trace_sc_validation_output.packet_context_type,
599 trace_sc_validation_output.event_header_type,
600 trace_sc_validation_output.stream_event_ctx_type,
601 event_context_type, event_payload_type,
602 1, 1, event_class->valid, &ec_validation_outputs[i],
603 ec_validation_flags);
604 BT_PUT(event_context_type);
605 BT_PUT(event_payload_type);
606 BT_PUT(event_class);
607
608 if (ret) {
609 goto end;
610 }
611
612 if ((ec_validation_outputs[i].valid_flags &
613 ec_validation_flags) != ec_validation_flags) {
614 /* Invalid event class */
615 ret = -1;
616 goto end;
617 }
618 }
619
620 stream_id = bt_ctf_stream_class_get_id(stream_class);
621 if (stream_id < 0) {
622 stream_id = trace->next_stream_id++;
623
624 /* Try to assign a new stream id */
625 for (i = 0; i < trace->stream_classes->len; i++) {
626 if (stream_id == bt_ctf_stream_class_get_id(
627 trace->stream_classes->pdata[i])) {
628 /* Duplicate stream id found */
629 ret = -1;
630 goto end;
631 }
632 }
633
634 if (bt_ctf_stream_class_set_id_no_check(stream_class,
635 stream_id)) {
636 /* TODO Should retry with a different stream id */
637 ret = -1;
638 goto end;
639 }
640 }
641
642 bt_object_set_parent(stream_class, trace);
643 g_ptr_array_add(trace->stream_classes, stream_class);
644
645 /*
646 * At this point we know that the function will be successful.
647 * Therefore we can replace the trace and stream class field
648 * types with what's in their validation output structure and
649 * mark them as valid. We can also replace the field types of
650 * all the event classes of the stream class and mark them as
651 * valid.
652 */
653 bt_ctf_validation_replace_types(trace, stream_class, NULL,
654 &trace_sc_validation_output, trace_sc_validation_flags);
655 trace->valid = 1;
656 stream_class->valid = 1;
657
658 /*
659 * Put what was not moved in bt_ctf_validation_replace_types().
660 */
661 bt_ctf_validation_output_put_types(&trace_sc_validation_output);
662
663 for (i = 0; i < event_class_count; i++) {
664 struct bt_ctf_event_class *event_class =
665 bt_ctf_stream_class_get_event_class(stream_class, i);
666
667 bt_ctf_validation_replace_types(NULL, NULL, event_class,
668 &ec_validation_outputs[i], ec_validation_flags);
669 event_class->valid = 1;
670 BT_PUT(event_class);
671
672 /*
673 * Put what was not moved in
674 * bt_ctf_validation_replace_types().
675 */
676 bt_ctf_validation_output_put_types(&ec_validation_outputs[i]);
677 }
678
679 /*
680 * All field type byte orders set as "native" byte ordering can now be
681 * safely set to trace's own endianness, including the stream class'.
682 */
683 bt_ctf_field_type_set_native_byte_order(trace->packet_header_type,
684 trace->byte_order);
685 bt_ctf_stream_class_set_byte_order(stream_class, trace->byte_order);
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_class_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->stream_classes->len;
729 end:
730 return ret;
731 }
732
733 struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class(
734 struct bt_ctf_trace *trace, int index)
735 {
736 struct bt_ctf_stream_class *stream_class = NULL;
737
738 if (!trace || index < 0 || index >= trace->stream_classes->len) {
739 goto end;
740 }
741
742 stream_class = g_ptr_array_index(trace->stream_classes, index);
743 bt_get(stream_class);
744 end:
745 return stream_class;
746 }
747
748 struct bt_ctf_stream_class *bt_ctf_trace_get_stream_class_by_id(
749 struct bt_ctf_trace *trace, uint32_t id)
750 {
751 int i;
752 struct bt_ctf_stream_class *stream_class = NULL;
753
754 if (!trace) {
755 goto end;
756 }
757
758 for (i = 0; i < trace->stream_classes->len; i++) {
759 struct bt_ctf_stream_class *stream_class_candidate;
760
761 stream_class_candidate =
762 g_ptr_array_index(trace->stream_classes, i);
763
764 if (bt_ctf_stream_class_get_id(stream_class_candidate) ==
765 (int64_t) id) {
766 stream_class = stream_class_candidate;
767 bt_get(stream_class);
768 goto end;
769 }
770 }
771
772 end:
773 return stream_class;
774 }
775
776 struct bt_ctf_clock_class *bt_ctf_trace_get_clock_class_by_name(
777 struct bt_ctf_trace *trace, const char *name)
778 {
779 size_t i;
780 struct bt_ctf_clock_class *clock_class = NULL;
781
782 if (!trace || !name) {
783 goto end;
784 }
785
786 for (i = 0; i < trace->clocks->len; i++) {
787 struct bt_ctf_clock_class *cur_clk =
788 g_ptr_array_index(trace->clocks, i);
789 const char *cur_clk_name = bt_ctf_clock_class_get_name(cur_clk);
790
791 if (!cur_clk_name) {
792 goto end;
793 }
794
795 if (!strcmp(cur_clk_name, name)) {
796 clock_class = cur_clk;
797 bt_get(clock_class);
798 goto end;
799 }
800 }
801
802 end:
803 return clock_class;
804 }
805
806 BT_HIDDEN
807 const char *get_byte_order_string(int byte_order)
808 {
809 const char *string;
810
811 switch (byte_order) {
812 case LITTLE_ENDIAN:
813 string = "le";
814 break;
815 case BIG_ENDIAN:
816 string = "be";
817 break;
818 default:
819 string = "unknown";
820 break;
821 }
822
823 return string;
824 }
825
826 static
827 int append_trace_metadata(struct bt_ctf_trace *trace,
828 struct metadata_context *context)
829 {
830 unsigned char *uuid = trace->uuid;
831 int ret;
832
833 g_string_append(context->string, "trace {\n");
834
835 g_string_append(context->string, "\tmajor = 1;\n");
836 g_string_append(context->string, "\tminor = 8;\n");
837
838 g_string_append_printf(context->string,
839 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
840 uuid[0], uuid[1], uuid[2], uuid[3],
841 uuid[4], uuid[5], uuid[6], uuid[7],
842 uuid[8], uuid[9], uuid[10], uuid[11],
843 uuid[12], uuid[13], uuid[14], uuid[15]);
844 g_string_append_printf(context->string, "\tbyte_order = %s;\n",
845 get_byte_order_string(trace->byte_order));
846
847 g_string_append(context->string, "\tpacket.header := ");
848 context->current_indentation_level++;
849 g_string_assign(context->field_name, "");
850 ret = bt_ctf_field_type_serialize(trace->packet_header_type,
851 context);
852 if (ret) {
853 goto end;
854 }
855 context->current_indentation_level--;
856
857 g_string_append(context->string, ";\n};\n\n");
858 end:
859 return ret;
860 }
861
862 static
863 void append_env_metadata(struct bt_ctf_trace *trace,
864 struct metadata_context *context)
865 {
866 int i;
867 int env_size;
868
869 env_size = bt_ctf_attributes_get_count(trace->environment);
870
871 if (env_size <= 0) {
872 return;
873 }
874
875 g_string_append(context->string, "env {\n");
876
877 for (i = 0; i < env_size; i++) {
878 struct bt_value *env_field_value_obj = NULL;
879 const char *entry_name;
880
881 entry_name = bt_ctf_attributes_get_field_name(
882 trace->environment, i);
883 env_field_value_obj = bt_ctf_attributes_get_field_value(
884 trace->environment, i);
885
886 if (!entry_name || !env_field_value_obj) {
887 goto loop_next;
888 }
889
890 switch (bt_value_get_type(env_field_value_obj)) {
891 case BT_VALUE_TYPE_INTEGER:
892 {
893 int ret;
894 int64_t int_value;
895
896 ret = bt_value_integer_get(env_field_value_obj,
897 &int_value);
898
899 if (ret) {
900 goto loop_next;
901 }
902
903 g_string_append_printf(context->string,
904 "\t%s = %" PRId64 ";\n", entry_name,
905 int_value);
906 break;
907 }
908 case BT_VALUE_TYPE_STRING:
909 {
910 int ret;
911 const char *str_value;
912 char *escaped_str = NULL;
913
914 ret = bt_value_string_get(env_field_value_obj,
915 &str_value);
916
917 if (ret) {
918 goto loop_next;
919 }
920
921 escaped_str = g_strescape(str_value, NULL);
922
923 if (!escaped_str) {
924 goto loop_next;
925 }
926
927 g_string_append_printf(context->string,
928 "\t%s = \"%s\";\n", entry_name, escaped_str);
929 free(escaped_str);
930 break;
931 }
932
933 default:
934 goto loop_next;
935 }
936
937 loop_next:
938 BT_PUT(env_field_value_obj);
939 }
940
941 g_string_append(context->string, "};\n\n");
942 }
943
944 char *bt_ctf_trace_get_metadata_string(struct bt_ctf_trace *trace)
945 {
946 char *metadata = NULL;
947 struct metadata_context *context = NULL;
948 int err = 0;
949 size_t i;
950
951 if (!trace) {
952 goto end;
953 }
954
955 context = g_new0(struct metadata_context, 1);
956 if (!context) {
957 goto end;
958 }
959
960 context->field_name = g_string_sized_new(DEFAULT_IDENTIFIER_SIZE);
961 context->string = g_string_sized_new(DEFAULT_METADATA_STRING_SIZE);
962 g_string_append(context->string, "/* CTF 1.8 */\n\n");
963 if (append_trace_metadata(trace, context)) {
964 goto error;
965 }
966 append_env_metadata(trace, context);
967 g_ptr_array_foreach(trace->clocks,
968 (GFunc)bt_ctf_clock_class_serialize, context);
969
970 for (i = 0; i < trace->stream_classes->len; i++) {
971 err = bt_ctf_stream_class_serialize(
972 trace->stream_classes->pdata[i], context);
973 if (err) {
974 goto error;
975 }
976 }
977
978 metadata = context->string->str;
979 error:
980 g_string_free(context->string, err ? TRUE : FALSE);
981 g_string_free(context->field_name, TRUE);
982 g_free(context);
983 end:
984 return metadata;
985 }
986
987 enum bt_ctf_byte_order bt_ctf_trace_get_byte_order(struct bt_ctf_trace *trace)
988 {
989 enum bt_ctf_byte_order ret = BT_CTF_BYTE_ORDER_UNKNOWN;
990
991 if (!trace) {
992 goto end;
993 }
994
995 switch (trace->byte_order) {
996 case BIG_ENDIAN:
997 ret = BT_CTF_BYTE_ORDER_BIG_ENDIAN;
998 break;
999 case LITTLE_ENDIAN:
1000 ret = BT_CTF_BYTE_ORDER_LITTLE_ENDIAN;
1001 break;
1002 default:
1003 break;
1004 }
1005 end:
1006 return ret;
1007 }
1008
1009 int bt_ctf_trace_set_byte_order(struct bt_ctf_trace *trace,
1010 enum bt_ctf_byte_order byte_order)
1011 {
1012 int ret = 0;
1013 int internal_byte_order;
1014
1015 if (!trace || trace->frozen) {
1016 ret = -1;
1017 goto end;
1018 }
1019
1020 switch (byte_order) {
1021 case BT_CTF_BYTE_ORDER_NATIVE:
1022 /*
1023 * This doesn't make sense since the CTF specification defines
1024 * the "native" byte order as "the byte order described in the
1025 * trace description". However, this behavior had been
1026 * implemented as part of v1.2 and is kept to maintain
1027 * compatibility.
1028 *
1029 * This may be changed on a major version bump only.
1030 */
1031 internal_byte_order = (G_BYTE_ORDER == G_LITTLE_ENDIAN) ?
1032 LITTLE_ENDIAN : BIG_ENDIAN;
1033 break;
1034 case BT_CTF_BYTE_ORDER_LITTLE_ENDIAN:
1035 internal_byte_order = LITTLE_ENDIAN;
1036 break;
1037 case BT_CTF_BYTE_ORDER_BIG_ENDIAN:
1038 case BT_CTF_BYTE_ORDER_NETWORK:
1039 internal_byte_order = BIG_ENDIAN;
1040 break;
1041 default:
1042 ret = -1;
1043 goto end;
1044 }
1045
1046 trace->byte_order = internal_byte_order;
1047 end:
1048 return ret;
1049 }
1050
1051 struct bt_ctf_field_type *bt_ctf_trace_get_packet_header_type(
1052 struct bt_ctf_trace *trace)
1053 {
1054 struct bt_ctf_field_type *field_type = NULL;
1055
1056 if (!trace) {
1057 goto end;
1058 }
1059
1060 bt_get(trace->packet_header_type);
1061 field_type = trace->packet_header_type;
1062 end:
1063 return field_type;
1064 }
1065
1066 int bt_ctf_trace_set_packet_header_type(struct bt_ctf_trace *trace,
1067 struct bt_ctf_field_type *packet_header_type)
1068 {
1069 int ret = 0;
1070
1071 if (!trace || trace->frozen) {
1072 ret = -1;
1073 goto end;
1074 }
1075
1076 /* packet_header_type must be a structure. */
1077 if (packet_header_type &&
1078 bt_ctf_field_type_get_type_id(packet_header_type) !=
1079 BT_CTF_TYPE_ID_STRUCT) {
1080 ret = -1;
1081 goto end;
1082 }
1083
1084 bt_put(trace->packet_header_type);
1085 trace->packet_header_type = bt_get(packet_header_type);
1086 end:
1087 return ret;
1088 }
1089
1090 static
1091 int get_stream_class_count(void *element)
1092 {
1093 return bt_ctf_trace_get_stream_class_count(
1094 (struct bt_ctf_trace *) element);
1095 }
1096
1097 static
1098 void *get_stream_class(void *element, int i)
1099 {
1100 return bt_ctf_trace_get_stream_class(
1101 (struct bt_ctf_trace *) element, i);
1102 }
1103
1104 static
1105 int visit_stream_class(void *object, bt_ctf_visitor visitor,void *data)
1106 {
1107 return bt_ctf_stream_class_visit(object, visitor, data);
1108 }
1109
1110 int bt_ctf_trace_visit(struct bt_ctf_trace *trace,
1111 bt_ctf_visitor visitor, void *data)
1112 {
1113 int ret;
1114 struct bt_ctf_object obj =
1115 { .object = trace, .type = BT_CTF_OBJECT_TYPE_TRACE };
1116
1117 if (!trace || !visitor) {
1118 ret = -1;
1119 goto end;
1120 }
1121
1122 ret = visitor_helper(&obj, get_stream_class_count,
1123 get_stream_class, visit_stream_class, visitor, data);
1124 end:
1125 return ret;
1126 }
1127
1128 static
1129 int invoke_listener(struct bt_ctf_object *object, void *data)
1130 {
1131 struct listener_wrapper *listener_wrapper = data;
1132
1133 listener_wrapper->listener(object, listener_wrapper->data);
1134 return 0;
1135 }
1136
1137 int bt_ctf_trace_add_listener(struct bt_ctf_trace *trace,
1138 bt_ctf_listener_cb listener, void *listener_data)
1139 {
1140 int ret = 0;
1141 struct listener_wrapper *listener_wrapper =
1142 g_new0(struct listener_wrapper, 1);
1143
1144 if (!trace || !listener || !listener_wrapper) {
1145 ret = -1;
1146 goto error;
1147 }
1148
1149 listener_wrapper->listener = listener;
1150 listener_wrapper->data = listener_data;
1151
1152 /* Visit the current schema. */
1153 ret = bt_ctf_trace_visit(trace, invoke_listener, listener_wrapper);
1154 if (ret) {
1155 goto error;
1156 }
1157
1158 /*
1159 * Add listener to the array of callbacks which will be invoked on
1160 * schema changes.
1161 */
1162 g_ptr_array_add(trace->listeners, listener_wrapper);
1163 return ret;
1164 error:
1165 g_free(listener_wrapper);
1166 return ret;
1167 }
1168
1169 BT_HIDDEN
1170 int bt_ctf_trace_object_modification(struct bt_ctf_object *object,
1171 void *trace_ptr)
1172 {
1173 size_t i;
1174 struct bt_ctf_trace *trace = trace_ptr;
1175
1176 assert(trace);
1177 assert(object);
1178
1179 if (trace->listeners->len == 0) {
1180 goto end;
1181 }
1182
1183 for (i = 0; i < trace->listeners->len; i++) {
1184 struct listener_wrapper *listener =
1185 g_ptr_array_index(trace->listeners, i);
1186
1187 listener->listener(object, listener->data);
1188 }
1189 end:
1190 return 0;
1191 }
1192
1193 BT_HIDDEN
1194 struct bt_ctf_field_type *get_field_type(enum field_type_alias alias)
1195 {
1196 int ret;
1197 unsigned int alignment, size;
1198 struct bt_ctf_field_type *field_type = NULL;
1199
1200 if (alias >= NR_FIELD_TYPE_ALIAS) {
1201 goto end;
1202 }
1203
1204 alignment = field_type_aliases_alignments[alias];
1205 size = field_type_aliases_sizes[alias];
1206 field_type = bt_ctf_field_type_integer_create(size);
1207 ret = bt_ctf_field_type_set_alignment(field_type, alignment);
1208 if (ret) {
1209 BT_PUT(field_type);
1210 }
1211 end:
1212 return field_type;
1213 }
1214
1215 static
1216 void bt_ctf_trace_freeze(struct bt_ctf_trace *trace)
1217 {
1218 int i;
1219
1220 bt_ctf_field_type_freeze(trace->packet_header_type);
1221 bt_ctf_attributes_freeze(trace->environment);
1222
1223 for (i = 0; i < trace->clocks->len; i++) {
1224 struct bt_ctf_clock_class *clock_class =
1225 g_ptr_array_index(trace->clocks, i);
1226
1227 bt_ctf_clock_class_freeze(clock_class);
1228 }
1229
1230 trace->frozen = 1;
1231 }
1232
1233 static
1234 int init_trace_packet_header(struct bt_ctf_trace *trace)
1235 {
1236 int ret = 0;
1237 struct bt_ctf_field *magic = NULL, *uuid_array = NULL;
1238 struct bt_ctf_field_type *_uint32_t =
1239 get_field_type(FIELD_TYPE_ALIAS_UINT32_T);
1240 struct bt_ctf_field_type *_uint8_t =
1241 get_field_type(FIELD_TYPE_ALIAS_UINT8_T);
1242 struct bt_ctf_field_type *trace_packet_header_type =
1243 bt_ctf_field_type_structure_create();
1244 struct bt_ctf_field_type *uuid_array_type =
1245 bt_ctf_field_type_array_create(_uint8_t, 16);
1246
1247 if (!trace_packet_header_type || !uuid_array_type) {
1248 ret = -1;
1249 goto end;
1250 }
1251
1252 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
1253 _uint32_t, "magic");
1254 if (ret) {
1255 goto end;
1256 }
1257
1258 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
1259 uuid_array_type, "uuid");
1260 if (ret) {
1261 goto end;
1262 }
1263
1264 ret = bt_ctf_field_type_structure_add_field(trace_packet_header_type,
1265 _uint32_t, "stream_id");
1266 if (ret) {
1267 goto end;
1268 }
1269
1270 ret = bt_ctf_trace_set_packet_header_type(trace,
1271 trace_packet_header_type);
1272 if (ret) {
1273 goto end;
1274 }
1275 end:
1276 bt_put(uuid_array_type);
1277 bt_put(_uint32_t);
1278 bt_put(_uint8_t);
1279 bt_put(magic);
1280 bt_put(uuid_array);
1281 bt_put(trace_packet_header_type);
1282 return ret;
1283 }
This page took 0.056022 seconds and 3 git commands to generate.