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/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>
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",
81 void destroy_text_data(struct text_component
*text
)
83 bt_put(text
->input_iterator
);
84 (void) g_string_free(text
->string
, TRUE
);
85 g_free(text
->options
.output_path
);
86 g_free(text
->options
.debug_info_dir
);
87 g_free(text
->options
.debug_info_target_prefix
);
92 struct text_component
*create_text(void)
94 struct text_component
*text
;
96 text
= g_new0(struct text_component
, 1);
100 text
->string
= g_string_new("");
113 void finalize_text(struct bt_private_component
*component
)
115 void *data
= bt_private_component_get_user_data(component
);
117 destroy_text_data(data
);
121 enum bt_component_status
handle_notification(struct text_component
*text
,
122 struct bt_notification
*notification
)
124 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
127 ret
= BT_COMPONENT_STATUS_ERROR
;
131 switch (bt_notification_get_type(notification
)) {
132 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
134 case BT_NOTIFICATION_TYPE_PACKET_END
:
136 case BT_NOTIFICATION_TYPE_EVENT
:
138 struct bt_ctf_event
*event
= bt_notification_event_get_event(
142 ret
= BT_COMPONENT_STATUS_ERROR
;
145 ret
= text_print_event(text
, event
);
147 if (ret
!= BT_COMPONENT_STATUS_OK
) {
152 case BT_NOTIFICATION_TYPE_STREAM_END
:
155 puts("Unhandled notification type");
162 enum bt_component_status
text_accept_port_connection(
163 struct bt_private_component
*component
,
164 struct bt_private_port
*self_port
,
165 struct bt_port
*other_port
)
167 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
168 struct bt_private_connection
*connection
;
169 struct text_component
*text
;
171 text
= bt_private_component_get_user_data(component
);
173 assert(!text
->input_iterator
);
174 connection
= bt_private_port_get_private_connection(self_port
);
176 text
->input_iterator
=
177 bt_private_connection_create_notification_iterator(connection
);
179 if (!text
->input_iterator
) {
180 ret
= BT_COMPONENT_STATUS_ERROR
;
188 enum bt_component_status
run(struct bt_private_component
*component
)
190 enum bt_component_status ret
;
191 struct bt_notification
*notification
= NULL
;
192 struct bt_notification_iterator
*it
;
193 struct text_component
*text
=
194 bt_private_component_get_user_data(component
);
195 enum bt_notification_iterator_status it_ret
;
197 it
= text
->input_iterator
;
199 it_ret
= bt_notification_iterator_next(it
);
201 case BT_NOTIFICATION_ITERATOR_STATUS_ERROR
:
202 ret
= BT_COMPONENT_STATUS_ERROR
;
204 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
205 ret
= BT_COMPONENT_STATUS_END
;
206 BT_PUT(text
->input_iterator
);
212 notification
= bt_notification_iterator_get_notification(it
);
213 assert(notification
);
214 ret
= handle_notification(text
, notification
);
215 text
->processed_first_event
= true;
217 bt_put(notification
);
222 enum bt_component_status
add_params_to_map(struct bt_value
*plugin_opt_map
)
224 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
227 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
228 const char *key
= plugin_options
[i
];
229 enum bt_value_status status
;
231 status
= bt_value_map_insert(plugin_opt_map
, key
, bt_value_null
);
233 case BT_VALUE_STATUS_OK
:
236 ret
= BT_COMPONENT_STATUS_ERROR
;
245 bool check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
247 struct text_component
*text
= data
;
248 struct bt_value
*plugin_opt_map
= text
->plugin_opt_map
;
250 if (!bt_value_map_get(plugin_opt_map
, key
)) {
252 "[warning] Parameter \"%s\" unknown to \"text\" plugin\n", key
);
258 enum bt_component_status
apply_one_string(const char *key
,
259 struct bt_value
*params
,
262 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
263 struct bt_value
*value
= NULL
;
264 enum bt_value_status status
;
267 value
= bt_value_map_get(params
, key
);
271 if (bt_value_is_null(value
)) {
274 status
= bt_value_string_get(value
, &str
);
276 case BT_VALUE_STATUS_OK
:
279 ret
= BT_COMPONENT_STATUS_ERROR
;
282 *option
= g_strdup(str
);
289 enum bt_component_status
apply_one_bool(const char *key
,
290 struct bt_value
*params
,
294 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
295 struct bt_value
*value
= NULL
;
296 enum bt_value_status status
;
298 value
= bt_value_map_get(params
, key
);
302 status
= bt_value_bool_get(value
, option
);
304 case BT_VALUE_STATUS_OK
:
307 ret
= BT_COMPONENT_STATUS_ERROR
;
319 void warn_wrong_color_param(struct text_component
*text
)
322 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
326 enum bt_component_status
apply_params(struct text_component
*text
,
327 struct bt_value
*params
)
329 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
330 enum bt_value_status status
;
334 text
->plugin_opt_map
= bt_value_map_create();
335 if (!text
->plugin_opt_map
) {
336 ret
= BT_COMPONENT_STATUS_ERROR
;
339 ret
= add_params_to_map(text
->plugin_opt_map
);
340 if (ret
!= BT_COMPONENT_STATUS_OK
) {
343 /* Report unknown parameters. */
344 status
= bt_value_map_foreach(params
, check_param_exists
, text
);
346 case BT_VALUE_STATUS_OK
:
349 ret
= BT_COMPONENT_STATUS_ERROR
;
352 /* Known parameters. */
353 text
->options
.color
= TEXT_COLOR_OPT_AUTO
;
354 if (bt_value_map_has_key(params
, "color")) {
355 struct bt_value
*color_value
;
358 color_value
= bt_value_map_get(params
, "color");
363 status
= bt_value_string_get(color_value
, &color
);
365 warn_wrong_color_param(text
);
367 if (strcmp(color
, "never") == 0) {
368 text
->options
.color
= TEXT_COLOR_OPT_NEVER
;
369 } else if (strcmp(color
, "auto") == 0) {
370 text
->options
.color
= TEXT_COLOR_OPT_AUTO
;
371 } else if (strcmp(color
, "always") == 0) {
372 text
->options
.color
= TEXT_COLOR_OPT_ALWAYS
;
374 warn_wrong_color_param(text
);
381 ret
= apply_one_string("output-path",
383 &text
->options
.output_path
);
384 if (ret
!= BT_COMPONENT_STATUS_OK
) {
388 ret
= apply_one_string("debug-info-dir",
390 &text
->options
.debug_info_dir
);
391 if (ret
!= BT_COMPONENT_STATUS_OK
) {
395 ret
= apply_one_string("debug-info-target-prefix",
397 &text
->options
.debug_info_target_prefix
);
398 if (ret
!= BT_COMPONENT_STATUS_OK
) {
402 value
= false; /* Default. */
403 ret
= apply_one_bool("debug-info-full-path", params
, &value
, NULL
);
404 if (ret
!= BT_COMPONENT_STATUS_OK
) {
407 text
->options
.debug_info_full_path
= value
;
409 value
= false; /* Default. */
410 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
411 if (ret
!= BT_COMPONENT_STATUS_OK
) {
414 text
->options
.print_delta_field
= !value
; /* Reverse logic. */
416 value
= false; /* Default. */
417 ret
= apply_one_bool("clock-cycles", params
, &value
, NULL
);
418 if (ret
!= BT_COMPONENT_STATUS_OK
) {
421 text
->options
.print_timestamp_cycles
= value
;
423 value
= false; /* Default. */
424 ret
= apply_one_bool("clock-seconds", params
, &value
, NULL
);
425 if (ret
!= BT_COMPONENT_STATUS_OK
) {
428 text
->options
.clock_seconds
= value
;
430 value
= false; /* Default. */
431 ret
= apply_one_bool("clock-date", params
, &value
, NULL
);
432 if (ret
!= BT_COMPONENT_STATUS_OK
) {
435 text
->options
.clock_date
= value
;
437 value
= false; /* Default. */
438 ret
= apply_one_bool("clock-gmt", params
, &value
, NULL
);
439 if (ret
!= BT_COMPONENT_STATUS_OK
) {
442 text
->options
.clock_gmt
= value
;
444 value
= false; /* Default. */
445 ret
= apply_one_bool("verbose", params
, &value
, NULL
);
446 if (ret
!= BT_COMPONENT_STATUS_OK
) {
449 text
->options
.verbose
= value
;
452 ret
= apply_one_string("name-default", params
, &str
);
453 if (ret
!= BT_COMPONENT_STATUS_OK
) {
457 text
->options
.name_default
= TEXT_DEFAULT_UNSET
;
458 } else if (!strcmp(str
, "show")) {
459 text
->options
.name_default
= TEXT_DEFAULT_SHOW
;
460 } else if (!strcmp(str
, "hide")) {
461 text
->options
.name_default
= TEXT_DEFAULT_HIDE
;
463 ret
= BT_COMPONENT_STATUS_ERROR
;
469 switch (text
->options
.name_default
) {
470 case TEXT_DEFAULT_UNSET
:
471 text
->options
.print_payload_field_names
= true;
472 text
->options
.print_context_field_names
= true;
473 text
->options
.print_header_field_names
= false;
474 text
->options
.print_scope_field_names
= false;
476 case TEXT_DEFAULT_SHOW
:
477 text
->options
.print_payload_field_names
= true;
478 text
->options
.print_context_field_names
= true;
479 text
->options
.print_header_field_names
= true;
480 text
->options
.print_scope_field_names
= true;
482 case TEXT_DEFAULT_HIDE
:
483 text
->options
.print_payload_field_names
= false;
484 text
->options
.print_context_field_names
= false;
485 text
->options
.print_header_field_names
= false;
486 text
->options
.print_scope_field_names
= false;
489 ret
= BT_COMPONENT_STATUS_ERROR
;
495 ret
= apply_one_bool("name-payload", params
, &value
, &found
);
496 if (ret
!= BT_COMPONENT_STATUS_OK
) {
500 text
->options
.print_payload_field_names
= value
;
505 ret
= apply_one_bool("name-context", params
, &value
, &found
);
506 if (ret
!= BT_COMPONENT_STATUS_OK
) {
510 text
->options
.print_context_field_names
= value
;
515 ret
= apply_one_bool("name-header", params
, &value
, &found
);
516 if (ret
!= BT_COMPONENT_STATUS_OK
) {
520 text
->options
.print_header_field_names
= value
;
525 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
526 if (ret
!= BT_COMPONENT_STATUS_OK
) {
530 text
->options
.print_scope_field_names
= value
;
534 ret
= apply_one_string("field-default", params
, &str
);
535 if (ret
!= BT_COMPONENT_STATUS_OK
) {
539 text
->options
.field_default
= TEXT_DEFAULT_UNSET
;
540 } else if (!strcmp(str
, "show")) {
541 text
->options
.field_default
= TEXT_DEFAULT_SHOW
;
542 } else if (!strcmp(str
, "hide")) {
543 text
->options
.field_default
= TEXT_DEFAULT_HIDE
;
545 ret
= BT_COMPONENT_STATUS_ERROR
;
551 switch (text
->options
.field_default
) {
552 case TEXT_DEFAULT_UNSET
:
553 text
->options
.print_trace_field
= false;
554 text
->options
.print_trace_hostname_field
= true;
555 text
->options
.print_trace_domain_field
= false;
556 text
->options
.print_trace_procname_field
= true;
557 text
->options
.print_trace_vpid_field
= true;
558 text
->options
.print_loglevel_field
= false;
559 text
->options
.print_emf_field
= false;
560 text
->options
.print_callsite_field
= false;
562 case TEXT_DEFAULT_SHOW
:
563 text
->options
.print_trace_field
= true;
564 text
->options
.print_trace_hostname_field
= true;
565 text
->options
.print_trace_domain_field
= true;
566 text
->options
.print_trace_procname_field
= true;
567 text
->options
.print_trace_vpid_field
= true;
568 text
->options
.print_loglevel_field
= true;
569 text
->options
.print_emf_field
= true;
570 text
->options
.print_callsite_field
= true;
572 case TEXT_DEFAULT_HIDE
:
573 text
->options
.print_trace_field
= false;
574 text
->options
.print_trace_hostname_field
= false;
575 text
->options
.print_trace_domain_field
= false;
576 text
->options
.print_trace_procname_field
= false;
577 text
->options
.print_trace_vpid_field
= false;
578 text
->options
.print_loglevel_field
= false;
579 text
->options
.print_emf_field
= false;
580 text
->options
.print_callsite_field
= false;
583 ret
= BT_COMPONENT_STATUS_ERROR
;
589 ret
= apply_one_bool("field-trace", params
, &value
, &found
);
590 if (ret
!= BT_COMPONENT_STATUS_OK
) {
594 text
->options
.print_trace_field
= value
;
599 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
600 if (ret
!= BT_COMPONENT_STATUS_OK
) {
604 text
->options
.print_trace_hostname_field
= value
;
609 ret
= apply_one_bool("field-trace:domain", params
, &value
, &found
);
610 if (ret
!= BT_COMPONENT_STATUS_OK
) {
614 text
->options
.print_trace_domain_field
= value
;
619 ret
= apply_one_bool("field-trace:procname", params
, &value
, &found
);
620 if (ret
!= BT_COMPONENT_STATUS_OK
) {
624 text
->options
.print_trace_procname_field
= value
;
629 ret
= apply_one_bool("field-trace:vpid", params
, &value
, &found
);
630 if (ret
!= BT_COMPONENT_STATUS_OK
) {
634 text
->options
.print_trace_vpid_field
= value
;
639 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
640 if (ret
!= BT_COMPONENT_STATUS_OK
) {
644 text
->options
.print_loglevel_field
= value
;
649 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
650 if (ret
!= BT_COMPONENT_STATUS_OK
) {
654 text
->options
.print_emf_field
= value
;
659 ret
= apply_one_bool("field-callsite", params
, &value
, &found
);
660 if (ret
!= BT_COMPONENT_STATUS_OK
) {
664 text
->options
.print_callsite_field
= value
;
668 bt_put(text
->plugin_opt_map
);
669 text
->plugin_opt_map
= NULL
;
675 void set_use_colors(struct text_component
*text
)
677 switch (text
->options
.color
) {
678 case TEXT_COLOR_OPT_ALWAYS
:
679 text
->use_colors
= true;
681 case TEXT_COLOR_OPT_AUTO
:
682 text
->use_colors
= text
->out
== stdout
&&
683 bt_common_colors_supported();
685 case TEXT_COLOR_OPT_NEVER
:
686 text
->use_colors
= false;
692 void init_stream_packet_context_quarks(void)
694 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
695 g_quark_from_string("timestamp_begin");
696 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
697 g_quark_from_string("timestamp_begin");
698 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
699 g_quark_from_string("timestamp_end");
700 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
701 g_quark_from_string("events_discarded");
702 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
703 g_quark_from_string("content_size");
704 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
705 g_quark_from_string("packet_size");
706 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
707 g_quark_from_string("packet_seq_num");
711 enum bt_component_status
text_component_init(
712 struct bt_private_component
*component
,
713 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_private_component_set_user_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_ACCEPT_PORT_CONNECTION_METHOD(text
, text_accept_port_connection
);
762 BT_PLUGIN_SINK_COMPONENT_CLASS_FINALIZE_METHOD(text
, finalize_text
);
763 BT_PLUGIN_SINK_COMPONENT_CLASS_DESCRIPTION(text
,
764 "Formats CTF-IR to text. Formerly known as ctf-text.");