Enumeration mapping iterator's initial position is inconsistent
[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 ret = bt_ctf_field_type_enumeration_mapping_iterator_next(iter);
842 if (ret) {
843 if (pretty->use_colors) {
844 g_string_append(pretty->string, COLOR_UNKNOWN);
845 }
846 g_string_append(pretty->string, "<unknown>");
847 if (pretty->use_colors) {
848 g_string_append(pretty->string, COLOR_RST);
849 }
850 goto skip_loop;
851 }
852 for (;;) {
853 const char *mapping_name;
854
855 if (bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
856 iter, &mapping_name, NULL, NULL) < 0) {
857 ret = BT_COMPONENT_STATUS_ERROR;
858 goto end;
859 }
860 if (nr_mappings++)
861 g_string_append(pretty->string, ", ");
862 if (pretty->use_colors) {
863 g_string_append(pretty->string, COLOR_ENUM_MAPPING_NAME);
864 }
865 print_escape_string(pretty, mapping_name);
866 if (pretty->use_colors) {
867 g_string_append(pretty->string, COLOR_RST);
868 }
869 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter) < 0) {
870 break;
871 }
872 }
873 skip_loop:
874 g_string_append(pretty->string, " : container = ");
875 ret = print_integer(pretty, container_field);
876 if (ret != BT_COMPONENT_STATUS_OK) {
877 goto end;
878 }
879 g_string_append(pretty->string, " )");
880 end:
881 bt_put(iter);
882 bt_put(container_field_type);
883 bt_put(container_field);
884 bt_put(enumeration_field_type);
885 return ret;
886 }
887
888 static
889 int filter_field_name(struct pretty_component *pretty, const char *field_name,
890 GQuark *filter_fields, int filter_array_len)
891 {
892 int i;
893 GQuark field_quark = g_quark_try_string(field_name);
894
895 if (!field_quark || pretty->options.verbose) {
896 return 1;
897 }
898
899 for (i = 0; i < filter_array_len; i++) {
900 if (field_quark == filter_fields[i]) {
901 return 0;
902 }
903 }
904 return 1;
905 }
906
907 static
908 enum bt_component_status print_struct_field(struct pretty_component *pretty,
909 struct bt_ctf_field *_struct,
910 struct bt_ctf_field_type *struct_type,
911 int i, bool print_names, int *nr_printed_fields,
912 GQuark *filter_fields, int filter_array_len)
913 {
914 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
915 const char *field_name;
916 struct bt_ctf_field *field = NULL;
917 struct bt_ctf_field_type *field_type = NULL;;
918
919 field = bt_ctf_field_structure_get_field_by_index(_struct, i);
920 if (!field) {
921 ret = BT_COMPONENT_STATUS_ERROR;
922 goto end;
923 }
924 if (bt_ctf_field_type_structure_get_field(struct_type,
925 &field_name, &field_type, i) < 0) {
926 ret = BT_COMPONENT_STATUS_ERROR;
927 goto end;
928 }
929
930 if (filter_fields && !filter_field_name(pretty, field_name,
931 filter_fields, filter_array_len)) {
932 ret = BT_COMPONENT_STATUS_OK;
933 goto end;
934 }
935
936 if (*nr_printed_fields > 0) {
937 g_string_append(pretty->string, ", ");
938 } else {
939 g_string_append(pretty->string, " ");
940 }
941 if (print_names) {
942 print_field_name_equal(pretty, rem_(field_name));
943 }
944 ret = print_field(pretty, field, print_names, NULL, 0);
945 *nr_printed_fields += 1;
946 end:
947 bt_put(field_type);
948 bt_put(field);
949 return ret;
950 }
951
952 static
953 enum bt_component_status print_struct(struct pretty_component *pretty,
954 struct bt_ctf_field *_struct, bool print_names,
955 GQuark *filter_fields, int filter_array_len)
956 {
957 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
958 struct bt_ctf_field_type *struct_type = NULL;
959 int nr_fields, i, nr_printed_fields;
960
961 struct_type = bt_ctf_field_get_type(_struct);
962 if (!struct_type) {
963 ret = BT_COMPONENT_STATUS_ERROR;
964 goto end;
965 }
966 nr_fields = bt_ctf_field_type_structure_get_field_count(struct_type);
967 if (nr_fields < 0) {
968 ret = BT_COMPONENT_STATUS_ERROR;
969 goto end;
970 }
971 g_string_append(pretty->string, "{");
972 pretty->depth++;
973 nr_printed_fields = 0;
974 for (i = 0; i < nr_fields; i++) {
975 ret = print_struct_field(pretty, _struct, struct_type, i,
976 print_names, &nr_printed_fields, filter_fields,
977 filter_array_len);
978 if (ret != BT_COMPONENT_STATUS_OK) {
979 goto end;
980 }
981 }
982 pretty->depth--;
983 g_string_append(pretty->string, " }");
984 end:
985 bt_put(struct_type);
986 return ret;
987 }
988
989 static
990 enum bt_component_status print_array_field(struct pretty_component *pretty,
991 struct bt_ctf_field *array, uint64_t i,
992 bool is_string, bool print_names)
993 {
994 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
995 struct bt_ctf_field *field = NULL;
996
997 if (!is_string) {
998 if (i != 0) {
999 g_string_append(pretty->string, ", ");
1000 } else {
1001 g_string_append(pretty->string, " ");
1002 }
1003 if (print_names) {
1004 g_string_append_printf(pretty->string, "[%" PRIu64 "] = ", i);
1005 }
1006 }
1007 field = bt_ctf_field_array_get_field(array, i);
1008 if (!field) {
1009 ret = BT_COMPONENT_STATUS_ERROR;
1010 goto end;
1011 }
1012 ret = print_field(pretty, field, print_names, NULL, 0);
1013 end:
1014 bt_put(field);
1015 return ret;
1016 }
1017
1018 static
1019 enum bt_component_status print_array(struct pretty_component *pretty,
1020 struct bt_ctf_field *array, bool print_names)
1021 {
1022 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1023 struct bt_ctf_field_type *array_type = NULL, *field_type = NULL;
1024 enum bt_ctf_field_type_id type_id;
1025 int64_t len;
1026 uint64_t i;
1027 bool is_string = false;
1028
1029 array_type = bt_ctf_field_get_type(array);
1030 if (!array_type) {
1031 ret = BT_COMPONENT_STATUS_ERROR;
1032 goto end;
1033 }
1034 field_type = bt_ctf_field_type_array_get_element_type(array_type);
1035 if (!field_type) {
1036 ret = BT_COMPONENT_STATUS_ERROR;
1037 goto end;
1038 }
1039 len = bt_ctf_field_type_array_get_length(array_type);
1040 if (len < 0) {
1041 ret = BT_COMPONENT_STATUS_ERROR;
1042 goto end;
1043 }
1044 type_id = bt_ctf_field_type_get_type_id(field_type);
1045 if (type_id == BT_CTF_FIELD_TYPE_ID_INTEGER) {
1046 enum bt_ctf_string_encoding encoding;
1047
1048 encoding = bt_ctf_field_type_integer_get_encoding(field_type);
1049 if (encoding == BT_CTF_STRING_ENCODING_UTF8
1050 || encoding == BT_CTF_STRING_ENCODING_ASCII) {
1051 int integer_len, integer_alignment;
1052
1053 integer_len = bt_ctf_field_type_integer_get_size(field_type);
1054 if (integer_len < 0) {
1055 return BT_COMPONENT_STATUS_ERROR;
1056 }
1057 integer_alignment = bt_ctf_field_type_get_alignment(field_type);
1058 if (integer_alignment < 0) {
1059 return BT_COMPONENT_STATUS_ERROR;
1060 }
1061 if (integer_len == CHAR_BIT
1062 && integer_alignment == CHAR_BIT) {
1063 is_string = true;
1064 }
1065 }
1066 }
1067
1068 if (is_string) {
1069 g_string_assign(pretty->tmp_string, "");
1070 } else {
1071 g_string_append(pretty->string, "[");
1072 }
1073
1074 pretty->depth++;
1075 for (i = 0; i < len; i++) {
1076 ret = print_array_field(pretty, array, i, is_string, print_names);
1077 if (ret != BT_COMPONENT_STATUS_OK) {
1078 goto end;
1079 }
1080 }
1081 pretty->depth--;
1082
1083 if (is_string) {
1084 if (pretty->use_colors) {
1085 g_string_append(pretty->string, COLOR_STRING_VALUE);
1086 }
1087 print_escape_string(pretty, pretty->tmp_string->str);
1088 if (pretty->use_colors) {
1089 g_string_append(pretty->string, COLOR_RST);
1090 }
1091 } else {
1092 g_string_append(pretty->string, " ]");
1093 }
1094 end:
1095 bt_put(field_type);
1096 bt_put(array_type);
1097 return ret;
1098 }
1099
1100 static
1101 enum bt_component_status print_sequence_field(struct pretty_component *pretty,
1102 struct bt_ctf_field *seq, uint64_t i,
1103 bool is_string, bool print_names)
1104 {
1105 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1106 struct bt_ctf_field *field = NULL;
1107
1108 if (!is_string) {
1109 if (i != 0) {
1110 g_string_append(pretty->string, ", ");
1111 } else {
1112 g_string_append(pretty->string, " ");
1113 }
1114 if (print_names) {
1115 g_string_append_printf(pretty->string, "[%" PRIu64 "] = ", i);
1116 }
1117 }
1118 field = bt_ctf_field_sequence_get_field(seq, i);
1119 if (!field) {
1120 ret = BT_COMPONENT_STATUS_ERROR;
1121 goto end;
1122 }
1123 ret = print_field(pretty, field, print_names, NULL, 0);
1124 end:
1125 bt_put(field);
1126 return ret;
1127 }
1128
1129 static
1130 enum bt_component_status print_sequence(struct pretty_component *pretty,
1131 struct bt_ctf_field *seq, bool print_names)
1132 {
1133 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1134 struct bt_ctf_field_type *seq_type = NULL, *field_type = NULL;
1135 struct bt_ctf_field *length_field = NULL;
1136 enum bt_ctf_field_type_id type_id;
1137 uint64_t len;
1138 uint64_t i;
1139 bool is_string = false;
1140
1141 seq_type = bt_ctf_field_get_type(seq);
1142 if (!seq_type) {
1143 ret = BT_COMPONENT_STATUS_ERROR;
1144 goto end;
1145 }
1146 length_field = bt_ctf_field_sequence_get_length(seq);
1147 if (!length_field) {
1148 ret = BT_COMPONENT_STATUS_ERROR;
1149 goto end;
1150 }
1151 if (bt_ctf_field_unsigned_integer_get_value(length_field, &len) < 0) {
1152 ret = BT_COMPONENT_STATUS_ERROR;
1153 goto end;
1154 }
1155 field_type = bt_ctf_field_type_sequence_get_element_type(seq_type);
1156 if (!field_type) {
1157 ret = BT_COMPONENT_STATUS_ERROR;
1158 goto end;
1159 }
1160 type_id = bt_ctf_field_type_get_type_id(field_type);
1161 if (type_id == BT_CTF_FIELD_TYPE_ID_INTEGER) {
1162 enum bt_ctf_string_encoding encoding;
1163
1164 encoding = bt_ctf_field_type_integer_get_encoding(field_type);
1165 if (encoding == BT_CTF_STRING_ENCODING_UTF8
1166 || encoding == BT_CTF_STRING_ENCODING_ASCII) {
1167 int integer_len, integer_alignment;
1168
1169 integer_len = bt_ctf_field_type_integer_get_size(field_type);
1170 if (integer_len < 0) {
1171 ret = BT_COMPONENT_STATUS_ERROR;
1172 goto end;
1173 }
1174 integer_alignment = bt_ctf_field_type_get_alignment(field_type);
1175 if (integer_alignment < 0) {
1176 ret = BT_COMPONENT_STATUS_ERROR;
1177 goto end;
1178 }
1179 if (integer_len == CHAR_BIT
1180 && integer_alignment == CHAR_BIT) {
1181 is_string = true;
1182 }
1183 }
1184 }
1185
1186 if (is_string) {
1187 g_string_assign(pretty->tmp_string, "");
1188 } else {
1189 g_string_append(pretty->string, "[");
1190 }
1191
1192 pretty->depth++;
1193 for (i = 0; i < len; i++) {
1194 ret = print_sequence_field(pretty, seq, i,
1195 is_string, print_names);
1196 if (ret != BT_COMPONENT_STATUS_OK) {
1197 goto end;
1198 }
1199 }
1200 pretty->depth--;
1201
1202 if (is_string) {
1203 if (pretty->use_colors) {
1204 g_string_append(pretty->string, COLOR_STRING_VALUE);
1205 }
1206 print_escape_string(pretty, pretty->tmp_string->str);
1207 if (pretty->use_colors) {
1208 g_string_append(pretty->string, COLOR_RST);
1209 }
1210 } else {
1211 g_string_append(pretty->string, " ]");
1212 }
1213 end:
1214 bt_put(length_field);
1215 bt_put(field_type);
1216 bt_put(seq_type);
1217 return ret;
1218 }
1219
1220 static
1221 enum bt_component_status print_variant(struct pretty_component *pretty,
1222 struct bt_ctf_field *variant, bool print_names)
1223 {
1224 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1225 struct bt_ctf_field *field = NULL;
1226
1227 field = bt_ctf_field_variant_get_current_field(variant);
1228 if (!field) {
1229 ret = BT_COMPONENT_STATUS_ERROR;
1230 goto end;
1231 }
1232 g_string_append(pretty->string, "{ ");
1233 pretty->depth++;
1234 if (print_names) {
1235 int iter_ret;
1236 struct bt_ctf_field *tag_field = NULL;
1237 const char *tag_choice;
1238 struct bt_ctf_field_type_enumeration_mapping_iterator *iter;
1239
1240 tag_field = bt_ctf_field_variant_get_tag(variant);
1241 if (!tag_field) {
1242 ret = BT_COMPONENT_STATUS_ERROR;
1243 goto end;
1244 }
1245
1246 iter = bt_ctf_field_enumeration_get_mappings(tag_field);
1247 iter_ret = bt_ctf_field_type_enumeration_mapping_iterator_next(
1248 iter);
1249 if (!iter || ret) {
1250 bt_put(tag_field);
1251 ret = BT_COMPONENT_STATUS_ERROR;
1252 goto end;
1253 }
1254
1255 iter_ret =
1256 bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
1257 iter, &tag_choice, NULL, NULL);
1258 if (iter_ret) {
1259 bt_put(iter);
1260 bt_put(tag_field);
1261 ret = BT_COMPONENT_STATUS_ERROR;
1262 goto end;
1263 }
1264 print_field_name_equal(pretty, rem_(tag_choice));
1265 bt_put(tag_field);
1266 bt_put(iter);
1267 }
1268 ret = print_field(pretty, field, print_names, NULL, 0);
1269 if (ret != BT_COMPONENT_STATUS_OK) {
1270 goto end;
1271 }
1272 pretty->depth--;
1273 g_string_append(pretty->string, " }");
1274 end:
1275 bt_put(field);
1276 return ret;
1277 }
1278
1279 static
1280 enum bt_component_status print_field(struct pretty_component *pretty,
1281 struct bt_ctf_field *field, bool print_names,
1282 GQuark *filter_fields, int filter_array_len)
1283 {
1284 enum bt_ctf_field_type_id type_id;
1285
1286 type_id = bt_ctf_field_get_type_id(field);
1287 switch (type_id) {
1288 case CTF_TYPE_INTEGER:
1289 return print_integer(pretty, field);
1290 case CTF_TYPE_FLOAT:
1291 {
1292 double v;
1293
1294 if (bt_ctf_field_floating_point_get_value(field, &v)) {
1295 return BT_COMPONENT_STATUS_ERROR;
1296 }
1297 if (pretty->use_colors) {
1298 g_string_append(pretty->string, COLOR_NUMBER_VALUE);
1299 }
1300 g_string_append_printf(pretty->string, "%g", v);
1301 if (pretty->use_colors) {
1302 g_string_append(pretty->string, COLOR_RST);
1303 }
1304 return BT_COMPONENT_STATUS_OK;
1305 }
1306 case CTF_TYPE_ENUM:
1307 return print_enum(pretty, field);
1308 case CTF_TYPE_STRING:
1309 {
1310 const char *str;
1311
1312 str = bt_ctf_field_string_get_value(field);
1313 if (!str) {
1314 return BT_COMPONENT_STATUS_ERROR;
1315 }
1316
1317 if (pretty->use_colors) {
1318 g_string_append(pretty->string, COLOR_STRING_VALUE);
1319 }
1320 print_escape_string(pretty, str);
1321 if (pretty->use_colors) {
1322 g_string_append(pretty->string, COLOR_RST);
1323 }
1324 return BT_COMPONENT_STATUS_OK;
1325 }
1326 case CTF_TYPE_STRUCT:
1327 return print_struct(pretty, field, print_names, filter_fields,
1328 filter_array_len);
1329 case CTF_TYPE_VARIANT:
1330 return print_variant(pretty, field, print_names);
1331 case CTF_TYPE_ARRAY:
1332 return print_array(pretty, field, print_names);
1333 case CTF_TYPE_SEQUENCE:
1334 return print_sequence(pretty, field, print_names);
1335 default:
1336 // TODO: log instead
1337 fprintf(pretty->err, "[error] Unknown type id: %d\n", (int) type_id);
1338 return BT_COMPONENT_STATUS_ERROR;
1339 }
1340 }
1341
1342 static
1343 enum bt_component_status print_stream_packet_context(struct pretty_component *pretty,
1344 struct bt_ctf_event *event)
1345 {
1346 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1347 struct bt_ctf_packet *packet = NULL;
1348 struct bt_ctf_field *main_field = NULL;
1349
1350 packet = bt_ctf_event_get_packet(event);
1351 if (!packet) {
1352 ret = BT_COMPONENT_STATUS_ERROR;
1353 goto end;
1354 }
1355 main_field = bt_ctf_packet_get_context(packet);
1356 if (!main_field) {
1357 goto end;
1358 }
1359 if (!pretty->start_line) {
1360 g_string_append(pretty->string, ", ");
1361 }
1362 pretty->start_line = false;
1363 if (pretty->options.print_scope_field_names) {
1364 print_name_equal(pretty, "stream.packet.context");
1365 }
1366 ret = print_field(pretty, main_field,
1367 pretty->options.print_context_field_names,
1368 stream_packet_context_quarks,
1369 STREAM_PACKET_CONTEXT_QUARKS_LEN);
1370 end:
1371 bt_put(main_field);
1372 bt_put(packet);
1373 return ret;
1374 }
1375
1376 static
1377 enum bt_component_status print_event_header_raw(struct pretty_component *pretty,
1378 struct bt_ctf_event *event)
1379 {
1380 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1381 struct bt_ctf_field *main_field = NULL;
1382
1383 main_field = bt_ctf_event_get_header(event);
1384 if (!main_field) {
1385 goto end;
1386 }
1387 if (!pretty->start_line) {
1388 g_string_append(pretty->string, ", ");
1389 }
1390 pretty->start_line = false;
1391 if (pretty->options.print_scope_field_names) {
1392 print_name_equal(pretty, "stream.event.header");
1393 }
1394 ret = print_field(pretty, main_field,
1395 pretty->options.print_header_field_names, NULL, 0);
1396 end:
1397 bt_put(main_field);
1398 return ret;
1399 }
1400
1401 static
1402 enum bt_component_status print_stream_event_context(struct pretty_component *pretty,
1403 struct bt_ctf_event *event)
1404 {
1405 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1406 struct bt_ctf_field *main_field = NULL;
1407
1408 main_field = bt_ctf_event_get_stream_event_context(event);
1409 if (!main_field) {
1410 goto end;
1411 }
1412 if (!pretty->start_line) {
1413 g_string_append(pretty->string, ", ");
1414 }
1415 pretty->start_line = false;
1416 if (pretty->options.print_scope_field_names) {
1417 print_name_equal(pretty, "stream.event.context");
1418 }
1419 ret = print_field(pretty, main_field,
1420 pretty->options.print_context_field_names, NULL, 0);
1421 end:
1422 bt_put(main_field);
1423 return ret;
1424 }
1425
1426 static
1427 enum bt_component_status print_event_context(struct pretty_component *pretty,
1428 struct bt_ctf_event *event)
1429 {
1430 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1431 struct bt_ctf_field *main_field = NULL;
1432
1433 main_field = bt_ctf_event_get_event_context(event);
1434 if (!main_field) {
1435 goto end;
1436 }
1437 if (!pretty->start_line) {
1438 g_string_append(pretty->string, ", ");
1439 }
1440 pretty->start_line = false;
1441 if (pretty->options.print_scope_field_names) {
1442 print_name_equal(pretty, "event.context");
1443 }
1444 ret = print_field(pretty, main_field,
1445 pretty->options.print_context_field_names, NULL, 0);
1446 end:
1447 bt_put(main_field);
1448 return ret;
1449 }
1450
1451 static
1452 enum bt_component_status print_event_payload(struct pretty_component *pretty,
1453 struct bt_ctf_event *event)
1454 {
1455 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1456 struct bt_ctf_field *main_field = NULL;
1457
1458 main_field = bt_ctf_event_get_event_payload(event);
1459 if (!main_field) {
1460 goto end;
1461 }
1462 if (!pretty->start_line) {
1463 g_string_append(pretty->string, ", ");
1464 }
1465 pretty->start_line = false;
1466 if (pretty->options.print_scope_field_names) {
1467 print_name_equal(pretty, "event.fields");
1468 }
1469 ret = print_field(pretty, main_field,
1470 pretty->options.print_payload_field_names, NULL, 0);
1471 end:
1472 bt_put(main_field);
1473 return ret;
1474 }
1475
1476 static
1477 int flush_buf(struct pretty_component *pretty)
1478 {
1479 int ret = 0;
1480
1481 if (pretty->string->len == 0) {
1482 goto end;
1483 }
1484
1485 if (fwrite(pretty->string->str, pretty->string->len, 1, pretty->out) != 1) {
1486 ret = -1;
1487 }
1488
1489 end:
1490 return ret;
1491 }
1492
1493 BT_HIDDEN
1494 enum bt_component_status pretty_print_event(struct pretty_component *pretty,
1495 struct bt_notification *event_notif)
1496 {
1497 enum bt_component_status ret;
1498 struct bt_ctf_event *event =
1499 bt_notification_event_get_event(event_notif);
1500 struct bt_clock_class_priority_map *cc_prio_map =
1501 bt_notification_event_get_clock_class_priority_map(event_notif);
1502
1503 assert(event);
1504 assert(cc_prio_map);
1505 pretty->start_line = true;
1506 g_string_assign(pretty->string, "");
1507 ret = print_event_header(pretty, event, cc_prio_map);
1508 if (ret != BT_COMPONENT_STATUS_OK) {
1509 goto end;
1510 }
1511
1512 ret = print_stream_packet_context(pretty, event);
1513 if (ret != BT_COMPONENT_STATUS_OK) {
1514 goto end;
1515 }
1516
1517 if (pretty->options.verbose) {
1518 ret = print_event_header_raw(pretty, event);
1519 if (ret != BT_COMPONENT_STATUS_OK) {
1520 goto end;
1521 }
1522 }
1523
1524 ret = print_stream_event_context(pretty, event);
1525 if (ret != BT_COMPONENT_STATUS_OK) {
1526 goto end;
1527 }
1528
1529 ret = print_event_context(pretty, event);
1530 if (ret != BT_COMPONENT_STATUS_OK) {
1531 goto end;
1532 }
1533
1534 ret = print_event_payload(pretty, event);
1535 if (ret != BT_COMPONENT_STATUS_OK) {
1536 goto end;
1537 }
1538
1539 g_string_append_c(pretty->string, '\n');
1540 if (flush_buf(pretty)) {
1541 ret = BT_COMPONENT_STATUS_ERROR;
1542 goto end;
1543 }
1544
1545 end:
1546 bt_put(event);
1547 bt_put(cc_prio_map);
1548 return ret;
1549 }
1550
1551 BT_HIDDEN
1552 enum bt_component_status pretty_print_discarded_elements(
1553 struct pretty_component *pretty,
1554 struct bt_notification *notif)
1555 {
1556 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1557 struct bt_ctf_stream *stream = NULL;
1558 struct bt_ctf_stream_class *stream_class = NULL;
1559 struct bt_ctf_trace *trace = NULL;
1560 const char *stream_name;
1561 const char *trace_name;
1562 const unsigned char *trace_uuid;
1563 int64_t stream_class_id;
1564 int64_t stream_id;
1565 bool is_discarded_events;
1566 int64_t count;
1567 struct bt_ctf_clock_value *clock_value = NULL;
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 clock_value = is_discarded_events ?
1621 bt_notification_discarded_events_get_begin_clock_value(notif) :
1622 bt_notification_discarded_packets_get_begin_clock_value(notif);
1623 print_timestamp_wall(pretty, clock_value);
1624 BT_PUT(clock_value);
1625 fprintf(stderr, "%s] and [", pretty->string->str);
1626 g_string_assign(pretty->string, "");
1627 clock_value = is_discarded_events ?
1628 bt_notification_discarded_events_get_end_clock_value(notif) :
1629 bt_notification_discarded_packets_get_end_clock_value(notif);
1630 print_timestamp_wall(pretty, clock_value);
1631 BT_PUT(clock_value);
1632 fprintf(stderr, "%s] in trace \"%s\" ",
1633 pretty->string->str, trace_name);
1634
1635 if (trace_uuid) {
1636 fprintf(stderr,
1637 "(UUID: %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x) ",
1638 trace_uuid[0],
1639 trace_uuid[1],
1640 trace_uuid[2],
1641 trace_uuid[3],
1642 trace_uuid[4],
1643 trace_uuid[5],
1644 trace_uuid[6],
1645 trace_uuid[7],
1646 trace_uuid[8],
1647 trace_uuid[9],
1648 trace_uuid[10],
1649 trace_uuid[11],
1650 trace_uuid[12],
1651 trace_uuid[13],
1652 trace_uuid[14],
1653 trace_uuid[15]);
1654 } else {
1655 fprintf(stderr, "(no UUID) ");
1656 }
1657
1658 fprintf(stderr, "within stream \"%s\" (stream class ID: %" PRId64 ", ",
1659 stream_name, stream_class_id);
1660
1661 if (stream_id >= 0) {
1662 fprintf(stderr, "stream ID: %" PRId64, stream_id);
1663 } else {
1664 fprintf(stderr, "no stream ID");
1665 }
1666
1667 fprintf(stderr, ").%s\n", bt_common_color_reset());
1668 bt_put(stream);
1669 bt_put(stream_class);
1670 bt_put(trace);
1671 bt_put(clock_value);
1672 return ret;
1673 }
This page took 0.110956 seconds and 4 git commands to generate.