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