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