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