Text output identical with babeltrace 1
[babeltrace.git] / plugins / text / text.c
1 /*
2 * text.c
3 *
4 * Babeltrace CTF Text Output Plugin
5 *
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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/plugin/plugin-dev.h>
31 #include <babeltrace/graph/component.h>
32 #include <babeltrace/graph/private-component.h>
33 #include <babeltrace/graph/component-sink.h>
34 #include <babeltrace/graph/port.h>
35 #include <babeltrace/graph/private-port.h>
36 #include <babeltrace/graph/connection.h>
37 #include <babeltrace/graph/private-connection.h>
38 #include <babeltrace/graph/notification.h>
39 #include <babeltrace/graph/notification-iterator.h>
40 #include <babeltrace/graph/notification-event.h>
41 #include <babeltrace/values.h>
42 #include <babeltrace/compiler.h>
43 #include <babeltrace/common-internal.h>
44 #include <plugins-common.h>
45 #include <stdio.h>
46 #include <stdbool.h>
47 #include <glib.h>
48 #include "text.h"
49 #include <assert.h>
50
51 static
52 const char *plugin_options[] = {
53 "color",
54 "output-path",
55 "debug-info-dir",
56 "debug-info-target-prefix",
57 "debug-info-full-path",
58 "no-delta",
59 "clock-cycles",
60 "clock-seconds",
61 "clock-date",
62 "clock-gmt",
63 "verbose",
64 "name-default", /* show/hide */
65 "name-payload",
66 "name-context",
67 "name-scope",
68 "name-header",
69 "field-default", /* show/hide */
70 "field-trace",
71 "field-trace:hostname",
72 "field-trace:domain",
73 "field-trace:procname",
74 "field-trace:vpid",
75 "field-loglevel",
76 "field-emf",
77 "field-callsite",
78 };
79
80 static
81 void destroy_text_data(struct text_component *text)
82 {
83 bt_put(text->input_iterator);
84 (void) g_string_free(text->string, TRUE);
85 g_free(text->options.output_path);
86 g_free(text->options.debug_info_dir);
87 g_free(text->options.debug_info_target_prefix);
88 g_free(text);
89 }
90
91 static
92 struct text_component *create_text(void)
93 {
94 struct text_component *text;
95
96 text = g_new0(struct text_component, 1);
97 if (!text) {
98 goto end;
99 }
100 text->string = g_string_new("");
101 if (!text->string) {
102 goto error;
103 }
104 end:
105 return text;
106
107 error:
108 g_free(text);
109 return NULL;
110 }
111
112 static
113 void finalize_text(struct bt_private_component *component)
114 {
115 void *data = bt_private_component_get_user_data(component);
116
117 destroy_text_data(data);
118 }
119
120 static
121 enum bt_component_status handle_notification(struct text_component *text,
122 struct bt_notification *notification)
123 {
124 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
125
126 if (!text) {
127 ret = BT_COMPONENT_STATUS_ERROR;
128 goto end;
129 }
130
131 switch (bt_notification_get_type(notification)) {
132 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
133 break;
134 case BT_NOTIFICATION_TYPE_PACKET_END:
135 break;
136 case BT_NOTIFICATION_TYPE_EVENT:
137 {
138 struct bt_ctf_event *event = bt_notification_event_get_event(
139 notification);
140
141 if (!event) {
142 ret = BT_COMPONENT_STATUS_ERROR;
143 goto end;
144 }
145 ret = text_print_event(text, event);
146 bt_put(event);
147 if (ret != BT_COMPONENT_STATUS_OK) {
148 goto end;
149 }
150 break;
151 }
152 case BT_NOTIFICATION_TYPE_STREAM_END:
153 break;
154 default:
155 puts("Unhandled notification type");
156 }
157 end:
158 return ret;
159 }
160
161 static
162 enum bt_component_status text_accept_port_connection(
163 struct bt_private_component *component,
164 struct bt_private_port *self_port,
165 struct bt_port *other_port)
166 {
167 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
168 struct bt_private_connection *connection;
169 struct text_component *text;
170
171 text = bt_private_component_get_user_data(component);
172 assert(text);
173 assert(!text->input_iterator);
174 connection = bt_private_port_get_private_connection(self_port);
175 assert(connection);
176 text->input_iterator =
177 bt_private_connection_create_notification_iterator(connection);
178
179 if (!text->input_iterator) {
180 ret = BT_COMPONENT_STATUS_ERROR;
181 }
182
183 bt_put(connection);
184 return ret;
185 }
186
187 static
188 enum bt_component_status run(struct bt_private_component *component)
189 {
190 enum bt_component_status ret;
191 struct bt_notification *notification = NULL;
192 struct bt_notification_iterator *it;
193 struct text_component *text =
194 bt_private_component_get_user_data(component);
195 enum bt_notification_iterator_status it_ret;
196
197 it = text->input_iterator;
198
199 it_ret = bt_notification_iterator_next(it);
200 switch (it_ret) {
201 case BT_NOTIFICATION_ITERATOR_STATUS_ERROR:
202 ret = BT_COMPONENT_STATUS_ERROR;
203 goto end;
204 case BT_NOTIFICATION_ITERATOR_STATUS_END:
205 ret = BT_COMPONENT_STATUS_END;
206 BT_PUT(text->input_iterator);
207 goto end;
208 default:
209 break;
210 }
211
212 notification = bt_notification_iterator_get_notification(it);
213 assert(notification);
214 ret = handle_notification(text, notification);
215 text->processed_first_event = true;
216 end:
217 bt_put(notification);
218 return ret;
219 }
220
221 static
222 enum bt_component_status add_params_to_map(struct bt_value *plugin_opt_map)
223 {
224 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
225 unsigned int i;
226
227 for (i = 0; i < BT_ARRAY_SIZE(plugin_options); i++) {
228 const char *key = plugin_options[i];
229 enum bt_value_status status;
230
231 status = bt_value_map_insert(plugin_opt_map, key, bt_value_null);
232 switch (status) {
233 case BT_VALUE_STATUS_OK:
234 break;
235 default:
236 ret = BT_COMPONENT_STATUS_ERROR;
237 goto end;
238 }
239 }
240 end:
241 return ret;
242 }
243
244 static
245 bool check_param_exists(const char *key, struct bt_value *object, void *data)
246 {
247 struct text_component *text = data;
248 struct bt_value *plugin_opt_map = text->plugin_opt_map;
249
250 if (!bt_value_map_get(plugin_opt_map, key)) {
251 fprintf(text->err,
252 "[warning] Parameter \"%s\" unknown to \"text\" plugin\n", key);
253 }
254 return true;
255 }
256
257 static
258 enum bt_component_status apply_one_string(const char *key,
259 struct bt_value *params,
260 char **option)
261 {
262 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
263 struct bt_value *value = NULL;
264 enum bt_value_status status;
265 const char *str;
266
267 value = bt_value_map_get(params, key);
268 if (!value) {
269 goto end;
270 }
271 if (bt_value_is_null(value)) {
272 goto end;
273 }
274 status = bt_value_string_get(value, &str);
275 switch (status) {
276 case BT_VALUE_STATUS_OK:
277 break;
278 default:
279 ret = BT_COMPONENT_STATUS_ERROR;
280 goto end;
281 }
282 *option = g_strdup(str);
283 end:
284 bt_put(value);
285 return ret;
286 }
287
288 static
289 enum bt_component_status apply_one_bool(const char *key,
290 struct bt_value *params,
291 bool *option,
292 bool *found)
293 {
294 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
295 struct bt_value *value = NULL;
296 enum bt_value_status status;
297
298 value = bt_value_map_get(params, key);
299 if (!value) {
300 goto end;
301 }
302 status = bt_value_bool_get(value, option);
303 switch (status) {
304 case BT_VALUE_STATUS_OK:
305 break;
306 default:
307 ret = BT_COMPONENT_STATUS_ERROR;
308 goto end;
309 }
310 if (found) {
311 *found = true;
312 }
313 end:
314 bt_put(value);
315 return ret;
316 }
317
318 static
319 void warn_wrong_color_param(struct text_component *text)
320 {
321 fprintf(text->err,
322 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
323 }
324
325 static
326 enum bt_component_status apply_params(struct text_component *text,
327 struct bt_value *params)
328 {
329 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
330 enum bt_value_status status;
331 bool value, found;
332 char *str = NULL;
333
334 text->plugin_opt_map = bt_value_map_create();
335 if (!text->plugin_opt_map) {
336 ret = BT_COMPONENT_STATUS_ERROR;
337 goto end;
338 }
339 ret = add_params_to_map(text->plugin_opt_map);
340 if (ret != BT_COMPONENT_STATUS_OK) {
341 goto end;
342 }
343 /* Report unknown parameters. */
344 status = bt_value_map_foreach(params, check_param_exists, text);
345 switch (status) {
346 case BT_VALUE_STATUS_OK:
347 break;
348 default:
349 ret = BT_COMPONENT_STATUS_ERROR;
350 goto end;
351 }
352 /* Known parameters. */
353 text->options.color = TEXT_COLOR_OPT_AUTO;
354 if (bt_value_map_has_key(params, "color")) {
355 struct bt_value *color_value;
356 const char *color;
357
358 color_value = bt_value_map_get(params, "color");
359 if (!color_value) {
360 goto end;
361 }
362
363 status = bt_value_string_get(color_value, &color);
364 if (status) {
365 warn_wrong_color_param(text);
366 } else {
367 if (strcmp(color, "never") == 0) {
368 text->options.color = TEXT_COLOR_OPT_NEVER;
369 } else if (strcmp(color, "auto") == 0) {
370 text->options.color = TEXT_COLOR_OPT_AUTO;
371 } else if (strcmp(color, "always") == 0) {
372 text->options.color = TEXT_COLOR_OPT_ALWAYS;
373 } else {
374 warn_wrong_color_param(text);
375 }
376 }
377
378 bt_put(color_value);
379 }
380
381 ret = apply_one_string("output-path",
382 params,
383 &text->options.output_path);
384 if (ret != BT_COMPONENT_STATUS_OK) {
385 goto end;
386 }
387
388 ret = apply_one_string("debug-info-dir",
389 params,
390 &text->options.debug_info_dir);
391 if (ret != BT_COMPONENT_STATUS_OK) {
392 goto end;
393 }
394
395 ret = apply_one_string("debug-info-target-prefix",
396 params,
397 &text->options.debug_info_target_prefix);
398 if (ret != BT_COMPONENT_STATUS_OK) {
399 goto end;
400 }
401
402 value = false; /* Default. */
403 ret = apply_one_bool("debug-info-full-path", params, &value, NULL);
404 if (ret != BT_COMPONENT_STATUS_OK) {
405 goto end;
406 }
407 text->options.debug_info_full_path = value;
408
409 value = false; /* Default. */
410 ret = apply_one_bool("no-delta", params, &value, NULL);
411 if (ret != BT_COMPONENT_STATUS_OK) {
412 goto end;
413 }
414 text->options.print_delta_field = !value; /* Reverse logic. */
415
416 value = false; /* Default. */
417 ret = apply_one_bool("clock-cycles", params, &value, NULL);
418 if (ret != BT_COMPONENT_STATUS_OK) {
419 goto end;
420 }
421 text->options.print_timestamp_cycles = value;
422
423 value = false; /* Default. */
424 ret = apply_one_bool("clock-seconds", params, &value, NULL);
425 if (ret != BT_COMPONENT_STATUS_OK) {
426 goto end;
427 }
428 text->options.clock_seconds = value;
429
430 value = false; /* Default. */
431 ret = apply_one_bool("clock-date", params, &value, NULL);
432 if (ret != BT_COMPONENT_STATUS_OK) {
433 goto end;
434 }
435 text->options.clock_date = value;
436
437 value = false; /* Default. */
438 ret = apply_one_bool("clock-gmt", params, &value, NULL);
439 if (ret != BT_COMPONENT_STATUS_OK) {
440 goto end;
441 }
442 text->options.clock_gmt = value;
443
444 value = false; /* Default. */
445 ret = apply_one_bool("verbose", params, &value, NULL);
446 if (ret != BT_COMPONENT_STATUS_OK) {
447 goto end;
448 }
449 text->options.verbose = value;
450
451 /* Names. */
452 ret = apply_one_string("name-default", params, &str);
453 if (ret != BT_COMPONENT_STATUS_OK) {
454 goto end;
455 }
456 if (!str) {
457 text->options.name_default = TEXT_DEFAULT_UNSET;
458 } else if (!strcmp(str, "show")) {
459 text->options.name_default = TEXT_DEFAULT_SHOW;
460 } else if (!strcmp(str, "hide")) {
461 text->options.name_default = TEXT_DEFAULT_HIDE;
462 } else {
463 ret = BT_COMPONENT_STATUS_ERROR;
464 goto end;
465 }
466 g_free(str);
467 str = NULL;
468
469 switch (text->options.name_default) {
470 case TEXT_DEFAULT_UNSET:
471 text->options.print_payload_field_names = true;
472 text->options.print_context_field_names = true;
473 text->options.print_header_field_names = false;
474 text->options.print_scope_field_names = false;
475 break;
476 case TEXT_DEFAULT_SHOW:
477 text->options.print_payload_field_names = true;
478 text->options.print_context_field_names = true;
479 text->options.print_header_field_names = true;
480 text->options.print_scope_field_names = true;
481 break;
482 case TEXT_DEFAULT_HIDE:
483 text->options.print_payload_field_names = false;
484 text->options.print_context_field_names = false;
485 text->options.print_header_field_names = false;
486 text->options.print_scope_field_names = false;
487 break;
488 default:
489 ret = BT_COMPONENT_STATUS_ERROR;
490 goto end;
491 }
492
493 value = false;
494 found = false;
495 ret = apply_one_bool("name-payload", params, &value, &found);
496 if (ret != BT_COMPONENT_STATUS_OK) {
497 goto end;
498 }
499 if (found) {
500 text->options.print_payload_field_names = value;
501 }
502
503 value = false;
504 found = false;
505 ret = apply_one_bool("name-context", params, &value, &found);
506 if (ret != BT_COMPONENT_STATUS_OK) {
507 goto end;
508 }
509 if (found) {
510 text->options.print_context_field_names = value;
511 }
512
513 value = false;
514 found = false;
515 ret = apply_one_bool("name-header", params, &value, &found);
516 if (ret != BT_COMPONENT_STATUS_OK) {
517 goto end;
518 }
519 if (found) {
520 text->options.print_header_field_names = value;
521 }
522
523 value = false;
524 found = false;
525 ret = apply_one_bool("name-scope", params, &value, &found);
526 if (ret != BT_COMPONENT_STATUS_OK) {
527 goto end;
528 }
529 if (found) {
530 text->options.print_scope_field_names = value;
531 }
532
533 /* Fields. */
534 ret = apply_one_string("field-default", params, &str);
535 if (ret != BT_COMPONENT_STATUS_OK) {
536 goto end;
537 }
538 if (!str) {
539 text->options.field_default = TEXT_DEFAULT_UNSET;
540 } else if (!strcmp(str, "show")) {
541 text->options.field_default = TEXT_DEFAULT_SHOW;
542 } else if (!strcmp(str, "hide")) {
543 text->options.field_default = TEXT_DEFAULT_HIDE;
544 } else {
545 ret = BT_COMPONENT_STATUS_ERROR;
546 goto end;
547 }
548 g_free(str);
549 str = NULL;
550
551 switch (text->options.field_default) {
552 case TEXT_DEFAULT_UNSET:
553 text->options.print_trace_field = false;
554 text->options.print_trace_hostname_field = true;
555 text->options.print_trace_domain_field = false;
556 text->options.print_trace_procname_field = true;
557 text->options.print_trace_vpid_field = true;
558 text->options.print_loglevel_field = false;
559 text->options.print_emf_field = false;
560 text->options.print_callsite_field = false;
561 break;
562 case TEXT_DEFAULT_SHOW:
563 text->options.print_trace_field = true;
564 text->options.print_trace_hostname_field = true;
565 text->options.print_trace_domain_field = true;
566 text->options.print_trace_procname_field = true;
567 text->options.print_trace_vpid_field = true;
568 text->options.print_loglevel_field = true;
569 text->options.print_emf_field = true;
570 text->options.print_callsite_field = true;
571 break;
572 case TEXT_DEFAULT_HIDE:
573 text->options.print_trace_field = false;
574 text->options.print_trace_hostname_field = false;
575 text->options.print_trace_domain_field = false;
576 text->options.print_trace_procname_field = false;
577 text->options.print_trace_vpid_field = false;
578 text->options.print_loglevel_field = false;
579 text->options.print_emf_field = false;
580 text->options.print_callsite_field = false;
581 break;
582 default:
583 ret = BT_COMPONENT_STATUS_ERROR;
584 goto end;
585 }
586
587 value = false;
588 found = false;
589 ret = apply_one_bool("field-trace", params, &value, &found);
590 if (ret != BT_COMPONENT_STATUS_OK) {
591 goto end;
592 }
593 if (found) {
594 text->options.print_trace_field = value;
595 }
596
597 value = false;
598 found = false;
599 ret = apply_one_bool("field-trace:hostname", params, &value, &found);
600 if (ret != BT_COMPONENT_STATUS_OK) {
601 goto end;
602 }
603 if (found) {
604 text->options.print_trace_hostname_field = value;
605 }
606
607 value = false;
608 found = false;
609 ret = apply_one_bool("field-trace:domain", params, &value, &found);
610 if (ret != BT_COMPONENT_STATUS_OK) {
611 goto end;
612 }
613 if (found) {
614 text->options.print_trace_domain_field = value;
615 }
616
617 value = false;
618 found = false;
619 ret = apply_one_bool("field-trace:procname", params, &value, &found);
620 if (ret != BT_COMPONENT_STATUS_OK) {
621 goto end;
622 }
623 if (found) {
624 text->options.print_trace_procname_field = value;
625 }
626
627 value = false;
628 found = false;
629 ret = apply_one_bool("field-trace:vpid", params, &value, &found);
630 if (ret != BT_COMPONENT_STATUS_OK) {
631 goto end;
632 }
633 if (found) {
634 text->options.print_trace_vpid_field = value;
635 }
636
637 value = false;
638 found = false;
639 ret = apply_one_bool("field-loglevel", params, &value, &found);
640 if (ret != BT_COMPONENT_STATUS_OK) {
641 goto end;
642 }
643 if (found) {
644 text->options.print_loglevel_field = value;
645 }
646
647 value = false;
648 found = false;
649 ret = apply_one_bool("field-emf", params, &value, &found);
650 if (ret != BT_COMPONENT_STATUS_OK) {
651 goto end;
652 }
653 if (found) {
654 text->options.print_emf_field = value;
655 }
656
657 value = false;
658 found = false;
659 ret = apply_one_bool("field-callsite", params, &value, &found);
660 if (ret != BT_COMPONENT_STATUS_OK) {
661 goto end;
662 }
663 if (found) {
664 text->options.print_callsite_field = value;
665 }
666
667 end:
668 bt_put(text->plugin_opt_map);
669 text->plugin_opt_map = NULL;
670 g_free(str);
671 return ret;
672 }
673
674 static
675 void set_use_colors(struct text_component *text)
676 {
677 switch (text->options.color) {
678 case TEXT_COLOR_OPT_ALWAYS:
679 text->use_colors = true;
680 break;
681 case TEXT_COLOR_OPT_AUTO:
682 text->use_colors = text->out == stdout &&
683 bt_common_colors_supported();
684 break;
685 case TEXT_COLOR_OPT_NEVER:
686 text->use_colors = false;
687 break;
688 }
689 }
690
691 static
692 void init_stream_packet_context_quarks(void)
693 {
694 stream_packet_context_quarks[Q_TIMESTAMP_BEGIN] =
695 g_quark_from_string("timestamp_begin");
696 stream_packet_context_quarks[Q_TIMESTAMP_BEGIN] =
697 g_quark_from_string("timestamp_begin");
698 stream_packet_context_quarks[Q_TIMESTAMP_END] =
699 g_quark_from_string("timestamp_end");
700 stream_packet_context_quarks[Q_EVENTS_DISCARDED] =
701 g_quark_from_string("events_discarded");
702 stream_packet_context_quarks[Q_CONTENT_SIZE] =
703 g_quark_from_string("content_size");
704 stream_packet_context_quarks[Q_PACKET_SIZE] =
705 g_quark_from_string("packet_size");
706 stream_packet_context_quarks[Q_PACKET_SEQ_NUM] =
707 g_quark_from_string("packet_seq_num");
708 }
709
710 static
711 enum bt_component_status text_component_init(
712 struct bt_private_component *component,
713 struct bt_value *params,
714 UNUSED_VAR void *init_method_data)
715 {
716 enum bt_component_status ret;
717 struct text_component *text = create_text();
718
719 if (!text) {
720 ret = BT_COMPONENT_STATUS_NOMEM;
721 goto end;
722 }
723
724 text->out = stdout;
725 text->err = stderr;
726
727 text->delta_cycles = -1ULL;
728 text->last_cycles_timestamp = -1ULL;
729
730 text->delta_real_timestamp = -1ULL;
731 text->last_real_timestamp = -1ULL;
732
733 ret = apply_params(text, params);
734 if (ret != BT_COMPONENT_STATUS_OK) {
735 goto error;
736 }
737
738 set_use_colors(text);
739
740 ret = bt_private_component_set_user_data(component, text);
741 if (ret != BT_COMPONENT_STATUS_OK) {
742 goto error;
743 }
744
745 init_stream_packet_context_quarks();
746
747 end:
748 return ret;
749 error:
750 destroy_text_data(text);
751 return ret;
752 }
753
754 /* Initialize plug-in entry points. */
755 BT_PLUGIN(text);
756 BT_PLUGIN_DESCRIPTION("Babeltrace text output plug-in.");
757 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
758 BT_PLUGIN_LICENSE("MIT");
759 BT_PLUGIN_SINK_COMPONENT_CLASS(text, run);
760 BT_PLUGIN_SINK_COMPONENT_CLASS_INIT_METHOD(text, text_component_init);
761 BT_PLUGIN_SINK_COMPONENT_CLASS_ACCEPT_PORT_CONNECTION_METHOD(text, text_accept_port_connection);
762 BT_PLUGIN_SINK_COMPONENT_CLASS_FINALIZE_METHOD(text, finalize_text);
763 BT_PLUGIN_SINK_COMPONENT_CLASS_DESCRIPTION(text,
764 "Formats CTF-IR to text. Formerly known as ctf-text.");
This page took 0.044752 seconds and 4 git commands to generate.