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