Move to kernel style SPDX license identifiers
[babeltrace.git] / src / plugins / text / pretty / print.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 */
7
8 #include <babeltrace2/babeltrace.h>
9 #include "compat/bitfield.h"
10 #include "common/common.h"
11 #include "common/uuid.h"
12 #include "compat/time.h"
13 #include "common/assert.h"
14 #include <inttypes.h>
15 #include <ctype.h>
16 #include <stdbool.h>
17 #include <string.h>
18 #include "pretty.h"
19
20 #define NSEC_PER_SEC 1000000000LL
21
22 static char color_name[32];
23 static char color_field_name[32];
24 static char color_rst[32];
25 static char color_string_value[32];
26 static char color_number_value[32];
27 static char color_enum_mapping_name[32];
28 static char color_unknown[32];
29 static char color_event_name[32];
30 static char color_timestamp[32];
31
32 struct timestamp {
33 int64_t real_timestamp; /* Relative to UNIX epoch. */
34 uint64_t clock_snapshot; /* In cycles. */
35 };
36
37 static
38 int print_field(struct pretty_component *pretty,
39 const bt_field *field, bool print_names);
40
41 static
42 void print_name_equal(struct pretty_component *pretty, const char *name)
43 {
44 if (pretty->use_colors) {
45 bt_common_g_string_append(pretty->string, color_name);
46 bt_common_g_string_append(pretty->string, name);
47 bt_common_g_string_append(pretty->string, color_rst);
48 } else {
49 bt_common_g_string_append(pretty->string, name);
50 }
51 bt_common_g_string_append(pretty->string, " = ");
52 }
53
54 static
55 void print_field_name_equal(struct pretty_component *pretty, const char *name)
56 {
57 if (pretty->use_colors) {
58 bt_common_g_string_append(pretty->string, color_field_name);
59 bt_common_g_string_append(pretty->string, name);
60 bt_common_g_string_append(pretty->string, color_rst);
61 } else {
62 bt_common_g_string_append(pretty->string, name);
63 }
64 bt_common_g_string_append(pretty->string, " = ");
65 }
66
67 static
68 void print_timestamp_cycles(struct pretty_component *pretty,
69 const bt_clock_snapshot *clock_snapshot, bool update_last)
70 {
71 uint64_t cycles;
72
73 cycles = bt_clock_snapshot_get_value(clock_snapshot);
74 bt_common_g_string_append_printf(pretty->string, "%020" PRIu64, cycles);
75
76 if (update_last) {
77 if (pretty->last_cycles_timestamp != -1ULL) {
78 pretty->delta_cycles = cycles - pretty->last_cycles_timestamp;
79 }
80
81 pretty->last_cycles_timestamp = cycles;
82 }
83 }
84
85 static
86 void print_timestamp_wall(struct pretty_component *pretty,
87 const bt_clock_snapshot *clock_snapshot, bool update_last)
88 {
89 int ret;
90 int64_t ts_nsec = 0; /* add configurable offset */
91 int64_t ts_sec = 0; /* add configurable offset */
92 uint64_t ts_sec_abs, ts_nsec_abs;
93 bool is_negative;
94
95 if (!clock_snapshot) {
96 bt_common_g_string_append(pretty->string, "??:??:??.?????????");
97 return;
98 }
99
100 ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, &ts_nsec);
101 if (ret) {
102 // TODO: log, this is unexpected
103 bt_common_g_string_append(pretty->string, "Error");
104 return;
105 }
106
107 if (update_last) {
108 if (pretty->last_real_timestamp != -1ULL) {
109 pretty->delta_real_timestamp = ts_nsec - pretty->last_real_timestamp;
110 }
111
112 pretty->last_real_timestamp = ts_nsec;
113 }
114
115 ts_sec += ts_nsec / NSEC_PER_SEC;
116 ts_nsec = ts_nsec % NSEC_PER_SEC;
117
118 if (ts_sec >= 0 && ts_nsec >= 0) {
119 is_negative = false;
120 ts_sec_abs = ts_sec;
121 ts_nsec_abs = ts_nsec;
122 } else if (ts_sec > 0 && ts_nsec < 0) {
123 is_negative = false;
124 ts_sec_abs = ts_sec - 1;
125 ts_nsec_abs = NSEC_PER_SEC + ts_nsec;
126 } else if (ts_sec == 0 && ts_nsec < 0) {
127 is_negative = true;
128 ts_sec_abs = ts_sec;
129 ts_nsec_abs = -ts_nsec;
130 } else if (ts_sec < 0 && ts_nsec > 0) {
131 is_negative = true;
132 ts_sec_abs = -(ts_sec + 1);
133 ts_nsec_abs = NSEC_PER_SEC - ts_nsec;
134 } else if (ts_sec < 0 && ts_nsec == 0) {
135 is_negative = true;
136 ts_sec_abs = -ts_sec;
137 ts_nsec_abs = ts_nsec;
138 } else { /* (ts_sec < 0 && ts_nsec < 0) */
139 is_negative = true;
140 ts_sec_abs = -ts_sec;
141 ts_nsec_abs = -ts_nsec;
142 }
143
144 if (!pretty->options.clock_seconds) {
145 struct tm tm;
146 time_t time_s = (time_t) ts_sec_abs;
147
148 if (is_negative && !pretty->negative_timestamp_warning_done) {
149 // TODO: log instead
150 fprintf(stderr, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
151 pretty->negative_timestamp_warning_done = true;
152 goto seconds;
153 }
154
155 if (!pretty->options.clock_gmt) {
156 struct tm *res;
157
158 res = bt_localtime_r(&time_s, &tm);
159 if (!res) {
160 // TODO: log instead
161 fprintf(stderr, "[warning] Unable to get localtime.\n");
162 goto seconds;
163 }
164 } else {
165 struct tm *res;
166
167 res = bt_gmtime_r(&time_s, &tm);
168 if (!res) {
169 // TODO: log instead
170 fprintf(stderr, "[warning] Unable to get gmtime.\n");
171 goto seconds;
172 }
173 }
174 if (pretty->options.clock_date) {
175 char timestr[26];
176 size_t res;
177
178 /* Print date and time */
179 res = strftime(timestr, sizeof(timestr),
180 "%Y-%m-%d ", &tm);
181 if (!res) {
182 // TODO: log instead
183 fprintf(stderr, "[warning] Unable to print ascii time.\n");
184 goto seconds;
185 }
186
187 bt_common_g_string_append(pretty->string, timestr);
188 }
189
190 /* Print time in HH:MM:SS.ns */
191 bt_common_g_string_append_printf(pretty->string,
192 "%02d:%02d:%02d.%09" PRIu64, tm.tm_hour, tm.tm_min,
193 tm.tm_sec, ts_nsec_abs);
194 goto end;
195 }
196 seconds:
197 bt_common_g_string_append_printf(pretty->string, "%s%" PRId64 ".%09" PRIu64,
198 is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs);
199 end:
200 return;
201 }
202
203 static
204 int print_event_timestamp(struct pretty_component *pretty,
205 const bt_message *event_msg, bool *start_line)
206 {
207 bool print_names = pretty->options.print_header_field_names;
208 int ret = 0;
209 const bt_clock_snapshot *clock_snapshot = NULL;
210
211 if (!bt_message_event_borrow_stream_class_default_clock_class_const(
212 event_msg)) {
213 /* No default clock class: skip the timestamp without an error */
214 goto end;
215 }
216
217 clock_snapshot = bt_message_event_borrow_default_clock_snapshot_const(event_msg);
218
219 if (print_names) {
220 print_name_equal(pretty, "timestamp");
221 } else {
222 bt_common_g_string_append(pretty->string, "[");
223 }
224 if (pretty->use_colors) {
225 bt_common_g_string_append(pretty->string, color_timestamp);
226 }
227 if (pretty->options.print_timestamp_cycles) {
228 print_timestamp_cycles(pretty, clock_snapshot, true);
229 } else {
230 print_timestamp_wall(pretty, clock_snapshot, true);
231 }
232 if (pretty->use_colors) {
233 bt_common_g_string_append(pretty->string, color_rst);
234 }
235
236 if (!print_names)
237 bt_common_g_string_append(pretty->string, "] ");
238
239 if (pretty->options.print_delta_field) {
240 if (print_names) {
241 bt_common_g_string_append(pretty->string, ", ");
242 print_name_equal(pretty, "delta");
243 } else {
244 bt_common_g_string_append(pretty->string, "(");
245 }
246 if (pretty->options.print_timestamp_cycles) {
247 if (pretty->delta_cycles == -1ULL) {
248 bt_common_g_string_append(pretty->string,
249 "+??????????\?\?"); /* Not a trigraph. */
250 } else {
251 bt_common_g_string_append_printf(pretty->string,
252 "+%012" PRIu64, pretty->delta_cycles);
253 }
254 } else {
255 if (pretty->delta_real_timestamp != -1ULL) {
256 uint64_t delta_sec, delta_nsec, delta;
257
258 delta = pretty->delta_real_timestamp;
259 delta_sec = delta / NSEC_PER_SEC;
260 delta_nsec = delta % NSEC_PER_SEC;
261 bt_common_g_string_append_printf(pretty->string,
262 "+%" PRIu64 ".%09" PRIu64,
263 delta_sec, delta_nsec);
264 } else {
265 bt_common_g_string_append(pretty->string, "+?.?????????");
266 }
267 }
268 if (!print_names) {
269 bt_common_g_string_append(pretty->string, ") ");
270 }
271 }
272 *start_line = !print_names;
273
274 end:
275 return ret;
276 }
277
278 static
279 int print_event_header(struct pretty_component *pretty,
280 const bt_message *event_msg)
281 {
282 bool print_names = pretty->options.print_header_field_names;
283 int ret = 0;
284 const bt_event_class *event_class = NULL;
285 const bt_stream *stream = NULL;
286 const bt_trace *trace = NULL;
287 const bt_event *event = bt_message_event_borrow_event_const(event_msg);
288 const char *ev_name;
289 int dom_print = 0;
290 bt_property_availability prop_avail;
291
292 event_class = bt_event_borrow_class_const(event);
293 stream = bt_event_borrow_stream_const(event);
294 trace = bt_stream_borrow_trace_const(stream);
295 ret = print_event_timestamp(pretty, event_msg, &pretty->start_line);
296 if (ret) {
297 goto end;
298 }
299 if (pretty->options.print_trace_field) {
300 const char *name;
301
302 name = bt_trace_get_name(trace);
303 if (name) {
304 if (!pretty->start_line) {
305 bt_common_g_string_append(pretty->string, ", ");
306 }
307 if (print_names) {
308 print_name_equal(pretty, "trace");
309 }
310
311 bt_common_g_string_append(pretty->string, name);
312
313 if (print_names) {
314 bt_common_g_string_append(pretty->string, ", ");
315 }
316 }
317 }
318 if (pretty->options.print_trace_hostname_field) {
319 const bt_value *hostname_str;
320
321 hostname_str = bt_trace_borrow_environment_entry_value_by_name_const(
322 trace, "hostname");
323 if (hostname_str) {
324 const char *str;
325
326 if (!pretty->start_line) {
327 bt_common_g_string_append(pretty->string, ", ");
328 }
329 if (print_names) {
330 print_name_equal(pretty, "trace:hostname");
331 }
332 str = bt_value_string_get(hostname_str);
333 bt_common_g_string_append(pretty->string, str);
334 dom_print = 1;
335 }
336 }
337 if (pretty->options.print_trace_domain_field) {
338 const bt_value *domain_str;
339
340 domain_str = bt_trace_borrow_environment_entry_value_by_name_const(
341 trace, "domain");
342 if (domain_str) {
343 const char *str;
344
345 if (!pretty->start_line) {
346 bt_common_g_string_append(pretty->string, ", ");
347 }
348 if (print_names) {
349 print_name_equal(pretty, "trace:domain");
350 } else if (dom_print) {
351 bt_common_g_string_append(pretty->string, ":");
352 }
353 str = bt_value_string_get(domain_str);
354 bt_common_g_string_append(pretty->string, str);
355 dom_print = 1;
356 }
357 }
358 if (pretty->options.print_trace_procname_field) {
359 const bt_value *procname_str;
360
361 procname_str = bt_trace_borrow_environment_entry_value_by_name_const(
362 trace, "procname");
363 if (procname_str) {
364 const char *str;
365
366 if (!pretty->start_line) {
367 bt_common_g_string_append(pretty->string, ", ");
368 }
369 if (print_names) {
370 print_name_equal(pretty, "trace:procname");
371 } else if (dom_print) {
372 bt_common_g_string_append(pretty->string, ":");
373 }
374 str = bt_value_string_get(procname_str);
375 bt_common_g_string_append(pretty->string, str);
376 dom_print = 1;
377 }
378 }
379 if (pretty->options.print_trace_vpid_field) {
380 const bt_value *vpid_value;
381
382 vpid_value = bt_trace_borrow_environment_entry_value_by_name_const(
383 trace, "vpid");
384 if (vpid_value) {
385 int64_t value;
386
387 if (!pretty->start_line) {
388 bt_common_g_string_append(pretty->string, ", ");
389 }
390 if (print_names) {
391 print_name_equal(pretty, "trace:vpid");
392 } else if (dom_print) {
393 bt_common_g_string_append(pretty->string, ":");
394 }
395 value = bt_value_integer_signed_get(vpid_value);
396 bt_common_g_string_append_printf(pretty->string,
397 "(%" PRId64 ")", value);
398 dom_print = 1;
399 }
400 }
401 if (pretty->options.print_loglevel_field) {
402 static const char *log_level_names[] = {
403 [ BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY ] = "TRACE_EMERG",
404 [ BT_EVENT_CLASS_LOG_LEVEL_ALERT ] = "TRACE_ALERT",
405 [ BT_EVENT_CLASS_LOG_LEVEL_CRITICAL ] = "TRACE_CRIT",
406 [ BT_EVENT_CLASS_LOG_LEVEL_ERROR ] = "TRACE_ERR",
407 [ BT_EVENT_CLASS_LOG_LEVEL_WARNING ] = "TRACE_WARNING",
408 [ BT_EVENT_CLASS_LOG_LEVEL_NOTICE ] = "TRACE_NOTICE",
409 [ BT_EVENT_CLASS_LOG_LEVEL_INFO ] = "TRACE_INFO",
410 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM ] = "TRACE_DEBUG_SYSTEM",
411 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM ] = "TRACE_DEBUG_PROGRAM",
412 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS ] = "TRACE_DEBUG_PROCESS",
413 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE ] = "TRACE_DEBUG_MODULE",
414 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT ] = "TRACE_DEBUG_UNIT",
415 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION ] = "TRACE_DEBUG_FUNCTION",
416 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE ] = "TRACE_DEBUG_LINE",
417 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG ] = "TRACE_DEBUG",
418 };
419 bt_event_class_log_level log_level;
420 const char *log_level_str = NULL;
421
422 prop_avail = bt_event_class_get_log_level(event_class,
423 &log_level);
424 if (prop_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) {
425 log_level_str = log_level_names[log_level];
426 BT_ASSERT_DBG(log_level_str);
427
428 if (!pretty->start_line) {
429 bt_common_g_string_append(pretty->string, ", ");
430 }
431 if (print_names) {
432 print_name_equal(pretty, "loglevel");
433 } else if (dom_print) {
434 bt_common_g_string_append(pretty->string, ":");
435 }
436
437 bt_common_g_string_append(pretty->string, log_level_str);
438 bt_common_g_string_append_printf(
439 pretty->string, " (%d)", (int) log_level);
440 dom_print = 1;
441 }
442 }
443 if (pretty->options.print_emf_field) {
444 const char *uri_str;
445
446 uri_str = bt_event_class_get_emf_uri(event_class);
447 if (uri_str) {
448 if (!pretty->start_line) {
449 bt_common_g_string_append(pretty->string, ", ");
450 }
451 if (print_names) {
452 print_name_equal(pretty, "model.emf.uri");
453 } else if (dom_print) {
454 bt_common_g_string_append(pretty->string, ":");
455 }
456
457 bt_common_g_string_append(pretty->string, uri_str);
458 dom_print = 1;
459 }
460 }
461 if (dom_print && !print_names) {
462 bt_common_g_string_append(pretty->string, " ");
463 }
464 if (!pretty->start_line) {
465 bt_common_g_string_append(pretty->string, ", ");
466 }
467 pretty->start_line = true;
468 if (print_names) {
469 print_name_equal(pretty, "name");
470 }
471 ev_name = bt_event_class_get_name(event_class);
472 if (pretty->use_colors) {
473 if (ev_name) {
474 bt_common_g_string_append(pretty->string,
475 color_event_name);
476 } else {
477 bt_common_g_string_append(pretty->string,
478 color_unknown);
479 }
480 }
481 if (ev_name) {
482 bt_common_g_string_append(pretty->string, ev_name);
483 } else {
484 bt_common_g_string_append(pretty->string, "<unknown>");
485 }
486 if (pretty->use_colors) {
487 bt_common_g_string_append(pretty->string, color_rst);
488 }
489 if (!print_names) {
490 bt_common_g_string_append(pretty->string, ": ");
491 } else {
492 bt_common_g_string_append(pretty->string, ", ");
493 }
494
495 end:
496 return ret;
497 }
498
499 static
500 int print_integer(struct pretty_component *pretty,
501 const bt_field *field)
502 {
503 int ret = 0;
504 bt_field_class_integer_preferred_display_base base;
505 const bt_field_class *int_fc;
506 union {
507 uint64_t u;
508 int64_t s;
509 } v;
510 bool rst_color = false;
511 bt_field_class_type ft_type;
512
513 int_fc = bt_field_borrow_class_const(field);
514 BT_ASSERT_DBG(int_fc);
515 ft_type = bt_field_get_class_type(field);
516 if (bt_field_class_type_is(ft_type,
517 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER)) {
518 v.u = bt_field_integer_unsigned_get_value(field);
519 } else {
520 v.s = bt_field_integer_signed_get_value(field);
521 }
522
523 if (pretty->use_colors) {
524 bt_common_g_string_append(pretty->string, color_number_value);
525 rst_color = true;
526 }
527
528 base = bt_field_class_integer_get_preferred_display_base(int_fc);
529 switch (base) {
530 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY:
531 {
532 int bitnr, len;
533
534 len = bt_field_class_integer_get_field_value_range(int_fc);
535 bt_common_g_string_append(pretty->string, "0b");
536 _bt_safe_lshift(v.u, 64 - len);
537 for (bitnr = 0; bitnr < len; bitnr++) {
538 bt_common_g_string_append_c(pretty->string,
539 (v.u & (1ULL << 63)) ? '1' : '0');
540 _bt_safe_lshift(v.u, 1);
541 }
542 break;
543 }
544 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL:
545 {
546 if (bt_field_class_type_is(ft_type,
547 BT_FIELD_CLASS_TYPE_SIGNED_INTEGER)) {
548 int len;
549
550 len = bt_field_class_integer_get_field_value_range(
551 int_fc);
552 if (len < 64) {
553 size_t rounded_len;
554
555 BT_ASSERT_DBG(len != 0);
556 /* Round length to the nearest 3-bit */
557 rounded_len = (((len - 1) / 3) + 1) * 3;
558 v.u &= ((uint64_t) 1 << rounded_len) - 1;
559 }
560 }
561
562 bt_common_g_string_append_printf(pretty->string, "0%" PRIo64, v.u);
563 break;
564 }
565 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL:
566 if (bt_field_class_type_is(ft_type,
567 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER)) {
568 bt_common_g_string_append_printf(pretty->string, "%" PRIu64, v.u);
569 } else {
570 bt_common_g_string_append_printf(pretty->string, "%" PRId64, v.s);
571 }
572 break;
573 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL:
574 {
575 int len;
576
577 len = bt_field_class_integer_get_field_value_range(int_fc);
578 if (len < 64) {
579 /* Round length to the nearest nibble */
580 uint8_t rounded_len = ((len + 3) & ~0x3);
581
582 v.u &= ((uint64_t) 1 << rounded_len) - 1;
583 }
584
585 bt_common_g_string_append_printf(pretty->string, "0x%" PRIX64, v.u);
586 break;
587 }
588 default:
589 ret = -1;
590 goto end;
591 }
592 end:
593 if (rst_color) {
594 bt_common_g_string_append(pretty->string, color_rst);
595 }
596 return ret;
597 }
598
599 static
600 void print_escape_string(struct pretty_component *pretty, const char *str)
601 {
602 int i;
603
604 bt_common_g_string_append_c(pretty->string, '"');
605
606 for (i = 0; i < strlen(str); i++) {
607 /* Escape sequences not recognized by iscntrl(). */
608 switch (str[i]) {
609 case '\\':
610 bt_common_g_string_append(pretty->string, "\\\\");
611 continue;
612 case '\'':
613 bt_common_g_string_append(pretty->string, "\\\'");
614 continue;
615 case '\"':
616 bt_common_g_string_append(pretty->string, "\\\"");
617 continue;
618 case '\?':
619 bt_common_g_string_append(pretty->string, "\\\?");
620 continue;
621 }
622
623 /* Standard characters. */
624 if (!iscntrl((unsigned char) str[i])) {
625 bt_common_g_string_append_c(pretty->string, str[i]);
626 continue;
627 }
628
629 switch (str[i]) {
630 case '\0':
631 bt_common_g_string_append(pretty->string, "\\0");
632 break;
633 case '\a':
634 bt_common_g_string_append(pretty->string, "\\a");
635 break;
636 case '\b':
637 bt_common_g_string_append(pretty->string, "\\b");
638 break;
639 case '\e':
640 bt_common_g_string_append(pretty->string, "\\e");
641 break;
642 case '\f':
643 bt_common_g_string_append(pretty->string, "\\f");
644 break;
645 case '\n':
646 bt_common_g_string_append(pretty->string, "\\n");
647 break;
648 case '\r':
649 bt_common_g_string_append(pretty->string, "\\r");
650 break;
651 case '\t':
652 bt_common_g_string_append(pretty->string, "\\t");
653 break;
654 case '\v':
655 bt_common_g_string_append(pretty->string, "\\v");
656 break;
657 default:
658 /* Unhandled control-sequence, print as hex. */
659 bt_common_g_string_append_printf(pretty->string, "\\x%02x", str[i]);
660 break;
661 }
662 }
663
664 bt_common_g_string_append_c(pretty->string, '"');
665 }
666
667 static
668 int print_enum(struct pretty_component *pretty,
669 const bt_field *field)
670 {
671 int ret = 0;
672 bt_field_class_enumeration_mapping_label_array label_array;
673 uint64_t label_count;
674 uint64_t i;
675
676 switch (bt_field_get_class_type(field)) {
677 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
678 ret = bt_field_enumeration_unsigned_get_mapping_labels(field,
679 &label_array, &label_count);
680 break;
681 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
682 ret = bt_field_enumeration_signed_get_mapping_labels(field,
683 &label_array, &label_count);
684 break;
685 default:
686 bt_common_abort();
687 }
688
689 if (ret) {
690 ret = -1;
691 goto end;
692 }
693
694 bt_common_g_string_append(pretty->string, "( ");
695 if (label_count == 0) {
696 if (pretty->use_colors) {
697 bt_common_g_string_append(pretty->string, color_unknown);
698 }
699 bt_common_g_string_append(pretty->string, "<unknown>");
700 if (pretty->use_colors) {
701 bt_common_g_string_append(pretty->string, color_rst);
702 }
703 goto skip_loop;
704 }
705 for (i = 0; i < label_count; i++) {
706 const char *mapping_name = label_array[i];
707
708 if (i != 0) {
709 bt_common_g_string_append(pretty->string, ", ");
710 }
711 if (pretty->use_colors) {
712 bt_common_g_string_append(pretty->string, color_enum_mapping_name);
713 }
714 print_escape_string(pretty, mapping_name);
715 if (pretty->use_colors) {
716 bt_common_g_string_append(pretty->string, color_rst);
717 }
718 }
719 skip_loop:
720 bt_common_g_string_append(pretty->string, " : container = ");
721 ret = print_integer(pretty, field);
722 if (ret != 0) {
723 goto end;
724 }
725 bt_common_g_string_append(pretty->string, " )");
726 end:
727 return ret;
728 }
729
730 static
731 int print_struct_field(struct pretty_component *pretty,
732 const bt_field *_struct,
733 const bt_field_class *struct_class,
734 uint64_t i, bool print_names, uint64_t *nr_printed_fields)
735 {
736 int ret = 0;
737 const char *field_name;
738 const bt_field *field = NULL;
739 const bt_field_class_structure_member *member;
740
741 field = bt_field_structure_borrow_member_field_by_index_const(_struct, i);
742 if (!field) {
743 ret = -1;
744 goto end;
745 }
746
747 member = bt_field_class_structure_borrow_member_by_index_const(
748 struct_class, i);
749 field_name = bt_field_class_structure_member_get_name(member);
750
751 if (*nr_printed_fields > 0) {
752 bt_common_g_string_append(pretty->string, ", ");
753 } else {
754 bt_common_g_string_append(pretty->string, " ");
755 }
756 if (print_names) {
757 print_field_name_equal(pretty, field_name);
758 }
759 ret = print_field(pretty, field, print_names);
760 *nr_printed_fields += 1;
761
762 end:
763 return ret;
764 }
765
766 static
767 int print_struct(struct pretty_component *pretty,
768 const bt_field *_struct, bool print_names)
769 {
770 int ret = 0;
771 const bt_field_class *struct_class = NULL;
772 uint64_t nr_fields, i, nr_printed_fields;
773
774 struct_class = bt_field_borrow_class_const(_struct);
775 nr_fields = bt_field_class_structure_get_member_count(struct_class);
776
777 bt_common_g_string_append(pretty->string, "{");
778 pretty->depth++;
779 nr_printed_fields = 0;
780 for (i = 0; i < nr_fields; i++) {
781 ret = print_struct_field(pretty, _struct, struct_class, i,
782 print_names, &nr_printed_fields);
783 if (ret != 0) {
784 goto end;
785 }
786 }
787 pretty->depth--;
788 bt_common_g_string_append(pretty->string, " }");
789
790 end:
791 return ret;
792 }
793
794 static
795 int print_array_field(struct pretty_component *pretty,
796 const bt_field *array, uint64_t i, bool print_names)
797 {
798 const bt_field *field = NULL;
799
800 if (i != 0) {
801 bt_common_g_string_append(pretty->string, ", ");
802 } else {
803 bt_common_g_string_append(pretty->string, " ");
804 }
805 if (print_names) {
806 bt_common_g_string_append_printf(pretty->string, "[%" PRIu64 "] = ", i);
807 }
808
809 field = bt_field_array_borrow_element_field_by_index_const(array, i);
810 BT_ASSERT_DBG(field);
811 return print_field(pretty, field, print_names);
812 }
813
814 static
815 int print_array(struct pretty_component *pretty,
816 const bt_field *array, bool print_names)
817 {
818 int ret = 0;
819 uint64_t len;
820 uint64_t i;
821
822 len = bt_field_array_get_length(array);
823 bt_common_g_string_append(pretty->string, "[");
824 pretty->depth++;
825 for (i = 0; i < len; i++) {
826 ret = print_array_field(pretty, array, i, print_names);
827 if (ret != 0) {
828 goto end;
829 }
830 }
831 pretty->depth--;
832 bt_common_g_string_append(pretty->string, " ]");
833
834 end:
835 return ret;
836 }
837
838 static
839 int print_sequence_field(struct pretty_component *pretty,
840 const bt_field *seq, uint64_t i, bool print_names)
841 {
842 const bt_field *field = NULL;
843
844 if (i != 0) {
845 bt_common_g_string_append(pretty->string, ", ");
846 } else {
847 bt_common_g_string_append(pretty->string, " ");
848 }
849 if (print_names) {
850 bt_common_g_string_append_printf(pretty->string, "[%" PRIu64 "] = ", i);
851 }
852
853 field = bt_field_array_borrow_element_field_by_index_const(seq, i);
854 BT_ASSERT_DBG(field);
855 return print_field(pretty, field, print_names);
856 }
857
858 static
859 int print_sequence(struct pretty_component *pretty,
860 const bt_field *seq, bool print_names)
861 {
862 int ret = 0;
863 uint64_t len;
864 uint64_t i;
865
866 len = bt_field_array_get_length(seq);
867 bt_common_g_string_append(pretty->string, "[");
868
869 pretty->depth++;
870 for (i = 0; i < len; i++) {
871 ret = print_sequence_field(pretty, seq, i, print_names);
872 if (ret != 0) {
873 goto end;
874 }
875 }
876 pretty->depth--;
877 bt_common_g_string_append(pretty->string, " ]");
878
879 end:
880 return ret;
881 }
882
883 static
884 int print_option(struct pretty_component *pretty,
885 const bt_field *option, bool print_names)
886 {
887 int ret = 0;
888 const bt_field *field = NULL;
889
890 field = bt_field_option_borrow_field_const(option);
891 if (field) {
892 bt_common_g_string_append(pretty->string, "{ ");
893 pretty->depth++;
894 if (print_names) {
895 // TODO: find tag's name using field path
896 // print_field_name_equal(pretty, tag_choice);
897 }
898 ret = print_field(pretty, field, print_names);
899 if (ret != 0) {
900 goto end;
901 }
902 pretty->depth--;
903 bt_common_g_string_append(pretty->string, " }");
904 } else {
905 bt_common_g_string_append(pretty->string, "<none>");
906 }
907
908 end:
909 return ret;
910 }
911
912 static
913 int print_variant(struct pretty_component *pretty,
914 const bt_field *variant, bool print_names)
915 {
916 int ret = 0;
917 const bt_field *field = NULL;
918
919 field = bt_field_variant_borrow_selected_option_field_const(variant);
920 BT_ASSERT_DBG(field);
921 bt_common_g_string_append(pretty->string, "{ ");
922 pretty->depth++;
923 if (print_names) {
924 // TODO: find tag's name using field path
925 // print_field_name_equal(pretty, tag_choice);
926 }
927 ret = print_field(pretty, field, print_names);
928 if (ret != 0) {
929 goto end;
930 }
931 pretty->depth--;
932 bt_common_g_string_append(pretty->string, " }");
933
934 end:
935 return ret;
936 }
937
938 static
939 int print_field(struct pretty_component *pretty,
940 const bt_field *field, bool print_names)
941 {
942 bt_field_class_type class_id;
943
944 class_id = bt_field_get_class_type(field);
945 if (class_id == BT_FIELD_CLASS_TYPE_BOOL) {
946 bt_bool v;
947 const char *text;
948
949 v = bt_field_bool_get_value(field);
950 if (pretty->use_colors) {
951 bt_common_g_string_append(pretty->string, color_number_value);
952 }
953 if (v) {
954 text = "true";
955 } else {
956 text = "false";
957 }
958 bt_common_g_string_append(pretty->string, text);
959 if (pretty->use_colors) {
960 bt_common_g_string_append(pretty->string, color_rst);
961 }
962 return 0;
963 } else if (class_id == BT_FIELD_CLASS_TYPE_BIT_ARRAY) {
964 uint64_t v = bt_field_bit_array_get_value_as_integer(field);
965
966 if (pretty->use_colors) {
967 bt_common_g_string_append(pretty->string,
968 color_number_value);
969 }
970 bt_common_g_string_append_printf(pretty->string, "0x%" PRIX64,
971 v);
972 if (pretty->use_colors) {
973 bt_common_g_string_append(pretty->string, color_rst);
974 }
975 return 0;
976 } else if (bt_field_class_type_is(class_id,
977 BT_FIELD_CLASS_TYPE_ENUMERATION)) {
978 return print_enum(pretty, field);
979 } else if (bt_field_class_type_is(class_id,
980 BT_FIELD_CLASS_TYPE_INTEGER)) {
981 return print_integer(pretty, field);
982 } else if (bt_field_class_type_is(class_id,
983 BT_FIELD_CLASS_TYPE_REAL)) {
984 double v;
985
986 if (class_id == BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL) {
987 v = bt_field_real_single_precision_get_value(field);
988 } else {
989 v = bt_field_real_double_precision_get_value(field);
990 }
991
992 if (pretty->use_colors) {
993 bt_common_g_string_append(pretty->string, color_number_value);
994 }
995 bt_common_g_string_append_printf(pretty->string, "%g", v);
996 if (pretty->use_colors) {
997 bt_common_g_string_append(pretty->string, color_rst);
998 }
999 return 0;
1000 } else if (class_id == BT_FIELD_CLASS_TYPE_STRING) {
1001 const char *str;
1002
1003 str = bt_field_string_get_value(field);
1004 if (!str) {
1005 return -1;
1006 }
1007
1008 if (pretty->use_colors) {
1009 bt_common_g_string_append(pretty->string, color_string_value);
1010 }
1011 print_escape_string(pretty, str);
1012 if (pretty->use_colors) {
1013 bt_common_g_string_append(pretty->string, color_rst);
1014 }
1015 return 0;
1016 } else if (class_id == BT_FIELD_CLASS_TYPE_STRUCTURE) {
1017 return print_struct(pretty, field, print_names);
1018 } else if (bt_field_class_type_is(class_id,
1019 BT_FIELD_CLASS_TYPE_OPTION)) {
1020 return print_option(pretty, field, print_names);
1021 } else if (bt_field_class_type_is(class_id,
1022 BT_FIELD_CLASS_TYPE_VARIANT)) {
1023 return print_variant(pretty, field, print_names);
1024 } else if (class_id == BT_FIELD_CLASS_TYPE_STATIC_ARRAY) {
1025 return print_array(pretty, field, print_names);
1026 } else if (bt_field_class_type_is(class_id,
1027 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY)) {
1028 return print_sequence(pretty, field, print_names);
1029 } else {
1030 // TODO: log instead
1031 fprintf(pretty->err, "[error] Unknown type id: %d\n", (int) class_id);
1032 return -1;
1033 }
1034 }
1035
1036 static
1037 int print_stream_packet_context(struct pretty_component *pretty,
1038 const bt_event *event)
1039 {
1040 int ret = 0;
1041 const bt_packet *packet = NULL;
1042 const bt_field *main_field = NULL;
1043
1044 packet = bt_event_borrow_packet_const(event);
1045 if (!packet) {
1046 goto end;
1047 }
1048 main_field = bt_packet_borrow_context_field_const(packet);
1049 if (!main_field) {
1050 goto end;
1051 }
1052 if (!pretty->start_line) {
1053 bt_common_g_string_append(pretty->string, ", ");
1054 }
1055 pretty->start_line = false;
1056 if (pretty->options.print_scope_field_names) {
1057 print_name_equal(pretty, "stream.packet.context");
1058 }
1059 ret = print_field(pretty, main_field,
1060 pretty->options.print_context_field_names);
1061
1062 end:
1063 return ret;
1064 }
1065
1066 static
1067 int print_stream_event_context(struct pretty_component *pretty,
1068 const bt_event *event)
1069 {
1070 int ret = 0;
1071 const bt_field *main_field = NULL;
1072
1073 main_field = bt_event_borrow_common_context_field_const(event);
1074 if (!main_field) {
1075 goto end;
1076 }
1077 if (!pretty->start_line) {
1078 bt_common_g_string_append(pretty->string, ", ");
1079 }
1080 pretty->start_line = false;
1081 if (pretty->options.print_scope_field_names) {
1082 print_name_equal(pretty, "stream.event.context");
1083 }
1084 ret = print_field(pretty, main_field,
1085 pretty->options.print_context_field_names);
1086
1087 end:
1088 return ret;
1089 }
1090
1091 static
1092 int print_event_context(struct pretty_component *pretty,
1093 const bt_event *event)
1094 {
1095 int ret = 0;
1096 const bt_field *main_field = NULL;
1097
1098 main_field = bt_event_borrow_specific_context_field_const(event);
1099 if (!main_field) {
1100 goto end;
1101 }
1102 if (!pretty->start_line) {
1103 bt_common_g_string_append(pretty->string, ", ");
1104 }
1105 pretty->start_line = false;
1106 if (pretty->options.print_scope_field_names) {
1107 print_name_equal(pretty, "event.context");
1108 }
1109 ret = print_field(pretty, main_field,
1110 pretty->options.print_context_field_names);
1111
1112 end:
1113 return ret;
1114 }
1115
1116 static
1117 int print_event_payload(struct pretty_component *pretty,
1118 const bt_event *event)
1119 {
1120 int ret = 0;
1121 const bt_field *main_field = NULL;
1122
1123 main_field = bt_event_borrow_payload_field_const(event);
1124 if (!main_field) {
1125 goto end;
1126 }
1127 if (!pretty->start_line) {
1128 bt_common_g_string_append(pretty->string, ", ");
1129 }
1130 pretty->start_line = false;
1131 if (pretty->options.print_scope_field_names) {
1132 print_name_equal(pretty, "event.fields");
1133 }
1134 ret = print_field(pretty, main_field,
1135 pretty->options.print_payload_field_names);
1136
1137 end:
1138 return ret;
1139 }
1140
1141 static
1142 int flush_buf(FILE *stream, struct pretty_component *pretty)
1143 {
1144 int ret = 0;
1145
1146 if (pretty->string->len == 0) {
1147 goto end;
1148 }
1149
1150 if (fwrite(pretty->string->str, pretty->string->len, 1, stream) != 1) {
1151 ret = -1;
1152 }
1153
1154 end:
1155 return ret;
1156 }
1157
1158 BT_HIDDEN
1159 int pretty_print_event(struct pretty_component *pretty,
1160 const bt_message *event_msg)
1161 {
1162 int ret;
1163 const bt_event *event =
1164 bt_message_event_borrow_event_const(event_msg);
1165
1166 BT_ASSERT_DBG(event);
1167 pretty->start_line = true;
1168 g_string_assign(pretty->string, "");
1169 ret = print_event_header(pretty, event_msg);
1170 if (ret != 0) {
1171 goto end;
1172 }
1173
1174 ret = print_stream_packet_context(pretty, event);
1175 if (ret != 0) {
1176 goto end;
1177 }
1178
1179 ret = print_stream_event_context(pretty, event);
1180 if (ret != 0) {
1181 goto end;
1182 }
1183
1184 ret = print_event_context(pretty, event);
1185 if (ret != 0) {
1186 goto end;
1187 }
1188
1189 ret = print_event_payload(pretty, event);
1190 if (ret != 0) {
1191 goto end;
1192 }
1193
1194 bt_common_g_string_append_c(pretty->string, '\n');
1195 if (flush_buf(pretty->out, pretty)) {
1196 ret = -1;
1197 goto end;
1198 }
1199
1200 end:
1201 return ret;
1202 }
1203
1204 static
1205 int print_discarded_elements_msg(struct pretty_component *pretty,
1206 const bt_stream *stream,
1207 const bt_clock_snapshot *begin_clock_snapshot,
1208 const bt_clock_snapshot *end_clock_snapshot,
1209 uint64_t count, const char *elem_type)
1210 {
1211 int ret = 0;
1212 const bt_stream_class *stream_class = NULL;
1213 const bt_trace *trace = NULL;
1214 const char *stream_name;
1215 const char *trace_name;
1216 bt_uuid trace_uuid;
1217 int64_t stream_class_id;
1218 int64_t stream_id;
1219 const char *init_msg;
1220
1221 /* Stream name */
1222 stream_name = bt_stream_get_name(stream);
1223 if (!stream_name) {
1224 stream_name = "(unknown)";
1225 }
1226
1227 /* Stream class ID */
1228 stream_class = bt_stream_borrow_class_const(stream);
1229 BT_ASSERT(stream_class);
1230 stream_class_id = bt_stream_class_get_id(stream_class);
1231
1232 /* Stream ID */
1233 stream_id = bt_stream_get_id(stream);
1234
1235 /* Trace name */
1236 trace = bt_stream_borrow_trace_const(stream);
1237 BT_ASSERT(trace);
1238 trace_name = bt_trace_get_name(trace);
1239 if (!trace_name) {
1240 trace_name = "(unknown)";
1241 }
1242
1243 /* Trace UUID */
1244 trace_uuid = bt_trace_get_uuid(trace);
1245
1246 /* Format message */
1247 g_string_assign(pretty->string, "");
1248
1249 if (count == UINT64_C(-1)) {
1250 init_msg = "Tracer may have discarded";
1251 } else {
1252 init_msg = "Tracer discarded";
1253 }
1254
1255 bt_common_g_string_append_printf(pretty->string,
1256 "%s%sWARNING%s%s: %s ",
1257 bt_common_color_fg_yellow(),
1258 bt_common_color_bold(),
1259 bt_common_color_reset(),
1260 bt_common_color_fg_yellow(), init_msg);
1261
1262 if (count == UINT64_C(-1)) {
1263 bt_common_g_string_append_printf(pretty->string, "%ss", elem_type);
1264 } else {
1265 bt_common_g_string_append_printf(pretty->string,
1266 "%" PRIu64 " %s%s", count, elem_type,
1267 count == 1 ? "" : "s");
1268 }
1269
1270 bt_common_g_string_append_c(pretty->string, ' ');
1271
1272 if (begin_clock_snapshot && end_clock_snapshot) {
1273 bt_common_g_string_append(pretty->string, "between [");
1274 print_timestamp_wall(pretty, begin_clock_snapshot, false);
1275 bt_common_g_string_append(pretty->string, "] and [");
1276 print_timestamp_wall(pretty, end_clock_snapshot, false);
1277 bt_common_g_string_append(pretty->string, "]");
1278 } else {
1279 bt_common_g_string_append(pretty->string, "(unknown time range)");
1280 }
1281
1282 bt_common_g_string_append_printf(pretty->string, " in trace \"%s\" ", trace_name);
1283
1284 if (trace_uuid) {
1285 bt_common_g_string_append_printf(pretty->string,
1286 "(UUID: " BT_UUID_FMT ") ",
1287 BT_UUID_FMT_VALUES(trace_uuid));
1288 } else {
1289 bt_common_g_string_append(pretty->string, "(no UUID) ");
1290 }
1291
1292 bt_common_g_string_append_printf(pretty->string,
1293 "within stream \"%s\" (stream class ID: %" PRIu64 ", ",
1294 stream_name, stream_class_id);
1295
1296 if (stream_id >= 0) {
1297 bt_common_g_string_append_printf(pretty->string,
1298 "stream ID: %" PRIu64, stream_id);
1299 } else {
1300 bt_common_g_string_append(pretty->string, "no stream ID");
1301 }
1302
1303 bt_common_g_string_append_printf(pretty->string, ").%s\n",
1304 bt_common_color_reset());
1305
1306 /*
1307 * Print to standard error stream to remain backward compatible
1308 * with Babeltrace 1.
1309 */
1310 if (flush_buf(stderr, pretty)) {
1311 ret = -1;
1312 }
1313
1314 return ret;
1315 }
1316
1317 BT_HIDDEN
1318 int pretty_print_discarded_items(struct pretty_component *pretty,
1319 const bt_message *msg)
1320 {
1321 const bt_clock_snapshot *begin = NULL;
1322 const bt_clock_snapshot *end = NULL;
1323 const bt_stream *stream;
1324 const bt_stream_class *stream_class;
1325 uint64_t count = UINT64_C(-1);
1326 const char *elem_type;
1327
1328 switch (bt_message_get_type(msg)) {
1329 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1330 stream = bt_message_discarded_events_borrow_stream_const(msg);
1331
1332 if (bt_message_discarded_events_get_count(msg, &count) ==
1333 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) {
1334 count = UINT64_C(-1);
1335 }
1336
1337 elem_type = "event";
1338 break;
1339 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1340 stream = bt_message_discarded_packets_borrow_stream_const(msg);
1341
1342 if (bt_message_discarded_packets_get_count(msg, &count) ==
1343 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) {
1344 count = UINT64_C(-1);
1345 }
1346
1347 elem_type = "packet";
1348 break;
1349 default:
1350 bt_common_abort();
1351 }
1352
1353 BT_ASSERT(stream);
1354 stream_class = bt_stream_borrow_class_const(stream);
1355
1356 switch (bt_message_get_type(msg)) {
1357 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1358 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
1359 stream_class)) {
1360 begin = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1361 msg);
1362 end = bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
1363 msg);
1364 }
1365
1366 break;
1367 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1368 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
1369 stream_class)) {
1370 begin = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1371 msg);
1372 end = bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1373 msg);
1374 }
1375
1376 break;
1377 default:
1378 bt_common_abort();
1379 }
1380
1381 print_discarded_elements_msg(pretty, stream, begin, end,
1382 count, elem_type);
1383 return 0;
1384 }
1385
1386 BT_HIDDEN
1387 void pretty_print_init(void)
1388 {
1389 strcpy(color_name, bt_common_color_bold());
1390 strcpy(color_field_name, bt_common_color_fg_cyan());
1391 strcpy(color_rst, bt_common_color_reset());
1392 strcpy(color_string_value, bt_common_color_bold());
1393 strcpy(color_number_value, bt_common_color_bold());
1394 strcpy(color_enum_mapping_name, bt_common_color_bold());
1395 strcpy(color_unknown, bt_common_color_bold());
1396 strcat(color_unknown, bt_common_color_fg_bright_red());
1397 strcpy(color_event_name, bt_common_color_bold());
1398 strcat(color_event_name, bt_common_color_fg_bright_magenta());
1399 strcpy(color_timestamp, bt_common_color_bold());
1400 strcat(color_timestamp, bt_common_color_fg_bright_yellow());
1401 }
This page took 0.09982 seconds and 4 git commands to generate.