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/component-sink.h>
33 #include <babeltrace/component/port.h>
34 #include <babeltrace/component/connection.h>
35 #include <babeltrace/component/notification/notification.h>
36 #include <babeltrace/component/notification/iterator.h>
37 #include <babeltrace/component/notification/event.h>
38 #include <babeltrace/values.h>
39 #include <babeltrace/compiler.h>
40 #include <babeltrace/common-internal.h>
41 #include <plugins-common.h>
49 const char *plugin_options
[] = {
53 "debug-info-target-prefix",
54 "debug-info-full-path",
61 "name-default", /* show/hide */
66 "field-default", /* show/hide */
68 "field-trace:hostname",
70 "field-trace:procname",
77 void destroy_text_data(struct text_component
*text
)
79 bt_put(text
->input_iterator
);
80 (void) g_string_free(text
->string
, TRUE
);
81 g_free(text
->options
.output_path
);
82 g_free(text
->options
.debug_info_dir
);
83 g_free(text
->options
.debug_info_target_prefix
);
88 struct text_component
*create_text(void)
90 struct text_component
*text
;
92 text
= g_new0(struct text_component
, 1);
96 text
->string
= g_string_new("");
109 void destroy_text(struct bt_component
*component
)
111 void *data
= bt_component_get_private_data(component
);
113 destroy_text_data(data
);
117 enum bt_component_status
handle_notification(struct text_component
*text
,
118 struct bt_notification
*notification
)
120 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
123 ret
= BT_COMPONENT_STATUS_ERROR
;
127 switch (bt_notification_get_type(notification
)) {
128 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
131 case BT_NOTIFICATION_TYPE_PACKET_END
:
134 case BT_NOTIFICATION_TYPE_EVENT
:
136 struct bt_ctf_event
*event
= bt_notification_event_get_event(
140 ret
= BT_COMPONENT_STATUS_ERROR
;
143 ret
= text_print_event(text
, event
);
145 if (ret
!= BT_COMPONENT_STATUS_OK
) {
150 case BT_NOTIFICATION_TYPE_STREAM_END
:
154 puts("Unhandled notification type");
161 enum bt_component_status
text_new_connection(struct bt_port
*own_port
,
162 struct bt_connection
*connection
)
164 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
165 struct bt_component
*component
;
166 struct text_component
*text
;
168 component
= bt_port_get_component(own_port
);
170 text
= bt_component_get_private_data(component
);
172 assert(!text
->input_iterator
);
173 text
->input_iterator
= bt_connection_create_notification_iterator(
176 if (!text
->input_iterator
) {
177 ret
= BT_COMPONENT_STATUS_ERROR
;
184 enum bt_component_status
run(struct bt_component
*component
)
186 enum bt_component_status ret
;
187 struct bt_notification
*notification
= NULL
;
188 struct bt_notification_iterator
*it
;
189 struct text_component
*text
= bt_component_get_private_data(component
);
191 it
= text
->input_iterator
;
193 if (likely(text
->processed_first_event
)) {
194 enum bt_notification_iterator_status it_ret
;
196 it_ret
= bt_notification_iterator_next(it
);
198 case BT_NOTIFICATION_ITERATOR_STATUS_ERROR
:
199 ret
= BT_COMPONENT_STATUS_ERROR
;
201 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
202 ret
= BT_COMPONENT_STATUS_END
;
203 BT_PUT(text
->input_iterator
);
209 notification
= bt_notification_iterator_get_notification(it
);
211 ret
= BT_COMPONENT_STATUS_ERROR
;
215 ret
= handle_notification(text
, notification
);
216 text
->processed_first_event
= true;
218 bt_put(notification
);
223 enum bt_component_status
add_params_to_map(struct bt_value
*plugin_opt_map
)
225 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
228 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
229 const char *key
= plugin_options
[i
];
230 enum bt_value_status status
;
232 status
= bt_value_map_insert(plugin_opt_map
, key
, bt_value_null
);
234 case BT_VALUE_STATUS_OK
:
237 ret
= BT_COMPONENT_STATUS_ERROR
;
246 bool check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
248 struct text_component
*text
= data
;
249 struct bt_value
*plugin_opt_map
= text
->plugin_opt_map
;
251 if (!bt_value_map_get(plugin_opt_map
, key
)) {
253 "[warning] Parameter \"%s\" unknown to \"text\" plugin\n", key
);
259 enum bt_component_status
apply_one_string(const char *key
,
260 struct bt_value
*params
,
263 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
264 struct bt_value
*value
= NULL
;
265 enum bt_value_status status
;
268 value
= bt_value_map_get(params
, key
);
272 if (bt_value_is_null(value
)) {
275 status
= bt_value_string_get(value
, &str
);
277 case BT_VALUE_STATUS_OK
:
280 ret
= BT_COMPONENT_STATUS_ERROR
;
283 *option
= g_strdup(str
);
290 enum bt_component_status
apply_one_bool(const char *key
,
291 struct bt_value
*params
,
295 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
296 struct bt_value
*value
= NULL
;
297 enum bt_value_status status
;
299 value
= bt_value_map_get(params
, key
);
303 status
= bt_value_bool_get(value
, option
);
305 case BT_VALUE_STATUS_OK
:
308 ret
= BT_COMPONENT_STATUS_ERROR
;
320 void warn_wrong_color_param(struct text_component
*text
)
323 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
327 enum bt_component_status
apply_params(struct text_component
*text
,
328 struct bt_value
*params
)
330 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
331 enum bt_value_status status
;
335 text
->plugin_opt_map
= bt_value_map_create();
336 if (!text
->plugin_opt_map
) {
337 ret
= BT_COMPONENT_STATUS_ERROR
;
340 ret
= add_params_to_map(text
->plugin_opt_map
);
341 if (ret
!= BT_COMPONENT_STATUS_OK
) {
344 /* Report unknown parameters. */
345 status
= bt_value_map_foreach(params
, check_param_exists
, text
);
347 case BT_VALUE_STATUS_OK
:
350 ret
= BT_COMPONENT_STATUS_ERROR
;
353 /* Known parameters. */
354 text
->options
.color
= TEXT_COLOR_OPT_AUTO
;
355 if (bt_value_map_has_key(params
, "color")) {
356 struct bt_value
*color_value
;
359 color_value
= bt_value_map_get(params
, "color");
364 ret
= bt_value_string_get(color_value
, &color
);
366 warn_wrong_color_param(text
);
368 if (strcmp(color
, "never") == 0) {
369 text
->options
.color
= TEXT_COLOR_OPT_NEVER
;
370 } else if (strcmp(color
, "auto") == 0) {
371 text
->options
.color
= TEXT_COLOR_OPT_AUTO
;
372 } else if (strcmp(color
, "always") == 0) {
373 text
->options
.color
= TEXT_COLOR_OPT_ALWAYS
;
375 warn_wrong_color_param(text
);
382 ret
= apply_one_string("output-path",
384 &text
->options
.output_path
);
385 if (ret
!= BT_COMPONENT_STATUS_OK
) {
389 ret
= apply_one_string("debug-info-dir",
391 &text
->options
.debug_info_dir
);
392 if (ret
!= BT_COMPONENT_STATUS_OK
) {
396 ret
= apply_one_string("debug-info-target-prefix",
398 &text
->options
.debug_info_target_prefix
);
399 if (ret
!= BT_COMPONENT_STATUS_OK
) {
403 value
= false; /* Default. */
404 ret
= apply_one_bool("debug-info-full-path", params
, &value
, NULL
);
405 if (ret
!= BT_COMPONENT_STATUS_OK
) {
408 text
->options
.debug_info_full_path
= value
;
410 value
= false; /* Default. */
411 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
412 if (ret
!= BT_COMPONENT_STATUS_OK
) {
415 text
->options
.print_delta_field
= !value
; /* Reverse logic. */
417 value
= false; /* Default. */
418 ret
= apply_one_bool("clock-cycles", params
, &value
, NULL
);
419 if (ret
!= BT_COMPONENT_STATUS_OK
) {
422 text
->options
.print_timestamp_cycles
= value
;
424 value
= false; /* Default. */
425 ret
= apply_one_bool("clock-seconds", params
, &value
, NULL
);
426 if (ret
!= BT_COMPONENT_STATUS_OK
) {
429 text
->options
.clock_seconds
= value
;
431 value
= false; /* Default. */
432 ret
= apply_one_bool("clock-date", params
, &value
, NULL
);
433 if (ret
!= BT_COMPONENT_STATUS_OK
) {
436 text
->options
.clock_date
= value
;
438 value
= false; /* Default. */
439 ret
= apply_one_bool("clock-gmt", params
, &value
, NULL
);
440 if (ret
!= BT_COMPONENT_STATUS_OK
) {
443 text
->options
.clock_gmt
= value
;
445 value
= false; /* Default. */
446 ret
= apply_one_bool("verbose", params
, &value
, NULL
);
447 if (ret
!= BT_COMPONENT_STATUS_OK
) {
450 text
->options
.verbose
= value
;
453 ret
= apply_one_string("name-default", params
, &str
);
454 if (ret
!= BT_COMPONENT_STATUS_OK
) {
458 text
->options
.name_default
= TEXT_DEFAULT_UNSET
;
459 } else if (!strcmp(str
, "show")) {
460 text
->options
.name_default
= TEXT_DEFAULT_SHOW
;
461 } else if (!strcmp(str
, "hide")) {
462 text
->options
.name_default
= TEXT_DEFAULT_HIDE
;
464 ret
= BT_COMPONENT_STATUS_ERROR
;
470 switch (text
->options
.name_default
) {
471 case TEXT_DEFAULT_UNSET
:
472 text
->options
.print_payload_field_names
= true;
473 text
->options
.print_context_field_names
= true;
474 text
->options
.print_header_field_names
= false;
475 text
->options
.print_scope_field_names
= false;
477 case TEXT_DEFAULT_SHOW
:
478 text
->options
.print_payload_field_names
= true;
479 text
->options
.print_context_field_names
= true;
480 text
->options
.print_header_field_names
= true;
481 text
->options
.print_scope_field_names
= true;
483 case TEXT_DEFAULT_HIDE
:
484 text
->options
.print_payload_field_names
= false;
485 text
->options
.print_context_field_names
= false;
486 text
->options
.print_header_field_names
= false;
487 text
->options
.print_scope_field_names
= false;
490 ret
= BT_COMPONENT_STATUS_ERROR
;
496 ret
= apply_one_bool("name-payload", params
, &value
, &found
);
497 if (ret
!= BT_COMPONENT_STATUS_OK
) {
501 text
->options
.print_payload_field_names
= value
;
506 ret
= apply_one_bool("name-context", params
, &value
, &found
);
507 if (ret
!= BT_COMPONENT_STATUS_OK
) {
511 text
->options
.print_context_field_names
= value
;
516 ret
= apply_one_bool("name-header", params
, &value
, &found
);
517 if (ret
!= BT_COMPONENT_STATUS_OK
) {
521 text
->options
.print_header_field_names
= value
;
526 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
527 if (ret
!= BT_COMPONENT_STATUS_OK
) {
531 text
->options
.print_scope_field_names
= value
;
535 ret
= apply_one_string("field-default", params
, &str
);
536 if (ret
!= BT_COMPONENT_STATUS_OK
) {
540 text
->options
.field_default
= TEXT_DEFAULT_UNSET
;
541 } else if (!strcmp(str
, "show")) {
542 text
->options
.field_default
= TEXT_DEFAULT_SHOW
;
543 } else if (!strcmp(str
, "hide")) {
544 text
->options
.field_default
= TEXT_DEFAULT_HIDE
;
546 ret
= BT_COMPONENT_STATUS_ERROR
;
552 switch (text
->options
.field_default
) {
553 case TEXT_DEFAULT_UNSET
:
554 text
->options
.print_trace_field
= false;
555 text
->options
.print_trace_hostname_field
= true;
556 text
->options
.print_trace_domain_field
= false;
557 text
->options
.print_trace_procname_field
= true;
558 text
->options
.print_trace_vpid_field
= true;
559 text
->options
.print_loglevel_field
= false;
560 text
->options
.print_emf_field
= false;
561 text
->options
.print_emf_field
= false;
563 case TEXT_DEFAULT_SHOW
:
564 text
->options
.print_trace_field
= true;
565 text
->options
.print_trace_hostname_field
= true;
566 text
->options
.print_trace_domain_field
= true;
567 text
->options
.print_trace_procname_field
= true;
568 text
->options
.print_trace_vpid_field
= true;
569 text
->options
.print_loglevel_field
= true;
570 text
->options
.print_emf_field
= true;
571 text
->options
.print_emf_field
= true;
573 case TEXT_DEFAULT_HIDE
:
574 text
->options
.print_trace_field
= false;
575 text
->options
.print_trace_hostname_field
= false;
576 text
->options
.print_trace_domain_field
= false;
577 text
->options
.print_trace_procname_field
= false;
578 text
->options
.print_trace_vpid_field
= false;
579 text
->options
.print_loglevel_field
= false;
580 text
->options
.print_emf_field
= false;
581 text
->options
.print_emf_field
= false;
584 ret
= BT_COMPONENT_STATUS_ERROR
;
590 ret
= apply_one_bool("field-trace", params
, &value
, &found
);
591 if (ret
!= BT_COMPONENT_STATUS_OK
) {
595 text
->options
.print_trace_field
= value
;
600 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
601 if (ret
!= BT_COMPONENT_STATUS_OK
) {
605 text
->options
.print_trace_hostname_field
= value
;
610 ret
= apply_one_bool("field-trace:domain", params
, &value
, &found
);
611 if (ret
!= BT_COMPONENT_STATUS_OK
) {
615 text
->options
.print_trace_domain_field
= value
;
620 ret
= apply_one_bool("field-trace:procname", params
, &value
, &found
);
621 if (ret
!= BT_COMPONENT_STATUS_OK
) {
625 text
->options
.print_trace_procname_field
= value
;
630 ret
= apply_one_bool("field-trace:vpid", params
, &value
, &found
);
631 if (ret
!= BT_COMPONENT_STATUS_OK
) {
635 text
->options
.print_trace_vpid_field
= value
;
640 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
641 if (ret
!= BT_COMPONENT_STATUS_OK
) {
645 text
->options
.print_loglevel_field
= value
;
650 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
651 if (ret
!= BT_COMPONENT_STATUS_OK
) {
655 text
->options
.print_emf_field
= value
;
660 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
661 if (ret
!= BT_COMPONENT_STATUS_OK
) {
665 text
->options
.print_emf_field
= value
;
669 bt_put(text
->plugin_opt_map
);
670 text
->plugin_opt_map
= NULL
;
676 void set_use_colors(struct text_component
*text
)
678 switch (text
->options
.color
) {
679 case TEXT_COLOR_OPT_ALWAYS
:
680 text
->use_colors
= true;
682 case TEXT_COLOR_OPT_AUTO
:
683 text
->use_colors
= text
->out
== stdout
&&
684 bt_common_colors_supported();
686 case TEXT_COLOR_OPT_NEVER
:
687 text
->use_colors
= false;
693 void init_stream_packet_context_quarks(void)
695 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
696 g_quark_from_string("timestamp_begin");
697 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
698 g_quark_from_string("timestamp_begin");
699 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
700 g_quark_from_string("timestamp_end");
701 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
702 g_quark_from_string("events_discarded");
703 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
704 g_quark_from_string("content_size");
705 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
706 g_quark_from_string("packet_size");
707 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
708 g_quark_from_string("packet_seq_num");
712 enum bt_component_status
text_component_init(
713 struct bt_component
*component
, struct bt_value
*params
,
714 UNUSED_VAR
void *init_method_data
)
716 enum bt_component_status ret
;
717 struct text_component
*text
= create_text();
720 ret
= BT_COMPONENT_STATUS_NOMEM
;
727 text
->delta_cycles
= -1ULL;
728 text
->last_cycles_timestamp
= -1ULL;
730 text
->delta_real_timestamp
= -1ULL;
731 text
->last_real_timestamp
= -1ULL;
733 ret
= apply_params(text
, params
);
734 if (ret
!= BT_COMPONENT_STATUS_OK
) {
738 set_use_colors(text
);
740 ret
= bt_component_set_private_data(component
, text
);
741 if (ret
!= BT_COMPONENT_STATUS_OK
) {
745 init_stream_packet_context_quarks();
750 destroy_text_data(text
);
754 /* Initialize plug-in entry points. */
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_NEW_CONNECTION_METHOD(text
, text_new_connection
);
762 BT_PLUGIN_SINK_COMPONENT_CLASS_DESTROY_METHOD(text
, destroy_text
);
763 BT_PLUGIN_SINK_COMPONENT_CLASS_DESCRIPTION(text
,
764 "Formats CTF-IR to text. Formerly known as ctf-text.");