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