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