| 1 | /* |
| 2 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <babeltrace2/babeltrace.h> |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | #include "common/assert.h" |
| 28 | #include "common/common.h" |
| 29 | #include "common/uuid.h" |
| 30 | #include "details.h" |
| 31 | #include "write.h" |
| 32 | #include "obj-lifetime-mgmt.h" |
| 33 | #include "colors.h" |
| 34 | |
| 35 | static inline |
| 36 | const char *plural(uint64_t value) |
| 37 | { |
| 38 | return value == 1 ? "" : "s"; |
| 39 | } |
| 40 | |
| 41 | static inline |
| 42 | void incr_indent_by(struct details_write_ctx *ctx, unsigned int value) |
| 43 | { |
| 44 | BT_ASSERT_DBG(ctx); |
| 45 | ctx->indent_level += value; |
| 46 | } |
| 47 | |
| 48 | static inline |
| 49 | void incr_indent(struct details_write_ctx *ctx) |
| 50 | { |
| 51 | incr_indent_by(ctx, 2); |
| 52 | } |
| 53 | |
| 54 | static inline |
| 55 | void decr_indent_by(struct details_write_ctx *ctx, unsigned int value) |
| 56 | { |
| 57 | BT_ASSERT_DBG(ctx); |
| 58 | BT_ASSERT_DBG(ctx->indent_level >= value); |
| 59 | ctx->indent_level -= value; |
| 60 | } |
| 61 | |
| 62 | static inline |
| 63 | void decr_indent(struct details_write_ctx *ctx) |
| 64 | { |
| 65 | decr_indent_by(ctx, 2); |
| 66 | } |
| 67 | |
| 68 | static inline |
| 69 | void format_uint(char *buf, uint64_t value, unsigned int base) |
| 70 | { |
| 71 | const char *spec = "%" PRIu64; |
| 72 | char *buf_start = buf; |
| 73 | unsigned int digits_per_group = 3; |
| 74 | char sep = ','; |
| 75 | bool sep_digits = true; |
| 76 | |
| 77 | switch (base) { |
| 78 | case 2: |
| 79 | case 16: |
| 80 | /* TODO: Support binary format */ |
| 81 | spec = "%" PRIx64; |
| 82 | strcpy(buf, "0x"); |
| 83 | buf_start = buf + 2; |
| 84 | digits_per_group = 4; |
| 85 | sep = ':'; |
| 86 | break; |
| 87 | case 8: |
| 88 | spec = "%" PRIo64; |
| 89 | strcpy(buf, "0"); |
| 90 | buf_start = buf + 1; |
| 91 | sep = ':'; |
| 92 | break; |
| 93 | case 10: |
| 94 | if (value <= 9999) { |
| 95 | /* |
| 96 | * Do not insert digit separators for numbers |
| 97 | * under 10,000 as it looks weird. |
| 98 | */ |
| 99 | sep_digits = false; |
| 100 | } |
| 101 | |
| 102 | break; |
| 103 | default: |
| 104 | abort(); |
| 105 | } |
| 106 | |
| 107 | sprintf(buf_start, spec, value); |
| 108 | |
| 109 | if (sep_digits) { |
| 110 | bt_common_sep_digits(buf_start, digits_per_group, sep); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | static inline |
| 115 | void format_int(char *buf, int64_t value, unsigned int base) |
| 116 | { |
| 117 | const char *spec = "%" PRIu64; |
| 118 | char *buf_start = buf; |
| 119 | unsigned int digits_per_group = 3; |
| 120 | char sep = ','; |
| 121 | bool sep_digits = true; |
| 122 | uint64_t abs_value = value < 0 ? (uint64_t) -value : (uint64_t) value; |
| 123 | |
| 124 | if (value < 0) { |
| 125 | buf[0] = '-'; |
| 126 | buf_start++; |
| 127 | } |
| 128 | |
| 129 | switch (base) { |
| 130 | case 2: |
| 131 | case 16: |
| 132 | /* TODO: Support binary format */ |
| 133 | spec = "%" PRIx64; |
| 134 | strcpy(buf_start, "0x"); |
| 135 | buf_start += 2; |
| 136 | digits_per_group = 4; |
| 137 | sep = ':'; |
| 138 | break; |
| 139 | case 8: |
| 140 | spec = "%" PRIo64; |
| 141 | strcpy(buf_start, "0"); |
| 142 | buf_start++; |
| 143 | sep = ':'; |
| 144 | break; |
| 145 | case 10: |
| 146 | if (value >= -9999 && value <= 9999) { |
| 147 | /* |
| 148 | * Do not insert digit separators for numbers |
| 149 | * over -10,000 and under 10,000 as it looks |
| 150 | * weird. |
| 151 | */ |
| 152 | sep_digits = false; |
| 153 | } |
| 154 | |
| 155 | break; |
| 156 | default: |
| 157 | abort(); |
| 158 | } |
| 159 | |
| 160 | sprintf(buf_start, spec, abs_value); |
| 161 | |
| 162 | if (sep_digits) { |
| 163 | bt_common_sep_digits(buf_start, digits_per_group, sep); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static inline |
| 168 | void write_nl(struct details_write_ctx *ctx) |
| 169 | { |
| 170 | BT_ASSERT_DBG(ctx); |
| 171 | g_string_append_c(ctx->str, '\n'); |
| 172 | } |
| 173 | |
| 174 | static inline |
| 175 | void write_sp(struct details_write_ctx *ctx) |
| 176 | { |
| 177 | BT_ASSERT_DBG(ctx); |
| 178 | g_string_append_c(ctx->str, ' '); |
| 179 | } |
| 180 | |
| 181 | static inline |
| 182 | void write_indent(struct details_write_ctx *ctx) |
| 183 | { |
| 184 | uint64_t i; |
| 185 | |
| 186 | BT_ASSERT_DBG(ctx); |
| 187 | |
| 188 | for (i = 0; i < ctx->indent_level; i++) { |
| 189 | write_sp(ctx); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | static inline |
| 194 | void write_compound_member_name(struct details_write_ctx *ctx, const char *name) |
| 195 | { |
| 196 | write_indent(ctx); |
| 197 | g_string_append_printf(ctx->str, "%s%s%s:", |
| 198 | color_fg_cyan(ctx), name, color_reset(ctx)); |
| 199 | } |
| 200 | |
| 201 | static inline |
| 202 | void write_array_index(struct details_write_ctx *ctx, uint64_t index, |
| 203 | const char *color) |
| 204 | { |
| 205 | char buf[32]; |
| 206 | |
| 207 | write_indent(ctx); |
| 208 | format_uint(buf, index, 10); |
| 209 | g_string_append_printf(ctx->str, "%s[%s]%s:", |
| 210 | color, buf, color_reset(ctx)); |
| 211 | } |
| 212 | |
| 213 | static inline |
| 214 | void write_obj_type_name(struct details_write_ctx *ctx, const char *name) |
| 215 | { |
| 216 | g_string_append_printf(ctx->str, "%s%s%s%s", |
| 217 | color_fg_yellow(ctx), color_bold(ctx), name, color_reset(ctx)); |
| 218 | } |
| 219 | |
| 220 | static inline |
| 221 | void write_prop_name(struct details_write_ctx *ctx, const char *prop_name) |
| 222 | { |
| 223 | g_string_append_printf(ctx->str, "%s%s%s", |
| 224 | color_fg_magenta(ctx), prop_name, color_reset(ctx)); |
| 225 | } |
| 226 | |
| 227 | static inline |
| 228 | void write_prop_name_line(struct details_write_ctx *ctx, const char *prop_name) |
| 229 | { |
| 230 | write_indent(ctx); |
| 231 | g_string_append_printf(ctx->str, "%s%s%s:", |
| 232 | color_fg_magenta(ctx), prop_name, color_reset(ctx)); |
| 233 | } |
| 234 | |
| 235 | static inline |
| 236 | void write_str_prop_value(struct details_write_ctx *ctx, const char *value) |
| 237 | { |
| 238 | g_string_append_printf(ctx->str, "%s%s%s", |
| 239 | color_bold(ctx), value, color_reset(ctx)); |
| 240 | } |
| 241 | |
| 242 | static inline |
| 243 | void write_none_prop_value(struct details_write_ctx *ctx, const char *value) |
| 244 | { |
| 245 | g_string_append_printf(ctx->str, "%s%s%s%s", |
| 246 | color_bold(ctx), color_fg_magenta(ctx), |
| 247 | value, color_reset(ctx)); |
| 248 | } |
| 249 | |
| 250 | static inline |
| 251 | void write_uint_str_prop_value(struct details_write_ctx *ctx, const char *value) |
| 252 | { |
| 253 | write_str_prop_value(ctx, value); |
| 254 | } |
| 255 | |
| 256 | static inline |
| 257 | void write_uint_prop_value(struct details_write_ctx *ctx, uint64_t value) |
| 258 | { |
| 259 | char buf[32]; |
| 260 | |
| 261 | format_uint(buf, value, 10); |
| 262 | write_uint_str_prop_value(ctx, buf); |
| 263 | } |
| 264 | |
| 265 | static inline |
| 266 | void write_int_prop_value(struct details_write_ctx *ctx, int64_t value) |
| 267 | { |
| 268 | char buf[32]; |
| 269 | |
| 270 | format_int(buf, value, 10); |
| 271 | write_uint_str_prop_value(ctx, buf); |
| 272 | } |
| 273 | |
| 274 | static inline |
| 275 | void write_float_prop_value(struct details_write_ctx *ctx, double value) |
| 276 | { |
| 277 | g_string_append_printf(ctx->str, "%s%f%s", |
| 278 | color_bold(ctx), value, color_reset(ctx)); |
| 279 | } |
| 280 | |
| 281 | static inline |
| 282 | void write_str_prop_line(struct details_write_ctx *ctx, const char *prop_name, |
| 283 | const char *prop_value) |
| 284 | { |
| 285 | BT_ASSERT_DBG(prop_value); |
| 286 | write_indent(ctx); |
| 287 | write_prop_name(ctx, prop_name); |
| 288 | g_string_append(ctx->str, ": "); |
| 289 | write_str_prop_value(ctx, prop_value); |
| 290 | write_nl(ctx); |
| 291 | } |
| 292 | |
| 293 | static inline |
| 294 | void write_uint_prop_line(struct details_write_ctx *ctx, const char *prop_name, |
| 295 | uint64_t prop_value) |
| 296 | { |
| 297 | write_indent(ctx); |
| 298 | write_prop_name(ctx, prop_name); |
| 299 | g_string_append(ctx->str, ": "); |
| 300 | write_uint_prop_value(ctx, prop_value); |
| 301 | write_nl(ctx); |
| 302 | } |
| 303 | |
| 304 | static inline |
| 305 | void write_int_prop_line(struct details_write_ctx *ctx, const char *prop_name, |
| 306 | int64_t prop_value) |
| 307 | { |
| 308 | write_indent(ctx); |
| 309 | write_prop_name(ctx, prop_name); |
| 310 | g_string_append(ctx->str, ": "); |
| 311 | write_int_prop_value(ctx, prop_value); |
| 312 | write_nl(ctx); |
| 313 | } |
| 314 | |
| 315 | static inline |
| 316 | void write_int_str_prop_value(struct details_write_ctx *ctx, const char *value) |
| 317 | { |
| 318 | write_str_prop_value(ctx, value); |
| 319 | } |
| 320 | |
| 321 | static inline |
| 322 | void write_bool_prop_value(struct details_write_ctx *ctx, bt_bool prop_value) |
| 323 | { |
| 324 | const char *str; |
| 325 | |
| 326 | g_string_append(ctx->str, color_bold(ctx)); |
| 327 | |
| 328 | if (prop_value) { |
| 329 | g_string_append(ctx->str, color_fg_green(ctx)); |
| 330 | str = "Yes"; |
| 331 | } else { |
| 332 | g_string_append(ctx->str, color_fg_red(ctx)); |
| 333 | str = "No"; |
| 334 | } |
| 335 | |
| 336 | g_string_append_printf(ctx->str, "%s%s", str, color_reset(ctx)); |
| 337 | } |
| 338 | |
| 339 | static inline |
| 340 | void write_bool_prop_line(struct details_write_ctx *ctx, const char *prop_name, |
| 341 | bt_bool prop_value) |
| 342 | { |
| 343 | write_indent(ctx); |
| 344 | write_prop_name(ctx, prop_name); |
| 345 | g_string_append(ctx->str, ": "); |
| 346 | write_bool_prop_value(ctx, prop_value); |
| 347 | write_nl(ctx); |
| 348 | } |
| 349 | |
| 350 | static inline |
| 351 | void write_uuid_prop_line(struct details_write_ctx *ctx, const char *prop_name, |
| 352 | bt_uuid uuid) |
| 353 | { |
| 354 | BT_ASSERT_DBG(uuid); |
| 355 | write_indent(ctx); |
| 356 | write_prop_name(ctx, prop_name); |
| 357 | g_string_append_printf(ctx->str, |
| 358 | ": %s" BT_UUID_FMT "%s\n", |
| 359 | color_bold(ctx), |
| 360 | BT_UUID_FMT_VALUES(uuid), |
| 361 | color_reset(ctx)); |
| 362 | } |
| 363 | |
| 364 | static |
| 365 | gint compare_strings(const char **a, const char **b) |
| 366 | { |
| 367 | return strcmp(*a, *b); |
| 368 | } |
| 369 | |
| 370 | static |
| 371 | bt_bool map_value_foreach_add_key_to_array(const char *key, |
| 372 | const bt_value *object, void *data) |
| 373 | { |
| 374 | GPtrArray *keys = data; |
| 375 | |
| 376 | BT_ASSERT_DBG(keys); |
| 377 | g_ptr_array_add(keys, (void *) key); |
| 378 | return BT_TRUE; |
| 379 | } |
| 380 | |
| 381 | static |
| 382 | void write_value(struct details_write_ctx *ctx, const bt_value *value, |
| 383 | const char *name) |
| 384 | { |
| 385 | uint64_t i; |
| 386 | bt_value_type value_type = bt_value_get_type(value); |
| 387 | GPtrArray *keys = g_ptr_array_new(); |
| 388 | char buf[64]; |
| 389 | |
| 390 | BT_ASSERT_DBG(keys); |
| 391 | |
| 392 | /* Write field's name */ |
| 393 | if (name) { |
| 394 | write_prop_name_line(ctx, name); |
| 395 | } |
| 396 | |
| 397 | /* Write field's value */ |
| 398 | switch (value_type) { |
| 399 | case BT_VALUE_TYPE_NULL: |
| 400 | write_sp(ctx); |
| 401 | write_none_prop_value(ctx, "Null"); |
| 402 | break; |
| 403 | case BT_VALUE_TYPE_BOOL: |
| 404 | write_sp(ctx); |
| 405 | write_bool_prop_value(ctx, bt_value_bool_get(value)); |
| 406 | break; |
| 407 | case BT_VALUE_TYPE_UNSIGNED_INTEGER: |
| 408 | format_uint(buf, bt_value_integer_unsigned_get(value), 10); |
| 409 | write_sp(ctx); |
| 410 | write_uint_str_prop_value(ctx, buf); |
| 411 | break; |
| 412 | case BT_VALUE_TYPE_SIGNED_INTEGER: |
| 413 | format_int(buf, bt_value_integer_signed_get(value), 10); |
| 414 | write_sp(ctx); |
| 415 | write_int_str_prop_value(ctx, buf); |
| 416 | break; |
| 417 | case BT_VALUE_TYPE_REAL: |
| 418 | write_sp(ctx); |
| 419 | write_float_prop_value(ctx, bt_value_real_get(value)); |
| 420 | break; |
| 421 | case BT_VALUE_TYPE_STRING: |
| 422 | write_sp(ctx); |
| 423 | write_str_prop_value(ctx, bt_value_string_get(value)); |
| 424 | break; |
| 425 | case BT_VALUE_TYPE_ARRAY: |
| 426 | { |
| 427 | uint64_t length = bt_value_array_get_length(value); |
| 428 | |
| 429 | if (length == 0) { |
| 430 | write_sp(ctx); |
| 431 | write_none_prop_value(ctx, "Empty"); |
| 432 | } else { |
| 433 | g_string_append(ctx->str, " Length "); |
| 434 | write_uint_prop_value(ctx, length); |
| 435 | g_string_append_c(ctx->str, ':'); |
| 436 | } |
| 437 | |
| 438 | incr_indent(ctx); |
| 439 | |
| 440 | for (i = 0; i < length; i++) { |
| 441 | const bt_value *elem_value = |
| 442 | bt_value_array_borrow_element_by_index_const( |
| 443 | value, i); |
| 444 | |
| 445 | write_nl(ctx); |
| 446 | write_array_index(ctx, i, color_fg_magenta(ctx)); |
| 447 | write_value(ctx, elem_value, NULL); |
| 448 | } |
| 449 | |
| 450 | decr_indent(ctx); |
| 451 | break; |
| 452 | } |
| 453 | case BT_VALUE_TYPE_MAP: |
| 454 | { |
| 455 | bt_value_map_foreach_entry_const_status foreach_status = |
| 456 | bt_value_map_foreach_entry_const(value, |
| 457 | map_value_foreach_add_key_to_array, keys); |
| 458 | |
| 459 | BT_ASSERT_DBG(foreach_status == |
| 460 | BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK); |
| 461 | g_ptr_array_sort(keys, (GCompareFunc) compare_strings); |
| 462 | |
| 463 | if (keys->len > 0) { |
| 464 | incr_indent(ctx); |
| 465 | |
| 466 | for (i = 0; i < keys->len; i++) { |
| 467 | const char *key = keys->pdata[i]; |
| 468 | const bt_value *entry_value = |
| 469 | bt_value_map_borrow_entry_value_const( |
| 470 | value, key); |
| 471 | |
| 472 | write_nl(ctx); |
| 473 | write_value(ctx, entry_value, key); |
| 474 | } |
| 475 | |
| 476 | decr_indent(ctx); |
| 477 | } else { |
| 478 | write_sp(ctx); |
| 479 | write_none_prop_value(ctx, "Empty"); |
| 480 | } |
| 481 | |
| 482 | break; |
| 483 | } |
| 484 | default: |
| 485 | abort(); |
| 486 | } |
| 487 | |
| 488 | g_ptr_array_free(keys, TRUE); |
| 489 | } |
| 490 | |
| 491 | static |
| 492 | void write_user_attributes(struct details_write_ctx *ctx, |
| 493 | const bt_value *user_attrs, bool write_newline, bool *written) |
| 494 | { |
| 495 | BT_ASSERT_DBG(user_attrs); |
| 496 | |
| 497 | if (!bt_value_map_is_empty(user_attrs)) { |
| 498 | write_value(ctx, user_attrs, "User attributes"); |
| 499 | |
| 500 | if (write_newline) { |
| 501 | write_nl(ctx); |
| 502 | } |
| 503 | |
| 504 | if (written) { |
| 505 | *written = true; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | static |
| 511 | void write_int_field_class_props(struct details_write_ctx *ctx, |
| 512 | const bt_field_class *fc, bool close) |
| 513 | { |
| 514 | g_string_append_printf(ctx->str, "(%s%" PRIu64 "-bit%s, Base ", |
| 515 | color_bold(ctx), |
| 516 | bt_field_class_integer_get_field_value_range(fc), |
| 517 | color_reset(ctx)); |
| 518 | |
| 519 | switch (bt_field_class_integer_get_preferred_display_base(fc)) { |
| 520 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY: |
| 521 | write_uint_prop_value(ctx, 2); |
| 522 | break; |
| 523 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL: |
| 524 | write_uint_prop_value(ctx, 8); |
| 525 | break; |
| 526 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL: |
| 527 | write_uint_prop_value(ctx, 10); |
| 528 | break; |
| 529 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL: |
| 530 | write_uint_prop_value(ctx, 16); |
| 531 | break; |
| 532 | default: |
| 533 | abort(); |
| 534 | } |
| 535 | |
| 536 | if (close) { |
| 537 | g_string_append(ctx->str, ")"); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | struct int_range { |
| 542 | union { |
| 543 | uint64_t u; |
| 544 | int64_t i; |
| 545 | } lower; |
| 546 | |
| 547 | union { |
| 548 | uint64_t u; |
| 549 | int64_t i; |
| 550 | } upper; |
| 551 | }; |
| 552 | |
| 553 | struct enum_field_class_mapping { |
| 554 | /* Weak */ |
| 555 | const char *label; |
| 556 | |
| 557 | /* Array of `struct int_range` */ |
| 558 | GArray *ranges; |
| 559 | }; |
| 560 | |
| 561 | static |
| 562 | gint compare_enum_field_class_mappings(struct enum_field_class_mapping **a, |
| 563 | struct enum_field_class_mapping **b) |
| 564 | { |
| 565 | return strcmp((*a)->label, (*b)->label); |
| 566 | } |
| 567 | |
| 568 | static |
| 569 | gint compare_int_ranges_signed(struct int_range *a, struct int_range *b) |
| 570 | { |
| 571 | |
| 572 | if (a->lower.i < b->lower.i) { |
| 573 | return -1; |
| 574 | } else if (a->lower.i > b->lower.i) { |
| 575 | return 1; |
| 576 | } else { |
| 577 | if (a->upper.i < b->upper.i) { |
| 578 | return -1; |
| 579 | } else if (a->upper.i > b->upper.i) { |
| 580 | return 1; |
| 581 | } else { |
| 582 | return 0; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | static |
| 588 | gint compare_int_ranges_unsigned(struct int_range *a, struct int_range *b) |
| 589 | { |
| 590 | if (a->lower.u < b->lower.u) { |
| 591 | return -1; |
| 592 | } else if (a->lower.u > b->lower.u) { |
| 593 | return 1; |
| 594 | } else { |
| 595 | if (a->upper.u < b->upper.u) { |
| 596 | return -1; |
| 597 | } else if (a->upper.u > b->upper.u) { |
| 598 | return 1; |
| 599 | } else { |
| 600 | return 0; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | static |
| 606 | GArray *range_set_to_int_ranges(const void *spec_range_set, bool is_signed) |
| 607 | { |
| 608 | uint64_t i; |
| 609 | const bt_integer_range_set *range_set; |
| 610 | GArray *ranges = g_array_new(FALSE, TRUE, sizeof(struct int_range)); |
| 611 | |
| 612 | if (!ranges) { |
| 613 | goto end; |
| 614 | } |
| 615 | |
| 616 | if (is_signed) { |
| 617 | range_set = bt_integer_range_set_signed_as_range_set_const( |
| 618 | spec_range_set); |
| 619 | } else { |
| 620 | range_set = bt_integer_range_set_unsigned_as_range_set_const( |
| 621 | spec_range_set); |
| 622 | } |
| 623 | |
| 624 | for (i = 0; i < bt_integer_range_set_get_range_count(range_set); i++) { |
| 625 | struct int_range range; |
| 626 | |
| 627 | if (is_signed) { |
| 628 | const bt_integer_range_signed *orig_range = |
| 629 | bt_integer_range_set_signed_borrow_range_by_index_const( |
| 630 | spec_range_set, i); |
| 631 | |
| 632 | range.lower.i = bt_integer_range_signed_get_lower(orig_range); |
| 633 | range.upper.i = bt_integer_range_signed_get_upper(orig_range); |
| 634 | } else { |
| 635 | const bt_integer_range_unsigned *orig_range = |
| 636 | bt_integer_range_set_unsigned_borrow_range_by_index_const( |
| 637 | spec_range_set, i); |
| 638 | |
| 639 | range.lower.u = bt_integer_range_unsigned_get_lower(orig_range); |
| 640 | range.upper.u = bt_integer_range_unsigned_get_upper(orig_range); |
| 641 | } |
| 642 | |
| 643 | g_array_append_val(ranges, range); |
| 644 | } |
| 645 | |
| 646 | if (is_signed) { |
| 647 | g_array_sort(ranges, (GCompareFunc) compare_int_ranges_signed); |
| 648 | } else { |
| 649 | g_array_sort(ranges, |
| 650 | (GCompareFunc) compare_int_ranges_unsigned); |
| 651 | } |
| 652 | |
| 653 | end: |
| 654 | return ranges; |
| 655 | } |
| 656 | |
| 657 | static |
| 658 | void destroy_enum_field_class_mapping(struct enum_field_class_mapping *mapping) |
| 659 | { |
| 660 | if (mapping->ranges) { |
| 661 | g_array_free(mapping->ranges, TRUE); |
| 662 | mapping->ranges = NULL; |
| 663 | } |
| 664 | |
| 665 | g_free(mapping); |
| 666 | } |
| 667 | |
| 668 | static |
| 669 | struct int_range *int_range_at(GArray *ranges, uint64_t index) |
| 670 | { |
| 671 | return &g_array_index(ranges, struct int_range, index); |
| 672 | } |
| 673 | |
| 674 | static |
| 675 | void write_int_range(struct details_write_ctx *ctx, |
| 676 | struct int_range *range, bool is_signed) |
| 677 | { |
| 678 | g_string_append(ctx->str, "["); |
| 679 | |
| 680 | if (is_signed) { |
| 681 | write_int_prop_value(ctx, range->lower.i); |
| 682 | } else { |
| 683 | write_int_prop_value(ctx, range->lower.u); |
| 684 | } |
| 685 | |
| 686 | if (range->lower.u != range->upper.u) { |
| 687 | g_string_append(ctx->str, ", "); |
| 688 | |
| 689 | if (is_signed) { |
| 690 | write_int_prop_value(ctx, range->upper.i); |
| 691 | } else { |
| 692 | write_int_prop_value(ctx, range->upper.u); |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | g_string_append(ctx->str, "]"); |
| 697 | } |
| 698 | |
| 699 | static |
| 700 | void write_enum_field_class_mappings(struct details_write_ctx *ctx, |
| 701 | const bt_field_class *fc) |
| 702 | { |
| 703 | GPtrArray *mappings; |
| 704 | uint64_t i; |
| 705 | uint64_t range_i; |
| 706 | bool is_signed = bt_field_class_get_type(fc) == |
| 707 | BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION; |
| 708 | |
| 709 | mappings = g_ptr_array_new_with_free_func( |
| 710 | (GDestroyNotify) destroy_enum_field_class_mapping); |
| 711 | BT_ASSERT_DBG(mappings); |
| 712 | |
| 713 | /* |
| 714 | * Copy field class's mappings to our own arrays and structures |
| 715 | * to sort them. |
| 716 | */ |
| 717 | for (i = 0; i < bt_field_class_enumeration_get_mapping_count(fc); i++) { |
| 718 | const void *fc_mapping; |
| 719 | const void *fc_range_set; |
| 720 | struct enum_field_class_mapping *mapping = g_new0( |
| 721 | struct enum_field_class_mapping, 1); |
| 722 | |
| 723 | BT_ASSERT_DBG(mapping); |
| 724 | |
| 725 | if (is_signed) { |
| 726 | fc_mapping = bt_field_class_enumeration_signed_borrow_mapping_by_index_const( |
| 727 | fc, i); |
| 728 | fc_range_set = bt_field_class_enumeration_signed_mapping_borrow_ranges_const( |
| 729 | fc_mapping); |
| 730 | } else { |
| 731 | fc_mapping = bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const( |
| 732 | fc, i); |
| 733 | fc_range_set = bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const( |
| 734 | fc_mapping); |
| 735 | } |
| 736 | |
| 737 | mapping->label = bt_field_class_enumeration_mapping_get_label( |
| 738 | bt_field_class_enumeration_signed_mapping_as_mapping_const( |
| 739 | fc_mapping)); |
| 740 | mapping->ranges = range_set_to_int_ranges(fc_range_set, |
| 741 | is_signed); |
| 742 | BT_ASSERT_DBG(mapping->ranges); |
| 743 | g_ptr_array_add(mappings, mapping); |
| 744 | } |
| 745 | |
| 746 | /* Sort mappings (ranges are already sorted within mappings) */ |
| 747 | g_ptr_array_sort(mappings, |
| 748 | (GCompareFunc) compare_enum_field_class_mappings); |
| 749 | |
| 750 | /* Write mappings */ |
| 751 | for (i = 0; i < mappings->len; i++) { |
| 752 | struct enum_field_class_mapping *mapping = mappings->pdata[i]; |
| 753 | |
| 754 | write_nl(ctx); |
| 755 | write_prop_name_line(ctx, mapping->label); |
| 756 | |
| 757 | for (range_i = 0; range_i < mapping->ranges->len; range_i++) { |
| 758 | write_sp(ctx); |
| 759 | write_int_range(ctx, |
| 760 | int_range_at(mapping->ranges, range_i), |
| 761 | is_signed); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | g_ptr_array_free(mappings, TRUE); |
| 766 | } |
| 767 | |
| 768 | static |
| 769 | void write_field_path(struct details_write_ctx *ctx, |
| 770 | const bt_field_path *field_path) |
| 771 | { |
| 772 | uint64_t i; |
| 773 | |
| 774 | g_string_append_c(ctx->str, '['); |
| 775 | |
| 776 | switch (bt_field_path_get_root_scope(field_path)) { |
| 777 | case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT: |
| 778 | write_str_prop_value(ctx, "Packet context"); |
| 779 | break; |
| 780 | case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT: |
| 781 | write_str_prop_value(ctx, "Event common context"); |
| 782 | break; |
| 783 | case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT: |
| 784 | write_str_prop_value(ctx, "Event specific context"); |
| 785 | break; |
| 786 | case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD: |
| 787 | write_str_prop_value(ctx, "Event payload"); |
| 788 | break; |
| 789 | default: |
| 790 | abort(); |
| 791 | } |
| 792 | |
| 793 | g_string_append(ctx->str, ": "); |
| 794 | |
| 795 | for (i = 0; i < bt_field_path_get_item_count(field_path); i++) { |
| 796 | const bt_field_path_item *fp_item = |
| 797 | bt_field_path_borrow_item_by_index_const(field_path, i); |
| 798 | |
| 799 | if (i != 0) { |
| 800 | g_string_append(ctx->str, ", "); |
| 801 | } |
| 802 | |
| 803 | switch (bt_field_path_item_get_type(fp_item)) { |
| 804 | case BT_FIELD_PATH_ITEM_TYPE_INDEX: |
| 805 | write_uint_prop_value(ctx, |
| 806 | bt_field_path_item_index_get_index(fp_item)); |
| 807 | break; |
| 808 | case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT: |
| 809 | write_str_prop_value(ctx, "<current>"); |
| 810 | break; |
| 811 | default: |
| 812 | abort(); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | g_string_append_c(ctx->str, ']'); |
| 817 | } |
| 818 | |
| 819 | static |
| 820 | void write_field_class(struct details_write_ctx *ctx, const bt_field_class *fc); |
| 821 | |
| 822 | static |
| 823 | void write_variant_field_class_option(struct details_write_ctx *ctx, |
| 824 | const bt_field_class *fc, uint64_t index) |
| 825 | { |
| 826 | bt_field_class_type fc_type = bt_field_class_get_type(fc); |
| 827 | const bt_field_class_variant_option *option = |
| 828 | bt_field_class_variant_borrow_option_by_index_const( |
| 829 | fc, index); |
| 830 | const void *orig_ranges = NULL; |
| 831 | GArray *int_ranges = NULL; |
| 832 | bool is_signed; |
| 833 | const bt_value *user_attrs = |
| 834 | bt_field_class_variant_option_borrow_user_attributes_const( |
| 835 | option); |
| 836 | const bt_field_class *option_fc = |
| 837 | bt_field_class_variant_option_borrow_field_class_const(option); |
| 838 | |
| 839 | write_nl(ctx); |
| 840 | write_compound_member_name(ctx, |
| 841 | bt_field_class_variant_option_get_name(option)); |
| 842 | |
| 843 | if (fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD) { |
| 844 | const bt_field_class_variant_with_selector_field_integer_unsigned_option *spec_opt = |
| 845 | bt_field_class_variant_with_selector_field_integer_unsigned_borrow_option_by_index_const( |
| 846 | fc, index); |
| 847 | |
| 848 | orig_ranges = |
| 849 | bt_field_class_variant_with_selector_field_integer_unsigned_option_borrow_ranges_const( |
| 850 | spec_opt); |
| 851 | is_signed = false; |
| 852 | } else if (fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD) { |
| 853 | const bt_field_class_variant_with_selector_field_integer_signed_option *spec_opt = |
| 854 | bt_field_class_variant_with_selector_field_integer_signed_borrow_option_by_index_const( |
| 855 | fc, index); |
| 856 | |
| 857 | orig_ranges = |
| 858 | bt_field_class_variant_with_selector_field_integer_signed_option_borrow_ranges_const( |
| 859 | spec_opt); |
| 860 | is_signed = true; |
| 861 | } |
| 862 | |
| 863 | if (orig_ranges) { |
| 864 | uint64_t i; |
| 865 | |
| 866 | int_ranges = range_set_to_int_ranges(orig_ranges, is_signed); |
| 867 | BT_ASSERT_DBG(int_ranges); |
| 868 | |
| 869 | for (i = 0; i < int_ranges->len; i++) { |
| 870 | struct int_range *range = int_range_at(int_ranges, i); |
| 871 | |
| 872 | write_sp(ctx); |
| 873 | write_int_range(ctx, range, is_signed); |
| 874 | } |
| 875 | |
| 876 | g_string_append(ctx->str, ": "); |
| 877 | } else { |
| 878 | write_sp(ctx); |
| 879 | } |
| 880 | |
| 881 | if (bt_value_map_is_empty(user_attrs)) { |
| 882 | write_field_class(ctx, option_fc); |
| 883 | } else { |
| 884 | write_nl(ctx); |
| 885 | incr_indent(ctx); |
| 886 | |
| 887 | /* Field class */ |
| 888 | write_prop_name_line(ctx, "Field class"); |
| 889 | write_sp(ctx); |
| 890 | write_field_class(ctx, option_fc); |
| 891 | write_nl(ctx); |
| 892 | |
| 893 | /* User attributes */ |
| 894 | write_user_attributes(ctx, user_attrs, |
| 895 | false, NULL); |
| 896 | |
| 897 | decr_indent(ctx); |
| 898 | } |
| 899 | |
| 900 | if (int_ranges) { |
| 901 | g_array_free(int_ranges, TRUE); |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | static |
| 906 | void write_field_class(struct details_write_ctx *ctx, const bt_field_class *fc) |
| 907 | { |
| 908 | uint64_t i; |
| 909 | const char *type; |
| 910 | bt_field_class_type fc_type = bt_field_class_get_type(fc); |
| 911 | const bt_value *user_attrs; |
| 912 | bool wrote_user_attrs = false; |
| 913 | |
| 914 | /* Write field class's type */ |
| 915 | switch (fc_type) { |
| 916 | case BT_FIELD_CLASS_TYPE_BOOL: |
| 917 | type = "Boolean"; |
| 918 | break; |
| 919 | case BT_FIELD_CLASS_TYPE_BIT_ARRAY: |
| 920 | type = "Bit array"; |
| 921 | break; |
| 922 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: |
| 923 | type = "Unsigned integer"; |
| 924 | break; |
| 925 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: |
| 926 | type = "Signed integer"; |
| 927 | break; |
| 928 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: |
| 929 | type = "Unsigned enumeration"; |
| 930 | break; |
| 931 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: |
| 932 | type = "Signed enumeration"; |
| 933 | break; |
| 934 | case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL: |
| 935 | type = "Single-precision real"; |
| 936 | break; |
| 937 | case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL: |
| 938 | type = "Double-precision real"; |
| 939 | break; |
| 940 | case BT_FIELD_CLASS_TYPE_STRING: |
| 941 | type = "String"; |
| 942 | break; |
| 943 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
| 944 | type = "Structure"; |
| 945 | break; |
| 946 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
| 947 | type = "Static array"; |
| 948 | break; |
| 949 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD: |
| 950 | type = "Dynamic array (no length field)"; |
| 951 | break; |
| 952 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD: |
| 953 | type = "Dynamic array (with length field)"; |
| 954 | break; |
| 955 | case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD: |
| 956 | type = "Option (no selector)"; |
| 957 | break; |
| 958 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD: |
| 959 | type = "Option (boolean selector)"; |
| 960 | break; |
| 961 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: |
| 962 | type = "Option (unsigned integer selector)"; |
| 963 | break; |
| 964 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD: |
| 965 | type = "Option (signed integer selector)"; |
| 966 | break; |
| 967 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD: |
| 968 | type = "Variant (no selector)"; |
| 969 | break; |
| 970 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: |
| 971 | type = "Variant (unsigned integer selector)"; |
| 972 | break; |
| 973 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD: |
| 974 | type = "Variant (signed integer selector)"; |
| 975 | break; |
| 976 | default: |
| 977 | abort(); |
| 978 | } |
| 979 | |
| 980 | g_string_append_printf(ctx->str, "%s%s%s", |
| 981 | color_fg_blue(ctx), type, color_reset(ctx)); |
| 982 | |
| 983 | /* Write field class's single-line properties */ |
| 984 | if (bt_field_class_type_is(fc_type, BT_FIELD_CLASS_TYPE_ENUMERATION)) { |
| 985 | uint64_t mapping_count = |
| 986 | bt_field_class_enumeration_get_mapping_count(fc); |
| 987 | |
| 988 | write_sp(ctx); |
| 989 | write_int_field_class_props(ctx, fc, false); |
| 990 | g_string_append(ctx->str, ", "); |
| 991 | write_uint_prop_value(ctx, mapping_count); |
| 992 | g_string_append_printf(ctx->str, " mapping%s)", |
| 993 | plural(mapping_count)); |
| 994 | } else if (bt_field_class_type_is(fc_type, |
| 995 | BT_FIELD_CLASS_TYPE_INTEGER)) { |
| 996 | write_sp(ctx); |
| 997 | write_int_field_class_props(ctx, fc, true); |
| 998 | } else if (fc_type == BT_FIELD_CLASS_TYPE_STRUCTURE) { |
| 999 | uint64_t member_count = |
| 1000 | bt_field_class_structure_get_member_count(fc); |
| 1001 | |
| 1002 | g_string_append(ctx->str, " ("); |
| 1003 | write_uint_prop_value(ctx, member_count); |
| 1004 | g_string_append_printf(ctx->str, " member%s)", |
| 1005 | plural(member_count)); |
| 1006 | } else if (fc_type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY) { |
| 1007 | g_string_append(ctx->str, " (Length "); |
| 1008 | write_uint_prop_value(ctx, |
| 1009 | bt_field_class_array_static_get_length(fc)); |
| 1010 | g_string_append_c(ctx->str, ')'); |
| 1011 | } else if (fc_type == BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD) { |
| 1012 | const bt_field_path *length_field_path = |
| 1013 | bt_field_class_array_dynamic_with_length_field_borrow_length_field_path_const( |
| 1014 | fc); |
| 1015 | |
| 1016 | g_string_append(ctx->str, " (Length field path "); |
| 1017 | write_field_path(ctx, length_field_path); |
| 1018 | g_string_append_c(ctx->str, ')'); |
| 1019 | } else if (bt_field_class_type_is(fc_type, |
| 1020 | BT_FIELD_CLASS_TYPE_OPTION_WITH_SELECTOR_FIELD)) { |
| 1021 | const bt_field_path *selector_field_path = |
| 1022 | bt_field_class_option_with_selector_field_borrow_selector_field_path_const( |
| 1023 | fc); |
| 1024 | |
| 1025 | g_string_append(ctx->str, " (Selector field path "); |
| 1026 | write_field_path(ctx, selector_field_path); |
| 1027 | g_string_append_c(ctx->str, ')'); |
| 1028 | } else if (bt_field_class_type_is(fc_type, |
| 1029 | BT_FIELD_CLASS_TYPE_VARIANT)) { |
| 1030 | uint64_t option_count = |
| 1031 | bt_field_class_variant_get_option_count(fc); |
| 1032 | const bt_field_path *sel_field_path = NULL; |
| 1033 | |
| 1034 | if (bt_field_class_type_is(fc_type, |
| 1035 | BT_FIELD_CLASS_TYPE_VARIANT_WITH_SELECTOR_FIELD)) { |
| 1036 | sel_field_path = |
| 1037 | bt_field_class_variant_with_selector_field_borrow_selector_field_path_const( |
| 1038 | fc); |
| 1039 | BT_ASSERT_DBG(sel_field_path); |
| 1040 | } |
| 1041 | |
| 1042 | g_string_append(ctx->str, " ("); |
| 1043 | write_uint_prop_value(ctx, option_count); |
| 1044 | g_string_append_printf(ctx->str, " option%s", |
| 1045 | plural(option_count)); |
| 1046 | |
| 1047 | if (sel_field_path) { |
| 1048 | g_string_append(ctx->str, ", Selector field path "); |
| 1049 | write_field_path(ctx, sel_field_path); |
| 1050 | } |
| 1051 | |
| 1052 | g_string_append_c(ctx->str, ')'); |
| 1053 | } |
| 1054 | |
| 1055 | incr_indent(ctx); |
| 1056 | user_attrs = bt_field_class_borrow_user_attributes_const(fc); |
| 1057 | if (!bt_value_map_is_empty(user_attrs)) { |
| 1058 | g_string_append(ctx->str, ":\n"); |
| 1059 | write_user_attributes(ctx, user_attrs, false, NULL); |
| 1060 | wrote_user_attrs = true; |
| 1061 | } |
| 1062 | |
| 1063 | /* Write field class's complex properties */ |
| 1064 | if (bt_field_class_type_is(fc_type, BT_FIELD_CLASS_TYPE_ENUMERATION)) { |
| 1065 | uint64_t mapping_count = |
| 1066 | bt_field_class_enumeration_get_mapping_count(fc); |
| 1067 | |
| 1068 | if (mapping_count > 0) { |
| 1069 | if (wrote_user_attrs) { |
| 1070 | write_nl(ctx); |
| 1071 | write_indent(ctx); |
| 1072 | write_prop_name(ctx, "Mappings"); |
| 1073 | g_string_append_c(ctx->str, ':'); |
| 1074 | incr_indent(ctx); |
| 1075 | } else { |
| 1076 | /* Each mapping starts with its own newline */ |
| 1077 | g_string_append_c(ctx->str, ':'); |
| 1078 | } |
| 1079 | |
| 1080 | write_enum_field_class_mappings(ctx, fc); |
| 1081 | |
| 1082 | if (wrote_user_attrs) { |
| 1083 | decr_indent(ctx); |
| 1084 | } |
| 1085 | } |
| 1086 | } else if (fc_type == BT_FIELD_CLASS_TYPE_STRUCTURE) { |
| 1087 | uint64_t member_count = |
| 1088 | bt_field_class_structure_get_member_count(fc); |
| 1089 | |
| 1090 | if (member_count > 0) { |
| 1091 | if (wrote_user_attrs) { |
| 1092 | write_nl(ctx); |
| 1093 | write_indent(ctx); |
| 1094 | write_prop_name(ctx, "Members"); |
| 1095 | g_string_append_c(ctx->str, ':'); |
| 1096 | incr_indent(ctx); |
| 1097 | } else { |
| 1098 | /* Each member starts with its own newline */ |
| 1099 | g_string_append_c(ctx->str, ':'); |
| 1100 | } |
| 1101 | |
| 1102 | for (i = 0; i < member_count; i++) { |
| 1103 | const bt_field_class_structure_member *member = |
| 1104 | bt_field_class_structure_borrow_member_by_index_const( |
| 1105 | fc, i); |
| 1106 | const bt_value *user_attrs; |
| 1107 | const bt_field_class *member_fc = |
| 1108 | bt_field_class_structure_member_borrow_field_class_const(member); |
| 1109 | |
| 1110 | write_nl(ctx); |
| 1111 | write_compound_member_name(ctx, |
| 1112 | bt_field_class_structure_member_get_name(member)); |
| 1113 | user_attrs = bt_field_class_structure_member_borrow_user_attributes_const( |
| 1114 | member); |
| 1115 | |
| 1116 | if (bt_value_map_is_empty(user_attrs)) { |
| 1117 | write_sp(ctx); |
| 1118 | write_field_class(ctx, member_fc); |
| 1119 | } else { |
| 1120 | write_nl(ctx); |
| 1121 | incr_indent(ctx); |
| 1122 | |
| 1123 | /* Field class */ |
| 1124 | write_prop_name_line(ctx, "Field class"); |
| 1125 | write_sp(ctx); |
| 1126 | write_field_class(ctx, member_fc); |
| 1127 | write_nl(ctx); |
| 1128 | |
| 1129 | /* User attributes */ |
| 1130 | write_user_attributes(ctx, user_attrs, |
| 1131 | false, NULL); |
| 1132 | |
| 1133 | decr_indent(ctx); |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | if (wrote_user_attrs) { |
| 1138 | decr_indent(ctx); |
| 1139 | } |
| 1140 | } |
| 1141 | } else if (bt_field_class_type_is(fc_type, BT_FIELD_CLASS_TYPE_ARRAY)) { |
| 1142 | if (wrote_user_attrs) { |
| 1143 | write_nl(ctx); |
| 1144 | } else { |
| 1145 | g_string_append(ctx->str, ":\n"); |
| 1146 | } |
| 1147 | |
| 1148 | write_prop_name_line(ctx, "Element"); |
| 1149 | write_sp(ctx); |
| 1150 | write_field_class(ctx, |
| 1151 | bt_field_class_array_borrow_element_field_class_const(fc)); |
| 1152 | } else if (bt_field_class_type_is(fc_type, |
| 1153 | BT_FIELD_CLASS_TYPE_OPTION)) { |
| 1154 | const void *ranges = NULL; |
| 1155 | bool selector_is_signed = false; |
| 1156 | |
| 1157 | if (wrote_user_attrs) { |
| 1158 | write_nl(ctx); |
| 1159 | } else { |
| 1160 | g_string_append(ctx->str, ":\n"); |
| 1161 | } |
| 1162 | |
| 1163 | if (fc_type == BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD) { |
| 1164 | write_bool_prop_line(ctx, "Selector is reversed", |
| 1165 | bt_field_class_option_with_selector_field_bool_selector_is_reversed(fc)); |
| 1166 | } else if (fc_type == BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD) { |
| 1167 | ranges = bt_field_class_option_with_selector_field_integer_unsigned_borrow_selector_ranges_const(fc); |
| 1168 | } else if (fc_type == BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD) { |
| 1169 | ranges = bt_field_class_option_with_selector_field_integer_signed_borrow_selector_ranges_const(fc); |
| 1170 | selector_is_signed = true; |
| 1171 | } |
| 1172 | |
| 1173 | if (ranges) { |
| 1174 | GArray *sorted_ranges = range_set_to_int_ranges( |
| 1175 | ranges, selector_is_signed); |
| 1176 | uint64_t i; |
| 1177 | |
| 1178 | BT_ASSERT_DBG(sorted_ranges); |
| 1179 | BT_ASSERT_DBG(sorted_ranges->len > 0); |
| 1180 | write_prop_name_line(ctx, "Selector ranges"); |
| 1181 | |
| 1182 | for (i = 0; i < sorted_ranges->len; i++) { |
| 1183 | write_sp(ctx); |
| 1184 | write_int_range(ctx, |
| 1185 | int_range_at(sorted_ranges, i), |
| 1186 | selector_is_signed); |
| 1187 | } |
| 1188 | |
| 1189 | write_nl(ctx); |
| 1190 | g_array_free(sorted_ranges, TRUE); |
| 1191 | } |
| 1192 | |
| 1193 | write_prop_name_line(ctx, "Content"); |
| 1194 | write_sp(ctx); |
| 1195 | write_field_class(ctx, |
| 1196 | bt_field_class_option_borrow_field_class_const(fc)); |
| 1197 | } else if (bt_field_class_type_is(fc_type, |
| 1198 | BT_FIELD_CLASS_TYPE_VARIANT)) { |
| 1199 | uint64_t option_count = |
| 1200 | bt_field_class_variant_get_option_count(fc); |
| 1201 | |
| 1202 | if (option_count > 0) { |
| 1203 | if (wrote_user_attrs) { |
| 1204 | write_nl(ctx); |
| 1205 | write_indent(ctx); |
| 1206 | write_prop_name(ctx, "Options"); |
| 1207 | g_string_append_c(ctx->str, ':'); |
| 1208 | incr_indent(ctx); |
| 1209 | } else { |
| 1210 | /* Each option starts with its own newline */ |
| 1211 | g_string_append_c(ctx->str, ':'); |
| 1212 | } |
| 1213 | |
| 1214 | for (i = 0; i < option_count; i++) { |
| 1215 | write_variant_field_class_option(ctx, fc, i); |
| 1216 | } |
| 1217 | |
| 1218 | if (wrote_user_attrs) { |
| 1219 | decr_indent(ctx); |
| 1220 | } |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | decr_indent(ctx); |
| 1225 | } |
| 1226 | |
| 1227 | static |
| 1228 | void write_root_field_class(struct details_write_ctx *ctx, const char *name, |
| 1229 | const bt_field_class *fc) |
| 1230 | { |
| 1231 | BT_ASSERT_DBG(name); |
| 1232 | BT_ASSERT_DBG(fc); |
| 1233 | write_indent(ctx); |
| 1234 | write_prop_name(ctx, name); |
| 1235 | g_string_append(ctx->str, ": "); |
| 1236 | write_field_class(ctx, fc); |
| 1237 | write_nl(ctx); |
| 1238 | } |
| 1239 | |
| 1240 | static |
| 1241 | void write_event_class(struct details_write_ctx *ctx, const bt_event_class *ec) |
| 1242 | { |
| 1243 | const char *name = bt_event_class_get_name(ec); |
| 1244 | const char *emf_uri; |
| 1245 | const bt_field_class *fc; |
| 1246 | bt_event_class_log_level log_level; |
| 1247 | |
| 1248 | write_indent(ctx); |
| 1249 | write_obj_type_name(ctx, "Event class"); |
| 1250 | |
| 1251 | /* Write name and ID */ |
| 1252 | if (name) { |
| 1253 | g_string_append_printf(ctx->str, " `%s%s%s`", |
| 1254 | color_fg_green(ctx), name, color_reset(ctx)); |
| 1255 | } |
| 1256 | |
| 1257 | g_string_append(ctx->str, " (ID "); |
| 1258 | write_uint_prop_value(ctx, bt_event_class_get_id(ec)); |
| 1259 | g_string_append(ctx->str, "):\n"); |
| 1260 | |
| 1261 | /* Write properties */ |
| 1262 | incr_indent(ctx); |
| 1263 | |
| 1264 | /* Write user attributes */ |
| 1265 | write_user_attributes(ctx, |
| 1266 | bt_event_class_borrow_user_attributes_const(ec), true, NULL); |
| 1267 | |
| 1268 | /* Write log level */ |
| 1269 | if (bt_event_class_get_log_level(ec, &log_level) == |
| 1270 | BT_PROPERTY_AVAILABILITY_AVAILABLE) { |
| 1271 | const char *ll_str = NULL; |
| 1272 | |
| 1273 | switch (log_level) { |
| 1274 | case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY: |
| 1275 | ll_str = "Emergency"; |
| 1276 | break; |
| 1277 | case BT_EVENT_CLASS_LOG_LEVEL_ALERT: |
| 1278 | ll_str = "Alert"; |
| 1279 | break; |
| 1280 | case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL: |
| 1281 | ll_str = "Critical"; |
| 1282 | break; |
| 1283 | case BT_EVENT_CLASS_LOG_LEVEL_ERROR: |
| 1284 | ll_str = "Error"; |
| 1285 | break; |
| 1286 | case BT_EVENT_CLASS_LOG_LEVEL_WARNING: |
| 1287 | ll_str = "Warning"; |
| 1288 | break; |
| 1289 | case BT_EVENT_CLASS_LOG_LEVEL_NOTICE: |
| 1290 | ll_str = "Notice"; |
| 1291 | break; |
| 1292 | case BT_EVENT_CLASS_LOG_LEVEL_INFO: |
| 1293 | ll_str = "Info"; |
| 1294 | break; |
| 1295 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM: |
| 1296 | ll_str = "Debug (system)"; |
| 1297 | break; |
| 1298 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM: |
| 1299 | ll_str = "Debug (program)"; |
| 1300 | break; |
| 1301 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS: |
| 1302 | ll_str = "Debug (process)"; |
| 1303 | break; |
| 1304 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE: |
| 1305 | ll_str = "Debug (module)"; |
| 1306 | break; |
| 1307 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT: |
| 1308 | ll_str = "Debug (unit)"; |
| 1309 | break; |
| 1310 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION: |
| 1311 | ll_str = "Debug (function)"; |
| 1312 | break; |
| 1313 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE: |
| 1314 | ll_str = "Debug (line)"; |
| 1315 | break; |
| 1316 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG: |
| 1317 | ll_str = "Debug"; |
| 1318 | break; |
| 1319 | default: |
| 1320 | abort(); |
| 1321 | } |
| 1322 | |
| 1323 | write_str_prop_line(ctx, "Log level", ll_str); |
| 1324 | } |
| 1325 | |
| 1326 | /* Write EMF URI */ |
| 1327 | emf_uri = bt_event_class_get_emf_uri(ec); |
| 1328 | if (emf_uri) { |
| 1329 | write_str_prop_line(ctx, "EMF URI", emf_uri); |
| 1330 | } |
| 1331 | |
| 1332 | /* Write specific context field class */ |
| 1333 | fc = bt_event_class_borrow_specific_context_field_class_const(ec); |
| 1334 | if (fc) { |
| 1335 | write_root_field_class(ctx, "Specific context field class", fc); |
| 1336 | } |
| 1337 | |
| 1338 | /* Write payload field class */ |
| 1339 | fc = bt_event_class_borrow_payload_field_class_const(ec); |
| 1340 | if (fc) { |
| 1341 | write_root_field_class(ctx, "Payload field class", fc); |
| 1342 | } |
| 1343 | |
| 1344 | decr_indent(ctx); |
| 1345 | } |
| 1346 | |
| 1347 | static |
| 1348 | void write_clock_class_prop_lines(struct details_write_ctx *ctx, |
| 1349 | const bt_clock_class *cc) |
| 1350 | { |
| 1351 | int64_t offset_seconds; |
| 1352 | uint64_t offset_cycles; |
| 1353 | const char *str; |
| 1354 | |
| 1355 | str = bt_clock_class_get_name(cc); |
| 1356 | if (str) { |
| 1357 | write_str_prop_line(ctx, "Name", str); |
| 1358 | } |
| 1359 | |
| 1360 | write_user_attributes(ctx, |
| 1361 | bt_clock_class_borrow_user_attributes_const(cc), true, NULL); |
| 1362 | str = bt_clock_class_get_description(cc); |
| 1363 | if (str) { |
| 1364 | write_str_prop_line(ctx, "Description", str); |
| 1365 | } |
| 1366 | |
| 1367 | write_uint_prop_line(ctx, "Frequency (Hz)", |
| 1368 | bt_clock_class_get_frequency(cc)); |
| 1369 | write_uint_prop_line(ctx, "Precision (cycles)", |
| 1370 | bt_clock_class_get_precision(cc)); |
| 1371 | bt_clock_class_get_offset(cc, &offset_seconds, &offset_cycles); |
| 1372 | write_int_prop_line(ctx, "Offset (s)", offset_seconds); |
| 1373 | write_uint_prop_line(ctx, "Offset (cycles)", offset_cycles); |
| 1374 | write_bool_prop_line(ctx, "Origin is Unix epoch", |
| 1375 | bt_clock_class_origin_is_unix_epoch(cc)); |
| 1376 | |
| 1377 | if (ctx->details_comp->cfg.with_uuid) { |
| 1378 | bt_uuid uuid = bt_clock_class_get_uuid(cc); |
| 1379 | |
| 1380 | if (uuid) { |
| 1381 | write_uuid_prop_line(ctx, "UUID", uuid); |
| 1382 | } |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | static |
| 1387 | gint compare_event_classes(const bt_event_class **a, const bt_event_class **b) |
| 1388 | { |
| 1389 | uint64_t id_a = bt_event_class_get_id(*a); |
| 1390 | uint64_t id_b = bt_event_class_get_id(*b); |
| 1391 | |
| 1392 | if (id_a < id_b) { |
| 1393 | return -1; |
| 1394 | } else if (id_a > id_b) { |
| 1395 | return 1; |
| 1396 | } else { |
| 1397 | return 0; |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | static |
| 1402 | void write_stream_class(struct details_write_ctx *ctx, |
| 1403 | const bt_stream_class *sc) |
| 1404 | { |
| 1405 | const bt_field_class *fc; |
| 1406 | GPtrArray *event_classes = g_ptr_array_new(); |
| 1407 | uint64_t i; |
| 1408 | |
| 1409 | write_indent(ctx); |
| 1410 | write_obj_type_name(ctx, "Stream class"); |
| 1411 | |
| 1412 | /* Write name and ID */ |
| 1413 | if (ctx->details_comp->cfg.with_stream_class_name) { |
| 1414 | const char *name = bt_stream_class_get_name(sc); |
| 1415 | |
| 1416 | if (name) { |
| 1417 | g_string_append(ctx->str, " `"); |
| 1418 | write_str_prop_value(ctx, name); |
| 1419 | g_string_append(ctx->str, "`"); |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | g_string_append(ctx->str, " (ID "); |
| 1424 | write_uint_prop_value(ctx, bt_stream_class_get_id(sc)); |
| 1425 | g_string_append(ctx->str, "):\n"); |
| 1426 | |
| 1427 | /* Write properties */ |
| 1428 | incr_indent(ctx); |
| 1429 | |
| 1430 | /* Write user attributes */ |
| 1431 | write_user_attributes(ctx, |
| 1432 | bt_stream_class_borrow_user_attributes_const(sc), true, NULL); |
| 1433 | |
| 1434 | /* Write configuration */ |
| 1435 | write_bool_prop_line(ctx, |
| 1436 | "Supports packets", bt_stream_class_supports_packets(sc)); |
| 1437 | |
| 1438 | if (bt_stream_class_supports_packets(sc)) { |
| 1439 | write_bool_prop_line(ctx, |
| 1440 | "Packets have beginning default clock snapshot", |
| 1441 | bt_stream_class_packets_have_beginning_default_clock_snapshot(sc)); |
| 1442 | write_bool_prop_line(ctx, |
| 1443 | "Packets have end default clock snapshot", |
| 1444 | bt_stream_class_packets_have_end_default_clock_snapshot(sc)); |
| 1445 | } |
| 1446 | |
| 1447 | write_bool_prop_line(ctx, |
| 1448 | "Supports discarded events", |
| 1449 | bt_stream_class_supports_discarded_events(sc)); |
| 1450 | |
| 1451 | if (bt_stream_class_supports_discarded_events(sc)) { |
| 1452 | write_bool_prop_line(ctx, |
| 1453 | "Discarded events have default clock snapshots", |
| 1454 | bt_stream_class_discarded_events_have_default_clock_snapshots(sc)); |
| 1455 | } |
| 1456 | |
| 1457 | write_bool_prop_line(ctx, |
| 1458 | "Supports discarded packets", |
| 1459 | bt_stream_class_supports_discarded_packets(sc)); |
| 1460 | |
| 1461 | if (bt_stream_class_supports_discarded_packets(sc)) { |
| 1462 | write_bool_prop_line(ctx, |
| 1463 | "Discarded packets have default clock snapshots", |
| 1464 | bt_stream_class_discarded_packets_have_default_clock_snapshots(sc)); |
| 1465 | } |
| 1466 | |
| 1467 | /* Write default clock class */ |
| 1468 | if (bt_stream_class_borrow_default_clock_class_const(sc)) { |
| 1469 | write_indent(ctx); |
| 1470 | write_prop_name(ctx, "Default clock class"); |
| 1471 | g_string_append_c(ctx->str, ':'); |
| 1472 | write_nl(ctx); |
| 1473 | incr_indent(ctx); |
| 1474 | write_clock_class_prop_lines(ctx, |
| 1475 | bt_stream_class_borrow_default_clock_class_const(sc)); |
| 1476 | decr_indent(ctx); |
| 1477 | } |
| 1478 | |
| 1479 | fc = bt_stream_class_borrow_packet_context_field_class_const(sc); |
| 1480 | if (fc) { |
| 1481 | write_root_field_class(ctx, "Packet context field class", fc); |
| 1482 | } |
| 1483 | |
| 1484 | fc = bt_stream_class_borrow_event_common_context_field_class_const(sc); |
| 1485 | if (fc) { |
| 1486 | write_root_field_class(ctx, "Event common context field class", |
| 1487 | fc); |
| 1488 | } |
| 1489 | |
| 1490 | for (i = 0; i < bt_stream_class_get_event_class_count(sc); i++) { |
| 1491 | g_ptr_array_add(event_classes, |
| 1492 | (gpointer) bt_stream_class_borrow_event_class_by_index_const( |
| 1493 | sc, i)); |
| 1494 | } |
| 1495 | |
| 1496 | g_ptr_array_sort(event_classes, (GCompareFunc) compare_event_classes); |
| 1497 | |
| 1498 | for (i = 0; i < event_classes->len; i++) { |
| 1499 | write_event_class(ctx, event_classes->pdata[i]); |
| 1500 | } |
| 1501 | |
| 1502 | decr_indent(ctx); |
| 1503 | g_ptr_array_free(event_classes, TRUE); |
| 1504 | } |
| 1505 | |
| 1506 | static |
| 1507 | gint compare_stream_classes(const bt_stream_class **a, const bt_stream_class **b) |
| 1508 | { |
| 1509 | uint64_t id_a = bt_stream_class_get_id(*a); |
| 1510 | uint64_t id_b = bt_stream_class_get_id(*b); |
| 1511 | |
| 1512 | if (id_a < id_b) { |
| 1513 | return -1; |
| 1514 | } else if (id_a > id_b) { |
| 1515 | return 1; |
| 1516 | } else { |
| 1517 | return 0; |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | static |
| 1522 | void write_trace_class(struct details_write_ctx *ctx, const bt_trace_class *tc) |
| 1523 | { |
| 1524 | GPtrArray *stream_classes = g_ptr_array_new(); |
| 1525 | uint64_t i; |
| 1526 | bool printed_prop = false; |
| 1527 | |
| 1528 | write_indent(ctx); |
| 1529 | write_obj_type_name(ctx, "Trace class"); |
| 1530 | |
| 1531 | |
| 1532 | for (i = 0; i < bt_trace_class_get_stream_class_count(tc); i++) { |
| 1533 | g_ptr_array_add(stream_classes, |
| 1534 | (gpointer) bt_trace_class_borrow_stream_class_by_index_const( |
| 1535 | tc, i)); |
| 1536 | } |
| 1537 | |
| 1538 | g_ptr_array_sort(stream_classes, (GCompareFunc) compare_stream_classes); |
| 1539 | |
| 1540 | if (stream_classes->len > 0) { |
| 1541 | if (!printed_prop) { |
| 1542 | g_string_append(ctx->str, ":\n"); |
| 1543 | printed_prop = true; |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | incr_indent(ctx); |
| 1548 | |
| 1549 | /* Write user attributes */ |
| 1550 | write_user_attributes(ctx, |
| 1551 | bt_trace_class_borrow_user_attributes_const(tc), true, |
| 1552 | &printed_prop); |
| 1553 | |
| 1554 | /* Write stream classes */ |
| 1555 | for (i = 0; i < stream_classes->len; i++) { |
| 1556 | write_stream_class(ctx, stream_classes->pdata[i]); |
| 1557 | } |
| 1558 | |
| 1559 | if (!printed_prop) { |
| 1560 | write_nl(ctx); |
| 1561 | } |
| 1562 | |
| 1563 | decr_indent(ctx); |
| 1564 | g_ptr_array_free(stream_classes, TRUE); |
| 1565 | } |
| 1566 | |
| 1567 | static |
| 1568 | int try_write_meta(struct details_write_ctx *ctx, const bt_trace_class *tc, |
| 1569 | const bt_stream_class *sc, const bt_event_class *ec) |
| 1570 | { |
| 1571 | int ret = 0; |
| 1572 | |
| 1573 | BT_ASSERT_DBG(tc); |
| 1574 | |
| 1575 | if (details_need_to_write_trace_class(ctx, tc)) { |
| 1576 | uint64_t sc_i; |
| 1577 | |
| 1578 | if (ctx->details_comp->cfg.compact && |
| 1579 | ctx->details_comp->printed_something) { |
| 1580 | /* |
| 1581 | * There are no empty line between messages in |
| 1582 | * compact mode, so write one here to decouple |
| 1583 | * the trace class from the next message. |
| 1584 | */ |
| 1585 | write_nl(ctx); |
| 1586 | } |
| 1587 | |
| 1588 | /* |
| 1589 | * write_trace_class() also writes all its stream |
| 1590 | * classes their event classes, so we don't need to |
| 1591 | * rewrite `sc`. |
| 1592 | */ |
| 1593 | write_trace_class(ctx, tc); |
| 1594 | |
| 1595 | /* |
| 1596 | * Mark this trace class as written, as well as all |
| 1597 | * its stream classes and their event classes. |
| 1598 | */ |
| 1599 | ret = details_did_write_trace_class(ctx, tc); |
| 1600 | if (ret) { |
| 1601 | goto end; |
| 1602 | } |
| 1603 | |
| 1604 | for (sc_i = 0; sc_i < bt_trace_class_get_stream_class_count(tc); |
| 1605 | sc_i++) { |
| 1606 | uint64_t ec_i; |
| 1607 | const bt_stream_class *tc_sc = |
| 1608 | bt_trace_class_borrow_stream_class_by_index_const( |
| 1609 | tc, sc_i); |
| 1610 | |
| 1611 | details_did_write_meta_object(ctx, tc, tc_sc); |
| 1612 | |
| 1613 | for (ec_i = 0; ec_i < |
| 1614 | bt_stream_class_get_event_class_count(tc_sc); |
| 1615 | ec_i++) { |
| 1616 | details_did_write_meta_object(ctx, tc, |
| 1617 | bt_stream_class_borrow_event_class_by_index_const( |
| 1618 | tc_sc, ec_i)); |
| 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | goto end; |
| 1623 | } |
| 1624 | |
| 1625 | if (sc && details_need_to_write_meta_object(ctx, tc, sc)) { |
| 1626 | uint64_t ec_i; |
| 1627 | |
| 1628 | BT_ASSERT_DBG(tc); |
| 1629 | |
| 1630 | if (ctx->details_comp->cfg.compact && |
| 1631 | ctx->details_comp->printed_something) { |
| 1632 | /* |
| 1633 | * There are no empty line between messages in |
| 1634 | * compact mode, so write one here to decouple |
| 1635 | * the stream class from the next message. |
| 1636 | */ |
| 1637 | write_nl(ctx); |
| 1638 | } |
| 1639 | |
| 1640 | /* |
| 1641 | * write_stream_class() also writes all its event |
| 1642 | * classes, so we don't need to rewrite `ec`. |
| 1643 | */ |
| 1644 | write_stream_class(ctx, sc); |
| 1645 | |
| 1646 | /* |
| 1647 | * Mark this stream class as written, as well as all its |
| 1648 | * event classes. |
| 1649 | */ |
| 1650 | details_did_write_meta_object(ctx, tc, sc); |
| 1651 | |
| 1652 | for (ec_i = 0; ec_i < |
| 1653 | bt_stream_class_get_event_class_count(sc); |
| 1654 | ec_i++) { |
| 1655 | details_did_write_meta_object(ctx, tc, |
| 1656 | bt_stream_class_borrow_event_class_by_index_const( |
| 1657 | sc, ec_i)); |
| 1658 | } |
| 1659 | |
| 1660 | goto end; |
| 1661 | } |
| 1662 | |
| 1663 | if (ec && details_need_to_write_meta_object(ctx, tc, ec)) { |
| 1664 | BT_ASSERT_DBG(sc); |
| 1665 | |
| 1666 | if (ctx->details_comp->cfg.compact && |
| 1667 | ctx->details_comp->printed_something) { |
| 1668 | /* |
| 1669 | * There are no empty line between messages in |
| 1670 | * compact mode, so write one here to decouple |
| 1671 | * the event class from the next message. |
| 1672 | */ |
| 1673 | write_nl(ctx); |
| 1674 | } |
| 1675 | |
| 1676 | write_event_class(ctx, ec); |
| 1677 | details_did_write_meta_object(ctx, tc, ec); |
| 1678 | goto end; |
| 1679 | } |
| 1680 | |
| 1681 | end: |
| 1682 | return ret; |
| 1683 | } |
| 1684 | |
| 1685 | static |
| 1686 | void write_time_str(struct details_write_ctx *ctx, const char *str) |
| 1687 | { |
| 1688 | if (!ctx->details_comp->cfg.with_time) { |
| 1689 | goto end; |
| 1690 | } |
| 1691 | |
| 1692 | g_string_append_printf(ctx->str, "[%s%s%s%s]", |
| 1693 | color_bold(ctx), color_fg_blue(ctx), str, color_reset(ctx)); |
| 1694 | |
| 1695 | if (ctx->details_comp->cfg.compact) { |
| 1696 | write_sp(ctx); |
| 1697 | } else { |
| 1698 | write_nl(ctx); |
| 1699 | } |
| 1700 | |
| 1701 | end: |
| 1702 | return; |
| 1703 | } |
| 1704 | |
| 1705 | static |
| 1706 | void write_time(struct details_write_ctx *ctx, const bt_clock_snapshot *cs) |
| 1707 | { |
| 1708 | bt_clock_snapshot_get_ns_from_origin_status cs_status; |
| 1709 | int64_t ns_from_origin; |
| 1710 | char buf[32]; |
| 1711 | |
| 1712 | if (!ctx->details_comp->cfg.with_time) { |
| 1713 | goto end; |
| 1714 | } |
| 1715 | |
| 1716 | format_uint(buf, bt_clock_snapshot_get_value(cs), 10); |
| 1717 | g_string_append_printf(ctx->str, "[%s%s%s%s%s", |
| 1718 | color_bold(ctx), color_fg_blue(ctx), buf, |
| 1719 | color_reset(ctx), |
| 1720 | ctx->details_comp->cfg.compact ? "" : " cycles"); |
| 1721 | cs_status = bt_clock_snapshot_get_ns_from_origin(cs, &ns_from_origin); |
| 1722 | if (cs_status == BT_CLOCK_SNAPSHOT_GET_NS_FROM_ORIGIN_STATUS_OK) { |
| 1723 | format_int(buf, ns_from_origin, 10); |
| 1724 | g_string_append_printf(ctx->str, "%s %s%s%s%s%s", |
| 1725 | ctx->details_comp->cfg.compact ? "" : ",", |
| 1726 | color_bold(ctx), color_fg_blue(ctx), buf, |
| 1727 | color_reset(ctx), |
| 1728 | ctx->details_comp->cfg.compact ? "" : " ns from origin"); |
| 1729 | } |
| 1730 | |
| 1731 | g_string_append(ctx->str, "]"); |
| 1732 | |
| 1733 | if (ctx->details_comp->cfg.compact) { |
| 1734 | write_sp(ctx); |
| 1735 | } else { |
| 1736 | write_nl(ctx); |
| 1737 | } |
| 1738 | |
| 1739 | end: |
| 1740 | return; |
| 1741 | } |
| 1742 | |
| 1743 | static |
| 1744 | int write_message_follow_tag(struct details_write_ctx *ctx, |
| 1745 | const bt_stream *stream) |
| 1746 | { |
| 1747 | int ret; |
| 1748 | uint64_t unique_trace_id; |
| 1749 | const bt_stream_class *sc = bt_stream_borrow_class_const(stream); |
| 1750 | const bt_trace *trace = bt_stream_borrow_trace_const(stream); |
| 1751 | |
| 1752 | ret = details_trace_unique_id(ctx, trace, &unique_trace_id); |
| 1753 | if (ret) { |
| 1754 | goto end; |
| 1755 | } |
| 1756 | |
| 1757 | if (ctx->details_comp->cfg.compact) { |
| 1758 | g_string_append_printf(ctx->str, |
| 1759 | "%s{%s%" PRIu64 " %" PRIu64 " %" PRIu64 "%s%s}%s ", |
| 1760 | color_fg_cyan(ctx), color_bold(ctx), |
| 1761 | unique_trace_id, bt_stream_class_get_id(sc), |
| 1762 | bt_stream_get_id(stream), |
| 1763 | color_reset(ctx), color_fg_cyan(ctx), color_reset(ctx)); |
| 1764 | } else { |
| 1765 | g_string_append_printf(ctx->str, |
| 1766 | "%s{Trace %s%" PRIu64 "%s%s, Stream class ID %s%" PRIu64 "%s%s, Stream ID %s%" PRIu64 "%s%s}%s\n", |
| 1767 | color_fg_cyan(ctx), |
| 1768 | color_bold(ctx), unique_trace_id, |
| 1769 | color_reset(ctx), color_fg_cyan(ctx), |
| 1770 | color_bold(ctx), bt_stream_class_get_id(sc), |
| 1771 | color_reset(ctx), color_fg_cyan(ctx), |
| 1772 | color_bold(ctx), bt_stream_get_id(stream), |
| 1773 | color_reset(ctx), color_fg_cyan(ctx), |
| 1774 | color_reset(ctx)); |
| 1775 | } |
| 1776 | |
| 1777 | end: |
| 1778 | return ret; |
| 1779 | } |
| 1780 | |
| 1781 | static |
| 1782 | void write_field(struct details_write_ctx *ctx, const bt_field *field, |
| 1783 | const char *name) |
| 1784 | { |
| 1785 | uint64_t i; |
| 1786 | bt_field_class_type fc_type = bt_field_get_class_type(field); |
| 1787 | const bt_field_class *fc; |
| 1788 | char buf[64]; |
| 1789 | |
| 1790 | /* Write field's name */ |
| 1791 | if (name) { |
| 1792 | write_compound_member_name(ctx, name); |
| 1793 | } |
| 1794 | |
| 1795 | /* Write field's value */ |
| 1796 | if (fc_type == BT_FIELD_CLASS_TYPE_BOOL) { |
| 1797 | write_sp(ctx); |
| 1798 | write_bool_prop_value(ctx, bt_field_bool_get_value(field)); |
| 1799 | } else if (fc_type == BT_FIELD_CLASS_TYPE_BIT_ARRAY) { |
| 1800 | format_uint(buf, bt_field_bit_array_get_value_as_integer(field), |
| 1801 | 16); |
| 1802 | write_sp(ctx); |
| 1803 | write_uint_str_prop_value(ctx, buf); |
| 1804 | } else if (bt_field_class_type_is(fc_type, |
| 1805 | BT_FIELD_CLASS_TYPE_INTEGER)) { |
| 1806 | unsigned int fmt_base; |
| 1807 | bt_field_class_integer_preferred_display_base base; |
| 1808 | |
| 1809 | fc = bt_field_borrow_class_const(field); |
| 1810 | base = bt_field_class_integer_get_preferred_display_base(fc); |
| 1811 | |
| 1812 | switch (base) { |
| 1813 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL: |
| 1814 | fmt_base = 10; |
| 1815 | break; |
| 1816 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL: |
| 1817 | fmt_base = 8; |
| 1818 | break; |
| 1819 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY: |
| 1820 | fmt_base = 2; |
| 1821 | break; |
| 1822 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL: |
| 1823 | fmt_base = 16; |
| 1824 | break; |
| 1825 | default: |
| 1826 | abort(); |
| 1827 | } |
| 1828 | |
| 1829 | if (bt_field_class_type_is(fc_type, |
| 1830 | BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER)) { |
| 1831 | format_uint(buf, |
| 1832 | bt_field_integer_unsigned_get_value(field), |
| 1833 | fmt_base); |
| 1834 | write_sp(ctx); |
| 1835 | write_uint_str_prop_value(ctx, buf); |
| 1836 | } else { |
| 1837 | format_int(buf, |
| 1838 | bt_field_integer_signed_get_value(field), |
| 1839 | fmt_base); |
| 1840 | write_sp(ctx); |
| 1841 | write_int_str_prop_value(ctx, buf); |
| 1842 | } |
| 1843 | } else if (fc_type == BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL) { |
| 1844 | write_sp(ctx); |
| 1845 | write_float_prop_value(ctx, bt_field_real_single_precision_get_value(field)); |
| 1846 | } else if (fc_type == BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL) { |
| 1847 | write_sp(ctx); |
| 1848 | write_float_prop_value(ctx, bt_field_real_double_precision_get_value(field)); |
| 1849 | } else if (fc_type == BT_FIELD_CLASS_TYPE_STRING) { |
| 1850 | write_sp(ctx); |
| 1851 | write_str_prop_value(ctx, bt_field_string_get_value(field)); |
| 1852 | } else if (fc_type == BT_FIELD_CLASS_TYPE_STRUCTURE) { |
| 1853 | uint64_t member_count; |
| 1854 | |
| 1855 | fc = bt_field_borrow_class_const(field); |
| 1856 | member_count = bt_field_class_structure_get_member_count(fc); |
| 1857 | |
| 1858 | if (member_count > 0) { |
| 1859 | incr_indent(ctx); |
| 1860 | |
| 1861 | for (i = 0; i < member_count; i++) { |
| 1862 | const bt_field_class_structure_member *member = |
| 1863 | bt_field_class_structure_borrow_member_by_index_const( |
| 1864 | fc, i); |
| 1865 | const bt_field *member_field = |
| 1866 | bt_field_structure_borrow_member_field_by_index_const( |
| 1867 | field, i); |
| 1868 | |
| 1869 | write_nl(ctx); |
| 1870 | write_field(ctx, member_field, |
| 1871 | bt_field_class_structure_member_get_name(member)); |
| 1872 | } |
| 1873 | |
| 1874 | decr_indent(ctx); |
| 1875 | } else { |
| 1876 | write_sp(ctx); |
| 1877 | write_none_prop_value(ctx, "Empty"); |
| 1878 | } |
| 1879 | } else if (bt_field_class_type_is(fc_type, BT_FIELD_CLASS_TYPE_ARRAY)) { |
| 1880 | uint64_t length = bt_field_array_get_length(field); |
| 1881 | |
| 1882 | if (length == 0) { |
| 1883 | write_sp(ctx); |
| 1884 | write_none_prop_value(ctx, "Empty"); |
| 1885 | } else { |
| 1886 | g_string_append(ctx->str, " Length "); |
| 1887 | write_uint_prop_value(ctx, length); |
| 1888 | g_string_append_c(ctx->str, ':'); |
| 1889 | } |
| 1890 | |
| 1891 | incr_indent(ctx); |
| 1892 | |
| 1893 | for (i = 0; i < length; i++) { |
| 1894 | const bt_field *elem_field = |
| 1895 | bt_field_array_borrow_element_field_by_index_const( |
| 1896 | field, i); |
| 1897 | |
| 1898 | write_nl(ctx); |
| 1899 | write_array_index(ctx, i, color_fg_cyan(ctx)); |
| 1900 | write_field(ctx, elem_field, NULL); |
| 1901 | } |
| 1902 | |
| 1903 | decr_indent(ctx); |
| 1904 | } else if (bt_field_class_type_is(fc_type, |
| 1905 | BT_FIELD_CLASS_TYPE_OPTION)) { |
| 1906 | const bt_field *content_field = |
| 1907 | bt_field_option_borrow_field_const(field); |
| 1908 | |
| 1909 | if (!content_field) { |
| 1910 | write_sp(ctx); |
| 1911 | write_none_prop_value(ctx, "None"); |
| 1912 | } else { |
| 1913 | write_field(ctx, content_field, NULL); |
| 1914 | } |
| 1915 | } else if (bt_field_class_type_is(fc_type, |
| 1916 | BT_FIELD_CLASS_TYPE_VARIANT)) { |
| 1917 | write_field(ctx, |
| 1918 | bt_field_variant_borrow_selected_option_field_const( |
| 1919 | field), NULL); |
| 1920 | } else { |
| 1921 | abort(); |
| 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | static |
| 1926 | void write_root_field(struct details_write_ctx *ctx, const char *name, |
| 1927 | const bt_field *field) |
| 1928 | { |
| 1929 | BT_ASSERT_DBG(name); |
| 1930 | BT_ASSERT_DBG(field); |
| 1931 | write_indent(ctx); |
| 1932 | write_prop_name(ctx, name); |
| 1933 | g_string_append(ctx->str, ":"); |
| 1934 | write_field(ctx, field, NULL); |
| 1935 | write_nl(ctx); |
| 1936 | } |
| 1937 | |
| 1938 | static |
| 1939 | int write_event_message(struct details_write_ctx *ctx, |
| 1940 | const bt_message *msg) |
| 1941 | { |
| 1942 | int ret = 0; |
| 1943 | const bt_event *event = bt_message_event_borrow_event_const(msg); |
| 1944 | const bt_stream *stream = bt_event_borrow_stream_const(event); |
| 1945 | const bt_event_class *ec = bt_event_borrow_class_const(event); |
| 1946 | const bt_stream_class *sc = bt_event_class_borrow_stream_class_const(ec); |
| 1947 | const bt_trace_class *tc = bt_stream_class_borrow_trace_class_const(sc); |
| 1948 | const char *ec_name; |
| 1949 | const bt_field *field; |
| 1950 | |
| 1951 | ret = try_write_meta(ctx, tc, sc, ec); |
| 1952 | if (ret) { |
| 1953 | goto end; |
| 1954 | } |
| 1955 | |
| 1956 | if (!ctx->details_comp->cfg.with_data) { |
| 1957 | goto end; |
| 1958 | } |
| 1959 | |
| 1960 | if (ctx->str->len > 0) { |
| 1961 | /* |
| 1962 | * Output buffer contains metadata: separate blocks with |
| 1963 | * newline. |
| 1964 | */ |
| 1965 | write_nl(ctx); |
| 1966 | } |
| 1967 | |
| 1968 | /* Write time */ |
| 1969 | if (bt_stream_class_borrow_default_clock_class_const(sc)) { |
| 1970 | write_time(ctx, |
| 1971 | bt_message_event_borrow_default_clock_snapshot_const( |
| 1972 | msg)); |
| 1973 | } |
| 1974 | |
| 1975 | /* Write follow tag for message */ |
| 1976 | ret = write_message_follow_tag(ctx, stream); |
| 1977 | if (ret) { |
| 1978 | goto end; |
| 1979 | } |
| 1980 | |
| 1981 | /* Write object's basic properties */ |
| 1982 | write_obj_type_name(ctx, "Event"); |
| 1983 | ec_name = bt_event_class_get_name(ec); |
| 1984 | if (ec_name) { |
| 1985 | g_string_append_printf(ctx->str, " `%s%s%s`", |
| 1986 | color_fg_green(ctx), ec_name, color_reset(ctx)); |
| 1987 | } |
| 1988 | |
| 1989 | g_string_append(ctx->str, " ("); |
| 1990 | |
| 1991 | if (!ctx->details_comp->cfg.compact) { |
| 1992 | g_string_append(ctx->str, "Class ID "); |
| 1993 | } |
| 1994 | |
| 1995 | write_uint_prop_value(ctx, bt_event_class_get_id(ec)); |
| 1996 | g_string_append(ctx->str, ")"); |
| 1997 | |
| 1998 | if (ctx->details_comp->cfg.compact) { |
| 1999 | write_nl(ctx); |
| 2000 | goto end; |
| 2001 | } |
| 2002 | |
| 2003 | /* Write fields */ |
| 2004 | g_string_append(ctx->str, ":\n"); |
| 2005 | incr_indent(ctx); |
| 2006 | field = bt_event_borrow_common_context_field_const(event); |
| 2007 | if (field) { |
| 2008 | write_root_field(ctx, "Common context", field); |
| 2009 | } |
| 2010 | |
| 2011 | field = bt_event_borrow_specific_context_field_const(event); |
| 2012 | if (field) { |
| 2013 | write_root_field(ctx, "Specific context", field); |
| 2014 | } |
| 2015 | |
| 2016 | field = bt_event_borrow_payload_field_const(event); |
| 2017 | if (field) { |
| 2018 | write_root_field(ctx, "Payload", field); |
| 2019 | } |
| 2020 | |
| 2021 | decr_indent(ctx); |
| 2022 | |
| 2023 | end: |
| 2024 | return ret; |
| 2025 | } |
| 2026 | |
| 2027 | static |
| 2028 | gint compare_streams(const bt_stream **a, const bt_stream **b) |
| 2029 | { |
| 2030 | uint64_t id_a = bt_stream_get_id(*a); |
| 2031 | uint64_t id_b = bt_stream_get_id(*b); |
| 2032 | |
| 2033 | if (id_a < id_b) { |
| 2034 | return -1; |
| 2035 | } else if (id_a > id_b) { |
| 2036 | return 1; |
| 2037 | } else { |
| 2038 | const bt_stream_class *a_sc = bt_stream_borrow_class_const(*a); |
| 2039 | const bt_stream_class *b_sc = bt_stream_borrow_class_const(*b); |
| 2040 | uint64_t a_sc_id = bt_stream_class_get_id(a_sc); |
| 2041 | uint64_t b_sc_id = bt_stream_class_get_id(b_sc); |
| 2042 | |
| 2043 | if (a_sc_id < b_sc_id) { |
| 2044 | return -1; |
| 2045 | } else if (a_sc_id > b_sc_id) { |
| 2046 | return 1; |
| 2047 | } else { |
| 2048 | return 0; |
| 2049 | } |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | static |
| 2054 | void write_trace(struct details_write_ctx *ctx, const bt_trace *trace) |
| 2055 | { |
| 2056 | const char *name; |
| 2057 | GPtrArray *streams = g_ptr_array_new(); |
| 2058 | uint64_t i; |
| 2059 | bool printed_prop = false; |
| 2060 | GPtrArray *env_names = g_ptr_array_new(); |
| 2061 | uint64_t env_count; |
| 2062 | |
| 2063 | write_indent(ctx); |
| 2064 | write_obj_type_name(ctx, "Trace"); |
| 2065 | |
| 2066 | /* Write name */ |
| 2067 | if (ctx->details_comp->cfg.with_trace_name) { |
| 2068 | name = bt_trace_get_name(trace); |
| 2069 | if (name) { |
| 2070 | g_string_append(ctx->str, " `"); |
| 2071 | write_str_prop_value(ctx, name); |
| 2072 | g_string_append(ctx->str, "`"); |
| 2073 | } |
| 2074 | } |
| 2075 | |
| 2076 | /* Write properties */ |
| 2077 | incr_indent(ctx); |
| 2078 | |
| 2079 | /* Write UUID */ |
| 2080 | if (ctx->details_comp->cfg.with_uuid) { |
| 2081 | bt_uuid uuid = bt_trace_get_uuid(trace); |
| 2082 | |
| 2083 | if (uuid) { |
| 2084 | if (!printed_prop) { |
| 2085 | g_string_append(ctx->str, ":\n"); |
| 2086 | printed_prop = true; |
| 2087 | } |
| 2088 | |
| 2089 | write_uuid_prop_line(ctx, "UUID", uuid); |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | /* Write environment */ |
| 2094 | env_count = bt_trace_get_environment_entry_count(trace); |
| 2095 | if (env_count > 0) { |
| 2096 | if (!printed_prop) { |
| 2097 | g_string_append(ctx->str, ":\n"); |
| 2098 | printed_prop = true; |
| 2099 | } |
| 2100 | |
| 2101 | write_indent(ctx); |
| 2102 | write_prop_name(ctx, "Environment"); |
| 2103 | g_string_append(ctx->str, " ("); |
| 2104 | write_uint_prop_value(ctx, env_count); |
| 2105 | g_string_append_printf(ctx->str, " entr%s):", |
| 2106 | env_count == 1 ? "y" : "ies"); |
| 2107 | write_nl(ctx); |
| 2108 | incr_indent(ctx); |
| 2109 | |
| 2110 | for (i = 0; i < env_count; i++) { |
| 2111 | const char *name; |
| 2112 | const bt_value *value; |
| 2113 | |
| 2114 | bt_trace_borrow_environment_entry_by_index_const( |
| 2115 | trace, i, &name, &value); |
| 2116 | g_ptr_array_add(env_names, (gpointer) name); |
| 2117 | } |
| 2118 | |
| 2119 | g_ptr_array_sort(env_names, (GCompareFunc) compare_strings); |
| 2120 | |
| 2121 | for (i = 0; i < env_names->len; i++) { |
| 2122 | const char *name = env_names->pdata[i]; |
| 2123 | const bt_value *value = |
| 2124 | bt_trace_borrow_environment_entry_value_by_name_const( |
| 2125 | trace, name); |
| 2126 | |
| 2127 | BT_ASSERT_DBG(value); |
| 2128 | write_compound_member_name(ctx, name); |
| 2129 | write_sp(ctx); |
| 2130 | |
| 2131 | if (bt_value_get_type(value) == |
| 2132 | BT_VALUE_TYPE_SIGNED_INTEGER) { |
| 2133 | write_int_prop_value(ctx, |
| 2134 | bt_value_integer_signed_get(value)); |
| 2135 | } else if (bt_value_get_type(value) == |
| 2136 | BT_VALUE_TYPE_STRING) { |
| 2137 | write_str_prop_value(ctx, |
| 2138 | bt_value_string_get(value)); |
| 2139 | } else { |
| 2140 | abort(); |
| 2141 | } |
| 2142 | |
| 2143 | write_nl(ctx); |
| 2144 | } |
| 2145 | |
| 2146 | decr_indent(ctx); |
| 2147 | } |
| 2148 | |
| 2149 | for (i = 0; i < bt_trace_get_stream_count(trace); i++) { |
| 2150 | g_ptr_array_add(streams, |
| 2151 | (gpointer) bt_trace_borrow_stream_by_index_const( |
| 2152 | trace, i)); |
| 2153 | } |
| 2154 | |
| 2155 | g_ptr_array_sort(streams, (GCompareFunc) compare_streams); |
| 2156 | |
| 2157 | if (streams->len > 0 && !printed_prop) { |
| 2158 | g_string_append(ctx->str, ":\n"); |
| 2159 | printed_prop = true; |
| 2160 | } |
| 2161 | |
| 2162 | for (i = 0; i < streams->len; i++) { |
| 2163 | const bt_stream *stream = streams->pdata[i]; |
| 2164 | |
| 2165 | write_indent(ctx); |
| 2166 | write_obj_type_name(ctx, "Stream"); |
| 2167 | g_string_append(ctx->str, " (ID "); |
| 2168 | write_uint_prop_value(ctx, bt_stream_get_id(stream)); |
| 2169 | g_string_append(ctx->str, ", Class ID "); |
| 2170 | write_uint_prop_value(ctx, bt_stream_class_get_id( |
| 2171 | bt_stream_borrow_class_const(stream))); |
| 2172 | g_string_append(ctx->str, ")"); |
| 2173 | write_nl(ctx); |
| 2174 | } |
| 2175 | |
| 2176 | decr_indent(ctx); |
| 2177 | |
| 2178 | if (!printed_prop) { |
| 2179 | write_nl(ctx); |
| 2180 | } |
| 2181 | |
| 2182 | g_ptr_array_free(streams, TRUE); |
| 2183 | g_ptr_array_free(env_names, TRUE); |
| 2184 | } |
| 2185 | |
| 2186 | static |
| 2187 | int write_stream_beginning_message(struct details_write_ctx *ctx, |
| 2188 | const bt_message *msg) |
| 2189 | { |
| 2190 | int ret = 0; |
| 2191 | const bt_stream *stream = |
| 2192 | bt_message_stream_beginning_borrow_stream_const(msg); |
| 2193 | const bt_trace *trace = bt_stream_borrow_trace_const(stream); |
| 2194 | const bt_stream_class *sc = bt_stream_borrow_class_const(stream); |
| 2195 | const bt_clock_class *cc = bt_stream_class_borrow_default_clock_class_const(sc); |
| 2196 | const bt_trace_class *tc = bt_stream_class_borrow_trace_class_const(sc); |
| 2197 | const char *name; |
| 2198 | |
| 2199 | ret = try_write_meta(ctx, tc, sc, NULL); |
| 2200 | if (ret) { |
| 2201 | goto end; |
| 2202 | } |
| 2203 | |
| 2204 | if (!ctx->details_comp->cfg.with_data) { |
| 2205 | goto end; |
| 2206 | } |
| 2207 | |
| 2208 | if (ctx->str->len > 0) { |
| 2209 | /* |
| 2210 | * Output buffer contains metadata: separate blocks with |
| 2211 | * newline. |
| 2212 | */ |
| 2213 | write_nl(ctx); |
| 2214 | } |
| 2215 | |
| 2216 | /* Write time */ |
| 2217 | if (cc) { |
| 2218 | const bt_clock_snapshot *cs; |
| 2219 | bt_message_stream_clock_snapshot_state cs_state = |
| 2220 | bt_message_stream_beginning_borrow_default_clock_snapshot_const(msg, &cs); |
| 2221 | |
| 2222 | if (cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { |
| 2223 | write_time(ctx, cs); |
| 2224 | } else { |
| 2225 | write_time_str(ctx, "Unknown"); |
| 2226 | } |
| 2227 | } |
| 2228 | |
| 2229 | /* Write follow tag for message */ |
| 2230 | ret = write_message_follow_tag(ctx, stream); |
| 2231 | if (ret) { |
| 2232 | goto end; |
| 2233 | } |
| 2234 | |
| 2235 | /* Write stream properties */ |
| 2236 | write_obj_type_name(ctx, "Stream beginning"); |
| 2237 | |
| 2238 | if (ctx->details_comp->cfg.compact) { |
| 2239 | write_nl(ctx); |
| 2240 | goto end; |
| 2241 | } |
| 2242 | |
| 2243 | g_string_append(ctx->str, ":\n"); |
| 2244 | incr_indent(ctx); |
| 2245 | |
| 2246 | if (ctx->details_comp->cfg.with_stream_name) { |
| 2247 | name = bt_stream_get_name(stream); |
| 2248 | if (name) { |
| 2249 | write_str_prop_line(ctx, "Name", name); |
| 2250 | } |
| 2251 | } |
| 2252 | |
| 2253 | if (ctx->details_comp->cfg.with_stream_class_name) { |
| 2254 | name = bt_stream_class_get_name(sc); |
| 2255 | if (name) { |
| 2256 | write_str_prop_line(ctx, "Class name", name); |
| 2257 | } |
| 2258 | } |
| 2259 | |
| 2260 | write_trace(ctx, trace); |
| 2261 | decr_indent(ctx); |
| 2262 | |
| 2263 | end: |
| 2264 | return ret; |
| 2265 | } |
| 2266 | |
| 2267 | static |
| 2268 | int write_stream_end_message(struct details_write_ctx *ctx, |
| 2269 | const bt_message *msg) |
| 2270 | { |
| 2271 | int ret = 0; |
| 2272 | const bt_stream *stream = |
| 2273 | bt_message_stream_end_borrow_stream_const(msg); |
| 2274 | const bt_stream_class *sc = |
| 2275 | bt_stream_borrow_class_const(stream); |
| 2276 | const bt_clock_class *cc = |
| 2277 | bt_stream_class_borrow_default_clock_class_const(sc); |
| 2278 | |
| 2279 | if (!ctx->details_comp->cfg.with_data) { |
| 2280 | goto end; |
| 2281 | } |
| 2282 | |
| 2283 | /* Write time */ |
| 2284 | if (cc) { |
| 2285 | const bt_clock_snapshot *cs; |
| 2286 | bt_message_stream_clock_snapshot_state cs_state = |
| 2287 | bt_message_stream_end_borrow_default_clock_snapshot_const(msg, &cs); |
| 2288 | |
| 2289 | if (cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { |
| 2290 | write_time(ctx, cs); |
| 2291 | } else { |
| 2292 | write_time_str(ctx, "Unknown"); |
| 2293 | } |
| 2294 | } |
| 2295 | |
| 2296 | /* Write follow tag for message */ |
| 2297 | ret = write_message_follow_tag(ctx, stream); |
| 2298 | if (ret) { |
| 2299 | goto end; |
| 2300 | } |
| 2301 | |
| 2302 | /* Write stream properties */ |
| 2303 | write_obj_type_name(ctx, "Stream end\n"); |
| 2304 | |
| 2305 | end: |
| 2306 | return ret; |
| 2307 | } |
| 2308 | |
| 2309 | static |
| 2310 | int write_packet_beginning_message(struct details_write_ctx *ctx, |
| 2311 | const bt_message *msg) |
| 2312 | { |
| 2313 | int ret = 0; |
| 2314 | const bt_packet *packet = |
| 2315 | bt_message_packet_beginning_borrow_packet_const(msg); |
| 2316 | const bt_stream *stream = bt_packet_borrow_stream_const(packet); |
| 2317 | const bt_stream_class *sc = bt_stream_borrow_class_const(stream); |
| 2318 | const bt_field *field; |
| 2319 | |
| 2320 | if (!ctx->details_comp->cfg.with_data) { |
| 2321 | goto end; |
| 2322 | } |
| 2323 | |
| 2324 | /* Write time */ |
| 2325 | if (bt_stream_class_packets_have_beginning_default_clock_snapshot(sc)) { |
| 2326 | write_time(ctx, |
| 2327 | bt_message_packet_beginning_borrow_default_clock_snapshot_const( |
| 2328 | msg)); |
| 2329 | } |
| 2330 | |
| 2331 | /* Write follow tag for message */ |
| 2332 | ret = write_message_follow_tag(ctx, stream); |
| 2333 | if (ret) { |
| 2334 | goto end; |
| 2335 | } |
| 2336 | |
| 2337 | write_obj_type_name(ctx, "Packet beginning"); |
| 2338 | |
| 2339 | if (ctx->details_comp->cfg.compact) { |
| 2340 | write_nl(ctx); |
| 2341 | goto end; |
| 2342 | } |
| 2343 | |
| 2344 | /* Write field */ |
| 2345 | field = bt_packet_borrow_context_field_const(packet); |
| 2346 | if (field) { |
| 2347 | g_string_append(ctx->str, ":\n"); |
| 2348 | incr_indent(ctx); |
| 2349 | write_root_field(ctx, "Context", field); |
| 2350 | decr_indent(ctx); |
| 2351 | } else { |
| 2352 | write_nl(ctx); |
| 2353 | } |
| 2354 | |
| 2355 | end: |
| 2356 | return ret; |
| 2357 | } |
| 2358 | |
| 2359 | static |
| 2360 | int write_discarded_items_message(struct details_write_ctx *ctx, |
| 2361 | const char *name, const bt_stream *stream, |
| 2362 | const bt_clock_snapshot *beginning_cs, |
| 2363 | const bt_clock_snapshot *end_cs, uint64_t count) |
| 2364 | { |
| 2365 | int ret = 0; |
| 2366 | |
| 2367 | /* Write times */ |
| 2368 | if (beginning_cs) { |
| 2369 | write_time(ctx, beginning_cs); |
| 2370 | BT_ASSERT_DBG(end_cs); |
| 2371 | write_time(ctx, end_cs); |
| 2372 | } |
| 2373 | |
| 2374 | /* Write follow tag for message */ |
| 2375 | ret = write_message_follow_tag(ctx, stream); |
| 2376 | if (ret) { |
| 2377 | goto end; |
| 2378 | } |
| 2379 | |
| 2380 | write_obj_type_name(ctx, "Discarded "); |
| 2381 | write_obj_type_name(ctx, name); |
| 2382 | |
| 2383 | /* Write count */ |
| 2384 | if (count == UINT64_C(-1)) { |
| 2385 | write_nl(ctx); |
| 2386 | goto end; |
| 2387 | } |
| 2388 | |
| 2389 | g_string_append(ctx->str, " ("); |
| 2390 | write_uint_prop_value(ctx, count); |
| 2391 | g_string_append_printf(ctx->str, " %s)\n", name); |
| 2392 | |
| 2393 | end: |
| 2394 | return ret; |
| 2395 | } |
| 2396 | |
| 2397 | static |
| 2398 | int write_discarded_events_message(struct details_write_ctx *ctx, |
| 2399 | const bt_message *msg) |
| 2400 | { |
| 2401 | int ret = 0; |
| 2402 | const bt_stream *stream = bt_message_discarded_events_borrow_stream_const( |
| 2403 | msg); |
| 2404 | const bt_stream_class *sc = bt_stream_borrow_class_const(stream); |
| 2405 | const bt_clock_snapshot *beginning_cs = NULL; |
| 2406 | const bt_clock_snapshot *end_cs = NULL; |
| 2407 | uint64_t count; |
| 2408 | |
| 2409 | if (!ctx->details_comp->cfg.with_data) { |
| 2410 | goto end; |
| 2411 | } |
| 2412 | |
| 2413 | if (bt_stream_class_discarded_events_have_default_clock_snapshots(sc)) { |
| 2414 | beginning_cs = |
| 2415 | bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const( |
| 2416 | msg); |
| 2417 | end_cs = |
| 2418 | bt_message_discarded_events_borrow_end_default_clock_snapshot_const( |
| 2419 | msg); |
| 2420 | } |
| 2421 | |
| 2422 | if (bt_message_discarded_events_get_count(msg, &count) != |
| 2423 | BT_PROPERTY_AVAILABILITY_AVAILABLE) { |
| 2424 | count = UINT64_C(-1); |
| 2425 | } |
| 2426 | |
| 2427 | ret = write_discarded_items_message(ctx, "events", stream, |
| 2428 | beginning_cs, end_cs, count); |
| 2429 | |
| 2430 | end: |
| 2431 | return ret; |
| 2432 | } |
| 2433 | |
| 2434 | static |
| 2435 | int write_discarded_packets_message(struct details_write_ctx *ctx, |
| 2436 | const bt_message *msg) |
| 2437 | { |
| 2438 | int ret = 0; |
| 2439 | const bt_stream *stream = bt_message_discarded_packets_borrow_stream_const( |
| 2440 | msg); |
| 2441 | const bt_stream_class *sc = bt_stream_borrow_class_const(stream); |
| 2442 | const bt_clock_snapshot *beginning_cs = NULL; |
| 2443 | const bt_clock_snapshot *end_cs = NULL; |
| 2444 | uint64_t count; |
| 2445 | |
| 2446 | if (!ctx->details_comp->cfg.with_data) { |
| 2447 | goto end; |
| 2448 | } |
| 2449 | |
| 2450 | if (bt_stream_class_discarded_packets_have_default_clock_snapshots(sc)) { |
| 2451 | beginning_cs = |
| 2452 | bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const( |
| 2453 | msg); |
| 2454 | end_cs = |
| 2455 | bt_message_discarded_packets_borrow_end_default_clock_snapshot_const( |
| 2456 | msg); |
| 2457 | } |
| 2458 | |
| 2459 | if (bt_message_discarded_packets_get_count(msg, &count) != |
| 2460 | BT_PROPERTY_AVAILABILITY_AVAILABLE) { |
| 2461 | count = UINT64_C(-1); |
| 2462 | } |
| 2463 | |
| 2464 | ret = write_discarded_items_message(ctx, "packets", stream, |
| 2465 | beginning_cs, end_cs, count); |
| 2466 | |
| 2467 | end: |
| 2468 | return ret; |
| 2469 | } |
| 2470 | |
| 2471 | static |
| 2472 | int write_packet_end_message(struct details_write_ctx *ctx, |
| 2473 | const bt_message *msg) |
| 2474 | { |
| 2475 | int ret = 0; |
| 2476 | const bt_packet *packet = |
| 2477 | bt_message_packet_end_borrow_packet_const(msg); |
| 2478 | const bt_stream *stream = bt_packet_borrow_stream_const(packet); |
| 2479 | const bt_stream_class *sc = bt_stream_borrow_class_const(stream); |
| 2480 | |
| 2481 | if (!ctx->details_comp->cfg.with_data) { |
| 2482 | goto end; |
| 2483 | } |
| 2484 | |
| 2485 | /* Write time */ |
| 2486 | if (bt_stream_class_packets_have_end_default_clock_snapshot(sc)) { |
| 2487 | write_time(ctx, |
| 2488 | bt_message_packet_end_borrow_default_clock_snapshot_const( |
| 2489 | msg)); |
| 2490 | } |
| 2491 | |
| 2492 | /* Write follow tag for message */ |
| 2493 | ret = write_message_follow_tag(ctx, stream); |
| 2494 | if (ret) { |
| 2495 | goto end; |
| 2496 | } |
| 2497 | |
| 2498 | write_obj_type_name(ctx, "Packet end"); |
| 2499 | write_nl(ctx); |
| 2500 | |
| 2501 | end: |
| 2502 | return ret; |
| 2503 | } |
| 2504 | |
| 2505 | static |
| 2506 | int write_message_iterator_inactivity_message(struct details_write_ctx *ctx, |
| 2507 | const bt_message *msg) |
| 2508 | { |
| 2509 | int ret = 0; |
| 2510 | const bt_clock_snapshot *cs = |
| 2511 | bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const( |
| 2512 | msg); |
| 2513 | |
| 2514 | /* Write time */ |
| 2515 | write_time(ctx, cs); |
| 2516 | write_obj_type_name(ctx, "Message iterator inactivity"); |
| 2517 | |
| 2518 | if (ctx->details_comp->cfg.compact) { |
| 2519 | write_nl(ctx); |
| 2520 | goto end; |
| 2521 | } |
| 2522 | |
| 2523 | /* Write clock class properties */ |
| 2524 | g_string_append(ctx->str, ":\n"); |
| 2525 | incr_indent(ctx); |
| 2526 | write_indent(ctx); |
| 2527 | write_prop_name(ctx, "Clock class"); |
| 2528 | g_string_append_c(ctx->str, ':'); |
| 2529 | write_nl(ctx); |
| 2530 | incr_indent(ctx); |
| 2531 | write_clock_class_prop_lines(ctx, |
| 2532 | bt_clock_snapshot_borrow_clock_class_const(cs)); |
| 2533 | decr_indent(ctx); |
| 2534 | |
| 2535 | end: |
| 2536 | return ret; |
| 2537 | } |
| 2538 | |
| 2539 | BT_HIDDEN |
| 2540 | int details_write_message(struct details_comp *details_comp, |
| 2541 | const bt_message *msg) |
| 2542 | { |
| 2543 | int ret = 0; |
| 2544 | struct details_write_ctx ctx = { |
| 2545 | .details_comp = details_comp, |
| 2546 | .str = details_comp->str, |
| 2547 | .indent_level = 0, |
| 2548 | }; |
| 2549 | |
| 2550 | /* Reset output buffer */ |
| 2551 | g_string_assign(details_comp->str, ""); |
| 2552 | |
| 2553 | switch (bt_message_get_type(msg)) { |
| 2554 | case BT_MESSAGE_TYPE_EVENT: |
| 2555 | ret = write_event_message(&ctx, msg); |
| 2556 | break; |
| 2557 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
| 2558 | ret = write_message_iterator_inactivity_message(&ctx, msg); |
| 2559 | break; |
| 2560 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
| 2561 | ret = write_stream_beginning_message(&ctx, msg); |
| 2562 | break; |
| 2563 | case BT_MESSAGE_TYPE_STREAM_END: |
| 2564 | ret = write_stream_end_message(&ctx, msg); |
| 2565 | break; |
| 2566 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
| 2567 | ret = write_packet_beginning_message(&ctx, msg); |
| 2568 | break; |
| 2569 | case BT_MESSAGE_TYPE_PACKET_END: |
| 2570 | ret = write_packet_end_message(&ctx, msg); |
| 2571 | break; |
| 2572 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
| 2573 | ret = write_discarded_events_message(&ctx, msg); |
| 2574 | break; |
| 2575 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: |
| 2576 | ret = write_discarded_packets_message(&ctx, msg); |
| 2577 | break; |
| 2578 | default: |
| 2579 | abort(); |
| 2580 | } |
| 2581 | |
| 2582 | /* |
| 2583 | * If this component printed at least one character so far, and |
| 2584 | * we're not in compact mode, and there's something in the |
| 2585 | * output buffer for this message, then prepend a newline to the |
| 2586 | * output buffer to visually separate message blocks. |
| 2587 | */ |
| 2588 | if (details_comp->printed_something && !details_comp->cfg.compact && |
| 2589 | details_comp->str->len > 0) { |
| 2590 | /* TODO: Optimize this */ |
| 2591 | g_string_prepend_c(details_comp->str, '\n'); |
| 2592 | } |
| 2593 | |
| 2594 | return ret; |
| 2595 | } |