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