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_accept_port_connection(struct bt_component
*component
,
162 struct bt_port
*self_port
)
164 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
165 struct bt_connection
*connection
;
166 struct text_component
*text
;
168 text
= bt_component_get_private_data(component
);
170 assert(!text
->input_iterator
);
171 connection
= bt_port_get_connection(self_port
);
173 text
->input_iterator
= bt_connection_create_notification_iterator(
176 if (!text
->input_iterator
) {
177 ret
= BT_COMPONENT_STATUS_ERROR
;
185 enum bt_component_status
run(struct bt_component
*component
)
187 enum bt_component_status ret
;
188 struct bt_notification
*notification
= NULL
;
189 struct bt_notification_iterator
*it
;
190 struct text_component
*text
= bt_component_get_private_data(component
);
192 it
= text
->input_iterator
;
194 if (likely(text
->processed_first_event
)) {
195 enum bt_notification_iterator_status it_ret
;
197 it_ret
= bt_notification_iterator_next(it
);
199 case BT_NOTIFICATION_ITERATOR_STATUS_ERROR
:
200 ret
= BT_COMPONENT_STATUS_ERROR
;
202 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
203 ret
= BT_COMPONENT_STATUS_END
;
204 BT_PUT(text
->input_iterator
);
210 notification
= bt_notification_iterator_get_notification(it
);
212 ret
= BT_COMPONENT_STATUS_ERROR
;
216 ret
= handle_notification(text
, notification
);
217 text
->processed_first_event
= true;
219 bt_put(notification
);
224 enum bt_component_status
add_params_to_map(struct bt_value
*plugin_opt_map
)
226 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
229 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
230 const char *key
= plugin_options
[i
];
231 enum bt_value_status status
;
233 status
= bt_value_map_insert(plugin_opt_map
, key
, bt_value_null
);
235 case BT_VALUE_STATUS_OK
:
238 ret
= BT_COMPONENT_STATUS_ERROR
;
247 bool check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
249 struct text_component
*text
= data
;
250 struct bt_value
*plugin_opt_map
= text
->plugin_opt_map
;
252 if (!bt_value_map_get(plugin_opt_map
, key
)) {
254 "[warning] Parameter \"%s\" unknown to \"text\" plugin\n", key
);
260 enum bt_component_status
apply_one_string(const char *key
,
261 struct bt_value
*params
,
264 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
265 struct bt_value
*value
= NULL
;
266 enum bt_value_status status
;
269 value
= bt_value_map_get(params
, key
);
273 if (bt_value_is_null(value
)) {
276 status
= bt_value_string_get(value
, &str
);
278 case BT_VALUE_STATUS_OK
:
281 ret
= BT_COMPONENT_STATUS_ERROR
;
284 *option
= g_strdup(str
);
291 enum bt_component_status
apply_one_bool(const char *key
,
292 struct bt_value
*params
,
296 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
297 struct bt_value
*value
= NULL
;
298 enum bt_value_status status
;
300 value
= bt_value_map_get(params
, key
);
304 status
= bt_value_bool_get(value
, option
);
306 case BT_VALUE_STATUS_OK
:
309 ret
= BT_COMPONENT_STATUS_ERROR
;
321 void warn_wrong_color_param(struct text_component
*text
)
324 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
328 enum bt_component_status
apply_params(struct text_component
*text
,
329 struct bt_value
*params
)
331 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
332 enum bt_value_status status
;
336 text
->plugin_opt_map
= bt_value_map_create();
337 if (!text
->plugin_opt_map
) {
338 ret
= BT_COMPONENT_STATUS_ERROR
;
341 ret
= add_params_to_map(text
->plugin_opt_map
);
342 if (ret
!= BT_COMPONENT_STATUS_OK
) {
345 /* Report unknown parameters. */
346 status
= bt_value_map_foreach(params
, check_param_exists
, text
);
348 case BT_VALUE_STATUS_OK
:
351 ret
= BT_COMPONENT_STATUS_ERROR
;
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
;
360 color_value
= bt_value_map_get(params
, "color");
365 status
= bt_value_string_get(color_value
, &color
);
367 warn_wrong_color_param(text
);
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
;
376 warn_wrong_color_param(text
);
383 ret
= apply_one_string("output-path",
385 &text
->options
.output_path
);
386 if (ret
!= BT_COMPONENT_STATUS_OK
) {
390 ret
= apply_one_string("debug-info-dir",
392 &text
->options
.debug_info_dir
);
393 if (ret
!= BT_COMPONENT_STATUS_OK
) {
397 ret
= apply_one_string("debug-info-target-prefix",
399 &text
->options
.debug_info_target_prefix
);
400 if (ret
!= BT_COMPONENT_STATUS_OK
) {
404 value
= false; /* Default. */
405 ret
= apply_one_bool("debug-info-full-path", params
, &value
, NULL
);
406 if (ret
!= BT_COMPONENT_STATUS_OK
) {
409 text
->options
.debug_info_full_path
= value
;
411 value
= false; /* Default. */
412 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
413 if (ret
!= BT_COMPONENT_STATUS_OK
) {
416 text
->options
.print_delta_field
= !value
; /* Reverse logic. */
418 value
= false; /* Default. */
419 ret
= apply_one_bool("clock-cycles", params
, &value
, NULL
);
420 if (ret
!= BT_COMPONENT_STATUS_OK
) {
423 text
->options
.print_timestamp_cycles
= value
;
425 value
= false; /* Default. */
426 ret
= apply_one_bool("clock-seconds", params
, &value
, NULL
);
427 if (ret
!= BT_COMPONENT_STATUS_OK
) {
430 text
->options
.clock_seconds
= value
;
432 value
= false; /* Default. */
433 ret
= apply_one_bool("clock-date", params
, &value
, NULL
);
434 if (ret
!= BT_COMPONENT_STATUS_OK
) {
437 text
->options
.clock_date
= value
;
439 value
= false; /* Default. */
440 ret
= apply_one_bool("clock-gmt", params
, &value
, NULL
);
441 if (ret
!= BT_COMPONENT_STATUS_OK
) {
444 text
->options
.clock_gmt
= value
;
446 value
= false; /* Default. */
447 ret
= apply_one_bool("verbose", params
, &value
, NULL
);
448 if (ret
!= BT_COMPONENT_STATUS_OK
) {
451 text
->options
.verbose
= value
;
454 ret
= apply_one_string("name-default", params
, &str
);
455 if (ret
!= BT_COMPONENT_STATUS_OK
) {
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
;
465 ret
= BT_COMPONENT_STATUS_ERROR
;
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;
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;
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;
491 ret
= BT_COMPONENT_STATUS_ERROR
;
497 ret
= apply_one_bool("name-payload", params
, &value
, &found
);
498 if (ret
!= BT_COMPONENT_STATUS_OK
) {
502 text
->options
.print_payload_field_names
= value
;
507 ret
= apply_one_bool("name-context", params
, &value
, &found
);
508 if (ret
!= BT_COMPONENT_STATUS_OK
) {
512 text
->options
.print_context_field_names
= value
;
517 ret
= apply_one_bool("name-header", params
, &value
, &found
);
518 if (ret
!= BT_COMPONENT_STATUS_OK
) {
522 text
->options
.print_header_field_names
= value
;
527 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
528 if (ret
!= BT_COMPONENT_STATUS_OK
) {
532 text
->options
.print_scope_field_names
= value
;
536 ret
= apply_one_string("field-default", params
, &str
);
537 if (ret
!= BT_COMPONENT_STATUS_OK
) {
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
;
547 ret
= BT_COMPONENT_STATUS_ERROR
;
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;
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;
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;
585 ret
= BT_COMPONENT_STATUS_ERROR
;
591 ret
= apply_one_bool("field-trace", params
, &value
, &found
);
592 if (ret
!= BT_COMPONENT_STATUS_OK
) {
596 text
->options
.print_trace_field
= value
;
601 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
602 if (ret
!= BT_COMPONENT_STATUS_OK
) {
606 text
->options
.print_trace_hostname_field
= value
;
611 ret
= apply_one_bool("field-trace:domain", params
, &value
, &found
);
612 if (ret
!= BT_COMPONENT_STATUS_OK
) {
616 text
->options
.print_trace_domain_field
= value
;
621 ret
= apply_one_bool("field-trace:procname", params
, &value
, &found
);
622 if (ret
!= BT_COMPONENT_STATUS_OK
) {
626 text
->options
.print_trace_procname_field
= value
;
631 ret
= apply_one_bool("field-trace:vpid", params
, &value
, &found
);
632 if (ret
!= BT_COMPONENT_STATUS_OK
) {
636 text
->options
.print_trace_vpid_field
= value
;
641 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
642 if (ret
!= BT_COMPONENT_STATUS_OK
) {
646 text
->options
.print_loglevel_field
= value
;
651 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
652 if (ret
!= BT_COMPONENT_STATUS_OK
) {
656 text
->options
.print_emf_field
= value
;
661 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
662 if (ret
!= BT_COMPONENT_STATUS_OK
) {
666 text
->options
.print_emf_field
= value
;
670 bt_put(text
->plugin_opt_map
);
671 text
->plugin_opt_map
= NULL
;
677 void set_use_colors(struct text_component
*text
)
679 switch (text
->options
.color
) {
680 case TEXT_COLOR_OPT_ALWAYS
:
681 text
->use_colors
= true;
683 case TEXT_COLOR_OPT_AUTO
:
684 text
->use_colors
= text
->out
== stdout
&&
685 bt_common_colors_supported();
687 case TEXT_COLOR_OPT_NEVER
:
688 text
->use_colors
= false;
694 void init_stream_packet_context_quarks(void)
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");
713 enum bt_component_status
text_component_init(
714 struct bt_component
*component
, struct bt_value
*params
,
715 UNUSED_VAR
void *init_method_data
)
717 enum bt_component_status ret
;
718 struct text_component
*text
= create_text();
721 ret
= BT_COMPONENT_STATUS_NOMEM
;
728 text
->delta_cycles
= -1ULL;
729 text
->last_cycles_timestamp
= -1ULL;
731 text
->delta_real_timestamp
= -1ULL;
732 text
->last_real_timestamp
= -1ULL;
734 ret
= apply_params(text
, params
);
735 if (ret
!= BT_COMPONENT_STATUS_OK
) {
739 set_use_colors(text
);
741 ret
= bt_component_set_private_data(component
, text
);
742 if (ret
!= BT_COMPONENT_STATUS_OK
) {
746 init_stream_packet_context_quarks();
751 destroy_text_data(text
);
755 /* Initialize plug-in entry points. */
757 BT_PLUGIN_DESCRIPTION("Babeltrace text output plug-in.");
758 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
759 BT_PLUGIN_LICENSE("MIT");
760 BT_PLUGIN_SINK_COMPONENT_CLASS(text
, run
);
761 BT_PLUGIN_SINK_COMPONENT_CLASS_INIT_METHOD(text
, text_component_init
);
762 BT_PLUGIN_SINK_COMPONENT_CLASS_ACCEPT_PORT_CONNECTION_METHOD(text
, text_accept_port_connection
);
763 BT_PLUGIN_SINK_COMPONENT_CLASS_DESTROY_METHOD(text
, destroy_text
);
764 BT_PLUGIN_SINK_COMPONENT_CLASS_DESCRIPTION(text
,
765 "Formats CTF-IR to text. Formerly known as ctf-text.");