Visibility hidden by default
[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 /*
668 * Print the unknown label.
669 */
670 static
671 void print_enum_value_label_unknown(struct pretty_component *pretty)
672 {
673 if (pretty->use_colors) {
674 bt_common_g_string_append(pretty->string, color_unknown);
675 }
676
677 bt_common_g_string_append(pretty->string, "<unknown>");
678
679 if (pretty->use_colors) {
680 bt_common_g_string_append(pretty->string, color_rst);
681 }
682 }
683
684 /*
685 * Print labels for a value. If there are more than one label for the
686 * value, they will be printed in "{ }".
687 */
688 static
689 void print_enum_value_label_array(struct pretty_component *pretty,
690 uint64_t label_count,
691 bt_field_class_enumeration_mapping_label_array label_array)
692 {
693 uint64_t i;
694
695 if (label_count > 1) {
696 bt_common_g_string_append(pretty->string, "{ ");
697 }
698
699 for (i = 0; i < label_count; i++) {
700 const char *mapping_name = label_array[i];
701
702 if (i != 0) {
703 bt_common_g_string_append(pretty->string, ", ");
704 }
705 if (pretty->use_colors) {
706 bt_common_g_string_append(pretty->string, color_enum_mapping_name);
707 }
708 print_escape_string(pretty, mapping_name);
709 if (pretty->use_colors) {
710 bt_common_g_string_append(pretty->string, color_rst);
711 }
712 }
713
714 if (label_count > 1) {
715 bt_common_g_string_append(pretty->string, " }");
716 }
717 }
718
719 /*
720 * Print arrays of labels and counts are ORed bit flags.
721 */
722 static
723 void print_enum_value_bit_flag_label_arrays(struct pretty_component *pretty)
724 {
725 uint64_t i;
726 bool first_label = true;
727
728 /* For each bit with a label count > 0, print the labels. */
729 for (i = 0; i < ENUMERATION_MAX_BITFLAGS_COUNT; i++) {
730 uint64_t label_count = pretty->enum_bit_labels[i]->len;
731
732 if (label_count > 0) {
733 if (!first_label) {
734 bt_common_g_string_append(pretty->string, " | ");
735 }
736 print_enum_value_label_array(pretty, label_count,
737 (void *) pretty->enum_bit_labels[i]->pdata);
738 first_label = false;
739 }
740 }
741 }
742
743 /*
744 * Get the labels mapping to an unsigned value.
745 *
746 * This function will set the count to the count of mapping labels that match
747 * the value. If count == 0, the caller has nothing to do, if count > 0,
748 * the label_array contains the labels to print and the caller is reponsible
749 * to call g_free on it once the values have been used.
750 */
751 static
752 void print_enum_unsigned_get_mapping_labels_for_value(const bt_field_class *fc,
753 uint64_t value, GPtrArray *labels)
754 {
755 uint64_t mapping_count = bt_field_class_enumeration_get_mapping_count(fc);
756 uint64_t i;
757
758 for (i = 0; i < mapping_count; i++) {
759 uint64_t range_i;
760 const struct bt_field_class_enumeration_unsigned_mapping *mapping =
761 bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const(fc, i);
762 const bt_integer_range_set_unsigned *ranges =
763 bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const(mapping);
764 uint64_t range_count = bt_integer_range_set_get_range_count(
765 bt_integer_range_set_unsigned_as_range_set_const(ranges));
766
767 for (range_i = 0; range_i < range_count; range_i++) {
768 const bt_integer_range_unsigned *range =
769 bt_integer_range_set_unsigned_borrow_range_by_index_const(ranges, range_i);
770 uint64_t lower = bt_integer_range_unsigned_get_lower(range);
771 uint64_t upper = bt_integer_range_unsigned_get_upper(range);
772
773 /*
774 * Flag is active if this range represents a single value
775 * (lower == upper) and the lower is the same as the bit
776 * value to test against.
777 */
778 if ((lower == upper) && (lower == value)) {
779 g_ptr_array_add(labels, (void *) bt_field_class_enumeration_mapping_get_label(
780 bt_field_class_enumeration_unsigned_mapping_as_mapping_const(mapping)));
781 break;
782 }
783 }
784 }
785 }
786
787 /*
788 * Splits an unsigned enum value into its bit and for each bit set,
789 * try to find a corresponding label.
790 *
791 * If any bit set does not have a corresponding label, then it prints
792 * an unknown value, otherwise, it prints the labels, separated by '|'
793 */
794 static
795 void print_enum_unsigned_try_bit_flags(struct pretty_component *pretty,
796 const bt_field *field)
797 {
798 uint64_t i;
799 uint64_t value = bt_field_integer_unsigned_get_value(field);
800 const bt_field_class *fc = bt_field_borrow_class_const(field);
801
802 /* Value is 0, if there was a label for it, we would know by now. */
803 if (value == 0) {
804 print_enum_value_label_unknown(pretty);
805 goto end;
806 }
807
808 for (i = 0; i < ENUMERATION_MAX_BITFLAGS_COUNT; i++) {
809 uint64_t bit_value = UINT64_C(1) << i;
810
811 if ((value & bit_value) != 0) {
812 print_enum_unsigned_get_mapping_labels_for_value(
813 fc, bit_value, pretty->enum_bit_labels[i]);
814
815 if (pretty->enum_bit_labels[i]->len == 0) {
816 /*
817 * This bit has no matching label, so this
818 * field is not a bit flag field, print
819 * unknown and return.
820 */
821 print_enum_value_label_unknown(pretty);
822 goto end;
823 }
824 }
825 }
826
827 print_enum_value_bit_flag_label_arrays(pretty);
828
829 end:
830 return;
831 }
832
833 /*
834 * Get the labels mapping to a signed value
835 *
836 * This function will set the count to the count of mapping labels that match
837 * the value. If count == 0, the caller has nothing to do, if count > 0,
838 * the label_array contains the labels to print and the caller is reponsible
839 * to call g_free on it once the values have been used.
840 */
841 static
842 void print_enum_signed_get_mapping_labels_for_value(const bt_field_class *fc,
843 int64_t value, GPtrArray *labels)
844 {
845 uint64_t mapping_count = bt_field_class_enumeration_get_mapping_count(fc);
846 uint64_t i;
847
848 for (i = 0; i < mapping_count; i++) {
849 uint64_t range_i;
850 const struct bt_field_class_enumeration_signed_mapping *mapping =
851 bt_field_class_enumeration_signed_borrow_mapping_by_index_const(fc, i);
852 const bt_integer_range_set_signed *ranges =
853 bt_field_class_enumeration_signed_mapping_borrow_ranges_const(mapping);
854 uint64_t range_count = bt_integer_range_set_get_range_count(
855 bt_integer_range_set_signed_as_range_set_const(ranges));
856
857 for (range_i = 0; range_i < range_count; range_i++) {
858 const bt_integer_range_signed *range =
859 bt_integer_range_set_signed_borrow_range_by_index_const(ranges, range_i);
860 uint64_t lower = bt_integer_range_signed_get_lower(range);
861 uint64_t upper = bt_integer_range_signed_get_upper(range);
862
863 /*
864 * Flag is active if this range represents a single value
865 * (lower == upper) and the lower is the same as the bit
866 * value to test against.
867 */
868 if ((lower == upper) && (lower == value)) {
869 g_ptr_array_add(labels, (void*) bt_field_class_enumeration_mapping_get_label(
870 bt_field_class_enumeration_signed_mapping_as_mapping_const(mapping)));
871 break;
872 }
873 }
874 }
875 }
876
877 /*
878 * Splits a signed enum value into its bit and for each bit set,
879 * try to find a corresponding label.
880 *
881 * If any bit set does not have a corresponding label, then it prints
882 * an unknown value, otherwise, it prints the labels, separated by '|'.
883 */
884 static
885 void print_enum_signed_try_bit_flags(struct pretty_component *pretty,
886 const bt_field *field)
887 {
888 uint64_t i;
889 int64_t value = bt_field_integer_signed_get_value(field);
890 const bt_field_class *fc = bt_field_borrow_class_const(field);
891
892 /*
893 * Negative value, not a bit flag enum
894 * For 0, if there was a value, we would know by now.
895 */
896 if (value <= 0) {
897 print_enum_value_label_unknown(pretty);
898 goto end;
899 }
900
901 for (i = 0; i < ENUMERATION_MAX_BITFLAGS_COUNT; i++) {
902 uint64_t bit_value = 1ULL << i;
903
904 if ((value & bit_value) != 0) {
905 print_enum_signed_get_mapping_labels_for_value(
906 fc, bit_value, pretty->enum_bit_labels[i]);
907
908 if (pretty->enum_bit_labels[i]->len == 0) {
909 /*
910 * This bit has no matching label, so this
911 * field is not a bit flag field, print
912 * unknown and return.
913 */
914 print_enum_value_label_unknown(pretty);
915 goto end;
916 }
917 }
918 }
919
920 print_enum_value_bit_flag_label_arrays(pretty);
921
922 end:
923 return;
924 }
925
926 /*
927 * Main function to try to print the value of the enum field as a
928 * bit flag. It calls the appropriate function depending on the
929 * field type's signedness and prints the label for each bit set,
930 * if it's a bit flag field.
931 */
932 static
933 void print_enum_try_bit_flags(struct pretty_component *pretty,
934 const bt_field *field)
935 {
936 const bt_field_class *fc = bt_field_borrow_class_const(field);
937 uint64_t int_range = bt_field_class_integer_get_field_value_range(fc);
938 int i;
939
940 BT_ASSERT(int_range <= ENUMERATION_MAX_BITFLAGS_COUNT);
941
942 /*
943 * Remove all labels from the previous enumeration field.
944 */
945 for (i = 0; i < ENUMERATION_MAX_BITFLAGS_COUNT; i++) {
946 g_ptr_array_set_size(pretty->enum_bit_labels[i], 0);
947 }
948
949 /* Get the mapping labels for the bit value. */
950 switch (bt_field_class_get_type(fc)) {
951 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
952 print_enum_unsigned_try_bit_flags(pretty, field);
953 break;
954 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
955 print_enum_signed_try_bit_flags(pretty, field);
956 break;
957 default:
958 bt_common_abort();
959 }
960 }
961
962 static
963 int print_enum(struct pretty_component *pretty,
964 const bt_field *field)
965 {
966 int ret = 0;
967 bt_field_class_enumeration_mapping_label_array label_array;
968 uint64_t label_count;
969
970 switch (bt_field_get_class_type(field)) {
971 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION:
972 ret = bt_field_enumeration_unsigned_get_mapping_labels(field,
973 &label_array, &label_count);
974 break;
975 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION:
976 ret = bt_field_enumeration_signed_get_mapping_labels(field,
977 &label_array, &label_count);
978 break;
979 default:
980 bt_common_abort();
981 }
982
983 if (ret) {
984 ret = -1;
985 goto end;
986 }
987
988 bt_common_g_string_append(pretty->string, "( ");
989 if (label_count != 0) {
990 /* The numerical value matches some labels, print them. */
991 print_enum_value_label_array(pretty, label_count, label_array);
992 } else if (pretty->options.print_enum_flags) {
993 /*
994 * The numerical value of the enumeration does not match any
995 * label but the `print-enum-flags` parameter is TRUE so try to
996 * decompose the value of the enum into bits and print it as a
997 * bit flag enum.
998 */
999 print_enum_try_bit_flags(pretty, field);
1000 } else {
1001 print_enum_value_label_unknown(pretty);
1002 }
1003
1004 /* Print the actual value of the enum. */
1005 bt_common_g_string_append(pretty->string, " : container = ");
1006 ret = print_integer(pretty, field);
1007 if (ret != 0) {
1008 goto end;
1009 }
1010 bt_common_g_string_append(pretty->string, " )");
1011 end:
1012 return ret;
1013 }
1014
1015 static
1016 int print_struct_field(struct pretty_component *pretty,
1017 const bt_field *_struct,
1018 const bt_field_class *struct_class,
1019 uint64_t i, bool print_names, uint64_t *nr_printed_fields)
1020 {
1021 int ret = 0;
1022 const char *field_name;
1023 const bt_field *field = NULL;
1024 const bt_field_class_structure_member *member;
1025
1026 field = bt_field_structure_borrow_member_field_by_index_const(_struct, i);
1027 if (!field) {
1028 ret = -1;
1029 goto end;
1030 }
1031
1032 member = bt_field_class_structure_borrow_member_by_index_const(
1033 struct_class, i);
1034 field_name = bt_field_class_structure_member_get_name(member);
1035
1036 if (*nr_printed_fields > 0) {
1037 bt_common_g_string_append(pretty->string, ", ");
1038 } else {
1039 bt_common_g_string_append(pretty->string, " ");
1040 }
1041 if (print_names) {
1042 print_field_name_equal(pretty, field_name);
1043 }
1044 ret = print_field(pretty, field, print_names);
1045 *nr_printed_fields += 1;
1046
1047 end:
1048 return ret;
1049 }
1050
1051 static
1052 int print_struct(struct pretty_component *pretty,
1053 const bt_field *_struct, bool print_names)
1054 {
1055 int ret = 0;
1056 const bt_field_class *struct_class = NULL;
1057 uint64_t nr_fields, i, nr_printed_fields;
1058
1059 struct_class = bt_field_borrow_class_const(_struct);
1060 nr_fields = bt_field_class_structure_get_member_count(struct_class);
1061
1062 bt_common_g_string_append(pretty->string, "{");
1063 pretty->depth++;
1064 nr_printed_fields = 0;
1065 for (i = 0; i < nr_fields; i++) {
1066 ret = print_struct_field(pretty, _struct, struct_class, i,
1067 print_names, &nr_printed_fields);
1068 if (ret != 0) {
1069 goto end;
1070 }
1071 }
1072 pretty->depth--;
1073 bt_common_g_string_append(pretty->string, " }");
1074
1075 end:
1076 return ret;
1077 }
1078
1079 static
1080 int print_array_field(struct pretty_component *pretty,
1081 const bt_field *array, uint64_t i, bool print_names)
1082 {
1083 const bt_field *field = NULL;
1084
1085 if (i != 0) {
1086 bt_common_g_string_append(pretty->string, ", ");
1087 } else {
1088 bt_common_g_string_append(pretty->string, " ");
1089 }
1090 if (print_names) {
1091 bt_common_g_string_append_printf(pretty->string, "[%" PRIu64 "] = ", i);
1092 }
1093
1094 field = bt_field_array_borrow_element_field_by_index_const(array, i);
1095 BT_ASSERT_DBG(field);
1096 return print_field(pretty, field, print_names);
1097 }
1098
1099 static
1100 int print_array(struct pretty_component *pretty,
1101 const bt_field *array, bool print_names)
1102 {
1103 int ret = 0;
1104 uint64_t len;
1105 uint64_t i;
1106
1107 len = bt_field_array_get_length(array);
1108 bt_common_g_string_append(pretty->string, "[");
1109 pretty->depth++;
1110 for (i = 0; i < len; i++) {
1111 ret = print_array_field(pretty, array, i, print_names);
1112 if (ret != 0) {
1113 goto end;
1114 }
1115 }
1116 pretty->depth--;
1117 bt_common_g_string_append(pretty->string, " ]");
1118
1119 end:
1120 return ret;
1121 }
1122
1123 static
1124 int print_sequence_field(struct pretty_component *pretty,
1125 const bt_field *seq, uint64_t i, bool print_names)
1126 {
1127 const bt_field *field = NULL;
1128
1129 if (i != 0) {
1130 bt_common_g_string_append(pretty->string, ", ");
1131 } else {
1132 bt_common_g_string_append(pretty->string, " ");
1133 }
1134 if (print_names) {
1135 bt_common_g_string_append_printf(pretty->string, "[%" PRIu64 "] = ", i);
1136 }
1137
1138 field = bt_field_array_borrow_element_field_by_index_const(seq, i);
1139 BT_ASSERT_DBG(field);
1140 return print_field(pretty, field, print_names);
1141 }
1142
1143 static
1144 int print_sequence(struct pretty_component *pretty,
1145 const bt_field *seq, bool print_names)
1146 {
1147 int ret = 0;
1148 uint64_t len;
1149 uint64_t i;
1150
1151 len = bt_field_array_get_length(seq);
1152 bt_common_g_string_append(pretty->string, "[");
1153
1154 pretty->depth++;
1155 for (i = 0; i < len; i++) {
1156 ret = print_sequence_field(pretty, seq, i, print_names);
1157 if (ret != 0) {
1158 goto end;
1159 }
1160 }
1161 pretty->depth--;
1162 bt_common_g_string_append(pretty->string, " ]");
1163
1164 end:
1165 return ret;
1166 }
1167
1168 static
1169 int print_option(struct pretty_component *pretty,
1170 const bt_field *option, bool print_names)
1171 {
1172 int ret = 0;
1173 const bt_field *field = NULL;
1174
1175 field = bt_field_option_borrow_field_const(option);
1176 if (field) {
1177 bt_common_g_string_append(pretty->string, "{ ");
1178 pretty->depth++;
1179 if (print_names) {
1180 // TODO: find tag's name using field path
1181 // print_field_name_equal(pretty, tag_choice);
1182 }
1183 ret = print_field(pretty, field, print_names);
1184 if (ret != 0) {
1185 goto end;
1186 }
1187 pretty->depth--;
1188 bt_common_g_string_append(pretty->string, " }");
1189 } else {
1190 bt_common_g_string_append(pretty->string, "<none>");
1191 }
1192
1193 end:
1194 return ret;
1195 }
1196
1197 static
1198 int print_variant(struct pretty_component *pretty,
1199 const bt_field *variant, bool print_names)
1200 {
1201 int ret = 0;
1202 const bt_field *field = NULL;
1203
1204 field = bt_field_variant_borrow_selected_option_field_const(variant);
1205 BT_ASSERT_DBG(field);
1206 bt_common_g_string_append(pretty->string, "{ ");
1207 pretty->depth++;
1208 if (print_names) {
1209 // TODO: find tag's name using field path
1210 // print_field_name_equal(pretty, tag_choice);
1211 }
1212 ret = print_field(pretty, field, print_names);
1213 if (ret != 0) {
1214 goto end;
1215 }
1216 pretty->depth--;
1217 bt_common_g_string_append(pretty->string, " }");
1218
1219 end:
1220 return ret;
1221 }
1222
1223 static
1224 int print_field(struct pretty_component *pretty,
1225 const bt_field *field, bool print_names)
1226 {
1227 bt_field_class_type class_id;
1228
1229 class_id = bt_field_get_class_type(field);
1230 if (class_id == BT_FIELD_CLASS_TYPE_BOOL) {
1231 bt_bool v;
1232 const char *text;
1233
1234 v = bt_field_bool_get_value(field);
1235 if (pretty->use_colors) {
1236 bt_common_g_string_append(pretty->string, color_number_value);
1237 }
1238 if (v) {
1239 text = "true";
1240 } else {
1241 text = "false";
1242 }
1243 bt_common_g_string_append(pretty->string, text);
1244 if (pretty->use_colors) {
1245 bt_common_g_string_append(pretty->string, color_rst);
1246 }
1247 return 0;
1248 } else if (class_id == BT_FIELD_CLASS_TYPE_BIT_ARRAY) {
1249 uint64_t v = bt_field_bit_array_get_value_as_integer(field);
1250
1251 if (pretty->use_colors) {
1252 bt_common_g_string_append(pretty->string,
1253 color_number_value);
1254 }
1255 bt_common_g_string_append_printf(pretty->string, "0x%" PRIX64,
1256 v);
1257 if (pretty->use_colors) {
1258 bt_common_g_string_append(pretty->string, color_rst);
1259 }
1260 return 0;
1261 } else if (bt_field_class_type_is(class_id,
1262 BT_FIELD_CLASS_TYPE_ENUMERATION)) {
1263 return print_enum(pretty, field);
1264 } else if (bt_field_class_type_is(class_id,
1265 BT_FIELD_CLASS_TYPE_INTEGER)) {
1266 return print_integer(pretty, field);
1267 } else if (bt_field_class_type_is(class_id,
1268 BT_FIELD_CLASS_TYPE_REAL)) {
1269 double v;
1270
1271 if (class_id == BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL) {
1272 v = bt_field_real_single_precision_get_value(field);
1273 } else {
1274 v = bt_field_real_double_precision_get_value(field);
1275 }
1276
1277 if (pretty->use_colors) {
1278 bt_common_g_string_append(pretty->string, color_number_value);
1279 }
1280 bt_common_g_string_append_printf(pretty->string, "%g", v);
1281 if (pretty->use_colors) {
1282 bt_common_g_string_append(pretty->string, color_rst);
1283 }
1284 return 0;
1285 } else if (class_id == BT_FIELD_CLASS_TYPE_STRING) {
1286 const char *str;
1287
1288 str = bt_field_string_get_value(field);
1289 if (!str) {
1290 return -1;
1291 }
1292
1293 if (pretty->use_colors) {
1294 bt_common_g_string_append(pretty->string, color_string_value);
1295 }
1296 print_escape_string(pretty, str);
1297 if (pretty->use_colors) {
1298 bt_common_g_string_append(pretty->string, color_rst);
1299 }
1300 return 0;
1301 } else if (class_id == BT_FIELD_CLASS_TYPE_STRUCTURE) {
1302 return print_struct(pretty, field, print_names);
1303 } else if (bt_field_class_type_is(class_id,
1304 BT_FIELD_CLASS_TYPE_OPTION)) {
1305 return print_option(pretty, field, print_names);
1306 } else if (bt_field_class_type_is(class_id,
1307 BT_FIELD_CLASS_TYPE_VARIANT)) {
1308 return print_variant(pretty, field, print_names);
1309 } else if (class_id == BT_FIELD_CLASS_TYPE_STATIC_ARRAY) {
1310 return print_array(pretty, field, print_names);
1311 } else if (bt_field_class_type_is(class_id,
1312 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY)) {
1313 return print_sequence(pretty, field, print_names);
1314 } else {
1315 // TODO: log instead
1316 fprintf(pretty->err, "[error] Unknown type id: %d\n", (int) class_id);
1317 return -1;
1318 }
1319 }
1320
1321 static
1322 int print_stream_packet_context(struct pretty_component *pretty,
1323 const bt_event *event)
1324 {
1325 int ret = 0;
1326 const bt_packet *packet = NULL;
1327 const bt_field *main_field = NULL;
1328
1329 packet = bt_event_borrow_packet_const(event);
1330 if (!packet) {
1331 goto end;
1332 }
1333 main_field = bt_packet_borrow_context_field_const(packet);
1334 if (!main_field) {
1335 goto end;
1336 }
1337 if (!pretty->start_line) {
1338 bt_common_g_string_append(pretty->string, ", ");
1339 }
1340 pretty->start_line = false;
1341 if (pretty->options.print_scope_field_names) {
1342 print_name_equal(pretty, "stream.packet.context");
1343 }
1344 ret = print_field(pretty, main_field,
1345 pretty->options.print_context_field_names);
1346
1347 end:
1348 return ret;
1349 }
1350
1351 static
1352 int print_stream_event_context(struct pretty_component *pretty,
1353 const bt_event *event)
1354 {
1355 int ret = 0;
1356 const bt_field *main_field = NULL;
1357
1358 main_field = bt_event_borrow_common_context_field_const(event);
1359 if (!main_field) {
1360 goto end;
1361 }
1362 if (!pretty->start_line) {
1363 bt_common_g_string_append(pretty->string, ", ");
1364 }
1365 pretty->start_line = false;
1366 if (pretty->options.print_scope_field_names) {
1367 print_name_equal(pretty, "stream.event.context");
1368 }
1369 ret = print_field(pretty, main_field,
1370 pretty->options.print_context_field_names);
1371
1372 end:
1373 return ret;
1374 }
1375
1376 static
1377 int print_event_context(struct pretty_component *pretty,
1378 const bt_event *event)
1379 {
1380 int ret = 0;
1381 const bt_field *main_field = NULL;
1382
1383 main_field = bt_event_borrow_specific_context_field_const(event);
1384 if (!main_field) {
1385 goto end;
1386 }
1387 if (!pretty->start_line) {
1388 bt_common_g_string_append(pretty->string, ", ");
1389 }
1390 pretty->start_line = false;
1391 if (pretty->options.print_scope_field_names) {
1392 print_name_equal(pretty, "event.context");
1393 }
1394 ret = print_field(pretty, main_field,
1395 pretty->options.print_context_field_names);
1396
1397 end:
1398 return ret;
1399 }
1400
1401 static
1402 int print_event_payload(struct pretty_component *pretty,
1403 const bt_event *event)
1404 {
1405 int ret = 0;
1406 const bt_field *main_field = NULL;
1407
1408 main_field = bt_event_borrow_payload_field_const(event);
1409 if (!main_field) {
1410 goto end;
1411 }
1412 if (!pretty->start_line) {
1413 bt_common_g_string_append(pretty->string, ", ");
1414 }
1415 pretty->start_line = false;
1416 if (pretty->options.print_scope_field_names) {
1417 print_name_equal(pretty, "event.fields");
1418 }
1419 ret = print_field(pretty, main_field,
1420 pretty->options.print_payload_field_names);
1421
1422 end:
1423 return ret;
1424 }
1425
1426 static
1427 int flush_buf(FILE *stream, struct pretty_component *pretty)
1428 {
1429 int ret = 0;
1430
1431 if (pretty->string->len == 0) {
1432 goto end;
1433 }
1434
1435 if (fwrite(pretty->string->str, pretty->string->len, 1, stream) != 1) {
1436 ret = -1;
1437 }
1438
1439 end:
1440 return ret;
1441 }
1442
1443 int pretty_print_event(struct pretty_component *pretty,
1444 const bt_message *event_msg)
1445 {
1446 int ret;
1447 const bt_event *event =
1448 bt_message_event_borrow_event_const(event_msg);
1449
1450 BT_ASSERT_DBG(event);
1451 pretty->start_line = true;
1452 g_string_assign(pretty->string, "");
1453 ret = print_event_header(pretty, event_msg);
1454 if (ret != 0) {
1455 goto end;
1456 }
1457
1458 ret = print_stream_packet_context(pretty, event);
1459 if (ret != 0) {
1460 goto end;
1461 }
1462
1463 ret = print_stream_event_context(pretty, event);
1464 if (ret != 0) {
1465 goto end;
1466 }
1467
1468 ret = print_event_context(pretty, event);
1469 if (ret != 0) {
1470 goto end;
1471 }
1472
1473 ret = print_event_payload(pretty, event);
1474 if (ret != 0) {
1475 goto end;
1476 }
1477
1478 bt_common_g_string_append_c(pretty->string, '\n');
1479 if (flush_buf(pretty->out, pretty)) {
1480 ret = -1;
1481 goto end;
1482 }
1483
1484 end:
1485 return ret;
1486 }
1487
1488 static
1489 int print_discarded_elements_msg(struct pretty_component *pretty,
1490 const bt_stream *stream,
1491 const bt_clock_snapshot *begin_clock_snapshot,
1492 const bt_clock_snapshot *end_clock_snapshot,
1493 uint64_t count, const char *elem_type)
1494 {
1495 int ret = 0;
1496 const bt_stream_class *stream_class = NULL;
1497 const bt_trace *trace = NULL;
1498 const char *stream_name;
1499 const char *trace_name;
1500 bt_uuid trace_uuid;
1501 int64_t stream_class_id;
1502 int64_t stream_id;
1503 const char *init_msg;
1504
1505 /* Stream name */
1506 stream_name = bt_stream_get_name(stream);
1507 if (!stream_name) {
1508 stream_name = "(unknown)";
1509 }
1510
1511 /* Stream class ID */
1512 stream_class = bt_stream_borrow_class_const(stream);
1513 BT_ASSERT(stream_class);
1514 stream_class_id = bt_stream_class_get_id(stream_class);
1515
1516 /* Stream ID */
1517 stream_id = bt_stream_get_id(stream);
1518
1519 /* Trace name */
1520 trace = bt_stream_borrow_trace_const(stream);
1521 BT_ASSERT(trace);
1522 trace_name = bt_trace_get_name(trace);
1523 if (!trace_name) {
1524 trace_name = "(unknown)";
1525 }
1526
1527 /* Trace UUID */
1528 trace_uuid = bt_trace_get_uuid(trace);
1529
1530 /* Format message */
1531 g_string_assign(pretty->string, "");
1532
1533 if (count == UINT64_C(-1)) {
1534 init_msg = "Tracer may have discarded";
1535 } else {
1536 init_msg = "Tracer discarded";
1537 }
1538
1539 bt_common_g_string_append_printf(pretty->string,
1540 "%s%sWARNING%s%s: %s ",
1541 bt_common_color_fg_yellow(),
1542 bt_common_color_bold(),
1543 bt_common_color_reset(),
1544 bt_common_color_fg_yellow(), init_msg);
1545
1546 if (count == UINT64_C(-1)) {
1547 bt_common_g_string_append_printf(pretty->string, "%ss", elem_type);
1548 } else {
1549 bt_common_g_string_append_printf(pretty->string,
1550 "%" PRIu64 " %s%s", count, elem_type,
1551 count == 1 ? "" : "s");
1552 }
1553
1554 bt_common_g_string_append_c(pretty->string, ' ');
1555
1556 if (begin_clock_snapshot && end_clock_snapshot) {
1557 bt_common_g_string_append(pretty->string, "between [");
1558 print_timestamp_wall(pretty, begin_clock_snapshot, false);
1559 bt_common_g_string_append(pretty->string, "] and [");
1560 print_timestamp_wall(pretty, end_clock_snapshot, false);
1561 bt_common_g_string_append(pretty->string, "]");
1562 } else {
1563 bt_common_g_string_append(pretty->string, "(unknown time range)");
1564 }
1565
1566 bt_common_g_string_append_printf(pretty->string, " in trace \"%s\" ", trace_name);
1567
1568 if (trace_uuid) {
1569 bt_common_g_string_append_printf(pretty->string,
1570 "(UUID: " BT_UUID_FMT ") ",
1571 BT_UUID_FMT_VALUES(trace_uuid));
1572 } else {
1573 bt_common_g_string_append(pretty->string, "(no UUID) ");
1574 }
1575
1576 bt_common_g_string_append_printf(pretty->string,
1577 "within stream \"%s\" (stream class ID: %" PRIu64 ", ",
1578 stream_name, stream_class_id);
1579
1580 if (stream_id >= 0) {
1581 bt_common_g_string_append_printf(pretty->string,
1582 "stream ID: %" PRIu64, stream_id);
1583 } else {
1584 bt_common_g_string_append(pretty->string, "no stream ID");
1585 }
1586
1587 bt_common_g_string_append_printf(pretty->string, ").%s\n",
1588 bt_common_color_reset());
1589
1590 /*
1591 * Print to standard error stream to remain backward compatible
1592 * with Babeltrace 1.
1593 */
1594 if (flush_buf(stderr, pretty)) {
1595 ret = -1;
1596 }
1597
1598 return ret;
1599 }
1600
1601 int pretty_print_discarded_items(struct pretty_component *pretty,
1602 const bt_message *msg)
1603 {
1604 const bt_clock_snapshot *begin = NULL;
1605 const bt_clock_snapshot *end = NULL;
1606 const bt_stream *stream;
1607 const bt_stream_class *stream_class;
1608 uint64_t count = UINT64_C(-1);
1609 const char *elem_type;
1610
1611 switch (bt_message_get_type(msg)) {
1612 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1613 stream = bt_message_discarded_events_borrow_stream_const(msg);
1614
1615 if (bt_message_discarded_events_get_count(msg, &count) ==
1616 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) {
1617 count = UINT64_C(-1);
1618 }
1619
1620 elem_type = "event";
1621 break;
1622 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1623 stream = bt_message_discarded_packets_borrow_stream_const(msg);
1624
1625 if (bt_message_discarded_packets_get_count(msg, &count) ==
1626 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) {
1627 count = UINT64_C(-1);
1628 }
1629
1630 elem_type = "packet";
1631 break;
1632 default:
1633 bt_common_abort();
1634 }
1635
1636 BT_ASSERT(stream);
1637 stream_class = bt_stream_borrow_class_const(stream);
1638
1639 switch (bt_message_get_type(msg)) {
1640 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1641 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
1642 stream_class)) {
1643 begin = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1644 msg);
1645 end = bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
1646 msg);
1647 }
1648
1649 break;
1650 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1651 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
1652 stream_class)) {
1653 begin = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1654 msg);
1655 end = bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1656 msg);
1657 }
1658
1659 break;
1660 default:
1661 bt_common_abort();
1662 }
1663
1664 print_discarded_elements_msg(pretty, stream, begin, end,
1665 count, elem_type);
1666 return 0;
1667 }
1668
1669 void pretty_print_init(void)
1670 {
1671 strcpy(color_name, bt_common_color_bold());
1672 strcpy(color_field_name, bt_common_color_fg_cyan());
1673 strcpy(color_rst, bt_common_color_reset());
1674 strcpy(color_string_value, bt_common_color_bold());
1675 strcpy(color_number_value, bt_common_color_bold());
1676 strcpy(color_enum_mapping_name, bt_common_color_bold());
1677 strcpy(color_unknown, bt_common_color_bold());
1678 strcat(color_unknown, bt_common_color_fg_bright_red());
1679 strcpy(color_event_name, bt_common_color_bold());
1680 strcat(color_event_name, bt_common_color_fg_bright_magenta());
1681 strcpy(color_timestamp, bt_common_color_bold());
1682 strcat(color_timestamp, bt_common_color_fg_bright_yellow());
1683 }
This page took 0.064982 seconds and 4 git commands to generate.