Text output identical with babeltrace 1
[babeltrace.git] / plugins / text / print.c
CommitLineData
af9a82eb
JG
1/*
2 * print.c
3 *
4 * Babeltrace CTF Text Output Plugin Event Printing
5 *
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
f504043c 7 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
af9a82eb
JG
8 *
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30#include <babeltrace/ctf-ir/event.h>
31#include <babeltrace/ctf-ir/event-class.h>
6a18b281 32#include <babeltrace/ctf-ir/packet.h>
af9a82eb
JG
33#include <babeltrace/ctf-ir/stream.h>
34#include <babeltrace/ctf-ir/stream-class.h>
ac0c6bdd 35#include <babeltrace/ctf-ir/clock-class.h>
6a18b281
MD
36#include <babeltrace/ctf-ir/field-types.h>
37#include <babeltrace/ctf-ir/fields.h>
1556a1af 38#include <babeltrace/ctf-ir/trace.h>
6a18b281 39#include <babeltrace/bitfield.h>
ad96d936 40#include <babeltrace/common-internal.h>
6a18b281 41#include <inttypes.h>
af9a82eb
JG
42#include "text.h"
43
1556a1af
JG
44#define NSEC_PER_SEC 1000000000LL
45
ad96d936
PP
46#define COLOR_NAME BT_COMMON_COLOR_BOLD
47#define COLOR_FIELD_NAME BT_COMMON_COLOR_FG_CYAN
48#define COLOR_RST BT_COMMON_COLOR_RESET
0a2d9024
JG
49#define COLOR_STRING_VALUE BT_COMMON_COLOR_BOLD
50#define COLOR_NUMBER_VALUE BT_COMMON_COLOR_BOLD
51#define COLOR_ENUM_MAPPING_NAME BT_COMMON_COLOR_BOLD
8f3f0945 52#define COLOR_UNKNOWN BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_RED
ad96d936
PP
53#define COLOR_EVENT_NAME BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_MAGENTA
54#define COLOR_TIMESTAMP BT_COMMON_COLOR_BOLD BT_COMMON_COLOR_FG_YELLOW
55
6a18b281
MD
56static inline
57const char *rem_(const char *str)
58{
59 if (str[0] == '_')
60 return &str[1];
61 else
62 return str;
63}
64
af9a82eb
JG
65struct timestamp {
66 int64_t real_timestamp; /* Relative to UNIX epoch. */
67 uint64_t clock_value; /* In cycles. */
68};
69
6a18b281
MD
70static
71enum bt_component_status print_field(struct text_component *text,
2b4c4a7c
JD
72 struct bt_ctf_field *field, bool print_names,
73 GQuark *filters_fields, int filter_array_len);
6a18b281 74
ad96d936
PP
75static
76void print_name_equal(struct text_component *text, const char *name)
77{
78 if (text->use_colors) {
79 fprintf(text->out, "%s%s%s = ", COLOR_NAME, name, COLOR_RST);
80 } else {
06e8da8b 81 fprintf(text->out, "%s = ", name);
ad96d936
PP
82 }
83}
84
85static
86void print_field_name_equal(struct text_component *text, const char *name)
87{
88 if (text->use_colors) {
89 fprintf(text->out, "%s%s%s = ", COLOR_FIELD_NAME, name,
90 COLOR_RST);
91 } else {
06e8da8b 92 fprintf(text->out, "%s = ", name);
ad96d936
PP
93 }
94}
95
af9a82eb
JG
96static
97void print_timestamp_cycles(struct text_component *text,
ac0c6bdd 98 struct bt_ctf_clock_class *clock_class,
af9a82eb
JG
99 struct bt_ctf_event *event)
100{
1556a1af
JG
101 int ret;
102 struct bt_ctf_clock_value *clock_value;
103 uint64_t cycles;
104
ac0c6bdd 105 clock_value = bt_ctf_event_get_clock_value(event, clock_class);
1556a1af
JG
106 if (!clock_value) {
107 fputs("????????????????????", text->out);
108 return;
109 }
110
111 ret = bt_ctf_clock_value_get_value(clock_value, &cycles);
112 bt_put(clock_value);
113 if (ret) {
114 fprintf(text->out, "Error");
115 return;
116 }
117 fprintf(text->out, "%020" PRIu64, cycles);
3af83b5a
MD
118
119 if (text->last_cycles_timestamp != -1ULL) {
120 text->delta_cycles = cycles - text->last_cycles_timestamp;
121 }
122 text->last_cycles_timestamp = cycles;
af9a82eb
JG
123}
124
125static
126void print_timestamp_wall(struct text_component *text,
ac0c6bdd 127 struct bt_ctf_clock_class *clock_class,
af9a82eb
JG
128 struct bt_ctf_event *event)
129{
1556a1af
JG
130 int ret;
131 struct bt_ctf_clock_value *clock_value;
132 int64_t ts_nsec = 0; /* add configurable offset */
133 int64_t ts_sec = 0; /* add configurable offset */
134 uint64_t ts_sec_abs, ts_nsec_abs;
135 bool is_negative;
af9a82eb 136
ac0c6bdd 137 clock_value = bt_ctf_event_get_clock_value(event, clock_class);
1556a1af
JG
138 if (!clock_value) {
139 fputs("??:??:??.?????????", text->out);
140 return;
141 }
142
143 ret = bt_ctf_clock_value_get_value_ns_from_epoch(clock_value, &ts_nsec);
144 bt_put(clock_value);
145 if (ret) {
146 fprintf(text->out, "Error");
147 return;
148 }
149
3af83b5a
MD
150 if (text->last_real_timestamp != -1ULL) {
151 text->delta_real_timestamp = ts_nsec - text->last_real_timestamp;
152 }
153 text->last_real_timestamp = ts_nsec;
154
1556a1af
JG
155 ts_sec += ts_nsec / NSEC_PER_SEC;
156 ts_nsec = ts_nsec % NSEC_PER_SEC;
157 if (ts_sec >= 0 && ts_nsec >= 0) {
158 is_negative = false;
159 ts_sec_abs = ts_sec;
160 ts_nsec_abs = ts_nsec;
161 } else if (ts_sec > 0 && ts_nsec < 0) {
162 is_negative = false;
163 ts_sec_abs = ts_sec - 1;
164 ts_nsec_abs = NSEC_PER_SEC + ts_nsec;
165 } else if (ts_sec == 0 && ts_nsec < 0) {
166 is_negative = true;
167 ts_sec_abs = ts_sec;
168 ts_nsec_abs = -ts_nsec;
169 } else if (ts_sec < 0 && ts_nsec > 0) {
170 is_negative = true;
171 ts_sec_abs = -(ts_sec + 1);
172 ts_nsec_abs = NSEC_PER_SEC - ts_nsec;
173 } else if (ts_sec < 0 && ts_nsec == 0) {
174 is_negative = true;
175 ts_sec_abs = -ts_sec;
176 ts_nsec_abs = ts_nsec;
177 } else { /* (ts_sec < 0 && ts_nsec < 0) */
178 is_negative = true;
179 ts_sec_abs = -ts_sec;
180 ts_nsec_abs = -ts_nsec;
181 }
182
f504043c 183 if (!text->options.clock_seconds) {
1556a1af
JG
184 struct tm tm;
185 time_t time_s = (time_t) ts_sec_abs;
186
187 if (is_negative) {
188 fprintf(stderr, "[warning] Fallback to [sec.ns] to print negative time value. Use --clock-seconds.\n");
189 goto seconds;
190 }
191
f504043c 192 if (!text->options.clock_gmt) {
1556a1af
JG
193 struct tm *res;
194
195 res = localtime_r(&time_s, &tm);
196 if (!res) {
197 fprintf(stderr, "[warning] Unable to get localtime.\n");
198 goto seconds;
199 }
200 } else {
201 struct tm *res;
202
203 res = gmtime_r(&time_s, &tm);
204 if (!res) {
205 fprintf(stderr, "[warning] Unable to get gmtime.\n");
206 goto seconds;
207 }
208 }
f504043c 209 if (text->options.clock_date) {
1556a1af
JG
210 char timestr[26];
211 size_t res;
212
213 /* Print date and time */
214 res = strftime(timestr, sizeof(timestr),
215 "%F ", &tm);
216 if (!res) {
217 fprintf(stderr, "[warning] Unable to print ascii time.\n");
218 goto seconds;
219 }
220 fprintf(text->out, "%s", timestr);
221 }
222 /* Print time in HH:MM:SS.ns */
223 fprintf(text->out, "%02d:%02d:%02d.%09" PRIu64,
224 tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec_abs);
225 goto end;
226 }
227seconds:
228 fprintf(text->out, "%s%" PRId64 ".%09" PRIu64,
229 is_negative ? "-" : "", ts_sec_abs, ts_nsec_abs);
230end:
231 return;
af9a82eb
JG
232}
233
234static
235enum bt_component_status print_event_timestamp(struct text_component *text,
c3c30b08 236 struct bt_ctf_event *event, bool *start_line)
af9a82eb
JG
237{
238 bool print_names = text->options.print_header_field_names;
239 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
6a18b281 240 struct bt_ctf_stream *stream = NULL;
1556a1af
JG
241 struct bt_ctf_stream_class *stream_class = NULL;
242 struct bt_ctf_trace *trace = NULL;
ac0c6bdd 243 struct bt_ctf_clock_class *clock_class = NULL;
af9a82eb 244 FILE *out = text->out;
af9a82eb
JG
245
246 stream = bt_ctf_event_get_stream(event);
247 if (!stream) {
248 ret = BT_COMPONENT_STATUS_ERROR;
249 goto end;
250 }
251
1556a1af 252 stream_class = bt_ctf_stream_get_class(stream);
f504043c
MD
253 if (!stream_class) {
254 ret = BT_COMPONENT_STATUS_ERROR;
255 goto end;
256 }
1556a1af 257 trace = bt_ctf_stream_class_get_trace(stream_class);
f504043c
MD
258 if (!trace) {
259 ret = BT_COMPONENT_STATUS_ERROR;
260 goto end;
261 }
ac0c6bdd
PP
262 clock_class = bt_ctf_trace_get_clock_class(trace, 0);
263 if (!clock_class) {
f504043c
MD
264 ret = BT_COMPONENT_STATUS_ERROR;
265 goto end;
266 }
af9a82eb 267
ad96d936
PP
268 if (print_names) {
269 print_name_equal(text, "timestamp");
270 } else {
271 fputs("[", out);
272 }
273 if (text->use_colors) {
274 fputs(COLOR_TIMESTAMP, text->out);
275 }
af9a82eb 276 if (text->options.print_timestamp_cycles) {
ac0c6bdd 277 print_timestamp_cycles(text, clock_class, event);
af9a82eb 278 } else {
ac0c6bdd 279 print_timestamp_wall(text, clock_class, event);
af9a82eb 280 }
ad96d936
PP
281 if (text->use_colors) {
282 fputs(COLOR_RST, text->out);
283 }
af9a82eb 284
c3c30b08
MD
285 if (!print_names)
286 fputs("] ", out);
c3c30b08 287
3af83b5a 288 if (text->options.print_delta_field) {
ad96d936
PP
289 if (print_names) {
290 fputs(", ", text->out);
291 print_name_equal(text, "delta");
292 } else {
3af83b5a 293 fputs("(", text->out);
ad96d936 294 }
3af83b5a
MD
295 if (text->options.print_timestamp_cycles) {
296 if (text->delta_cycles == -1ULL) {
297 fputs("+??????????\?\?) ", text->out); /* Not a trigraph. */
298 } else {
299 fprintf(text->out, "+%012" PRIu64, text->delta_cycles);
300 }
301 } else {
302 if (text->delta_real_timestamp != -1ULL) {
303 uint64_t delta_sec, delta_nsec, delta;
f504043c 304
3af83b5a
MD
305 delta = text->delta_real_timestamp;
306 delta_sec = delta / NSEC_PER_SEC;
307 delta_nsec = delta % NSEC_PER_SEC;
308 fprintf(text->out, "+%" PRIu64 ".%09" PRIu64,
309 delta_sec, delta_nsec);
310 } else {
311 fputs("+?.?????????", text->out);
312 }
313 }
314 if (!print_names) {
315 fputs(") ", text->out);
316 }
317 }
318 *start_line = !print_names;
f504043c 319
af9a82eb
JG
320end:
321 bt_put(stream);
ac0c6bdd 322 bt_put(clock_class);
1556a1af
JG
323 bt_put(stream_class);
324 bt_put(trace);
af9a82eb
JG
325 return ret;
326}
327
6a18b281 328static
af9a82eb
JG
329enum bt_component_status print_event_header(struct text_component *text,
330 struct bt_ctf_event *event)
331{
6a18b281
MD
332 bool print_names = text->options.print_header_field_names;
333 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
334 struct bt_ctf_event_class *event_class = NULL;
c3c30b08
MD
335 struct bt_ctf_stream_class *stream_class = NULL;
336 struct bt_ctf_trace *trace_class = NULL;
60535549 337 int dom_print = 0;
af9a82eb 338
6a18b281
MD
339 event_class = bt_ctf_event_get_class(event);
340 if (!event_class) {
341 ret = BT_COMPONENT_STATUS_ERROR;
342 goto end;
343 }
c3c30b08
MD
344 stream_class = bt_ctf_event_class_get_stream_class(event_class);
345 if (!stream_class) {
346 ret = BT_COMPONENT_STATUS_ERROR;
347 goto end;
348 }
349 trace_class = bt_ctf_stream_class_get_trace(stream_class);
350 if (!trace_class) {
351 ret = BT_COMPONENT_STATUS_ERROR;
352 goto end;
353 }
c3c30b08 354 ret = print_event_timestamp(text, event, &text->start_line);
af9a82eb
JG
355 if (ret != BT_COMPONENT_STATUS_OK) {
356 goto end;
357 }
c3c30b08
MD
358 if (text->options.print_trace_field) {
359 const char *name;
360
361 name = bt_ctf_trace_get_name(trace_class);
362 if (name) {
363 if (!text->start_line) {
364 fputs(", ", text->out);
365 }
c3c30b08 366 if (print_names) {
ad96d936 367 print_name_equal(text, "trace");
c3c30b08
MD
368 }
369 fprintf(text->out, "%s", name);
60535549
JD
370 if (!print_names) {
371 fprintf(text->out, " ");
372 }
c3c30b08
MD
373 }
374 }
375 if (text->options.print_trace_hostname_field) {
376 struct bt_value *hostname_str;
377
378 hostname_str = bt_ctf_trace_get_environment_field_value_by_name(trace_class,
379 "hostname");
380 if (hostname_str) {
381 const char *str;
382
383 if (!text->start_line) {
384 fputs(", ", text->out);
385 }
c3c30b08 386 if (print_names) {
ad96d936 387 print_name_equal(text, "trace:hostname");
c3c30b08
MD
388 }
389 if (bt_value_string_get(hostname_str, &str)
390 == BT_VALUE_STATUS_OK) {
391 fprintf(text->out, "%s", str);
392 }
393 bt_put(hostname_str);
60535549 394 dom_print = 1;
c3c30b08
MD
395 }
396 }
397 if (text->options.print_trace_domain_field) {
398 struct bt_value *domain_str;
399
400 domain_str = bt_ctf_trace_get_environment_field_value_by_name(trace_class,
401 "domain");
402 if (domain_str) {
403 const char *str;
404
405 if (!text->start_line) {
406 fputs(", ", text->out);
407 }
c3c30b08 408 if (print_names) {
ad96d936 409 print_name_equal(text, "trace:domain");
60535549
JD
410 } else if (dom_print) {
411 fputs(":", text->out);
c3c30b08
MD
412 }
413 if (bt_value_string_get(domain_str, &str)
414 == BT_VALUE_STATUS_OK) {
415 fprintf(text->out, "%s", str);
416 }
417 bt_put(domain_str);
60535549 418 dom_print = 1;
c3c30b08
MD
419 }
420 }
421 if (text->options.print_trace_procname_field) {
422 struct bt_value *procname_str;
423
424 procname_str = bt_ctf_trace_get_environment_field_value_by_name(trace_class,
425 "procname");
426 if (procname_str) {
427 const char *str;
428
429 if (!text->start_line) {
430 fputs(", ", text->out);
431 }
c3c30b08 432 if (print_names) {
ad96d936 433 print_name_equal(text, "trace:procname");
60535549
JD
434 } else if (dom_print) {
435 fputs(":", text->out);
c3c30b08
MD
436 }
437 if (bt_value_string_get(procname_str, &str)
438 == BT_VALUE_STATUS_OK) {
439 fprintf(text->out, "%s", str);
440 }
441 bt_put(procname_str);
60535549 442 dom_print = 1;
c3c30b08
MD
443 }
444 }
445 if (text->options.print_trace_vpid_field) {
446 struct bt_value *vpid_value;
447
448 vpid_value = bt_ctf_trace_get_environment_field_value_by_name(trace_class,
449 "vpid");
450 if (vpid_value) {
451 int64_t value;
452
453 if (!text->start_line) {
454 fputs(", ", text->out);
455 }
c3c30b08 456 if (print_names) {
ad96d936 457 print_name_equal(text, "trace:vpid");
60535549
JD
458 } else if (dom_print) {
459 fputs(":", text->out);
c3c30b08
MD
460 }
461 if (bt_value_integer_get(vpid_value, &value)
462 == BT_VALUE_STATUS_OK) {
463 fprintf(text->out, "(%" PRId64 ")", value);
464 }
465 bt_put(vpid_value);
60535549 466 dom_print = 1;
c3c30b08
MD
467 }
468 }
469 if (text->options.print_loglevel_field) {
470 struct bt_value *loglevel_str, *loglevel_value;
471
472 loglevel_str = bt_ctf_event_class_get_attribute_value_by_name(event_class,
473 "loglevel_string");
474 loglevel_value = bt_ctf_event_class_get_attribute_value_by_name(event_class,
475 "loglevel");
476 if (loglevel_str || loglevel_value) {
477 bool has_str = false;
478
479 if (!text->start_line) {
480 fputs(", ", text->out);
481 }
c3c30b08 482 if (print_names) {
ad96d936 483 print_name_equal(text, "loglevel");
60535549
JD
484 } else if (dom_print) {
485 fputs(":", text->out);
c3c30b08
MD
486 }
487 if (loglevel_str) {
488 const char *str;
489
490 if (bt_value_string_get(loglevel_str, &str)
491 == BT_VALUE_STATUS_OK) {
492 fprintf(text->out, "%s", str);
493 has_str = true;
494 }
495 }
496 if (loglevel_value) {
497 int64_t value;
498
499 if (bt_value_integer_get(loglevel_value, &value)
500 == BT_VALUE_STATUS_OK) {
501 fprintf(text->out, "%s(%" PRId64 ")",
502 has_str ? " " : "", value);
503 }
504 }
505 bt_put(loglevel_str);
506 bt_put(loglevel_value);
60535549 507 dom_print = 1;
c3c30b08
MD
508 }
509 }
510 if (text->options.print_emf_field) {
511 struct bt_value *uri_str;
512
513 uri_str = bt_ctf_event_class_get_attribute_value_by_name(event_class,
514 "model.emf.uri");
515 if (uri_str) {
516 if (!text->start_line) {
517 fputs(", ", text->out);
518 }
c3c30b08 519 if (print_names) {
ad96d936 520 print_name_equal(text, "model.emf.uri");
60535549
JD
521 } else if (dom_print) {
522 fputs(":", text->out);
c3c30b08
MD
523 }
524 if (uri_str) {
525 const char *str;
526
527 if (bt_value_string_get(uri_str, &str)
528 == BT_VALUE_STATUS_OK) {
529 fprintf(text->out, "%s", str);
530 }
531 }
532 bt_put(uri_str);
60535549 533 dom_print = 1;
c3c30b08
MD
534 }
535 }
60535549
JD
536 if (dom_print && !print_names) {
537 fputs(" ", text->out);
538 }
c3c30b08
MD
539 if (!text->start_line) {
540 fputs(", ", text->out);
541 }
60535549 542 text->start_line = true;
6a18b281 543 if (print_names) {
ad96d936
PP
544 print_name_equal(text, "name");
545 }
546 if (text->use_colors) {
547 fputs(COLOR_EVENT_NAME, text->out);
6a18b281 548 }
af9a82eb 549 fputs(bt_ctf_event_class_get_name(event_class), text->out);
ad96d936
PP
550 if (text->use_colors) {
551 fputs(COLOR_RST, text->out);
552 }
60535549
JD
553 if (!print_names) {
554 fputs(": ", text->out);
555 } else {
556 fputs(", ", text->out);
557 }
af9a82eb 558end:
c3c30b08
MD
559 bt_put(trace_class);
560 bt_put(stream_class);
6a18b281
MD
561 bt_put(event_class);
562 return ret;
563}
564
565static
566enum bt_component_status print_integer(struct text_component *text,
567 struct bt_ctf_field *field)
568{
569 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
570 struct bt_ctf_field_type *field_type = NULL;
571 enum bt_ctf_integer_base base;
572 enum bt_ctf_string_encoding encoding;
573 int signedness;
574 union {
575 uint64_t u;
576 int64_t s;
577 } v;
ad96d936 578 bool rst_color = false;
6a18b281
MD
579
580 field_type = bt_ctf_field_get_type(field);
581 if (!field_type) {
582 ret = BT_COMPONENT_STATUS_ERROR;
583 goto end;
584 }
585 signedness = bt_ctf_field_type_integer_get_signed(field_type);
586 if (signedness < 0) {
587 ret = BT_COMPONENT_STATUS_ERROR;
588 goto end;
589 }
590 if (!signedness) {
591 if (bt_ctf_field_unsigned_integer_get_value(field, &v.u) < 0) {
592 ret = BT_COMPONENT_STATUS_ERROR;
593 goto end;
594 }
595 } else {
596 if (bt_ctf_field_signed_integer_get_value(field, &v.s) < 0) {
597 ret = BT_COMPONENT_STATUS_ERROR;
598 goto end;
599 }
600 }
601
602 encoding = bt_ctf_field_type_integer_get_encoding(field_type);
603 switch (encoding) {
604 case BT_CTF_STRING_ENCODING_UTF8:
605 case BT_CTF_STRING_ENCODING_ASCII:
606 g_string_append_c(text->string, (int) v.u);
607 goto end;
608 case BT_CTF_STRING_ENCODING_NONE:
609 case BT_CTF_STRING_ENCODING_UNKNOWN:
610 break;
611 default:
612 ret = BT_COMPONENT_STATUS_ERROR;
613 goto end;
614 }
615
ad96d936
PP
616 if (text->use_colors) {
617 fputs(COLOR_NUMBER_VALUE, text->out);
618 rst_color = true;
619 }
620
6a18b281
MD
621 base = bt_ctf_field_type_integer_get_base(field_type);
622 switch (base) {
623 case BT_CTF_INTEGER_BASE_BINARY:
624 {
625 int bitnr, len;
626
627 len = bt_ctf_field_type_integer_get_size(field_type);
628 if (len < 0) {
629 ret = BT_COMPONENT_STATUS_ERROR;
630 goto end;
631 }
632 fprintf(text->out, "0b");
633 v.u = _bt_piecewise_lshift(v.u, 64 - len);
634 for (bitnr = 0; bitnr < len; bitnr++) {
635 fprintf(text->out, "%u", (v.u & (1ULL << 63)) ? 1 : 0);
636 v.u = _bt_piecewise_lshift(v.u, 1);
637 }
638 break;
639 }
640 case BT_CTF_INTEGER_BASE_OCTAL:
641 {
642 if (signedness) {
643 int len;
644
645 len = bt_ctf_field_type_integer_get_size(field_type);
646 if (len < 0) {
647 ret = BT_COMPONENT_STATUS_ERROR;
648 goto end;
649 }
650 if (len < 64) {
651 size_t rounded_len;
652
653 assert(len != 0);
654 /* Round length to the nearest 3-bit */
655 rounded_len = (((len - 1) / 3) + 1) * 3;
656 v.u &= ((uint64_t) 1 << rounded_len) - 1;
657 }
658 }
659
660 fprintf(text->out, "0%" PRIo64, v.u);
661 break;
662 }
663 case BT_CTF_INTEGER_BASE_DECIMAL:
664 if (!signedness) {
665 fprintf(text->out, "%" PRIu64, v.u);
666 } else {
667 fprintf(text->out, "%" PRId64, v.s);
668 }
669 break;
670 case BT_CTF_INTEGER_BASE_HEXADECIMAL:
671 {
672 int len;
673
674 len = bt_ctf_field_type_integer_get_size(field_type);
675 if (len < 0) {
676 ret = BT_COMPONENT_STATUS_ERROR;
677 goto end;
678 }
679 if (len < 64) {
680 /* Round length to the nearest nibble */
681 uint8_t rounded_len = ((len + 3) & ~0x3);
682
683 v.u &= ((uint64_t) 1 << rounded_len) - 1;
684 }
685
686 fprintf(text->out, "0x%" PRIX64, v.u);
687 break;
688 }
689 default:
690 ret = BT_COMPONENT_STATUS_ERROR;
691 goto end;
692 }
693end:
ad96d936
PP
694 if (rst_color) {
695 fputs(COLOR_RST, text->out);
696 }
6a18b281
MD
697 bt_put(field_type);
698 return ret;
699}
700
701static
702enum bt_component_status print_enum(struct text_component *text,
703 struct bt_ctf_field *field)
704{
705 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
706 struct bt_ctf_field *container_field = NULL;
96e8f959
MD
707 struct bt_ctf_field_type *enumeration_field_type = NULL;
708 struct bt_ctf_field_type *container_field_type = NULL;
709 struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL;
710 int nr_mappings = 0;
711 int is_signed;
712
713 enumeration_field_type = bt_ctf_field_get_type(field);
714 if (!enumeration_field_type) {
715 ret = BT_COMPONENT_STATUS_ERROR;
716 goto end;
717 }
6a18b281
MD
718 container_field = bt_ctf_field_enumeration_get_container(field);
719 if (!container_field) {
720 ret = BT_COMPONENT_STATUS_ERROR;
721 goto end;
722 }
96e8f959
MD
723 container_field_type = bt_ctf_field_get_type(container_field);
724 if (!container_field_type) {
725 ret = BT_COMPONENT_STATUS_ERROR;
726 goto end;
727 }
728 is_signed = bt_ctf_field_type_integer_get_signed(container_field_type);
729 if (is_signed < 0) {
730 ret = BT_COMPONENT_STATUS_ERROR;
731 goto end;
732 }
733 if (is_signed) {
734 int64_t value;
735
736 if (bt_ctf_field_signed_integer_get_value(container_field,
737 &value)) {
738 ret = BT_COMPONENT_STATUS_ERROR;
739 goto end;
740 }
741 iter = bt_ctf_field_type_enumeration_find_mappings_by_signed_value(
742 enumeration_field_type, value);
6a18b281 743 } else {
96e8f959
MD
744 uint64_t value;
745
746 if (bt_ctf_field_unsigned_integer_get_value(container_field,
747 &value)) {
748 ret = BT_COMPONENT_STATUS_ERROR;
749 goto end;
750 }
751 iter = bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(
752 enumeration_field_type, value);
753 }
754 if (!iter) {
755 ret = BT_COMPONENT_STATUS_ERROR;
756 goto end;
757 }
758 fprintf(text->out, "( ");
759 for (;;) {
760 const char *mapping_name;
761
762 if (bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
763 iter, &mapping_name, NULL, NULL) < 0) {
764 ret = BT_COMPONENT_STATUS_ERROR;
765 goto end;
766 }
767 if (nr_mappings++)
768 fprintf(text->out, ", ");
ad96d936
PP
769 if (text->use_colors) {
770 fputs(COLOR_ENUM_MAPPING_NAME, text->out);
771 }
772 // TODO: escape string
96e8f959 773 fprintf(text->out, "\"%s\"", mapping_name);
ad96d936
PP
774 if (text->use_colors) {
775 fputs(COLOR_RST, text->out);
776 }
96e8f959
MD
777 if (bt_ctf_field_type_enumeration_mapping_iterator_next(iter) < 0) {
778 break;
779 }
780 }
781 if (!nr_mappings) {
ad96d936
PP
782 if (text->use_colors) {
783 fputs(COLOR_UNKNOWN, text->out);
784 }
96e8f959 785 fprintf(text->out, "<unknown>");
ad96d936
PP
786 if (text->use_colors) {
787 fputs(COLOR_RST, text->out);
788 }
6a18b281
MD
789 }
790 fprintf(text->out, " : container = ");
791 ret = print_integer(text, container_field);
792 if (ret != BT_COMPONENT_STATUS_OK) {
793 goto end;
794 }
795 fprintf(text->out, " )");
796end:
96e8f959
MD
797 bt_put(iter);
798 bt_put(container_field_type);
6a18b281 799 bt_put(container_field);
96e8f959 800 bt_put(enumeration_field_type);
6a18b281
MD
801 return ret;
802}
803
2b4c4a7c
JD
804static
805int filter_field_name(struct text_component *text, const char *field_name,
806 GQuark *filter_fields, int filter_array_len)
807{
808 int i;
809 GQuark field_quark = g_quark_try_string(field_name);
810
811 if (!field_quark || text->options.verbose) {
812 return 1;
813 }
814
815 for (i = 0; i < filter_array_len; i++) {
816 if (field_quark == filter_fields[i]) {
817 return 0;
818 }
819 }
820 return 1;
821}
822
6a18b281
MD
823static
824enum bt_component_status print_struct_field(struct text_component *text,
825 struct bt_ctf_field *_struct,
826 struct bt_ctf_field_type *struct_type,
2b4c4a7c
JD
827 int i, bool print_names, int *nr_printed_fields,
828 GQuark *filter_fields, int filter_array_len)
6a18b281
MD
829{
830 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
831 const char *field_name;
832 struct bt_ctf_field *field = NULL;
833 struct bt_ctf_field_type *field_type = NULL;;
834
835 field = bt_ctf_field_structure_get_field_by_index(_struct, i);
836 if (!field) {
837 ret = BT_COMPONENT_STATUS_ERROR;
838 goto end;
839 }
840 if (bt_ctf_field_type_structure_get_field(struct_type,
841 &field_name, &field_type, i) < 0) {
842 ret = BT_COMPONENT_STATUS_ERROR;
843 goto end;
844 }
845
2b4c4a7c
JD
846 if (filter_fields && !filter_field_name(text, field_name,
847 filter_fields, filter_array_len)) {
848 ret = BT_COMPONENT_STATUS_OK;
849 goto end;
850 }
851
852 if (*nr_printed_fields > 0) {
6a18b281
MD
853 fprintf(text->out, ", ");
854 } else {
855 fprintf(text->out, " ");
856 }
857 if (print_names) {
ad96d936 858 print_field_name_equal(text, rem_(field_name));
6a18b281 859 }
2b4c4a7c
JD
860 ret = print_field(text, field, print_names, NULL, 0);
861 *nr_printed_fields += 1;
6a18b281
MD
862end:
863 bt_put(field_type);
864 bt_put(field);
865 return ret;
866}
867
868static
869enum bt_component_status print_struct(struct text_component *text,
2b4c4a7c
JD
870 struct bt_ctf_field *_struct, bool print_names,
871 GQuark *filter_fields, int filter_array_len)
6a18b281
MD
872{
873 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
874 struct bt_ctf_field_type *struct_type = NULL;
2b4c4a7c 875 int nr_fields, i, nr_printed_fields;
6a18b281
MD
876
877 struct_type = bt_ctf_field_get_type(_struct);
878 if (!struct_type) {
879 ret = BT_COMPONENT_STATUS_ERROR;
880 goto end;
881 }
882 nr_fields = bt_ctf_field_type_structure_get_field_count(struct_type);
883 if (nr_fields < 0) {
884 ret = BT_COMPONENT_STATUS_ERROR;
885 goto end;
886 }
887 fprintf(text->out, "{");
888 text->depth++;
2b4c4a7c 889 nr_printed_fields = 0;
6a18b281
MD
890 for (i = 0; i < nr_fields; i++) {
891 ret = print_struct_field(text, _struct, struct_type, i,
2b4c4a7c
JD
892 print_names, &nr_printed_fields, filter_fields,
893 filter_array_len);
6a18b281
MD
894 if (ret != BT_COMPONENT_STATUS_OK) {
895 goto end;
896 }
897 }
898 text->depth--;
899 fprintf(text->out, " }");
900end:
901 bt_put(struct_type);
902 return ret;
903}
904
905static
906enum bt_component_status print_array_field(struct text_component *text,
907 struct bt_ctf_field *array, uint64_t i,
908 bool is_string, bool print_names)
909{
910 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
911 struct bt_ctf_field *field = NULL;
912
913 if (!is_string) {
914 if (i != 0) {
915 fprintf(text->out, ", ");
916 } else {
917 fprintf(text->out, " ");
918 }
60535549
JD
919 if (print_names) {
920 fprintf(text->out, "[%" PRIu64 "] = ", i);
921 }
6a18b281
MD
922 }
923 field = bt_ctf_field_array_get_field(array, i);
924 if (!field) {
925 ret = BT_COMPONENT_STATUS_ERROR;
926 goto end;
927 }
2b4c4a7c 928 ret = print_field(text, field, print_names, NULL, 0);
6a18b281
MD
929end:
930 bt_put(field);
931 return ret;
932}
933
934static
935enum bt_component_status print_array(struct text_component *text,
936 struct bt_ctf_field *array, bool print_names)
937{
938 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
939 struct bt_ctf_field_type *array_type = NULL, *field_type = NULL;
940 enum bt_ctf_type_id type_id;
941 int64_t len;
942 uint64_t i;
943 bool is_string = false;
944
945 array_type = bt_ctf_field_get_type(array);
946 if (!array_type) {
947 ret = BT_COMPONENT_STATUS_ERROR;
948 goto end;
949 }
950 field_type = bt_ctf_field_type_array_get_element_type(array_type);
951 if (!field_type) {
952 ret = BT_COMPONENT_STATUS_ERROR;
953 goto end;
954 }
955 len = bt_ctf_field_type_array_get_length(array_type);
956 if (len < 0) {
957 ret = BT_COMPONENT_STATUS_ERROR;
958 goto end;
959 }
960 type_id = bt_ctf_field_type_get_type_id(field_type);
961 if (type_id == BT_CTF_TYPE_ID_INTEGER) {
962 enum bt_ctf_string_encoding encoding;
963
964 encoding = bt_ctf_field_type_integer_get_encoding(field_type);
965 if (encoding == BT_CTF_STRING_ENCODING_UTF8
966 || encoding == BT_CTF_STRING_ENCODING_ASCII) {
967 int integer_len, integer_alignment;
968
969 integer_len = bt_ctf_field_type_integer_get_size(field_type);
970 if (integer_len < 0) {
971 return BT_COMPONENT_STATUS_ERROR;
972 }
973 integer_alignment = bt_ctf_field_type_get_alignment(field_type);
974 if (integer_alignment < 0) {
975 return BT_COMPONENT_STATUS_ERROR;
976 }
977 if (integer_len == CHAR_BIT
978 && integer_alignment == CHAR_BIT) {
979 is_string = true;
980 }
981 }
982 }
983
984 if (is_string) {
985 g_string_assign(text->string, "");
986 } else {
987 fprintf(text->out, "[");
988 }
989
990 text->depth++;
991 for (i = 0; i < len; i++) {
992 ret = print_array_field(text, array, i, is_string, print_names);
993 if (ret != BT_COMPONENT_STATUS_OK) {
994 goto end;
995 }
996 }
997 text->depth--;
998
999 if (is_string) {
ad96d936
PP
1000 if (text->use_colors) {
1001 fputs(COLOR_STRING_VALUE, text->out);
1002 }
1003 // TODO: escape string
6a18b281 1004 fprintf(text->out, "\"%s\"", text->string->str);
ad96d936
PP
1005 if (text->use_colors) {
1006 fputs(COLOR_RST, text->out);
1007 }
6a18b281
MD
1008 } else {
1009 fprintf(text->out, " ]");
1010 }
1011end:
1012 bt_put(field_type);
1013 bt_put(array_type);
1014 return ret;
1015}
1016
1017static
1018enum bt_component_status print_sequence_field(struct text_component *text,
1019 struct bt_ctf_field *seq, uint64_t i,
1020 bool is_string, bool print_names)
1021{
1022 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1023 struct bt_ctf_field *field = NULL;
1024
1025 if (!is_string) {
1026 if (i != 0) {
1027 fprintf(text->out, ", ");
1028 } else {
1029 fprintf(text->out, " ");
1030 }
60535549
JD
1031 if (print_names) {
1032 fprintf(text->out, "[%" PRIu64 "] = ", i);
1033 }
6a18b281
MD
1034 }
1035 field = bt_ctf_field_sequence_get_field(seq, i);
1036 if (!field) {
1037 ret = BT_COMPONENT_STATUS_ERROR;
1038 goto end;
1039 }
2b4c4a7c 1040 ret = print_field(text, field, print_names, NULL, 0);
6a18b281
MD
1041end:
1042 bt_put(field);
1043 return ret;
1044}
1045
1046static
1047enum bt_component_status print_sequence(struct text_component *text,
1048 struct bt_ctf_field *seq, bool print_names)
1049{
1050 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1051 struct bt_ctf_field_type *seq_type = NULL, *field_type = NULL;
1052 struct bt_ctf_field *length_field = NULL;
1053 enum bt_ctf_type_id type_id;
1054 uint64_t len;
1055 uint64_t i;
1056 bool is_string = false;
1057
1058 seq_type = bt_ctf_field_get_type(seq);
1059 if (!seq_type) {
1060 ret = BT_COMPONENT_STATUS_ERROR;
1061 goto end;
1062 }
1063 length_field = bt_ctf_field_sequence_get_length(seq);
1064 if (!length_field) {
1065 ret = BT_COMPONENT_STATUS_ERROR;
1066 goto end;
1067 }
1068 if (bt_ctf_field_unsigned_integer_get_value(length_field, &len) < 0) {
1069 ret = BT_COMPONENT_STATUS_ERROR;
1070 goto end;
1071 }
1072 field_type = bt_ctf_field_type_sequence_get_element_type(seq_type);
1073 if (!field_type) {
1074 ret = BT_COMPONENT_STATUS_ERROR;
1075 goto end;
1076 }
1077 type_id = bt_ctf_field_type_get_type_id(field_type);
1078 if (type_id == BT_CTF_TYPE_ID_INTEGER) {
1079 enum bt_ctf_string_encoding encoding;
1080
1081 encoding = bt_ctf_field_type_integer_get_encoding(field_type);
1082 if (encoding == BT_CTF_STRING_ENCODING_UTF8
1083 || encoding == BT_CTF_STRING_ENCODING_ASCII) {
1084 int integer_len, integer_alignment;
1085
1086 integer_len = bt_ctf_field_type_integer_get_size(field_type);
1087 if (integer_len < 0) {
1088 ret = BT_COMPONENT_STATUS_ERROR;
1089 goto end;
1090 }
1091 integer_alignment = bt_ctf_field_type_get_alignment(field_type);
1092 if (integer_alignment < 0) {
1093 ret = BT_COMPONENT_STATUS_ERROR;
1094 goto end;
1095 }
1096 if (integer_len == CHAR_BIT
1097 && integer_alignment == CHAR_BIT) {
1098 is_string = true;
1099 }
1100 }
1101 }
1102
1103 if (is_string) {
1104 g_string_assign(text->string, "");
1105 } else {
1106 fprintf(text->out, "[");
1107 }
1108
1109 text->depth++;
1110 for (i = 0; i < len; i++) {
1111 ret = print_sequence_field(text, seq, i,
1112 is_string, print_names);
1113 if (ret != BT_COMPONENT_STATUS_OK) {
1114 goto end;
1115 }
1116 }
1117 text->depth--;
1118
1119 if (is_string) {
ad96d936
PP
1120 if (text->use_colors) {
1121 fputs(COLOR_STRING_VALUE, text->out);
1122 }
1123 // TODO: escape string
6a18b281 1124 fprintf(text->out, "\"%s\"", text->string->str);
ad96d936
PP
1125 if (text->use_colors) {
1126 fputs(COLOR_RST, text->out);
1127 }
6a18b281
MD
1128 } else {
1129 fprintf(text->out, " ]");
1130 }
1131end:
1132 bt_put(length_field);
1133 bt_put(field_type);
1134 bt_put(seq_type);
1135 return ret;
1136}
1137
1138static
1139enum bt_component_status print_variant(struct text_component *text,
1140 struct bt_ctf_field *variant, bool print_names)
1141{
1142 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1143 struct bt_ctf_field *field = NULL;
1144
1145 field = bt_ctf_field_variant_get_current_field(variant);
1146 if (!field) {
1147 ret = BT_COMPONENT_STATUS_ERROR;
1148 goto end;
1149 }
1150 fprintf(text->out, "{ ");
1151 text->depth++;
1152 if (print_names) {
e0f15669 1153 int iter_ret;
6a18b281
MD
1154 struct bt_ctf_field *tag_field = NULL;
1155 const char *tag_choice;
e0f15669 1156 struct bt_ctf_field_type_enumeration_mapping_iterator *iter;
6a18b281
MD
1157
1158 tag_field = bt_ctf_field_variant_get_tag(variant);
1159 if (!tag_field) {
1160 ret = BT_COMPONENT_STATUS_ERROR;
1161 goto end;
1162 }
e0f15669
JG
1163
1164 iter = bt_ctf_field_enumeration_get_mappings(tag_field);
1165 if (!iter) {
1166 bt_put(tag_field);
1167 ret = BT_COMPONENT_STATUS_ERROR;
1168 goto end;
1169 }
1170
1171 iter_ret =
8803be2a
JG
1172 bt_ctf_field_type_enumeration_mapping_iterator_get_signed(
1173 iter, &tag_choice, NULL, NULL);
e0f15669
JG
1174 if (iter_ret) {
1175 bt_put(iter);
6a18b281
MD
1176 bt_put(tag_field);
1177 ret = BT_COMPONENT_STATUS_ERROR;
1178 goto end;
1179 }
ad96d936 1180 print_field_name_equal(text, rem_(tag_choice));
6a18b281 1181 bt_put(tag_field);
e0f15669 1182 bt_put(iter);
6a18b281 1183 }
2b4c4a7c 1184 ret = print_field(text, field, print_names, NULL, 0);
6a18b281
MD
1185 if (ret != BT_COMPONENT_STATUS_OK) {
1186 goto end;
1187 }
1188 text->depth--;
1189 fprintf(text->out, " }");
1190end:
1191 bt_put(field);
1192 return ret;
1193}
1194
1195static
1196enum bt_component_status print_field(struct text_component *text,
2b4c4a7c
JD
1197 struct bt_ctf_field *field, bool print_names,
1198 GQuark *filter_fields, int filter_array_len)
6a18b281
MD
1199{
1200 enum bt_ctf_type_id type_id;
1201
1202 type_id = bt_ctf_field_get_type_id(field);
1203 switch (type_id) {
1204 case CTF_TYPE_INTEGER:
1205 return print_integer(text, field);
1206 case CTF_TYPE_FLOAT:
1207 {
1208 double v;
1209
1210 if (bt_ctf_field_floating_point_get_value(field, &v)) {
1211 return BT_COMPONENT_STATUS_ERROR;
1212 }
ad96d936
PP
1213 if (text->use_colors) {
1214 fputs(COLOR_NUMBER_VALUE, text->out);
1215 }
6a18b281 1216 fprintf(text->out, "%g", v);
ad96d936
PP
1217 if (text->use_colors) {
1218 fputs(COLOR_RST, text->out);
1219 }
6a18b281
MD
1220 return BT_COMPONENT_STATUS_OK;
1221 }
1222 case CTF_TYPE_ENUM:
1223 return print_enum(text, field);
1224 case CTF_TYPE_STRING:
ad96d936
PP
1225 if (text->use_colors) {
1226 fputs(COLOR_STRING_VALUE, text->out);
1227 }
1228 // TODO: escape the string value
1229 fprintf(text->out, "\"%s\"",
1230 bt_ctf_field_string_get_value(field));
1231 if (text->use_colors) {
1232 fputs(COLOR_RST, text->out);
1233 }
6a18b281
MD
1234 return BT_COMPONENT_STATUS_OK;
1235 case CTF_TYPE_STRUCT:
2b4c4a7c
JD
1236 return print_struct(text, field, print_names, filter_fields,
1237 filter_array_len);
6a18b281
MD
1238 case CTF_TYPE_UNTAGGED_VARIANT:
1239 case CTF_TYPE_VARIANT:
1240 return print_variant(text, field, print_names);
1241 case CTF_TYPE_ARRAY:
1242 return print_array(text, field, print_names);
1243 case CTF_TYPE_SEQUENCE:
1244 return print_sequence(text, field, print_names);
1245 default:
1246 fprintf(text->err, "[error] Unknown type id: %d\n", (int) type_id);
1247 return BT_COMPONENT_STATUS_ERROR;
1248 }
1249}
1250
1251static
1252enum bt_component_status print_stream_packet_context(struct text_component *text,
1253 struct bt_ctf_event *event)
1254{
1255 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1256 struct bt_ctf_packet *packet = NULL;
1257 struct bt_ctf_field *main_field = NULL;
1258
1259 packet = bt_ctf_event_get_packet(event);
1260 if (!packet) {
1261 ret = BT_COMPONENT_STATUS_ERROR;
1262 goto end;
1263 }
1264 main_field = bt_ctf_packet_get_context(packet);
1265 if (!main_field) {
6a18b281
MD
1266 goto end;
1267 }
6e1bc0df
MD
1268 if (!text->start_line) {
1269 fputs(", ", text->out);
1270 }
1271 text->start_line = false;
6a18b281 1272 if (text->options.print_scope_field_names) {
ad96d936 1273 print_name_equal(text, "stream.packet.context");
6a18b281
MD
1274 }
1275 ret = print_field(text, main_field,
2b4c4a7c
JD
1276 text->options.print_context_field_names,
1277 stream_packet_context_quarks,
1278 STREAM_PACKET_CONTEXT_QUARKS_LEN);
6a18b281
MD
1279end:
1280 bt_put(main_field);
1281 bt_put(packet);
1282 return ret;
1283}
1284
1285static
1286enum bt_component_status print_event_header_raw(struct text_component *text,
1287 struct bt_ctf_event *event)
1288{
1289 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1290 struct bt_ctf_field *main_field = NULL;
1291
1292 main_field = bt_ctf_event_get_header(event);
1293 if (!main_field) {
6a18b281
MD
1294 goto end;
1295 }
6e1bc0df
MD
1296 if (!text->start_line) {
1297 fputs(", ", text->out);
1298 }
1299 text->start_line = false;
6a18b281 1300 if (text->options.print_scope_field_names) {
ad96d936 1301 print_name_equal(text, "stream.event.header");
6a18b281
MD
1302 }
1303 ret = print_field(text, main_field,
2b4c4a7c 1304 text->options.print_header_field_names, NULL, 0);
6a18b281
MD
1305end:
1306 bt_put(main_field);
1307 return ret;
1308}
1309
1310static
1311enum bt_component_status print_stream_event_context(struct text_component *text,
1312 struct bt_ctf_event *event)
1313{
1314 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1315 struct bt_ctf_field *main_field = NULL;
1316
1317 main_field = bt_ctf_event_get_stream_event_context(event);
1318 if (!main_field) {
6a18b281
MD
1319 goto end;
1320 }
6e1bc0df
MD
1321 if (!text->start_line) {
1322 fputs(", ", text->out);
1323 }
1324 text->start_line = false;
6a18b281 1325 if (text->options.print_scope_field_names) {
ad96d936 1326 print_name_equal(text, "stream.event.context");
6a18b281
MD
1327 }
1328 ret = print_field(text, main_field,
2b4c4a7c 1329 text->options.print_context_field_names, NULL, 0);
6a18b281
MD
1330end:
1331 bt_put(main_field);
1332 return ret;
1333}
1334
1335static
1336enum bt_component_status print_event_context(struct text_component *text,
1337 struct bt_ctf_event *event)
1338{
1339 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1340 struct bt_ctf_field *main_field = NULL;
1341
1342 main_field = bt_ctf_event_get_event_context(event);
1343 if (!main_field) {
6a18b281
MD
1344 goto end;
1345 }
6e1bc0df
MD
1346 if (!text->start_line) {
1347 fputs(", ", text->out);
1348 }
1349 text->start_line = false;
6a18b281 1350 if (text->options.print_scope_field_names) {
ad96d936 1351 print_name_equal(text, "event.context");
6a18b281
MD
1352 }
1353 ret = print_field(text, main_field,
2b4c4a7c 1354 text->options.print_context_field_names, NULL, 0);
6a18b281
MD
1355end:
1356 bt_put(main_field);
1357 return ret;
1358}
1359
1360static
1361enum bt_component_status print_event_payload(struct text_component *text,
1362 struct bt_ctf_event *event)
1363{
1364 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
1365 struct bt_ctf_field *main_field = NULL;
1366
1367 main_field = bt_ctf_event_get_payload_field(event);
1368 if (!main_field) {
6a18b281
MD
1369 goto end;
1370 }
6e1bc0df
MD
1371 if (!text->start_line) {
1372 fputs(", ", text->out);
1373 }
1374 text->start_line = false;
6a18b281 1375 if (text->options.print_scope_field_names) {
ad96d936 1376 print_name_equal(text, "event.fields");
6a18b281
MD
1377 }
1378 ret = print_field(text, main_field,
2b4c4a7c 1379 text->options.print_payload_field_names, NULL, 0);
6a18b281
MD
1380end:
1381 bt_put(main_field);
af9a82eb
JG
1382 return ret;
1383}
1384
1385BT_HIDDEN
1386enum bt_component_status text_print_event(struct text_component *text,
1387 struct bt_ctf_event *event)
1388{
1389 enum bt_component_status ret;
1390
6e1bc0df 1391 text->start_line = true;
af9a82eb
JG
1392 ret = print_event_header(text, event);
1393 if (ret != BT_COMPONENT_STATUS_OK) {
1394 goto end;
1395 }
6a18b281
MD
1396
1397 ret = print_stream_packet_context(text, event);
1398 if (ret != BT_COMPONENT_STATUS_OK) {
1399 goto end;
1400 }
6a18b281 1401
60535549
JD
1402 if (text->options.verbose) {
1403 ret = print_event_header_raw(text, event);
1404 if (ret != BT_COMPONENT_STATUS_OK) {
1405 goto end;
1406 }
6a18b281 1407 }
6a18b281
MD
1408
1409 ret = print_stream_event_context(text, event);
1410 if (ret != BT_COMPONENT_STATUS_OK) {
1411 goto end;
1412 }
6a18b281
MD
1413
1414 ret = print_event_context(text, event);
1415 if (ret != BT_COMPONENT_STATUS_OK) {
1416 goto end;
1417 }
6a18b281
MD
1418
1419 ret = print_event_payload(text, event);
1420 if (ret != BT_COMPONENT_STATUS_OK) {
1421 goto end;
1422 }
af9a82eb
JG
1423
1424 fputc('\n', text->out);
1425end:
1426 return ret;
1427}
This page took 0.085823 seconds and 4 git commands to generate.