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