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