4 * Babeltrace CTF Text Output Plugin
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
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
30 #include <babeltrace/plugin/plugin-dev.h>
31 #include <babeltrace/component/component.h>
32 #include <babeltrace/component/private-component.h>
33 #include <babeltrace/component/component-sink.h>
34 #include <babeltrace/component/port.h>
35 #include <babeltrace/component/private-port.h>
36 #include <babeltrace/component/connection.h>
37 #include <babeltrace/component/private-connection.h>
38 #include <babeltrace/component/notification/notification.h>
39 #include <babeltrace/component/notification/iterator.h>
40 #include <babeltrace/component/notification/event.h>
41 #include <babeltrace/values.h>
42 #include <babeltrace/compiler.h>
43 #include <babeltrace/common-internal.h>
44 #include <plugins-common.h>
52 const char *plugin_options
[] = {
56 "debug-info-target-prefix",
57 "debug-info-full-path",
64 "name-default", /* show/hide */
69 "field-default", /* show/hide */
71 "field-trace:hostname",
73 "field-trace:procname",
80 void destroy_text_data(struct text_component
*text
)
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
);
91 struct text_component
*create_text(void)
93 struct text_component
*text
;
95 text
= g_new0(struct text_component
, 1);
99 text
->string
= g_string_new("");
112 void finalize_text(struct bt_private_component
*component
)
114 void *data
= bt_private_component_get_user_data(component
);
116 destroy_text_data(data
);
120 enum bt_component_status
handle_notification(struct text_component
*text
,
121 struct bt_notification
*notification
)
123 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
126 ret
= BT_COMPONENT_STATUS_ERROR
;
130 switch (bt_notification_get_type(notification
)) {
131 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
134 case BT_NOTIFICATION_TYPE_PACKET_END
:
137 case BT_NOTIFICATION_TYPE_EVENT
:
139 struct bt_ctf_event
*event
= bt_notification_event_get_event(
143 ret
= BT_COMPONENT_STATUS_ERROR
;
146 ret
= text_print_event(text
, event
);
148 if (ret
!= BT_COMPONENT_STATUS_OK
) {
153 case BT_NOTIFICATION_TYPE_STREAM_END
:
157 puts("Unhandled notification type");
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
)
169 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
170 struct bt_private_connection
*connection
;
171 struct text_component
*text
;
173 text
= bt_private_component_get_user_data(component
);
175 assert(!text
->input_iterator
);
176 connection
= bt_private_port_get_private_connection(self_port
);
178 text
->input_iterator
=
179 bt_private_connection_create_notification_iterator(connection
);
181 if (!text
->input_iterator
) {
182 ret
= BT_COMPONENT_STATUS_ERROR
;
190 enum bt_component_status
run(struct bt_private_component
*component
)
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
);
198 it
= text
->input_iterator
;
200 if (likely(text
->processed_first_event
)) {
201 enum bt_notification_iterator_status it_ret
;
203 it_ret
= bt_notification_iterator_next(it
);
205 case BT_NOTIFICATION_ITERATOR_STATUS_ERROR
:
206 ret
= BT_COMPONENT_STATUS_ERROR
;
208 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
209 ret
= BT_COMPONENT_STATUS_END
;
210 BT_PUT(text
->input_iterator
);
216 notification
= bt_notification_iterator_get_notification(it
);
218 ret
= BT_COMPONENT_STATUS_ERROR
;
222 ret
= handle_notification(text
, notification
);
223 text
->processed_first_event
= true;
225 bt_put(notification
);
230 enum bt_component_status
add_params_to_map(struct bt_value
*plugin_opt_map
)
232 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
235 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
236 const char *key
= plugin_options
[i
];
237 enum bt_value_status status
;
239 status
= bt_value_map_insert(plugin_opt_map
, key
, bt_value_null
);
241 case BT_VALUE_STATUS_OK
:
244 ret
= BT_COMPONENT_STATUS_ERROR
;
253 bool check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
255 struct text_component
*text
= data
;
256 struct bt_value
*plugin_opt_map
= text
->plugin_opt_map
;
258 if (!bt_value_map_get(plugin_opt_map
, key
)) {
260 "[warning] Parameter \"%s\" unknown to \"text\" plugin\n", key
);
266 enum bt_component_status
apply_one_string(const char *key
,
267 struct bt_value
*params
,
270 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
271 struct bt_value
*value
= NULL
;
272 enum bt_value_status status
;
275 value
= bt_value_map_get(params
, key
);
279 if (bt_value_is_null(value
)) {
282 status
= bt_value_string_get(value
, &str
);
284 case BT_VALUE_STATUS_OK
:
287 ret
= BT_COMPONENT_STATUS_ERROR
;
290 *option
= g_strdup(str
);
297 enum bt_component_status
apply_one_bool(const char *key
,
298 struct bt_value
*params
,
302 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
303 struct bt_value
*value
= NULL
;
304 enum bt_value_status status
;
306 value
= bt_value_map_get(params
, key
);
310 status
= bt_value_bool_get(value
, option
);
312 case BT_VALUE_STATUS_OK
:
315 ret
= BT_COMPONENT_STATUS_ERROR
;
327 void warn_wrong_color_param(struct text_component
*text
)
330 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
334 enum bt_component_status
apply_params(struct text_component
*text
,
335 struct bt_value
*params
)
337 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
338 enum bt_value_status status
;
342 text
->plugin_opt_map
= bt_value_map_create();
343 if (!text
->plugin_opt_map
) {
344 ret
= BT_COMPONENT_STATUS_ERROR
;
347 ret
= add_params_to_map(text
->plugin_opt_map
);
348 if (ret
!= BT_COMPONENT_STATUS_OK
) {
351 /* Report unknown parameters. */
352 status
= bt_value_map_foreach(params
, check_param_exists
, text
);
354 case BT_VALUE_STATUS_OK
:
357 ret
= BT_COMPONENT_STATUS_ERROR
;
360 /* Known parameters. */
361 text
->options
.color
= TEXT_COLOR_OPT_AUTO
;
362 if (bt_value_map_has_key(params
, "color")) {
363 struct bt_value
*color_value
;
366 color_value
= bt_value_map_get(params
, "color");
371 status
= bt_value_string_get(color_value
, &color
);
373 warn_wrong_color_param(text
);
375 if (strcmp(color
, "never") == 0) {
376 text
->options
.color
= TEXT_COLOR_OPT_NEVER
;
377 } else if (strcmp(color
, "auto") == 0) {
378 text
->options
.color
= TEXT_COLOR_OPT_AUTO
;
379 } else if (strcmp(color
, "always") == 0) {
380 text
->options
.color
= TEXT_COLOR_OPT_ALWAYS
;
382 warn_wrong_color_param(text
);
389 ret
= apply_one_string("output-path",
391 &text
->options
.output_path
);
392 if (ret
!= BT_COMPONENT_STATUS_OK
) {
396 ret
= apply_one_string("debug-info-dir",
398 &text
->options
.debug_info_dir
);
399 if (ret
!= BT_COMPONENT_STATUS_OK
) {
403 ret
= apply_one_string("debug-info-target-prefix",
405 &text
->options
.debug_info_target_prefix
);
406 if (ret
!= BT_COMPONENT_STATUS_OK
) {
410 value
= false; /* Default. */
411 ret
= apply_one_bool("debug-info-full-path", params
, &value
, NULL
);
412 if (ret
!= BT_COMPONENT_STATUS_OK
) {
415 text
->options
.debug_info_full_path
= value
;
417 value
= false; /* Default. */
418 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
419 if (ret
!= BT_COMPONENT_STATUS_OK
) {
422 text
->options
.print_delta_field
= !value
; /* Reverse logic. */
424 value
= false; /* Default. */
425 ret
= apply_one_bool("clock-cycles", params
, &value
, NULL
);
426 if (ret
!= BT_COMPONENT_STATUS_OK
) {
429 text
->options
.print_timestamp_cycles
= value
;
431 value
= false; /* Default. */
432 ret
= apply_one_bool("clock-seconds", params
, &value
, NULL
);
433 if (ret
!= BT_COMPONENT_STATUS_OK
) {
436 text
->options
.clock_seconds
= value
;
438 value
= false; /* Default. */
439 ret
= apply_one_bool("clock-date", params
, &value
, NULL
);
440 if (ret
!= BT_COMPONENT_STATUS_OK
) {
443 text
->options
.clock_date
= value
;
445 value
= false; /* Default. */
446 ret
= apply_one_bool("clock-gmt", params
, &value
, NULL
);
447 if (ret
!= BT_COMPONENT_STATUS_OK
) {
450 text
->options
.clock_gmt
= value
;
452 value
= false; /* Default. */
453 ret
= apply_one_bool("verbose", params
, &value
, NULL
);
454 if (ret
!= BT_COMPONENT_STATUS_OK
) {
457 text
->options
.verbose
= value
;
460 ret
= apply_one_string("name-default", params
, &str
);
461 if (ret
!= BT_COMPONENT_STATUS_OK
) {
465 text
->options
.name_default
= TEXT_DEFAULT_UNSET
;
466 } else if (!strcmp(str
, "show")) {
467 text
->options
.name_default
= TEXT_DEFAULT_SHOW
;
468 } else if (!strcmp(str
, "hide")) {
469 text
->options
.name_default
= TEXT_DEFAULT_HIDE
;
471 ret
= BT_COMPONENT_STATUS_ERROR
;
477 switch (text
->options
.name_default
) {
478 case TEXT_DEFAULT_UNSET
:
479 text
->options
.print_payload_field_names
= true;
480 text
->options
.print_context_field_names
= true;
481 text
->options
.print_header_field_names
= false;
482 text
->options
.print_scope_field_names
= false;
484 case TEXT_DEFAULT_SHOW
:
485 text
->options
.print_payload_field_names
= true;
486 text
->options
.print_context_field_names
= true;
487 text
->options
.print_header_field_names
= true;
488 text
->options
.print_scope_field_names
= true;
490 case TEXT_DEFAULT_HIDE
:
491 text
->options
.print_payload_field_names
= false;
492 text
->options
.print_context_field_names
= false;
493 text
->options
.print_header_field_names
= false;
494 text
->options
.print_scope_field_names
= false;
497 ret
= BT_COMPONENT_STATUS_ERROR
;
503 ret
= apply_one_bool("name-payload", params
, &value
, &found
);
504 if (ret
!= BT_COMPONENT_STATUS_OK
) {
508 text
->options
.print_payload_field_names
= value
;
513 ret
= apply_one_bool("name-context", params
, &value
, &found
);
514 if (ret
!= BT_COMPONENT_STATUS_OK
) {
518 text
->options
.print_context_field_names
= value
;
523 ret
= apply_one_bool("name-header", params
, &value
, &found
);
524 if (ret
!= BT_COMPONENT_STATUS_OK
) {
528 text
->options
.print_header_field_names
= value
;
533 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
534 if (ret
!= BT_COMPONENT_STATUS_OK
) {
538 text
->options
.print_scope_field_names
= value
;
542 ret
= apply_one_string("field-default", params
, &str
);
543 if (ret
!= BT_COMPONENT_STATUS_OK
) {
547 text
->options
.field_default
= TEXT_DEFAULT_UNSET
;
548 } else if (!strcmp(str
, "show")) {
549 text
->options
.field_default
= TEXT_DEFAULT_SHOW
;
550 } else if (!strcmp(str
, "hide")) {
551 text
->options
.field_default
= TEXT_DEFAULT_HIDE
;
553 ret
= BT_COMPONENT_STATUS_ERROR
;
559 switch (text
->options
.field_default
) {
560 case TEXT_DEFAULT_UNSET
:
561 text
->options
.print_trace_field
= false;
562 text
->options
.print_trace_hostname_field
= true;
563 text
->options
.print_trace_domain_field
= false;
564 text
->options
.print_trace_procname_field
= true;
565 text
->options
.print_trace_vpid_field
= true;
566 text
->options
.print_loglevel_field
= false;
567 text
->options
.print_emf_field
= false;
568 text
->options
.print_emf_field
= false;
570 case TEXT_DEFAULT_SHOW
:
571 text
->options
.print_trace_field
= true;
572 text
->options
.print_trace_hostname_field
= true;
573 text
->options
.print_trace_domain_field
= true;
574 text
->options
.print_trace_procname_field
= true;
575 text
->options
.print_trace_vpid_field
= true;
576 text
->options
.print_loglevel_field
= true;
577 text
->options
.print_emf_field
= true;
578 text
->options
.print_emf_field
= true;
580 case TEXT_DEFAULT_HIDE
:
581 text
->options
.print_trace_field
= false;
582 text
->options
.print_trace_hostname_field
= false;
583 text
->options
.print_trace_domain_field
= false;
584 text
->options
.print_trace_procname_field
= false;
585 text
->options
.print_trace_vpid_field
= false;
586 text
->options
.print_loglevel_field
= false;
587 text
->options
.print_emf_field
= false;
588 text
->options
.print_emf_field
= false;
591 ret
= BT_COMPONENT_STATUS_ERROR
;
597 ret
= apply_one_bool("field-trace", params
, &value
, &found
);
598 if (ret
!= BT_COMPONENT_STATUS_OK
) {
602 text
->options
.print_trace_field
= value
;
607 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
608 if (ret
!= BT_COMPONENT_STATUS_OK
) {
612 text
->options
.print_trace_hostname_field
= value
;
617 ret
= apply_one_bool("field-trace:domain", params
, &value
, &found
);
618 if (ret
!= BT_COMPONENT_STATUS_OK
) {
622 text
->options
.print_trace_domain_field
= value
;
627 ret
= apply_one_bool("field-trace:procname", params
, &value
, &found
);
628 if (ret
!= BT_COMPONENT_STATUS_OK
) {
632 text
->options
.print_trace_procname_field
= value
;
637 ret
= apply_one_bool("field-trace:vpid", params
, &value
, &found
);
638 if (ret
!= BT_COMPONENT_STATUS_OK
) {
642 text
->options
.print_trace_vpid_field
= value
;
647 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
648 if (ret
!= BT_COMPONENT_STATUS_OK
) {
652 text
->options
.print_loglevel_field
= value
;
657 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
658 if (ret
!= BT_COMPONENT_STATUS_OK
) {
662 text
->options
.print_emf_field
= value
;
667 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
668 if (ret
!= BT_COMPONENT_STATUS_OK
) {
672 text
->options
.print_emf_field
= value
;
676 bt_put(text
->plugin_opt_map
);
677 text
->plugin_opt_map
= NULL
;
683 void set_use_colors(struct text_component
*text
)
685 switch (text
->options
.color
) {
686 case TEXT_COLOR_OPT_ALWAYS
:
687 text
->use_colors
= true;
689 case TEXT_COLOR_OPT_AUTO
:
690 text
->use_colors
= text
->out
== stdout
&&
691 bt_common_colors_supported();
693 case TEXT_COLOR_OPT_NEVER
:
694 text
->use_colors
= false;
700 void init_stream_packet_context_quarks(void)
702 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
703 g_quark_from_string("timestamp_begin");
704 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
705 g_quark_from_string("timestamp_begin");
706 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
707 g_quark_from_string("timestamp_end");
708 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
709 g_quark_from_string("events_discarded");
710 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
711 g_quark_from_string("content_size");
712 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
713 g_quark_from_string("packet_size");
714 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
715 g_quark_from_string("packet_seq_num");
719 enum bt_component_status
text_component_init(
720 struct bt_private_component
*component
,
721 struct bt_value
*params
,
722 UNUSED_VAR
void *init_method_data
)
724 enum bt_component_status ret
;
725 struct text_component
*text
= create_text();
728 ret
= BT_COMPONENT_STATUS_NOMEM
;
735 text
->delta_cycles
= -1ULL;
736 text
->last_cycles_timestamp
= -1ULL;
738 text
->delta_real_timestamp
= -1ULL;
739 text
->last_real_timestamp
= -1ULL;
741 ret
= apply_params(text
, params
);
742 if (ret
!= BT_COMPONENT_STATUS_OK
) {
746 set_use_colors(text
);
748 ret
= bt_private_component_set_user_data(component
, text
);
749 if (ret
!= BT_COMPONENT_STATUS_OK
) {
753 init_stream_packet_context_quarks();
758 destroy_text_data(text
);
762 /* Initialize plug-in entry points. */
764 BT_PLUGIN_DESCRIPTION("Babeltrace text output plug-in.");
765 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
766 BT_PLUGIN_LICENSE("MIT");
767 BT_PLUGIN_SINK_COMPONENT_CLASS(text
, run
);
768 BT_PLUGIN_SINK_COMPONENT_CLASS_INIT_METHOD(text
, text_component_init
);
769 BT_PLUGIN_SINK_COMPONENT_CLASS_ACCEPT_PORT_CONNECTION_METHOD(text
, text_accept_port_connection
);
770 BT_PLUGIN_SINK_COMPONENT_CLASS_FINALIZE_METHOD(text
, finalize_text
);
771 BT_PLUGIN_SINK_COMPONENT_CLASS_DESCRIPTION(text
,
772 "Formats CTF-IR to text. Formerly known as ctf-text.");