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