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/private-component-sink.h>
34 #include <babeltrace/graph/component-sink.h>
35 #include <babeltrace/graph/port.h>
36 #include <babeltrace/graph/private-port.h>
37 #include <babeltrace/graph/connection.h>
38 #include <babeltrace/graph/private-connection.h>
39 #include <babeltrace/graph/notification.h>
40 #include <babeltrace/graph/notification-iterator.h>
41 #include <babeltrace/graph/notification-event.h>
42 #include <babeltrace/values.h>
43 #include <babeltrace/compiler-internal.h>
44 #include <babeltrace/common-internal.h>
45 #include <plugins-common.h>
54 const char *plugin_options
[] = {
63 "name-default", /* show/hide */
68 "field-default", /* show/hide */
70 "field-trace:hostname",
72 "field-trace:procname",
80 void destroy_pretty_data(struct pretty_component
*pretty
)
82 bt_put(pretty
->input_iterator
);
85 (void) g_string_free(pretty
->string
, TRUE
);
88 if (pretty
->tmp_string
) {
89 (void) g_string_free(pretty
->tmp_string
, TRUE
);
92 if (pretty
->out
!= stdout
) {
95 ret
= fclose(pretty
->out
);
97 perror("close output file");
100 g_free(pretty
->options
.output_path
);
105 struct pretty_component
*create_pretty(void)
107 struct pretty_component
*pretty
;
109 pretty
= g_new0(struct pretty_component
, 1);
113 pretty
->string
= g_string_new("");
114 if (!pretty
->string
) {
117 pretty
->tmp_string
= g_string_new("");
118 if (!pretty
->tmp_string
) {
130 void pretty_finalize(struct bt_private_component
*component
)
132 void *data
= bt_private_component_get_user_data(component
);
134 destroy_pretty_data(data
);
138 enum bt_component_status
handle_notification(struct pretty_component
*pretty
,
139 struct bt_notification
*notification
)
141 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
145 switch (bt_notification_get_type(notification
)) {
146 case BT_NOTIFICATION_TYPE_EVENT
:
147 ret
= pretty_print_event(pretty
, notification
);
149 case BT_NOTIFICATION_TYPE_INACTIVITY
:
150 fprintf(stderr
, "Inactivity notification\n");
152 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
153 case BT_NOTIFICATION_TYPE_PACKET_END
:
154 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
155 case BT_NOTIFICATION_TYPE_STREAM_END
:
158 fprintf(stderr
, "Unhandled notification type\n");
165 void pretty_port_connected(
166 struct bt_private_component
*component
,
167 struct bt_private_port
*self_port
,
168 struct bt_port
*other_port
)
170 struct bt_private_connection
*connection
;
171 struct pretty_component
*pretty
;
172 static const enum bt_notification_type notif_types
[] = {
173 BT_NOTIFICATION_TYPE_EVENT
,
174 BT_NOTIFICATION_TYPE_SENTINEL
,
177 pretty
= bt_private_component_get_user_data(component
);
179 assert(!pretty
->input_iterator
);
180 connection
= bt_private_port_get_private_connection(self_port
);
182 pretty
->input_iterator
=
183 bt_private_connection_create_notification_iterator(connection
,
186 if (!pretty
->input_iterator
) {
187 pretty
->error
= true;
194 enum bt_component_status
pretty_consume(struct bt_private_component
*component
)
196 enum bt_component_status ret
;
197 struct bt_notification
*notification
= NULL
;
198 struct bt_notification_iterator
*it
;
199 struct pretty_component
*pretty
=
200 bt_private_component_get_user_data(component
);
201 enum bt_notification_iterator_status it_ret
;
203 if (unlikely(pretty
->error
)) {
204 ret
= BT_COMPONENT_STATUS_ERROR
;
208 it
= pretty
->input_iterator
;
209 it_ret
= bt_notification_iterator_next(it
);
212 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
213 ret
= BT_COMPONENT_STATUS_END
;
214 BT_PUT(pretty
->input_iterator
);
216 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
217 ret
= BT_COMPONENT_STATUS_AGAIN
;
219 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
222 ret
= BT_COMPONENT_STATUS_ERROR
;
226 notification
= bt_notification_iterator_get_notification(it
);
227 assert(notification
);
228 ret
= handle_notification(pretty
, notification
);
231 bt_put(notification
);
236 enum bt_component_status
add_params_to_map(struct bt_value
*plugin_opt_map
)
238 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
241 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
242 const char *key
= plugin_options
[i
];
243 enum bt_value_status status
;
245 status
= bt_value_map_insert(plugin_opt_map
, key
, bt_value_null
);
247 case BT_VALUE_STATUS_OK
:
250 ret
= BT_COMPONENT_STATUS_ERROR
;
259 bt_bool
check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
261 struct pretty_component
*pretty
= data
;
262 struct bt_value
*plugin_opt_map
= pretty
->plugin_opt_map
;
264 if (!bt_value_map_get(plugin_opt_map
, key
)) {
266 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key
);
272 enum bt_component_status
apply_one_string(const char *key
,
273 struct bt_value
*params
,
276 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
277 struct bt_value
*value
= NULL
;
278 enum bt_value_status status
;
281 value
= bt_value_map_get(params
, key
);
285 if (bt_value_is_null(value
)) {
288 status
= bt_value_string_get(value
, &str
);
290 case BT_VALUE_STATUS_OK
:
293 ret
= BT_COMPONENT_STATUS_ERROR
;
296 *option
= g_strdup(str
);
303 enum bt_component_status
apply_one_bool(const char *key
,
304 struct bt_value
*params
,
308 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
309 struct bt_value
*value
= NULL
;
310 enum bt_value_status status
;
313 value
= bt_value_map_get(params
, key
);
317 status
= bt_value_bool_get(value
, &bool_val
);
319 case BT_VALUE_STATUS_OK
:
322 ret
= BT_COMPONENT_STATUS_ERROR
;
325 *option
= (bool) bool_val
;
335 void warn_wrong_color_param(struct pretty_component
*pretty
)
338 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
342 enum bt_component_status
open_output_file(struct pretty_component
*pretty
)
344 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
346 if (!pretty
->options
.output_path
) {
350 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
358 ret
= BT_COMPONENT_STATUS_ERROR
;
364 enum bt_component_status
apply_params(struct pretty_component
*pretty
,
365 struct bt_value
*params
)
367 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
368 enum bt_value_status status
;
372 pretty
->plugin_opt_map
= bt_value_map_create();
373 if (!pretty
->plugin_opt_map
) {
374 ret
= BT_COMPONENT_STATUS_ERROR
;
377 ret
= add_params_to_map(pretty
->plugin_opt_map
);
378 if (ret
!= BT_COMPONENT_STATUS_OK
) {
381 /* Report unknown parameters. */
382 status
= bt_value_map_foreach(params
, check_param_exists
, pretty
);
384 case BT_VALUE_STATUS_OK
:
387 ret
= BT_COMPONENT_STATUS_ERROR
;
390 /* Known parameters. */
391 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
392 if (bt_value_map_has_key(params
, "color")) {
393 struct bt_value
*color_value
;
396 color_value
= bt_value_map_get(params
, "color");
401 status
= bt_value_string_get(color_value
, &color
);
403 warn_wrong_color_param(pretty
);
405 if (strcmp(color
, "never") == 0) {
406 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
407 } else if (strcmp(color
, "auto") == 0) {
408 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
409 } else if (strcmp(color
, "always") == 0) {
410 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
412 warn_wrong_color_param(pretty
);
419 ret
= apply_one_string("path", params
, &pretty
->options
.output_path
);
420 if (ret
!= BT_COMPONENT_STATUS_OK
) {
423 ret
= open_output_file(pretty
);
424 if (ret
!= BT_COMPONENT_STATUS_OK
) {
428 value
= false; /* Default. */
429 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
430 if (ret
!= BT_COMPONENT_STATUS_OK
) {
433 pretty
->options
.print_delta_field
= !value
; /* Reverse logic. */
435 value
= false; /* Default. */
436 ret
= apply_one_bool("clock-cycles", params
, &value
, NULL
);
437 if (ret
!= BT_COMPONENT_STATUS_OK
) {
440 pretty
->options
.print_timestamp_cycles
= value
;
442 value
= false; /* Default. */
443 ret
= apply_one_bool("clock-seconds", params
, &value
, NULL
);
444 if (ret
!= BT_COMPONENT_STATUS_OK
) {
447 pretty
->options
.clock_seconds
= value
;
449 value
= false; /* Default. */
450 ret
= apply_one_bool("clock-date", params
, &value
, NULL
);
451 if (ret
!= BT_COMPONENT_STATUS_OK
) {
454 pretty
->options
.clock_date
= value
;
456 value
= false; /* Default. */
457 ret
= apply_one_bool("clock-gmt", params
, &value
, NULL
);
458 if (ret
!= BT_COMPONENT_STATUS_OK
) {
461 pretty
->options
.clock_gmt
= value
;
463 value
= false; /* Default. */
464 ret
= apply_one_bool("verbose", params
, &value
, NULL
);
465 if (ret
!= BT_COMPONENT_STATUS_OK
) {
468 pretty
->options
.verbose
= value
;
471 ret
= apply_one_string("name-default", params
, &str
);
472 if (ret
!= BT_COMPONENT_STATUS_OK
) {
476 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
477 } else if (!strcmp(str
, "show")) {
478 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
479 } else if (!strcmp(str
, "hide")) {
480 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
482 ret
= BT_COMPONENT_STATUS_ERROR
;
488 switch (pretty
->options
.name_default
) {
489 case PRETTY_DEFAULT_UNSET
:
490 pretty
->options
.print_payload_field_names
= true;
491 pretty
->options
.print_context_field_names
= true;
492 pretty
->options
.print_header_field_names
= false;
493 pretty
->options
.print_scope_field_names
= false;
495 case PRETTY_DEFAULT_SHOW
:
496 pretty
->options
.print_payload_field_names
= true;
497 pretty
->options
.print_context_field_names
= true;
498 pretty
->options
.print_header_field_names
= true;
499 pretty
->options
.print_scope_field_names
= true;
501 case PRETTY_DEFAULT_HIDE
:
502 pretty
->options
.print_payload_field_names
= false;
503 pretty
->options
.print_context_field_names
= false;
504 pretty
->options
.print_header_field_names
= false;
505 pretty
->options
.print_scope_field_names
= false;
508 ret
= BT_COMPONENT_STATUS_ERROR
;
514 ret
= apply_one_bool("name-payload", params
, &value
, &found
);
515 if (ret
!= BT_COMPONENT_STATUS_OK
) {
519 pretty
->options
.print_payload_field_names
= value
;
524 ret
= apply_one_bool("name-context", params
, &value
, &found
);
525 if (ret
!= BT_COMPONENT_STATUS_OK
) {
529 pretty
->options
.print_context_field_names
= value
;
534 ret
= apply_one_bool("name-header", params
, &value
, &found
);
535 if (ret
!= BT_COMPONENT_STATUS_OK
) {
539 pretty
->options
.print_header_field_names
= value
;
544 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
545 if (ret
!= BT_COMPONENT_STATUS_OK
) {
549 pretty
->options
.print_scope_field_names
= value
;
553 ret
= apply_one_string("field-default", params
, &str
);
554 if (ret
!= BT_COMPONENT_STATUS_OK
) {
558 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
559 } else if (!strcmp(str
, "show")) {
560 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
561 } else if (!strcmp(str
, "hide")) {
562 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
564 ret
= BT_COMPONENT_STATUS_ERROR
;
570 switch (pretty
->options
.field_default
) {
571 case PRETTY_DEFAULT_UNSET
:
572 pretty
->options
.print_trace_field
= false;
573 pretty
->options
.print_trace_hostname_field
= true;
574 pretty
->options
.print_trace_domain_field
= false;
575 pretty
->options
.print_trace_procname_field
= true;
576 pretty
->options
.print_trace_vpid_field
= true;
577 pretty
->options
.print_loglevel_field
= false;
578 pretty
->options
.print_emf_field
= false;
579 pretty
->options
.print_callsite_field
= false;
581 case PRETTY_DEFAULT_SHOW
:
582 pretty
->options
.print_trace_field
= true;
583 pretty
->options
.print_trace_hostname_field
= true;
584 pretty
->options
.print_trace_domain_field
= true;
585 pretty
->options
.print_trace_procname_field
= true;
586 pretty
->options
.print_trace_vpid_field
= true;
587 pretty
->options
.print_loglevel_field
= true;
588 pretty
->options
.print_emf_field
= true;
589 pretty
->options
.print_callsite_field
= true;
591 case PRETTY_DEFAULT_HIDE
:
592 pretty
->options
.print_trace_field
= false;
593 pretty
->options
.print_trace_hostname_field
= false;
594 pretty
->options
.print_trace_domain_field
= false;
595 pretty
->options
.print_trace_procname_field
= false;
596 pretty
->options
.print_trace_vpid_field
= false;
597 pretty
->options
.print_loglevel_field
= false;
598 pretty
->options
.print_emf_field
= false;
599 pretty
->options
.print_callsite_field
= false;
602 ret
= BT_COMPONENT_STATUS_ERROR
;
608 ret
= apply_one_bool("field-trace", params
, &value
, &found
);
609 if (ret
!= BT_COMPONENT_STATUS_OK
) {
613 pretty
->options
.print_trace_field
= value
;
618 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
619 if (ret
!= BT_COMPONENT_STATUS_OK
) {
623 pretty
->options
.print_trace_hostname_field
= value
;
628 ret
= apply_one_bool("field-trace:domain", params
, &value
, &found
);
629 if (ret
!= BT_COMPONENT_STATUS_OK
) {
633 pretty
->options
.print_trace_domain_field
= value
;
638 ret
= apply_one_bool("field-trace:procname", params
, &value
, &found
);
639 if (ret
!= BT_COMPONENT_STATUS_OK
) {
643 pretty
->options
.print_trace_procname_field
= value
;
648 ret
= apply_one_bool("field-trace:vpid", params
, &value
, &found
);
649 if (ret
!= BT_COMPONENT_STATUS_OK
) {
653 pretty
->options
.print_trace_vpid_field
= value
;
658 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
659 if (ret
!= BT_COMPONENT_STATUS_OK
) {
663 pretty
->options
.print_loglevel_field
= value
;
668 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
669 if (ret
!= BT_COMPONENT_STATUS_OK
) {
673 pretty
->options
.print_emf_field
= value
;
678 ret
= apply_one_bool("field-callsite", params
, &value
, &found
);
679 if (ret
!= BT_COMPONENT_STATUS_OK
) {
683 pretty
->options
.print_callsite_field
= value
;
687 bt_put(pretty
->plugin_opt_map
);
688 pretty
->plugin_opt_map
= NULL
;
694 void set_use_colors(struct pretty_component
*pretty
)
696 switch (pretty
->options
.color
) {
697 case PRETTY_COLOR_OPT_ALWAYS
:
698 pretty
->use_colors
= true;
700 case PRETTY_COLOR_OPT_AUTO
:
701 pretty
->use_colors
= pretty
->out
== stdout
&&
702 bt_common_colors_supported();
704 case PRETTY_COLOR_OPT_NEVER
:
705 pretty
->use_colors
= false;
711 void init_stream_packet_context_quarks(void)
713 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
714 g_quark_from_string("timestamp_begin");
715 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
716 g_quark_from_string("timestamp_begin");
717 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
718 g_quark_from_string("timestamp_end");
719 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
720 g_quark_from_string("events_discarded");
721 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
722 g_quark_from_string("content_size");
723 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
724 g_quark_from_string("packet_size");
725 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
726 g_quark_from_string("packet_seq_num");
730 enum bt_component_status
pretty_init(
731 struct bt_private_component
*component
,
732 struct bt_value
*params
,
733 UNUSED_VAR
void *init_method_data
)
735 enum bt_component_status ret
;
736 struct pretty_component
*pretty
= create_pretty();
739 ret
= BT_COMPONENT_STATUS_NOMEM
;
743 ret
= bt_private_component_sink_add_input_private_port(component
,
745 if (ret
!= BT_COMPONENT_STATUS_OK
) {
749 pretty
->out
= stdout
;
750 pretty
->err
= stderr
;
752 pretty
->delta_cycles
= -1ULL;
753 pretty
->last_cycles_timestamp
= -1ULL;
755 pretty
->delta_real_timestamp
= -1ULL;
756 pretty
->last_real_timestamp
= -1ULL;
758 ret
= apply_params(pretty
, params
);
759 if (ret
!= BT_COMPONENT_STATUS_OK
) {
763 set_use_colors(pretty
);
764 ret
= bt_private_component_set_user_data(component
, pretty
);
765 if (ret
!= BT_COMPONENT_STATUS_OK
) {
769 init_stream_packet_context_quarks();
774 destroy_pretty_data(pretty
);