| 1 | /* |
| 2 | * print.c |
| 3 | * |
| 4 | * Babeltrace CTF Text Output Plugin Event Printing |
| 5 | * |
| 6 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 7 | * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 8 | * |
| 9 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 10 | * |
| 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | * SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | #include <babeltrace/babeltrace.h> |
| 31 | #include <babeltrace/bitfield-internal.h> |
| 32 | #include <babeltrace/common-internal.h> |
| 33 | #include <babeltrace/compat/time-internal.h> |
| 34 | #include <babeltrace/assert-internal.h> |
| 35 | #include <inttypes.h> |
| 36 | #include <ctype.h> |
| 37 | #include "pretty.h" |
| 38 | |
| 39 | #define NSEC_PER_SEC 1000000000LL |
| 40 | |
| 41 | #define COLOR_NAME BT_COMMON_COLOR_BOLD |
| 42 | #define COLOR_FIELD_NAME BT_COMMON_COLOR_FG_CYAN |
| 43 | #define COLOR_RST BT_COMMON_COLOR_RESET |
| 44 | #define COLOR_STRING_VALUE BT_COMMON_COLOR_BOLD |
| 45 | #define COLOR_NUMBER_VALUE BT_COMMON_COLOR_BOLD |
| 46 | #define COLOR_ENUM_MAPPING_NAME BT_COMMON_COLOR_BOLD |
| 47 | #define COLOR_UNKNOWN BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_RED |
| 48 | #define COLOR_EVENT_NAME BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_MAGENTA |
| 49 | #define COLOR_TIMESTAMP BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_YELLOW |
| 50 | |
| 51 | struct timestamp { |
| 52 | int64_t real_timestamp; /* Relative to UNIX epoch. */ |
| 53 | uint64_t clock_value; /* In cycles. */ |
| 54 | }; |
| 55 | |
| 56 | static |
| 57 | enum bt_component_status print_field(struct pretty_component *pretty, |
| 58 | struct bt_field *field, bool print_names, |
| 59 | GQuark *filters_fields, int filter_array_len); |
| 60 | |
| 61 | static |
| 62 | void print_name_equal(struct pretty_component *pretty, const char *name) |
| 63 | { |
| 64 | if (pretty->use_colors) { |
| 65 | g_string_append_printf(pretty->string, "%s%s%s = ", COLOR_NAME, |
| 66 | name, COLOR_RST); |
| 67 | } else { |
| 68 | g_string_append_printf(pretty->string, "%s = ", name); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static |
| 73 | void print_field_name_equal(struct pretty_component *pretty, const char *name) |
| 74 | { |
| 75 | if (pretty->use_colors) { |
| 76 | g_string_append_printf(pretty->string, "%s%s%s = ", |
| 77 | COLOR_FIELD_NAME, name, COLOR_RST); |
| 78 | } else { |
| 79 | g_string_append_printf(pretty->string, "%s = ", name); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static |
| 84 | void print_timestamp_cycles(struct pretty_component *pretty, |
| 85 | struct bt_event *event) |
| 86 | { |
| 87 | struct bt_clock_value *clock_value; |
| 88 | uint64_t cycles; |
| 89 | enum bt_clock_value_status cv_status; |
| 90 | |
| 91 | cv_status = bt_event_borrow_default_clock_value(event, &clock_value); |
| 92 | if (cv_status != BT_CLOCK_VALUE_STATUS_KNOWN || !clock_value) { |
| 93 | g_string_append(pretty->string, "????????????????????"); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | cycles = bt_clock_value_get_value(clock_value); |
| 98 | g_string_append_printf(pretty->string, "%020" PRIu64, cycles); |
| 99 | |
| 100 | if (pretty->last_cycles_timestamp != -1ULL) { |
| 101 | pretty->delta_cycles = cycles - pretty->last_cycles_timestamp; |
| 102 | } |
| 103 | pretty->last_cycles_timestamp = cycles; |
| 104 | } |
| 105 | |
| 106 | static |
| 107 | void print_timestamp_wall(struct pretty_component *pretty, |
| 108 | struct bt_clock_value *clock_value) |
| 109 | { |
| 110 | int ret; |
| 111 | int64_t ts_nsec = 0; /* add configurable offset */ |
| 112 | int64_t ts_sec = 0; /* add configurable offset */ |
| 113 | uint64_t ts_sec_abs, ts_nsec_abs; |
| 114 | bool is_negative; |
| 115 | |
| 116 | if (!clock_value) { |
| 117 | g_string_append(pretty->string, "??:??:??.?????????"); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | ret = bt_clock_value_get_ns_from_origin(clock_value, &ts_nsec); |
| 122 | if (ret) { |
| 123 | // TODO: log, this is unexpected |
| 124 | g_string_append(pretty->string, "Error"); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | if (pretty->last_real_timestamp != -1ULL) { |
| 129 | pretty->delta_real_timestamp = ts_nsec - pretty->last_real_timestamp; |
| 130 | } |
| 131 | |
| 132 | pretty->last_real_timestamp = ts_nsec; |
| 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 | g_string_append(pretty->string, timestr); |
| 206 | } |
| 207 | |
| 208 | /* Print time in HH:MM:SS.ns */ |
| 209 | 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 | 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 | enum bt_component_status print_event_timestamp(struct pretty_component *pretty, |
| 223 | struct bt_event *event, bool *start_line) |
| 224 | { |
| 225 | bool print_names = pretty->options.print_header_field_names; |
| 226 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 227 | struct bt_stream *stream = NULL; |
| 228 | struct bt_stream_class *stream_class = NULL; |
| 229 | struct bt_trace *trace = NULL; |
| 230 | struct bt_clock_value *clock_value = NULL; |
| 231 | enum bt_clock_value_status cv_status; |
| 232 | |
| 233 | stream = bt_event_borrow_stream(event); |
| 234 | if (!stream) { |
| 235 | ret = BT_COMPONENT_STATUS_ERROR; |
| 236 | goto end; |
| 237 | } |
| 238 | |
| 239 | stream_class = bt_stream_borrow_class(stream); |
| 240 | if (!stream_class) { |
| 241 | ret = BT_COMPONENT_STATUS_ERROR; |
| 242 | goto end; |
| 243 | } |
| 244 | trace = bt_stream_class_borrow_trace(stream_class); |
| 245 | if (!trace) { |
| 246 | ret = BT_COMPONENT_STATUS_ERROR; |
| 247 | goto end; |
| 248 | } |
| 249 | |
| 250 | cv_status = bt_event_borrow_default_clock_value(event, &clock_value); |
| 251 | if (cv_status != BT_CLOCK_VALUE_STATUS_KNOWN || !clock_value) { |
| 252 | /* No default clock value: skip the timestamp without an error */ |
| 253 | goto end; |
| 254 | } |
| 255 | |
| 256 | if (print_names) { |
| 257 | print_name_equal(pretty, "timestamp"); |
| 258 | } else { |
| 259 | g_string_append(pretty->string, "["); |
| 260 | } |
| 261 | if (pretty->use_colors) { |
| 262 | g_string_append(pretty->string, COLOR_TIMESTAMP); |
| 263 | } |
| 264 | if (pretty->options.print_timestamp_cycles) { |
| 265 | print_timestamp_cycles(pretty, event); |
| 266 | } else { |
| 267 | clock_value = NULL; |
| 268 | cv_status = bt_event_borrow_default_clock_value(event, |
| 269 | &clock_value); |
| 270 | print_timestamp_wall(pretty, clock_value); |
| 271 | } |
| 272 | if (pretty->use_colors) { |
| 273 | g_string_append(pretty->string, COLOR_RST); |
| 274 | } |
| 275 | |
| 276 | if (!print_names) |
| 277 | g_string_append(pretty->string, "] "); |
| 278 | |
| 279 | if (pretty->options.print_delta_field) { |
| 280 | if (print_names) { |
| 281 | g_string_append(pretty->string, ", "); |
| 282 | print_name_equal(pretty, "delta"); |
| 283 | } else { |
| 284 | g_string_append(pretty->string, "("); |
| 285 | } |
| 286 | if (pretty->options.print_timestamp_cycles) { |
| 287 | if (pretty->delta_cycles == -1ULL) { |
| 288 | g_string_append(pretty->string, |
| 289 | "+??????????\?\?) "); /* Not a trigraph. */ |
| 290 | } else { |
| 291 | g_string_append_printf(pretty->string, |
| 292 | "+%012" PRIu64, pretty->delta_cycles); |
| 293 | } |
| 294 | } else { |
| 295 | if (pretty->delta_real_timestamp != -1ULL) { |
| 296 | uint64_t delta_sec, delta_nsec, delta; |
| 297 | |
| 298 | delta = pretty->delta_real_timestamp; |
| 299 | delta_sec = delta / NSEC_PER_SEC; |
| 300 | delta_nsec = delta % NSEC_PER_SEC; |
| 301 | g_string_append_printf(pretty->string, |
| 302 | "+%" PRIu64 ".%09" PRIu64, |
| 303 | delta_sec, delta_nsec); |
| 304 | } else { |
| 305 | g_string_append(pretty->string, "+?.?????????"); |
| 306 | } |
| 307 | } |
| 308 | if (!print_names) { |
| 309 | g_string_append(pretty->string, ") "); |
| 310 | } |
| 311 | } |
| 312 | *start_line = !print_names; |
| 313 | |
| 314 | end: |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | static |
| 319 | enum bt_component_status print_event_header(struct pretty_component *pretty, |
| 320 | struct bt_event *event) |
| 321 | { |
| 322 | bool print_names = pretty->options.print_header_field_names; |
| 323 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 324 | struct bt_event_class *event_class = NULL; |
| 325 | struct bt_stream_class *stream_class = NULL; |
| 326 | struct bt_trace *trace_class = NULL; |
| 327 | int dom_print = 0; |
| 328 | enum bt_property_availability prop_avail; |
| 329 | |
| 330 | event_class = bt_event_borrow_class(event); |
| 331 | if (!event_class) { |
| 332 | ret = BT_COMPONENT_STATUS_ERROR; |
| 333 | goto end; |
| 334 | } |
| 335 | stream_class = bt_event_class_borrow_stream_class(event_class); |
| 336 | if (!stream_class) { |
| 337 | ret = BT_COMPONENT_STATUS_ERROR; |
| 338 | goto end; |
| 339 | } |
| 340 | trace_class = bt_stream_class_borrow_trace(stream_class); |
| 341 | if (!trace_class) { |
| 342 | ret = BT_COMPONENT_STATUS_ERROR; |
| 343 | goto end; |
| 344 | } |
| 345 | ret = print_event_timestamp(pretty, event, &pretty->start_line); |
| 346 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 347 | goto end; |
| 348 | } |
| 349 | if (pretty->options.print_trace_field) { |
| 350 | const char *name; |
| 351 | |
| 352 | name = bt_trace_get_name(trace_class); |
| 353 | if (name) { |
| 354 | if (!pretty->start_line) { |
| 355 | g_string_append(pretty->string, ", "); |
| 356 | } |
| 357 | if (print_names) { |
| 358 | print_name_equal(pretty, "trace"); |
| 359 | } |
| 360 | |
| 361 | g_string_append(pretty->string, name); |
| 362 | |
| 363 | if (!print_names) { |
| 364 | g_string_append(pretty->string, " "); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | if (pretty->options.print_trace_hostname_field) { |
| 369 | struct bt_value *hostname_str; |
| 370 | |
| 371 | hostname_str = bt_trace_borrow_environment_entry_value_by_name( |
| 372 | trace_class, "hostname"); |
| 373 | if (hostname_str) { |
| 374 | const char *str; |
| 375 | |
| 376 | if (!pretty->start_line) { |
| 377 | g_string_append(pretty->string, ", "); |
| 378 | } |
| 379 | if (print_names) { |
| 380 | print_name_equal(pretty, "trace:hostname"); |
| 381 | } |
| 382 | if (bt_value_string_get(hostname_str, &str) |
| 383 | == BT_VALUE_STATUS_OK) { |
| 384 | g_string_append(pretty->string, str); |
| 385 | } |
| 386 | dom_print = 1; |
| 387 | } |
| 388 | } |
| 389 | if (pretty->options.print_trace_domain_field) { |
| 390 | struct bt_value *domain_str; |
| 391 | |
| 392 | domain_str = bt_trace_borrow_environment_entry_value_by_name( |
| 393 | trace_class, "domain"); |
| 394 | if (domain_str) { |
| 395 | const char *str; |
| 396 | |
| 397 | if (!pretty->start_line) { |
| 398 | g_string_append(pretty->string, ", "); |
| 399 | } |
| 400 | if (print_names) { |
| 401 | print_name_equal(pretty, "trace:domain"); |
| 402 | } else if (dom_print) { |
| 403 | g_string_append(pretty->string, ":"); |
| 404 | } |
| 405 | if (bt_value_string_get(domain_str, &str) |
| 406 | == BT_VALUE_STATUS_OK) { |
| 407 | g_string_append(pretty->string, str); |
| 408 | } |
| 409 | dom_print = 1; |
| 410 | } |
| 411 | } |
| 412 | if (pretty->options.print_trace_procname_field) { |
| 413 | struct bt_value *procname_str; |
| 414 | |
| 415 | procname_str = bt_trace_borrow_environment_entry_value_by_name( |
| 416 | trace_class, "procname"); |
| 417 | if (procname_str) { |
| 418 | const char *str; |
| 419 | |
| 420 | if (!pretty->start_line) { |
| 421 | g_string_append(pretty->string, ", "); |
| 422 | } |
| 423 | if (print_names) { |
| 424 | print_name_equal(pretty, "trace:procname"); |
| 425 | } else if (dom_print) { |
| 426 | g_string_append(pretty->string, ":"); |
| 427 | } |
| 428 | if (bt_value_string_get(procname_str, &str) |
| 429 | == BT_VALUE_STATUS_OK) { |
| 430 | g_string_append(pretty->string, str); |
| 431 | } |
| 432 | |
| 433 | dom_print = 1; |
| 434 | } |
| 435 | } |
| 436 | if (pretty->options.print_trace_vpid_field) { |
| 437 | struct bt_value *vpid_value; |
| 438 | |
| 439 | vpid_value = bt_trace_borrow_environment_entry_value_by_name( |
| 440 | trace_class, "vpid"); |
| 441 | if (vpid_value) { |
| 442 | int64_t value; |
| 443 | |
| 444 | if (!pretty->start_line) { |
| 445 | g_string_append(pretty->string, ", "); |
| 446 | } |
| 447 | if (print_names) { |
| 448 | print_name_equal(pretty, "trace:vpid"); |
| 449 | } else if (dom_print) { |
| 450 | g_string_append(pretty->string, ":"); |
| 451 | } |
| 452 | if (bt_value_integer_get(vpid_value, &value) |
| 453 | == BT_VALUE_STATUS_OK) { |
| 454 | g_string_append_printf(pretty->string, "(%" PRId64 ")", value); |
| 455 | } |
| 456 | |
| 457 | dom_print = 1; |
| 458 | } |
| 459 | } |
| 460 | if (pretty->options.print_loglevel_field) { |
| 461 | static const char *log_level_names[] = { |
| 462 | [ BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY ] = "TRACE_EMERG", |
| 463 | [ BT_EVENT_CLASS_LOG_LEVEL_ALERT ] = "TRACE_ALERT", |
| 464 | [ BT_EVENT_CLASS_LOG_LEVEL_CRITICAL ] = "TRACE_CRIT", |
| 465 | [ BT_EVENT_CLASS_LOG_LEVEL_ERROR ] = "TRACE_ERR", |
| 466 | [ BT_EVENT_CLASS_LOG_LEVEL_WARNING ] = "TRACE_WARNING", |
| 467 | [ BT_EVENT_CLASS_LOG_LEVEL_NOTICE ] = "TRACE_NOTICE", |
| 468 | [ BT_EVENT_CLASS_LOG_LEVEL_INFO ] = "TRACE_INFO", |
| 469 | [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM ] = "TRACE_DEBUG_SYSTEM", |
| 470 | [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM ] = "TRACE_DEBUG_PROGRAM", |
| 471 | [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS ] = "TRACE_DEBUG_PROCESS", |
| 472 | [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE ] = "TRACE_DEBUG_MODULE", |
| 473 | [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT ] = "TRACE_DEBUG_UNIT", |
| 474 | [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION ] = "TRACE_DEBUG_FUNCTION", |
| 475 | [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE ] = "TRACE_DEBUG_LINE", |
| 476 | [ BT_EVENT_CLASS_LOG_LEVEL_DEBUG ] = "TRACE_DEBUG", |
| 477 | }; |
| 478 | enum bt_event_class_log_level log_level; |
| 479 | const char *log_level_str = NULL; |
| 480 | |
| 481 | prop_avail = bt_event_class_get_log_level(event_class, |
| 482 | &log_level); |
| 483 | if (prop_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) { |
| 484 | log_level_str = log_level_names[log_level]; |
| 485 | BT_ASSERT(log_level_str); |
| 486 | |
| 487 | if (!pretty->start_line) { |
| 488 | g_string_append(pretty->string, ", "); |
| 489 | } |
| 490 | if (print_names) { |
| 491 | print_name_equal(pretty, "loglevel"); |
| 492 | } else if (dom_print) { |
| 493 | g_string_append(pretty->string, ":"); |
| 494 | } |
| 495 | |
| 496 | g_string_append(pretty->string, log_level_str); |
| 497 | g_string_append_printf( |
| 498 | pretty->string, " (%d)", (int) log_level); |
| 499 | dom_print = 1; |
| 500 | } |
| 501 | } |
| 502 | if (pretty->options.print_emf_field) { |
| 503 | const char *uri_str; |
| 504 | |
| 505 | uri_str = bt_event_class_get_emf_uri(event_class); |
| 506 | if (uri_str) { |
| 507 | if (!pretty->start_line) { |
| 508 | g_string_append(pretty->string, ", "); |
| 509 | } |
| 510 | if (print_names) { |
| 511 | print_name_equal(pretty, "model.emf.uri"); |
| 512 | } else if (dom_print) { |
| 513 | g_string_append(pretty->string, ":"); |
| 514 | } |
| 515 | |
| 516 | g_string_append(pretty->string, uri_str); |
| 517 | dom_print = 1; |
| 518 | } |
| 519 | } |
| 520 | if (dom_print && !print_names) { |
| 521 | g_string_append(pretty->string, " "); |
| 522 | } |
| 523 | if (!pretty->start_line) { |
| 524 | g_string_append(pretty->string, ", "); |
| 525 | } |
| 526 | pretty->start_line = true; |
| 527 | if (print_names) { |
| 528 | print_name_equal(pretty, "name"); |
| 529 | } |
| 530 | if (pretty->use_colors) { |
| 531 | g_string_append(pretty->string, COLOR_EVENT_NAME); |
| 532 | } |
| 533 | g_string_append(pretty->string, bt_event_class_get_name(event_class)); |
| 534 | if (pretty->use_colors) { |
| 535 | g_string_append(pretty->string, COLOR_RST); |
| 536 | } |
| 537 | if (!print_names) { |
| 538 | g_string_append(pretty->string, ": "); |
| 539 | } else { |
| 540 | g_string_append(pretty->string, ", "); |
| 541 | } |
| 542 | |
| 543 | end: |
| 544 | return ret; |
| 545 | } |
| 546 | |
| 547 | static |
| 548 | enum bt_component_status print_integer(struct pretty_component *pretty, |
| 549 | struct bt_field *field) |
| 550 | { |
| 551 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 552 | enum bt_field_type_integer_preferred_display_base base; |
| 553 | struct bt_field_type *int_ft; |
| 554 | union { |
| 555 | uint64_t u; |
| 556 | int64_t s; |
| 557 | } v; |
| 558 | bool rst_color = false; |
| 559 | enum bt_field_type_id ft_id; |
| 560 | |
| 561 | int_ft = bt_field_borrow_type(field); |
| 562 | BT_ASSERT(int_ft); |
| 563 | ft_id = bt_field_get_type_id(field); |
| 564 | if (ft_id == BT_FIELD_TYPE_ID_UNSIGNED_INTEGER || |
| 565 | ft_id == BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION) { |
| 566 | v.u = bt_field_unsigned_integer_get_value(field); |
| 567 | } else { |
| 568 | v.s = bt_field_signed_integer_get_value(field); |
| 569 | } |
| 570 | |
| 571 | if (pretty->use_colors) { |
| 572 | g_string_append(pretty->string, COLOR_NUMBER_VALUE); |
| 573 | rst_color = true; |
| 574 | } |
| 575 | |
| 576 | base = bt_field_type_integer_get_preferred_display_base(int_ft); |
| 577 | switch (base) { |
| 578 | case BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_BINARY: |
| 579 | { |
| 580 | int bitnr, len; |
| 581 | |
| 582 | len = bt_field_type_integer_get_field_value_range(int_ft); |
| 583 | g_string_append(pretty->string, "0b"); |
| 584 | v.u = _bt_piecewise_lshift(v.u, 64 - len); |
| 585 | for (bitnr = 0; bitnr < len; bitnr++) { |
| 586 | g_string_append_printf(pretty->string, "%u", (v.u & (1ULL << 63)) ? 1 : 0); |
| 587 | v.u = _bt_piecewise_lshift(v.u, 1); |
| 588 | } |
| 589 | break; |
| 590 | } |
| 591 | case BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL: |
| 592 | { |
| 593 | if (ft_id == BT_FIELD_TYPE_ID_SIGNED_INTEGER || |
| 594 | ft_id == BT_FIELD_TYPE_ID_SIGNED_ENUMERATION) { |
| 595 | int len; |
| 596 | |
| 597 | len = bt_field_type_integer_get_field_value_range( |
| 598 | int_ft); |
| 599 | if (len < 64) { |
| 600 | size_t rounded_len; |
| 601 | |
| 602 | BT_ASSERT(len != 0); |
| 603 | /* Round length to the nearest 3-bit */ |
| 604 | rounded_len = (((len - 1) / 3) + 1) * 3; |
| 605 | v.u &= ((uint64_t) 1 << rounded_len) - 1; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | g_string_append_printf(pretty->string, "0%" PRIo64, v.u); |
| 610 | break; |
| 611 | } |
| 612 | case BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL: |
| 613 | if (ft_id == BT_FIELD_TYPE_ID_UNSIGNED_INTEGER || |
| 614 | ft_id == BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION) { |
| 615 | g_string_append_printf(pretty->string, "%" PRIu64, v.u); |
| 616 | } else { |
| 617 | g_string_append_printf(pretty->string, "%" PRId64, v.s); |
| 618 | } |
| 619 | break; |
| 620 | case BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL: |
| 621 | { |
| 622 | int len; |
| 623 | |
| 624 | len = bt_field_type_integer_get_field_value_range(int_ft); |
| 625 | if (len < 64) { |
| 626 | /* Round length to the nearest nibble */ |
| 627 | uint8_t rounded_len = ((len + 3) & ~0x3); |
| 628 | |
| 629 | v.u &= ((uint64_t) 1 << rounded_len) - 1; |
| 630 | } |
| 631 | |
| 632 | g_string_append_printf(pretty->string, "0x%" PRIX64, v.u); |
| 633 | break; |
| 634 | } |
| 635 | default: |
| 636 | ret = BT_COMPONENT_STATUS_ERROR; |
| 637 | goto end; |
| 638 | } |
| 639 | end: |
| 640 | if (rst_color) { |
| 641 | g_string_append(pretty->string, COLOR_RST); |
| 642 | } |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | static |
| 647 | void print_escape_string(struct pretty_component *pretty, const char *str) |
| 648 | { |
| 649 | int i; |
| 650 | |
| 651 | g_string_append_c(pretty->string, '"'); |
| 652 | |
| 653 | for (i = 0; i < strlen(str); i++) { |
| 654 | /* Escape sequences not recognized by iscntrl(). */ |
| 655 | switch (str[i]) { |
| 656 | case '\\': |
| 657 | g_string_append(pretty->string, "\\\\"); |
| 658 | continue; |
| 659 | case '\'': |
| 660 | g_string_append(pretty->string, "\\\'"); |
| 661 | continue; |
| 662 | case '\"': |
| 663 | g_string_append(pretty->string, "\\\""); |
| 664 | continue; |
| 665 | case '\?': |
| 666 | g_string_append(pretty->string, "\\\?"); |
| 667 | continue; |
| 668 | } |
| 669 | |
| 670 | /* Standard characters. */ |
| 671 | if (!iscntrl(str[i])) { |
| 672 | g_string_append_c(pretty->string, str[i]); |
| 673 | continue; |
| 674 | } |
| 675 | |
| 676 | switch (str[i]) { |
| 677 | case '\0': |
| 678 | g_string_append(pretty->string, "\\0"); |
| 679 | break; |
| 680 | case '\a': |
| 681 | g_string_append(pretty->string, "\\a"); |
| 682 | break; |
| 683 | case '\b': |
| 684 | g_string_append(pretty->string, "\\b"); |
| 685 | break; |
| 686 | case '\e': |
| 687 | g_string_append(pretty->string, "\\e"); |
| 688 | break; |
| 689 | case '\f': |
| 690 | g_string_append(pretty->string, "\\f"); |
| 691 | break; |
| 692 | case '\n': |
| 693 | g_string_append(pretty->string, "\\n"); |
| 694 | break; |
| 695 | case '\r': |
| 696 | g_string_append(pretty->string, "\\r"); |
| 697 | break; |
| 698 | case '\t': |
| 699 | g_string_append(pretty->string, "\\t"); |
| 700 | break; |
| 701 | case '\v': |
| 702 | g_string_append(pretty->string, "\\v"); |
| 703 | break; |
| 704 | default: |
| 705 | /* Unhandled control-sequence, print as hex. */ |
| 706 | g_string_append_printf(pretty->string, "\\x%02x", str[i]); |
| 707 | break; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | g_string_append_c(pretty->string, '"'); |
| 712 | } |
| 713 | |
| 714 | static |
| 715 | enum bt_component_status print_enum(struct pretty_component *pretty, |
| 716 | struct bt_field *field) |
| 717 | { |
| 718 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 719 | struct bt_field_type *enumeration_field_type = NULL; |
| 720 | bt_field_type_enumeration_mapping_label_array label_array; |
| 721 | uint64_t label_count; |
| 722 | uint64_t i; |
| 723 | |
| 724 | enumeration_field_type = bt_field_borrow_type(field); |
| 725 | if (!enumeration_field_type) { |
| 726 | ret = BT_COMPONENT_STATUS_ERROR; |
| 727 | goto end; |
| 728 | } |
| 729 | |
| 730 | switch (bt_field_get_type_id(field)) { |
| 731 | case BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION: |
| 732 | ret = bt_field_unsigned_enumeration_get_mapping_labels(field, |
| 733 | &label_array, &label_count); |
| 734 | break; |
| 735 | case BT_FIELD_TYPE_ID_SIGNED_ENUMERATION: |
| 736 | ret = bt_field_signed_enumeration_get_mapping_labels(field, |
| 737 | &label_array, &label_count); |
| 738 | break; |
| 739 | default: |
| 740 | abort(); |
| 741 | } |
| 742 | |
| 743 | if (ret) { |
| 744 | ret = BT_COMPONENT_STATUS_ERROR; |
| 745 | goto end; |
| 746 | } |
| 747 | |
| 748 | g_string_append(pretty->string, "( "); |
| 749 | if (label_count == 0) { |
| 750 | if (pretty->use_colors) { |
| 751 | g_string_append(pretty->string, COLOR_UNKNOWN); |
| 752 | } |
| 753 | g_string_append(pretty->string, "<unknown>"); |
| 754 | if (pretty->use_colors) { |
| 755 | g_string_append(pretty->string, COLOR_RST); |
| 756 | } |
| 757 | goto skip_loop; |
| 758 | } |
| 759 | for (i = 0; i < label_count; i++) { |
| 760 | const char *mapping_name = label_array[i]; |
| 761 | |
| 762 | if (i == 0) { |
| 763 | g_string_append(pretty->string, ", "); |
| 764 | } |
| 765 | if (pretty->use_colors) { |
| 766 | g_string_append(pretty->string, COLOR_ENUM_MAPPING_NAME); |
| 767 | } |
| 768 | print_escape_string(pretty, mapping_name); |
| 769 | if (pretty->use_colors) { |
| 770 | g_string_append(pretty->string, COLOR_RST); |
| 771 | } |
| 772 | } |
| 773 | skip_loop: |
| 774 | g_string_append(pretty->string, " : container = "); |
| 775 | ret = print_integer(pretty, field); |
| 776 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 777 | goto end; |
| 778 | } |
| 779 | g_string_append(pretty->string, " )"); |
| 780 | end: |
| 781 | return ret; |
| 782 | } |
| 783 | |
| 784 | static |
| 785 | int filter_field_name(struct pretty_component *pretty, const char *field_name, |
| 786 | GQuark *filter_fields, int filter_array_len) |
| 787 | { |
| 788 | int i; |
| 789 | GQuark field_quark = g_quark_try_string(field_name); |
| 790 | |
| 791 | if (!field_quark || pretty->options.verbose) { |
| 792 | return 1; |
| 793 | } |
| 794 | |
| 795 | for (i = 0; i < filter_array_len; i++) { |
| 796 | if (field_quark == filter_fields[i]) { |
| 797 | return 0; |
| 798 | } |
| 799 | } |
| 800 | return 1; |
| 801 | } |
| 802 | |
| 803 | static |
| 804 | enum bt_component_status print_struct_field(struct pretty_component *pretty, |
| 805 | struct bt_field *_struct, |
| 806 | struct bt_field_type *struct_type, |
| 807 | uint64_t i, bool print_names, uint64_t *nr_printed_fields, |
| 808 | GQuark *filter_fields, int filter_array_len) |
| 809 | { |
| 810 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 811 | const char *field_name; |
| 812 | struct bt_field *field = NULL; |
| 813 | struct bt_field_type *field_type = NULL;; |
| 814 | |
| 815 | field = bt_field_structure_borrow_member_field_by_index(_struct, i); |
| 816 | if (!field) { |
| 817 | ret = BT_COMPONENT_STATUS_ERROR; |
| 818 | goto end; |
| 819 | } |
| 820 | |
| 821 | bt_field_type_structure_borrow_member_by_index(struct_type, i, |
| 822 | &field_name, &field_type); |
| 823 | |
| 824 | if (filter_fields && !filter_field_name(pretty, field_name, |
| 825 | filter_fields, filter_array_len)) { |
| 826 | ret = BT_COMPONENT_STATUS_OK; |
| 827 | goto end; |
| 828 | } |
| 829 | |
| 830 | if (*nr_printed_fields > 0) { |
| 831 | g_string_append(pretty->string, ", "); |
| 832 | } else { |
| 833 | g_string_append(pretty->string, " "); |
| 834 | } |
| 835 | if (print_names) { |
| 836 | print_field_name_equal(pretty, field_name); |
| 837 | } |
| 838 | ret = print_field(pretty, field, print_names, NULL, 0); |
| 839 | *nr_printed_fields += 1; |
| 840 | |
| 841 | end: |
| 842 | return ret; |
| 843 | } |
| 844 | |
| 845 | static |
| 846 | enum bt_component_status print_struct(struct pretty_component *pretty, |
| 847 | struct bt_field *_struct, bool print_names, |
| 848 | GQuark *filter_fields, int filter_array_len) |
| 849 | { |
| 850 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 851 | struct bt_field_type *struct_type = NULL; |
| 852 | uint64_t nr_fields, i, nr_printed_fields; |
| 853 | |
| 854 | struct_type = bt_field_borrow_type(_struct); |
| 855 | if (!struct_type) { |
| 856 | ret = BT_COMPONENT_STATUS_ERROR; |
| 857 | goto end; |
| 858 | } |
| 859 | nr_fields = bt_field_type_structure_get_member_count(struct_type); |
| 860 | if (nr_fields < 0) { |
| 861 | ret = BT_COMPONENT_STATUS_ERROR; |
| 862 | goto end; |
| 863 | } |
| 864 | g_string_append(pretty->string, "{"); |
| 865 | pretty->depth++; |
| 866 | nr_printed_fields = 0; |
| 867 | for (i = 0; i < nr_fields; i++) { |
| 868 | ret = print_struct_field(pretty, _struct, struct_type, i, |
| 869 | print_names, &nr_printed_fields, filter_fields, |
| 870 | filter_array_len); |
| 871 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 872 | goto end; |
| 873 | } |
| 874 | } |
| 875 | pretty->depth--; |
| 876 | g_string_append(pretty->string, " }"); |
| 877 | |
| 878 | end: |
| 879 | return ret; |
| 880 | } |
| 881 | |
| 882 | static |
| 883 | enum bt_component_status print_array_field(struct pretty_component *pretty, |
| 884 | struct bt_field *array, uint64_t i, bool print_names) |
| 885 | { |
| 886 | struct bt_field *field = NULL; |
| 887 | |
| 888 | if (i != 0) { |
| 889 | g_string_append(pretty->string, ", "); |
| 890 | } else { |
| 891 | g_string_append(pretty->string, " "); |
| 892 | } |
| 893 | if (print_names) { |
| 894 | g_string_append_printf(pretty->string, "[%" PRIu64 "] = ", i); |
| 895 | } |
| 896 | |
| 897 | field = bt_field_array_borrow_element_field_by_index(array, i); |
| 898 | BT_ASSERT(field); |
| 899 | return print_field(pretty, field, print_names, NULL, 0); |
| 900 | } |
| 901 | |
| 902 | static |
| 903 | enum bt_component_status print_array(struct pretty_component *pretty, |
| 904 | struct bt_field *array, bool print_names) |
| 905 | { |
| 906 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 907 | struct bt_field_type *array_type = NULL; |
| 908 | uint64_t len; |
| 909 | uint64_t i; |
| 910 | |
| 911 | array_type = bt_field_borrow_type(array); |
| 912 | if (!array_type) { |
| 913 | ret = BT_COMPONENT_STATUS_ERROR; |
| 914 | goto end; |
| 915 | } |
| 916 | len = bt_field_array_get_length(array); |
| 917 | g_string_append(pretty->string, "["); |
| 918 | pretty->depth++; |
| 919 | for (i = 0; i < len; i++) { |
| 920 | ret = print_array_field(pretty, array, i, print_names); |
| 921 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 922 | goto end; |
| 923 | } |
| 924 | } |
| 925 | pretty->depth--; |
| 926 | g_string_append(pretty->string, " ]"); |
| 927 | |
| 928 | end: |
| 929 | return ret; |
| 930 | } |
| 931 | |
| 932 | static |
| 933 | enum bt_component_status print_sequence_field(struct pretty_component *pretty, |
| 934 | struct bt_field *seq, uint64_t i, bool print_names) |
| 935 | { |
| 936 | struct bt_field *field = NULL; |
| 937 | |
| 938 | if (i != 0) { |
| 939 | g_string_append(pretty->string, ", "); |
| 940 | } else { |
| 941 | g_string_append(pretty->string, " "); |
| 942 | } |
| 943 | if (print_names) { |
| 944 | g_string_append_printf(pretty->string, "[%" PRIu64 "] = ", i); |
| 945 | } |
| 946 | |
| 947 | field = bt_field_array_borrow_element_field_by_index(seq, i); |
| 948 | BT_ASSERT(field); |
| 949 | return print_field(pretty, field, print_names, NULL, 0); |
| 950 | } |
| 951 | |
| 952 | static |
| 953 | enum bt_component_status print_sequence(struct pretty_component *pretty, |
| 954 | struct bt_field *seq, bool print_names) |
| 955 | { |
| 956 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 957 | uint64_t len; |
| 958 | uint64_t i; |
| 959 | |
| 960 | len = bt_field_array_get_length(seq); |
| 961 | if (len < 0) { |
| 962 | ret = BT_COMPONENT_STATUS_ERROR; |
| 963 | goto end; |
| 964 | } |
| 965 | |
| 966 | g_string_append(pretty->string, "["); |
| 967 | |
| 968 | pretty->depth++; |
| 969 | for (i = 0; i < len; i++) { |
| 970 | ret = print_sequence_field(pretty, seq, i, print_names); |
| 971 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 972 | goto end; |
| 973 | } |
| 974 | } |
| 975 | pretty->depth--; |
| 976 | g_string_append(pretty->string, " ]"); |
| 977 | |
| 978 | end: |
| 979 | return ret; |
| 980 | } |
| 981 | |
| 982 | static |
| 983 | enum bt_component_status print_variant(struct pretty_component *pretty, |
| 984 | struct bt_field *variant, bool print_names) |
| 985 | { |
| 986 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 987 | struct bt_field *field = NULL; |
| 988 | |
| 989 | field = bt_field_variant_borrow_selected_option_field(variant); |
| 990 | BT_ASSERT(field); |
| 991 | g_string_append(pretty->string, "{ "); |
| 992 | pretty->depth++; |
| 993 | if (print_names) { |
| 994 | // TODO: find tag's name using field path |
| 995 | // print_field_name_equal(pretty, tag_choice); |
| 996 | } |
| 997 | ret = print_field(pretty, field, print_names, NULL, 0); |
| 998 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 999 | goto end; |
| 1000 | } |
| 1001 | pretty->depth--; |
| 1002 | g_string_append(pretty->string, " }"); |
| 1003 | |
| 1004 | end: |
| 1005 | return ret; |
| 1006 | } |
| 1007 | |
| 1008 | static |
| 1009 | enum bt_component_status print_field(struct pretty_component *pretty, |
| 1010 | struct bt_field *field, bool print_names, |
| 1011 | GQuark *filter_fields, int filter_array_len) |
| 1012 | { |
| 1013 | enum bt_field_type_id type_id; |
| 1014 | |
| 1015 | type_id = bt_field_get_type_id(field); |
| 1016 | switch (type_id) { |
| 1017 | case BT_FIELD_TYPE_ID_UNSIGNED_INTEGER: |
| 1018 | case BT_FIELD_TYPE_ID_SIGNED_INTEGER: |
| 1019 | return print_integer(pretty, field); |
| 1020 | case BT_FIELD_TYPE_ID_REAL: |
| 1021 | { |
| 1022 | double v; |
| 1023 | |
| 1024 | v = bt_field_real_get_value(field); |
| 1025 | if (pretty->use_colors) { |
| 1026 | g_string_append(pretty->string, COLOR_NUMBER_VALUE); |
| 1027 | } |
| 1028 | g_string_append_printf(pretty->string, "%g", v); |
| 1029 | if (pretty->use_colors) { |
| 1030 | g_string_append(pretty->string, COLOR_RST); |
| 1031 | } |
| 1032 | return BT_COMPONENT_STATUS_OK; |
| 1033 | } |
| 1034 | case BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION: |
| 1035 | case BT_FIELD_TYPE_ID_SIGNED_ENUMERATION: |
| 1036 | return print_enum(pretty, field); |
| 1037 | case BT_FIELD_TYPE_ID_STRING: |
| 1038 | { |
| 1039 | const char *str; |
| 1040 | |
| 1041 | str = bt_field_string_get_value(field); |
| 1042 | if (!str) { |
| 1043 | return BT_COMPONENT_STATUS_ERROR; |
| 1044 | } |
| 1045 | |
| 1046 | if (pretty->use_colors) { |
| 1047 | g_string_append(pretty->string, COLOR_STRING_VALUE); |
| 1048 | } |
| 1049 | print_escape_string(pretty, str); |
| 1050 | if (pretty->use_colors) { |
| 1051 | g_string_append(pretty->string, COLOR_RST); |
| 1052 | } |
| 1053 | return BT_COMPONENT_STATUS_OK; |
| 1054 | } |
| 1055 | case BT_FIELD_TYPE_ID_STRUCTURE: |
| 1056 | return print_struct(pretty, field, print_names, filter_fields, |
| 1057 | filter_array_len); |
| 1058 | case BT_FIELD_TYPE_ID_VARIANT: |
| 1059 | return print_variant(pretty, field, print_names); |
| 1060 | case BT_FIELD_TYPE_ID_STATIC_ARRAY: |
| 1061 | return print_array(pretty, field, print_names); |
| 1062 | case BT_FIELD_TYPE_ID_DYNAMIC_ARRAY: |
| 1063 | return print_sequence(pretty, field, print_names); |
| 1064 | default: |
| 1065 | // TODO: log instead |
| 1066 | fprintf(pretty->err, "[error] Unknown type id: %d\n", (int) type_id); |
| 1067 | return BT_COMPONENT_STATUS_ERROR; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | static |
| 1072 | enum bt_component_status print_stream_packet_context(struct pretty_component *pretty, |
| 1073 | struct bt_event *event) |
| 1074 | { |
| 1075 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 1076 | struct bt_packet *packet = NULL; |
| 1077 | struct bt_field *main_field = NULL; |
| 1078 | |
| 1079 | packet = bt_event_borrow_packet(event); |
| 1080 | if (!packet) { |
| 1081 | ret = BT_COMPONENT_STATUS_ERROR; |
| 1082 | goto end; |
| 1083 | } |
| 1084 | main_field = bt_packet_borrow_context_field(packet); |
| 1085 | if (!main_field) { |
| 1086 | goto end; |
| 1087 | } |
| 1088 | if (!pretty->start_line) { |
| 1089 | 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 | stream_packet_context_quarks, |
| 1098 | STREAM_PACKET_CONTEXT_QUARKS_LEN); |
| 1099 | |
| 1100 | end: |
| 1101 | return ret; |
| 1102 | } |
| 1103 | |
| 1104 | static |
| 1105 | enum bt_component_status print_event_header_raw(struct pretty_component *pretty, |
| 1106 | struct bt_event *event) |
| 1107 | { |
| 1108 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 1109 | struct bt_field *main_field = NULL; |
| 1110 | |
| 1111 | main_field = bt_event_borrow_header_field(event); |
| 1112 | if (!main_field) { |
| 1113 | goto end; |
| 1114 | } |
| 1115 | if (!pretty->start_line) { |
| 1116 | g_string_append(pretty->string, ", "); |
| 1117 | } |
| 1118 | pretty->start_line = false; |
| 1119 | if (pretty->options.print_scope_field_names) { |
| 1120 | print_name_equal(pretty, "stream.event.header"); |
| 1121 | } |
| 1122 | ret = print_field(pretty, main_field, |
| 1123 | pretty->options.print_header_field_names, NULL, 0); |
| 1124 | |
| 1125 | end: |
| 1126 | return ret; |
| 1127 | } |
| 1128 | |
| 1129 | static |
| 1130 | enum bt_component_status print_stream_event_context(struct pretty_component *pretty, |
| 1131 | struct bt_event *event) |
| 1132 | { |
| 1133 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 1134 | struct bt_field *main_field = NULL; |
| 1135 | |
| 1136 | main_field = bt_event_borrow_common_context_field(event); |
| 1137 | if (!main_field) { |
| 1138 | goto end; |
| 1139 | } |
| 1140 | if (!pretty->start_line) { |
| 1141 | g_string_append(pretty->string, ", "); |
| 1142 | } |
| 1143 | pretty->start_line = false; |
| 1144 | if (pretty->options.print_scope_field_names) { |
| 1145 | print_name_equal(pretty, "stream.event.context"); |
| 1146 | } |
| 1147 | ret = print_field(pretty, main_field, |
| 1148 | pretty->options.print_context_field_names, NULL, 0); |
| 1149 | |
| 1150 | end: |
| 1151 | return ret; |
| 1152 | } |
| 1153 | |
| 1154 | static |
| 1155 | enum bt_component_status print_event_context(struct pretty_component *pretty, |
| 1156 | struct bt_event *event) |
| 1157 | { |
| 1158 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 1159 | struct bt_field *main_field = NULL; |
| 1160 | |
| 1161 | main_field = bt_event_borrow_specific_context_field(event); |
| 1162 | if (!main_field) { |
| 1163 | goto end; |
| 1164 | } |
| 1165 | if (!pretty->start_line) { |
| 1166 | g_string_append(pretty->string, ", "); |
| 1167 | } |
| 1168 | pretty->start_line = false; |
| 1169 | if (pretty->options.print_scope_field_names) { |
| 1170 | print_name_equal(pretty, "event.context"); |
| 1171 | } |
| 1172 | ret = print_field(pretty, main_field, |
| 1173 | pretty->options.print_context_field_names, NULL, 0); |
| 1174 | |
| 1175 | end: |
| 1176 | return ret; |
| 1177 | } |
| 1178 | |
| 1179 | static |
| 1180 | enum bt_component_status print_event_payload(struct pretty_component *pretty, |
| 1181 | struct bt_event *event) |
| 1182 | { |
| 1183 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 1184 | struct bt_field *main_field = NULL; |
| 1185 | |
| 1186 | main_field = bt_event_borrow_payload_field(event); |
| 1187 | if (!main_field) { |
| 1188 | goto end; |
| 1189 | } |
| 1190 | if (!pretty->start_line) { |
| 1191 | g_string_append(pretty->string, ", "); |
| 1192 | } |
| 1193 | pretty->start_line = false; |
| 1194 | if (pretty->options.print_scope_field_names) { |
| 1195 | print_name_equal(pretty, "event.fields"); |
| 1196 | } |
| 1197 | ret = print_field(pretty, main_field, |
| 1198 | pretty->options.print_payload_field_names, NULL, 0); |
| 1199 | |
| 1200 | end: |
| 1201 | return ret; |
| 1202 | } |
| 1203 | |
| 1204 | static |
| 1205 | int flush_buf(FILE *stream, struct pretty_component *pretty) |
| 1206 | { |
| 1207 | int ret = 0; |
| 1208 | |
| 1209 | if (pretty->string->len == 0) { |
| 1210 | goto end; |
| 1211 | } |
| 1212 | |
| 1213 | if (fwrite(pretty->string->str, pretty->string->len, 1, stream) != 1) { |
| 1214 | ret = -1; |
| 1215 | } |
| 1216 | |
| 1217 | end: |
| 1218 | return ret; |
| 1219 | } |
| 1220 | |
| 1221 | BT_HIDDEN |
| 1222 | enum bt_component_status pretty_print_event(struct pretty_component *pretty, |
| 1223 | struct bt_notification *event_notif) |
| 1224 | { |
| 1225 | enum bt_component_status ret; |
| 1226 | struct bt_event *event = |
| 1227 | bt_notification_event_borrow_event(event_notif); |
| 1228 | |
| 1229 | BT_ASSERT(event); |
| 1230 | pretty->start_line = true; |
| 1231 | g_string_assign(pretty->string, ""); |
| 1232 | ret = print_event_header(pretty, event); |
| 1233 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 1234 | goto end; |
| 1235 | } |
| 1236 | |
| 1237 | ret = print_stream_packet_context(pretty, event); |
| 1238 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 1239 | goto end; |
| 1240 | } |
| 1241 | |
| 1242 | if (pretty->options.verbose) { |
| 1243 | ret = print_event_header_raw(pretty, event); |
| 1244 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 1245 | goto end; |
| 1246 | } |
| 1247 | } |
| 1248 | |
| 1249 | ret = print_stream_event_context(pretty, event); |
| 1250 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 1251 | goto end; |
| 1252 | } |
| 1253 | |
| 1254 | ret = print_event_context(pretty, event); |
| 1255 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 1256 | goto end; |
| 1257 | } |
| 1258 | |
| 1259 | ret = print_event_payload(pretty, event); |
| 1260 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 1261 | goto end; |
| 1262 | } |
| 1263 | |
| 1264 | g_string_append_c(pretty->string, '\n'); |
| 1265 | if (flush_buf(pretty->out, pretty)) { |
| 1266 | ret = BT_COMPONENT_STATUS_ERROR; |
| 1267 | goto end; |
| 1268 | } |
| 1269 | |
| 1270 | end: |
| 1271 | return ret; |
| 1272 | } |
| 1273 | |
| 1274 | static |
| 1275 | enum bt_component_status print_discarded_elements_msg( |
| 1276 | struct pretty_component *pretty, struct bt_packet *packet, |
| 1277 | uint64_t count, const char *elem_type) |
| 1278 | { |
| 1279 | #if 0 |
| 1280 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 1281 | struct bt_stream *stream = NULL; |
| 1282 | struct bt_stream_class *stream_class = NULL; |
| 1283 | struct bt_trace *trace = NULL; |
| 1284 | const char *stream_name; |
| 1285 | const char *trace_name; |
| 1286 | const unsigned char *trace_uuid; |
| 1287 | int64_t stream_class_id; |
| 1288 | int64_t stream_id; |
| 1289 | struct bt_clock_value *begin_clock_value = NULL; |
| 1290 | struct bt_clock_value *end_clock_value = NULL; |
| 1291 | |
| 1292 | /* Stream name */ |
| 1293 | BT_ASSERT(packet); |
| 1294 | stream = bt_packet_borrow_stream(packet); |
| 1295 | BT_ASSERT(stream); |
| 1296 | stream_name = bt_stream_get_name(stream); |
| 1297 | |
| 1298 | /* Stream class ID */ |
| 1299 | stream_class = bt_stream_borrow_class(stream); |
| 1300 | BT_ASSERT(stream_class); |
| 1301 | stream_class_id = bt_stream_class_get_id(stream_class); |
| 1302 | |
| 1303 | /* Stream ID */ |
| 1304 | stream_id = bt_stream_get_id(stream); |
| 1305 | |
| 1306 | /* Trace name */ |
| 1307 | trace = bt_stream_class_borrow_trace(stream_class); |
| 1308 | BT_ASSERT(trace); |
| 1309 | trace_name = bt_trace_get_name(trace); |
| 1310 | if (!trace_name) { |
| 1311 | trace_name = "(unknown)"; |
| 1312 | } |
| 1313 | |
| 1314 | /* Trace UUID */ |
| 1315 | trace_uuid = bt_trace_get_uuid(trace); |
| 1316 | |
| 1317 | /* Beginning and end times */ |
| 1318 | (void) bt_packet_borrow_previous_packet_default_end_clock_value( |
| 1319 | packet, &begin_clock_value); |
| 1320 | (void) bt_packet_borrow_default_end_clock_value(packet, |
| 1321 | &end_clock_value); |
| 1322 | |
| 1323 | /* Format message */ |
| 1324 | g_string_assign(pretty->string, ""); |
| 1325 | g_string_append_printf(pretty->string, |
| 1326 | "%s%sWARNING%s%s: Tracer discarded %" PRId64 " %s%s ", |
| 1327 | bt_common_color_fg_yellow(), |
| 1328 | bt_common_color_bold(), |
| 1329 | bt_common_color_reset(), |
| 1330 | bt_common_color_fg_yellow(), |
| 1331 | count, elem_type, count == 1 ? "" : "s"); |
| 1332 | |
| 1333 | if (begin_clock_value && end_clock_value) { |
| 1334 | g_string_append(pretty->string, "between ["); |
| 1335 | print_timestamp_wall(pretty, begin_clock_value); |
| 1336 | g_string_append(pretty->string, "] and ["); |
| 1337 | print_timestamp_wall(pretty, end_clock_value); |
| 1338 | g_string_append(pretty->string, "]"); |
| 1339 | } else { |
| 1340 | g_string_append(pretty->string, "(unknown time range)"); |
| 1341 | } |
| 1342 | |
| 1343 | g_string_append_printf(pretty->string, " in trace \"%s\" ", trace_name); |
| 1344 | |
| 1345 | if (trace_uuid) { |
| 1346 | g_string_append_printf(pretty->string, |
| 1347 | "(UUID: %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x) ", |
| 1348 | trace_uuid[0], |
| 1349 | trace_uuid[1], |
| 1350 | trace_uuid[2], |
| 1351 | trace_uuid[3], |
| 1352 | trace_uuid[4], |
| 1353 | trace_uuid[5], |
| 1354 | trace_uuid[6], |
| 1355 | trace_uuid[7], |
| 1356 | trace_uuid[8], |
| 1357 | trace_uuid[9], |
| 1358 | trace_uuid[10], |
| 1359 | trace_uuid[11], |
| 1360 | trace_uuid[12], |
| 1361 | trace_uuid[13], |
| 1362 | trace_uuid[14], |
| 1363 | trace_uuid[15]); |
| 1364 | } else { |
| 1365 | g_string_append(pretty->string, "(no UUID) "); |
| 1366 | } |
| 1367 | |
| 1368 | g_string_append_printf(pretty->string, |
| 1369 | "within stream \"%s\" (stream class ID: %" PRId64 ", ", |
| 1370 | stream_name, stream_class_id); |
| 1371 | |
| 1372 | if (stream_id >= 0) { |
| 1373 | g_string_append_printf(pretty->string, |
| 1374 | "stream ID: %" PRId64, stream_id); |
| 1375 | } else { |
| 1376 | g_string_append(pretty->string, "no stream ID"); |
| 1377 | } |
| 1378 | |
| 1379 | g_string_append_printf(pretty->string, ").%s\n", |
| 1380 | bt_common_color_reset()); |
| 1381 | |
| 1382 | /* |
| 1383 | * Print to standard error stream to remain backward compatible |
| 1384 | * with Babeltrace 1. |
| 1385 | */ |
| 1386 | if (flush_buf(stderr, pretty)) { |
| 1387 | ret = BT_COMPONENT_STATUS_ERROR; |
| 1388 | } |
| 1389 | |
| 1390 | return ret; |
| 1391 | #endif |
| 1392 | return 0; |
| 1393 | } |
| 1394 | |
| 1395 | BT_HIDDEN |
| 1396 | enum bt_component_status pretty_print_packet(struct pretty_component *pretty, |
| 1397 | struct bt_notification *packet_beginning_notif) |
| 1398 | { |
| 1399 | #if 0 |
| 1400 | struct bt_packet *packet = bt_notification_packet_begin_borrow_packet( |
| 1401 | packet_beginning_notif); |
| 1402 | uint64_t count; |
| 1403 | enum bt_component_status status = BT_COMPONENT_STATUS_OK; |
| 1404 | |
| 1405 | if (bt_packet_get_discarded_event_count(packet, &count) == |
| 1406 | BT_PACKET_PROPERTY_AVAILABILITY_AVAILABLE && |
| 1407 | count > 0) { |
| 1408 | status = print_discarded_elements_msg(pretty, packet, |
| 1409 | count, "event"); |
| 1410 | if (status != BT_COMPONENT_STATUS_OK) { |
| 1411 | goto end; |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | if (bt_packet_get_discarded_packet_count(packet, &count) == |
| 1416 | BT_PACKET_PROPERTY_AVAILABILITY_AVAILABLE && |
| 1417 | count > 0) { |
| 1418 | status = print_discarded_elements_msg(pretty, packet, |
| 1419 | count, "packet"); |
| 1420 | if (status != BT_COMPONENT_STATUS_OK) { |
| 1421 | goto end; |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | end: |
| 1426 | return status; |
| 1427 | #endif |
| 1428 | return 0; |
| 1429 | } |