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
);
83 (void) g_string_free(pretty
->string
, TRUE
);
84 if (pretty
->out
!= stdout
) {
87 ret
= fclose(pretty
->out
);
89 perror("close output file");
92 g_free(pretty
->options
.output_path
);
97 struct pretty_component
*create_pretty(void)
99 struct pretty_component
*pretty
;
101 pretty
= g_new0(struct pretty_component
, 1);
105 pretty
->string
= g_string_new("");
106 if (!pretty
->string
) {
118 void pretty_finalize(struct bt_private_component
*component
)
120 void *data
= bt_private_component_get_user_data(component
);
122 destroy_pretty_data(data
);
126 enum bt_component_status
handle_notification(struct pretty_component
*pretty
,
127 struct bt_notification
*notification
)
129 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
133 if (bt_notification_get_type(notification
) == BT_NOTIFICATION_TYPE_EVENT
) {
134 ret
= pretty_print_event(pretty
, notification
);
141 void pretty_port_connected(
142 struct bt_private_component
*component
,
143 struct bt_private_port
*self_port
,
144 struct bt_port
*other_port
)
146 struct bt_private_connection
*connection
;
147 struct pretty_component
*pretty
;
148 static const enum bt_notification_type notif_types
[] = {
149 BT_NOTIFICATION_TYPE_EVENT
,
150 BT_NOTIFICATION_TYPE_SENTINEL
,
153 pretty
= bt_private_component_get_user_data(component
);
155 assert(!pretty
->input_iterator
);
156 connection
= bt_private_port_get_private_connection(self_port
);
158 pretty
->input_iterator
=
159 bt_private_connection_create_notification_iterator(connection
,
162 if (!pretty
->input_iterator
) {
163 pretty
->error
= true;
170 enum bt_component_status
pretty_consume(struct bt_private_component
*component
)
172 enum bt_component_status ret
;
173 struct bt_notification
*notification
= NULL
;
174 struct bt_notification_iterator
*it
;
175 struct pretty_component
*pretty
=
176 bt_private_component_get_user_data(component
);
177 enum bt_notification_iterator_status it_ret
;
179 if (unlikely(pretty
->error
)) {
180 ret
= BT_COMPONENT_STATUS_ERROR
;
184 it
= pretty
->input_iterator
;
185 it_ret
= bt_notification_iterator_next(it
);
188 case BT_NOTIFICATION_ITERATOR_STATUS_ERROR
:
189 ret
= BT_COMPONENT_STATUS_ERROR
;
191 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
192 ret
= BT_COMPONENT_STATUS_END
;
193 BT_PUT(pretty
->input_iterator
);
195 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
196 ret
= BT_COMPONENT_STATUS_AGAIN
;
198 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
201 ret
= BT_COMPONENT_STATUS_ERROR
;
205 notification
= bt_notification_iterator_get_notification(it
);
206 assert(notification
);
207 ret
= handle_notification(pretty
, notification
);
210 bt_put(notification
);
215 enum bt_component_status
add_params_to_map(struct bt_value
*plugin_opt_map
)
217 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
220 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
221 const char *key
= plugin_options
[i
];
222 enum bt_value_status status
;
224 status
= bt_value_map_insert(plugin_opt_map
, key
, bt_value_null
);
226 case BT_VALUE_STATUS_OK
:
229 ret
= BT_COMPONENT_STATUS_ERROR
;
238 bool check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
240 struct pretty_component
*pretty
= data
;
241 struct bt_value
*plugin_opt_map
= pretty
->plugin_opt_map
;
243 if (!bt_value_map_get(plugin_opt_map
, key
)) {
245 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key
);
251 enum bt_component_status
apply_one_string(const char *key
,
252 struct bt_value
*params
,
255 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
256 struct bt_value
*value
= NULL
;
257 enum bt_value_status status
;
260 value
= bt_value_map_get(params
, key
);
264 if (bt_value_is_null(value
)) {
267 status
= bt_value_string_get(value
, &str
);
269 case BT_VALUE_STATUS_OK
:
272 ret
= BT_COMPONENT_STATUS_ERROR
;
275 *option
= g_strdup(str
);
282 enum bt_component_status
apply_one_bool(const char *key
,
283 struct bt_value
*params
,
287 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
288 struct bt_value
*value
= NULL
;
289 enum bt_value_status status
;
291 value
= bt_value_map_get(params
, key
);
295 status
= bt_value_bool_get(value
, option
);
297 case BT_VALUE_STATUS_OK
:
300 ret
= BT_COMPONENT_STATUS_ERROR
;
312 void warn_wrong_color_param(struct pretty_component
*pretty
)
315 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
319 enum bt_component_status
open_output_file(struct pretty_component
*pretty
)
321 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
323 if (!pretty
->options
.output_path
) {
327 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
335 ret
= BT_COMPONENT_STATUS_ERROR
;
341 enum bt_component_status
apply_params(struct pretty_component
*pretty
,
342 struct bt_value
*params
)
344 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
345 enum bt_value_status status
;
349 pretty
->plugin_opt_map
= bt_value_map_create();
350 if (!pretty
->plugin_opt_map
) {
351 ret
= BT_COMPONENT_STATUS_ERROR
;
354 ret
= add_params_to_map(pretty
->plugin_opt_map
);
355 if (ret
!= BT_COMPONENT_STATUS_OK
) {
358 /* Report unknown parameters. */
359 status
= bt_value_map_foreach(params
, check_param_exists
, pretty
);
361 case BT_VALUE_STATUS_OK
:
364 ret
= BT_COMPONENT_STATUS_ERROR
;
367 /* Known parameters. */
368 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
369 if (bt_value_map_has_key(params
, "color")) {
370 struct bt_value
*color_value
;
373 color_value
= bt_value_map_get(params
, "color");
378 status
= bt_value_string_get(color_value
, &color
);
380 warn_wrong_color_param(pretty
);
382 if (strcmp(color
, "never") == 0) {
383 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
384 } else if (strcmp(color
, "auto") == 0) {
385 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
386 } else if (strcmp(color
, "always") == 0) {
387 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
389 warn_wrong_color_param(pretty
);
396 ret
= apply_one_string("path", params
, &pretty
->options
.output_path
);
397 if (ret
!= BT_COMPONENT_STATUS_OK
) {
400 ret
= open_output_file(pretty
);
401 if (ret
!= BT_COMPONENT_STATUS_OK
) {
405 value
= false; /* Default. */
406 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
407 if (ret
!= BT_COMPONENT_STATUS_OK
) {
410 pretty
->options
.print_delta_field
= !value
; /* Reverse logic. */
412 value
= false; /* Default. */
413 ret
= apply_one_bool("clock-cycles", params
, &value
, NULL
);
414 if (ret
!= BT_COMPONENT_STATUS_OK
) {
417 pretty
->options
.print_timestamp_cycles
= value
;
419 value
= false; /* Default. */
420 ret
= apply_one_bool("clock-seconds", params
, &value
, NULL
);
421 if (ret
!= BT_COMPONENT_STATUS_OK
) {
424 pretty
->options
.clock_seconds
= value
;
426 value
= false; /* Default. */
427 ret
= apply_one_bool("clock-date", params
, &value
, NULL
);
428 if (ret
!= BT_COMPONENT_STATUS_OK
) {
431 pretty
->options
.clock_date
= value
;
433 value
= false; /* Default. */
434 ret
= apply_one_bool("clock-gmt", params
, &value
, NULL
);
435 if (ret
!= BT_COMPONENT_STATUS_OK
) {
438 pretty
->options
.clock_gmt
= value
;
440 value
= false; /* Default. */
441 ret
= apply_one_bool("verbose", params
, &value
, NULL
);
442 if (ret
!= BT_COMPONENT_STATUS_OK
) {
445 pretty
->options
.verbose
= value
;
448 ret
= apply_one_string("name-default", params
, &str
);
449 if (ret
!= BT_COMPONENT_STATUS_OK
) {
453 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
454 } else if (!strcmp(str
, "show")) {
455 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
456 } else if (!strcmp(str
, "hide")) {
457 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
459 ret
= BT_COMPONENT_STATUS_ERROR
;
465 switch (pretty
->options
.name_default
) {
466 case PRETTY_DEFAULT_UNSET
:
467 pretty
->options
.print_payload_field_names
= true;
468 pretty
->options
.print_context_field_names
= true;
469 pretty
->options
.print_header_field_names
= false;
470 pretty
->options
.print_scope_field_names
= false;
472 case PRETTY_DEFAULT_SHOW
:
473 pretty
->options
.print_payload_field_names
= true;
474 pretty
->options
.print_context_field_names
= true;
475 pretty
->options
.print_header_field_names
= true;
476 pretty
->options
.print_scope_field_names
= true;
478 case PRETTY_DEFAULT_HIDE
:
479 pretty
->options
.print_payload_field_names
= false;
480 pretty
->options
.print_context_field_names
= false;
481 pretty
->options
.print_header_field_names
= false;
482 pretty
->options
.print_scope_field_names
= false;
485 ret
= BT_COMPONENT_STATUS_ERROR
;
491 ret
= apply_one_bool("name-payload", params
, &value
, &found
);
492 if (ret
!= BT_COMPONENT_STATUS_OK
) {
496 pretty
->options
.print_payload_field_names
= value
;
501 ret
= apply_one_bool("name-context", params
, &value
, &found
);
502 if (ret
!= BT_COMPONENT_STATUS_OK
) {
506 pretty
->options
.print_context_field_names
= value
;
511 ret
= apply_one_bool("name-header", params
, &value
, &found
);
512 if (ret
!= BT_COMPONENT_STATUS_OK
) {
516 pretty
->options
.print_header_field_names
= value
;
521 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
522 if (ret
!= BT_COMPONENT_STATUS_OK
) {
526 pretty
->options
.print_scope_field_names
= value
;
530 ret
= apply_one_string("field-default", params
, &str
);
531 if (ret
!= BT_COMPONENT_STATUS_OK
) {
535 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
536 } else if (!strcmp(str
, "show")) {
537 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
538 } else if (!strcmp(str
, "hide")) {
539 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
541 ret
= BT_COMPONENT_STATUS_ERROR
;
547 switch (pretty
->options
.field_default
) {
548 case PRETTY_DEFAULT_UNSET
:
549 pretty
->options
.print_trace_field
= false;
550 pretty
->options
.print_trace_hostname_field
= true;
551 pretty
->options
.print_trace_domain_field
= false;
552 pretty
->options
.print_trace_procname_field
= true;
553 pretty
->options
.print_trace_vpid_field
= true;
554 pretty
->options
.print_loglevel_field
= false;
555 pretty
->options
.print_emf_field
= false;
556 pretty
->options
.print_callsite_field
= false;
558 case PRETTY_DEFAULT_SHOW
:
559 pretty
->options
.print_trace_field
= true;
560 pretty
->options
.print_trace_hostname_field
= true;
561 pretty
->options
.print_trace_domain_field
= true;
562 pretty
->options
.print_trace_procname_field
= true;
563 pretty
->options
.print_trace_vpid_field
= true;
564 pretty
->options
.print_loglevel_field
= true;
565 pretty
->options
.print_emf_field
= true;
566 pretty
->options
.print_callsite_field
= true;
568 case PRETTY_DEFAULT_HIDE
:
569 pretty
->options
.print_trace_field
= false;
570 pretty
->options
.print_trace_hostname_field
= false;
571 pretty
->options
.print_trace_domain_field
= false;
572 pretty
->options
.print_trace_procname_field
= false;
573 pretty
->options
.print_trace_vpid_field
= false;
574 pretty
->options
.print_loglevel_field
= false;
575 pretty
->options
.print_emf_field
= false;
576 pretty
->options
.print_callsite_field
= false;
579 ret
= BT_COMPONENT_STATUS_ERROR
;
585 ret
= apply_one_bool("field-trace", params
, &value
, &found
);
586 if (ret
!= BT_COMPONENT_STATUS_OK
) {
590 pretty
->options
.print_trace_field
= value
;
595 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
596 if (ret
!= BT_COMPONENT_STATUS_OK
) {
600 pretty
->options
.print_trace_hostname_field
= value
;
605 ret
= apply_one_bool("field-trace:domain", params
, &value
, &found
);
606 if (ret
!= BT_COMPONENT_STATUS_OK
) {
610 pretty
->options
.print_trace_domain_field
= value
;
615 ret
= apply_one_bool("field-trace:procname", params
, &value
, &found
);
616 if (ret
!= BT_COMPONENT_STATUS_OK
) {
620 pretty
->options
.print_trace_procname_field
= value
;
625 ret
= apply_one_bool("field-trace:vpid", params
, &value
, &found
);
626 if (ret
!= BT_COMPONENT_STATUS_OK
) {
630 pretty
->options
.print_trace_vpid_field
= value
;
635 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
636 if (ret
!= BT_COMPONENT_STATUS_OK
) {
640 pretty
->options
.print_loglevel_field
= value
;
645 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
646 if (ret
!= BT_COMPONENT_STATUS_OK
) {
650 pretty
->options
.print_emf_field
= value
;
655 ret
= apply_one_bool("field-callsite", params
, &value
, &found
);
656 if (ret
!= BT_COMPONENT_STATUS_OK
) {
660 pretty
->options
.print_callsite_field
= value
;
664 bt_put(pretty
->plugin_opt_map
);
665 pretty
->plugin_opt_map
= NULL
;
671 void set_use_colors(struct pretty_component
*pretty
)
673 switch (pretty
->options
.color
) {
674 case PRETTY_COLOR_OPT_ALWAYS
:
675 pretty
->use_colors
= true;
677 case PRETTY_COLOR_OPT_AUTO
:
678 pretty
->use_colors
= pretty
->out
== stdout
&&
679 bt_common_colors_supported();
681 case PRETTY_COLOR_OPT_NEVER
:
682 pretty
->use_colors
= false;
688 void init_stream_packet_context_quarks(void)
690 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
691 g_quark_from_string("timestamp_begin");
692 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
693 g_quark_from_string("timestamp_begin");
694 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
695 g_quark_from_string("timestamp_end");
696 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
697 g_quark_from_string("events_discarded");
698 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
699 g_quark_from_string("content_size");
700 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
701 g_quark_from_string("packet_size");
702 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
703 g_quark_from_string("packet_seq_num");
707 enum bt_component_status
pretty_init(
708 struct bt_private_component
*component
,
709 struct bt_value
*params
,
710 UNUSED_VAR
void *init_method_data
)
712 enum bt_component_status ret
;
713 struct pretty_component
*pretty
= create_pretty();
717 ret
= BT_COMPONENT_STATUS_NOMEM
;
721 priv_port
= bt_private_component_sink_add_input_private_port(component
,
724 ret
= BT_COMPONENT_STATUS_NOMEM
;
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
);