Commit | Line | Data |
---|---|---|
af9a82eb | 1 | /* |
af9a82eb | 2 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
f504043c | 3 | * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
af9a82eb JG |
4 | * |
5 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
6 | * | |
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: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
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 | |
23 | * SOFTWARE. | |
24 | */ | |
25 | ||
9d408fca | 26 | #include <babeltrace/babeltrace.h> |
3d9990ac | 27 | #include <babeltrace/bitfield-internal.h> |
ad96d936 | 28 | #include <babeltrace/common-internal.h> |
58a2480d | 29 | #include <babeltrace/compat/time-internal.h> |
f6ccaed9 | 30 | #include <babeltrace/assert-internal.h> |
6a18b281 | 31 | #include <inttypes.h> |
93a4161c | 32 | #include <ctype.h> |
3228cc1d | 33 | #include "pretty.h" |
af9a82eb | 34 | |
1556a1af JG |
35 | #define NSEC_PER_SEC 1000000000LL |
36 | ||
5280f742 | 37 | #define COLOR_NAME BT_COMMON_COLOR_BOLD |
08899d39 | 38 | #define COLOR_FIELD_NAME BT_COMMON_COLOR_FG_CYAN |
5280f742 PP |
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 | |
08899d39 | 44 | #define COLOR_EVENT_NAME BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_MAGENTA |
5280f742 | 45 | #define COLOR_TIMESTAMP BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_YELLOW |
ad96d936 | 46 | |
af9a82eb JG |
47 | struct timestamp { |
48 | int64_t real_timestamp; /* Relative to UNIX epoch. */ | |
605e1019 | 49 | uint64_t clock_snapshot; /* In cycles. */ |
af9a82eb JG |
50 | }; |
51 | ||
6a18b281 | 52 | static |
d94d92ac | 53 | int print_field(struct pretty_component *pretty, |
b19ff26f | 54 | const bt_field *field, bool print_names, |
2b4c4a7c | 55 | GQuark *filters_fields, int filter_array_len); |
6a18b281 | 56 | |
ad96d936 | 57 | static |
3228cc1d | 58 | void print_name_equal(struct pretty_component *pretty, const char *name) |
ad96d936 | 59 | { |
3228cc1d | 60 | if (pretty->use_colors) { |
5280f742 PP |
61 | g_string_append_printf(pretty->string, "%s%s%s = ", COLOR_NAME, |
62 | name, COLOR_RST); | |
ad96d936 | 63 | } else { |
5280f742 | 64 | g_string_append_printf(pretty->string, "%s = ", name); |
ad96d936 PP |
65 | } |
66 | } | |
67 | ||
68 | static | |
3228cc1d | 69 | void print_field_name_equal(struct pretty_component *pretty, const char *name) |
ad96d936 | 70 | { |
3228cc1d | 71 | if (pretty->use_colors) { |
5280f742 PP |
72 | g_string_append_printf(pretty->string, "%s%s%s = ", |
73 | COLOR_FIELD_NAME, name, COLOR_RST); | |
ad96d936 | 74 | } else { |
5280f742 | 75 | g_string_append_printf(pretty->string, "%s = ", name); |
ad96d936 PP |
76 | } |
77 | } | |
78 | ||
af9a82eb | 79 | static |
3228cc1d | 80 | void print_timestamp_cycles(struct pretty_component *pretty, |
9c202476 | 81 | const bt_clock_snapshot *clock_snapshot, bool update_last) |
af9a82eb | 82 | { |
1556a1af | 83 | uint64_t cycles; |
1556a1af | 84 | |
605e1019 | 85 | cycles = bt_clock_snapshot_get_value(clock_snapshot); |
5280f742 | 86 | g_string_append_printf(pretty->string, "%020" PRIu64, cycles); |
3af83b5a | 87 | |
9c202476 PP |
88 | if (update_last) { |
89 | if (pretty->last_cycles_timestamp != -1ULL) { | |
90 | pretty->delta_cycles = cycles - pretty->last_cycles_timestamp; | |
91 | } | |
2c091c04 | 92 | |
9c202476 PP |
93 | pretty->last_cycles_timestamp = cycles; |
94 | } | |
af9a82eb JG |
95 | } |
96 | ||
97 | static | |
3228cc1d | 98 | void print_timestamp_wall(struct pretty_component *pretty, |
9c202476 | 99 | const bt_clock_snapshot *clock_snapshot, bool update_last) |
af9a82eb | 100 | { |
1556a1af | 101 | int ret; |
1556a1af JG |
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; | |
105 | bool is_negative; | |
af9a82eb | 106 | |
605e1019 | 107 | if (!clock_snapshot) { |
5280f742 | 108 | g_string_append(pretty->string, "??:??:??.?????????"); |
1556a1af JG |
109 | return; |
110 | } | |
111 | ||
605e1019 | 112 | ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, &ts_nsec); |
1556a1af | 113 | if (ret) { |
5280f742 PP |
114 | // TODO: log, this is unexpected |
115 | g_string_append(pretty->string, "Error"); | |
1556a1af JG |
116 | return; |
117 | } | |
118 | ||
9c202476 PP |
119 | if (update_last) { |
120 | if (pretty->last_real_timestamp != -1ULL) { | |
121 | pretty->delta_real_timestamp = ts_nsec - pretty->last_real_timestamp; | |
122 | } | |
123 | ||
124 | pretty->last_real_timestamp = ts_nsec; | |
3af83b5a | 125 | } |
3af83b5a | 126 | |
1556a1af JG |
127 | ts_sec += ts_nsec / NSEC_PER_SEC; |
128 | ts_nsec = ts_nsec % NSEC_PER_SEC; | |
5280f742 | 129 | |
1556a1af JG |
130 | if (ts_sec >= 0 && ts_nsec >= 0) { |
131 | is_negative = false; | |
132 | ts_sec_abs = ts_sec; | |
133 | ts_nsec_abs = ts_nsec; | |
134 | } else if (ts_sec > 0 && ts_nsec < 0) { | |
135 | is_negative = false; | |
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) { | |
139 | is_negative = true; | |
140 | ts_sec_abs = ts_sec; | |
141 | ts_nsec_abs = -ts_nsec; | |
142 | } else if (ts_sec < 0 && ts_nsec > 0) { | |
143 | is_negative = true; | |
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) { | |
147 | is_negative = true; | |
148 | ts_sec_abs = -ts_sec; | |
149 | ts_nsec_abs = ts_nsec; | |
150 | } else { /* (ts_sec < 0 && ts_nsec < 0) */ | |
151 | is_negative = true; | |
152 | ts_sec_abs = -ts_sec; | |
153 | ts_nsec_abs = -ts_nsec; | |
154 | } | |
155 | ||
3228cc1d | 156 | if (!pretty->options.clock_seconds) { |
1556a1af JG |
157 | struct tm tm; |
158 | time_t time_s = (time_t) ts_sec_abs; | |
159 | ||
18adbd19 | 160 | if (is_negative && !pretty->negative_timestamp_warning_done) { |
5280f742 | 161 | // TODO: log instead |
1556a1af | 162 | fprintf(stderr, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n"); |
18adbd19 | 163 | pretty->negative_timestamp_warning_done = true; |
1556a1af JG |
164 | goto seconds; |
165 | } | |
166 | ||
3228cc1d | 167 | if (!pretty->options.clock_gmt) { |
1556a1af JG |
168 | struct tm *res; |
169 | ||
58a2480d | 170 | res = bt_localtime_r(&time_s, &tm); |
1556a1af | 171 | if (!res) { |
5280f742 | 172 | // TODO: log instead |
1556a1af JG |
173 | fprintf(stderr, "[warning] Unable to get localtime.\n"); |
174 | goto seconds; | |
175 | } | |
176 | } else { | |
177 | struct tm *res; | |
178 | ||
58a2480d | 179 | res = bt_gmtime_r(&time_s, &tm); |
1556a1af | 180 | if (!res) { |
5280f742 | 181 | // TODO: log instead |
1556a1af JG |
182 | fprintf(stderr, "[warning] Unable to get gmtime.\n"); |
183 | goto seconds; | |
184 | } | |
185 | } | |
3228cc1d | 186 | if (pretty->options.clock_date) { |
1556a1af JG |
187 | char timestr[26]; |
188 | size_t res; | |
189 | ||
190 | /* Print date and time */ | |
191 | res = strftime(timestr, sizeof(timestr), | |
4d2a94f1 | 192 | "%Y-%m-%d ", &tm); |
1556a1af | 193 | if (!res) { |
5280f742 | 194 | // TODO: log instead |
1556a1af JG |
195 | fprintf(stderr, "[warning] Unable to print ascii time.\n"); |
196 | goto seconds; | |
197 | } | |
5280f742 PP |
198 | |
199 | g_string_append(pretty->string, timestr); | |
1556a1af | 200 | } |
5280f742 | 201 | |
1556a1af | 202 | /* Print time in HH:MM:SS.ns */ |
5280f742 PP |
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); | |
1556a1af JG |
206 | goto end; |
207 | } | |
208 | seconds: | |
5280f742 PP |
209 | g_string_append_printf(pretty->string, "%s%" PRId64 ".%09" PRIu64, |
210 | is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs); | |
1556a1af JG |
211 | end: |
212 | return; | |
af9a82eb JG |
213 | } |
214 | ||
215 | static | |
d94d92ac | 216 | int print_event_timestamp(struct pretty_component *pretty, |
2c091c04 | 217 | const bt_message *event_msg, bool *start_line) |
af9a82eb | 218 | { |
3228cc1d | 219 | bool print_names = pretty->options.print_header_field_names; |
d94d92ac | 220 | int ret = 0; |
605e1019 | 221 | const bt_clock_snapshot *clock_snapshot = NULL; |
af9a82eb | 222 | |
81c25620 PP |
223 | if (!bt_message_event_borrow_stream_class_default_clock_class_const( |
224 | event_msg)) { | |
2c091c04 | 225 | /* No default clock class: skip the timestamp without an error */ |
f504043c MD |
226 | goto end; |
227 | } | |
d9f65f09 | 228 | |
2c091c04 PP |
229 | if (bt_message_event_borrow_default_clock_snapshot_const(event_msg, |
230 | &clock_snapshot) != BT_CLOCK_SNAPSHOT_STATE_KNOWN) { | |
81c25620 PP |
231 | /* |
232 | * No known default clock value: skip the timestamp | |
233 | * without an error. | |
234 | */ | |
f504043c MD |
235 | goto end; |
236 | } | |
af9a82eb | 237 | |
ad96d936 | 238 | if (print_names) { |
3228cc1d | 239 | print_name_equal(pretty, "timestamp"); |
ad96d936 | 240 | } else { |
5280f742 | 241 | g_string_append(pretty->string, "["); |
ad96d936 | 242 | } |
3228cc1d | 243 | if (pretty->use_colors) { |
5280f742 | 244 | g_string_append(pretty->string, COLOR_TIMESTAMP); |
ad96d936 | 245 | } |
3228cc1d | 246 | if (pretty->options.print_timestamp_cycles) { |
9c202476 | 247 | print_timestamp_cycles(pretty, clock_snapshot, true); |
af9a82eb | 248 | } else { |
9c202476 | 249 | print_timestamp_wall(pretty, clock_snapshot, true); |
af9a82eb | 250 | } |
3228cc1d | 251 | if (pretty->use_colors) { |
5280f742 | 252 | g_string_append(pretty->string, COLOR_RST); |
ad96d936 | 253 | } |
af9a82eb | 254 | |
c3c30b08 | 255 | if (!print_names) |
5280f742 | 256 | g_string_append(pretty->string, "] "); |
c3c30b08 | 257 | |
3228cc1d | 258 | if (pretty->options.print_delta_field) { |
ad96d936 | 259 | if (print_names) { |
5280f742 | 260 | g_string_append(pretty->string, ", "); |
3228cc1d | 261 | print_name_equal(pretty, "delta"); |
ad96d936 | 262 | } else { |
5280f742 | 263 | g_string_append(pretty->string, "("); |
ad96d936 | 264 | } |
3228cc1d PP |
265 | if (pretty->options.print_timestamp_cycles) { |
266 | if (pretty->delta_cycles == -1ULL) { | |
5280f742 | 267 | g_string_append(pretty->string, |
c6a871e9 | 268 | "+??????????\?\?"); /* Not a trigraph. */ |
3af83b5a | 269 | } else { |
5280f742 PP |
270 | g_string_append_printf(pretty->string, |
271 | "+%012" PRIu64, pretty->delta_cycles); | |
3af83b5a MD |
272 | } |
273 | } else { | |
3228cc1d | 274 | if (pretty->delta_real_timestamp != -1ULL) { |
3af83b5a | 275 | uint64_t delta_sec, delta_nsec, delta; |
f504043c | 276 | |
3228cc1d | 277 | delta = pretty->delta_real_timestamp; |
3af83b5a MD |
278 | delta_sec = delta / NSEC_PER_SEC; |
279 | delta_nsec = delta % NSEC_PER_SEC; | |
5280f742 PP |
280 | g_string_append_printf(pretty->string, |
281 | "+%" PRIu64 ".%09" PRIu64, | |
3af83b5a MD |
282 | delta_sec, delta_nsec); |
283 | } else { | |
5280f742 | 284 | g_string_append(pretty->string, "+?.?????????"); |
3af83b5a MD |
285 | } |
286 | } | |
287 | if (!print_names) { | |
5280f742 | 288 | g_string_append(pretty->string, ") "); |
3af83b5a MD |
289 | } |
290 | } | |
291 | *start_line = !print_names; | |
f504043c | 292 | |
af9a82eb | 293 | end: |
af9a82eb JG |
294 | return ret; |
295 | } | |
296 | ||
6a18b281 | 297 | static |
d94d92ac | 298 | int print_event_header(struct pretty_component *pretty, |
2c091c04 | 299 | const bt_message *event_msg) |
af9a82eb | 300 | { |
3228cc1d | 301 | bool print_names = pretty->options.print_header_field_names; |
d94d92ac | 302 | int ret = 0; |
b19ff26f PP |
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; | |
2c091c04 | 309 | const bt_event *event = bt_message_event_borrow_event_const(event_msg); |
60535549 | 310 | int dom_print = 0; |
4cdfc5e8 | 311 | bt_property_availability prop_avail; |
af9a82eb | 312 | |
40f4ba76 | 313 | event_class = bt_event_borrow_class_const(event); |
40f4ba76 | 314 | stream_class = bt_event_class_borrow_stream_class_const(event_class); |
862ca4ed PP |
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); | |
2c091c04 | 319 | ret = print_event_timestamp(pretty, event_msg, &pretty->start_line); |
d94d92ac | 320 | if (ret) { |
af9a82eb JG |
321 | goto end; |
322 | } | |
3228cc1d | 323 | if (pretty->options.print_trace_field) { |
c3c30b08 MD |
324 | const char *name; |
325 | ||
862ca4ed | 326 | name = bt_trace_get_name(trace); |
c3c30b08 | 327 | if (name) { |
3228cc1d | 328 | if (!pretty->start_line) { |
5280f742 | 329 | g_string_append(pretty->string, ", "); |
c3c30b08 | 330 | } |
c3c30b08 | 331 | if (print_names) { |
3228cc1d | 332 | print_name_equal(pretty, "trace"); |
c3c30b08 | 333 | } |
5280f742 PP |
334 | |
335 | g_string_append(pretty->string, name); | |
336 | ||
22b7fdc6 PP |
337 | if (print_names) { |
338 | g_string_append(pretty->string, ", "); | |
60535549 | 339 | } |
c3c30b08 MD |
340 | } |
341 | } | |
3228cc1d | 342 | if (pretty->options.print_trace_hostname_field) { |
b19ff26f | 343 | const bt_value *hostname_str; |
c3c30b08 | 344 | |
862ca4ed | 345 | hostname_str = bt_trace_class_borrow_environment_entry_value_by_name_const( |
a82e90e8 | 346 | trace_class, "hostname"); |
c3c30b08 MD |
347 | if (hostname_str) { |
348 | const char *str; | |
349 | ||
3228cc1d | 350 | if (!pretty->start_line) { |
5280f742 | 351 | g_string_append(pretty->string, ", "); |
c3c30b08 | 352 | } |
c3c30b08 | 353 | if (print_names) { |
3228cc1d | 354 | print_name_equal(pretty, "trace:hostname"); |
c3c30b08 | 355 | } |
601b0d3c PP |
356 | str = bt_value_string_get(hostname_str); |
357 | g_string_append(pretty->string, str); | |
60535549 | 358 | dom_print = 1; |
c3c30b08 MD |
359 | } |
360 | } | |
3228cc1d | 361 | if (pretty->options.print_trace_domain_field) { |
b19ff26f | 362 | const bt_value *domain_str; |
c3c30b08 | 363 | |
862ca4ed | 364 | domain_str = bt_trace_class_borrow_environment_entry_value_by_name_const( |
a82e90e8 | 365 | trace_class, "domain"); |
c3c30b08 MD |
366 | if (domain_str) { |
367 | const char *str; | |
368 | ||
3228cc1d | 369 | if (!pretty->start_line) { |
5280f742 | 370 | g_string_append(pretty->string, ", "); |
c3c30b08 | 371 | } |
c3c30b08 | 372 | if (print_names) { |
3228cc1d | 373 | print_name_equal(pretty, "trace:domain"); |
60535549 | 374 | } else if (dom_print) { |
5280f742 | 375 | g_string_append(pretty->string, ":"); |
c3c30b08 | 376 | } |
601b0d3c PP |
377 | str = bt_value_string_get(domain_str); |
378 | g_string_append(pretty->string, str); | |
60535549 | 379 | dom_print = 1; |
c3c30b08 MD |
380 | } |
381 | } | |
3228cc1d | 382 | if (pretty->options.print_trace_procname_field) { |
b19ff26f | 383 | const bt_value *procname_str; |
c3c30b08 | 384 | |
862ca4ed | 385 | procname_str = bt_trace_class_borrow_environment_entry_value_by_name_const( |
a82e90e8 | 386 | trace_class, "procname"); |
c3c30b08 MD |
387 | if (procname_str) { |
388 | const char *str; | |
389 | ||
3228cc1d | 390 | if (!pretty->start_line) { |
5280f742 | 391 | g_string_append(pretty->string, ", "); |
c3c30b08 | 392 | } |
c3c30b08 | 393 | if (print_names) { |
3228cc1d | 394 | print_name_equal(pretty, "trace:procname"); |
60535549 | 395 | } else if (dom_print) { |
5280f742 | 396 | g_string_append(pretty->string, ":"); |
c3c30b08 | 397 | } |
601b0d3c PP |
398 | str = bt_value_string_get(procname_str); |
399 | g_string_append(pretty->string, str); | |
60535549 | 400 | dom_print = 1; |
c3c30b08 MD |
401 | } |
402 | } | |
3228cc1d | 403 | if (pretty->options.print_trace_vpid_field) { |
b19ff26f | 404 | const bt_value *vpid_value; |
c3c30b08 | 405 | |
862ca4ed | 406 | vpid_value = bt_trace_class_borrow_environment_entry_value_by_name_const( |
a82e90e8 | 407 | trace_class, "vpid"); |
c3c30b08 MD |
408 | if (vpid_value) { |
409 | int64_t value; | |
410 | ||
3228cc1d | 411 | if (!pretty->start_line) { |
5280f742 | 412 | g_string_append(pretty->string, ", "); |
c3c30b08 | 413 | } |
c3c30b08 | 414 | if (print_names) { |
3228cc1d | 415 | print_name_equal(pretty, "trace:vpid"); |
60535549 | 416 | } else if (dom_print) { |
5280f742 | 417 | g_string_append(pretty->string, ":"); |
c3c30b08 | 418 | } |
fdd3a2da | 419 | value = bt_value_signed_integer_get(vpid_value); |
601b0d3c PP |
420 | g_string_append_printf(pretty->string, |
421 | "(%" PRId64 ")", value); | |
60535549 | 422 | dom_print = 1; |
c3c30b08 MD |
423 | } |
424 | } | |
3228cc1d | 425 | if (pretty->options.print_loglevel_field) { |
cf76ce92 | 426 | static const char *log_level_names[] = { |
50842bdc PP |
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", | |
cf76ce92 | 442 | }; |
4cdfc5e8 | 443 | bt_event_class_log_level log_level; |
cf76ce92 PP |
444 | const char *log_level_str = NULL; |
445 | ||
44c440bc PP |
446 | prop_avail = bt_event_class_get_log_level(event_class, |
447 | &log_level); | |
448 | if (prop_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) { | |
cf76ce92 | 449 | log_level_str = log_level_names[log_level]; |
44c440bc | 450 | BT_ASSERT(log_level_str); |
c3c30b08 | 451 | |
3228cc1d | 452 | if (!pretty->start_line) { |
5280f742 | 453 | g_string_append(pretty->string, ", "); |
c3c30b08 | 454 | } |
c3c30b08 | 455 | if (print_names) { |
3228cc1d | 456 | print_name_equal(pretty, "loglevel"); |
60535549 | 457 | } else if (dom_print) { |
5280f742 | 458 | g_string_append(pretty->string, ":"); |
c3c30b08 | 459 | } |
cf76ce92 PP |
460 | |
461 | g_string_append(pretty->string, log_level_str); | |
462 | g_string_append_printf( | |
463 | pretty->string, " (%d)", (int) log_level); | |
60535549 | 464 | dom_print = 1; |
c3c30b08 MD |
465 | } |
466 | } | |
3228cc1d | 467 | if (pretty->options.print_emf_field) { |
cf76ce92 | 468 | const char *uri_str; |
c3c30b08 | 469 | |
50842bdc | 470 | uri_str = bt_event_class_get_emf_uri(event_class); |
c3c30b08 | 471 | if (uri_str) { |
3228cc1d | 472 | if (!pretty->start_line) { |
5280f742 | 473 | g_string_append(pretty->string, ", "); |
c3c30b08 | 474 | } |
c3c30b08 | 475 | if (print_names) { |
3228cc1d | 476 | print_name_equal(pretty, "model.emf.uri"); |
60535549 | 477 | } else if (dom_print) { |
5280f742 | 478 | g_string_append(pretty->string, ":"); |
c3c30b08 | 479 | } |
c3c30b08 | 480 | |
cf76ce92 | 481 | g_string_append(pretty->string, uri_str); |
60535549 | 482 | dom_print = 1; |
c3c30b08 MD |
483 | } |
484 | } | |
60535549 | 485 | if (dom_print && !print_names) { |
5280f742 | 486 | g_string_append(pretty->string, " "); |
60535549 | 487 | } |
3228cc1d | 488 | if (!pretty->start_line) { |
5280f742 | 489 | g_string_append(pretty->string, ", "); |
c3c30b08 | 490 | } |
3228cc1d | 491 | pretty->start_line = true; |
6a18b281 | 492 | if (print_names) { |
3228cc1d | 493 | print_name_equal(pretty, "name"); |
ad96d936 | 494 | } |
3228cc1d | 495 | if (pretty->use_colors) { |
5280f742 | 496 | g_string_append(pretty->string, COLOR_EVENT_NAME); |
6a18b281 | 497 | } |
50842bdc | 498 | g_string_append(pretty->string, bt_event_class_get_name(event_class)); |
3228cc1d | 499 | if (pretty->use_colors) { |
5280f742 | 500 | g_string_append(pretty->string, COLOR_RST); |
ad96d936 | 501 | } |
60535549 | 502 | if (!print_names) { |
5280f742 | 503 | g_string_append(pretty->string, ": "); |
60535549 | 504 | } else { |
5280f742 | 505 | g_string_append(pretty->string, ", "); |
60535549 | 506 | } |
a82e90e8 | 507 | |
af9a82eb | 508 | end: |
6a18b281 MD |
509 | return ret; |
510 | } | |
511 | ||
512 | static | |
d94d92ac | 513 | int print_integer(struct pretty_component *pretty, |
b19ff26f | 514 | const bt_field *field) |
6a18b281 | 515 | { |
d94d92ac | 516 | int ret = 0; |
4cdfc5e8 | 517 | bt_field_class_integer_preferred_display_base base; |
b19ff26f | 518 | const bt_field_class *int_fc; |
6a18b281 MD |
519 | union { |
520 | uint64_t u; | |
521 | int64_t s; | |
522 | } v; | |
ad96d936 | 523 | bool rst_color = false; |
4cdfc5e8 | 524 | bt_field_class_type ft_type; |
6a18b281 | 525 | |
40f4ba76 | 526 | int_fc = bt_field_borrow_class_const(field); |
5cd6d0e5 | 527 | BT_ASSERT(int_fc); |
864cad70 PP |
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) { | |
44c440bc | 531 | v.u = bt_field_unsigned_integer_get_value(field); |
6a18b281 | 532 | } else { |
44c440bc | 533 | v.s = bt_field_signed_integer_get_value(field); |
6a18b281 MD |
534 | } |
535 | ||
3228cc1d | 536 | if (pretty->use_colors) { |
5280f742 | 537 | g_string_append(pretty->string, COLOR_NUMBER_VALUE); |
ad96d936 PP |
538 | rst_color = true; |
539 | } | |
540 | ||
5cd6d0e5 | 541 | base = bt_field_class_integer_get_preferred_display_base(int_fc); |
6a18b281 | 542 | switch (base) { |
5cd6d0e5 | 543 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY: |
6a18b281 MD |
544 | { |
545 | int bitnr, len; | |
546 | ||
5cd6d0e5 | 547 | len = bt_field_class_integer_get_field_value_range(int_fc); |
5280f742 | 548 | g_string_append(pretty->string, "0b"); |
6a18b281 MD |
549 | v.u = _bt_piecewise_lshift(v.u, 64 - len); |
550 | for (bitnr = 0; bitnr < len; bitnr++) { | |
5280f742 | 551 | g_string_append_printf(pretty->string, "%u", (v.u & (1ULL << 63)) ? 1 : 0); |
6a18b281 MD |
552 | v.u = _bt_piecewise_lshift(v.u, 1); |
553 | } | |
554 | break; | |
555 | } | |
5cd6d0e5 | 556 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL: |
6a18b281 | 557 | { |
864cad70 PP |
558 | if (ft_type == BT_FIELD_CLASS_TYPE_SIGNED_INTEGER || |
559 | ft_type == BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION) { | |
6a18b281 MD |
560 | int len; |
561 | ||
5cd6d0e5 PP |
562 | len = bt_field_class_integer_get_field_value_range( |
563 | int_fc); | |
6a18b281 MD |
564 | if (len < 64) { |
565 | size_t rounded_len; | |
566 | ||
f6ccaed9 | 567 | BT_ASSERT(len != 0); |
6a18b281 MD |
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; | |
571 | } | |
572 | } | |
573 | ||
5280f742 | 574 | g_string_append_printf(pretty->string, "0%" PRIo64, v.u); |
6a18b281 MD |
575 | break; |
576 | } | |
5cd6d0e5 | 577 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL: |
864cad70 PP |
578 | if (ft_type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || |
579 | ft_type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION) { | |
5280f742 | 580 | g_string_append_printf(pretty->string, "%" PRIu64, v.u); |
6a18b281 | 581 | } else { |
5280f742 | 582 | g_string_append_printf(pretty->string, "%" PRId64, v.s); |
6a18b281 MD |
583 | } |
584 | break; | |
5cd6d0e5 | 585 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL: |
6a18b281 MD |
586 | { |
587 | int len; | |
588 | ||
5cd6d0e5 | 589 | len = bt_field_class_integer_get_field_value_range(int_fc); |
6a18b281 MD |
590 | if (len < 64) { |
591 | /* Round length to the nearest nibble */ | |
592 | uint8_t rounded_len = ((len + 3) & ~0x3); | |
593 | ||
594 | v.u &= ((uint64_t) 1 << rounded_len) - 1; | |
595 | } | |
596 | ||
5280f742 | 597 | g_string_append_printf(pretty->string, "0x%" PRIX64, v.u); |
6a18b281 MD |
598 | break; |
599 | } | |
600 | default: | |
d94d92ac | 601 | ret = -1; |
6a18b281 MD |
602 | goto end; |
603 | } | |
604 | end: | |
ad96d936 | 605 | if (rst_color) { |
5280f742 | 606 | g_string_append(pretty->string, COLOR_RST); |
ad96d936 | 607 | } |
6a18b281 MD |
608 | return ret; |
609 | } | |
610 | ||
93a4161c | 611 | static |
3228cc1d | 612 | void print_escape_string(struct pretty_component *pretty, const char *str) |
93a4161c JD |
613 | { |
614 | int i; | |
615 | ||
5280f742 PP |
616 | g_string_append_c(pretty->string, '"'); |
617 | ||
93a4161c JD |
618 | for (i = 0; i < strlen(str); i++) { |
619 | /* Escape sequences not recognized by iscntrl(). */ | |
620 | switch (str[i]) { | |
621 | case '\\': | |
5280f742 | 622 | g_string_append(pretty->string, "\\\\"); |
93a4161c JD |
623 | continue; |
624 | case '\'': | |
5280f742 | 625 | g_string_append(pretty->string, "\\\'"); |
93a4161c JD |
626 | continue; |
627 | case '\"': | |
5280f742 | 628 | g_string_append(pretty->string, "\\\""); |
93a4161c JD |
629 | continue; |
630 | case '\?': | |
5280f742 | 631 | g_string_append(pretty->string, "\\\?"); |
93a4161c JD |
632 | continue; |
633 | } | |
634 | ||
635 | /* Standard characters. */ | |
636 | if (!iscntrl(str[i])) { | |
5280f742 | 637 | g_string_append_c(pretty->string, str[i]); |
93a4161c JD |
638 | continue; |
639 | } | |
640 | ||
641 | switch (str[i]) { | |
642 | case '\0': | |
5280f742 | 643 | g_string_append(pretty->string, "\\0"); |
93a4161c JD |
644 | break; |
645 | case '\a': | |
5280f742 | 646 | g_string_append(pretty->string, "\\a"); |
93a4161c JD |
647 | break; |
648 | case '\b': | |
5280f742 | 649 | g_string_append(pretty->string, "\\b"); |
93a4161c JD |
650 | break; |
651 | case '\e': | |
5280f742 | 652 | g_string_append(pretty->string, "\\e"); |
93a4161c JD |
653 | break; |
654 | case '\f': | |
5280f742 | 655 | g_string_append(pretty->string, "\\f"); |
93a4161c JD |
656 | break; |
657 | case '\n': | |
5280f742 | 658 | g_string_append(pretty->string, "\\n"); |
93a4161c JD |
659 | break; |
660 | case '\r': | |
5280f742 | 661 | g_string_append(pretty->string, "\\r"); |
93a4161c JD |
662 | break; |
663 | case '\t': | |
5280f742 | 664 | g_string_append(pretty->string, "\\t"); |
93a4161c JD |
665 | break; |
666 | case '\v': | |
5280f742 | 667 | g_string_append(pretty->string, "\\v"); |
93a4161c JD |
668 | break; |
669 | default: | |
670 | /* Unhandled control-sequence, print as hex. */ | |
5280f742 | 671 | g_string_append_printf(pretty->string, "\\x%02x", str[i]); |
93a4161c JD |
672 | break; |
673 | } | |
674 | } | |
5280f742 PP |
675 | |
676 | g_string_append_c(pretty->string, '"'); | |
93a4161c JD |
677 | } |
678 | ||
6a18b281 | 679 | static |
d94d92ac | 680 | int print_enum(struct pretty_component *pretty, |
b19ff26f | 681 | const bt_field *field) |
6a18b281 | 682 | { |
d94d92ac | 683 | int ret = 0; |
b19ff26f | 684 | const bt_field_class *enumeration_field_class = NULL; |
5cd6d0e5 | 685 | bt_field_class_enumeration_mapping_label_array label_array; |
44c440bc PP |
686 | uint64_t label_count; |
687 | uint64_t i; | |
96e8f959 | 688 | |
40f4ba76 | 689 | enumeration_field_class = bt_field_borrow_class_const(field); |
5cd6d0e5 | 690 | if (!enumeration_field_class) { |
d94d92ac | 691 | ret = -1; |
96e8f959 MD |
692 | goto end; |
693 | } | |
44c440bc | 694 | |
864cad70 PP |
695 | switch (bt_field_get_class_type(field)) { |
696 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: | |
44c440bc PP |
697 | ret = bt_field_unsigned_enumeration_get_mapping_labels(field, |
698 | &label_array, &label_count); | |
699 | break; | |
864cad70 | 700 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: |
44c440bc PP |
701 | ret = bt_field_signed_enumeration_get_mapping_labels(field, |
702 | &label_array, &label_count); | |
703 | break; | |
704 | default: | |
705 | abort(); | |
96e8f959 | 706 | } |
44c440bc PP |
707 | |
708 | if (ret) { | |
d94d92ac | 709 | ret = -1; |
96e8f959 MD |
710 | goto end; |
711 | } | |
44c440bc | 712 | |
83ef3205 | 713 | g_string_append(pretty->string, "( "); |
44c440bc | 714 | if (label_count == 0) { |
83ef3205 MD |
715 | if (pretty->use_colors) { |
716 | g_string_append(pretty->string, COLOR_UNKNOWN); | |
717 | } | |
718 | g_string_append(pretty->string, "<unknown>"); | |
719 | if (pretty->use_colors) { | |
720 | g_string_append(pretty->string, COLOR_RST); | |
721 | } | |
722 | goto skip_loop; | |
96e8f959 | 723 | } |
44c440bc PP |
724 | for (i = 0; i < label_count; i++) { |
725 | const char *mapping_name = label_array[i]; | |
96e8f959 | 726 | |
44c440bc | 727 | if (i == 0) { |
5280f742 | 728 | g_string_append(pretty->string, ", "); |
44c440bc | 729 | } |
3228cc1d | 730 | if (pretty->use_colors) { |
5280f742 | 731 | g_string_append(pretty->string, COLOR_ENUM_MAPPING_NAME); |
ad96d936 | 732 | } |
3228cc1d PP |
733 | print_escape_string(pretty, mapping_name); |
734 | if (pretty->use_colors) { | |
5280f742 | 735 | g_string_append(pretty->string, COLOR_RST); |
ad96d936 | 736 | } |
96e8f959 | 737 | } |
83ef3205 | 738 | skip_loop: |
5280f742 | 739 | g_string_append(pretty->string, " : container = "); |
312c056a | 740 | ret = print_integer(pretty, field); |
d94d92ac | 741 | if (ret != 0) { |
6a18b281 MD |
742 | goto end; |
743 | } | |
5280f742 | 744 | g_string_append(pretty->string, " )"); |
6a18b281 | 745 | end: |
6a18b281 MD |
746 | return ret; |
747 | } | |
748 | ||
2b4c4a7c | 749 | static |
3228cc1d | 750 | int filter_field_name(struct pretty_component *pretty, const char *field_name, |
2b4c4a7c JD |
751 | GQuark *filter_fields, int filter_array_len) |
752 | { | |
753 | int i; | |
754 | GQuark field_quark = g_quark_try_string(field_name); | |
755 | ||
3228cc1d | 756 | if (!field_quark || pretty->options.verbose) { |
2b4c4a7c JD |
757 | return 1; |
758 | } | |
759 | ||
760 | for (i = 0; i < filter_array_len; i++) { | |
761 | if (field_quark == filter_fields[i]) { | |
762 | return 0; | |
763 | } | |
764 | } | |
765 | return 1; | |
766 | } | |
767 | ||
6a18b281 | 768 | static |
d94d92ac | 769 | int print_struct_field(struct pretty_component *pretty, |
b19ff26f PP |
770 | const bt_field *_struct, |
771 | const bt_field_class *struct_class, | |
44c440bc | 772 | uint64_t i, bool print_names, uint64_t *nr_printed_fields, |
2b4c4a7c | 773 | GQuark *filter_fields, int filter_array_len) |
6a18b281 | 774 | { |
d94d92ac | 775 | int ret = 0; |
6a18b281 | 776 | const char *field_name; |
b19ff26f | 777 | const bt_field *field = NULL; |
1e6fd1d7 | 778 | const bt_field_class_structure_member *member; |
6a18b281 | 779 | |
40f4ba76 | 780 | field = bt_field_structure_borrow_member_field_by_index_const(_struct, i); |
6a18b281 | 781 | if (!field) { |
d94d92ac | 782 | ret = -1; |
6a18b281 MD |
783 | goto end; |
784 | } | |
44c440bc | 785 | |
1e6fd1d7 PP |
786 | member = bt_field_class_structure_borrow_member_by_index_const( |
787 | struct_class, i); | |
788 | field_name = bt_field_class_structure_member_get_name(member); | |
6a18b281 | 789 | |
3228cc1d | 790 | if (filter_fields && !filter_field_name(pretty, field_name, |
2b4c4a7c | 791 | filter_fields, filter_array_len)) { |
d94d92ac | 792 | ret = 0; |
2b4c4a7c JD |
793 | goto end; |
794 | } | |
795 | ||
796 | if (*nr_printed_fields > 0) { | |
5280f742 | 797 | g_string_append(pretty->string, ", "); |
6a18b281 | 798 | } else { |
5280f742 | 799 | g_string_append(pretty->string, " "); |
6a18b281 MD |
800 | } |
801 | if (print_names) { | |
6acddae3 | 802 | print_field_name_equal(pretty, field_name); |
6a18b281 | 803 | } |
3228cc1d | 804 | ret = print_field(pretty, field, print_names, NULL, 0); |
2b4c4a7c | 805 | *nr_printed_fields += 1; |
a82e90e8 | 806 | |
6a18b281 | 807 | end: |
6a18b281 MD |
808 | return ret; |
809 | } | |
810 | ||
811 | static | |
d94d92ac | 812 | int print_struct(struct pretty_component *pretty, |
b19ff26f | 813 | const bt_field *_struct, bool print_names, |
2b4c4a7c | 814 | GQuark *filter_fields, int filter_array_len) |
6a18b281 | 815 | { |
d94d92ac | 816 | int ret = 0; |
b19ff26f | 817 | const bt_field_class *struct_class = NULL; |
44c440bc | 818 | uint64_t nr_fields, i, nr_printed_fields; |
6a18b281 | 819 | |
40f4ba76 | 820 | struct_class = bt_field_borrow_class_const(_struct); |
5cd6d0e5 | 821 | if (!struct_class) { |
d94d92ac | 822 | ret = -1; |
6a18b281 MD |
823 | goto end; |
824 | } | |
53dc85fa | 825 | |
5cd6d0e5 | 826 | nr_fields = bt_field_class_structure_get_member_count(struct_class); |
53dc85fa | 827 | |
5280f742 | 828 | g_string_append(pretty->string, "{"); |
3228cc1d | 829 | pretty->depth++; |
2b4c4a7c | 830 | nr_printed_fields = 0; |
6a18b281 | 831 | for (i = 0; i < nr_fields; i++) { |
5cd6d0e5 | 832 | ret = print_struct_field(pretty, _struct, struct_class, i, |
2b4c4a7c JD |
833 | print_names, &nr_printed_fields, filter_fields, |
834 | filter_array_len); | |
d94d92ac | 835 | if (ret != 0) { |
6a18b281 MD |
836 | goto end; |
837 | } | |
838 | } | |
3228cc1d | 839 | pretty->depth--; |
5280f742 | 840 | g_string_append(pretty->string, " }"); |
a82e90e8 | 841 | |
6a18b281 | 842 | end: |
6a18b281 MD |
843 | return ret; |
844 | } | |
845 | ||
846 | static | |
d94d92ac | 847 | int print_array_field(struct pretty_component *pretty, |
b19ff26f | 848 | const bt_field *array, uint64_t i, bool print_names) |
6a18b281 | 849 | { |
b19ff26f | 850 | const bt_field *field = NULL; |
6a18b281 | 851 | |
44c440bc PP |
852 | if (i != 0) { |
853 | g_string_append(pretty->string, ", "); | |
854 | } else { | |
855 | g_string_append(pretty->string, " "); | |
6a18b281 | 856 | } |
44c440bc PP |
857 | if (print_names) { |
858 | g_string_append_printf(pretty->string, "[%" PRIu64 "] = ", i); | |
6a18b281 | 859 | } |
a82e90e8 | 860 | |
40f4ba76 | 861 | field = bt_field_array_borrow_element_field_by_index_const(array, i); |
44c440bc PP |
862 | BT_ASSERT(field); |
863 | return print_field(pretty, field, print_names, NULL, 0); | |
6a18b281 MD |
864 | } |
865 | ||
866 | static | |
d94d92ac | 867 | int print_array(struct pretty_component *pretty, |
b19ff26f | 868 | const bt_field *array, bool print_names) |
6a18b281 | 869 | { |
d94d92ac | 870 | int ret = 0; |
b19ff26f | 871 | const bt_field_class *array_class = NULL; |
44c440bc | 872 | uint64_t len; |
6a18b281 | 873 | uint64_t i; |
6a18b281 | 874 | |
40f4ba76 | 875 | array_class = bt_field_borrow_class_const(array); |
5cd6d0e5 | 876 | if (!array_class) { |
d94d92ac | 877 | ret = -1; |
6a18b281 MD |
878 | goto end; |
879 | } | |
44c440bc PP |
880 | len = bt_field_array_get_length(array); |
881 | g_string_append(pretty->string, "["); | |
3228cc1d | 882 | pretty->depth++; |
6a18b281 | 883 | for (i = 0; i < len; i++) { |
44c440bc | 884 | ret = print_array_field(pretty, array, i, print_names); |
d94d92ac | 885 | if (ret != 0) { |
6a18b281 MD |
886 | goto end; |
887 | } | |
888 | } | |
3228cc1d | 889 | pretty->depth--; |
44c440bc | 890 | g_string_append(pretty->string, " ]"); |
a82e90e8 | 891 | |
6a18b281 | 892 | end: |
6a18b281 MD |
893 | return ret; |
894 | } | |
895 | ||
896 | static | |
d94d92ac | 897 | int print_sequence_field(struct pretty_component *pretty, |
b19ff26f | 898 | const bt_field *seq, uint64_t i, bool print_names) |
6a18b281 | 899 | { |
b19ff26f | 900 | const bt_field *field = NULL; |
6a18b281 | 901 | |
44c440bc PP |
902 | if (i != 0) { |
903 | g_string_append(pretty->string, ", "); | |
904 | } else { | |
905 | g_string_append(pretty->string, " "); | |
6a18b281 | 906 | } |
44c440bc PP |
907 | if (print_names) { |
908 | g_string_append_printf(pretty->string, "[%" PRIu64 "] = ", i); | |
6a18b281 | 909 | } |
a82e90e8 | 910 | |
40f4ba76 | 911 | field = bt_field_array_borrow_element_field_by_index_const(seq, i); |
44c440bc PP |
912 | BT_ASSERT(field); |
913 | return print_field(pretty, field, print_names, NULL, 0); | |
6a18b281 MD |
914 | } |
915 | ||
916 | static | |
d94d92ac | 917 | int print_sequence(struct pretty_component *pretty, |
b19ff26f | 918 | const bt_field *seq, bool print_names) |
6a18b281 | 919 | { |
d94d92ac | 920 | int ret = 0; |
44c440bc | 921 | uint64_t len; |
6a18b281 | 922 | uint64_t i; |
6a18b281 | 923 | |
44c440bc | 924 | len = bt_field_array_get_length(seq); |
312c056a | 925 | if (len < 0) { |
d94d92ac | 926 | ret = -1; |
6a18b281 MD |
927 | goto end; |
928 | } | |
6a18b281 | 929 | |
44c440bc | 930 | g_string_append(pretty->string, "["); |
6a18b281 | 931 | |
3228cc1d | 932 | pretty->depth++; |
6a18b281 | 933 | for (i = 0; i < len; i++) { |
44c440bc | 934 | ret = print_sequence_field(pretty, seq, i, print_names); |
d94d92ac | 935 | if (ret != 0) { |
6a18b281 MD |
936 | goto end; |
937 | } | |
938 | } | |
3228cc1d | 939 | pretty->depth--; |
44c440bc | 940 | g_string_append(pretty->string, " ]"); |
a82e90e8 | 941 | |
6a18b281 | 942 | end: |
6a18b281 MD |
943 | return ret; |
944 | } | |
945 | ||
946 | static | |
d94d92ac | 947 | int print_variant(struct pretty_component *pretty, |
b19ff26f | 948 | const bt_field *variant, bool print_names) |
6a18b281 | 949 | { |
d94d92ac | 950 | int ret = 0; |
b19ff26f | 951 | const bt_field *field = NULL; |
6a18b281 | 952 | |
40f4ba76 | 953 | field = bt_field_variant_borrow_selected_option_field_const(variant); |
44c440bc | 954 | BT_ASSERT(field); |
5280f742 | 955 | g_string_append(pretty->string, "{ "); |
3228cc1d | 956 | pretty->depth++; |
6a18b281 | 957 | if (print_names) { |
44c440bc PP |
958 | // TODO: find tag's name using field path |
959 | // print_field_name_equal(pretty, tag_choice); | |
6a18b281 | 960 | } |
3228cc1d | 961 | ret = print_field(pretty, field, print_names, NULL, 0); |
d94d92ac | 962 | if (ret != 0) { |
6a18b281 MD |
963 | goto end; |
964 | } | |
3228cc1d | 965 | pretty->depth--; |
5280f742 | 966 | g_string_append(pretty->string, " }"); |
a82e90e8 | 967 | |
6a18b281 | 968 | end: |
6a18b281 MD |
969 | return ret; |
970 | } | |
971 | ||
972 | static | |
d94d92ac | 973 | int print_field(struct pretty_component *pretty, |
b19ff26f | 974 | const bt_field *field, bool print_names, |
2b4c4a7c | 975 | GQuark *filter_fields, int filter_array_len) |
6a18b281 | 976 | { |
4cdfc5e8 | 977 | bt_field_class_type class_id; |
6a18b281 | 978 | |
864cad70 | 979 | class_id = bt_field_get_class_type(field); |
5cd6d0e5 | 980 | switch (class_id) { |
864cad70 PP |
981 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: |
982 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: | |
3228cc1d | 983 | return print_integer(pretty, field); |
864cad70 | 984 | case BT_FIELD_CLASS_TYPE_REAL: |
6a18b281 MD |
985 | { |
986 | double v; | |
987 | ||
44c440bc | 988 | v = bt_field_real_get_value(field); |
3228cc1d | 989 | if (pretty->use_colors) { |
5280f742 | 990 | g_string_append(pretty->string, COLOR_NUMBER_VALUE); |
ad96d936 | 991 | } |
5280f742 | 992 | g_string_append_printf(pretty->string, "%g", v); |
3228cc1d | 993 | if (pretty->use_colors) { |
5280f742 | 994 | g_string_append(pretty->string, COLOR_RST); |
ad96d936 | 995 | } |
d94d92ac | 996 | return 0; |
6a18b281 | 997 | } |
864cad70 PP |
998 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: |
999 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: | |
3228cc1d | 1000 | return print_enum(pretty, field); |
864cad70 | 1001 | case BT_FIELD_CLASS_TYPE_STRING: |
95a518bf JG |
1002 | { |
1003 | const char *str; | |
1004 | ||
50842bdc | 1005 | str = bt_field_string_get_value(field); |
95a518bf | 1006 | if (!str) { |
d94d92ac | 1007 | return -1; |
95a518bf JG |
1008 | } |
1009 | ||
3228cc1d | 1010 | if (pretty->use_colors) { |
5280f742 | 1011 | g_string_append(pretty->string, COLOR_STRING_VALUE); |
ad96d936 | 1012 | } |
95a518bf | 1013 | print_escape_string(pretty, str); |
3228cc1d | 1014 | if (pretty->use_colors) { |
5280f742 | 1015 | g_string_append(pretty->string, COLOR_RST); |
ad96d936 | 1016 | } |
d94d92ac | 1017 | return 0; |
95a518bf | 1018 | } |
864cad70 | 1019 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
3228cc1d | 1020 | return print_struct(pretty, field, print_names, filter_fields, |
2b4c4a7c | 1021 | filter_array_len); |
864cad70 | 1022 | case BT_FIELD_CLASS_TYPE_VARIANT: |
3228cc1d | 1023 | return print_variant(pretty, field, print_names); |
864cad70 | 1024 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
3228cc1d | 1025 | return print_array(pretty, field, print_names); |
864cad70 | 1026 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: |
3228cc1d | 1027 | return print_sequence(pretty, field, print_names); |
6a18b281 | 1028 | default: |
5280f742 | 1029 | // TODO: log instead |
5cd6d0e5 | 1030 | fprintf(pretty->err, "[error] Unknown type id: %d\n", (int) class_id); |
d94d92ac | 1031 | return -1; |
6a18b281 MD |
1032 | } |
1033 | } | |
1034 | ||
1035 | static | |
d94d92ac | 1036 | int print_stream_packet_context(struct pretty_component *pretty, |
b19ff26f | 1037 | const bt_event *event) |
6a18b281 | 1038 | { |
d94d92ac | 1039 | int ret = 0; |
b19ff26f PP |
1040 | const bt_packet *packet = NULL; |
1041 | const bt_field *main_field = NULL; | |
6a18b281 | 1042 | |
40f4ba76 | 1043 | packet = bt_event_borrow_packet_const(event); |
6a18b281 | 1044 | if (!packet) { |
d94d92ac | 1045 | ret = -1; |
6a18b281 MD |
1046 | goto end; |
1047 | } | |
40f4ba76 | 1048 | main_field = bt_packet_borrow_context_field_const(packet); |
6a18b281 | 1049 | if (!main_field) { |
6a18b281 MD |
1050 | goto end; |
1051 | } | |
3228cc1d | 1052 | if (!pretty->start_line) { |
5280f742 | 1053 | g_string_append(pretty->string, ", "); |
6e1bc0df | 1054 | } |
3228cc1d PP |
1055 | pretty->start_line = false; |
1056 | if (pretty->options.print_scope_field_names) { | |
1057 | print_name_equal(pretty, "stream.packet.context"); | |
6a18b281 | 1058 | } |
3228cc1d PP |
1059 | ret = print_field(pretty, main_field, |
1060 | pretty->options.print_context_field_names, | |
2b4c4a7c JD |
1061 | stream_packet_context_quarks, |
1062 | STREAM_PACKET_CONTEXT_QUARKS_LEN); | |
a82e90e8 | 1063 | |
6a18b281 | 1064 | end: |
6a18b281 MD |
1065 | return ret; |
1066 | } | |
1067 | ||
6a18b281 | 1068 | static |
d94d92ac | 1069 | int print_stream_event_context(struct pretty_component *pretty, |
b19ff26f | 1070 | const bt_event *event) |
6a18b281 | 1071 | { |
d94d92ac | 1072 | int ret = 0; |
b19ff26f | 1073 | const bt_field *main_field = NULL; |
6a18b281 | 1074 | |
40f4ba76 | 1075 | main_field = bt_event_borrow_common_context_field_const(event); |
6a18b281 | 1076 | if (!main_field) { |
6a18b281 MD |
1077 | goto end; |
1078 | } | |
3228cc1d | 1079 | if (!pretty->start_line) { |
5280f742 | 1080 | g_string_append(pretty->string, ", "); |
6e1bc0df | 1081 | } |
3228cc1d PP |
1082 | pretty->start_line = false; |
1083 | if (pretty->options.print_scope_field_names) { | |
1084 | print_name_equal(pretty, "stream.event.context"); | |
6a18b281 | 1085 | } |
3228cc1d PP |
1086 | ret = print_field(pretty, main_field, |
1087 | pretty->options.print_context_field_names, NULL, 0); | |
a82e90e8 | 1088 | |
6a18b281 | 1089 | end: |
6a18b281 MD |
1090 | return ret; |
1091 | } | |
1092 | ||
1093 | static | |
d94d92ac | 1094 | int print_event_context(struct pretty_component *pretty, |
b19ff26f | 1095 | const bt_event *event) |
6a18b281 | 1096 | { |
d94d92ac | 1097 | int ret = 0; |
b19ff26f | 1098 | const bt_field *main_field = NULL; |
6a18b281 | 1099 | |
40f4ba76 | 1100 | main_field = bt_event_borrow_specific_context_field_const(event); |
6a18b281 | 1101 | if (!main_field) { |
6a18b281 MD |
1102 | goto end; |
1103 | } | |
3228cc1d | 1104 | if (!pretty->start_line) { |
5280f742 | 1105 | g_string_append(pretty->string, ", "); |
6e1bc0df | 1106 | } |
3228cc1d PP |
1107 | pretty->start_line = false; |
1108 | if (pretty->options.print_scope_field_names) { | |
1109 | print_name_equal(pretty, "event.context"); | |
6a18b281 | 1110 | } |
3228cc1d PP |
1111 | ret = print_field(pretty, main_field, |
1112 | pretty->options.print_context_field_names, NULL, 0); | |
a82e90e8 | 1113 | |
6a18b281 | 1114 | end: |
6a18b281 MD |
1115 | return ret; |
1116 | } | |
1117 | ||
1118 | static | |
d94d92ac | 1119 | int print_event_payload(struct pretty_component *pretty, |
b19ff26f | 1120 | const bt_event *event) |
6a18b281 | 1121 | { |
d94d92ac | 1122 | int ret = 0; |
b19ff26f | 1123 | const bt_field *main_field = NULL; |
6a18b281 | 1124 | |
40f4ba76 | 1125 | main_field = bt_event_borrow_payload_field_const(event); |
6a18b281 | 1126 | if (!main_field) { |
6a18b281 MD |
1127 | goto end; |
1128 | } | |
3228cc1d | 1129 | if (!pretty->start_line) { |
5280f742 | 1130 | g_string_append(pretty->string, ", "); |
6e1bc0df | 1131 | } |
3228cc1d PP |
1132 | pretty->start_line = false; |
1133 | if (pretty->options.print_scope_field_names) { | |
1134 | print_name_equal(pretty, "event.fields"); | |
6a18b281 | 1135 | } |
3228cc1d PP |
1136 | ret = print_field(pretty, main_field, |
1137 | pretty->options.print_payload_field_names, NULL, 0); | |
a82e90e8 | 1138 | |
6a18b281 | 1139 | end: |
af9a82eb JG |
1140 | return ret; |
1141 | } | |
1142 | ||
08899d39 | 1143 | static |
0f6bea4e | 1144 | int flush_buf(FILE *stream, struct pretty_component *pretty) |
08899d39 PP |
1145 | { |
1146 | int ret = 0; | |
1147 | ||
1148 | if (pretty->string->len == 0) { | |
1149 | goto end; | |
1150 | } | |
1151 | ||
0f6bea4e | 1152 | if (fwrite(pretty->string->str, pretty->string->len, 1, stream) != 1) { |
08899d39 PP |
1153 | ret = -1; |
1154 | } | |
1155 | ||
1156 | end: | |
1157 | return ret; | |
1158 | } | |
1159 | ||
af9a82eb | 1160 | BT_HIDDEN |
d94d92ac | 1161 | int pretty_print_event(struct pretty_component *pretty, |
d6e69534 | 1162 | const bt_message *event_msg) |
af9a82eb | 1163 | { |
d94d92ac | 1164 | int ret; |
b19ff26f | 1165 | const bt_event *event = |
d6e69534 | 1166 | bt_message_event_borrow_event_const(event_msg); |
af9a82eb | 1167 | |
f6ccaed9 | 1168 | BT_ASSERT(event); |
3228cc1d | 1169 | pretty->start_line = true; |
5280f742 | 1170 | g_string_assign(pretty->string, ""); |
2c091c04 | 1171 | ret = print_event_header(pretty, event_msg); |
d94d92ac | 1172 | if (ret != 0) { |
af9a82eb JG |
1173 | goto end; |
1174 | } | |
6a18b281 | 1175 | |
3228cc1d | 1176 | ret = print_stream_packet_context(pretty, event); |
d94d92ac | 1177 | if (ret != 0) { |
6a18b281 MD |
1178 | goto end; |
1179 | } | |
6a18b281 | 1180 | |
3228cc1d | 1181 | ret = print_stream_event_context(pretty, event); |
d94d92ac | 1182 | if (ret != 0) { |
6a18b281 MD |
1183 | goto end; |
1184 | } | |
6a18b281 | 1185 | |
3228cc1d | 1186 | ret = print_event_context(pretty, event); |
d94d92ac | 1187 | if (ret != 0) { |
6a18b281 MD |
1188 | goto end; |
1189 | } | |
6a18b281 | 1190 | |
3228cc1d | 1191 | ret = print_event_payload(pretty, event); |
d94d92ac | 1192 | if (ret != 0) { |
6a18b281 MD |
1193 | goto end; |
1194 | } | |
af9a82eb | 1195 | |
5280f742 | 1196 | g_string_append_c(pretty->string, '\n'); |
0f6bea4e | 1197 | if (flush_buf(pretty->out, pretty)) { |
d94d92ac | 1198 | ret = -1; |
5280f742 PP |
1199 | goto end; |
1200 | } | |
1201 | ||
af9a82eb JG |
1202 | end: |
1203 | return ret; | |
1204 | } | |
0f6bea4e PP |
1205 | |
1206 | static | |
8e53bed4 PP |
1207 | int print_discarded_elements_msg(struct pretty_component *pretty, |
1208 | const bt_stream *stream, | |
1209 | const bt_clock_snapshot *begin_clock_snapshot, | |
1210 | const bt_clock_snapshot *end_clock_snapshot, | |
0f6bea4e PP |
1211 | uint64_t count, const char *elem_type) |
1212 | { | |
d94d92ac | 1213 | int ret = 0; |
b19ff26f PP |
1214 | const bt_stream_class *stream_class = NULL; |
1215 | const bt_trace *trace = NULL; | |
0f6bea4e PP |
1216 | const char *stream_name; |
1217 | const char *trace_name; | |
8e53bed4 | 1218 | bt_uuid trace_uuid; |
0f6bea4e PP |
1219 | int64_t stream_class_id; |
1220 | int64_t stream_id; | |
697f479d | 1221 | const char *init_msg; |
0f6bea4e PP |
1222 | |
1223 | /* Stream name */ | |
0f6bea4e | 1224 | stream_name = bt_stream_get_name(stream); |
8e53bed4 PP |
1225 | if (!stream_name) { |
1226 | stream_name = "(unknown)"; | |
1227 | } | |
0f6bea4e PP |
1228 | |
1229 | /* Stream class ID */ | |
40f4ba76 | 1230 | stream_class = bt_stream_borrow_class_const(stream); |
0f6bea4e PP |
1231 | BT_ASSERT(stream_class); |
1232 | stream_class_id = bt_stream_class_get_id(stream_class); | |
1233 | ||
1234 | /* Stream ID */ | |
1235 | stream_id = bt_stream_get_id(stream); | |
1236 | ||
1237 | /* Trace name */ | |
8e53bed4 | 1238 | trace = bt_stream_borrow_trace_const(stream); |
0f6bea4e PP |
1239 | BT_ASSERT(trace); |
1240 | trace_name = bt_trace_get_name(trace); | |
1241 | if (!trace_name) { | |
1242 | trace_name = "(unknown)"; | |
1243 | } | |
1244 | ||
1245 | /* Trace UUID */ | |
8e53bed4 PP |
1246 | trace_uuid = bt_trace_class_get_uuid( |
1247 | bt_trace_borrow_class_const(trace)); | |
0f6bea4e PP |
1248 | |
1249 | /* Format message */ | |
1250 | g_string_assign(pretty->string, ""); | |
697f479d PP |
1251 | |
1252 | if (count == UINT64_C(-1)) { | |
1253 | init_msg = "Tracer may have discarded"; | |
1254 | } else { | |
1255 | init_msg = "Tracer discarded"; | |
1256 | } | |
1257 | ||
0f6bea4e | 1258 | g_string_append_printf(pretty->string, |
697f479d | 1259 | "%s%sWARNING%s%s: %s ", |
0f6bea4e PP |
1260 | bt_common_color_fg_yellow(), |
1261 | bt_common_color_bold(), | |
1262 | bt_common_color_reset(), | |
697f479d | 1263 | bt_common_color_fg_yellow(), init_msg); |
8e53bed4 PP |
1264 | |
1265 | if (count == UINT64_C(-1)) { | |
697f479d | 1266 | g_string_append_printf(pretty->string, "%ss", elem_type); |
8e53bed4 PP |
1267 | } else { |
1268 | g_string_append_printf(pretty->string, | |
1269 | "%" PRIu64 " %s%s", count, elem_type, | |
1270 | count == 1 ? "" : "s"); | |
1271 | } | |
0f6bea4e | 1272 | |
605e1019 | 1273 | if (begin_clock_snapshot && end_clock_snapshot) { |
8e53bed4 | 1274 | g_string_append(pretty->string, " between ["); |
9c202476 | 1275 | print_timestamp_wall(pretty, begin_clock_snapshot, false); |
0f6bea4e | 1276 | g_string_append(pretty->string, "] and ["); |
9c202476 | 1277 | print_timestamp_wall(pretty, end_clock_snapshot, false); |
0f6bea4e PP |
1278 | g_string_append(pretty->string, "]"); |
1279 | } else { | |
1280 | g_string_append(pretty->string, "(unknown time range)"); | |
1281 | } | |
1282 | ||
1283 | g_string_append_printf(pretty->string, " in trace \"%s\" ", trace_name); | |
1284 | ||
1285 | if (trace_uuid) { | |
1286 | g_string_append_printf(pretty->string, | |
1287 | "(UUID: %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x) ", | |
1288 | trace_uuid[0], | |
1289 | trace_uuid[1], | |
1290 | trace_uuid[2], | |
1291 | trace_uuid[3], | |
1292 | trace_uuid[4], | |
1293 | trace_uuid[5], | |
1294 | trace_uuid[6], | |
1295 | trace_uuid[7], | |
1296 | trace_uuid[8], | |
1297 | trace_uuid[9], | |
1298 | trace_uuid[10], | |
1299 | trace_uuid[11], | |
1300 | trace_uuid[12], | |
1301 | trace_uuid[13], | |
1302 | trace_uuid[14], | |
1303 | trace_uuid[15]); | |
1304 | } else { | |
1305 | g_string_append(pretty->string, "(no UUID) "); | |
1306 | } | |
1307 | ||
1308 | g_string_append_printf(pretty->string, | |
8e53bed4 | 1309 | "within stream \"%s\" (stream class ID: %" PRIu64 ", ", |
0f6bea4e PP |
1310 | stream_name, stream_class_id); |
1311 | ||
1312 | if (stream_id >= 0) { | |
1313 | g_string_append_printf(pretty->string, | |
8e53bed4 | 1314 | "stream ID: %" PRIu64, stream_id); |
0f6bea4e PP |
1315 | } else { |
1316 | g_string_append(pretty->string, "no stream ID"); | |
1317 | } | |
1318 | ||
1319 | g_string_append_printf(pretty->string, ").%s\n", | |
1320 | bt_common_color_reset()); | |
1321 | ||
1322 | /* | |
1323 | * Print to standard error stream to remain backward compatible | |
1324 | * with Babeltrace 1. | |
1325 | */ | |
1326 | if (flush_buf(stderr, pretty)) { | |
d94d92ac | 1327 | ret = -1; |
0f6bea4e PP |
1328 | } |
1329 | ||
1330 | return ret; | |
1331 | } | |
1332 | ||
1333 | BT_HIDDEN | |
8e53bed4 PP |
1334 | int pretty_print_discarded_items(struct pretty_component *pretty, |
1335 | const bt_message *msg) | |
0f6bea4e | 1336 | { |
8e53bed4 PP |
1337 | const bt_clock_snapshot *begin = NULL; |
1338 | const bt_clock_snapshot *end = NULL; | |
1339 | const bt_stream *stream; | |
1340 | const bt_stream_class *stream_class; | |
1341 | uint64_t count = UINT64_C(-1); | |
1342 | const char *elem_type; | |
1343 | ||
1344 | switch (bt_message_get_type(msg)) { | |
1345 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
1346 | stream = bt_message_discarded_events_borrow_stream_const(msg); | |
1347 | ||
1348 | if (bt_message_discarded_events_get_count(msg, &count) == | |
1349 | BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) { | |
1350 | count = UINT64_C(-1); | |
0f6bea4e | 1351 | } |
8e53bed4 PP |
1352 | |
1353 | elem_type = "event"; | |
1354 | break; | |
1355 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
1356 | stream = bt_message_discarded_packets_borrow_stream_const(msg); | |
1357 | ||
1358 | if (bt_message_discarded_packets_get_count(msg, &count) == | |
1359 | BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) { | |
1360 | count = UINT64_C(-1); | |
1361 | } | |
1362 | ||
1363 | elem_type = "packet"; | |
1364 | break; | |
1365 | default: | |
1366 | abort(); | |
0f6bea4e PP |
1367 | } |
1368 | ||
8e53bed4 PP |
1369 | BT_ASSERT(stream); |
1370 | stream_class = bt_stream_borrow_class_const(stream); | |
1371 | ||
1372 | if (bt_stream_class_borrow_default_clock_class_const(stream_class)) { | |
1373 | switch (bt_message_get_type(msg)) { | |
1374 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
1375 | bt_message_discarded_events_borrow_default_beginning_clock_snapshot_const( | |
1376 | msg, &begin); | |
1377 | bt_message_discarded_events_borrow_default_end_clock_snapshot_const( | |
1378 | msg, &end); | |
1379 | break; | |
1380 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
1381 | bt_message_discarded_packets_borrow_default_beginning_clock_snapshot_const( | |
1382 | msg, &begin); | |
1383 | bt_message_discarded_packets_borrow_default_end_clock_snapshot_const( | |
1384 | msg, &end); | |
1385 | break; | |
1386 | default: | |
1387 | abort(); | |
0f6bea4e PP |
1388 | } |
1389 | } | |
1390 | ||
8e53bed4 PP |
1391 | print_discarded_elements_msg(pretty, stream, begin, end, |
1392 | count, elem_type); | |
44c440bc | 1393 | return 0; |
0f6bea4e | 1394 | } |