b713f5d0697d91571d3e53a27fdc7f31c8b02d9d
[babeltrace.git] / src / plugins / text / pretty / pretty.c
1 /*
2 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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
26 #define BT_LOG_TAG "PLUGIN-TEXT-PRETTY-SINK"
27 #include "logging.h"
28
29 #include <babeltrace2/babeltrace.h>
30 #include "compat/compiler.h"
31 #include "common/common.h"
32 #include <stdio.h>
33 #include <stdbool.h>
34 #include <glib.h>
35 #include "common/assert.h"
36
37 #include "pretty.h"
38
39 GQuark stream_packet_context_quarks[STREAM_PACKET_CONTEXT_QUARKS_LEN];
40
41 static
42 const char *plugin_options[] = {
43 "color",
44 "path",
45 "no-delta",
46 "clock-cycles",
47 "clock-seconds",
48 "clock-date",
49 "clock-gmt",
50 "verbose",
51 "name-default", /* show/hide */
52 "name-payload",
53 "name-context",
54 "name-scope",
55 "name-header",
56 "field-default", /* show/hide */
57 "field-trace",
58 "field-trace:hostname",
59 "field-trace:domain",
60 "field-trace:procname",
61 "field-trace:vpid",
62 "field-loglevel",
63 "field-emf",
64 "field-callsite",
65 };
66
67 static
68 const char * const in_port_name = "in";
69
70 static
71 void destroy_pretty_data(struct pretty_component *pretty)
72 {
73 bt_self_component_port_input_message_iterator_put_ref(pretty->iterator);
74
75 if (pretty->string) {
76 (void) g_string_free(pretty->string, TRUE);
77 }
78
79 if (pretty->tmp_string) {
80 (void) g_string_free(pretty->tmp_string, TRUE);
81 }
82
83 if (pretty->out != stdout) {
84 int ret;
85
86 ret = fclose(pretty->out);
87 if (ret) {
88 perror("close output file");
89 }
90 }
91 g_free(pretty->options.output_path);
92 g_free(pretty);
93 }
94
95 static
96 struct pretty_component *create_pretty(void)
97 {
98 struct pretty_component *pretty;
99
100 pretty = g_new0(struct pretty_component, 1);
101 if (!pretty) {
102 goto end;
103 }
104 pretty->string = g_string_new("");
105 if (!pretty->string) {
106 goto error;
107 }
108 pretty->tmp_string = g_string_new("");
109 if (!pretty->tmp_string) {
110 goto error;
111 }
112 end:
113 return pretty;
114
115 error:
116 g_free(pretty);
117 return NULL;
118 }
119
120 BT_HIDDEN
121 void pretty_finalize(bt_self_component_sink *comp)
122 {
123 destroy_pretty_data(
124 bt_self_component_get_data(
125 bt_self_component_sink_as_self_component(comp)));
126 }
127
128 static
129 bt_self_component_status handle_message(
130 struct pretty_component *pretty,
131 const bt_message *message)
132 {
133 bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK;
134
135 BT_ASSERT(pretty);
136
137 switch (bt_message_get_type(message)) {
138 case BT_MESSAGE_TYPE_EVENT:
139 if (pretty_print_event(pretty, message)) {
140 ret = BT_SELF_COMPONENT_STATUS_ERROR;
141 }
142 break;
143 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
144 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
145 if (pretty_print_discarded_items(pretty, message)) {
146 ret = BT_SELF_COMPONENT_STATUS_ERROR;
147 }
148 break;
149 default:
150 break;
151 }
152
153 return ret;
154 }
155
156 BT_HIDDEN
157 bt_self_component_status pretty_graph_is_configured(
158 bt_self_component_sink *comp)
159 {
160 bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
161 struct pretty_component *pretty;
162
163 pretty = bt_self_component_get_data(
164 bt_self_component_sink_as_self_component(comp));
165 BT_ASSERT(pretty);
166 BT_ASSERT(!pretty->iterator);
167 pretty->iterator = bt_self_component_port_input_message_iterator_create(
168 bt_self_component_sink_borrow_input_port_by_name(comp,
169 in_port_name));
170 if (!pretty->iterator) {
171 status = BT_SELF_COMPONENT_STATUS_NOMEM;
172 }
173
174 return status;
175 }
176
177 BT_HIDDEN
178 bt_self_component_status pretty_consume(
179 bt_self_component_sink *comp)
180 {
181 bt_self_component_status ret;
182 bt_message_array_const msgs;
183 bt_self_component_port_input_message_iterator *it;
184 struct pretty_component *pretty = bt_self_component_get_data(
185 bt_self_component_sink_as_self_component(comp));
186 bt_message_iterator_status it_ret;
187 uint64_t count = 0;
188 uint64_t i = 0;
189
190 it = pretty->iterator;
191 it_ret = bt_self_component_port_input_message_iterator_next(it,
192 &msgs, &count);
193
194 switch (it_ret) {
195 case BT_MESSAGE_ITERATOR_STATUS_OK:
196 break;
197 case BT_MESSAGE_ITERATOR_STATUS_NOMEM:
198 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
199 goto end;
200 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
201 ret = BT_SELF_COMPONENT_STATUS_AGAIN;
202 goto end;
203 case BT_MESSAGE_ITERATOR_STATUS_END:
204 ret = BT_SELF_COMPONENT_STATUS_END;
205 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
206 pretty->iterator);
207 goto end;
208 default:
209 ret = BT_SELF_COMPONENT_STATUS_ERROR;
210 goto end;
211 }
212
213 BT_ASSERT(it_ret == BT_MESSAGE_ITERATOR_STATUS_OK);
214
215 for (i = 0; i < count; i++) {
216 ret = handle_message(pretty, msgs[i]);
217 if (ret) {
218 goto end;
219 }
220
221 bt_message_put_ref(msgs[i]);
222 }
223
224 end:
225 for (; i < count; i++) {
226 bt_message_put_ref(msgs[i]);
227 }
228
229 return ret;
230 }
231
232 static
233 int add_params_to_map(bt_value *plugin_opt_map)
234 {
235 int ret = 0;
236 unsigned int i;
237
238 for (i = 0; i < BT_ARRAY_SIZE(plugin_options); i++) {
239 const char *key = plugin_options[i];
240 bt_value_status status;
241
242 status = bt_value_map_insert_entry(plugin_opt_map, key,
243 bt_value_null);
244 switch (status) {
245 case BT_VALUE_STATUS_OK:
246 break;
247 default:
248 ret = -1;
249 goto end;
250 }
251 }
252 end:
253 return ret;
254 }
255
256 static
257 bt_bool check_param_exists(const char *key, const bt_value *object,
258 void *data)
259 {
260 struct pretty_component *pretty = data;
261
262 if (!bt_value_map_has_entry(pretty->plugin_opt_map,
263 key)) {
264 fprintf(pretty->err,
265 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key);
266 }
267 return BT_TRUE;
268 }
269
270 static
271 void apply_one_string(const char *key, const bt_value *params, char **option)
272 {
273 const bt_value *value = NULL;
274 const char *str;
275
276 value = bt_value_map_borrow_entry_value_const(params, key);
277 if (!value) {
278 goto end;
279 }
280 if (bt_value_is_null(value)) {
281 goto end;
282 }
283 str = bt_value_string_get(value);
284 *option = g_strdup(str);
285
286 end:
287 return;
288 }
289
290 static
291 void apply_one_bool(const char *key, const bt_value *params, bool *option,
292 bool *found)
293 {
294 const bt_value *value = NULL;
295 bt_bool bool_val;
296
297 value = bt_value_map_borrow_entry_value_const(params, key);
298 if (!value) {
299 goto end;
300 }
301 bool_val = bt_value_bool_get(value);
302 *option = (bool) bool_val;
303 if (found) {
304 *found = true;
305 }
306
307 end:
308 return;
309 }
310
311 static
312 void warn_wrong_color_param(struct pretty_component *pretty)
313 {
314 fprintf(pretty->err,
315 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
316 }
317
318 static
319 int open_output_file(struct pretty_component *pretty)
320 {
321 int ret = 0;
322
323 if (!pretty->options.output_path) {
324 goto end;
325 }
326
327 pretty->out = fopen(pretty->options.output_path, "w");
328 if (!pretty->out) {
329 goto error;
330 }
331
332 goto end;
333
334 error:
335 ret = -1;
336
337 end:
338 return ret;
339 }
340
341 static
342 int apply_params(struct pretty_component *pretty, const bt_value *params)
343 {
344 int ret = 0;
345 bt_value_status status;
346 bool value, found;
347 char *str = NULL;
348
349 pretty->plugin_opt_map = bt_value_map_create();
350 if (!pretty->plugin_opt_map) {
351 ret = -1;
352 goto end;
353 }
354 ret = add_params_to_map(pretty->plugin_opt_map);
355 if (ret) {
356 goto end;
357 }
358 /* Report unknown parameters. */
359 status = bt_value_map_foreach_entry_const(params,
360 check_param_exists, pretty);
361 switch (status) {
362 case BT_VALUE_STATUS_OK:
363 break;
364 default:
365 ret = -1;
366 goto end;
367 }
368 /* Known parameters. */
369 pretty->options.color = PRETTY_COLOR_OPT_AUTO;
370 if (bt_value_map_has_entry(params, "color")) {
371 const bt_value *color_value;
372 const char *color;
373
374 color_value = bt_value_map_borrow_entry_value_const(params,
375 "color");
376 if (!color_value) {
377 goto end;
378 }
379
380 color = bt_value_string_get(color_value);
381
382 if (strcmp(color, "never") == 0) {
383 pretty->options.color = PRETTY_COLOR_OPT_NEVER;
384 } else if (strcmp(color, "auto") == 0) {
385 pretty->options.color = PRETTY_COLOR_OPT_AUTO;
386 } else if (strcmp(color, "always") == 0) {
387 pretty->options.color = PRETTY_COLOR_OPT_ALWAYS;
388 } else {
389 warn_wrong_color_param(pretty);
390 }
391 }
392
393 apply_one_string("path", params, &pretty->options.output_path);
394 ret = open_output_file(pretty);
395 if (ret) {
396 goto end;
397 }
398
399 value = false; /* Default. */
400 apply_one_bool("no-delta", params, &value, NULL);
401 pretty->options.print_delta_field = !value; /* Reverse logic. */
402
403 value = false; /* Default. */
404 apply_one_bool("clock-cycles", params, &value, NULL);
405 pretty->options.print_timestamp_cycles = value;
406
407 value = false; /* Default. */
408 apply_one_bool("clock-seconds", params, &value, NULL);
409 pretty->options.clock_seconds = value;
410
411 value = false; /* Default. */
412 apply_one_bool("clock-date", params, &value, NULL);
413 pretty->options.clock_date = value;
414
415 value = false; /* Default. */
416 apply_one_bool("clock-gmt", params, &value, NULL);
417 pretty->options.clock_gmt = value;
418
419 value = false; /* Default. */
420 apply_one_bool("verbose", params, &value, NULL);
421 pretty->options.verbose = value;
422
423 /* Names. */
424 apply_one_string("name-default", params, &str);
425 if (!str) {
426 pretty->options.name_default = PRETTY_DEFAULT_UNSET;
427 } else if (!strcmp(str, "show")) {
428 pretty->options.name_default = PRETTY_DEFAULT_SHOW;
429 } else if (!strcmp(str, "hide")) {
430 pretty->options.name_default = PRETTY_DEFAULT_HIDE;
431 } else {
432 ret = -1;
433 goto end;
434 }
435 g_free(str);
436 str = NULL;
437
438 switch (pretty->options.name_default) {
439 case PRETTY_DEFAULT_UNSET:
440 pretty->options.print_payload_field_names = true;
441 pretty->options.print_context_field_names = true;
442 pretty->options.print_header_field_names = false;
443 pretty->options.print_scope_field_names = false;
444 break;
445 case PRETTY_DEFAULT_SHOW:
446 pretty->options.print_payload_field_names = true;
447 pretty->options.print_context_field_names = true;
448 pretty->options.print_header_field_names = true;
449 pretty->options.print_scope_field_names = true;
450 break;
451 case PRETTY_DEFAULT_HIDE:
452 pretty->options.print_payload_field_names = false;
453 pretty->options.print_context_field_names = false;
454 pretty->options.print_header_field_names = false;
455 pretty->options.print_scope_field_names = false;
456 break;
457 default:
458 ret = -1;
459 goto end;
460 }
461
462 value = false;
463 found = false;
464 apply_one_bool("name-payload", params, &value, &found);
465 if (found) {
466 pretty->options.print_payload_field_names = value;
467 }
468
469 value = false;
470 found = false;
471 apply_one_bool("name-context", params, &value, &found);
472 if (found) {
473 pretty->options.print_context_field_names = value;
474 }
475
476 value = false;
477 found = false;
478 apply_one_bool("name-header", params, &value, &found);
479 if (found) {
480 pretty->options.print_header_field_names = value;
481 }
482
483 value = false;
484 found = false;
485 apply_one_bool("name-scope", params, &value, &found);
486 if (found) {
487 pretty->options.print_scope_field_names = value;
488 }
489
490 /* Fields. */
491 apply_one_string("field-default", params, &str);
492 if (!str) {
493 pretty->options.field_default = PRETTY_DEFAULT_UNSET;
494 } else if (!strcmp(str, "show")) {
495 pretty->options.field_default = PRETTY_DEFAULT_SHOW;
496 } else if (!strcmp(str, "hide")) {
497 pretty->options.field_default = PRETTY_DEFAULT_HIDE;
498 } else {
499 ret = -1;
500 goto end;
501 }
502 g_free(str);
503 str = NULL;
504
505 switch (pretty->options.field_default) {
506 case PRETTY_DEFAULT_UNSET:
507 pretty->options.print_trace_field = false;
508 pretty->options.print_trace_hostname_field = true;
509 pretty->options.print_trace_domain_field = false;
510 pretty->options.print_trace_procname_field = true;
511 pretty->options.print_trace_vpid_field = true;
512 pretty->options.print_loglevel_field = false;
513 pretty->options.print_emf_field = false;
514 pretty->options.print_callsite_field = false;
515 break;
516 case PRETTY_DEFAULT_SHOW:
517 pretty->options.print_trace_field = true;
518 pretty->options.print_trace_hostname_field = true;
519 pretty->options.print_trace_domain_field = true;
520 pretty->options.print_trace_procname_field = true;
521 pretty->options.print_trace_vpid_field = true;
522 pretty->options.print_loglevel_field = true;
523 pretty->options.print_emf_field = true;
524 pretty->options.print_callsite_field = true;
525 break;
526 case PRETTY_DEFAULT_HIDE:
527 pretty->options.print_trace_field = false;
528 pretty->options.print_trace_hostname_field = false;
529 pretty->options.print_trace_domain_field = false;
530 pretty->options.print_trace_procname_field = false;
531 pretty->options.print_trace_vpid_field = false;
532 pretty->options.print_loglevel_field = false;
533 pretty->options.print_emf_field = false;
534 pretty->options.print_callsite_field = false;
535 break;
536 default:
537 ret = -1;
538 goto end;
539 }
540
541 value = false;
542 found = false;
543 apply_one_bool("field-trace", params, &value, &found);
544 if (found) {
545 pretty->options.print_trace_field = value;
546 }
547
548 value = false;
549 found = false;
550 apply_one_bool("field-trace:hostname", params, &value, &found);
551 if (found) {
552 pretty->options.print_trace_hostname_field = value;
553 }
554
555 value = false;
556 found = false;
557 apply_one_bool("field-trace:domain", params, &value, &found);
558 if (found) {
559 pretty->options.print_trace_domain_field = value;
560 }
561
562 value = false;
563 found = false;
564 apply_one_bool("field-trace:procname", params, &value, &found);
565 if (found) {
566 pretty->options.print_trace_procname_field = value;
567 }
568
569 value = false;
570 found = false;
571 apply_one_bool("field-trace:vpid", params, &value, &found);
572 if (found) {
573 pretty->options.print_trace_vpid_field = value;
574 }
575
576 value = false;
577 found = false;
578 apply_one_bool("field-loglevel", params, &value, &found);
579 if (found) {
580 pretty->options.print_loglevel_field = value;
581 }
582
583 value = false;
584 found = false;
585 apply_one_bool("field-emf", params, &value, &found);
586 if (found) {
587 pretty->options.print_emf_field = value;
588 }
589
590 value = false;
591 found = false;
592 apply_one_bool("field-callsite", params, &value, &found);
593 if (found) {
594 pretty->options.print_callsite_field = value;
595 }
596
597 end:
598 bt_value_put_ref(pretty->plugin_opt_map);
599 pretty->plugin_opt_map = NULL;
600 g_free(str);
601 return ret;
602 }
603
604 static
605 void set_use_colors(struct pretty_component *pretty)
606 {
607 switch (pretty->options.color) {
608 case PRETTY_COLOR_OPT_ALWAYS:
609 pretty->use_colors = true;
610 break;
611 case PRETTY_COLOR_OPT_AUTO:
612 pretty->use_colors = pretty->out == stdout &&
613 bt_common_colors_supported();
614 break;
615 case PRETTY_COLOR_OPT_NEVER:
616 pretty->use_colors = false;
617 break;
618 }
619 }
620
621 static
622 void init_stream_packet_context_quarks(void)
623 {
624 stream_packet_context_quarks[Q_TIMESTAMP_BEGIN] =
625 g_quark_from_string("timestamp_begin");
626 stream_packet_context_quarks[Q_TIMESTAMP_BEGIN] =
627 g_quark_from_string("timestamp_begin");
628 stream_packet_context_quarks[Q_TIMESTAMP_END] =
629 g_quark_from_string("timestamp_end");
630 stream_packet_context_quarks[Q_EVENTS_DISCARDED] =
631 g_quark_from_string("events_discarded");
632 stream_packet_context_quarks[Q_CONTENT_SIZE] =
633 g_quark_from_string("content_size");
634 stream_packet_context_quarks[Q_PACKET_SIZE] =
635 g_quark_from_string("packet_size");
636 stream_packet_context_quarks[Q_PACKET_SEQ_NUM] =
637 g_quark_from_string("packet_seq_num");
638 }
639
640 BT_HIDDEN
641 bt_self_component_status pretty_init(
642 bt_self_component_sink *comp,
643 const bt_value *params,
644 __attribute__((unused)) void *init_method_data)
645 {
646 bt_self_component_status ret;
647 struct pretty_component *pretty = create_pretty();
648
649 if (!pretty) {
650 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
651 goto end;
652 }
653
654 ret = bt_self_component_sink_add_input_port(comp, in_port_name,
655 NULL, NULL);
656 if (ret != BT_SELF_COMPONENT_STATUS_OK) {
657 goto end;
658 }
659
660 pretty->out = stdout;
661 pretty->err = stderr;
662
663 pretty->delta_cycles = -1ULL;
664 pretty->last_cycles_timestamp = -1ULL;
665
666 pretty->delta_real_timestamp = -1ULL;
667 pretty->last_real_timestamp = -1ULL;
668
669 if (apply_params(pretty, params)) {
670 ret = BT_SELF_COMPONENT_STATUS_ERROR;
671 goto error;
672 }
673
674 set_use_colors(pretty);
675 bt_self_component_set_data(
676 bt_self_component_sink_as_self_component(comp), pretty);
677 init_stream_packet_context_quarks();
678
679 end:
680 return ret;
681
682 error:
683 destroy_pretty_data(pretty);
684 return ret;
685 }
This page took 0.041554 seconds and 3 git commands to generate.