Print timestamps in text plug-in
[babeltrace.git] / plugins / text / print.c
1 /*
2 * print.c
3 *
4 * Babeltrace CTF Text Output Plugin Event Printing
5 *
6 * Copyright 2016 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/event.h>
30 #include <babeltrace/ctf-ir/event-class.h>
31 #include <babeltrace/ctf-ir/packet.h>
32 #include <babeltrace/ctf-ir/stream.h>
33 #include <babeltrace/ctf-ir/stream-class.h>
34 #include <babeltrace/ctf-ir/clock.h>
35 #include <babeltrace/ctf-ir/field-types.h>
36 #include <babeltrace/ctf-ir/fields.h>
37 #include <babeltrace/ctf-ir/trace.h>
38 #include <babeltrace/bitfield.h>
39 #include <inttypes.h>
40 #include "text.h"
41
42 #define NSEC_PER_SEC 1000000000LL
43
44 static inline
45 const char *rem_(const char *str)
46 {
47 if (str[0] == '_')
48 return &str[1];
49 else
50 return str;
51 }
52
53 struct timestamp {
54 int64_t real_timestamp; /* Relative to UNIX epoch. */
55 uint64_t clock_value; /* In cycles. */
56 };
57
58 static
59 enum bt_component_status print_field(struct text_component *text,
60 struct bt_ctf_field *field, bool print_names);
61
62 static
63 void print_timestamp_cycles(struct text_component *text,
64 struct bt_ctf_clock *clock,
65 struct bt_ctf_event *event)
66 {
67 int ret;
68 struct bt_ctf_clock_value *clock_value;
69 uint64_t cycles;
70
71 clock_value = bt_ctf_event_get_clock_value(event, clock);
72 if (!clock_value) {
73 fputs("????????????????????", text->out);
74 return;
75 }
76
77 ret = bt_ctf_clock_value_get_value(clock_value, &cycles);
78 bt_put(clock_value);
79 if (ret) {
80 fprintf(text->out, "Error");
81 return;
82 }
83 fprintf(text->out, "%020" PRIu64, cycles);
84 }
85
86 static
87 void print_timestamp_wall(struct text_component *text,
88 struct bt_ctf_clock *clock,
89 struct bt_ctf_event *event)
90 {
91 int ret;
92 struct bt_ctf_clock_value *clock_value;
93 int64_t ts_nsec = 0; /* add configurable offset */
94 int64_t ts_sec = 0; /* add configurable offset */
95 uint64_t ts_sec_abs, ts_nsec_abs;
96 bool is_negative;
97
98 clock_value = bt_ctf_event_get_clock_value(event, clock);
99 if (!clock_value) {
100 fputs("??:??:??.?????????", text->out);
101 return;
102 }
103
104 ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, &ts_nsec);
105 bt_put(clock_value);
106 if (ret) {
107 fprintf(text->out, "Error");
108 return;
109 }
110
111 ts_sec += ts_nsec / NSEC_PER_SEC;
112 ts_nsec = ts_nsec % NSEC_PER_SEC;
113 if (ts_sec >= 0 && ts_nsec >= 0) {
114 is_negative = false;
115 ts_sec_abs = ts_sec;
116 ts_nsec_abs = ts_nsec;
117 } else if (ts_sec > 0 && ts_nsec < 0) {
118 is_negative = false;
119 ts_sec_abs = ts_sec - 1;
120 ts_nsec_abs = NSEC_PER_SEC + ts_nsec;
121 } else if (ts_sec == 0 && ts_nsec < 0) {
122 is_negative = true;
123 ts_sec_abs = ts_sec;
124 ts_nsec_abs = -ts_nsec;
125 } else if (ts_sec < 0 && ts_nsec > 0) {
126 is_negative = true;
127 ts_sec_abs = -(ts_sec + 1);
128 ts_nsec_abs = NSEC_PER_SEC - ts_nsec;
129 } else if (ts_sec < 0 && ts_nsec == 0) {
130 is_negative = true;
131 ts_sec_abs = -ts_sec;
132 ts_nsec_abs = ts_nsec;
133 } else { /* (ts_sec < 0 && ts_nsec < 0) */
134 is_negative = true;
135 ts_sec_abs = -ts_sec;
136 ts_nsec_abs = -ts_nsec;
137 }
138
139 if (/*!opt_clock_seconds*/true) {
140 struct tm tm;
141 time_t time_s = (time_t) ts_sec_abs;
142
143 if (is_negative) {
144 fprintf(stderr, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
145 goto seconds;
146 }
147
148 if (/*!opt_clock_gmt*/true) {
149 struct tm *res;
150
151 res = localtime_r(&time_s, &tm);
152 if (!res) {
153 fprintf(stderr, "[warning] Unable to get localtime.\n");
154 goto seconds;
155 }
156 } else {
157 struct tm *res;
158
159 res = gmtime_r(&time_s, &tm);
160 if (!res) {
161 fprintf(stderr, "[warning] Unable to get gmtime.\n");
162 goto seconds;
163 }
164 }
165 if (/*opt_clock_date*/false) {
166 char timestr[26];
167 size_t res;
168
169 /* Print date and time */
170 res = strftime(timestr, sizeof(timestr),
171 "%F ", &tm);
172 if (!res) {
173 fprintf(stderr, "[warning] Unable to print ascii time.\n");
174 goto seconds;
175 }
176 fprintf(text->out, "%s", timestr);
177 }
178 /* Print time in HH:MM:SS.ns */
179 fprintf(text->out, "%02d:%02d:%02d.%09" PRIu64,
180 tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec_abs);
181 goto end;
182 }
183 seconds:
184 fprintf(text->out, "%s%" PRId64 ".%09" PRIu64,
185 is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs);
186 end:
187 return;
188 }
189
190 static
191 enum bt_component_status print_event_timestamp(struct text_component *text,
192 struct bt_ctf_event *event)
193 {
194 bool print_names = text->options.print_header_field_names;
195 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
196 struct bt_ctf_stream *stream = NULL;
197 struct bt_ctf_stream_class *stream_class = NULL;
198 struct bt_ctf_trace *trace = NULL;
199 struct bt_ctf_clock *clock = NULL;
200 FILE *out = text->out;
201 FILE *err = text->err;
202 uint64_t real_timestamp;
203
204 stream = bt_ctf_event_get_stream(event);
205 if (!stream) {
206 ret = BT_COMPONENT_STATUS_ERROR;
207 goto end;
208 }
209
210 /* FIXME - error checking */
211 stream_class = bt_ctf_stream_get_class(stream);
212 trace = bt_ctf_stream_class_get_trace(stream_class);
213 clock = bt_ctf_trace_get_clock(trace, 0);
214
215 fputs(print_names ? "timestamp = " : "[", out);
216 if (text->options.print_timestamp_cycles) {
217 print_timestamp_cycles(text, clock, event);
218 } else {
219 print_timestamp_wall(text, clock, event);
220 }
221
222 fputs(print_names ? ", " : "] ", out);
223 if (!text->options.print_delta_field) {
224 goto end;
225 }
226 end:
227 bt_put(stream);
228 bt_put(clock);
229 bt_put(stream_class);
230 bt_put(trace);
231 return ret;
232 }
233
234 static
235 enum bt_component_status print_event_header(struct text_component *text,
236 struct bt_ctf_event *event)
237 {
238 bool print_names = text->options.print_header_field_names;
239 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
240 struct bt_ctf_event_class *event_class = NULL;
241
242 event_class = bt_ctf_event_get_class(event);
243 if (!event_class) {
244 ret = BT_COMPONENT_STATUS_ERROR;
245 goto end;
246 }
247 if (!text->start_line) {
248 fputs(", ", text->out);
249 }
250 text->start_line = false;
251 ret = print_event_timestamp(text, event);
252 if (ret != BT_COMPONENT_STATUS_OK) {
253 goto end;
254 }
255 if (print_names) {
256 fputs("name = ", text->out);
257 }
258 fputs(bt_ctf_event_class_get_name(event_class), text->out);
259 end:
260 bt_put(event_class);
261 return ret;
262 }
263
264 static
265 enum bt_component_status print_integer(struct text_component *text,
266 struct bt_ctf_field *field)
267 {
268 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
269 struct bt_ctf_field_type *field_type = NULL;
270 enum bt_ctf_integer_base base;
271 enum bt_ctf_string_encoding encoding;
272 int signedness;
273 union {
274 uint64_t u;
275 int64_t s;
276 } v;
277
278 field_type = bt_ctf_field_get_type(field);
279 if (!field_type) {
280 ret = BT_COMPONENT_STATUS_ERROR;
281 goto end;
282 }
283 signedness = bt_ctf_field_type_integer_get_signed(field_type);
284 if (signedness < 0) {
285 ret = BT_COMPONENT_STATUS_ERROR;
286 goto end;
287 }
288 if (!signedness) {
289 if (bt_ctf_field_unsigned_integer_get_value(field, &v.u) < 0) {
290 ret = BT_COMPONENT_STATUS_ERROR;
291 goto end;
292 }
293 } else {
294 if (bt_ctf_field_signed_integer_get_value(field, &v.s) < 0) {
295 ret = BT_COMPONENT_STATUS_ERROR;
296 goto end;
297 }
298 }
299
300 encoding = bt_ctf_field_type_integer_get_encoding(field_type);
301 switch (encoding) {
302 case BT_CTF_STRING_ENCODING_UTF8:
303 case BT_CTF_STRING_ENCODING_ASCII:
304 g_string_append_c(text->string, (int) v.u);
305 goto end;
306 case BT_CTF_STRING_ENCODING_NONE:
307 case BT_CTF_STRING_ENCODING_UNKNOWN:
308 break;
309 default:
310 ret = BT_COMPONENT_STATUS_ERROR;
311 goto end;
312 }
313
314 base = bt_ctf_field_type_integer_get_base(field_type);
315 switch (base) {
316 case BT_CTF_INTEGER_BASE_BINARY:
317 {
318 int bitnr, len;
319
320 len = bt_ctf_field_type_integer_get_size(field_type);
321 if (len < 0) {
322 ret = BT_COMPONENT_STATUS_ERROR;
323 goto end;
324 }
325 fprintf(text->out, "0b");
326 v.u = _bt_piecewise_lshift(v.u, 64 - len);
327 for (bitnr = 0; bitnr < len; bitnr++) {
328 fprintf(text->out, "%u", (v.u & (1ULL << 63)) ? 1 : 0);
329 v.u = _bt_piecewise_lshift(v.u, 1);
330 }
331 break;
332 }
333 case BT_CTF_INTEGER_BASE_OCTAL:
334 {
335 if (signedness) {
336 int len;
337
338 len = bt_ctf_field_type_integer_get_size(field_type);
339 if (len < 0) {
340 ret = BT_COMPONENT_STATUS_ERROR;
341 goto end;
342 }
343 if (len < 64) {
344 size_t rounded_len;
345
346 assert(len != 0);
347 /* Round length to the nearest 3-bit */
348 rounded_len = (((len - 1) / 3) + 1) * 3;
349 v.u &= ((uint64_t) 1 << rounded_len) - 1;
350 }
351 }
352
353 fprintf(text->out, "0%" PRIo64, v.u);
354 break;
355 }
356 case BT_CTF_INTEGER_BASE_DECIMAL:
357 if (!signedness) {
358 fprintf(text->out, "%" PRIu64, v.u);
359 } else {
360 fprintf(text->out, "%" PRId64, v.s);
361 }
362 break;
363 case BT_CTF_INTEGER_BASE_HEXADECIMAL:
364 {
365 int len;
366
367 len = bt_ctf_field_type_integer_get_size(field_type);
368 if (len < 0) {
369 ret = BT_COMPONENT_STATUS_ERROR;
370 goto end;
371 }
372 if (len < 64) {
373 /* Round length to the nearest nibble */
374 uint8_t rounded_len = ((len + 3) & ~0x3);
375
376 v.u &= ((uint64_t) 1 << rounded_len) - 1;
377 }
378
379 fprintf(text->out, "0x%" PRIX64, v.u);
380 break;
381 }
382 default:
383 ret = BT_COMPONENT_STATUS_ERROR;
384 goto end;
385 }
386 end:
387 bt_put(field_type);
388 return ret;
389 }
390
391 static
392 enum bt_component_status print_enum(struct text_component *text,
393 struct bt_ctf_field *field)
394 {
395 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
396 struct bt_ctf_field *container_field = NULL;
397 const char *mapping_name;
398
399 container_field = bt_ctf_field_enumeration_get_container(field);
400 if (!container_field) {
401 ret = BT_COMPONENT_STATUS_ERROR;
402 goto end;
403 }
404 mapping_name = bt_ctf_field_enumeration_get_mapping_name(field);
405 if (mapping_name) {
406 fprintf(text->out, "( \"%s\"", mapping_name);
407 } else {
408 fprintf(text->out, "( <unknown>");
409 }
410 fprintf(text->out, " : container = ");
411 ret = print_integer(text, container_field);
412 if (ret != BT_COMPONENT_STATUS_OK) {
413 goto end;
414 }
415 fprintf(text->out, " )");
416 end:
417 bt_put(container_field);
418 return ret;
419 }
420
421 static
422 enum bt_component_status print_struct_field(struct text_component *text,
423 struct bt_ctf_field *_struct,
424 struct bt_ctf_field_type *struct_type,
425 int i, bool print_names)
426 {
427 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
428 const char *field_name;
429 struct bt_ctf_field *field = NULL;
430 struct bt_ctf_field_type *field_type = NULL;;
431
432 field = bt_ctf_field_structure_get_field_by_index(_struct, i);
433 if (!field) {
434 ret = BT_COMPONENT_STATUS_ERROR;
435 goto end;
436 }
437 if (bt_ctf_field_type_structure_get_field(struct_type,
438 &field_name, &field_type, i) < 0) {
439 ret = BT_COMPONENT_STATUS_ERROR;
440 goto end;
441 }
442
443 if (i != 0) {
444 fprintf(text->out, ", ");
445 } else {
446 fprintf(text->out, " ");
447 }
448 if (print_names) {
449 fprintf(text->out, "%s = ", rem_(field_name));
450 }
451 ret = print_field(text, field, print_names);
452 end:
453 bt_put(field_type);
454 bt_put(field);
455 return ret;
456 }
457
458 static
459 enum bt_component_status print_struct(struct text_component *text,
460 struct bt_ctf_field *_struct, bool print_names)
461 {
462 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
463 struct bt_ctf_field_type *struct_type = NULL;
464 int nr_fields, i;
465
466 struct_type = bt_ctf_field_get_type(_struct);
467 if (!struct_type) {
468 ret = BT_COMPONENT_STATUS_ERROR;
469 goto end;
470 }
471 nr_fields = bt_ctf_field_type_structure_get_field_count(struct_type);
472 if (nr_fields < 0) {
473 ret = BT_COMPONENT_STATUS_ERROR;
474 goto end;
475 }
476 fprintf(text->out, "{");
477 text->depth++;
478 for (i = 0; i < nr_fields; i++) {
479 ret = print_struct_field(text, _struct, struct_type, i,
480 print_names);
481 if (ret != BT_COMPONENT_STATUS_OK) {
482 goto end;
483 }
484 }
485 text->depth--;
486 fprintf(text->out, " }");
487 end:
488 bt_put(struct_type);
489 return ret;
490 }
491
492 static
493 enum bt_component_status print_array_field(struct text_component *text,
494 struct bt_ctf_field *array, uint64_t i,
495 bool is_string, bool print_names)
496 {
497 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
498 struct bt_ctf_field *field = NULL;
499
500 if (!is_string) {
501 if (i != 0) {
502 fprintf(text->out, ", ");
503 } else {
504 fprintf(text->out, " ");
505 }
506 }
507 field = bt_ctf_field_array_get_field(array, i);
508 if (!field) {
509 ret = BT_COMPONENT_STATUS_ERROR;
510 goto end;
511 }
512 ret = print_field(text, field, print_names);
513 end:
514 bt_put(field);
515 return ret;
516 }
517
518 static
519 enum bt_component_status print_array(struct text_component *text,
520 struct bt_ctf_field *array, bool print_names)
521 {
522 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
523 struct bt_ctf_field_type *array_type = NULL, *field_type = NULL;
524 enum bt_ctf_type_id type_id;
525 int64_t len;
526 uint64_t i;
527 bool is_string = false;
528
529 array_type = bt_ctf_field_get_type(array);
530 if (!array_type) {
531 ret = BT_COMPONENT_STATUS_ERROR;
532 goto end;
533 }
534 field_type = bt_ctf_field_type_array_get_element_type(array_type);
535 if (!field_type) {
536 ret = BT_COMPONENT_STATUS_ERROR;
537 goto end;
538 }
539 len = bt_ctf_field_type_array_get_length(array_type);
540 if (len < 0) {
541 ret = BT_COMPONENT_STATUS_ERROR;
542 goto end;
543 }
544 type_id = bt_ctf_field_type_get_type_id(field_type);
545 if (type_id == BT_CTF_TYPE_ID_INTEGER) {
546 enum bt_ctf_string_encoding encoding;
547
548 encoding = bt_ctf_field_type_integer_get_encoding(field_type);
549 if (encoding == BT_CTF_STRING_ENCODING_UTF8
550 || encoding == BT_CTF_STRING_ENCODING_ASCII) {
551 int integer_len, integer_alignment;
552
553 integer_len = bt_ctf_field_type_integer_get_size(field_type);
554 if (integer_len < 0) {
555 return BT_COMPONENT_STATUS_ERROR;
556 }
557 integer_alignment = bt_ctf_field_type_get_alignment(field_type);
558 if (integer_alignment < 0) {
559 return BT_COMPONENT_STATUS_ERROR;
560 }
561 if (integer_len == CHAR_BIT
562 && integer_alignment == CHAR_BIT) {
563 is_string = true;
564 }
565 }
566 }
567
568 if (is_string) {
569 g_string_assign(text->string, "");
570 } else {
571 fprintf(text->out, "[");
572 }
573
574 text->depth++;
575 for (i = 0; i < len; i++) {
576 ret = print_array_field(text, array, i, is_string, print_names);
577 if (ret != BT_COMPONENT_STATUS_OK) {
578 goto end;
579 }
580 }
581 text->depth--;
582
583 if (is_string) {
584 fprintf(text->out, "\"%s\"", text->string->str);
585 } else {
586 fprintf(text->out, " ]");
587 }
588 end:
589 bt_put(field_type);
590 bt_put(array_type);
591 return ret;
592 }
593
594 static
595 enum bt_component_status print_sequence_field(struct text_component *text,
596 struct bt_ctf_field *seq, uint64_t i,
597 bool is_string, bool print_names)
598 {
599 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
600 struct bt_ctf_field *field = NULL;
601
602 if (!is_string) {
603 if (i != 0) {
604 fprintf(text->out, ", ");
605 } else {
606 fprintf(text->out, " ");
607 }
608 }
609 field = bt_ctf_field_sequence_get_field(seq, i);
610 if (!field) {
611 ret = BT_COMPONENT_STATUS_ERROR;
612 goto end;
613 }
614 ret = print_field(text, field, print_names);
615 end:
616 bt_put(field);
617 return ret;
618 }
619
620 static
621 enum bt_component_status print_sequence(struct text_component *text,
622 struct bt_ctf_field *seq, bool print_names)
623 {
624 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
625 struct bt_ctf_field_type *seq_type = NULL, *field_type = NULL;
626 struct bt_ctf_field *length_field = NULL;
627 enum bt_ctf_type_id type_id;
628 uint64_t len;
629 uint64_t i;
630 bool is_string = false;
631
632 seq_type = bt_ctf_field_get_type(seq);
633 if (!seq_type) {
634 ret = BT_COMPONENT_STATUS_ERROR;
635 goto end;
636 }
637 length_field = bt_ctf_field_sequence_get_length(seq);
638 if (!length_field) {
639 ret = BT_COMPONENT_STATUS_ERROR;
640 goto end;
641 }
642 if (bt_ctf_field_unsigned_integer_get_value(length_field, &len) < 0) {
643 ret = BT_COMPONENT_STATUS_ERROR;
644 goto end;
645 }
646 field_type = bt_ctf_field_type_sequence_get_element_type(seq_type);
647 if (!field_type) {
648 ret = BT_COMPONENT_STATUS_ERROR;
649 goto end;
650 }
651 type_id = bt_ctf_field_type_get_type_id(field_type);
652 if (type_id == BT_CTF_TYPE_ID_INTEGER) {
653 enum bt_ctf_string_encoding encoding;
654
655 encoding = bt_ctf_field_type_integer_get_encoding(field_type);
656 if (encoding == BT_CTF_STRING_ENCODING_UTF8
657 || encoding == BT_CTF_STRING_ENCODING_ASCII) {
658 int integer_len, integer_alignment;
659
660 integer_len = bt_ctf_field_type_integer_get_size(field_type);
661 if (integer_len < 0) {
662 ret = BT_COMPONENT_STATUS_ERROR;
663 goto end;
664 }
665 integer_alignment = bt_ctf_field_type_get_alignment(field_type);
666 if (integer_alignment < 0) {
667 ret = BT_COMPONENT_STATUS_ERROR;
668 goto end;
669 }
670 if (integer_len == CHAR_BIT
671 && integer_alignment == CHAR_BIT) {
672 is_string = true;
673 }
674 }
675 }
676
677 if (is_string) {
678 g_string_assign(text->string, "");
679 } else {
680 fprintf(text->out, "[");
681 }
682
683 text->depth++;
684 for (i = 0; i < len; i++) {
685 ret = print_sequence_field(text, seq, i,
686 is_string, print_names);
687 if (ret != BT_COMPONENT_STATUS_OK) {
688 goto end;
689 }
690 }
691 text->depth--;
692
693 if (is_string) {
694 fprintf(text->out, "\"%s\"", text->string->str);
695 } else {
696 fprintf(text->out, " ]");
697 }
698 end:
699 bt_put(length_field);
700 bt_put(field_type);
701 bt_put(seq_type);
702 return ret;
703 }
704
705 static
706 enum bt_component_status print_variant(struct text_component *text,
707 struct bt_ctf_field *variant, bool print_names)
708 {
709 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
710 struct bt_ctf_field *field = NULL;
711
712 field = bt_ctf_field_variant_get_current_field(variant);
713 if (!field) {
714 ret = BT_COMPONENT_STATUS_ERROR;
715 goto end;
716 }
717 fprintf(text->out, "{ ");
718 text->depth++;
719 if (print_names) {
720 struct bt_ctf_field *tag_field = NULL;
721 const char *tag_choice;
722
723 tag_field = bt_ctf_field_variant_get_tag(variant);
724 if (!tag_field) {
725 ret = BT_COMPONENT_STATUS_ERROR;
726 goto end;
727 }
728 tag_choice = bt_ctf_field_enumeration_get_mapping_name(tag_field);
729 if (!tag_choice) {
730 bt_put(tag_field);
731 ret = BT_COMPONENT_STATUS_ERROR;
732 goto end;
733 }
734 fprintf(text->out, "%s = ", rem_(tag_choice));
735 bt_put(tag_field);
736 }
737 ret = print_field(text, field, print_names);
738 if (ret != BT_COMPONENT_STATUS_OK) {
739 goto end;
740 }
741 text->depth--;
742 fprintf(text->out, " }");
743 end:
744 bt_put(field);
745 return ret;
746 }
747
748 static
749 enum bt_component_status print_field(struct text_component *text,
750 struct bt_ctf_field *field, bool print_names)
751 {
752 enum bt_ctf_type_id type_id;
753
754 type_id = bt_ctf_field_get_type_id(field);
755 switch (type_id) {
756 case CTF_TYPE_INTEGER:
757 return print_integer(text, field);
758 case CTF_TYPE_FLOAT:
759 {
760 double v;
761
762 if (bt_ctf_field_floating_point_get_value(field, &v)) {
763 return BT_COMPONENT_STATUS_ERROR;
764 }
765 fprintf(text->out, "%g", v);
766 return BT_COMPONENT_STATUS_OK;
767 }
768 case CTF_TYPE_ENUM:
769 return print_enum(text, field);
770 case CTF_TYPE_STRING:
771 fprintf(text->out, "\"%s\"", bt_ctf_field_string_get_value(field));
772 return BT_COMPONENT_STATUS_OK;
773 case CTF_TYPE_STRUCT:
774 return print_struct(text, field, print_names);
775 case CTF_TYPE_UNTAGGED_VARIANT:
776 case CTF_TYPE_VARIANT:
777 return print_variant(text, field, print_names);
778 case CTF_TYPE_ARRAY:
779 return print_array(text, field, print_names);
780 case CTF_TYPE_SEQUENCE:
781 return print_sequence(text, field, print_names);
782 default:
783 fprintf(text->err, "[error] Unknown type id: %d\n", (int) type_id);
784 return BT_COMPONENT_STATUS_ERROR;
785 }
786 }
787
788 static
789 enum bt_component_status print_stream_packet_context(struct text_component *text,
790 struct bt_ctf_event *event)
791 {
792 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
793 struct bt_ctf_packet *packet = NULL;
794 struct bt_ctf_field *main_field = NULL;
795
796 packet = bt_ctf_event_get_packet(event);
797 if (!packet) {
798 ret = BT_COMPONENT_STATUS_ERROR;
799 goto end;
800 }
801 main_field = bt_ctf_packet_get_context(packet);
802 if (!main_field) {
803 ret = BT_COMPONENT_STATUS_ERROR;
804 goto end;
805 }
806 if (!text->start_line) {
807 fputs(", ", text->out);
808 }
809 text->start_line = false;
810 if (text->options.print_scope_field_names) {
811 fputs("stream.packet.context = ", text->out);
812 }
813 ret = print_field(text, main_field,
814 text->options.print_context_field_names);
815 end:
816 bt_put(main_field);
817 bt_put(packet);
818 return ret;
819 }
820
821 static
822 enum bt_component_status print_event_header_raw(struct text_component *text,
823 struct bt_ctf_event *event)
824 {
825 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
826 struct bt_ctf_field *main_field = NULL;
827
828 main_field = bt_ctf_event_get_header(event);
829 if (!main_field) {
830 ret = BT_COMPONENT_STATUS_ERROR;
831 goto end;
832 }
833 if (!text->start_line) {
834 fputs(", ", text->out);
835 }
836 text->start_line = false;
837 if (text->options.print_scope_field_names) {
838 fputs("stream.event.header = ", text->out);
839 }
840 ret = print_field(text, main_field,
841 text->options.print_header_field_names);
842 end:
843 bt_put(main_field);
844 return ret;
845 }
846
847 static
848 enum bt_component_status print_stream_event_context(struct text_component *text,
849 struct bt_ctf_event *event)
850 {
851 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
852 struct bt_ctf_field *main_field = NULL;
853
854 main_field = bt_ctf_event_get_stream_event_context(event);
855 if (!main_field) {
856 ret = BT_COMPONENT_STATUS_ERROR;
857 goto end;
858 }
859 if (!text->start_line) {
860 fputs(", ", text->out);
861 }
862 text->start_line = false;
863 if (text->options.print_scope_field_names) {
864 fputs("stream.event.context = ", text->out);
865 }
866 ret = print_field(text, main_field,
867 text->options.print_context_field_names);
868 end:
869 bt_put(main_field);
870 return ret;
871 }
872
873 static
874 enum bt_component_status print_event_context(struct text_component *text,
875 struct bt_ctf_event *event)
876 {
877 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
878 struct bt_ctf_field *main_field = NULL;
879
880 main_field = bt_ctf_event_get_event_context(event);
881 if (!main_field) {
882 ret = BT_COMPONENT_STATUS_ERROR;
883 goto end;
884 }
885 if (!text->start_line) {
886 fputs(", ", text->out);
887 }
888 text->start_line = false;
889 if (text->options.print_scope_field_names) {
890 fputs("event.context = ", text->out);
891 }
892 ret = print_field(text, main_field,
893 text->options.print_context_field_names);
894 end:
895 bt_put(main_field);
896 return ret;
897 }
898
899 static
900 enum bt_component_status print_event_payload(struct text_component *text,
901 struct bt_ctf_event *event)
902 {
903 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
904 struct bt_ctf_field *main_field = NULL;
905
906 main_field = bt_ctf_event_get_payload_field(event);
907 if (!main_field) {
908 ret = BT_COMPONENT_STATUS_ERROR;
909 goto end;
910 }
911 if (!text->start_line) {
912 fputs(", ", text->out);
913 }
914 text->start_line = false;
915 if (text->options.print_scope_field_names) {
916 fputs("event.fields = ", text->out);
917 }
918 ret = print_field(text, main_field,
919 text->options.print_payload_field_names);
920 end:
921 bt_put(main_field);
922 return ret;
923 }
924
925 BT_HIDDEN
926 enum bt_component_status text_print_event(struct text_component *text,
927 struct bt_ctf_event *event)
928 {
929 enum bt_component_status ret;
930
931 text->start_line = true;
932 ret = print_event_header(text, event);
933 if (ret != BT_COMPONENT_STATUS_OK) {
934 goto end;
935 }
936
937 ret = print_stream_packet_context(text, event);
938 if (ret != BT_COMPONENT_STATUS_OK) {
939 goto end;
940 }
941
942 ret = print_event_header_raw(text, event);
943 if (ret != BT_COMPONENT_STATUS_OK) {
944 goto end;
945 }
946
947 ret = print_stream_event_context(text, event);
948 if (ret != BT_COMPONENT_STATUS_OK) {
949 goto end;
950 }
951
952 ret = print_event_context(text, event);
953 if (ret != BT_COMPONENT_STATUS_OK) {
954 goto end;
955 }
956
957 ret = print_event_payload(text, event);
958 if (ret != BT_COMPONENT_STATUS_OK) {
959 goto end;
960 }
961
962 fputc('\n', text->out);
963 end:
964 return ret;
965 }
This page took 0.049603 seconds and 5 git commands to generate.