Commit | Line | Data |
---|---|---|
a0bcd369 PP |
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 | ||
a0bcd369 | 23 | #include <babeltrace2/babeltrace.h> |
b03487ab PP |
24 | #include <stdio.h> |
25 | #include <string.h> | |
a0bcd369 PP |
26 | |
27 | #include "common/assert.h" | |
28 | #include "common/common.h" | |
d126826c | 29 | #include "common/uuid.h" |
a0bcd369 PP |
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(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(ctx); | |
58 | BT_ASSERT(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(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(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(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 | |
7753db0d PP |
202 | void write_array_index(struct details_write_ctx *ctx, uint64_t index, |
203 | const char *color) | |
a0bcd369 PP |
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:", | |
7753db0d | 210 | color, buf, color_reset(ctx)); |
a0bcd369 PP |
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 | ||
7753db0d PP |
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 | ||
a0bcd369 PP |
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 | ||
1bca9d21 PP |
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 | ||
a0bcd369 PP |
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(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 | |
9edadc81 | 322 | void write_bool_prop_value(struct details_write_ctx *ctx, bt_bool prop_value) |
a0bcd369 PP |
323 | { |
324 | const char *str; | |
325 | ||
9edadc81 | 326 | g_string_append(ctx->str, color_bold(ctx)); |
a0bcd369 PP |
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 | ||
9edadc81 PP |
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); | |
a0bcd369 PP |
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(uuid); | |
355 | write_indent(ctx); | |
356 | write_prop_name(ctx, prop_name); | |
357 | g_string_append_printf(ctx->str, | |
d126826c | 358 | ": %s" BT_UUID_FMT "%s\n", |
a0bcd369 | 359 | color_bold(ctx), |
d126826c | 360 | BT_UUID_FMT_VALUES(uuid), |
a0bcd369 PP |
361 | color_reset(ctx)); |
362 | } | |
363 | ||
7753db0d PP |
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(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(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_size(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(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(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 | ||
a0bcd369 PP |
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 | ||
02b61fe0 | 541 | struct int_range { |
a0bcd369 PP |
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 | ||
02b61fe0 | 557 | /* Array of `struct int_range` */ |
a0bcd369 PP |
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 | |
02b61fe0 | 569 | gint compare_int_ranges_signed(struct int_range *a, struct int_range *b) |
a0bcd369 PP |
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 | |
02b61fe0 | 588 | gint compare_int_ranges_unsigned(struct int_range *a, struct int_range *b) |
a0bcd369 PP |
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 | ||
02b61fe0 PP |
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 | ||
a0bcd369 PP |
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 | |
02b61fe0 PP |
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) | |
a0bcd369 PP |
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 | ||
02b61fe0 PP |
686 | if (range->lower.u != range->upper.u) { |
687 | g_string_append(ctx->str, ", "); | |
a0bcd369 | 688 | |
02b61fe0 PP |
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 | } | |
a0bcd369 PP |
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(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; | |
02b61fe0 | 719 | const void *fc_range_set; |
a0bcd369 PP |
720 | struct enum_field_class_mapping *mapping = g_new0( |
721 | struct enum_field_class_mapping, 1); | |
722 | ||
723 | BT_ASSERT(mapping); | |
a0bcd369 PP |
724 | |
725 | if (is_signed) { | |
60bbfc7c | 726 | fc_mapping = bt_field_class_enumeration_signed_borrow_mapping_by_index_const( |
a0bcd369 | 727 | fc, i); |
60bbfc7c | 728 | fc_range_set = bt_field_class_enumeration_signed_mapping_borrow_ranges_const( |
02b61fe0 | 729 | fc_mapping); |
a0bcd369 | 730 | } else { |
60bbfc7c | 731 | fc_mapping = bt_field_class_enumeration_unsigned_borrow_mapping_by_index_const( |
a0bcd369 | 732 | fc, i); |
60bbfc7c | 733 | fc_range_set = bt_field_class_enumeration_unsigned_mapping_borrow_ranges_const( |
02b61fe0 | 734 | fc_mapping); |
a0bcd369 PP |
735 | } |
736 | ||
737 | mapping->label = bt_field_class_enumeration_mapping_get_label( | |
60bbfc7c | 738 | bt_field_class_enumeration_signed_mapping_as_mapping_const( |
a0bcd369 | 739 | fc_mapping)); |
02b61fe0 PP |
740 | mapping->ranges = range_set_to_int_ranges(fc_range_set, |
741 | is_signed); | |
742 | BT_ASSERT(mapping->ranges); | |
a0bcd369 PP |
743 | g_ptr_array_add(mappings, mapping); |
744 | } | |
745 | ||
02b61fe0 | 746 | /* Sort mappings (ranges are already sorted within mappings) */ |
a0bcd369 PP |
747 | g_ptr_array_sort(mappings, |
748 | (GCompareFunc) compare_enum_field_class_mappings); | |
749 | ||
a0bcd369 PP |
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); | |
7753db0d | 755 | write_prop_name_line(ctx, mapping->label); |
a0bcd369 | 756 | |
a0bcd369 | 757 | for (range_i = 0; range_i < mapping->ranges->len; range_i++) { |
02b61fe0 PP |
758 | write_sp(ctx); |
759 | write_int_range(ctx, | |
760 | int_range_at(mapping->ranges, range_i), | |
761 | is_signed); | |
a0bcd369 | 762 | } |
a0bcd369 PP |
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)) { | |
056995e5 | 777 | case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT: |
a0bcd369 PP |
778 | write_str_prop_value(ctx, "Packet context"); |
779 | break; | |
056995e5 | 780 | case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT: |
a0bcd369 PP |
781 | write_str_prop_value(ctx, "Event common context"); |
782 | break; | |
056995e5 | 783 | case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT: |
a0bcd369 PP |
784 | write_str_prop_value(ctx, "Event specific context"); |
785 | break; | |
056995e5 | 786 | case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD: |
a0bcd369 PP |
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 | |
02b61fe0 PP |
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) | |
a0bcd369 | 825 | { |
a0bcd369 | 826 | bt_field_class_type fc_type = bt_field_class_get_type(fc); |
02b61fe0 PP |
827 | const bt_field_class_variant_option *option = |
828 | bt_field_class_variant_borrow_option_by_index_const( | |
829 | fc, index); | |
7753db0d | 830 | const void *orig_ranges = NULL; |
02b61fe0 PP |
831 | GArray *int_ranges = NULL; |
832 | bool is_signed; | |
7753db0d PP |
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); | |
a0bcd369 | 838 | |
02b61fe0 PP |
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_SELECTOR) { | |
60bbfc7c PP |
844 | const bt_field_class_variant_with_selector_unsigned_option *spec_opt = |
845 | bt_field_class_variant_with_selector_unsigned_borrow_option_by_index_const( | |
02b61fe0 PP |
846 | fc, index); |
847 | ||
848 | orig_ranges = | |
60bbfc7c | 849 | bt_field_class_variant_with_selector_unsigned_option_borrow_ranges_const( |
02b61fe0 PP |
850 | spec_opt); |
851 | is_signed = false; | |
7753db0d | 852 | } else if (fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR) { |
60bbfc7c PP |
853 | const bt_field_class_variant_with_selector_signed_option *spec_opt = |
854 | bt_field_class_variant_with_selector_signed_borrow_option_by_index_const( | |
02b61fe0 PP |
855 | fc, index); |
856 | ||
857 | orig_ranges = | |
60bbfc7c | 858 | bt_field_class_variant_with_selector_signed_option_borrow_ranges_const( |
02b61fe0 PP |
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(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 { | |
a0bcd369 PP |
878 | write_sp(ctx); |
879 | } | |
880 | ||
7753db0d PP |
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); | |
02b61fe0 | 886 | |
7753db0d PP |
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); | |
02b61fe0 | 892 | |
7753db0d PP |
893 | /* User attributes */ |
894 | write_user_attributes(ctx, user_attrs, | |
895 | false, NULL); | |
02b61fe0 | 896 | |
7753db0d | 897 | decr_indent(ctx); |
02b61fe0 PP |
898 | } |
899 | ||
7753db0d PP |
900 | if (int_ranges) { |
901 | g_array_free(int_ranges, TRUE); | |
02b61fe0 PP |
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); | |
7753db0d PP |
911 | const bt_value *user_attrs; |
912 | bool wrote_user_attrs = false; | |
02b61fe0 | 913 | |
a0bcd369 PP |
914 | /* Write field class's type */ |
915 | switch (fc_type) { | |
9edadc81 PP |
916 | case BT_FIELD_CLASS_TYPE_BOOL: |
917 | type = "Boolean"; | |
918 | break; | |
635615fb PP |
919 | case BT_FIELD_CLASS_TYPE_BIT_ARRAY: |
920 | type = "Bit array"; | |
921 | break; | |
a0bcd369 PP |
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_REAL: | |
935 | type = "Real"; | |
936 | break; | |
937 | case BT_FIELD_CLASS_TYPE_STRING: | |
938 | type = "String"; | |
939 | break; | |
940 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
941 | type = "Structure"; | |
942 | break; | |
943 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: | |
944 | type = "Static array"; | |
945 | break; | |
946 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: | |
947 | type = "Dynamic array"; | |
948 | break; | |
1bca9d21 PP |
949 | case BT_FIELD_CLASS_TYPE_OPTION: |
950 | type = "Option"; | |
951 | break; | |
02b61fe0 PP |
952 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: |
953 | type = "Variant (no selector)"; | |
954 | break; | |
955 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: | |
956 | type = "Variant (unsigned selector)"; | |
957 | break; | |
958 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: | |
959 | type = "Variant (signed selector)"; | |
a0bcd369 PP |
960 | break; |
961 | default: | |
962 | abort(); | |
963 | } | |
964 | ||
965 | g_string_append_printf(ctx->str, "%s%s%s", | |
966 | color_fg_blue(ctx), type, color_reset(ctx)); | |
967 | ||
7753db0d | 968 | /* Write field class's single-line properties */ |
a0bcd369 PP |
969 | switch (fc_type) { |
970 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: | |
971 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: | |
972 | write_sp(ctx); | |
973 | write_int_field_class_props(ctx, fc, true); | |
974 | break; | |
975 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: | |
976 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: | |
977 | { | |
978 | uint64_t mapping_count = | |
979 | bt_field_class_enumeration_get_mapping_count(fc); | |
980 | ||
981 | write_sp(ctx); | |
982 | write_int_field_class_props(ctx, fc, false); | |
983 | g_string_append(ctx->str, ", "); | |
984 | write_uint_prop_value(ctx, mapping_count); | |
985 | g_string_append_printf(ctx->str, " mapping%s)", | |
986 | plural(mapping_count)); | |
a0bcd369 PP |
987 | break; |
988 | } | |
989 | case BT_FIELD_CLASS_TYPE_REAL: | |
990 | if (bt_field_class_real_is_single_precision(fc)) { | |
991 | g_string_append(ctx->str, " (Single precision)"); | |
992 | } else { | |
993 | g_string_append(ctx->str, " (Double precision)"); | |
994 | } | |
995 | ||
996 | break; | |
997 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
998 | { | |
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)); | |
a0bcd369 PP |
1006 | break; |
1007 | } | |
1008 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: | |
1009 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: | |
1010 | if (fc_type == BT_FIELD_CLASS_TYPE_STATIC_ARRAY) { | |
1011 | g_string_append(ctx->str, " (Length "); | |
1012 | write_uint_prop_value(ctx, | |
60bbfc7c | 1013 | bt_field_class_array_static_get_length(fc)); |
a0bcd369 PP |
1014 | g_string_append_c(ctx->str, ')'); |
1015 | } else { | |
1016 | const bt_field_path *length_field_path = | |
60bbfc7c | 1017 | bt_field_class_array_dynamic_borrow_length_field_path_const( |
a0bcd369 PP |
1018 | fc); |
1019 | ||
1020 | if (length_field_path) { | |
1021 | g_string_append(ctx->str, " (Length field path "); | |
1022 | write_field_path(ctx, length_field_path); | |
1023 | g_string_append_c(ctx->str, ')'); | |
1024 | } | |
1025 | } | |
1026 | ||
a0bcd369 | 1027 | break; |
1bca9d21 PP |
1028 | case BT_FIELD_CLASS_TYPE_OPTION: |
1029 | { | |
1030 | const bt_field_path *selector_field_path = | |
1031 | bt_field_class_option_borrow_selector_field_path_const(fc); | |
1032 | ||
1033 | if (selector_field_path) { | |
1034 | g_string_append(ctx->str, " (Selector field path "); | |
1035 | write_field_path(ctx, selector_field_path); | |
1036 | g_string_append_c(ctx->str, ')'); | |
1037 | } | |
1038 | ||
7753db0d PP |
1039 | break; |
1040 | } | |
1041 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: | |
1042 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: | |
1043 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: | |
1044 | { | |
1045 | uint64_t option_count = | |
1046 | bt_field_class_variant_get_option_count(fc); | |
1047 | const bt_field_path *sel_field_path = NULL; | |
1048 | ||
1049 | if (fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR || | |
1050 | fc_type == BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR) { | |
1051 | sel_field_path = | |
1052 | bt_field_class_variant_with_selector_borrow_selector_field_path_const( | |
1053 | fc); | |
1054 | BT_ASSERT(sel_field_path); | |
1055 | } | |
1056 | ||
1057 | g_string_append(ctx->str, " ("); | |
1058 | write_uint_prop_value(ctx, option_count); | |
1059 | g_string_append_printf(ctx->str, " option%s", | |
1060 | plural(option_count)); | |
1061 | ||
1062 | if (sel_field_path) { | |
1063 | g_string_append(ctx->str, ", Selector field path "); | |
1064 | write_field_path(ctx, sel_field_path); | |
1065 | } | |
1066 | ||
1067 | g_string_append_c(ctx->str, ')'); | |
1068 | break; | |
1069 | } | |
1070 | default: | |
1071 | break; | |
1072 | } | |
1073 | ||
1074 | incr_indent(ctx); | |
1075 | user_attrs = bt_field_class_borrow_user_attributes_const(fc); | |
1076 | if (!bt_value_map_is_empty(user_attrs)) { | |
1077 | g_string_append(ctx->str, ":\n"); | |
1078 | write_user_attributes(ctx, user_attrs, false, NULL); | |
1079 | wrote_user_attrs = true; | |
1080 | } | |
1081 | ||
1082 | /* Write field class's complex properties */ | |
1083 | switch (fc_type) { | |
1084 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: | |
1085 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: | |
1086 | { | |
1087 | uint64_t mapping_count = | |
1088 | bt_field_class_enumeration_get_mapping_count(fc); | |
1089 | ||
1090 | if (mapping_count > 0) { | |
1091 | if (wrote_user_attrs) { | |
1092 | write_nl(ctx); | |
1093 | write_indent(ctx); | |
1094 | write_prop_name(ctx, "Mappings"); | |
1095 | g_string_append_c(ctx->str, ':'); | |
1096 | incr_indent(ctx); | |
1097 | } else { | |
1098 | /* Each mapping starts with its own newline */ | |
1099 | g_string_append_c(ctx->str, ':'); | |
1100 | } | |
1101 | ||
1102 | write_enum_field_class_mappings(ctx, fc); | |
1103 | ||
1104 | if (wrote_user_attrs) { | |
1105 | decr_indent(ctx); | |
1106 | } | |
1107 | } | |
1108 | ||
1109 | break; | |
1110 | } | |
1111 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
1112 | { | |
1113 | uint64_t member_count = | |
1114 | bt_field_class_structure_get_member_count(fc); | |
1115 | ||
1116 | if (member_count > 0) { | |
1117 | if (wrote_user_attrs) { | |
1118 | write_nl(ctx); | |
1119 | write_indent(ctx); | |
1120 | write_prop_name(ctx, "Members"); | |
1121 | g_string_append_c(ctx->str, ':'); | |
1122 | incr_indent(ctx); | |
1123 | } else { | |
1124 | /* Each member starts with its own newline */ | |
1125 | g_string_append_c(ctx->str, ':'); | |
1126 | } | |
1127 | ||
1128 | for (i = 0; i < member_count; i++) { | |
1129 | const bt_field_class_structure_member *member = | |
1130 | bt_field_class_structure_borrow_member_by_index_const( | |
1131 | fc, i); | |
1132 | const bt_value *user_attrs; | |
1133 | const bt_field_class *member_fc = | |
1134 | bt_field_class_structure_member_borrow_field_class_const(member); | |
1135 | ||
1136 | write_nl(ctx); | |
1137 | write_compound_member_name(ctx, | |
1138 | bt_field_class_structure_member_get_name(member)); | |
1139 | user_attrs = bt_field_class_structure_member_borrow_user_attributes_const( | |
1140 | member); | |
1141 | ||
1142 | if (bt_value_map_is_empty(user_attrs)) { | |
1143 | write_sp(ctx); | |
1144 | write_field_class(ctx, member_fc); | |
1145 | } else { | |
1146 | write_nl(ctx); | |
1147 | incr_indent(ctx); | |
1148 | ||
1149 | /* Field class */ | |
1150 | write_prop_name_line(ctx, "Field class"); | |
1151 | write_sp(ctx); | |
1152 | write_field_class(ctx, member_fc); | |
1153 | write_nl(ctx); | |
1154 | ||
1155 | /* User attributes */ | |
1156 | write_user_attributes(ctx, user_attrs, | |
1157 | false, NULL); | |
1158 | ||
1159 | decr_indent(ctx); | |
1160 | } | |
1161 | } | |
1162 | ||
1163 | if (wrote_user_attrs) { | |
1164 | decr_indent(ctx); | |
1165 | } | |
1166 | } | |
1167 | ||
1168 | break; | |
1169 | } | |
1170 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: | |
1171 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: | |
1172 | if (wrote_user_attrs) { | |
1173 | write_nl(ctx); | |
1174 | } else { | |
1175 | g_string_append(ctx->str, ":\n"); | |
1176 | } | |
1177 | ||
1178 | write_prop_name_line(ctx, "Element"); | |
1179 | write_sp(ctx); | |
1180 | write_field_class(ctx, | |
1181 | bt_field_class_array_borrow_element_field_class_const(fc)); | |
1182 | break; | |
1183 | case BT_FIELD_CLASS_TYPE_OPTION: | |
1184 | if (wrote_user_attrs) { | |
1185 | write_nl(ctx); | |
1186 | } else { | |
1187 | g_string_append(ctx->str, ":\n"); | |
1188 | } | |
1189 | ||
1190 | write_prop_name_line(ctx, "Content"); | |
1bca9d21 PP |
1191 | write_sp(ctx); |
1192 | write_field_class(ctx, | |
1193 | bt_field_class_option_borrow_field_class_const(fc)); | |
1bca9d21 | 1194 | break; |
02b61fe0 PP |
1195 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: |
1196 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: | |
1197 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: | |
7753db0d PP |
1198 | { |
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 | ||
a0bcd369 | 1223 | break; |
7753db0d | 1224 | } |
a0bcd369 PP |
1225 | default: |
1226 | break; | |
1227 | } | |
7753db0d PP |
1228 | |
1229 | decr_indent(ctx); | |
a0bcd369 PP |
1230 | } |
1231 | ||
1232 | static | |
1233 | void write_root_field_class(struct details_write_ctx *ctx, const char *name, | |
1234 | const bt_field_class *fc) | |
1235 | { | |
1236 | BT_ASSERT(name); | |
1237 | BT_ASSERT(fc); | |
1238 | write_indent(ctx); | |
1239 | write_prop_name(ctx, name); | |
1240 | g_string_append(ctx->str, ": "); | |
02b61fe0 | 1241 | write_field_class(ctx, fc); |
a0bcd369 PP |
1242 | write_nl(ctx); |
1243 | } | |
1244 | ||
1245 | static | |
1246 | void write_event_class(struct details_write_ctx *ctx, const bt_event_class *ec) | |
1247 | { | |
1248 | const char *name = bt_event_class_get_name(ec); | |
1249 | const char *emf_uri; | |
1250 | const bt_field_class *fc; | |
1251 | bt_event_class_log_level log_level; | |
1252 | ||
1253 | write_indent(ctx); | |
1254 | write_obj_type_name(ctx, "Event class"); | |
1255 | ||
1256 | /* Write name and ID */ | |
1257 | if (name) { | |
1258 | g_string_append_printf(ctx->str, " `%s%s%s`", | |
1259 | color_fg_green(ctx), name, color_reset(ctx)); | |
1260 | } | |
1261 | ||
1262 | g_string_append(ctx->str, " (ID "); | |
1263 | write_uint_prop_value(ctx, bt_event_class_get_id(ec)); | |
1264 | g_string_append(ctx->str, "):\n"); | |
1265 | ||
1266 | /* Write properties */ | |
1267 | incr_indent(ctx); | |
1268 | ||
7753db0d PP |
1269 | /* Write user attributes */ |
1270 | write_user_attributes(ctx, | |
1271 | bt_event_class_borrow_user_attributes_const(ec), true, NULL); | |
1272 | ||
a0bcd369 PP |
1273 | /* Write log level */ |
1274 | if (bt_event_class_get_log_level(ec, &log_level) == | |
1275 | BT_PROPERTY_AVAILABILITY_AVAILABLE) { | |
1276 | const char *ll_str = NULL; | |
1277 | ||
1278 | switch (log_level) { | |
1279 | case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY: | |
1280 | ll_str = "Emergency"; | |
1281 | break; | |
1282 | case BT_EVENT_CLASS_LOG_LEVEL_ALERT: | |
1283 | ll_str = "Alert"; | |
1284 | break; | |
1285 | case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL: | |
1286 | ll_str = "Critical"; | |
1287 | break; | |
1288 | case BT_EVENT_CLASS_LOG_LEVEL_ERROR: | |
1289 | ll_str = "Error"; | |
1290 | break; | |
1291 | case BT_EVENT_CLASS_LOG_LEVEL_WARNING: | |
1292 | ll_str = "Warning"; | |
1293 | break; | |
1294 | case BT_EVENT_CLASS_LOG_LEVEL_NOTICE: | |
1295 | ll_str = "Notice"; | |
1296 | break; | |
1297 | case BT_EVENT_CLASS_LOG_LEVEL_INFO: | |
1298 | ll_str = "Info"; | |
1299 | break; | |
1300 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM: | |
1301 | ll_str = "Debug (system)"; | |
1302 | break; | |
1303 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM: | |
1304 | ll_str = "Debug (program)"; | |
1305 | break; | |
1306 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS: | |
1307 | ll_str = "Debug (process)"; | |
1308 | break; | |
1309 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE: | |
1310 | ll_str = "Debug (module)"; | |
1311 | break; | |
1312 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT: | |
1313 | ll_str = "Debug (unit)"; | |
1314 | break; | |
1315 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION: | |
1316 | ll_str = "Debug (function)"; | |
1317 | break; | |
1318 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE: | |
1319 | ll_str = "Debug (line)"; | |
1320 | break; | |
1321 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG: | |
1322 | ll_str = "Debug"; | |
1323 | break; | |
1324 | default: | |
1325 | abort(); | |
1326 | } | |
1327 | ||
1328 | write_str_prop_line(ctx, "Log level", ll_str); | |
1329 | } | |
1330 | ||
1331 | /* Write EMF URI */ | |
1332 | emf_uri = bt_event_class_get_emf_uri(ec); | |
1333 | if (emf_uri) { | |
1334 | write_str_prop_line(ctx, "EMF URI", emf_uri); | |
1335 | } | |
1336 | ||
1337 | /* Write specific context field class */ | |
1338 | fc = bt_event_class_borrow_specific_context_field_class_const(ec); | |
1339 | if (fc) { | |
1340 | write_root_field_class(ctx, "Specific context field class", fc); | |
1341 | } | |
1342 | ||
1343 | /* Write payload field class */ | |
1344 | fc = bt_event_class_borrow_payload_field_class_const(ec); | |
1345 | if (fc) { | |
1346 | write_root_field_class(ctx, "Payload field class", fc); | |
1347 | } | |
1348 | ||
1349 | decr_indent(ctx); | |
1350 | } | |
1351 | ||
1352 | static | |
1353 | void write_clock_class_prop_lines(struct details_write_ctx *ctx, | |
1354 | const bt_clock_class *cc) | |
1355 | { | |
1356 | int64_t offset_seconds; | |
1357 | uint64_t offset_cycles; | |
1358 | const char *str; | |
1359 | ||
1360 | str = bt_clock_class_get_name(cc); | |
1361 | if (str) { | |
1362 | write_str_prop_line(ctx, "Name", str); | |
1363 | } | |
1364 | ||
7753db0d PP |
1365 | write_user_attributes(ctx, |
1366 | bt_clock_class_borrow_user_attributes_const(cc), true, NULL); | |
a0bcd369 PP |
1367 | str = bt_clock_class_get_description(cc); |
1368 | if (str) { | |
1369 | write_str_prop_line(ctx, "Description", str); | |
1370 | } | |
1371 | ||
1372 | write_uint_prop_line(ctx, "Frequency (Hz)", | |
1373 | bt_clock_class_get_frequency(cc)); | |
1374 | write_uint_prop_line(ctx, "Precision (cycles)", | |
1375 | bt_clock_class_get_precision(cc)); | |
1376 | bt_clock_class_get_offset(cc, &offset_seconds, &offset_cycles); | |
1377 | write_int_prop_line(ctx, "Offset (s)", offset_seconds); | |
1378 | write_uint_prop_line(ctx, "Offset (cycles)", offset_cycles); | |
1379 | write_bool_prop_line(ctx, "Origin is Unix epoch", | |
1380 | bt_clock_class_origin_is_unix_epoch(cc)); | |
1381 | ||
1382 | if (ctx->details_comp->cfg.with_uuid) { | |
1383 | bt_uuid uuid = bt_clock_class_get_uuid(cc); | |
1384 | ||
1385 | if (uuid) { | |
1386 | write_uuid_prop_line(ctx, "UUID", uuid); | |
1387 | } | |
1388 | } | |
1389 | } | |
1390 | ||
1391 | static | |
1392 | gint compare_event_classes(const bt_event_class **a, const bt_event_class **b) | |
1393 | { | |
1394 | uint64_t id_a = bt_event_class_get_id(*a); | |
1395 | uint64_t id_b = bt_event_class_get_id(*b); | |
1396 | ||
1397 | if (id_a < id_b) { | |
1398 | return -1; | |
1399 | } else if (id_a > id_b) { | |
1400 | return 1; | |
1401 | } else { | |
1402 | return 0; | |
1403 | } | |
1404 | } | |
1405 | ||
1406 | static | |
1407 | void write_stream_class(struct details_write_ctx *ctx, | |
1408 | const bt_stream_class *sc) | |
1409 | { | |
1410 | const bt_field_class *fc; | |
1411 | GPtrArray *event_classes = g_ptr_array_new(); | |
1412 | uint64_t i; | |
1413 | ||
1414 | write_indent(ctx); | |
1415 | write_obj_type_name(ctx, "Stream class"); | |
1416 | ||
1417 | /* Write name and ID */ | |
1418 | if (ctx->details_comp->cfg.with_stream_class_name) { | |
1419 | const char *name = bt_stream_class_get_name(sc); | |
1420 | ||
1421 | if (name) { | |
1422 | g_string_append(ctx->str, " `"); | |
1423 | write_str_prop_value(ctx, name); | |
1424 | g_string_append(ctx->str, "`"); | |
1425 | } | |
1426 | } | |
1427 | ||
1428 | g_string_append(ctx->str, " (ID "); | |
1429 | write_uint_prop_value(ctx, bt_stream_class_get_id(sc)); | |
1430 | g_string_append(ctx->str, "):\n"); | |
1431 | ||
1432 | /* Write properties */ | |
1433 | incr_indent(ctx); | |
1434 | ||
7753db0d PP |
1435 | /* Write user attributes */ |
1436 | write_user_attributes(ctx, | |
1437 | bt_stream_class_borrow_user_attributes_const(sc), true, NULL); | |
1438 | ||
a0bcd369 PP |
1439 | /* Write configuration */ |
1440 | write_bool_prop_line(ctx, | |
37a93d41 PP |
1441 | "Supports packets", bt_stream_class_supports_packets(sc)); |
1442 | ||
1443 | if (bt_stream_class_supports_packets(sc)) { | |
1444 | write_bool_prop_line(ctx, | |
1445 | "Packets have beginning default clock snapshot", | |
1446 | bt_stream_class_packets_have_beginning_default_clock_snapshot(sc)); | |
1447 | write_bool_prop_line(ctx, | |
1448 | "Packets have end default clock snapshot", | |
1449 | bt_stream_class_packets_have_end_default_clock_snapshot(sc)); | |
1450 | } | |
1451 | ||
a0bcd369 PP |
1452 | write_bool_prop_line(ctx, |
1453 | "Supports discarded events", | |
1454 | bt_stream_class_supports_discarded_events(sc)); | |
74f62477 PP |
1455 | |
1456 | if (bt_stream_class_supports_discarded_events(sc)) { | |
1457 | write_bool_prop_line(ctx, | |
1458 | "Discarded events have default clock snapshots", | |
1459 | bt_stream_class_discarded_events_have_default_clock_snapshots(sc)); | |
1460 | } | |
1461 | ||
a0bcd369 PP |
1462 | write_bool_prop_line(ctx, |
1463 | "Supports discarded packets", | |
1464 | bt_stream_class_supports_discarded_packets(sc)); | |
74f62477 PP |
1465 | |
1466 | if (bt_stream_class_supports_discarded_packets(sc)) { | |
1467 | write_bool_prop_line(ctx, | |
1468 | "Discarded packets have default clock snapshots", | |
1469 | bt_stream_class_discarded_packets_have_default_clock_snapshots(sc)); | |
1470 | } | |
a0bcd369 PP |
1471 | |
1472 | /* Write default clock class */ | |
1473 | if (bt_stream_class_borrow_default_clock_class_const(sc)) { | |
1474 | write_indent(ctx); | |
1475 | write_prop_name(ctx, "Default clock class"); | |
1476 | g_string_append_c(ctx->str, ':'); | |
1477 | write_nl(ctx); | |
1478 | incr_indent(ctx); | |
1479 | write_clock_class_prop_lines(ctx, | |
1480 | bt_stream_class_borrow_default_clock_class_const(sc)); | |
1481 | decr_indent(ctx); | |
1482 | } | |
1483 | ||
1484 | fc = bt_stream_class_borrow_packet_context_field_class_const(sc); | |
1485 | if (fc) { | |
1486 | write_root_field_class(ctx, "Packet context field class", fc); | |
1487 | } | |
1488 | ||
1489 | fc = bt_stream_class_borrow_event_common_context_field_class_const(sc); | |
1490 | if (fc) { | |
1491 | write_root_field_class(ctx, "Event common context field class", | |
1492 | fc); | |
1493 | } | |
1494 | ||
1495 | for (i = 0; i < bt_stream_class_get_event_class_count(sc); i++) { | |
1496 | g_ptr_array_add(event_classes, | |
1497 | (gpointer) bt_stream_class_borrow_event_class_by_index_const( | |
1498 | sc, i)); | |
1499 | } | |
1500 | ||
1501 | g_ptr_array_sort(event_classes, (GCompareFunc) compare_event_classes); | |
1502 | ||
1503 | for (i = 0; i < event_classes->len; i++) { | |
1504 | write_event_class(ctx, event_classes->pdata[i]); | |
1505 | } | |
1506 | ||
1507 | decr_indent(ctx); | |
1508 | g_ptr_array_free(event_classes, TRUE); | |
1509 | } | |
1510 | ||
1511 | static | |
1512 | gint compare_stream_classes(const bt_stream_class **a, const bt_stream_class **b) | |
1513 | { | |
1514 | uint64_t id_a = bt_stream_class_get_id(*a); | |
1515 | uint64_t id_b = bt_stream_class_get_id(*b); | |
1516 | ||
1517 | if (id_a < id_b) { | |
1518 | return -1; | |
1519 | } else if (id_a > id_b) { | |
1520 | return 1; | |
1521 | } else { | |
1522 | return 0; | |
1523 | } | |
1524 | } | |
1525 | ||
a0bcd369 PP |
1526 | static |
1527 | void write_trace_class(struct details_write_ctx *ctx, const bt_trace_class *tc) | |
1528 | { | |
1529 | GPtrArray *stream_classes = g_ptr_array_new(); | |
a0bcd369 PP |
1530 | uint64_t i; |
1531 | bool printed_prop = false; | |
1532 | ||
1533 | write_indent(ctx); | |
1534 | write_obj_type_name(ctx, "Trace class"); | |
1535 | ||
7753db0d | 1536 | |
a0bcd369 PP |
1537 | for (i = 0; i < bt_trace_class_get_stream_class_count(tc); i++) { |
1538 | g_ptr_array_add(stream_classes, | |
1539 | (gpointer) bt_trace_class_borrow_stream_class_by_index_const( | |
1540 | tc, i)); | |
1541 | } | |
1542 | ||
1543 | g_ptr_array_sort(stream_classes, (GCompareFunc) compare_stream_classes); | |
1544 | ||
1545 | if (stream_classes->len > 0) { | |
1546 | if (!printed_prop) { | |
1547 | g_string_append(ctx->str, ":\n"); | |
1548 | printed_prop = true; | |
1549 | } | |
1550 | } | |
1551 | ||
cd03c43c PP |
1552 | incr_indent(ctx); |
1553 | ||
7753db0d PP |
1554 | /* Write user attributes */ |
1555 | write_user_attributes(ctx, | |
1556 | bt_trace_class_borrow_user_attributes_const(tc), true, | |
1557 | &printed_prop); | |
1558 | ||
1559 | /* Write stream classes */ | |
a0bcd369 PP |
1560 | for (i = 0; i < stream_classes->len; i++) { |
1561 | write_stream_class(ctx, stream_classes->pdata[i]); | |
1562 | } | |
1563 | ||
a0bcd369 PP |
1564 | if (!printed_prop) { |
1565 | write_nl(ctx); | |
1566 | } | |
1567 | ||
cd03c43c | 1568 | decr_indent(ctx); |
a0bcd369 | 1569 | g_ptr_array_free(stream_classes, TRUE); |
a0bcd369 PP |
1570 | } |
1571 | ||
1572 | static | |
1573 | int try_write_meta(struct details_write_ctx *ctx, const bt_trace_class *tc, | |
1574 | const bt_stream_class *sc, const bt_event_class *ec) | |
1575 | { | |
1576 | int ret = 0; | |
1577 | ||
1578 | BT_ASSERT(tc); | |
1579 | ||
1580 | if (details_need_to_write_trace_class(ctx, tc)) { | |
1581 | uint64_t sc_i; | |
1582 | ||
1583 | if (ctx->details_comp->cfg.compact && | |
1584 | ctx->details_comp->printed_something) { | |
1585 | /* | |
1586 | * There are no empty line between messages in | |
1587 | * compact mode, so write one here to decouple | |
1588 | * the trace class from the next message. | |
1589 | */ | |
1590 | write_nl(ctx); | |
1591 | } | |
1592 | ||
1593 | /* | |
1594 | * write_trace_class() also writes all its stream | |
1595 | * classes their event classes, so we don't need to | |
1596 | * rewrite `sc`. | |
1597 | */ | |
1598 | write_trace_class(ctx, tc); | |
1599 | write_nl(ctx); | |
1600 | ||
1601 | /* | |
1602 | * Mark this trace class as written, as well as all | |
1603 | * its stream classes and their event classes. | |
1604 | */ | |
1605 | ret = details_did_write_trace_class(ctx, tc); | |
1606 | if (ret) { | |
1607 | goto end; | |
1608 | } | |
1609 | ||
1610 | for (sc_i = 0; sc_i < bt_trace_class_get_stream_class_count(tc); | |
1611 | sc_i++) { | |
1612 | uint64_t ec_i; | |
1613 | const bt_stream_class *tc_sc = | |
1614 | bt_trace_class_borrow_stream_class_by_index_const( | |
1615 | tc, sc_i); | |
1616 | ||
1617 | details_did_write_meta_object(ctx, tc, tc_sc); | |
1618 | ||
1619 | for (ec_i = 0; ec_i < | |
1620 | bt_stream_class_get_event_class_count(tc_sc); | |
1621 | ec_i++) { | |
1622 | details_did_write_meta_object(ctx, tc, | |
1623 | bt_stream_class_borrow_event_class_by_index_const( | |
1624 | tc_sc, ec_i)); | |
1625 | } | |
1626 | } | |
1627 | ||
1628 | goto end; | |
1629 | } | |
1630 | ||
1631 | if (sc && details_need_to_write_meta_object(ctx, tc, sc)) { | |
1632 | uint64_t ec_i; | |
1633 | ||
1634 | BT_ASSERT(tc); | |
1635 | ||
1636 | if (ctx->details_comp->cfg.compact && | |
1637 | ctx->details_comp->printed_something) { | |
1638 | /* | |
1639 | * There are no empty line between messages in | |
1640 | * compact mode, so write one here to decouple | |
1641 | * the stream class from the next message. | |
1642 | */ | |
1643 | write_nl(ctx); | |
1644 | } | |
1645 | ||
1646 | /* | |
1647 | * write_stream_class() also writes all its event | |
1648 | * classes, so we don't need to rewrite `ec`. | |
1649 | */ | |
1650 | write_stream_class(ctx, sc); | |
1651 | write_nl(ctx); | |
1652 | ||
1653 | /* | |
1654 | * Mark this stream class as written, as well as all its | |
1655 | * event classes. | |
1656 | */ | |
1657 | details_did_write_meta_object(ctx, tc, sc); | |
1658 | ||
1659 | for (ec_i = 0; ec_i < | |
1660 | bt_stream_class_get_event_class_count(sc); | |
1661 | ec_i++) { | |
1662 | details_did_write_meta_object(ctx, tc, | |
1663 | bt_stream_class_borrow_event_class_by_index_const( | |
1664 | sc, ec_i)); | |
1665 | } | |
1666 | ||
1667 | goto end; | |
1668 | } | |
1669 | ||
1670 | if (ec && details_need_to_write_meta_object(ctx, tc, ec)) { | |
1671 | BT_ASSERT(sc); | |
1672 | ||
1673 | if (ctx->details_comp->cfg.compact && | |
1674 | ctx->details_comp->printed_something) { | |
1675 | /* | |
1676 | * There are no empty line between messages in | |
1677 | * compact mode, so write one here to decouple | |
1678 | * the event class from the next message. | |
1679 | */ | |
1680 | write_nl(ctx); | |
1681 | } | |
1682 | ||
1683 | write_event_class(ctx, ec); | |
1684 | write_nl(ctx); | |
1685 | details_did_write_meta_object(ctx, tc, ec); | |
1686 | goto end; | |
1687 | } | |
1688 | ||
1689 | end: | |
1690 | return ret; | |
1691 | } | |
1692 | ||
1693 | static | |
1694 | void write_time_str(struct details_write_ctx *ctx, const char *str) | |
1695 | { | |
1696 | if (!ctx->details_comp->cfg.with_time) { | |
1697 | goto end; | |
1698 | } | |
1699 | ||
1700 | g_string_append_printf(ctx->str, "[%s%s%s%s]", | |
1701 | color_bold(ctx), color_fg_blue(ctx), str, color_reset(ctx)); | |
1702 | ||
1703 | if (ctx->details_comp->cfg.compact) { | |
1704 | write_sp(ctx); | |
1705 | } else { | |
1706 | write_nl(ctx); | |
1707 | } | |
1708 | ||
1709 | end: | |
1710 | return; | |
1711 | } | |
1712 | ||
1713 | static | |
1714 | void write_time(struct details_write_ctx *ctx, const bt_clock_snapshot *cs) | |
1715 | { | |
fb25b9e3 | 1716 | bt_clock_snapshot_get_ns_from_origin_status cs_status; |
a0bcd369 PP |
1717 | int64_t ns_from_origin; |
1718 | char buf[32]; | |
1719 | ||
1720 | if (!ctx->details_comp->cfg.with_time) { | |
1721 | goto end; | |
1722 | } | |
1723 | ||
1724 | format_uint(buf, bt_clock_snapshot_get_value(cs), 10); | |
1725 | g_string_append_printf(ctx->str, "[%s%s%s%s%s", | |
1726 | color_bold(ctx), color_fg_blue(ctx), buf, | |
1727 | color_reset(ctx), | |
1728 | ctx->details_comp->cfg.compact ? "" : " cycles"); | |
fb25b9e3 PP |
1729 | cs_status = bt_clock_snapshot_get_ns_from_origin(cs, &ns_from_origin); |
1730 | if (cs_status == BT_CLOCK_SNAPSHOT_GET_NS_FROM_ORIGIN_STATUS_OK) { | |
a0bcd369 PP |
1731 | format_int(buf, ns_from_origin, 10); |
1732 | g_string_append_printf(ctx->str, "%s %s%s%s%s%s", | |
1733 | ctx->details_comp->cfg.compact ? "" : ",", | |
1734 | color_bold(ctx), color_fg_blue(ctx), buf, | |
1735 | color_reset(ctx), | |
1736 | ctx->details_comp->cfg.compact ? "" : " ns from origin"); | |
1737 | } | |
1738 | ||
1739 | g_string_append(ctx->str, "]"); | |
1740 | ||
1741 | if (ctx->details_comp->cfg.compact) { | |
1742 | write_sp(ctx); | |
1743 | } else { | |
1744 | write_nl(ctx); | |
1745 | } | |
1746 | ||
1747 | end: | |
1748 | return; | |
1749 | } | |
1750 | ||
1751 | static | |
1752 | int write_message_follow_tag(struct details_write_ctx *ctx, | |
1753 | const bt_stream *stream) | |
1754 | { | |
1755 | int ret; | |
1756 | uint64_t unique_trace_id; | |
1757 | const bt_stream_class *sc = bt_stream_borrow_class_const(stream); | |
1758 | const bt_trace *trace = bt_stream_borrow_trace_const(stream); | |
1759 | ||
1760 | ret = details_trace_unique_id(ctx, trace, &unique_trace_id); | |
1761 | if (ret) { | |
1762 | goto end; | |
1763 | } | |
1764 | ||
1765 | if (ctx->details_comp->cfg.compact) { | |
1766 | g_string_append_printf(ctx->str, | |
1767 | "%s{%s%" PRIu64 " %" PRIu64 " %" PRIu64 "%s%s}%s ", | |
1768 | color_fg_cyan(ctx), color_bold(ctx), | |
1769 | unique_trace_id, bt_stream_class_get_id(sc), | |
1770 | bt_stream_get_id(stream), | |
1771 | color_reset(ctx), color_fg_cyan(ctx), color_reset(ctx)); | |
1772 | } else { | |
1773 | g_string_append_printf(ctx->str, | |
1774 | "%s{Trace %s%" PRIu64 "%s%s, Stream class ID %s%" PRIu64 "%s%s, Stream ID %s%" PRIu64 "%s%s}%s\n", | |
1775 | color_fg_cyan(ctx), | |
1776 | color_bold(ctx), unique_trace_id, | |
1777 | color_reset(ctx), color_fg_cyan(ctx), | |
1778 | color_bold(ctx), bt_stream_class_get_id(sc), | |
1779 | color_reset(ctx), color_fg_cyan(ctx), | |
1780 | color_bold(ctx), bt_stream_get_id(stream), | |
1781 | color_reset(ctx), color_fg_cyan(ctx), | |
1782 | color_reset(ctx)); | |
1783 | } | |
1784 | ||
1785 | end: | |
1786 | return ret; | |
1787 | } | |
1788 | ||
1789 | static | |
1790 | void write_field(struct details_write_ctx *ctx, const bt_field *field, | |
1791 | const char *name) | |
1792 | { | |
1793 | uint64_t i; | |
1794 | bt_field_class_type fc_type = bt_field_get_class_type(field); | |
1795 | const bt_field_class *fc; | |
1796 | char buf[64]; | |
1797 | ||
1798 | /* Write field's name */ | |
1799 | if (name) { | |
1800 | write_compound_member_name(ctx, name); | |
1801 | } | |
1802 | ||
1803 | /* Write field's value */ | |
1804 | switch (fc_type) { | |
9edadc81 PP |
1805 | case BT_FIELD_CLASS_TYPE_BOOL: |
1806 | write_sp(ctx); | |
1807 | write_bool_prop_value(ctx, bt_field_bool_get_value(field)); | |
1808 | break; | |
635615fb PP |
1809 | case BT_FIELD_CLASS_TYPE_BIT_ARRAY: |
1810 | format_uint(buf, bt_field_bit_array_get_value_as_integer(field), | |
1811 | 16); | |
1812 | write_sp(ctx); | |
1813 | write_uint_str_prop_value(ctx, buf); | |
1814 | break; | |
a0bcd369 PP |
1815 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: |
1816 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: | |
1817 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: | |
1818 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: | |
1819 | { | |
1820 | unsigned int fmt_base; | |
1821 | bt_field_class_integer_preferred_display_base base; | |
1822 | ||
1823 | fc = bt_field_borrow_class_const(field); | |
1824 | base = bt_field_class_integer_get_preferred_display_base(fc); | |
1825 | ||
1826 | switch (base) { | |
1827 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL: | |
1828 | fmt_base = 10; | |
1829 | break; | |
1830 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL: | |
1831 | fmt_base = 8; | |
1832 | break; | |
1833 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY: | |
1834 | fmt_base = 2; | |
1835 | break; | |
1836 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL: | |
1837 | fmt_base = 16; | |
1838 | break; | |
1839 | default: | |
1840 | abort(); | |
1841 | } | |
1842 | ||
1843 | if (fc_type == BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER || | |
1844 | fc_type == BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION) { | |
1845 | format_uint(buf, | |
60bbfc7c | 1846 | bt_field_integer_unsigned_get_value(field), |
a0bcd369 PP |
1847 | fmt_base); |
1848 | write_sp(ctx); | |
1849 | write_uint_str_prop_value(ctx, buf); | |
1850 | } else { | |
1851 | format_int(buf, | |
60bbfc7c | 1852 | bt_field_integer_signed_get_value(field), |
a0bcd369 PP |
1853 | fmt_base); |
1854 | write_sp(ctx); | |
1855 | write_int_str_prop_value(ctx, buf); | |
1856 | } | |
1857 | ||
1858 | break; | |
1859 | } | |
1860 | case BT_FIELD_CLASS_TYPE_REAL: | |
1861 | write_sp(ctx); | |
1862 | write_float_prop_value(ctx, bt_field_real_get_value(field)); | |
1863 | break; | |
1864 | case BT_FIELD_CLASS_TYPE_STRING: | |
1865 | write_sp(ctx); | |
1866 | write_str_prop_value(ctx, bt_field_string_get_value(field)); | |
1867 | break; | |
1868 | case BT_FIELD_CLASS_TYPE_STRUCTURE: | |
1869 | { | |
1870 | uint64_t member_count; | |
1871 | ||
1872 | fc = bt_field_borrow_class_const(field); | |
1873 | member_count = bt_field_class_structure_get_member_count(fc); | |
1874 | ||
1875 | if (member_count > 0) { | |
1876 | incr_indent(ctx); | |
1877 | ||
1878 | for (i = 0; i < member_count; i++) { | |
1879 | const bt_field_class_structure_member *member = | |
1880 | bt_field_class_structure_borrow_member_by_index_const( | |
1881 | fc, i); | |
1882 | const bt_field *member_field = | |
1883 | bt_field_structure_borrow_member_field_by_index_const( | |
1884 | field, i); | |
1885 | ||
1886 | write_nl(ctx); | |
1887 | write_field(ctx, member_field, | |
1888 | bt_field_class_structure_member_get_name(member)); | |
1889 | } | |
1890 | ||
1891 | decr_indent(ctx); | |
1892 | } else { | |
423fc6f7 PP |
1893 | write_sp(ctx); |
1894 | write_none_prop_value(ctx, "Empty"); | |
a0bcd369 PP |
1895 | } |
1896 | ||
1897 | break; | |
1898 | } | |
1899 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: | |
1900 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: | |
1901 | { | |
1902 | uint64_t length = bt_field_array_get_length(field); | |
1903 | ||
1904 | if (length == 0) { | |
423fc6f7 PP |
1905 | write_sp(ctx); |
1906 | write_none_prop_value(ctx, "Empty"); | |
a0bcd369 PP |
1907 | } else { |
1908 | g_string_append(ctx->str, " Length "); | |
1909 | write_uint_prop_value(ctx, length); | |
1910 | g_string_append_c(ctx->str, ':'); | |
1911 | } | |
1912 | ||
1913 | incr_indent(ctx); | |
1914 | ||
1915 | for (i = 0; i < length; i++) { | |
1916 | const bt_field *elem_field = | |
1917 | bt_field_array_borrow_element_field_by_index_const( | |
1918 | field, i); | |
1919 | ||
1920 | write_nl(ctx); | |
7753db0d | 1921 | write_array_index(ctx, i, color_fg_cyan(ctx)); |
a0bcd369 PP |
1922 | write_field(ctx, elem_field, NULL); |
1923 | } | |
1924 | ||
1925 | decr_indent(ctx); | |
1926 | break; | |
1927 | } | |
1bca9d21 PP |
1928 | case BT_FIELD_CLASS_TYPE_OPTION: |
1929 | { | |
1930 | const bt_field *content_field = | |
1931 | bt_field_option_borrow_field_const(field); | |
1932 | ||
1933 | if (!content_field) { | |
1934 | write_sp(ctx); | |
1935 | write_none_prop_value(ctx, "None"); | |
1936 | } else { | |
1937 | write_field(ctx, content_field, NULL); | |
1938 | } | |
1939 | ||
1940 | break; | |
1941 | } | |
02b61fe0 PP |
1942 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR: |
1943 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_SELECTOR: | |
1944 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_SELECTOR: | |
a0bcd369 PP |
1945 | write_field(ctx, |
1946 | bt_field_variant_borrow_selected_option_field_const( | |
1947 | field), NULL); | |
1948 | break; | |
1949 | default: | |
1950 | abort(); | |
1951 | } | |
1952 | } | |
1953 | ||
1954 | static | |
1955 | void write_root_field(struct details_write_ctx *ctx, const char *name, | |
1956 | const bt_field *field) | |
1957 | { | |
1958 | BT_ASSERT(name); | |
1959 | BT_ASSERT(field); | |
1960 | write_indent(ctx); | |
1961 | write_prop_name(ctx, name); | |
1962 | g_string_append(ctx->str, ":"); | |
1963 | write_field(ctx, field, NULL); | |
1964 | write_nl(ctx); | |
1965 | } | |
1966 | ||
1967 | static | |
1968 | int write_event_message(struct details_write_ctx *ctx, | |
1969 | const bt_message *msg) | |
1970 | { | |
1971 | int ret = 0; | |
1972 | const bt_event *event = bt_message_event_borrow_event_const(msg); | |
1973 | const bt_stream *stream = bt_event_borrow_stream_const(event); | |
1974 | const bt_event_class *ec = bt_event_borrow_class_const(event); | |
1975 | const bt_stream_class *sc = bt_event_class_borrow_stream_class_const(ec); | |
1976 | const bt_trace_class *tc = bt_stream_class_borrow_trace_class_const(sc); | |
1977 | const char *ec_name; | |
1978 | const bt_field *field; | |
1979 | ||
1980 | ret = try_write_meta(ctx, tc, sc, ec); | |
1981 | if (ret) { | |
1982 | goto end; | |
1983 | } | |
1984 | ||
1985 | /* Write time */ | |
1986 | if (bt_stream_class_borrow_default_clock_class_const(sc)) { | |
1987 | write_time(ctx, | |
1988 | bt_message_event_borrow_default_clock_snapshot_const( | |
1989 | msg)); | |
1990 | } | |
1991 | ||
1992 | /* Write follow tag for message */ | |
1993 | ret = write_message_follow_tag(ctx, stream); | |
1994 | if (ret) { | |
1995 | goto end; | |
1996 | } | |
1997 | ||
1998 | /* Write object's basic properties */ | |
1999 | write_obj_type_name(ctx, "Event"); | |
2000 | ec_name = bt_event_class_get_name(ec); | |
2001 | if (ec_name) { | |
2002 | g_string_append_printf(ctx->str, " `%s%s%s`", | |
2003 | color_fg_green(ctx), ec_name, color_reset(ctx)); | |
2004 | } | |
2005 | ||
2006 | g_string_append(ctx->str, " ("); | |
2007 | ||
2008 | if (!ctx->details_comp->cfg.compact) { | |
2009 | g_string_append(ctx->str, "Class ID "); | |
2010 | } | |
2011 | ||
2012 | write_uint_prop_value(ctx, bt_event_class_get_id(ec)); | |
2013 | g_string_append(ctx->str, ")"); | |
2014 | ||
2015 | if (ctx->details_comp->cfg.compact) { | |
2016 | write_nl(ctx); | |
2017 | goto end; | |
2018 | } | |
2019 | ||
2020 | /* Write fields */ | |
2021 | g_string_append(ctx->str, ":\n"); | |
2022 | incr_indent(ctx); | |
2023 | field = bt_event_borrow_common_context_field_const(event); | |
2024 | if (field) { | |
2025 | write_root_field(ctx, "Common context", field); | |
2026 | } | |
2027 | ||
2028 | field = bt_event_borrow_specific_context_field_const(event); | |
2029 | if (field) { | |
2030 | write_root_field(ctx, "Specific context", field); | |
2031 | } | |
2032 | ||
2033 | field = bt_event_borrow_payload_field_const(event); | |
2034 | if (field) { | |
2035 | write_root_field(ctx, "Payload", field); | |
2036 | } | |
2037 | ||
2038 | decr_indent(ctx); | |
2039 | ||
2040 | end: | |
2041 | ||
2042 | return ret; | |
2043 | } | |
2044 | ||
2045 | static | |
2046 | gint compare_streams(const bt_stream **a, const bt_stream **b) | |
2047 | { | |
2048 | uint64_t id_a = bt_stream_get_id(*a); | |
2049 | uint64_t id_b = bt_stream_get_id(*b); | |
2050 | ||
2051 | if (id_a < id_b) { | |
2052 | return -1; | |
2053 | } else if (id_a > id_b) { | |
2054 | return 1; | |
2055 | } else { | |
2056 | const bt_stream_class *a_sc = bt_stream_borrow_class_const(*a); | |
2057 | const bt_stream_class *b_sc = bt_stream_borrow_class_const(*b); | |
2058 | uint64_t a_sc_id = bt_stream_class_get_id(a_sc); | |
2059 | uint64_t b_sc_id = bt_stream_class_get_id(b_sc); | |
2060 | ||
2061 | if (a_sc_id < b_sc_id) { | |
2062 | return -1; | |
2063 | } else if (a_sc_id > b_sc_id) { | |
2064 | return 1; | |
2065 | } else { | |
2066 | return 0; | |
2067 | } | |
2068 | } | |
2069 | } | |
2070 | ||
2071 | static | |
2072 | void write_trace(struct details_write_ctx *ctx, const bt_trace *trace) | |
2073 | { | |
2074 | const char *name; | |
a0bcd369 PP |
2075 | GPtrArray *streams = g_ptr_array_new(); |
2076 | uint64_t i; | |
2077 | bool printed_prop = false; | |
cd03c43c PP |
2078 | GPtrArray *env_names = g_ptr_array_new(); |
2079 | uint64_t env_count; | |
a0bcd369 PP |
2080 | |
2081 | write_indent(ctx); | |
2082 | write_obj_type_name(ctx, "Trace"); | |
2083 | ||
2084 | /* Write name */ | |
2085 | if (ctx->details_comp->cfg.with_trace_name) { | |
2086 | name = bt_trace_get_name(trace); | |
2087 | if (name) { | |
2088 | g_string_append(ctx->str, " `"); | |
2089 | write_str_prop_value(ctx, name); | |
2090 | g_string_append(ctx->str, "`"); | |
2091 | } | |
2092 | } | |
2093 | ||
2094 | /* Write properties */ | |
2095 | incr_indent(ctx); | |
2096 | ||
cd03c43c PP |
2097 | /* Write UUID */ |
2098 | if (ctx->details_comp->cfg.with_uuid) { | |
2099 | bt_uuid uuid = bt_trace_get_uuid(trace); | |
2100 | ||
2101 | if (uuid) { | |
a0bcd369 PP |
2102 | if (!printed_prop) { |
2103 | g_string_append(ctx->str, ":\n"); | |
2104 | printed_prop = true; | |
2105 | } | |
2106 | ||
cd03c43c | 2107 | write_uuid_prop_line(ctx, "UUID", uuid); |
a0bcd369 PP |
2108 | } |
2109 | } | |
2110 | ||
cd03c43c PP |
2111 | /* Write environment */ |
2112 | env_count = bt_trace_get_environment_entry_count(trace); | |
2113 | if (env_count > 0) { | |
2114 | if (!printed_prop) { | |
2115 | g_string_append(ctx->str, ":\n"); | |
2116 | printed_prop = true; | |
2117 | } | |
a0bcd369 | 2118 | |
cd03c43c PP |
2119 | write_indent(ctx); |
2120 | write_prop_name(ctx, "Environment"); | |
2121 | g_string_append(ctx->str, " ("); | |
2122 | write_uint_prop_value(ctx, env_count); | |
2123 | g_string_append_printf(ctx->str, " entr%s):", | |
2124 | env_count == 1 ? "y" : "ies"); | |
2125 | write_nl(ctx); | |
2126 | incr_indent(ctx); | |
2127 | ||
2128 | for (i = 0; i < env_count; i++) { | |
2129 | const char *name; | |
2130 | const bt_value *value; | |
2131 | ||
2132 | bt_trace_borrow_environment_entry_by_index_const( | |
2133 | trace, i, &name, &value); | |
2134 | g_ptr_array_add(env_names, (gpointer) name); | |
2135 | } | |
2136 | ||
2137 | g_ptr_array_sort(env_names, (GCompareFunc) compare_strings); | |
2138 | ||
2139 | for (i = 0; i < env_names->len; i++) { | |
2140 | const char *name = env_names->pdata[i]; | |
2141 | const bt_value *value = | |
2142 | bt_trace_borrow_environment_entry_value_by_name_const( | |
2143 | trace, name); | |
2144 | ||
2145 | BT_ASSERT(value); | |
2146 | write_compound_member_name(ctx, name); | |
2147 | write_sp(ctx); | |
2148 | ||
2149 | if (bt_value_get_type(value) == | |
2150 | BT_VALUE_TYPE_SIGNED_INTEGER) { | |
2151 | write_int_prop_value(ctx, | |
60bbfc7c | 2152 | bt_value_integer_signed_get(value)); |
cd03c43c PP |
2153 | } else if (bt_value_get_type(value) == |
2154 | BT_VALUE_TYPE_STRING) { | |
2155 | write_str_prop_value(ctx, | |
2156 | bt_value_string_get(value)); | |
2157 | } else { | |
2158 | abort(); | |
a0bcd369 PP |
2159 | } |
2160 | ||
cd03c43c | 2161 | write_nl(ctx); |
a0bcd369 | 2162 | } |
cd03c43c PP |
2163 | |
2164 | decr_indent(ctx); | |
a0bcd369 PP |
2165 | } |
2166 | ||
2167 | for (i = 0; i < bt_trace_get_stream_count(trace); i++) { | |
2168 | g_ptr_array_add(streams, | |
2169 | (gpointer) bt_trace_borrow_stream_by_index_const( | |
2170 | trace, i)); | |
2171 | } | |
2172 | ||
2173 | g_ptr_array_sort(streams, (GCompareFunc) compare_streams); | |
2174 | ||
2175 | if (streams->len > 0 && !printed_prop) { | |
2176 | g_string_append(ctx->str, ":\n"); | |
2177 | printed_prop = true; | |
2178 | } | |
2179 | ||
2180 | for (i = 0; i < streams->len; i++) { | |
2181 | const bt_stream *stream = streams->pdata[i]; | |
2182 | ||
2183 | write_indent(ctx); | |
2184 | write_obj_type_name(ctx, "Stream"); | |
2185 | g_string_append(ctx->str, " (ID "); | |
2186 | write_uint_prop_value(ctx, bt_stream_get_id(stream)); | |
2187 | g_string_append(ctx->str, ", Class ID "); | |
2188 | write_uint_prop_value(ctx, bt_stream_class_get_id( | |
2189 | bt_stream_borrow_class_const(stream))); | |
2190 | g_string_append(ctx->str, ")"); | |
2191 | write_nl(ctx); | |
2192 | } | |
2193 | ||
2194 | decr_indent(ctx); | |
2195 | ||
2196 | if (!printed_prop) { | |
2197 | write_nl(ctx); | |
2198 | } | |
2199 | ||
2200 | g_ptr_array_free(streams, TRUE); | |
cd03c43c | 2201 | g_ptr_array_free(env_names, TRUE); |
a0bcd369 PP |
2202 | } |
2203 | ||
2204 | static | |
2205 | int write_stream_beginning_message(struct details_write_ctx *ctx, | |
2206 | const bt_message *msg) | |
2207 | { | |
2208 | int ret = 0; | |
2209 | const bt_stream *stream = | |
2210 | bt_message_stream_beginning_borrow_stream_const(msg); | |
2211 | const bt_trace *trace = bt_stream_borrow_trace_const(stream); | |
2212 | const bt_stream_class *sc = bt_stream_borrow_class_const(stream); | |
b7cbc799 | 2213 | const bt_clock_class *cc = bt_stream_class_borrow_default_clock_class_const(sc); |
a0bcd369 PP |
2214 | const bt_trace_class *tc = bt_stream_class_borrow_trace_class_const(sc); |
2215 | const char *name; | |
2216 | ||
2217 | ret = try_write_meta(ctx, tc, sc, NULL); | |
2218 | if (ret) { | |
2219 | goto end; | |
2220 | } | |
2221 | ||
b7cbc799 SM |
2222 | /* Write time */ |
2223 | if (cc) { | |
2224 | const bt_clock_snapshot *cs; | |
2225 | bt_message_stream_clock_snapshot_state cs_state = | |
2226 | bt_message_stream_beginning_borrow_default_clock_snapshot_const(msg, &cs); | |
2227 | ||
2228 | if (cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { | |
2229 | write_time(ctx, cs); | |
2230 | } else { | |
2231 | write_time_str(ctx, "Unknown"); | |
2232 | } | |
2233 | } | |
2234 | ||
a0bcd369 PP |
2235 | /* Write follow tag for message */ |
2236 | ret = write_message_follow_tag(ctx, stream); | |
2237 | if (ret) { | |
2238 | goto end; | |
2239 | } | |
2240 | ||
2241 | /* Write stream properties */ | |
2242 | write_obj_type_name(ctx, "Stream beginning"); | |
2243 | ||
2244 | if (ctx->details_comp->cfg.compact) { | |
2245 | write_nl(ctx); | |
2246 | goto end; | |
2247 | } | |
2248 | ||
2249 | g_string_append(ctx->str, ":\n"); | |
2250 | incr_indent(ctx); | |
2251 | ||
2252 | if (ctx->details_comp->cfg.with_stream_name) { | |
2253 | name = bt_stream_get_name(stream); | |
2254 | if (name) { | |
2255 | write_str_prop_line(ctx, "Name", name); | |
2256 | } | |
2257 | } | |
2258 | ||
2259 | if (ctx->details_comp->cfg.with_stream_class_name) { | |
2260 | name = bt_stream_class_get_name(sc); | |
2261 | if (name) { | |
2262 | write_str_prop_line(ctx, "Class name", name); | |
2263 | } | |
2264 | } | |
2265 | ||
2266 | write_trace(ctx, trace); | |
2267 | decr_indent(ctx); | |
2268 | ||
2269 | end: | |
2270 | return ret; | |
2271 | } | |
2272 | ||
2273 | static | |
2274 | int write_stream_end_message(struct details_write_ctx *ctx, | |
2275 | const bt_message *msg) | |
2276 | { | |
2277 | int ret = 0; | |
2278 | const bt_stream *stream = | |
2279 | bt_message_stream_end_borrow_stream_const(msg); | |
b7cbc799 SM |
2280 | const bt_stream_class *sc = |
2281 | bt_stream_borrow_class_const(stream); | |
2282 | const bt_clock_class *cc = | |
2283 | bt_stream_class_borrow_default_clock_class_const(sc); | |
a0bcd369 PP |
2284 | |
2285 | /* Write time */ | |
b7cbc799 SM |
2286 | if (cc) { |
2287 | const bt_clock_snapshot *cs; | |
2288 | bt_message_stream_clock_snapshot_state cs_state = | |
2289 | bt_message_stream_end_borrow_default_clock_snapshot_const(msg, &cs); | |
a0bcd369 | 2290 | |
b7cbc799 SM |
2291 | if (cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { |
2292 | write_time(ctx, cs); | |
2293 | } else { | |
2294 | write_time_str(ctx, "Unknown"); | |
2295 | } | |
a0bcd369 PP |
2296 | } |
2297 | ||
2298 | /* Write follow tag for message */ | |
2299 | ret = write_message_follow_tag(ctx, stream); | |
2300 | if (ret) { | |
2301 | goto end; | |
2302 | } | |
2303 | ||
b7cbc799 SM |
2304 | /* Write stream properties */ |
2305 | write_obj_type_name(ctx, "Stream end\n"); | |
a0bcd369 PP |
2306 | |
2307 | end: | |
2308 | return ret; | |
2309 | } | |
2310 | ||
2311 | static | |
2312 | int write_packet_beginning_message(struct details_write_ctx *ctx, | |
2313 | const bt_message *msg) | |
2314 | { | |
2315 | int ret = 0; | |
2316 | const bt_packet *packet = | |
2317 | bt_message_packet_beginning_borrow_packet_const(msg); | |
2318 | const bt_stream *stream = bt_packet_borrow_stream_const(packet); | |
2319 | const bt_stream_class *sc = bt_stream_borrow_class_const(stream); | |
2320 | const bt_field *field; | |
2321 | ||
2322 | /* Write time */ | |
2323 | if (bt_stream_class_packets_have_beginning_default_clock_snapshot(sc)) { | |
2324 | write_time(ctx, | |
2325 | bt_message_packet_beginning_borrow_default_clock_snapshot_const( | |
2326 | msg)); | |
2327 | } | |
2328 | ||
2329 | /* Write follow tag for message */ | |
2330 | ret = write_message_follow_tag(ctx, stream); | |
2331 | if (ret) { | |
2332 | goto end; | |
2333 | } | |
2334 | ||
2335 | write_obj_type_name(ctx, "Packet beginning"); | |
2336 | ||
2337 | if (ctx->details_comp->cfg.compact) { | |
2338 | write_nl(ctx); | |
2339 | goto end; | |
2340 | } | |
2341 | ||
2342 | /* Write field */ | |
2343 | g_string_append(ctx->str, ":\n"); | |
2344 | incr_indent(ctx); | |
2345 | field = bt_packet_borrow_context_field_const(packet); | |
2346 | if (field) { | |
2347 | write_root_field(ctx, "Context", field); | |
2348 | } | |
2349 | ||
2350 | decr_indent(ctx); | |
2351 | ||
2352 | end: | |
2353 | return ret; | |
2354 | } | |
2355 | ||
2356 | static | |
2357 | int write_discarded_items_message(struct details_write_ctx *ctx, | |
2358 | const char *name, const bt_stream *stream, | |
2359 | const bt_clock_snapshot *beginning_cs, | |
2360 | const bt_clock_snapshot *end_cs, uint64_t count) | |
2361 | { | |
2362 | int ret = 0; | |
2363 | ||
2364 | /* Write times */ | |
2365 | if (beginning_cs) { | |
2366 | write_time(ctx, beginning_cs); | |
2367 | BT_ASSERT(end_cs); | |
2368 | write_time(ctx, end_cs); | |
2369 | } | |
2370 | ||
2371 | /* Write follow tag for message */ | |
2372 | ret = write_message_follow_tag(ctx, stream); | |
2373 | if (ret) { | |
2374 | goto end; | |
2375 | } | |
2376 | ||
2377 | write_obj_type_name(ctx, "Discarded "); | |
2378 | write_obj_type_name(ctx, name); | |
2379 | ||
2380 | /* Write count */ | |
2381 | if (count == UINT64_C(-1)) { | |
2382 | write_nl(ctx); | |
2383 | goto end; | |
2384 | } | |
2385 | ||
2386 | g_string_append(ctx->str, " ("); | |
2387 | write_uint_prop_value(ctx, count); | |
2388 | g_string_append_printf(ctx->str, " %s)\n", name); | |
2389 | ||
2390 | end: | |
2391 | return ret; | |
2392 | } | |
2393 | ||
2394 | static | |
2395 | int write_discarded_events_message(struct details_write_ctx *ctx, | |
2396 | const bt_message *msg) | |
2397 | { | |
2398 | const bt_stream *stream = bt_message_discarded_events_borrow_stream_const( | |
2399 | msg); | |
2400 | const bt_stream_class *sc = bt_stream_borrow_class_const(stream); | |
2401 | const bt_clock_snapshot *beginning_cs = NULL; | |
2402 | const bt_clock_snapshot *end_cs = NULL; | |
2403 | uint64_t count; | |
2404 | ||
2405 | if (bt_stream_class_discarded_events_have_default_clock_snapshots(sc)) { | |
2406 | beginning_cs = | |
2407 | bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const( | |
2408 | msg); | |
2409 | end_cs = | |
2410 | bt_message_discarded_events_borrow_end_default_clock_snapshot_const( | |
2411 | msg); | |
2412 | } | |
2413 | ||
2414 | if (bt_message_discarded_events_get_count(msg, &count) != | |
2415 | BT_PROPERTY_AVAILABILITY_AVAILABLE) { | |
2416 | count = UINT64_C(-1); | |
2417 | } | |
2418 | ||
2419 | return write_discarded_items_message(ctx, "events", stream, | |
2420 | beginning_cs, end_cs, count); | |
2421 | } | |
2422 | ||
2423 | static | |
2424 | int write_discarded_packets_message(struct details_write_ctx *ctx, | |
2425 | const bt_message *msg) | |
2426 | { | |
2427 | const bt_stream *stream = bt_message_discarded_packets_borrow_stream_const( | |
2428 | msg); | |
2429 | const bt_stream_class *sc = bt_stream_borrow_class_const(stream); | |
2430 | const bt_clock_snapshot *beginning_cs = NULL; | |
2431 | const bt_clock_snapshot *end_cs = NULL; | |
2432 | uint64_t count; | |
2433 | ||
2434 | if (bt_stream_class_discarded_packets_have_default_clock_snapshots(sc)) { | |
2435 | beginning_cs = | |
2436 | bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const( | |
2437 | msg); | |
2438 | end_cs = | |
2439 | bt_message_discarded_packets_borrow_end_default_clock_snapshot_const( | |
2440 | msg); | |
2441 | } | |
2442 | ||
2443 | if (bt_message_discarded_packets_get_count(msg, &count) != | |
2444 | BT_PROPERTY_AVAILABILITY_AVAILABLE) { | |
2445 | count = UINT64_C(-1); | |
2446 | } | |
2447 | ||
2448 | return write_discarded_items_message(ctx, "packets", stream, | |
2449 | beginning_cs, end_cs, count); | |
2450 | } | |
2451 | ||
2452 | static | |
2453 | int write_packet_end_message(struct details_write_ctx *ctx, | |
2454 | const bt_message *msg) | |
2455 | { | |
2456 | int ret = 0; | |
2457 | const bt_packet *packet = | |
2458 | bt_message_packet_end_borrow_packet_const(msg); | |
2459 | const bt_stream *stream = bt_packet_borrow_stream_const(packet); | |
2460 | const bt_stream_class *sc = bt_stream_borrow_class_const(stream); | |
2461 | ||
2462 | /* Write time */ | |
2463 | if (bt_stream_class_packets_have_end_default_clock_snapshot(sc)) { | |
2464 | write_time(ctx, | |
2465 | bt_message_packet_end_borrow_default_clock_snapshot_const( | |
2466 | msg)); | |
2467 | } | |
2468 | ||
2469 | /* Write follow tag for message */ | |
2470 | ret = write_message_follow_tag(ctx, stream); | |
2471 | if (ret) { | |
2472 | goto end; | |
2473 | } | |
2474 | ||
2475 | write_obj_type_name(ctx, "Packet end"); | |
2476 | write_nl(ctx); | |
2477 | ||
2478 | end: | |
2479 | return ret; | |
2480 | } | |
2481 | ||
2482 | static | |
2483 | int write_message_iterator_inactivity_message(struct details_write_ctx *ctx, | |
2484 | const bt_message *msg) | |
2485 | { | |
2486 | int ret = 0; | |
2487 | const bt_clock_snapshot *cs = | |
2488 | bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const( | |
2489 | msg); | |
2490 | ||
2491 | /* Write time */ | |
2492 | write_time(ctx, cs); | |
2493 | write_obj_type_name(ctx, "Message iterator inactivity"); | |
2494 | ||
2495 | if (ctx->details_comp->cfg.compact) { | |
2496 | write_nl(ctx); | |
2497 | goto end; | |
2498 | } | |
2499 | ||
2500 | /* Write clock class properties */ | |
2501 | g_string_append(ctx->str, ":\n"); | |
2502 | incr_indent(ctx); | |
2503 | write_indent(ctx); | |
2504 | write_prop_name(ctx, "Clock class"); | |
2505 | g_string_append_c(ctx->str, ':'); | |
2506 | write_nl(ctx); | |
2507 | incr_indent(ctx); | |
2508 | write_clock_class_prop_lines(ctx, | |
2509 | bt_clock_snapshot_borrow_clock_class_const(cs)); | |
2510 | decr_indent(ctx); | |
2511 | ||
2512 | end: | |
2513 | return ret; | |
2514 | } | |
2515 | ||
2516 | BT_HIDDEN | |
2517 | int details_write_message(struct details_comp *details_comp, | |
2518 | const bt_message *msg) | |
2519 | { | |
2520 | int ret = 0; | |
2521 | struct details_write_ctx ctx = { | |
2522 | .details_comp = details_comp, | |
2523 | .str = details_comp->str, | |
2524 | .indent_level = 0, | |
2525 | }; | |
2526 | ||
2527 | /* Reset output buffer */ | |
2528 | g_string_assign(details_comp->str, ""); | |
2529 | ||
2530 | if (details_comp->printed_something && !details_comp->cfg.compact) { | |
2531 | write_nl(&ctx); | |
2532 | } | |
2533 | ||
2534 | switch (bt_message_get_type(msg)) { | |
2535 | case BT_MESSAGE_TYPE_EVENT: | |
2536 | ret = write_event_message(&ctx, msg); | |
2537 | break; | |
2538 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: | |
2539 | ret = write_message_iterator_inactivity_message(&ctx, msg); | |
2540 | break; | |
2541 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: | |
2542 | ret = write_stream_beginning_message(&ctx, msg); | |
2543 | break; | |
2544 | case BT_MESSAGE_TYPE_STREAM_END: | |
2545 | ret = write_stream_end_message(&ctx, msg); | |
2546 | break; | |
2547 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: | |
2548 | ret = write_packet_beginning_message(&ctx, msg); | |
2549 | break; | |
2550 | case BT_MESSAGE_TYPE_PACKET_END: | |
2551 | ret = write_packet_end_message(&ctx, msg); | |
2552 | break; | |
a0bcd369 PP |
2553 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
2554 | ret = write_discarded_events_message(&ctx, msg); | |
2555 | break; | |
2556 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
2557 | ret = write_discarded_packets_message(&ctx, msg); | |
2558 | break; | |
2559 | default: | |
2560 | abort(); | |
2561 | } | |
2562 | ||
2563 | return ret; | |
2564 | } |