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