Port: Use portable format string for ISO 8601 dates
[babeltrace.git] / plugins / text / pretty / 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 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #include <babeltrace/ctf-ir/event.h>
31 #include <babeltrace/ctf-ir/event-class.h>
32 #include <babeltrace/ctf-ir/packet.h>
33 #include <babeltrace/ctf-ir/stream.h>
34 #include <babeltrace/ctf-ir/stream-class.h>
35 #include <babeltrace/ctf-ir/clock-class.h>
36 #include <babeltrace/ctf-ir/field-types.h>
37 #include <babeltrace/ctf-ir/fields.h>
38 #include <babeltrace/ctf-ir/trace.h>
39 #include <babeltrace/graph/notification-event.h>
40 #include <babeltrace/graph/clock-class-priority-map.h>
41 #include <babeltrace/bitfield-internal.h>
42 #include <babeltrace/common-internal.h>
43 #include <babeltrace/compat/time-internal.h>
44 #include <inttypes.h>
45 #include <ctype.h>
46 #include "pretty.h"
47
48 #define NSEC_PER_SEC 1000000000LL
49
50 #define COLOR_NAME BT_COMMON_COLOR_BOLD
51 #define COLOR_FIELD_NAME BT_COMMON_COLOR_FG_CYAN
52 #define COLOR_RST BT_COMMON_COLOR_RESET
53 #define COLOR_STRING_VALUE BT_COMMON_COLOR_BOLD
54 #define COLOR_NUMBER_VALUE BT_COMMON_COLOR_BOLD
55 #define COLOR_ENUM_MAPPING_NAME BT_COMMON_COLOR_BOLD
56 #define COLOR_UNKNOWN BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_RED
57 #define COLOR_EVENT_NAME BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_MAGENTA
58 #define COLOR_TIMESTAMP BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_YELLOW
59
60 static inline
61 const char *rem_(const char *str)
62 {
63 if (str[0] == '_')
64 return &str[1];
65 else
66 return str;
67 }
68
69 struct timestamp {
70 int64_t real_timestamp; /* Relative to UNIX epoch. */
71 uint64_t clock_value; /* In cycles. */
72 };
73
74 static
75 enum bt_component_status print_field(struct pretty_component *pretty,
76 struct bt_ctf_field *field, bool print_names,
77 GQuark *filters_fields, int filter_array_len);
78
79 static
80 void print_name_equal(struct pretty_component *pretty, const char *name)
81 {
82 if (pretty->use_colors) {
83 g_string_append_printf(pretty->string, "%s%s%s = ", COLOR_NAME,
84 name, COLOR_RST);
85 } else {
86 g_string_append_printf(pretty->string, "%s = ", name);
87 }
88 }
89
90 static
91 void print_field_name_equal(struct pretty_component *pretty, const char *name)
92 {
93 if (pretty->use_colors) {
94 g_string_append_printf(pretty->string, "%s%s%s = ",
95 COLOR_FIELD_NAME, name, COLOR_RST);
96 } else {
97 g_string_append_printf(pretty->string, "%s = ", name);
98 }
99 }
100
101 static
102 void print_timestamp_cycles(struct pretty_component *pretty,
103 struct bt_ctf_clock_class *clock_class,
104 struct bt_ctf_event *event)
105 {
106 int ret;
107 struct bt_ctf_clock_value *clock_value;
108 uint64_t cycles;
109
110 clock_value = bt_ctf_event_get_clock_value(event, clock_class);
111 if (!clock_value) {
112 g_string_append(pretty->string, "????????????????????");
113 return;
114 }
115
116 ret = bt_ctf_clock_value_get_value(clock_value, &cycles);
117 bt_put(clock_value);
118 if (ret) {
119 // TODO: log, this is unexpected
120 g_string_append(pretty->string, "Error");
121 return;
122 }
123
124 g_string_append_printf(pretty->string, "%020" PRIu64, cycles);
125
126 if (pretty->last_cycles_timestamp != -1ULL) {
127 pretty->delta_cycles = cycles - pretty->last_cycles_timestamp;
128 }
129 pretty->last_cycles_timestamp = cycles;
130 }
131
132 static
133 void print_timestamp_wall(struct pretty_component *pretty,
134 struct bt_ctf_clock_class *clock_class,
135 struct bt_ctf_event *event)
136 {
137 int ret;
138 struct bt_ctf_clock_value *clock_value;
139 int64_t ts_nsec = 0; /* add configurable offset */
140 int64_t ts_sec = 0; /* add configurable offset */
141 uint64_t ts_sec_abs, ts_nsec_abs;
142 bool is_negative;
143
144 clock_value = bt_ctf_event_get_clock_value(event, clock_class);
145 if (!clock_value) {
146 g_string_append(pretty->string, "??:??:??.?????????");
147 return;
148 }
149
150 ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, &ts_nsec);
151 bt_put(clock_value);
152 if (ret) {
153 // TODO: log, this is unexpected
154 g_string_append(pretty->string, "Error");
155 return;
156 }
157
158 if (pretty->last_real_timestamp != -1ULL) {
159 pretty->delta_real_timestamp = ts_nsec - pretty->last_real_timestamp;
160 }
161
162 pretty->last_real_timestamp = ts_nsec;
163 ts_sec += ts_nsec / NSEC_PER_SEC;
164 ts_nsec = ts_nsec % NSEC_PER_SEC;
165
166 if (ts_sec >= 0 && ts_nsec >= 0) {
167 is_negative = false;
168 ts_sec_abs = ts_sec;
169 ts_nsec_abs = ts_nsec;
170 } else if (ts_sec > 0 && ts_nsec < 0) {
171 is_negative = false;
172 ts_sec_abs = ts_sec - 1;
173 ts_nsec_abs = NSEC_PER_SEC + ts_nsec;
174 } else if (ts_sec == 0 && ts_nsec < 0) {
175 is_negative = true;
176 ts_sec_abs = ts_sec;
177 ts_nsec_abs = -ts_nsec;
178 } else if (ts_sec < 0 && ts_nsec > 0) {
179 is_negative = true;
180 ts_sec_abs = -(ts_sec + 1);
181 ts_nsec_abs = NSEC_PER_SEC - ts_nsec;
182 } else if (ts_sec < 0 && ts_nsec == 0) {
183 is_negative = true;
184 ts_sec_abs = -ts_sec;
185 ts_nsec_abs = ts_nsec;
186 } else { /* (ts_sec < 0 && ts_nsec < 0) */
187 is_negative = true;
188 ts_sec_abs = -ts_sec;
189 ts_nsec_abs = -ts_nsec;
190 }
191
192 if (!pretty->options.clock_seconds) {
193 struct tm tm;
194 time_t time_s = (time_t) ts_sec_abs;
195
196 if (is_negative) {
197 // TODO: log instead
198 fprintf(stderr, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
199 goto seconds;
200 }
201
202 if (!pretty->options.clock_gmt) {
203 struct tm *res;
204
205 res = bt_localtime_r(&time_s, &tm);
206 if (!res) {
207 // TODO: log instead
208 fprintf(stderr, "[warning] Unable to get localtime.\n");
209 goto seconds;
210 }
211 } else {
212 struct tm *res;
213
214 res = bt_gmtime_r(&time_s, &tm);
215 if (!res) {
216 // TODO: log instead
217 fprintf(stderr, "[warning] Unable to get gmtime.\n");
218 goto seconds;
219 }
220 }
221 if (pretty->options.clock_date) {
222 char timestr[26];
223 size_t res;
224
225 /* Print date and time */
226 res = strftime(timestr, sizeof(timestr),
227 "%Y-%m-%d ", &tm);
228 if (!res) {
229 // TODO: log instead
230 fprintf(stderr, "[warning] Unable to print ascii time.\n");
231 goto seconds;
232 }
233
234 g_string_append(pretty->string, timestr);
235 }
236
237 /* Print time in HH:MM:SS.ns */
238 g_string_append_printf(pretty->string,
239 "%02d:%02d:%02d.%09" PRIu64, tm.tm_hour, tm.tm_min,
240 tm.tm_sec, ts_nsec_abs);
241 goto end;
242 }
243 seconds:
244 g_string_append_printf(pretty->string, "%s%" PRId64 ".%09" PRIu64,
245 is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs);
246 end:
247 return;
248 }
249
250 static
251 enum bt_component_status print_event_timestamp(struct pretty_component *pretty,
252 struct bt_ctf_event *event,
253 struct bt_clock_class_priority_map *cc_prio_map,
254 bool *start_line)
255 {
256 bool print_names = pretty->options.print_header_field_names;
257 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
258 struct bt_ctf_stream *stream = NULL;
259 struct bt_ctf_stream_class *stream_class = NULL;
260 struct bt_ctf_trace *trace = NULL;
261 struct bt_ctf_clock_class *clock_class = NULL;
262
263 stream = bt_ctf_event_get_stream(event);
264 if (!stream) {
265 ret = BT_COMPONENT_STATUS_ERROR;
266 goto end;
267 }
268
269 stream_class = bt_ctf_stream_get_class(stream);
270 if (!stream_class) {
271 ret = BT_COMPONENT_STATUS_ERROR;
272 goto end;
273 }
274 trace = bt_ctf_stream_class_get_trace(stream_class);
275 if (!trace) {
276 ret = BT_COMPONENT_STATUS_ERROR;
277 goto end;
278 }
279
280 if (bt_clock_class_priority_map_get_clock_class_count(cc_prio_map) == 0) {
281 /* No clock class: skip the timestamp without an error */
282 goto end;
283 }
284
285 clock_class =
286 bt_clock_class_priority_map_get_highest_priority_clock_class(
287 cc_prio_map);
288 if (!clock_class) {
289 ret = BT_COMPONENT_STATUS_ERROR;
290 goto end;
291 }
292
293 if (print_names) {
294 print_name_equal(pretty, "timestamp");
295 } else {
296 g_string_append(pretty->string, "[");
297 }
298 if (pretty->use_colors) {
299 g_string_append(pretty->string, COLOR_TIMESTAMP);
300 }
301 if (pretty->options.print_timestamp_cycles) {
302 print_timestamp_cycles(pretty, clock_class, event);
303 } else {
304 print_timestamp_wall(pretty, clock_class, event);
305 }
306 if (pretty->use_colors) {
307 g_string_append(pretty->string, COLOR_RST);
308 }
309
310 if (!print_names)
311 g_string_append(pretty->string, "] ");
312
313 if (pretty->options.print_delta_field) {
314 if (print_names) {
315 g_string_append(pretty->string, ", ");
316 print_name_equal(pretty, "delta");
317 } else {
318 g_string_append(pretty->string, "(");
319 }
320 if (pretty->options.print_timestamp_cycles) {
321 if (pretty->delta_cycles == -1ULL) {
322 g_string_append(pretty->string,
323 "+??????????\?\?) "); /* Not a trigraph. */
324 } else {
325 g_string_append_printf(pretty->string,
326 "+%012" PRIu64, pretty->delta_cycles);
327 }
328 } else {
329 if (pretty->delta_real_timestamp != -1ULL) {
330 uint64_t delta_sec, delta_nsec, delta;
331
332 delta = pretty->delta_real_timestamp;
333 delta_sec = delta / NSEC_PER_SEC;
334 delta_nsec = delta % NSEC_PER_SEC;
335 g_string_append_printf(pretty->string,
336 "+%" PRIu64 ".%09" PRIu64,
337 delta_sec, delta_nsec);
338 } else {
339 g_string_append(pretty->string, "+?.?????????");
340 }
341 }
342 if (!print_names) {
343 g_string_append(pretty->string, ") ");
344 }
345 }
346 *start_line = !print_names;
347
348 end:
349 bt_put(stream);
350 bt_put(clock_class);
351 bt_put(stream_class);
352 bt_put(trace);
353 return ret;
354 }
355
356 static
357 enum bt_component_status print_event_header(struct pretty_component *pretty,
358 struct bt_ctf_event *event,
359 struct bt_clock_class_priority_map *cc_prio_map)
360 {
361 bool print_names = pretty->options.print_header_field_names;
362 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
363 struct bt_ctf_event_class *event_class = NULL;
364 struct bt_ctf_stream_class *stream_class = NULL;
365 struct bt_ctf_trace *trace_class = NULL;
366 int dom_print = 0;
367
368 event_class = bt_ctf_event_get_class(event);
369 if (!event_class) {
370 ret = BT_COMPONENT_STATUS_ERROR;
371 goto end;
372 }
373 stream_class = bt_ctf_event_class_get_stream_class(event_class);
374 if (!stream_class) {
375 ret = BT_COMPONENT_STATUS_ERROR;
376 goto end;
377 }
378 trace_class = bt_ctf_stream_class_get_trace(stream_class);
379 if (!trace_class) {
380 ret = BT_COMPONENT_STATUS_ERROR;
381 goto end;
382 }
383 ret = print_event_timestamp(pretty, event, cc_prio_map,
384 &pretty->start_line);
385 if (ret != BT_COMPONENT_STATUS_OK) {
386 goto end;
387 }
388 if (pretty->options.print_trace_field) {
389 const char *name;
390
391 name = bt_ctf_trace_get_name(trace_class);
392 if (name) {
393 if (!pretty->start_line) {
394 g_string_append(pretty->string, ", ");
395 }
396 if (print_names) {
397 print_name_equal(pretty, "trace");
398 }
399
400 g_string_append(pretty->string, name);
401
402 if (!print_names) {
403 g_string_append(pretty->string, " ");
404 }
405 }
406 }
407 if (pretty->options.print_trace_hostname_field) {
408 struct bt_value *hostname_str;
409
410 hostname_str = bt_ctf_trace_get_environment_field_value_by_name(trace_class,
411 "hostname");
412 if (hostname_str) {
413 const char *str;
414
415 if (!pretty->start_line) {
416 g_string_append(pretty->string, ", ");
417 }
418 if (print_names) {
419 print_name_equal(pretty, "trace:hostname");
420 }
421 if (bt_value_string_get(hostname_str, &str)
422 == BT_VALUE_STATUS_OK) {
423 g_string_append(pretty->string, str);
424 }
425 bt_put(hostname_str);
426 dom_print = 1;
427 }
428 }
429 if (pretty->options.print_trace_domain_field) {
430 struct bt_value *domain_str;
431
432 domain_str = bt_ctf_trace_get_environment_field_value_by_name(trace_class,
433 "domain");
434 if (domain_str) {
435 const char *str;
436
437 if (!pretty->start_line) {
438 g_string_append(pretty->string, ", ");
439 }
440 if (print_names) {
441 print_name_equal(pretty, "trace:domain");
442 } else if (dom_print) {
443 g_string_append(pretty->string, ":");
444 }
445 if (bt_value_string_get(domain_str, &str)
446 == BT_VALUE_STATUS_OK) {
447 g_string_append(pretty->string, str);
448 }
449 bt_put(domain_str);
450 dom_print = 1;
451 }
452 }
453 if (pretty->options.print_trace_procname_field) {
454 struct bt_value *procname_str;
455
456 procname_str = bt_ctf_trace_get_environment_field_value_by_name(trace_class,
457 "procname");
458 if (procname_str) {
459 const char *str;
460
461 if (!pretty->start_line) {
462 g_string_append(pretty->string, ", ");
463 }
464 if (print_names) {
465 print_name_equal(pretty, "trace:procname");
466 } else if (dom_print) {
467 g_string_append(pretty->string, ":");
468 }
469 if (bt_value_string_get(procname_str, &str)
470 == BT_VALUE_STATUS_OK) {
471 g_string_append(pretty->string, str);
472 }
473 bt_put(procname_str);
474 dom_print = 1;
475 }
476 }
477 if (pretty->options.print_trace_vpid_field) {
478 struct bt_value *vpid_value;
479
480 vpid_value = bt_ctf_trace_get_environment_field_value_by_name(trace_class,
481 "vpid");
482 if (vpid_value) {
483 int64_t value;
484
485 if (!pretty->start_line) {
486 g_string_append(pretty->string, ", ");
487 }
488 if (print_names) {
489 print_name_equal(pretty, "trace:vpid");
490 } else if (dom_print) {
491 g_string_append(pretty->string, ":");
492 }
493 if (bt_value_integer_get(vpid_value, &value)
494 == BT_VALUE_STATUS_OK) {
495 g_string_append_printf(pretty->string, "(%" PRId64 ")", value);
496 }
497 bt_put(vpid_value);
498 dom_print = 1;
499 }
500 }
501 if (pretty->options.print_loglevel_field) {
502 struct bt_value *loglevel_str, *loglevel_value;
503
504 loglevel_str = bt_ctf_event_class_get_attribute_value_by_name(event_class,
505 "loglevel_string");
506 loglevel_value = bt_ctf_event_class_get_attribute_value_by_name(event_class,
507 "loglevel");
508 if (loglevel_str || loglevel_value) {
509 bool has_str = false;
510
511 if (!pretty->start_line) {
512 g_string_append(pretty->string, ", ");
513 }
514 if (print_names) {
515 print_name_equal(pretty, "loglevel");
516 } else if (dom_print) {
517 g_string_append(pretty->string, ":");
518 }
519 if (loglevel_str) {
520 const char *str;
521
522 if (bt_value_string_get(loglevel_str, &str)
523 == BT_VALUE_STATUS_OK) {
524 g_string_append(pretty->string, str);
525 has_str = true;
526 }
527 }
528 if (loglevel_value) {
529 int64_t value;
530
531 if (bt_value_integer_get(loglevel_value, &value)
532 == BT_VALUE_STATUS_OK) {
533 g_string_append_printf(pretty->string, "%s(%" PRId64 ")",
534 has_str ? " " : "", value);
535 }
536 }
537 bt_put(loglevel_str);
538 bt_put(loglevel_value);
539 dom_print = 1;
540 }
541 }
542 if (pretty->options.print_emf_field) {
543 struct bt_value *uri_str;
544
545 uri_str = bt_ctf_event_class_get_attribute_value_by_name(event_class,
546 "model.emf.uri");
547 if (uri_str) {
548 if (!pretty->start_line) {
549 g_string_append(pretty->string, ", ");
550 }
551 if (print_names) {
552 print_name_equal(pretty, "model.emf.uri");
553 } else if (dom_print) {
554 g_string_append(pretty->string, ":");
555 }
556 if (uri_str) {
557 const char *str;
558
559 if (bt_value_string_get(uri_str, &str)
560 == BT_VALUE_STATUS_OK) {
561 g_string_append(pretty->string, str);
562 }
563 }
564 bt_put(uri_str);
565 dom_print = 1;
566 }
567 }
568 if (dom_print && !print_names) {
569 g_string_append(pretty->string, " ");
570 }
571 if (!pretty->start_line) {
572 g_string_append(pretty->string, ", ");
573 }
574 pretty->start_line = true;
575 if (print_names) {
576 print_name_equal(pretty, "name");
577 }
578 if (pretty->use_colors) {
579 g_string_append(pretty->string, COLOR_EVENT_NAME);
580 }
581 g_string_append(pretty->string, bt_ctf_event_class_get_name(event_class));
582 if (pretty->use_colors) {
583 g_string_append(pretty->string, COLOR_RST);
584 }
585 if (!print_names) {
586 g_string_append(pretty->string, ": ");
587 } else {
588 g_string_append(pretty->string, ", ");
589 }
590 end:
591 bt_put(trace_class);
592 bt_put(stream_class);
593 bt_put(event_class);
594 return ret;
595 }
596
597 static
598 enum bt_component_status print_integer(struct pretty_component *pretty,
599 struct bt_ctf_field *field)
600 {
601 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
602 struct bt_ctf_field_type *field_type = NULL;
603 enum bt_ctf_integer_base base;
604 enum bt_ctf_string_encoding encoding;
605 int signedness;
606 union {
607 uint64_t u;
608 int64_t s;
609 } v;
610 bool rst_color = false;
611
612 field_type = bt_ctf_field_get_type(field);
613 if (!field_type) {
614 ret = BT_COMPONENT_STATUS_ERROR;
615 goto end;
616 }
617 signedness = bt_ctf_field_type_integer_get_signed(field_type);
618 if (signedness < 0) {
619 ret = BT_COMPONENT_STATUS_ERROR;
620 goto end;
621 }
622 if (!signedness) {
623 if (bt_ctf_field_unsigned_integer_get_value(field, &v.u) < 0) {
624 ret = BT_COMPONENT_STATUS_ERROR;
625 goto end;
626 }
627 } else {
628 if (bt_ctf_field_signed_integer_get_value(field, &v.s) < 0) {
629 ret = BT_COMPONENT_STATUS_ERROR;
630 goto end;
631 }
632 }
633
634 encoding = bt_ctf_field_type_integer_get_encoding(field_type);
635 switch (encoding) {
636 case BT_CTF_STRING_ENCODING_UTF8:
637 case BT_CTF_STRING_ENCODING_ASCII:
638 g_string_append_c(pretty->tmp_string, (int) v.u);
639 goto end;
640 case BT_CTF_STRING_ENCODING_NONE:
641 case BT_CTF_STRING_ENCODING_UNKNOWN:
642 break;
643 default:
644 ret = BT_COMPONENT_STATUS_ERROR;
645 goto end;
646 }
647
648 if (pretty->use_colors) {
649 g_string_append(pretty->string, COLOR_NUMBER_VALUE);
650 rst_color = true;
651 }
652
653 base = bt_ctf_field_type_integer_get_base(field_type);
654 switch (base) {
655 case BT_CTF_INTEGER_BASE_BINARY:
656 {
657 int bitnr, len;
658
659 len = bt_ctf_field_type_integer_get_size(field_type);
660 if (len < 0) {
661 ret = BT_COMPONENT_STATUS_ERROR;
662 goto end;
663 }
664 g_string_append(pretty->string, "0b");
665 v.u = _bt_piecewise_lshift(v.u, 64 - len);
666 for (bitnr = 0; bitnr < len; bitnr++) {
667 g_string_append_printf(pretty->string, "%u", (v.u & (1ULL << 63)) ? 1 : 0);
668 v.u = _bt_piecewise_lshift(v.u, 1);
669 }
670 break;
671 }
672 case BT_CTF_INTEGER_BASE_OCTAL:
673 {
674 if (signedness) {
675 int len;
676
677 len = bt_ctf_field_type_integer_get_size(field_type);
678 if (len < 0) {
679 ret = BT_COMPONENT_STATUS_ERROR;
680 goto end;
681 }
682 if (len < 64) {
683 size_t rounded_len;
684
685 assert(len != 0);
686 /* Round length to the nearest 3-bit */
687 rounded_len = (((len - 1) / 3) + 1) * 3;
688 v.u &= ((uint64_t) 1 << rounded_len) - 1;
689 }
690 }
691
692 g_string_append_printf(pretty->string, "0%" PRIo64, v.u);
693 break;
694 }
695 case BT_CTF_INTEGER_BASE_DECIMAL:
696 if (!signedness) {
697 g_string_append_printf(pretty->string, "%" PRIu64, v.u);
698 } else {
699 g_string_append_printf(pretty->string, "%" PRId64, v.s);
700 }
701 break;
702 case BT_CTF_INTEGER_BASE_HEXADECIMAL:
703 {
704 int len;
705
706 len = bt_ctf_field_type_integer_get_size(field_type);
707 if (len < 0) {
708 ret = BT_COMPONENT_STATUS_ERROR;
709 goto end;
710 }
711 if (len < 64) {
712 /* Round length to the nearest nibble */
713 uint8_t rounded_len = ((len + 3) & ~0x3);
714
715 v.u &= ((uint64_t) 1 << rounded_len) - 1;
716 }
717
718 g_string_append_printf(pretty->string, "0x%" PRIX64, v.u);
719 break;
720 }
721 default:
722 ret = BT_COMPONENT_STATUS_ERROR;
723 goto end;
724 }
725 end:
726 if (rst_color) {
727 g_string_append(pretty->string, COLOR_RST);
728 }
729 bt_put(field_type);
730 return ret;
731 }
732
733 static
734 void print_escape_string(struct pretty_component *pretty, const char *str)
735 {
736 int i;
737
738 g_string_append_c(pretty->string, '"');
739
740 for (i = 0; i < strlen(str); i++) {
741 /* Escape sequences not recognized by iscntrl(). */
742 switch (str[i]) {
743 case '\\':
744 g_string_append(pretty->string, "\\\\");
745 continue;
746 case '\'':
747 g_string_append(pretty->string, "\\\'");
748 continue;
749 case '\"':
750 g_string_append(pretty->string, "\\\"");
751 continue;
752 case '\?':
753 g_string_append(pretty->string, "\\\?");
754 continue;
755 }
756
757 /* Standard characters. */
758 if (!iscntrl(str[i])) {
759 g_string_append_c(pretty->string, str[i]);
760 continue;
761 }
762
763 switch (str[i]) {
764 case '\0':
765 g_string_append(pretty->string, "\\0");
766 break;
767 case '\a':
768 g_string_append(pretty->string, "\\a");
769 break;
770 case '\b':
771 g_string_append(pretty->string, "\\b");
772 break;
773 case '\e':
774 g_string_append(pretty->string, "\\e");
775 break;
776 case '\f':
777 g_string_append(pretty->string, "\\f");
778 break;
779 case '\n':
780 g_string_append(pretty->string, "\\n");
781 break;
782 case '\r':
783 g_string_append(pretty->string, "\\r");
784 break;
785 case '\t':
786 g_string_append(pretty->string, "\\t");
787 break;
788 case '\v':
789 g_string_append(pretty->string, "\\v");
790 break;
791 default:
792 /* Unhandled control-sequence, print as hex. */
793 g_string_append_printf(pretty->string, "\\x%02x", str[i]);
794 break;
795 }
796 }
797
798 g_string_append_c(pretty->string, '"');
799 }
800
801 static
802 enum bt_component_status print_enum(struct pretty_component *pretty,
803 struct bt_ctf_field *field)
804 {
805 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
806 struct bt_ctf_field *container_field = NULL;
807 struct bt_ctf_field_type *enumeration_field_type = NULL;
808 struct bt_ctf_field_type *container_field_type = NULL;
809 struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL;
810 int nr_mappings = 0;
811 int is_signed;
812
813 enumeration_field_type = bt_ctf_field_get_type(field);
814 if (!enumeration_field_type) {
815 ret = BT_COMPONENT_STATUS_ERROR;
816 goto end;
817 }
818 container_field = bt_ctf_field_enumeration_get_container(field);
819 if (!container_field) {
820 ret = BT_COMPONENT_STATUS_ERROR;
821 goto end;
822 }
823 container_field_type = bt_ctf_field_get_type(container_field);
824 if (!container_field_type) {
825 ret = BT_COMPONENT_STATUS_ERROR;
826 goto end;
827 }
828 is_signed = bt_ctf_field_type_integer_get_signed(container_field_type);
829 if (is_signed < 0) {
830 ret = BT_COMPONENT_STATUS_ERROR;
831 goto end;
832 }
833 if (is_signed) {
834 int64_t value;
835
836 if (bt_ctf_field_signed_integer_get_value(container_field,
837 &value)) {
838 ret = BT_COMPONENT_STATUS_ERROR;
839 goto end;
840 }
841 iter = bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
842 enumeration_field_type, value);
843 } else {
844 uint64_t value;
845
846 if (bt_ctf_field_unsigned_integer_get_value(container_field,
847 &value)) {
848 ret = BT_COMPONENT_STATUS_ERROR;
849 goto end;
850 }
851 iter = bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
852 enumeration_field_type, value);
853 }
854 if (!iter) {
855 ret = BT_COMPONENT_STATUS_ERROR;
856 goto end;
857 }
858 g_string_append(pretty->string, "( ");
859 for (;;) {
860 const char *mapping_name;
861
862 if (bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
863 iter, &mapping_name, NULL, NULL) < 0) {
864 ret = BT_COMPONENT_STATUS_ERROR;
865 goto end;
866 }
867 if (nr_mappings++)
868 g_string_append(pretty->string, ", ");
869 if (pretty->use_colors) {
870 g_string_append(pretty->string, COLOR_ENUM_MAPPING_NAME);
871 }
872 print_escape_string(pretty, mapping_name);
873 if (pretty->use_colors) {
874 g_string_append(pretty->string, COLOR_RST);
875 }
876 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter) < 0) {
877 break;
878 }
879 }
880 if (!nr_mappings) {
881 if (pretty->use_colors) {
882 g_string_append(pretty->string, COLOR_UNKNOWN);
883 }
884 g_string_append(pretty->string, "<unknown>");
885 if (pretty->use_colors) {
886 g_string_append(pretty->string, COLOR_RST);
887 }
888 }
889 g_string_append(pretty->string, " : container = ");
890 ret = print_integer(pretty, container_field);
891 if (ret != BT_COMPONENT_STATUS_OK) {
892 goto end;
893 }
894 g_string_append(pretty->string, " )");
895 end:
896 bt_put(iter);
897 bt_put(container_field_type);
898 bt_put(container_field);
899 bt_put(enumeration_field_type);
900 return ret;
901 }
902
903 static
904 int filter_field_name(struct pretty_component *pretty, const char *field_name,
905 GQuark *filter_fields, int filter_array_len)
906 {
907 int i;
908 GQuark field_quark = g_quark_try_string(field_name);
909
910 if (!field_quark || pretty->options.verbose) {
911 return 1;
912 }
913
914 for (i = 0; i < filter_array_len; i++) {
915 if (field_quark == filter_fields[i]) {
916 return 0;
917 }
918 }
919 return 1;
920 }
921
922 static
923 enum bt_component_status print_struct_field(struct pretty_component *pretty,
924 struct bt_ctf_field *_struct,
925 struct bt_ctf_field_type *struct_type,
926 int i, bool print_names, int *nr_printed_fields,
927 GQuark *filter_fields, int filter_array_len)
928 {
929 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
930 const char *field_name;
931 struct bt_ctf_field *field = NULL;
932 struct bt_ctf_field_type *field_type = NULL;;
933
934 field = bt_ctf_field_structure_get_field_by_index(_struct, i);
935 if (!field) {
936 ret = BT_COMPONENT_STATUS_ERROR;
937 goto end;
938 }
939 if (bt_ctf_field_type_structure_get_field(struct_type,
940 &field_name, &field_type, i) < 0) {
941 ret = BT_COMPONENT_STATUS_ERROR;
942 goto end;
943 }
944
945 if (filter_fields && !filter_field_name(pretty, field_name,
946 filter_fields, filter_array_len)) {
947 ret = BT_COMPONENT_STATUS_OK;
948 goto end;
949 }
950
951 if (*nr_printed_fields > 0) {
952 g_string_append(pretty->string, ", ");
953 } else {
954 g_string_append(pretty->string, " ");
955 }
956 if (print_names) {
957 print_field_name_equal(pretty, rem_(field_name));
958 }
959 ret = print_field(pretty, field, print_names, NULL, 0);
960 *nr_printed_fields += 1;
961 end:
962 bt_put(field_type);
963 bt_put(field);
964 return ret;
965 }
966
967 static
968 enum bt_component_status print_struct(struct pretty_component *pretty,
969 struct bt_ctf_field *_struct, bool print_names,
970 GQuark *filter_fields, int filter_array_len)
971 {
972 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
973 struct bt_ctf_field_type *struct_type = NULL;
974 int nr_fields, i, nr_printed_fields;
975
976 struct_type = bt_ctf_field_get_type(_struct);
977 if (!struct_type) {
978 ret = BT_COMPONENT_STATUS_ERROR;
979 goto end;
980 }
981 nr_fields = bt_ctf_field_type_structure_get_field_count(struct_type);
982 if (nr_fields < 0) {
983 ret = BT_COMPONENT_STATUS_ERROR;
984 goto end;
985 }
986 g_string_append(pretty->string, "{");
987 pretty->depth++;
988 nr_printed_fields = 0;
989 for (i = 0; i < nr_fields; i++) {
990 ret = print_struct_field(pretty, _struct, struct_type, i,
991 print_names, &nr_printed_fields, filter_fields,
992 filter_array_len);
993 if (ret != BT_COMPONENT_STATUS_OK) {
994 goto end;
995 }
996 }
997 pretty->depth--;
998 g_string_append(pretty->string, " }");
999 end:
1000 bt_put(struct_type);
1001 return ret;
1002 }
1003
1004 static
1005 enum bt_component_status print_array_field(struct pretty_component *pretty,
1006 struct bt_ctf_field *array, uint64_t i,
1007 bool is_string, bool print_names)
1008 {
1009 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1010 struct bt_ctf_field *field = NULL;
1011
1012 if (!is_string) {
1013 if (i != 0) {
1014 g_string_append(pretty->string, ", ");
1015 } else {
1016 g_string_append(pretty->string, " ");
1017 }
1018 if (print_names) {
1019 g_string_append_printf(pretty->string, "[%" PRIu64 "] = ", i);
1020 }
1021 }
1022 field = bt_ctf_field_array_get_field(array, i);
1023 if (!field) {
1024 ret = BT_COMPONENT_STATUS_ERROR;
1025 goto end;
1026 }
1027 ret = print_field(pretty, field, print_names, NULL, 0);
1028 end:
1029 bt_put(field);
1030 return ret;
1031 }
1032
1033 static
1034 enum bt_component_status print_array(struct pretty_component *pretty,
1035 struct bt_ctf_field *array, bool print_names)
1036 {
1037 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1038 struct bt_ctf_field_type *array_type = NULL, *field_type = NULL;
1039 enum bt_ctf_field_type_id type_id;
1040 int64_t len;
1041 uint64_t i;
1042 bool is_string = false;
1043
1044 array_type = bt_ctf_field_get_type(array);
1045 if (!array_type) {
1046 ret = BT_COMPONENT_STATUS_ERROR;
1047 goto end;
1048 }
1049 field_type = bt_ctf_field_type_array_get_element_type(array_type);
1050 if (!field_type) {
1051 ret = BT_COMPONENT_STATUS_ERROR;
1052 goto end;
1053 }
1054 len = bt_ctf_field_type_array_get_length(array_type);
1055 if (len < 0) {
1056 ret = BT_COMPONENT_STATUS_ERROR;
1057 goto end;
1058 }
1059 type_id = bt_ctf_field_type_get_type_id(field_type);
1060 if (type_id == BT_CTF_FIELD_TYPE_ID_INTEGER) {
1061 enum bt_ctf_string_encoding encoding;
1062
1063 encoding = bt_ctf_field_type_integer_get_encoding(field_type);
1064 if (encoding == BT_CTF_STRING_ENCODING_UTF8
1065 || encoding == BT_CTF_STRING_ENCODING_ASCII) {
1066 int integer_len, integer_alignment;
1067
1068 integer_len = bt_ctf_field_type_integer_get_size(field_type);
1069 if (integer_len < 0) {
1070 return BT_COMPONENT_STATUS_ERROR;
1071 }
1072 integer_alignment = bt_ctf_field_type_get_alignment(field_type);
1073 if (integer_alignment < 0) {
1074 return BT_COMPONENT_STATUS_ERROR;
1075 }
1076 if (integer_len == CHAR_BIT
1077 && integer_alignment == CHAR_BIT) {
1078 is_string = true;
1079 }
1080 }
1081 }
1082
1083 if (is_string) {
1084 g_string_assign(pretty->tmp_string, "");
1085 } else {
1086 g_string_append(pretty->string, "[");
1087 }
1088
1089 pretty->depth++;
1090 for (i = 0; i < len; i++) {
1091 ret = print_array_field(pretty, array, i, is_string, print_names);
1092 if (ret != BT_COMPONENT_STATUS_OK) {
1093 goto end;
1094 }
1095 }
1096 pretty->depth--;
1097
1098 if (is_string) {
1099 if (pretty->use_colors) {
1100 g_string_append(pretty->string, COLOR_STRING_VALUE);
1101 }
1102 print_escape_string(pretty, pretty->tmp_string->str);
1103 if (pretty->use_colors) {
1104 g_string_append(pretty->string, COLOR_RST);
1105 }
1106 } else {
1107 g_string_append(pretty->string, " ]");
1108 }
1109 end:
1110 bt_put(field_type);
1111 bt_put(array_type);
1112 return ret;
1113 }
1114
1115 static
1116 enum bt_component_status print_sequence_field(struct pretty_component *pretty,
1117 struct bt_ctf_field *seq, uint64_t i,
1118 bool is_string, bool print_names)
1119 {
1120 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1121 struct bt_ctf_field *field = NULL;
1122
1123 if (!is_string) {
1124 if (i != 0) {
1125 g_string_append(pretty->string, ", ");
1126 } else {
1127 g_string_append(pretty->string, " ");
1128 }
1129 if (print_names) {
1130 g_string_append_printf(pretty->string, "[%" PRIu64 "] = ", i);
1131 }
1132 }
1133 field = bt_ctf_field_sequence_get_field(seq, i);
1134 if (!field) {
1135 ret = BT_COMPONENT_STATUS_ERROR;
1136 goto end;
1137 }
1138 ret = print_field(pretty, field, print_names, NULL, 0);
1139 end:
1140 bt_put(field);
1141 return ret;
1142 }
1143
1144 static
1145 enum bt_component_status print_sequence(struct pretty_component *pretty,
1146 struct bt_ctf_field *seq, bool print_names)
1147 {
1148 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1149 struct bt_ctf_field_type *seq_type = NULL, *field_type = NULL;
1150 struct bt_ctf_field *length_field = NULL;
1151 enum bt_ctf_field_type_id type_id;
1152 uint64_t len;
1153 uint64_t i;
1154 bool is_string = false;
1155
1156 seq_type = bt_ctf_field_get_type(seq);
1157 if (!seq_type) {
1158 ret = BT_COMPONENT_STATUS_ERROR;
1159 goto end;
1160 }
1161 length_field = bt_ctf_field_sequence_get_length(seq);
1162 if (!length_field) {
1163 ret = BT_COMPONENT_STATUS_ERROR;
1164 goto end;
1165 }
1166 if (bt_ctf_field_unsigned_integer_get_value(length_field, &len) < 0) {
1167 ret = BT_COMPONENT_STATUS_ERROR;
1168 goto end;
1169 }
1170 field_type = bt_ctf_field_type_sequence_get_element_type(seq_type);
1171 if (!field_type) {
1172 ret = BT_COMPONENT_STATUS_ERROR;
1173 goto end;
1174 }
1175 type_id = bt_ctf_field_type_get_type_id(field_type);
1176 if (type_id == BT_CTF_FIELD_TYPE_ID_INTEGER) {
1177 enum bt_ctf_string_encoding encoding;
1178
1179 encoding = bt_ctf_field_type_integer_get_encoding(field_type);
1180 if (encoding == BT_CTF_STRING_ENCODING_UTF8
1181 || encoding == BT_CTF_STRING_ENCODING_ASCII) {
1182 int integer_len, integer_alignment;
1183
1184 integer_len = bt_ctf_field_type_integer_get_size(field_type);
1185 if (integer_len < 0) {
1186 ret = BT_COMPONENT_STATUS_ERROR;
1187 goto end;
1188 }
1189 integer_alignment = bt_ctf_field_type_get_alignment(field_type);
1190 if (integer_alignment < 0) {
1191 ret = BT_COMPONENT_STATUS_ERROR;
1192 goto end;
1193 }
1194 if (integer_len == CHAR_BIT
1195 && integer_alignment == CHAR_BIT) {
1196 is_string = true;
1197 }
1198 }
1199 }
1200
1201 if (is_string) {
1202 g_string_assign(pretty->tmp_string, "");
1203 } else {
1204 g_string_append(pretty->string, "[");
1205 }
1206
1207 pretty->depth++;
1208 for (i = 0; i < len; i++) {
1209 ret = print_sequence_field(pretty, seq, i,
1210 is_string, print_names);
1211 if (ret != BT_COMPONENT_STATUS_OK) {
1212 goto end;
1213 }
1214 }
1215 pretty->depth--;
1216
1217 if (is_string) {
1218 if (pretty->use_colors) {
1219 g_string_append(pretty->string, COLOR_STRING_VALUE);
1220 }
1221 print_escape_string(pretty, pretty->tmp_string->str);
1222 if (pretty->use_colors) {
1223 g_string_append(pretty->string, COLOR_RST);
1224 }
1225 } else {
1226 g_string_append(pretty->string, " ]");
1227 }
1228 end:
1229 bt_put(length_field);
1230 bt_put(field_type);
1231 bt_put(seq_type);
1232 return ret;
1233 }
1234
1235 static
1236 enum bt_component_status print_variant(struct pretty_component *pretty,
1237 struct bt_ctf_field *variant, bool print_names)
1238 {
1239 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1240 struct bt_ctf_field *field = NULL;
1241
1242 field = bt_ctf_field_variant_get_current_field(variant);
1243 if (!field) {
1244 ret = BT_COMPONENT_STATUS_ERROR;
1245 goto end;
1246 }
1247 g_string_append(pretty->string, "{ ");
1248 pretty->depth++;
1249 if (print_names) {
1250 int iter_ret;
1251 struct bt_ctf_field *tag_field = NULL;
1252 const char *tag_choice;
1253 struct bt_ctf_field_type_enumeration_mapping_iterator *iter;
1254
1255 tag_field = bt_ctf_field_variant_get_tag(variant);
1256 if (!tag_field) {
1257 ret = BT_COMPONENT_STATUS_ERROR;
1258 goto end;
1259 }
1260
1261 iter = bt_ctf_field_enumeration_get_mappings(tag_field);
1262 if (!iter) {
1263 bt_put(tag_field);
1264 ret = BT_COMPONENT_STATUS_ERROR;
1265 goto end;
1266 }
1267
1268 iter_ret =
1269 bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
1270 iter, &tag_choice, NULL, NULL);
1271 if (iter_ret) {
1272 bt_put(iter);
1273 bt_put(tag_field);
1274 ret = BT_COMPONENT_STATUS_ERROR;
1275 goto end;
1276 }
1277 print_field_name_equal(pretty, rem_(tag_choice));
1278 bt_put(tag_field);
1279 bt_put(iter);
1280 }
1281 ret = print_field(pretty, field, print_names, NULL, 0);
1282 if (ret != BT_COMPONENT_STATUS_OK) {
1283 goto end;
1284 }
1285 pretty->depth--;
1286 g_string_append(pretty->string, " }");
1287 end:
1288 bt_put(field);
1289 return ret;
1290 }
1291
1292 static
1293 enum bt_component_status print_field(struct pretty_component *pretty,
1294 struct bt_ctf_field *field, bool print_names,
1295 GQuark *filter_fields, int filter_array_len)
1296 {
1297 enum bt_ctf_field_type_id type_id;
1298
1299 type_id = bt_ctf_field_get_type_id(field);
1300 switch (type_id) {
1301 case CTF_TYPE_INTEGER:
1302 return print_integer(pretty, field);
1303 case CTF_TYPE_FLOAT:
1304 {
1305 double v;
1306
1307 if (bt_ctf_field_floating_point_get_value(field, &v)) {
1308 return BT_COMPONENT_STATUS_ERROR;
1309 }
1310 if (pretty->use_colors) {
1311 g_string_append(pretty->string, COLOR_NUMBER_VALUE);
1312 }
1313 g_string_append_printf(pretty->string, "%g", v);
1314 if (pretty->use_colors) {
1315 g_string_append(pretty->string, COLOR_RST);
1316 }
1317 return BT_COMPONENT_STATUS_OK;
1318 }
1319 case CTF_TYPE_ENUM:
1320 return print_enum(pretty, field);
1321 case CTF_TYPE_STRING:
1322 if (pretty->use_colors) {
1323 g_string_append(pretty->string, COLOR_STRING_VALUE);
1324 }
1325 print_escape_string(pretty, bt_ctf_field_string_get_value(field));
1326 if (pretty->use_colors) {
1327 g_string_append(pretty->string, COLOR_RST);
1328 }
1329 return BT_COMPONENT_STATUS_OK;
1330 case CTF_TYPE_STRUCT:
1331 return print_struct(pretty, field, print_names, filter_fields,
1332 filter_array_len);
1333 case CTF_TYPE_VARIANT:
1334 return print_variant(pretty, field, print_names);
1335 case CTF_TYPE_ARRAY:
1336 return print_array(pretty, field, print_names);
1337 case CTF_TYPE_SEQUENCE:
1338 return print_sequence(pretty, field, print_names);
1339 default:
1340 // TODO: log instead
1341 fprintf(pretty->err, "[error] Unknown type id: %d\n", (int) type_id);
1342 return BT_COMPONENT_STATUS_ERROR;
1343 }
1344 }
1345
1346 static
1347 enum bt_component_status print_stream_packet_context(struct pretty_component *pretty,
1348 struct bt_ctf_event *event)
1349 {
1350 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1351 struct bt_ctf_packet *packet = NULL;
1352 struct bt_ctf_field *main_field = NULL;
1353
1354 packet = bt_ctf_event_get_packet(event);
1355 if (!packet) {
1356 ret = BT_COMPONENT_STATUS_ERROR;
1357 goto end;
1358 }
1359 main_field = bt_ctf_packet_get_context(packet);
1360 if (!main_field) {
1361 goto end;
1362 }
1363 if (!pretty->start_line) {
1364 g_string_append(pretty->string, ", ");
1365 }
1366 pretty->start_line = false;
1367 if (pretty->options.print_scope_field_names) {
1368 print_name_equal(pretty, "stream.packet.context");
1369 }
1370 ret = print_field(pretty, main_field,
1371 pretty->options.print_context_field_names,
1372 stream_packet_context_quarks,
1373 STREAM_PACKET_CONTEXT_QUARKS_LEN);
1374 end:
1375 bt_put(main_field);
1376 bt_put(packet);
1377 return ret;
1378 }
1379
1380 static
1381 enum bt_component_status print_event_header_raw(struct pretty_component *pretty,
1382 struct bt_ctf_event *event)
1383 {
1384 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1385 struct bt_ctf_field *main_field = NULL;
1386
1387 main_field = bt_ctf_event_get_header(event);
1388 if (!main_field) {
1389 goto end;
1390 }
1391 if (!pretty->start_line) {
1392 g_string_append(pretty->string, ", ");
1393 }
1394 pretty->start_line = false;
1395 if (pretty->options.print_scope_field_names) {
1396 print_name_equal(pretty, "stream.event.header");
1397 }
1398 ret = print_field(pretty, main_field,
1399 pretty->options.print_header_field_names, NULL, 0);
1400 end:
1401 bt_put(main_field);
1402 return ret;
1403 }
1404
1405 static
1406 enum bt_component_status print_stream_event_context(struct pretty_component *pretty,
1407 struct bt_ctf_event *event)
1408 {
1409 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1410 struct bt_ctf_field *main_field = NULL;
1411
1412 main_field = bt_ctf_event_get_stream_event_context(event);
1413 if (!main_field) {
1414 goto end;
1415 }
1416 if (!pretty->start_line) {
1417 g_string_append(pretty->string, ", ");
1418 }
1419 pretty->start_line = false;
1420 if (pretty->options.print_scope_field_names) {
1421 print_name_equal(pretty, "stream.event.context");
1422 }
1423 ret = print_field(pretty, main_field,
1424 pretty->options.print_context_field_names, NULL, 0);
1425 end:
1426 bt_put(main_field);
1427 return ret;
1428 }
1429
1430 static
1431 enum bt_component_status print_event_context(struct pretty_component *pretty,
1432 struct bt_ctf_event *event)
1433 {
1434 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1435 struct bt_ctf_field *main_field = NULL;
1436
1437 main_field = bt_ctf_event_get_event_context(event);
1438 if (!main_field) {
1439 goto end;
1440 }
1441 if (!pretty->start_line) {
1442 g_string_append(pretty->string, ", ");
1443 }
1444 pretty->start_line = false;
1445 if (pretty->options.print_scope_field_names) {
1446 print_name_equal(pretty, "event.context");
1447 }
1448 ret = print_field(pretty, main_field,
1449 pretty->options.print_context_field_names, NULL, 0);
1450 end:
1451 bt_put(main_field);
1452 return ret;
1453 }
1454
1455 static
1456 enum bt_component_status print_event_payload(struct pretty_component *pretty,
1457 struct bt_ctf_event *event)
1458 {
1459 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1460 struct bt_ctf_field *main_field = NULL;
1461
1462 main_field = bt_ctf_event_get_event_payload(event);
1463 if (!main_field) {
1464 goto end;
1465 }
1466 if (!pretty->start_line) {
1467 g_string_append(pretty->string, ", ");
1468 }
1469 pretty->start_line = false;
1470 if (pretty->options.print_scope_field_names) {
1471 print_name_equal(pretty, "event.fields");
1472 }
1473 ret = print_field(pretty, main_field,
1474 pretty->options.print_payload_field_names, NULL, 0);
1475 end:
1476 bt_put(main_field);
1477 return ret;
1478 }
1479
1480 BT_HIDDEN
1481 enum bt_component_status pretty_print_event(struct pretty_component *pretty,
1482 struct bt_notification *event_notif)
1483 {
1484 enum bt_component_status ret;
1485 struct bt_ctf_event *event =
1486 bt_notification_event_get_event(event_notif);
1487 struct bt_clock_class_priority_map *cc_prio_map =
1488 bt_notification_event_get_clock_class_priority_map(event_notif);
1489
1490 assert(event);
1491 assert(cc_prio_map);
1492 pretty->start_line = true;
1493 g_string_assign(pretty->string, "");
1494 ret = print_event_header(pretty, event, cc_prio_map);
1495 if (ret != BT_COMPONENT_STATUS_OK) {
1496 goto end;
1497 }
1498
1499 ret = print_stream_packet_context(pretty, event);
1500 if (ret != BT_COMPONENT_STATUS_OK) {
1501 goto end;
1502 }
1503
1504 if (pretty->options.verbose) {
1505 ret = print_event_header_raw(pretty, event);
1506 if (ret != BT_COMPONENT_STATUS_OK) {
1507 goto end;
1508 }
1509 }
1510
1511 ret = print_stream_event_context(pretty, event);
1512 if (ret != BT_COMPONENT_STATUS_OK) {
1513 goto end;
1514 }
1515
1516 ret = print_event_context(pretty, event);
1517 if (ret != BT_COMPONENT_STATUS_OK) {
1518 goto end;
1519 }
1520
1521 ret = print_event_payload(pretty, event);
1522 if (ret != BT_COMPONENT_STATUS_OK) {
1523 goto end;
1524 }
1525
1526 g_string_append_c(pretty->string, '\n');
1527 if (fwrite(pretty->string->str, pretty->string->len, 1, pretty->out) != 1) {
1528 ret = BT_COMPONENT_STATUS_ERROR;
1529 goto end;
1530 }
1531
1532 end:
1533 bt_put(event);
1534 bt_put(cc_prio_map);
1535 return ret;
1536 }
This page took 0.069362 seconds and 5 git commands to generate.