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