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 destroy_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
)
168 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
169 struct bt_private_connection
*connection
;
170 struct text_component
*text
;
172 text
= bt_private_component_get_user_data(component
);
174 assert(!text
->input_iterator
);
175 connection
= bt_private_port_get_private_connection(self_port
);
177 text
->input_iterator
=
178 bt_private_connection_create_notification_iterator(connection
);
180 if (!text
->input_iterator
) {
181 ret
= BT_COMPONENT_STATUS_ERROR
;
189 enum bt_component_status
run(struct bt_private_component
*component
)
191 enum bt_component_status ret
;
192 struct bt_notification
*notification
= NULL
;
193 struct bt_notification_iterator
*it
;
194 struct text_component
*text
=
195 bt_private_component_get_user_data(component
);
197 it
= text
->input_iterator
;
199 if (likely(text
->processed_first_event
)) {
200 enum bt_notification_iterator_status it_ret
;
202 it_ret
= bt_notification_iterator_next(it
);
204 case BT_NOTIFICATION_ITERATOR_STATUS_ERROR
:
205 ret
= BT_COMPONENT_STATUS_ERROR
;
207 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
208 ret
= BT_COMPONENT_STATUS_END
;
209 BT_PUT(text
->input_iterator
);
215 notification
= bt_notification_iterator_get_notification(it
);
217 ret
= BT_COMPONENT_STATUS_ERROR
;
221 ret
= handle_notification(text
, notification
);
222 text
->processed_first_event
= true;
224 bt_put(notification
);
229 enum bt_component_status
add_params_to_map(struct bt_value
*plugin_opt_map
)
231 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
234 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
235 const char *key
= plugin_options
[i
];
236 enum bt_value_status status
;
238 status
= bt_value_map_insert(plugin_opt_map
, key
, bt_value_null
);
240 case BT_VALUE_STATUS_OK
:
243 ret
= BT_COMPONENT_STATUS_ERROR
;
252 bool check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
254 struct text_component
*text
= data
;
255 struct bt_value
*plugin_opt_map
= text
->plugin_opt_map
;
257 if (!bt_value_map_get(plugin_opt_map
, key
)) {
259 "[warning] Parameter \"%s\" unknown to \"text\" plugin\n", key
);
265 enum bt_component_status
apply_one_string(const char *key
,
266 struct bt_value
*params
,
269 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
270 struct bt_value
*value
= NULL
;
271 enum bt_value_status status
;
274 value
= bt_value_map_get(params
, key
);
278 if (bt_value_is_null(value
)) {
281 status
= bt_value_string_get(value
, &str
);
283 case BT_VALUE_STATUS_OK
:
286 ret
= BT_COMPONENT_STATUS_ERROR
;
289 *option
= g_strdup(str
);
296 enum bt_component_status
apply_one_bool(const char *key
,
297 struct bt_value
*params
,
301 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
302 struct bt_value
*value
= NULL
;
303 enum bt_value_status status
;
305 value
= bt_value_map_get(params
, key
);
309 status
= bt_value_bool_get(value
, option
);
311 case BT_VALUE_STATUS_OK
:
314 ret
= BT_COMPONENT_STATUS_ERROR
;
326 void warn_wrong_color_param(struct text_component
*text
)
329 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
333 enum bt_component_status
apply_params(struct text_component
*text
,
334 struct bt_value
*params
)
336 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
337 enum bt_value_status status
;
341 text
->plugin_opt_map
= bt_value_map_create();
342 if (!text
->plugin_opt_map
) {
343 ret
= BT_COMPONENT_STATUS_ERROR
;
346 ret
= add_params_to_map(text
->plugin_opt_map
);
347 if (ret
!= BT_COMPONENT_STATUS_OK
) {
350 /* Report unknown parameters. */
351 status
= bt_value_map_foreach(params
, check_param_exists
, text
);
353 case BT_VALUE_STATUS_OK
:
356 ret
= BT_COMPONENT_STATUS_ERROR
;
359 /* Known parameters. */
360 text
->options
.color
= TEXT_COLOR_OPT_AUTO
;
361 if (bt_value_map_has_key(params
, "color")) {
362 struct bt_value
*color_value
;
365 color_value
= bt_value_map_get(params
, "color");
370 status
= bt_value_string_get(color_value
, &color
);
372 warn_wrong_color_param(text
);
374 if (strcmp(color
, "never") == 0) {
375 text
->options
.color
= TEXT_COLOR_OPT_NEVER
;
376 } else if (strcmp(color
, "auto") == 0) {
377 text
->options
.color
= TEXT_COLOR_OPT_AUTO
;
378 } else if (strcmp(color
, "always") == 0) {
379 text
->options
.color
= TEXT_COLOR_OPT_ALWAYS
;
381 warn_wrong_color_param(text
);
388 ret
= apply_one_string("output-path",
390 &text
->options
.output_path
);
391 if (ret
!= BT_COMPONENT_STATUS_OK
) {
395 ret
= apply_one_string("debug-info-dir",
397 &text
->options
.debug_info_dir
);
398 if (ret
!= BT_COMPONENT_STATUS_OK
) {
402 ret
= apply_one_string("debug-info-target-prefix",
404 &text
->options
.debug_info_target_prefix
);
405 if (ret
!= BT_COMPONENT_STATUS_OK
) {
409 value
= false; /* Default. */
410 ret
= apply_one_bool("debug-info-full-path", params
, &value
, NULL
);
411 if (ret
!= BT_COMPONENT_STATUS_OK
) {
414 text
->options
.debug_info_full_path
= value
;
416 value
= false; /* Default. */
417 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
418 if (ret
!= BT_COMPONENT_STATUS_OK
) {
421 text
->options
.print_delta_field
= !value
; /* Reverse logic. */
423 value
= false; /* Default. */
424 ret
= apply_one_bool("clock-cycles", params
, &value
, NULL
);
425 if (ret
!= BT_COMPONENT_STATUS_OK
) {
428 text
->options
.print_timestamp_cycles
= value
;
430 value
= false; /* Default. */
431 ret
= apply_one_bool("clock-seconds", params
, &value
, NULL
);
432 if (ret
!= BT_COMPONENT_STATUS_OK
) {
435 text
->options
.clock_seconds
= value
;
437 value
= false; /* Default. */
438 ret
= apply_one_bool("clock-date", params
, &value
, NULL
);
439 if (ret
!= BT_COMPONENT_STATUS_OK
) {
442 text
->options
.clock_date
= value
;
444 value
= false; /* Default. */
445 ret
= apply_one_bool("clock-gmt", params
, &value
, NULL
);
446 if (ret
!= BT_COMPONENT_STATUS_OK
) {
449 text
->options
.clock_gmt
= value
;
451 value
= false; /* Default. */
452 ret
= apply_one_bool("verbose", params
, &value
, NULL
);
453 if (ret
!= BT_COMPONENT_STATUS_OK
) {
456 text
->options
.verbose
= value
;
459 ret
= apply_one_string("name-default", params
, &str
);
460 if (ret
!= BT_COMPONENT_STATUS_OK
) {
464 text
->options
.name_default
= TEXT_DEFAULT_UNSET
;
465 } else if (!strcmp(str
, "show")) {
466 text
->options
.name_default
= TEXT_DEFAULT_SHOW
;
467 } else if (!strcmp(str
, "hide")) {
468 text
->options
.name_default
= TEXT_DEFAULT_HIDE
;
470 ret
= BT_COMPONENT_STATUS_ERROR
;
476 switch (text
->options
.name_default
) {
477 case TEXT_DEFAULT_UNSET
:
478 text
->options
.print_payload_field_names
= true;
479 text
->options
.print_context_field_names
= true;
480 text
->options
.print_header_field_names
= false;
481 text
->options
.print_scope_field_names
= false;
483 case TEXT_DEFAULT_SHOW
:
484 text
->options
.print_payload_field_names
= true;
485 text
->options
.print_context_field_names
= true;
486 text
->options
.print_header_field_names
= true;
487 text
->options
.print_scope_field_names
= true;
489 case TEXT_DEFAULT_HIDE
:
490 text
->options
.print_payload_field_names
= false;
491 text
->options
.print_context_field_names
= false;
492 text
->options
.print_header_field_names
= false;
493 text
->options
.print_scope_field_names
= false;
496 ret
= BT_COMPONENT_STATUS_ERROR
;
502 ret
= apply_one_bool("name-payload", params
, &value
, &found
);
503 if (ret
!= BT_COMPONENT_STATUS_OK
) {
507 text
->options
.print_payload_field_names
= value
;
512 ret
= apply_one_bool("name-context", params
, &value
, &found
);
513 if (ret
!= BT_COMPONENT_STATUS_OK
) {
517 text
->options
.print_context_field_names
= value
;
522 ret
= apply_one_bool("name-header", params
, &value
, &found
);
523 if (ret
!= BT_COMPONENT_STATUS_OK
) {
527 text
->options
.print_header_field_names
= value
;
532 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
533 if (ret
!= BT_COMPONENT_STATUS_OK
) {
537 text
->options
.print_scope_field_names
= value
;
541 ret
= apply_one_string("field-default", params
, &str
);
542 if (ret
!= BT_COMPONENT_STATUS_OK
) {
546 text
->options
.field_default
= TEXT_DEFAULT_UNSET
;
547 } else if (!strcmp(str
, "show")) {
548 text
->options
.field_default
= TEXT_DEFAULT_SHOW
;
549 } else if (!strcmp(str
, "hide")) {
550 text
->options
.field_default
= TEXT_DEFAULT_HIDE
;
552 ret
= BT_COMPONENT_STATUS_ERROR
;
558 switch (text
->options
.field_default
) {
559 case TEXT_DEFAULT_UNSET
:
560 text
->options
.print_trace_field
= false;
561 text
->options
.print_trace_hostname_field
= true;
562 text
->options
.print_trace_domain_field
= false;
563 text
->options
.print_trace_procname_field
= true;
564 text
->options
.print_trace_vpid_field
= true;
565 text
->options
.print_loglevel_field
= false;
566 text
->options
.print_emf_field
= false;
567 text
->options
.print_emf_field
= false;
569 case TEXT_DEFAULT_SHOW
:
570 text
->options
.print_trace_field
= true;
571 text
->options
.print_trace_hostname_field
= true;
572 text
->options
.print_trace_domain_field
= true;
573 text
->options
.print_trace_procname_field
= true;
574 text
->options
.print_trace_vpid_field
= true;
575 text
->options
.print_loglevel_field
= true;
576 text
->options
.print_emf_field
= true;
577 text
->options
.print_emf_field
= true;
579 case TEXT_DEFAULT_HIDE
:
580 text
->options
.print_trace_field
= false;
581 text
->options
.print_trace_hostname_field
= false;
582 text
->options
.print_trace_domain_field
= false;
583 text
->options
.print_trace_procname_field
= false;
584 text
->options
.print_trace_vpid_field
= false;
585 text
->options
.print_loglevel_field
= false;
586 text
->options
.print_emf_field
= false;
587 text
->options
.print_emf_field
= false;
590 ret
= BT_COMPONENT_STATUS_ERROR
;
596 ret
= apply_one_bool("field-trace", params
, &value
, &found
);
597 if (ret
!= BT_COMPONENT_STATUS_OK
) {
601 text
->options
.print_trace_field
= value
;
606 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
607 if (ret
!= BT_COMPONENT_STATUS_OK
) {
611 text
->options
.print_trace_hostname_field
= value
;
616 ret
= apply_one_bool("field-trace:domain", params
, &value
, &found
);
617 if (ret
!= BT_COMPONENT_STATUS_OK
) {
621 text
->options
.print_trace_domain_field
= value
;
626 ret
= apply_one_bool("field-trace:procname", params
, &value
, &found
);
627 if (ret
!= BT_COMPONENT_STATUS_OK
) {
631 text
->options
.print_trace_procname_field
= value
;
636 ret
= apply_one_bool("field-trace:vpid", params
, &value
, &found
);
637 if (ret
!= BT_COMPONENT_STATUS_OK
) {
641 text
->options
.print_trace_vpid_field
= value
;
646 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
647 if (ret
!= BT_COMPONENT_STATUS_OK
) {
651 text
->options
.print_loglevel_field
= value
;
656 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
657 if (ret
!= BT_COMPONENT_STATUS_OK
) {
661 text
->options
.print_emf_field
= value
;
666 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
667 if (ret
!= BT_COMPONENT_STATUS_OK
) {
671 text
->options
.print_emf_field
= value
;
675 bt_put(text
->plugin_opt_map
);
676 text
->plugin_opt_map
= NULL
;
682 void set_use_colors(struct text_component
*text
)
684 switch (text
->options
.color
) {
685 case TEXT_COLOR_OPT_ALWAYS
:
686 text
->use_colors
= true;
688 case TEXT_COLOR_OPT_AUTO
:
689 text
->use_colors
= text
->out
== stdout
&&
690 bt_common_colors_supported();
692 case TEXT_COLOR_OPT_NEVER
:
693 text
->use_colors
= false;
699 void init_stream_packet_context_quarks(void)
701 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
702 g_quark_from_string("timestamp_begin");
703 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
704 g_quark_from_string("timestamp_begin");
705 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
706 g_quark_from_string("timestamp_end");
707 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
708 g_quark_from_string("events_discarded");
709 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
710 g_quark_from_string("content_size");
711 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
712 g_quark_from_string("packet_size");
713 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
714 g_quark_from_string("packet_seq_num");
718 enum bt_component_status
text_component_init(
719 struct bt_private_component
*component
,
720 struct bt_value
*params
,
721 UNUSED_VAR
void *init_method_data
)
723 enum bt_component_status ret
;
724 struct text_component
*text
= create_text();
727 ret
= BT_COMPONENT_STATUS_NOMEM
;
734 text
->delta_cycles
= -1ULL;
735 text
->last_cycles_timestamp
= -1ULL;
737 text
->delta_real_timestamp
= -1ULL;
738 text
->last_real_timestamp
= -1ULL;
740 ret
= apply_params(text
, params
);
741 if (ret
!= BT_COMPONENT_STATUS_OK
) {
745 set_use_colors(text
);
747 ret
= bt_private_component_set_user_data(component
, text
);
748 if (ret
!= BT_COMPONENT_STATUS_OK
) {
752 init_stream_packet_context_quarks();
757 destroy_text_data(text
);
761 /* Initialize plug-in entry points. */
763 BT_PLUGIN_DESCRIPTION("Babeltrace text output plug-in.");
764 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
765 BT_PLUGIN_LICENSE("MIT");
766 BT_PLUGIN_SINK_COMPONENT_CLASS(text
, run
);
767 BT_PLUGIN_SINK_COMPONENT_CLASS_INIT_METHOD(text
, text_component_init
);
768 BT_PLUGIN_SINK_COMPONENT_CLASS_ACCEPT_PORT_CONNECTION_METHOD(text
, text_accept_port_connection
);
769 BT_PLUGIN_SINK_COMPONENT_CLASS_DESTROY_METHOD(text
, destroy_text
);
770 BT_PLUGIN_SINK_COMPONENT_CLASS_DESCRIPTION(text
,
771 "Formats CTF-IR to text. Formerly known as ctf-text.");