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