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_object_put_ref(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_PACKET_BEGIN
:
138 ret
= pretty_print_packet(pretty
, notification
);
140 case BT_NOTIFICATION_TYPE_EVENT
:
141 ret
= pretty_print_event(pretty
, notification
);
143 case BT_NOTIFICATION_TYPE_INACTIVITY
:
144 fprintf(stderr
, "Inactivity notification\n");
154 enum bt_component_status
pretty_port_connected(
155 struct bt_private_component
*component
,
156 struct bt_private_port
*self_port
,
157 struct bt_port
*other_port
)
159 enum bt_component_status status
= BT_COMPONENT_STATUS_OK
;
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 status
= BT_COMPONENT_STATUS_ERROR
;
175 bt_object_put_ref(connection
);
180 enum bt_component_status
pretty_consume(struct bt_private_component
*component
)
182 enum bt_component_status ret
;
183 bt_notification_array notifs
;
184 struct bt_notification_iterator
*it
;
185 struct pretty_component
*pretty
=
186 bt_private_component_get_user_data(component
);
187 enum bt_notification_iterator_status it_ret
;
191 it
= pretty
->input_iterator
;
192 it_ret
= bt_private_connection_notification_iterator_next(it
, ¬ifs
,
196 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
197 ret
= BT_COMPONENT_STATUS_END
;
198 BT_OBJECT_PUT_REF_AND_RESET(pretty
->input_iterator
);
200 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
201 ret
= BT_COMPONENT_STATUS_AGAIN
;
203 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
206 ret
= BT_COMPONENT_STATUS_ERROR
;
210 BT_ASSERT(it_ret
== BT_NOTIFICATION_ITERATOR_STATUS_OK
);
212 for (i
= 0; i
< count
; i
++) {
213 ret
= handle_notification(pretty
, notifs
[i
]);
218 bt_object_put_ref(notifs
[i
]);
222 for (; i
< count
; i
++) {
223 bt_object_put_ref(notifs
[i
]);
230 enum bt_component_status
add_params_to_map(
231 struct bt_private_value
*plugin_opt_map
)
233 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
236 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
237 const char *key
= plugin_options
[i
];
238 enum bt_value_status status
;
240 status
= bt_private_value_map_insert_entry(plugin_opt_map
, key
,
243 case BT_VALUE_STATUS_OK
:
246 ret
= BT_COMPONENT_STATUS_ERROR
;
255 bt_bool
check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
257 struct pretty_component
*pretty
= data
;
259 if (!bt_value_map_has_entry(
260 bt_value_borrow_from_private(pretty
->plugin_opt_map
),
263 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key
);
269 enum bt_component_status
apply_one_string(const char *key
,
270 struct bt_value
*params
,
273 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
274 struct bt_value
*value
= NULL
;
277 value
= bt_value_map_borrow_entry_value(params
, key
);
281 if (bt_value_is_null(value
)) {
284 str
= bt_value_string_get(value
);
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
;
301 value
= bt_value_map_borrow_entry_value(params
, key
);
305 bool_val
= bt_value_bool_get(value
);
306 *option
= (bool) bool_val
;
316 void warn_wrong_color_param(struct pretty_component
*pretty
)
319 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
323 enum bt_component_status
open_output_file(struct pretty_component
*pretty
)
325 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
327 if (!pretty
->options
.output_path
) {
331 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
339 ret
= BT_COMPONENT_STATUS_ERROR
;
345 enum bt_component_status
apply_params(struct pretty_component
*pretty
,
346 struct bt_value
*params
)
348 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
349 enum bt_value_status status
;
353 pretty
->plugin_opt_map
= bt_private_value_map_create();
354 if (!pretty
->plugin_opt_map
) {
355 ret
= BT_COMPONENT_STATUS_ERROR
;
358 ret
= add_params_to_map(pretty
->plugin_opt_map
);
359 if (ret
!= BT_COMPONENT_STATUS_OK
) {
362 /* Report unknown parameters. */
363 status
= bt_value_map_foreach_entry(params
, check_param_exists
, pretty
);
365 case BT_VALUE_STATUS_OK
:
368 ret
= BT_COMPONENT_STATUS_ERROR
;
371 /* Known parameters. */
372 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
373 if (bt_value_map_has_entry(params
, "color")) {
374 struct bt_value
*color_value
;
377 color_value
= bt_value_map_borrow_entry_value(params
, "color");
382 color
= bt_value_string_get(color_value
);
384 if (strcmp(color
, "never") == 0) {
385 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
386 } else if (strcmp(color
, "auto") == 0) {
387 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
388 } else if (strcmp(color
, "always") == 0) {
389 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
391 warn_wrong_color_param(pretty
);
395 ret
= apply_one_string("path", params
, &pretty
->options
.output_path
);
396 if (ret
!= BT_COMPONENT_STATUS_OK
) {
399 ret
= open_output_file(pretty
);
400 if (ret
!= BT_COMPONENT_STATUS_OK
) {
404 value
= false; /* Default. */
405 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
406 if (ret
!= BT_COMPONENT_STATUS_OK
) {
409 pretty
->options
.print_delta_field
= !value
; /* Reverse logic. */
411 value
= false; /* Default. */
412 ret
= apply_one_bool("clock-cycles", params
, &value
, NULL
);
413 if (ret
!= BT_COMPONENT_STATUS_OK
) {
416 pretty
->options
.print_timestamp_cycles
= value
;
418 value
= false; /* Default. */
419 ret
= apply_one_bool("clock-seconds", params
, &value
, NULL
);
420 if (ret
!= BT_COMPONENT_STATUS_OK
) {
423 pretty
->options
.clock_seconds
= value
;
425 value
= false; /* Default. */
426 ret
= apply_one_bool("clock-date", params
, &value
, NULL
);
427 if (ret
!= BT_COMPONENT_STATUS_OK
) {
430 pretty
->options
.clock_date
= value
;
432 value
= false; /* Default. */
433 ret
= apply_one_bool("clock-gmt", params
, &value
, NULL
);
434 if (ret
!= BT_COMPONENT_STATUS_OK
) {
437 pretty
->options
.clock_gmt
= value
;
439 value
= false; /* Default. */
440 ret
= apply_one_bool("verbose", params
, &value
, NULL
);
441 if (ret
!= BT_COMPONENT_STATUS_OK
) {
444 pretty
->options
.verbose
= value
;
447 ret
= apply_one_string("name-default", params
, &str
);
448 if (ret
!= BT_COMPONENT_STATUS_OK
) {
452 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
453 } else if (!strcmp(str
, "show")) {
454 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
455 } else if (!strcmp(str
, "hide")) {
456 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
458 ret
= BT_COMPONENT_STATUS_ERROR
;
464 switch (pretty
->options
.name_default
) {
465 case PRETTY_DEFAULT_UNSET
:
466 pretty
->options
.print_payload_field_names
= true;
467 pretty
->options
.print_context_field_names
= true;
468 pretty
->options
.print_header_field_names
= false;
469 pretty
->options
.print_scope_field_names
= false;
471 case PRETTY_DEFAULT_SHOW
:
472 pretty
->options
.print_payload_field_names
= true;
473 pretty
->options
.print_context_field_names
= true;
474 pretty
->options
.print_header_field_names
= true;
475 pretty
->options
.print_scope_field_names
= true;
477 case PRETTY_DEFAULT_HIDE
:
478 pretty
->options
.print_payload_field_names
= false;
479 pretty
->options
.print_context_field_names
= false;
480 pretty
->options
.print_header_field_names
= false;
481 pretty
->options
.print_scope_field_names
= false;
484 ret
= BT_COMPONENT_STATUS_ERROR
;
490 ret
= apply_one_bool("name-payload", params
, &value
, &found
);
491 if (ret
!= BT_COMPONENT_STATUS_OK
) {
495 pretty
->options
.print_payload_field_names
= value
;
500 ret
= apply_one_bool("name-context", params
, &value
, &found
);
501 if (ret
!= BT_COMPONENT_STATUS_OK
) {
505 pretty
->options
.print_context_field_names
= value
;
510 ret
= apply_one_bool("name-header", params
, &value
, &found
);
511 if (ret
!= BT_COMPONENT_STATUS_OK
) {
515 pretty
->options
.print_header_field_names
= value
;
520 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
521 if (ret
!= BT_COMPONENT_STATUS_OK
) {
525 pretty
->options
.print_scope_field_names
= value
;
529 ret
= apply_one_string("field-default", params
, &str
);
530 if (ret
!= BT_COMPONENT_STATUS_OK
) {
534 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
535 } else if (!strcmp(str
, "show")) {
536 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
537 } else if (!strcmp(str
, "hide")) {
538 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
540 ret
= BT_COMPONENT_STATUS_ERROR
;
546 switch (pretty
->options
.field_default
) {
547 case PRETTY_DEFAULT_UNSET
:
548 pretty
->options
.print_trace_field
= false;
549 pretty
->options
.print_trace_hostname_field
= true;
550 pretty
->options
.print_trace_domain_field
= false;
551 pretty
->options
.print_trace_procname_field
= true;
552 pretty
->options
.print_trace_vpid_field
= true;
553 pretty
->options
.print_loglevel_field
= false;
554 pretty
->options
.print_emf_field
= false;
555 pretty
->options
.print_callsite_field
= false;
557 case PRETTY_DEFAULT_SHOW
:
558 pretty
->options
.print_trace_field
= true;
559 pretty
->options
.print_trace_hostname_field
= true;
560 pretty
->options
.print_trace_domain_field
= true;
561 pretty
->options
.print_trace_procname_field
= true;
562 pretty
->options
.print_trace_vpid_field
= true;
563 pretty
->options
.print_loglevel_field
= true;
564 pretty
->options
.print_emf_field
= true;
565 pretty
->options
.print_callsite_field
= true;
567 case PRETTY_DEFAULT_HIDE
:
568 pretty
->options
.print_trace_field
= false;
569 pretty
->options
.print_trace_hostname_field
= false;
570 pretty
->options
.print_trace_domain_field
= false;
571 pretty
->options
.print_trace_procname_field
= false;
572 pretty
->options
.print_trace_vpid_field
= false;
573 pretty
->options
.print_loglevel_field
= false;
574 pretty
->options
.print_emf_field
= false;
575 pretty
->options
.print_callsite_field
= false;
578 ret
= BT_COMPONENT_STATUS_ERROR
;
584 ret
= apply_one_bool("field-trace", params
, &value
, &found
);
585 if (ret
!= BT_COMPONENT_STATUS_OK
) {
589 pretty
->options
.print_trace_field
= value
;
594 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
595 if (ret
!= BT_COMPONENT_STATUS_OK
) {
599 pretty
->options
.print_trace_hostname_field
= value
;
604 ret
= apply_one_bool("field-trace:domain", params
, &value
, &found
);
605 if (ret
!= BT_COMPONENT_STATUS_OK
) {
609 pretty
->options
.print_trace_domain_field
= value
;
614 ret
= apply_one_bool("field-trace:procname", params
, &value
, &found
);
615 if (ret
!= BT_COMPONENT_STATUS_OK
) {
619 pretty
->options
.print_trace_procname_field
= value
;
624 ret
= apply_one_bool("field-trace:vpid", params
, &value
, &found
);
625 if (ret
!= BT_COMPONENT_STATUS_OK
) {
629 pretty
->options
.print_trace_vpid_field
= value
;
634 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
635 if (ret
!= BT_COMPONENT_STATUS_OK
) {
639 pretty
->options
.print_loglevel_field
= value
;
644 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
645 if (ret
!= BT_COMPONENT_STATUS_OK
) {
649 pretty
->options
.print_emf_field
= value
;
654 ret
= apply_one_bool("field-callsite", params
, &value
, &found
);
655 if (ret
!= BT_COMPONENT_STATUS_OK
) {
659 pretty
->options
.print_callsite_field
= value
;
663 bt_object_put_ref(pretty
->plugin_opt_map
);
664 pretty
->plugin_opt_map
= NULL
;
670 void set_use_colors(struct pretty_component
*pretty
)
672 switch (pretty
->options
.color
) {
673 case PRETTY_COLOR_OPT_ALWAYS
:
674 pretty
->use_colors
= true;
676 case PRETTY_COLOR_OPT_AUTO
:
677 pretty
->use_colors
= pretty
->out
== stdout
&&
678 bt_common_colors_supported();
680 case PRETTY_COLOR_OPT_NEVER
:
681 pretty
->use_colors
= false;
687 void init_stream_packet_context_quarks(void)
689 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
690 g_quark_from_string("timestamp_begin");
691 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
692 g_quark_from_string("timestamp_begin");
693 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
694 g_quark_from_string("timestamp_end");
695 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
696 g_quark_from_string("events_discarded");
697 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
698 g_quark_from_string("content_size");
699 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
700 g_quark_from_string("packet_size");
701 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
702 g_quark_from_string("packet_seq_num");
706 enum bt_component_status
pretty_init(
707 struct bt_private_component
*component
,
708 struct bt_value
*params
,
709 UNUSED_VAR
void *init_method_data
)
711 enum bt_component_status ret
;
712 struct pretty_component
*pretty
= create_pretty();
715 ret
= BT_COMPONENT_STATUS_NOMEM
;
719 ret
= bt_private_component_sink_add_input_private_port(component
,
721 if (ret
!= BT_COMPONENT_STATUS_OK
) {
725 pretty
->out
= stdout
;
726 pretty
->err
= stderr
;
728 pretty
->delta_cycles
= -1ULL;
729 pretty
->last_cycles_timestamp
= -1ULL;
731 pretty
->delta_real_timestamp
= -1ULL;
732 pretty
->last_real_timestamp
= -1ULL;
734 ret
= apply_params(pretty
, params
);
735 if (ret
!= BT_COMPONENT_STATUS_OK
) {
739 set_use_colors(pretty
);
740 ret
= bt_private_component_set_user_data(component
, pretty
);
741 if (ret
!= BT_COMPONENT_STATUS_OK
) {
745 init_stream_packet_context_quarks();
750 destroy_pretty_data(pretty
);