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