2 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #include <babeltrace/babeltrace.h>
27 #include <babeltrace/bitfield-internal.h>
28 #include <babeltrace/common-internal.h>
29 #include <babeltrace/compat/time-internal.h>
30 #include <babeltrace/assert-internal.h>
35 #define NSEC_PER_SEC 1000000000LL
37 #define COLOR_NAME BT_COMMON_COLOR_BOLD
38 #define COLOR_FIELD_NAME BT_COMMON_COLOR_FG_CYAN
39 #define COLOR_RST BT_COMMON_COLOR_RESET
40 #define COLOR_STRING_VALUE BT_COMMON_COLOR_BOLD
41 #define COLOR_NUMBER_VALUE BT_COMMON_COLOR_BOLD
42 #define COLOR_ENUM_MAPPING_NAME BT_COMMON_COLOR_BOLD
43 #define COLOR_UNKNOWN BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_RED
44 #define COLOR_EVENT_NAME BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_MAGENTA
45 #define COLOR_TIMESTAMP BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_YELLOW
48 int64_t real_timestamp
; /* Relative to UNIX epoch. */
49 uint64_t clock_snapshot
; /* In cycles. */
53 int print_field(struct pretty_component
*pretty
,
54 const bt_field
*field
, bool print_names
,
55 GQuark
*filters_fields
, int filter_array_len
);
58 void print_name_equal(struct pretty_component
*pretty
, const char *name
)
60 if (pretty
->use_colors
) {
61 g_string_append_printf(pretty
->string
, "%s%s%s = ", COLOR_NAME
,
64 g_string_append_printf(pretty
->string
, "%s = ", name
);
69 void print_field_name_equal(struct pretty_component
*pretty
, const char *name
)
71 if (pretty
->use_colors
) {
72 g_string_append_printf(pretty
->string
, "%s%s%s = ",
73 COLOR_FIELD_NAME
, name
, COLOR_RST
);
75 g_string_append_printf(pretty
->string
, "%s = ", name
);
80 void print_timestamp_cycles(struct pretty_component
*pretty
,
81 const bt_clock_snapshot
*clock_snapshot
, bool update_last
)
85 cycles
= bt_clock_snapshot_get_value(clock_snapshot
);
86 g_string_append_printf(pretty
->string
, "%020" PRIu64
, cycles
);
89 if (pretty
->last_cycles_timestamp
!= -1ULL) {
90 pretty
->delta_cycles
= cycles
- pretty
->last_cycles_timestamp
;
93 pretty
->last_cycles_timestamp
= cycles
;
98 void print_timestamp_wall(struct pretty_component
*pretty
,
99 const bt_clock_snapshot
*clock_snapshot
, bool update_last
)
102 int64_t ts_nsec
= 0; /* add configurable offset */
103 int64_t ts_sec
= 0; /* add configurable offset */
104 uint64_t ts_sec_abs
, ts_nsec_abs
;
107 if (!clock_snapshot
) {
108 g_string_append(pretty
->string
, "??:??:??.?????????");
112 ret
= bt_clock_snapshot_get_ns_from_origin(clock_snapshot
, &ts_nsec
);
114 // TODO: log, this is unexpected
115 g_string_append(pretty
->string
, "Error");
120 if (pretty
->last_real_timestamp
!= -1ULL) {
121 pretty
->delta_real_timestamp
= ts_nsec
- pretty
->last_real_timestamp
;
124 pretty
->last_real_timestamp
= ts_nsec
;
127 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
128 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
130 if (ts_sec
>= 0 && ts_nsec
>= 0) {
133 ts_nsec_abs
= ts_nsec
;
134 } else if (ts_sec
> 0 && ts_nsec
< 0) {
136 ts_sec_abs
= ts_sec
- 1;
137 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
138 } else if (ts_sec
== 0 && ts_nsec
< 0) {
141 ts_nsec_abs
= -ts_nsec
;
142 } else if (ts_sec
< 0 && ts_nsec
> 0) {
144 ts_sec_abs
= -(ts_sec
+ 1);
145 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
146 } else if (ts_sec
< 0 && ts_nsec
== 0) {
148 ts_sec_abs
= -ts_sec
;
149 ts_nsec_abs
= ts_nsec
;
150 } else { /* (ts_sec < 0 && ts_nsec < 0) */
152 ts_sec_abs
= -ts_sec
;
153 ts_nsec_abs
= -ts_nsec
;
156 if (!pretty
->options
.clock_seconds
) {
158 time_t time_s
= (time_t) ts_sec_abs
;
160 if (is_negative
&& !pretty
->negative_timestamp_warning_done
) {
162 fprintf(stderr
, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
163 pretty
->negative_timestamp_warning_done
= true;
167 if (!pretty
->options
.clock_gmt
) {
170 res
= bt_localtime_r(&time_s
, &tm
);
173 fprintf(stderr
, "[warning] Unable to get localtime.\n");
179 res
= bt_gmtime_r(&time_s
, &tm
);
182 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
186 if (pretty
->options
.clock_date
) {
190 /* Print date and time */
191 res
= strftime(timestr
, sizeof(timestr
),
195 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
199 g_string_append(pretty
->string
, timestr
);
202 /* Print time in HH:MM:SS.ns */
203 g_string_append_printf(pretty
->string
,
204 "%02d:%02d:%02d.%09" PRIu64
, tm
.tm_hour
, tm
.tm_min
,
205 tm
.tm_sec
, ts_nsec_abs
);
209 g_string_append_printf(pretty
->string
, "%s%" PRId64
".%09" PRIu64
,
210 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
216 int print_event_timestamp(struct pretty_component
*pretty
,
217 const bt_message
*event_msg
, bool *start_line
)
219 bool print_names
= pretty
->options
.print_header_field_names
;
221 const bt_clock_snapshot
*clock_snapshot
= NULL
;
223 if (!bt_message_event_borrow_stream_class_default_clock_class_const(
225 /* No default clock class: skip the timestamp without an error */
229 if (bt_message_event_borrow_default_clock_snapshot_const(event_msg
,
230 &clock_snapshot
) != BT_CLOCK_SNAPSHOT_STATE_KNOWN
) {
232 * No known default clock value: skip the timestamp
239 print_name_equal(pretty
, "timestamp");
241 g_string_append(pretty
->string
, "[");
243 if (pretty
->use_colors
) {
244 g_string_append(pretty
->string
, COLOR_TIMESTAMP
);
246 if (pretty
->options
.print_timestamp_cycles
) {
247 print_timestamp_cycles(pretty
, clock_snapshot
, true);
249 print_timestamp_wall(pretty
, clock_snapshot
, true);
251 if (pretty
->use_colors
) {
252 g_string_append(pretty
->string
, COLOR_RST
);
256 g_string_append(pretty
->string
, "] ");
258 if (pretty
->options
.print_delta_field
) {
260 g_string_append(pretty
->string
, ", ");
261 print_name_equal(pretty
, "delta");
263 g_string_append(pretty
->string
, "(");
265 if (pretty
->options
.print_timestamp_cycles
) {
266 if (pretty
->delta_cycles
== -1ULL) {
267 g_string_append(pretty
->string
,
268 "+??????????\?\?"); /* Not a trigraph. */
270 g_string_append_printf(pretty
->string
,
271 "+%012" PRIu64
, pretty
->delta_cycles
);
274 if (pretty
->delta_real_timestamp
!= -1ULL) {
275 uint64_t delta_sec
, delta_nsec
, delta
;
277 delta
= pretty
->delta_real_timestamp
;
278 delta_sec
= delta
/ NSEC_PER_SEC
;
279 delta_nsec
= delta
% NSEC_PER_SEC
;
280 g_string_append_printf(pretty
->string
,
281 "+%" PRIu64
".%09" PRIu64
,
282 delta_sec
, delta_nsec
);
284 g_string_append(pretty
->string
, "+?.?????????");
288 g_string_append(pretty
->string
, ") ");
291 *start_line
= !print_names
;
298 int print_event_header(struct pretty_component
*pretty
,
299 const bt_message
*event_msg
)
301 bool print_names
= pretty
->options
.print_header_field_names
;
303 const bt_event_class
*event_class
= NULL
;
304 const bt_stream_class
*stream_class
= NULL
;
305 const bt_trace_class
*trace_class
= NULL
;
306 const bt_packet
*packet
= NULL
;
307 const bt_stream
*stream
= NULL
;
308 const bt_trace
*trace
= NULL
;
309 const bt_event
*event
= bt_message_event_borrow_event_const(event_msg
);
311 bt_property_availability prop_avail
;
313 event_class
= bt_event_borrow_class_const(event
);
314 stream_class
= bt_event_class_borrow_stream_class_const(event_class
);
315 trace_class
= bt_stream_class_borrow_trace_class_const(stream_class
);
316 packet
= bt_event_borrow_packet_const(event
);
317 stream
= bt_packet_borrow_stream_const(packet
);
318 trace
= bt_stream_borrow_trace_const(stream
);
319 ret
= print_event_timestamp(pretty
, event_msg
, &pretty
->start_line
);
323 if (pretty
->options
.print_trace_field
) {
326 name
= bt_trace_get_name(trace
);
328 if (!pretty
->start_line
) {
329 g_string_append(pretty
->string
, ", ");
332 print_name_equal(pretty
, "trace");
335 g_string_append(pretty
->string
, name
);
338 g_string_append(pretty
->string
, ", ");
342 if (pretty
->options
.print_trace_hostname_field
) {
343 const bt_value
*hostname_str
;
345 hostname_str
= bt_trace_class_borrow_environment_entry_value_by_name_const(
346 trace_class
, "hostname");
350 if (!pretty
->start_line
) {
351 g_string_append(pretty
->string
, ", ");
354 print_name_equal(pretty
, "trace:hostname");
356 str
= bt_value_string_get(hostname_str
);
357 g_string_append(pretty
->string
, str
);
361 if (pretty
->options
.print_trace_domain_field
) {
362 const bt_value
*domain_str
;
364 domain_str
= bt_trace_class_borrow_environment_entry_value_by_name_const(
365 trace_class
, "domain");
369 if (!pretty
->start_line
) {
370 g_string_append(pretty
->string
, ", ");
373 print_name_equal(pretty
, "trace:domain");
374 } else if (dom_print
) {
375 g_string_append(pretty
->string
, ":");
377 str
= bt_value_string_get(domain_str
);
378 g_string_append(pretty
->string
, str
);
382 if (pretty
->options
.print_trace_procname_field
) {
383 const bt_value
*procname_str
;
385 procname_str
= bt_trace_class_borrow_environment_entry_value_by_name_const(
386 trace_class
, "procname");
390 if (!pretty
->start_line
) {
391 g_string_append(pretty
->string
, ", ");
394 print_name_equal(pretty
, "trace:procname");
395 } else if (dom_print
) {
396 g_string_append(pretty
->string
, ":");
398 str
= bt_value_string_get(procname_str
);
399 g_string_append(pretty
->string
, str
);
403 if (pretty
->options
.print_trace_vpid_field
) {
404 const bt_value
*vpid_value
;
406 vpid_value
= bt_trace_class_borrow_environment_entry_value_by_name_const(
407 trace_class
, "vpid");
411 if (!pretty
->start_line
) {
412 g_string_append(pretty
->string
, ", ");
415 print_name_equal(pretty
, "trace:vpid");
416 } else if (dom_print
) {
417 g_string_append(pretty
->string
, ":");
419 value
= bt_value_signed_integer_get(vpid_value
);
420 g_string_append_printf(pretty
->string
,
421 "(%" PRId64
")", value
);
425 if (pretty
->options
.print_loglevel_field
) {
426 static const char *log_level_names
[] = {
427 [ BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
] = "TRACE_EMERG",
428 [ BT_EVENT_CLASS_LOG_LEVEL_ALERT
] = "TRACE_ALERT",
429 [ BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
] = "TRACE_CRIT",
430 [ BT_EVENT_CLASS_LOG_LEVEL_ERROR
] = "TRACE_ERR",
431 [ BT_EVENT_CLASS_LOG_LEVEL_WARNING
] = "TRACE_WARNING",
432 [ BT_EVENT_CLASS_LOG_LEVEL_NOTICE
] = "TRACE_NOTICE",
433 [ BT_EVENT_CLASS_LOG_LEVEL_INFO
] = "TRACE_INFO",
434 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
435 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
436 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
437 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
438 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
439 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
440 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
441 [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG
] = "TRACE_DEBUG",
443 bt_event_class_log_level log_level
;
444 const char *log_level_str
= NULL
;
446 prop_avail
= bt_event_class_get_log_level(event_class
,
448 if (prop_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
449 log_level_str
= log_level_names
[log_level
];
450 BT_ASSERT(log_level_str
);
452 if (!pretty
->start_line
) {
453 g_string_append(pretty
->string
, ", ");
456 print_name_equal(pretty
, "loglevel");
457 } else if (dom_print
) {
458 g_string_append(pretty
->string
, ":");
461 g_string_append(pretty
->string
, log_level_str
);
462 g_string_append_printf(
463 pretty
->string
, " (%d)", (int) log_level
);
467 if (pretty
->options
.print_emf_field
) {
470 uri_str
= bt_event_class_get_emf_uri(event_class
);
472 if (!pretty
->start_line
) {
473 g_string_append(pretty
->string
, ", ");
476 print_name_equal(pretty
, "model.emf.uri");
477 } else if (dom_print
) {
478 g_string_append(pretty
->string
, ":");
481 g_string_append(pretty
->string
, uri_str
);
485 if (dom_print
&& !print_names
) {
486 g_string_append(pretty
->string
, " ");
488 if (!pretty
->start_line
) {
489 g_string_append(pretty
->string
, ", ");
491 pretty
->start_line
= true;
493 print_name_equal(pretty
, "name");
495 if (pretty
->use_colors
) {
496 g_string_append(pretty
->string
, COLOR_EVENT_NAME
);
498 g_string_append(pretty
->string
, bt_event_class_get_name(event_class
));
499 if (pretty
->use_colors
) {
500 g_string_append(pretty
->string
, COLOR_RST
);
503 g_string_append(pretty
->string
, ": ");
505 g_string_append(pretty
->string
, ", ");
513 int print_integer(struct pretty_component
*pretty
,
514 const bt_field
*field
)
517 bt_field_class_integer_preferred_display_base base
;
518 const bt_field_class
*int_fc
;
523 bool rst_color
= false;
524 bt_field_class_type ft_type
;
526 int_fc
= bt_field_borrow_class_const(field
);
528 ft_type
= bt_field_get_class_type(field
);
529 if (ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
530 ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
) {
531 v
.u
= bt_field_unsigned_integer_get_value(field
);
533 v
.s
= bt_field_signed_integer_get_value(field
);
536 if (pretty
->use_colors
) {
537 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
541 base
= bt_field_class_integer_get_preferred_display_base(int_fc
);
543 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
547 len
= bt_field_class_integer_get_field_value_range(int_fc
);
548 g_string_append(pretty
->string
, "0b");
549 v
.u
= _bt_piecewise_lshift(v
.u
, 64 - len
);
550 for (bitnr
= 0; bitnr
< len
; bitnr
++) {
551 g_string_append_printf(pretty
->string
, "%u", (v
.u
& (1ULL << 63)) ? 1 : 0);
552 v
.u
= _bt_piecewise_lshift(v
.u
, 1);
556 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
558 if (ft_type
== BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
||
559 ft_type
== BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
) {
562 len
= bt_field_class_integer_get_field_value_range(
568 /* Round length to the nearest 3-bit */
569 rounded_len
= (((len
- 1) / 3) + 1) * 3;
570 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
574 g_string_append_printf(pretty
->string
, "0%" PRIo64
, v
.u
);
577 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
578 if (ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
||
579 ft_type
== BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
) {
580 g_string_append_printf(pretty
->string
, "%" PRIu64
, v
.u
);
582 g_string_append_printf(pretty
->string
, "%" PRId64
, v
.s
);
585 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
589 len
= bt_field_class_integer_get_field_value_range(int_fc
);
591 /* Round length to the nearest nibble */
592 uint8_t rounded_len
= ((len
+ 3) & ~0x3);
594 v
.u
&= ((uint64_t) 1 << rounded_len
) - 1;
597 g_string_append_printf(pretty
->string
, "0x%" PRIX64
, v
.u
);
606 g_string_append(pretty
->string
, COLOR_RST
);
612 void print_escape_string(struct pretty_component
*pretty
, const char *str
)
616 g_string_append_c(pretty
->string
, '"');
618 for (i
= 0; i
< strlen(str
); i
++) {
619 /* Escape sequences not recognized by iscntrl(). */
622 g_string_append(pretty
->string
, "\\\\");
625 g_string_append(pretty
->string
, "\\\'");
628 g_string_append(pretty
->string
, "\\\"");
631 g_string_append(pretty
->string
, "\\\?");
635 /* Standard characters. */
636 if (!iscntrl(str
[i
])) {
637 g_string_append_c(pretty
->string
, str
[i
]);
643 g_string_append(pretty
->string
, "\\0");
646 g_string_append(pretty
->string
, "\\a");
649 g_string_append(pretty
->string
, "\\b");
652 g_string_append(pretty
->string
, "\\e");
655 g_string_append(pretty
->string
, "\\f");
658 g_string_append(pretty
->string
, "\\n");
661 g_string_append(pretty
->string
, "\\r");
664 g_string_append(pretty
->string
, "\\t");
667 g_string_append(pretty
->string
, "\\v");
670 /* Unhandled control-sequence, print as hex. */
671 g_string_append_printf(pretty
->string
, "\\x%02x", str
[i
]);
676 g_string_append_c(pretty
->string
, '"');
680 int print_enum(struct pretty_component
*pretty
,
681 const bt_field
*field
)
684 const bt_field_class
*enumeration_field_class
= NULL
;
685 bt_field_class_enumeration_mapping_label_array label_array
;
686 uint64_t label_count
;
689 enumeration_field_class
= bt_field_borrow_class_const(field
);
690 if (!enumeration_field_class
) {
695 switch (bt_field_get_class_type(field
)) {
696 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
697 ret
= bt_field_unsigned_enumeration_get_mapping_labels(field
,
698 &label_array
, &label_count
);
700 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
701 ret
= bt_field_signed_enumeration_get_mapping_labels(field
,
702 &label_array
, &label_count
);
713 g_string_append(pretty
->string
, "( ");
714 if (label_count
== 0) {
715 if (pretty
->use_colors
) {
716 g_string_append(pretty
->string
, COLOR_UNKNOWN
);
718 g_string_append(pretty
->string
, "<unknown>");
719 if (pretty
->use_colors
) {
720 g_string_append(pretty
->string
, COLOR_RST
);
724 for (i
= 0; i
< label_count
; i
++) {
725 const char *mapping_name
= label_array
[i
];
728 g_string_append(pretty
->string
, ", ");
730 if (pretty
->use_colors
) {
731 g_string_append(pretty
->string
, COLOR_ENUM_MAPPING_NAME
);
733 print_escape_string(pretty
, mapping_name
);
734 if (pretty
->use_colors
) {
735 g_string_append(pretty
->string
, COLOR_RST
);
739 g_string_append(pretty
->string
, " : container = ");
740 ret
= print_integer(pretty
, field
);
744 g_string_append(pretty
->string
, " )");
750 int filter_field_name(struct pretty_component
*pretty
, const char *field_name
,
751 GQuark
*filter_fields
, int filter_array_len
)
754 GQuark field_quark
= g_quark_try_string(field_name
);
756 if (!field_quark
|| pretty
->options
.verbose
) {
760 for (i
= 0; i
< filter_array_len
; i
++) {
761 if (field_quark
== filter_fields
[i
]) {
769 int print_struct_field(struct pretty_component
*pretty
,
770 const bt_field
*_struct
,
771 const bt_field_class
*struct_class
,
772 uint64_t i
, bool print_names
, uint64_t *nr_printed_fields
,
773 GQuark
*filter_fields
, int filter_array_len
)
776 const char *field_name
;
777 const bt_field
*field
= NULL
;
778 const bt_field_class_structure_member
*member
;
780 field
= bt_field_structure_borrow_member_field_by_index_const(_struct
, i
);
786 member
= bt_field_class_structure_borrow_member_by_index_const(
788 field_name
= bt_field_class_structure_member_get_name(member
);
790 if (filter_fields
&& !filter_field_name(pretty
, field_name
,
791 filter_fields
, filter_array_len
)) {
796 if (*nr_printed_fields
> 0) {
797 g_string_append(pretty
->string
, ", ");
799 g_string_append(pretty
->string
, " ");
802 print_field_name_equal(pretty
, field_name
);
804 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
805 *nr_printed_fields
+= 1;
812 int print_struct(struct pretty_component
*pretty
,
813 const bt_field
*_struct
, bool print_names
,
814 GQuark
*filter_fields
, int filter_array_len
)
817 const bt_field_class
*struct_class
= NULL
;
818 uint64_t nr_fields
, i
, nr_printed_fields
;
820 struct_class
= bt_field_borrow_class_const(_struct
);
825 nr_fields
= bt_field_class_structure_get_member_count(struct_class
);
830 g_string_append(pretty
->string
, "{");
832 nr_printed_fields
= 0;
833 for (i
= 0; i
< nr_fields
; i
++) {
834 ret
= print_struct_field(pretty
, _struct
, struct_class
, i
,
835 print_names
, &nr_printed_fields
, filter_fields
,
842 g_string_append(pretty
->string
, " }");
849 int print_array_field(struct pretty_component
*pretty
,
850 const bt_field
*array
, uint64_t i
, bool print_names
)
852 const bt_field
*field
= NULL
;
855 g_string_append(pretty
->string
, ", ");
857 g_string_append(pretty
->string
, " ");
860 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
863 field
= bt_field_array_borrow_element_field_by_index_const(array
, i
);
865 return print_field(pretty
, field
, print_names
, NULL
, 0);
869 int print_array(struct pretty_component
*pretty
,
870 const bt_field
*array
, bool print_names
)
873 const bt_field_class
*array_class
= NULL
;
877 array_class
= bt_field_borrow_class_const(array
);
882 len
= bt_field_array_get_length(array
);
883 g_string_append(pretty
->string
, "[");
885 for (i
= 0; i
< len
; i
++) {
886 ret
= print_array_field(pretty
, array
, i
, print_names
);
892 g_string_append(pretty
->string
, " ]");
899 int print_sequence_field(struct pretty_component
*pretty
,
900 const bt_field
*seq
, uint64_t i
, bool print_names
)
902 const bt_field
*field
= NULL
;
905 g_string_append(pretty
->string
, ", ");
907 g_string_append(pretty
->string
, " ");
910 g_string_append_printf(pretty
->string
, "[%" PRIu64
"] = ", i
);
913 field
= bt_field_array_borrow_element_field_by_index_const(seq
, i
);
915 return print_field(pretty
, field
, print_names
, NULL
, 0);
919 int print_sequence(struct pretty_component
*pretty
,
920 const bt_field
*seq
, bool print_names
)
926 len
= bt_field_array_get_length(seq
);
932 g_string_append(pretty
->string
, "[");
935 for (i
= 0; i
< len
; i
++) {
936 ret
= print_sequence_field(pretty
, seq
, i
, print_names
);
942 g_string_append(pretty
->string
, " ]");
949 int print_variant(struct pretty_component
*pretty
,
950 const bt_field
*variant
, bool print_names
)
953 const bt_field
*field
= NULL
;
955 field
= bt_field_variant_borrow_selected_option_field_const(variant
);
957 g_string_append(pretty
->string
, "{ ");
960 // TODO: find tag's name using field path
961 // print_field_name_equal(pretty, tag_choice);
963 ret
= print_field(pretty
, field
, print_names
, NULL
, 0);
968 g_string_append(pretty
->string
, " }");
975 int print_field(struct pretty_component
*pretty
,
976 const bt_field
*field
, bool print_names
,
977 GQuark
*filter_fields
, int filter_array_len
)
979 bt_field_class_type class_id
;
981 class_id
= bt_field_get_class_type(field
);
983 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
984 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
985 return print_integer(pretty
, field
);
986 case BT_FIELD_CLASS_TYPE_REAL
:
990 v
= bt_field_real_get_value(field
);
991 if (pretty
->use_colors
) {
992 g_string_append(pretty
->string
, COLOR_NUMBER_VALUE
);
994 g_string_append_printf(pretty
->string
, "%g", v
);
995 if (pretty
->use_colors
) {
996 g_string_append(pretty
->string
, COLOR_RST
);
1000 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
1001 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
1002 return print_enum(pretty
, field
);
1003 case BT_FIELD_CLASS_TYPE_STRING
:
1007 str
= bt_field_string_get_value(field
);
1012 if (pretty
->use_colors
) {
1013 g_string_append(pretty
->string
, COLOR_STRING_VALUE
);
1015 print_escape_string(pretty
, str
);
1016 if (pretty
->use_colors
) {
1017 g_string_append(pretty
->string
, COLOR_RST
);
1021 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
1022 return print_struct(pretty
, field
, print_names
, filter_fields
,
1024 case BT_FIELD_CLASS_TYPE_VARIANT
:
1025 return print_variant(pretty
, field
, print_names
);
1026 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
1027 return print_array(pretty
, field
, print_names
);
1028 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
1029 return print_sequence(pretty
, field
, print_names
);
1031 // TODO: log instead
1032 fprintf(pretty
->err
, "[error] Unknown type id: %d\n", (int) class_id
);
1038 int print_stream_packet_context(struct pretty_component
*pretty
,
1039 const bt_event
*event
)
1042 const bt_packet
*packet
= NULL
;
1043 const bt_field
*main_field
= NULL
;
1045 packet
= bt_event_borrow_packet_const(event
);
1050 main_field
= bt_packet_borrow_context_field_const(packet
);
1054 if (!pretty
->start_line
) {
1055 g_string_append(pretty
->string
, ", ");
1057 pretty
->start_line
= false;
1058 if (pretty
->options
.print_scope_field_names
) {
1059 print_name_equal(pretty
, "stream.packet.context");
1061 ret
= print_field(pretty
, main_field
,
1062 pretty
->options
.print_context_field_names
,
1063 stream_packet_context_quarks
,
1064 STREAM_PACKET_CONTEXT_QUARKS_LEN
);
1071 int print_stream_event_context(struct pretty_component
*pretty
,
1072 const bt_event
*event
)
1075 const bt_field
*main_field
= NULL
;
1077 main_field
= bt_event_borrow_common_context_field_const(event
);
1081 if (!pretty
->start_line
) {
1082 g_string_append(pretty
->string
, ", ");
1084 pretty
->start_line
= false;
1085 if (pretty
->options
.print_scope_field_names
) {
1086 print_name_equal(pretty
, "stream.event.context");
1088 ret
= print_field(pretty
, main_field
,
1089 pretty
->options
.print_context_field_names
, NULL
, 0);
1096 int print_event_context(struct pretty_component
*pretty
,
1097 const bt_event
*event
)
1100 const bt_field
*main_field
= NULL
;
1102 main_field
= bt_event_borrow_specific_context_field_const(event
);
1106 if (!pretty
->start_line
) {
1107 g_string_append(pretty
->string
, ", ");
1109 pretty
->start_line
= false;
1110 if (pretty
->options
.print_scope_field_names
) {
1111 print_name_equal(pretty
, "event.context");
1113 ret
= print_field(pretty
, main_field
,
1114 pretty
->options
.print_context_field_names
, NULL
, 0);
1121 int print_event_payload(struct pretty_component
*pretty
,
1122 const bt_event
*event
)
1125 const bt_field
*main_field
= NULL
;
1127 main_field
= bt_event_borrow_payload_field_const(event
);
1131 if (!pretty
->start_line
) {
1132 g_string_append(pretty
->string
, ", ");
1134 pretty
->start_line
= false;
1135 if (pretty
->options
.print_scope_field_names
) {
1136 print_name_equal(pretty
, "event.fields");
1138 ret
= print_field(pretty
, main_field
,
1139 pretty
->options
.print_payload_field_names
, NULL
, 0);
1146 int flush_buf(FILE *stream
, struct pretty_component
*pretty
)
1150 if (pretty
->string
->len
== 0) {
1154 if (fwrite(pretty
->string
->str
, pretty
->string
->len
, 1, stream
) != 1) {
1163 int pretty_print_event(struct pretty_component
*pretty
,
1164 const bt_message
*event_msg
)
1167 const bt_event
*event
=
1168 bt_message_event_borrow_event_const(event_msg
);
1171 pretty
->start_line
= true;
1172 g_string_assign(pretty
->string
, "");
1173 ret
= print_event_header(pretty
, event_msg
);
1178 ret
= print_stream_packet_context(pretty
, event
);
1183 ret
= print_stream_event_context(pretty
, event
);
1188 ret
= print_event_context(pretty
, event
);
1193 ret
= print_event_payload(pretty
, event
);
1198 g_string_append_c(pretty
->string
, '\n');
1199 if (flush_buf(pretty
->out
, pretty
)) {
1209 int print_discarded_elements_msg(struct pretty_component
*pretty
,
1210 const bt_stream
*stream
,
1211 const bt_clock_snapshot
*begin_clock_snapshot
,
1212 const bt_clock_snapshot
*end_clock_snapshot
,
1213 uint64_t count
, const char *elem_type
)
1216 const bt_stream_class
*stream_class
= NULL
;
1217 const bt_trace
*trace
= NULL
;
1218 const char *stream_name
;
1219 const char *trace_name
;
1221 int64_t stream_class_id
;
1223 const char *init_msg
;
1226 stream_name
= bt_stream_get_name(stream
);
1228 stream_name
= "(unknown)";
1231 /* Stream class ID */
1232 stream_class
= bt_stream_borrow_class_const(stream
);
1233 BT_ASSERT(stream_class
);
1234 stream_class_id
= bt_stream_class_get_id(stream_class
);
1237 stream_id
= bt_stream_get_id(stream
);
1240 trace
= bt_stream_borrow_trace_const(stream
);
1242 trace_name
= bt_trace_get_name(trace
);
1244 trace_name
= "(unknown)";
1248 trace_uuid
= bt_trace_class_get_uuid(
1249 bt_trace_borrow_class_const(trace
));
1251 /* Format message */
1252 g_string_assign(pretty
->string
, "");
1254 if (count
== UINT64_C(-1)) {
1255 init_msg
= "Tracer may have discarded";
1257 init_msg
= "Tracer discarded";
1260 g_string_append_printf(pretty
->string
,
1261 "%s%sWARNING%s%s: %s ",
1262 bt_common_color_fg_yellow(),
1263 bt_common_color_bold(),
1264 bt_common_color_reset(),
1265 bt_common_color_fg_yellow(), init_msg
);
1267 if (count
== UINT64_C(-1)) {
1268 g_string_append_printf(pretty
->string
, "%ss", elem_type
);
1270 g_string_append_printf(pretty
->string
,
1271 "%" PRIu64
" %s%s", count
, elem_type
,
1272 count
== 1 ? "" : "s");
1275 if (begin_clock_snapshot
&& end_clock_snapshot
) {
1276 g_string_append(pretty
->string
, " between [");
1277 print_timestamp_wall(pretty
, begin_clock_snapshot
, false);
1278 g_string_append(pretty
->string
, "] and [");
1279 print_timestamp_wall(pretty
, end_clock_snapshot
, false);
1280 g_string_append(pretty
->string
, "]");
1282 g_string_append(pretty
->string
, "(unknown time range)");
1285 g_string_append_printf(pretty
->string
, " in trace \"%s\" ", trace_name
);
1288 g_string_append_printf(pretty
->string
,
1289 "(UUID: %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x) ",
1307 g_string_append(pretty
->string
, "(no UUID) ");
1310 g_string_append_printf(pretty
->string
,
1311 "within stream \"%s\" (stream class ID: %" PRIu64
", ",
1312 stream_name
, stream_class_id
);
1314 if (stream_id
>= 0) {
1315 g_string_append_printf(pretty
->string
,
1316 "stream ID: %" PRIu64
, stream_id
);
1318 g_string_append(pretty
->string
, "no stream ID");
1321 g_string_append_printf(pretty
->string
, ").%s\n",
1322 bt_common_color_reset());
1325 * Print to standard error stream to remain backward compatible
1326 * with Babeltrace 1.
1328 if (flush_buf(stderr
, pretty
)) {
1336 int pretty_print_discarded_items(struct pretty_component
*pretty
,
1337 const bt_message
*msg
)
1339 const bt_clock_snapshot
*begin
= NULL
;
1340 const bt_clock_snapshot
*end
= NULL
;
1341 const bt_stream
*stream
;
1342 const bt_stream_class
*stream_class
;
1343 uint64_t count
= UINT64_C(-1);
1344 const char *elem_type
;
1346 switch (bt_message_get_type(msg
)) {
1347 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1348 stream
= bt_message_discarded_events_borrow_stream_const(msg
);
1350 if (bt_message_discarded_events_get_count(msg
, &count
) ==
1351 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1352 count
= UINT64_C(-1);
1355 elem_type
= "event";
1357 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1358 stream
= bt_message_discarded_packets_borrow_stream_const(msg
);
1360 if (bt_message_discarded_packets_get_count(msg
, &count
) ==
1361 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE
) {
1362 count
= UINT64_C(-1);
1365 elem_type
= "packet";
1372 stream_class
= bt_stream_borrow_class_const(stream
);
1374 if (bt_stream_class_borrow_default_clock_class_const(stream_class
)) {
1375 switch (bt_message_get_type(msg
)) {
1376 case BT_MESSAGE_TYPE_DISCARDED_EVENTS
:
1377 bt_message_discarded_events_borrow_default_beginning_clock_snapshot_const(
1379 bt_message_discarded_events_borrow_default_end_clock_snapshot_const(
1382 case BT_MESSAGE_TYPE_DISCARDED_PACKETS
:
1383 bt_message_discarded_packets_borrow_default_beginning_clock_snapshot_const(
1385 bt_message_discarded_packets_borrow_default_end_clock_snapshot_const(
1393 print_discarded_elements_msg(pretty
, stream
, begin
, end
,