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-internal.h>
43 #include <babeltrace/common-internal.h>
44 #include <plugins-common.h>
53 const char *plugin_options
[] = {
57 "debug-info-target-prefix",
58 "debug-info-full-path",
65 "name-default", /* show/hide */
70 "field-default", /* show/hide */
72 "field-trace:hostname",
74 "field-trace:procname",
82 void destroy_pretty_data(struct pretty_component
*pretty
)
84 bt_put(pretty
->input_iterator
);
85 (void) g_string_free(pretty
->string
, TRUE
);
86 if (pretty
->out
!= stdout
) {
89 ret
= fclose(pretty
->out
);
91 perror("close output file");
94 g_free(pretty
->options
.output_path
);
95 g_free(pretty
->options
.debug_info_dir
);
96 g_free(pretty
->options
.debug_info_target_prefix
);
101 struct pretty_component
*create_pretty(void)
103 struct pretty_component
*pretty
;
105 pretty
= g_new0(struct pretty_component
, 1);
109 pretty
->string
= g_string_new("");
110 if (!pretty
->string
) {
122 void pretty_finalize(struct bt_private_component
*component
)
124 void *data
= bt_private_component_get_user_data(component
);
126 destroy_pretty_data(data
);
130 enum bt_component_status
handle_notification(struct pretty_component
*pretty
,
131 struct bt_notification
*notification
)
133 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
136 ret
= BT_COMPONENT_STATUS_ERROR
;
140 switch (bt_notification_get_type(notification
)) {
141 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
143 case BT_NOTIFICATION_TYPE_PACKET_END
:
145 case BT_NOTIFICATION_TYPE_EVENT
:
147 ret
= pretty_print_event(pretty
, notification
);
148 if (ret
!= BT_COMPONENT_STATUS_OK
) {
153 case BT_NOTIFICATION_TYPE_STREAM_END
:
156 puts("Unhandled notification type");
163 void pretty_port_connected(
164 struct bt_private_component
*component
,
165 struct bt_private_port
*self_port
,
166 struct bt_port
*other_port
)
168 struct bt_private_connection
*connection
;
169 struct pretty_component
*pretty
;
171 pretty
= bt_private_component_get_user_data(component
);
173 assert(!pretty
->input_iterator
);
174 connection
= bt_private_port_get_private_connection(self_port
);
176 pretty
->input_iterator
=
177 bt_private_connection_create_notification_iterator(connection
);
179 if (!pretty
->input_iterator
) {
180 pretty
->error
= true;
187 enum bt_component_status
pretty_consume(struct bt_private_component
*component
)
189 enum bt_component_status ret
;
190 struct bt_notification
*notification
= NULL
;
191 struct bt_notification_iterator
*it
;
192 struct pretty_component
*pretty
=
193 bt_private_component_get_user_data(component
);
194 enum bt_notification_iterator_status it_ret
;
196 if (unlikely(pretty
->error
)) {
197 ret
= BT_COMPONENT_STATUS_ERROR
;
201 it
= pretty
->input_iterator
;
202 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(pretty
->input_iterator
);
216 notification
= bt_notification_iterator_get_notification(it
);
217 assert(notification
);
218 ret
= handle_notification(pretty
, notification
);
220 bt_put(notification
);
225 enum bt_component_status
add_params_to_map(struct bt_value
*plugin_opt_map
)
227 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
230 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
231 const char *key
= plugin_options
[i
];
232 enum bt_value_status status
;
234 status
= bt_value_map_insert(plugin_opt_map
, key
, bt_value_null
);
236 case BT_VALUE_STATUS_OK
:
239 ret
= BT_COMPONENT_STATUS_ERROR
;
248 bool check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
250 struct pretty_component
*pretty
= data
;
251 struct bt_value
*plugin_opt_map
= pretty
->plugin_opt_map
;
253 if (!bt_value_map_get(plugin_opt_map
, key
)) {
255 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key
);
261 enum bt_component_status
apply_one_string(const char *key
,
262 struct bt_value
*params
,
265 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
266 struct bt_value
*value
= NULL
;
267 enum bt_value_status status
;
270 value
= bt_value_map_get(params
, key
);
274 if (bt_value_is_null(value
)) {
277 status
= bt_value_string_get(value
, &str
);
279 case BT_VALUE_STATUS_OK
:
282 ret
= BT_COMPONENT_STATUS_ERROR
;
285 *option
= g_strdup(str
);
292 enum bt_component_status
apply_one_bool(const char *key
,
293 struct bt_value
*params
,
297 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
298 struct bt_value
*value
= NULL
;
299 enum bt_value_status status
;
301 value
= bt_value_map_get(params
, key
);
305 status
= bt_value_bool_get(value
, option
);
307 case BT_VALUE_STATUS_OK
:
310 ret
= BT_COMPONENT_STATUS_ERROR
;
322 void warn_wrong_color_param(struct pretty_component
*pretty
)
325 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
329 enum bt_component_status
open_output_file(struct pretty_component
*pretty
)
331 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
333 if (!pretty
->options
.output_path
) {
337 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
345 ret
= BT_COMPONENT_STATUS_ERROR
;
351 enum bt_component_status
apply_params(struct pretty_component
*pretty
,
352 struct bt_value
*params
)
354 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
355 enum bt_value_status status
;
359 pretty
->plugin_opt_map
= bt_value_map_create();
360 if (!pretty
->plugin_opt_map
) {
361 ret
= BT_COMPONENT_STATUS_ERROR
;
364 ret
= add_params_to_map(pretty
->plugin_opt_map
);
365 if (ret
!= BT_COMPONENT_STATUS_OK
) {
368 /* Report unknown parameters. */
369 status
= bt_value_map_foreach(params
, check_param_exists
, pretty
);
371 case BT_VALUE_STATUS_OK
:
374 ret
= BT_COMPONENT_STATUS_ERROR
;
377 /* Known parameters. */
378 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
379 if (bt_value_map_has_key(params
, "color")) {
380 struct bt_value
*color_value
;
383 color_value
= bt_value_map_get(params
, "color");
388 status
= bt_value_string_get(color_value
, &color
);
390 warn_wrong_color_param(pretty
);
392 if (strcmp(color
, "never") == 0) {
393 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
394 } else if (strcmp(color
, "auto") == 0) {
395 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
396 } else if (strcmp(color
, "always") == 0) {
397 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
399 warn_wrong_color_param(pretty
);
406 ret
= apply_one_string("output-path",
408 &pretty
->options
.output_path
);
409 if (ret
!= BT_COMPONENT_STATUS_OK
) {
412 ret
= open_output_file(pretty
);
413 if (ret
!= BT_COMPONENT_STATUS_OK
) {
417 ret
= apply_one_string("debug-info-dir",
419 &pretty
->options
.debug_info_dir
);
420 if (ret
!= BT_COMPONENT_STATUS_OK
) {
424 ret
= apply_one_string("debug-info-target-prefix",
426 &pretty
->options
.debug_info_target_prefix
);
427 if (ret
!= BT_COMPONENT_STATUS_OK
) {
431 value
= false; /* Default. */
432 ret
= apply_one_bool("debug-info-full-path", params
, &value
, NULL
);
433 if (ret
!= BT_COMPONENT_STATUS_OK
) {
436 pretty
->options
.debug_info_full_path
= value
;
438 value
= false; /* Default. */
439 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
440 if (ret
!= BT_COMPONENT_STATUS_OK
) {
443 pretty
->options
.print_delta_field
= !value
; /* Reverse logic. */
445 value
= false; /* Default. */
446 ret
= apply_one_bool("clock-cycles", params
, &value
, NULL
);
447 if (ret
!= BT_COMPONENT_STATUS_OK
) {
450 pretty
->options
.print_timestamp_cycles
= value
;
452 value
= false; /* Default. */
453 ret
= apply_one_bool("clock-seconds", params
, &value
, NULL
);
454 if (ret
!= BT_COMPONENT_STATUS_OK
) {
457 pretty
->options
.clock_seconds
= value
;
459 value
= false; /* Default. */
460 ret
= apply_one_bool("clock-date", params
, &value
, NULL
);
461 if (ret
!= BT_COMPONENT_STATUS_OK
) {
464 pretty
->options
.clock_date
= value
;
466 value
= false; /* Default. */
467 ret
= apply_one_bool("clock-gmt", params
, &value
, NULL
);
468 if (ret
!= BT_COMPONENT_STATUS_OK
) {
471 pretty
->options
.clock_gmt
= value
;
473 value
= false; /* Default. */
474 ret
= apply_one_bool("verbose", params
, &value
, NULL
);
475 if (ret
!= BT_COMPONENT_STATUS_OK
) {
478 pretty
->options
.verbose
= value
;
481 ret
= apply_one_string("name-default", params
, &str
);
482 if (ret
!= BT_COMPONENT_STATUS_OK
) {
486 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
487 } else if (!strcmp(str
, "show")) {
488 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
489 } else if (!strcmp(str
, "hide")) {
490 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
492 ret
= BT_COMPONENT_STATUS_ERROR
;
498 switch (pretty
->options
.name_default
) {
499 case PRETTY_DEFAULT_UNSET
:
500 pretty
->options
.print_payload_field_names
= true;
501 pretty
->options
.print_context_field_names
= true;
502 pretty
->options
.print_header_field_names
= false;
503 pretty
->options
.print_scope_field_names
= false;
505 case PRETTY_DEFAULT_SHOW
:
506 pretty
->options
.print_payload_field_names
= true;
507 pretty
->options
.print_context_field_names
= true;
508 pretty
->options
.print_header_field_names
= true;
509 pretty
->options
.print_scope_field_names
= true;
511 case PRETTY_DEFAULT_HIDE
:
512 pretty
->options
.print_payload_field_names
= false;
513 pretty
->options
.print_context_field_names
= false;
514 pretty
->options
.print_header_field_names
= false;
515 pretty
->options
.print_scope_field_names
= false;
518 ret
= BT_COMPONENT_STATUS_ERROR
;
524 ret
= apply_one_bool("name-payload", params
, &value
, &found
);
525 if (ret
!= BT_COMPONENT_STATUS_OK
) {
529 pretty
->options
.print_payload_field_names
= value
;
534 ret
= apply_one_bool("name-context", params
, &value
, &found
);
535 if (ret
!= BT_COMPONENT_STATUS_OK
) {
539 pretty
->options
.print_context_field_names
= value
;
544 ret
= apply_one_bool("name-header", params
, &value
, &found
);
545 if (ret
!= BT_COMPONENT_STATUS_OK
) {
549 pretty
->options
.print_header_field_names
= value
;
554 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
555 if (ret
!= BT_COMPONENT_STATUS_OK
) {
559 pretty
->options
.print_scope_field_names
= value
;
563 ret
= apply_one_string("field-default", params
, &str
);
564 if (ret
!= BT_COMPONENT_STATUS_OK
) {
568 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
569 } else if (!strcmp(str
, "show")) {
570 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
571 } else if (!strcmp(str
, "hide")) {
572 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
574 ret
= BT_COMPONENT_STATUS_ERROR
;
580 switch (pretty
->options
.field_default
) {
581 case PRETTY_DEFAULT_UNSET
:
582 pretty
->options
.print_trace_field
= false;
583 pretty
->options
.print_trace_hostname_field
= true;
584 pretty
->options
.print_trace_domain_field
= false;
585 pretty
->options
.print_trace_procname_field
= true;
586 pretty
->options
.print_trace_vpid_field
= true;
587 pretty
->options
.print_loglevel_field
= false;
588 pretty
->options
.print_emf_field
= false;
589 pretty
->options
.print_callsite_field
= false;
591 case PRETTY_DEFAULT_SHOW
:
592 pretty
->options
.print_trace_field
= true;
593 pretty
->options
.print_trace_hostname_field
= true;
594 pretty
->options
.print_trace_domain_field
= true;
595 pretty
->options
.print_trace_procname_field
= true;
596 pretty
->options
.print_trace_vpid_field
= true;
597 pretty
->options
.print_loglevel_field
= true;
598 pretty
->options
.print_emf_field
= true;
599 pretty
->options
.print_callsite_field
= true;
601 case PRETTY_DEFAULT_HIDE
:
602 pretty
->options
.print_trace_field
= false;
603 pretty
->options
.print_trace_hostname_field
= false;
604 pretty
->options
.print_trace_domain_field
= false;
605 pretty
->options
.print_trace_procname_field
= false;
606 pretty
->options
.print_trace_vpid_field
= false;
607 pretty
->options
.print_loglevel_field
= false;
608 pretty
->options
.print_emf_field
= false;
609 pretty
->options
.print_callsite_field
= false;
612 ret
= BT_COMPONENT_STATUS_ERROR
;
618 ret
= apply_one_bool("field-trace", params
, &value
, &found
);
619 if (ret
!= BT_COMPONENT_STATUS_OK
) {
623 pretty
->options
.print_trace_field
= value
;
628 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
629 if (ret
!= BT_COMPONENT_STATUS_OK
) {
633 pretty
->options
.print_trace_hostname_field
= value
;
638 ret
= apply_one_bool("field-trace:domain", params
, &value
, &found
);
639 if (ret
!= BT_COMPONENT_STATUS_OK
) {
643 pretty
->options
.print_trace_domain_field
= value
;
648 ret
= apply_one_bool("field-trace:procname", params
, &value
, &found
);
649 if (ret
!= BT_COMPONENT_STATUS_OK
) {
653 pretty
->options
.print_trace_procname_field
= value
;
658 ret
= apply_one_bool("field-trace:vpid", params
, &value
, &found
);
659 if (ret
!= BT_COMPONENT_STATUS_OK
) {
663 pretty
->options
.print_trace_vpid_field
= value
;
668 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
669 if (ret
!= BT_COMPONENT_STATUS_OK
) {
673 pretty
->options
.print_loglevel_field
= value
;
678 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
679 if (ret
!= BT_COMPONENT_STATUS_OK
) {
683 pretty
->options
.print_emf_field
= value
;
688 ret
= apply_one_bool("field-callsite", params
, &value
, &found
);
689 if (ret
!= BT_COMPONENT_STATUS_OK
) {
693 pretty
->options
.print_callsite_field
= value
;
697 bt_put(pretty
->plugin_opt_map
);
698 pretty
->plugin_opt_map
= NULL
;
704 void set_use_colors(struct pretty_component
*pretty
)
706 switch (pretty
->options
.color
) {
707 case PRETTY_COLOR_OPT_ALWAYS
:
708 pretty
->use_colors
= true;
710 case PRETTY_COLOR_OPT_AUTO
:
711 pretty
->use_colors
= pretty
->out
== stdout
&&
712 bt_common_colors_supported();
714 case PRETTY_COLOR_OPT_NEVER
:
715 pretty
->use_colors
= false;
721 void init_stream_packet_context_quarks(void)
723 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
724 g_quark_from_string("timestamp_begin");
725 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
726 g_quark_from_string("timestamp_begin");
727 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
728 g_quark_from_string("timestamp_end");
729 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
730 g_quark_from_string("events_discarded");
731 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
732 g_quark_from_string("content_size");
733 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
734 g_quark_from_string("packet_size");
735 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
736 g_quark_from_string("packet_seq_num");
740 enum bt_component_status
pretty_init(
741 struct bt_private_component
*component
,
742 struct bt_value
*params
,
743 UNUSED_VAR
void *init_method_data
)
745 enum bt_component_status ret
;
746 struct pretty_component
*pretty
= create_pretty();
749 ret
= BT_COMPONENT_STATUS_NOMEM
;
753 pretty
->out
= stdout
;
754 pretty
->err
= stderr
;
756 pretty
->delta_cycles
= -1ULL;
757 pretty
->last_cycles_timestamp
= -1ULL;
759 pretty
->delta_real_timestamp
= -1ULL;
760 pretty
->last_real_timestamp
= -1ULL;
762 ret
= apply_params(pretty
, params
);
763 if (ret
!= BT_COMPONENT_STATUS_OK
) {
767 set_use_colors(pretty
);
769 ret
= bt_private_component_set_user_data(component
, pretty
);
770 if (ret
!= BT_COMPONENT_STATUS_OK
) {
774 init_stream_packet_context_quarks();
779 destroy_pretty_data(pretty
);