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/babeltrace.h>
31 #include <babeltrace/values.h>
32 #include <babeltrace/compiler-internal.h>
33 #include <babeltrace/common-internal.h>
34 #include <plugins-common.h>
38 #include <babeltrace/assert-internal.h>
42 GQuark stream_packet_context_quarks
[STREAM_PACKET_CONTEXT_QUARKS_LEN
];
45 const char *plugin_options
[] = {
54 "name-default", /* show/hide */
59 "field-default", /* show/hide */
61 "field-trace:hostname",
63 "field-trace:procname",
71 void destroy_pretty_data(struct pretty_component
*pretty
)
73 bt_put(pretty
->input_iterator
);
76 (void) g_string_free(pretty
->string
, TRUE
);
79 if (pretty
->tmp_string
) {
80 (void) g_string_free(pretty
->tmp_string
, TRUE
);
83 if (pretty
->out
!= stdout
) {
86 ret
= fclose(pretty
->out
);
88 perror("close output file");
91 g_free(pretty
->options
.output_path
);
96 struct pretty_component
*create_pretty(void)
98 struct pretty_component
*pretty
;
100 pretty
= g_new0(struct pretty_component
, 1);
104 pretty
->string
= g_string_new("");
105 if (!pretty
->string
) {
108 pretty
->tmp_string
= g_string_new("");
109 if (!pretty
->tmp_string
) {
121 void pretty_finalize(struct bt_private_component
*component
)
123 void *data
= bt_private_component_get_user_data(component
);
125 destroy_pretty_data(data
);
129 enum bt_component_status
handle_notification(struct pretty_component
*pretty
,
130 struct bt_notification
*notification
)
132 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
136 switch (bt_notification_get_type(notification
)) {
137 case BT_NOTIFICATION_TYPE_EVENT
:
138 ret
= pretty_print_event(pretty
, notification
);
140 case BT_NOTIFICATION_TYPE_INACTIVITY
:
141 fprintf(stderr
, "Inactivity notification\n");
143 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
:
144 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
:
145 ret
= pretty_print_discarded_elements(pretty
, notification
);
155 void pretty_port_connected(
156 struct bt_private_component
*component
,
157 struct bt_private_port
*self_port
,
158 struct bt_port
*other_port
)
160 enum bt_connection_status conn_status
;
161 struct bt_private_connection
*connection
;
162 struct pretty_component
*pretty
;
164 pretty
= bt_private_component_get_user_data(component
);
166 BT_ASSERT(!pretty
->input_iterator
);
167 connection
= bt_private_port_get_private_connection(self_port
);
168 BT_ASSERT(connection
);
169 conn_status
= bt_private_connection_create_notification_iterator(
170 connection
, &pretty
->input_iterator
);
171 if (conn_status
!= BT_CONNECTION_STATUS_OK
) {
172 pretty
->error
= true;
179 enum bt_component_status
pretty_consume(struct bt_private_component
*component
)
181 enum bt_component_status ret
;
182 struct bt_notification
*notification
= NULL
;
183 struct bt_notification_iterator
*it
;
184 struct pretty_component
*pretty
=
185 bt_private_component_get_user_data(component
);
186 enum bt_notification_iterator_status it_ret
;
188 if (unlikely(pretty
->error
)) {
189 ret
= BT_COMPONENT_STATUS_ERROR
;
193 it
= pretty
->input_iterator
;
194 it_ret
= bt_notification_iterator_next(it
);
197 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
198 ret
= BT_COMPONENT_STATUS_END
;
199 BT_PUT(pretty
->input_iterator
);
201 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
202 ret
= BT_COMPONENT_STATUS_AGAIN
;
204 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
207 ret
= BT_COMPONENT_STATUS_ERROR
;
211 notification
= bt_notification_iterator_borrow_notification(it
);
212 BT_ASSERT(notification
);
213 ret
= handle_notification(pretty
, notification
);
220 enum bt_component_status
add_params_to_map(struct bt_value
*plugin_opt_map
)
222 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
225 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
226 const char *key
= plugin_options
[i
];
227 enum bt_value_status status
;
229 status
= bt_value_map_insert(plugin_opt_map
, key
, bt_value_null
);
231 case BT_VALUE_STATUS_OK
:
234 ret
= BT_COMPONENT_STATUS_ERROR
;
243 bt_bool
check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
245 struct pretty_component
*pretty
= data
;
246 struct bt_value
*plugin_opt_map
= pretty
->plugin_opt_map
;
248 if (!bt_value_map_has_key(plugin_opt_map
, key
)) {
250 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key
);
256 enum bt_component_status
apply_one_string(const char *key
,
257 struct bt_value
*params
,
260 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
261 struct bt_value
*value
= NULL
;
262 enum bt_value_status status
;
265 value
= bt_value_map_borrow(params
, key
);
269 if (bt_value_is_null(value
)) {
272 status
= bt_value_string_get(value
, &str
);
274 case BT_VALUE_STATUS_OK
:
277 ret
= BT_COMPONENT_STATUS_ERROR
;
280 *option
= g_strdup(str
);
286 enum bt_component_status
apply_one_bool(const char *key
,
287 struct bt_value
*params
,
291 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
292 struct bt_value
*value
= NULL
;
293 enum bt_value_status status
;
296 value
= bt_value_map_borrow(params
, key
);
300 status
= bt_value_bool_get(value
, &bool_val
);
302 case BT_VALUE_STATUS_OK
:
305 ret
= BT_COMPONENT_STATUS_ERROR
;
308 *option
= (bool) bool_val
;
318 void warn_wrong_color_param(struct pretty_component
*pretty
)
321 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
325 enum bt_component_status
open_output_file(struct pretty_component
*pretty
)
327 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
329 if (!pretty
->options
.output_path
) {
333 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
341 ret
= BT_COMPONENT_STATUS_ERROR
;
347 enum bt_component_status
apply_params(struct pretty_component
*pretty
,
348 struct bt_value
*params
)
350 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
351 enum bt_value_status status
;
355 pretty
->plugin_opt_map
= bt_value_map_create();
356 if (!pretty
->plugin_opt_map
) {
357 ret
= BT_COMPONENT_STATUS_ERROR
;
360 ret
= add_params_to_map(pretty
->plugin_opt_map
);
361 if (ret
!= BT_COMPONENT_STATUS_OK
) {
364 /* Report unknown parameters. */
365 status
= bt_value_map_foreach(params
, check_param_exists
, pretty
);
367 case BT_VALUE_STATUS_OK
:
370 ret
= BT_COMPONENT_STATUS_ERROR
;
373 /* Known parameters. */
374 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
375 if (bt_value_map_has_key(params
, "color")) {
376 struct bt_value
*color_value
;
379 color_value
= bt_value_map_borrow(params
, "color");
384 status
= bt_value_string_get(color_value
, &color
);
386 warn_wrong_color_param(pretty
);
388 if (strcmp(color
, "never") == 0) {
389 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
390 } else if (strcmp(color
, "auto") == 0) {
391 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
392 } else if (strcmp(color
, "always") == 0) {
393 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
395 warn_wrong_color_param(pretty
);
400 ret
= apply_one_string("path", params
, &pretty
->options
.output_path
);
401 if (ret
!= BT_COMPONENT_STATUS_OK
) {
404 ret
= open_output_file(pretty
);
405 if (ret
!= BT_COMPONENT_STATUS_OK
) {
409 value
= false; /* Default. */
410 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
411 if (ret
!= BT_COMPONENT_STATUS_OK
) {
414 pretty
->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 pretty
->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 pretty
->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 pretty
->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 pretty
->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 pretty
->options
.verbose
= value
;
452 ret
= apply_one_string("name-default", params
, &str
);
453 if (ret
!= BT_COMPONENT_STATUS_OK
) {
457 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
458 } else if (!strcmp(str
, "show")) {
459 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
460 } else if (!strcmp(str
, "hide")) {
461 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
463 ret
= BT_COMPONENT_STATUS_ERROR
;
469 switch (pretty
->options
.name_default
) {
470 case PRETTY_DEFAULT_UNSET
:
471 pretty
->options
.print_payload_field_names
= true;
472 pretty
->options
.print_context_field_names
= true;
473 pretty
->options
.print_header_field_names
= false;
474 pretty
->options
.print_scope_field_names
= false;
476 case PRETTY_DEFAULT_SHOW
:
477 pretty
->options
.print_payload_field_names
= true;
478 pretty
->options
.print_context_field_names
= true;
479 pretty
->options
.print_header_field_names
= true;
480 pretty
->options
.print_scope_field_names
= true;
482 case PRETTY_DEFAULT_HIDE
:
483 pretty
->options
.print_payload_field_names
= false;
484 pretty
->options
.print_context_field_names
= false;
485 pretty
->options
.print_header_field_names
= false;
486 pretty
->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 pretty
->options
.print_payload_field_names
= value
;
505 ret
= apply_one_bool("name-context", params
, &value
, &found
);
506 if (ret
!= BT_COMPONENT_STATUS_OK
) {
510 pretty
->options
.print_context_field_names
= value
;
515 ret
= apply_one_bool("name-header", params
, &value
, &found
);
516 if (ret
!= BT_COMPONENT_STATUS_OK
) {
520 pretty
->options
.print_header_field_names
= value
;
525 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
526 if (ret
!= BT_COMPONENT_STATUS_OK
) {
530 pretty
->options
.print_scope_field_names
= value
;
534 ret
= apply_one_string("field-default", params
, &str
);
535 if (ret
!= BT_COMPONENT_STATUS_OK
) {
539 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
540 } else if (!strcmp(str
, "show")) {
541 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
542 } else if (!strcmp(str
, "hide")) {
543 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
545 ret
= BT_COMPONENT_STATUS_ERROR
;
551 switch (pretty
->options
.field_default
) {
552 case PRETTY_DEFAULT_UNSET
:
553 pretty
->options
.print_trace_field
= false;
554 pretty
->options
.print_trace_hostname_field
= true;
555 pretty
->options
.print_trace_domain_field
= false;
556 pretty
->options
.print_trace_procname_field
= true;
557 pretty
->options
.print_trace_vpid_field
= true;
558 pretty
->options
.print_loglevel_field
= false;
559 pretty
->options
.print_emf_field
= false;
560 pretty
->options
.print_callsite_field
= false;
562 case PRETTY_DEFAULT_SHOW
:
563 pretty
->options
.print_trace_field
= true;
564 pretty
->options
.print_trace_hostname_field
= true;
565 pretty
->options
.print_trace_domain_field
= true;
566 pretty
->options
.print_trace_procname_field
= true;
567 pretty
->options
.print_trace_vpid_field
= true;
568 pretty
->options
.print_loglevel_field
= true;
569 pretty
->options
.print_emf_field
= true;
570 pretty
->options
.print_callsite_field
= true;
572 case PRETTY_DEFAULT_HIDE
:
573 pretty
->options
.print_trace_field
= false;
574 pretty
->options
.print_trace_hostname_field
= false;
575 pretty
->options
.print_trace_domain_field
= false;
576 pretty
->options
.print_trace_procname_field
= false;
577 pretty
->options
.print_trace_vpid_field
= false;
578 pretty
->options
.print_loglevel_field
= false;
579 pretty
->options
.print_emf_field
= false;
580 pretty
->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 pretty
->options
.print_trace_field
= value
;
599 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
600 if (ret
!= BT_COMPONENT_STATUS_OK
) {
604 pretty
->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 pretty
->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 pretty
->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 pretty
->options
.print_trace_vpid_field
= value
;
639 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
640 if (ret
!= BT_COMPONENT_STATUS_OK
) {
644 pretty
->options
.print_loglevel_field
= value
;
649 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
650 if (ret
!= BT_COMPONENT_STATUS_OK
) {
654 pretty
->options
.print_emf_field
= value
;
659 ret
= apply_one_bool("field-callsite", params
, &value
, &found
);
660 if (ret
!= BT_COMPONENT_STATUS_OK
) {
664 pretty
->options
.print_callsite_field
= value
;
668 bt_put(pretty
->plugin_opt_map
);
669 pretty
->plugin_opt_map
= NULL
;
675 void set_use_colors(struct pretty_component
*pretty
)
677 switch (pretty
->options
.color
) {
678 case PRETTY_COLOR_OPT_ALWAYS
:
679 pretty
->use_colors
= true;
681 case PRETTY_COLOR_OPT_AUTO
:
682 pretty
->use_colors
= pretty
->out
== stdout
&&
683 bt_common_colors_supported();
685 case PRETTY_COLOR_OPT_NEVER
:
686 pretty
->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
pretty_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 pretty_component
*pretty
= create_pretty();
720 ret
= BT_COMPONENT_STATUS_NOMEM
;
724 ret
= bt_private_component_sink_add_input_private_port(component
,
726 if (ret
!= BT_COMPONENT_STATUS_OK
) {
730 pretty
->out
= stdout
;
731 pretty
->err
= stderr
;
733 pretty
->delta_cycles
= -1ULL;
734 pretty
->last_cycles_timestamp
= -1ULL;
736 pretty
->delta_real_timestamp
= -1ULL;
737 pretty
->last_real_timestamp
= -1ULL;
739 ret
= apply_params(pretty
, params
);
740 if (ret
!= BT_COMPONENT_STATUS_OK
) {
744 set_use_colors(pretty
);
745 ret
= bt_private_component_set_user_data(component
, pretty
);
746 if (ret
!= BT_COMPONENT_STATUS_OK
) {
750 init_stream_packet_context_quarks();
755 destroy_pretty_data(pretty
);