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
:
157 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
:
158 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
:
159 ret
= pretty_print_discarded_elements(pretty
, notification
);
162 fprintf(stderr
, "Unhandled notification type\n");
169 void pretty_port_connected(
170 struct bt_private_component
*component
,
171 struct bt_private_port
*self_port
,
172 struct bt_port
*other_port
)
174 enum bt_connection_status conn_status
;
175 struct bt_private_connection
*connection
;
176 struct pretty_component
*pretty
;
177 static const enum bt_notification_type notif_types
[] = {
178 BT_NOTIFICATION_TYPE_EVENT
,
179 BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
,
180 BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
,
181 BT_NOTIFICATION_TYPE_SENTINEL
,
184 pretty
= bt_private_component_get_user_data(component
);
186 assert(!pretty
->input_iterator
);
187 connection
= bt_private_port_get_private_connection(self_port
);
189 conn_status
= bt_private_connection_create_notification_iterator(
190 connection
, notif_types
, &pretty
->input_iterator
);
191 if (conn_status
!= BT_CONNECTION_STATUS_OK
) {
192 pretty
->error
= true;
199 enum bt_component_status
pretty_consume(struct bt_private_component
*component
)
201 enum bt_component_status ret
;
202 struct bt_notification
*notification
= NULL
;
203 struct bt_notification_iterator
*it
;
204 struct pretty_component
*pretty
=
205 bt_private_component_get_user_data(component
);
206 enum bt_notification_iterator_status it_ret
;
208 if (unlikely(pretty
->error
)) {
209 ret
= BT_COMPONENT_STATUS_ERROR
;
213 it
= pretty
->input_iterator
;
214 it_ret
= bt_notification_iterator_next(it
);
217 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
218 ret
= BT_COMPONENT_STATUS_END
;
219 BT_PUT(pretty
->input_iterator
);
221 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
222 ret
= BT_COMPONENT_STATUS_AGAIN
;
224 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
227 ret
= BT_COMPONENT_STATUS_ERROR
;
231 notification
= bt_notification_iterator_get_notification(it
);
232 assert(notification
);
233 ret
= handle_notification(pretty
, notification
);
236 bt_put(notification
);
241 enum bt_component_status
add_params_to_map(struct bt_value
*plugin_opt_map
)
243 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
246 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
247 const char *key
= plugin_options
[i
];
248 enum bt_value_status status
;
250 status
= bt_value_map_insert(plugin_opt_map
, key
, bt_value_null
);
252 case BT_VALUE_STATUS_OK
:
255 ret
= BT_COMPONENT_STATUS_ERROR
;
264 bt_bool
check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
266 struct pretty_component
*pretty
= data
;
267 struct bt_value
*plugin_opt_map
= pretty
->plugin_opt_map
;
269 if (!bt_value_map_get(plugin_opt_map
, key
)) {
271 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key
);
277 enum bt_component_status
apply_one_string(const char *key
,
278 struct bt_value
*params
,
281 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
282 struct bt_value
*value
= NULL
;
283 enum bt_value_status status
;
286 value
= bt_value_map_get(params
, key
);
290 if (bt_value_is_null(value
)) {
293 status
= bt_value_string_get(value
, &str
);
295 case BT_VALUE_STATUS_OK
:
298 ret
= BT_COMPONENT_STATUS_ERROR
;
301 *option
= g_strdup(str
);
308 enum bt_component_status
apply_one_bool(const char *key
,
309 struct bt_value
*params
,
313 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
314 struct bt_value
*value
= NULL
;
315 enum bt_value_status status
;
318 value
= bt_value_map_get(params
, key
);
322 status
= bt_value_bool_get(value
, &bool_val
);
324 case BT_VALUE_STATUS_OK
:
327 ret
= BT_COMPONENT_STATUS_ERROR
;
330 *option
= (bool) bool_val
;
340 void warn_wrong_color_param(struct pretty_component
*pretty
)
343 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
347 enum bt_component_status
open_output_file(struct pretty_component
*pretty
)
349 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
351 if (!pretty
->options
.output_path
) {
355 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
363 ret
= BT_COMPONENT_STATUS_ERROR
;
369 enum bt_component_status
apply_params(struct pretty_component
*pretty
,
370 struct bt_value
*params
)
372 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
373 enum bt_value_status status
;
377 pretty
->plugin_opt_map
= bt_value_map_create();
378 if (!pretty
->plugin_opt_map
) {
379 ret
= BT_COMPONENT_STATUS_ERROR
;
382 ret
= add_params_to_map(pretty
->plugin_opt_map
);
383 if (ret
!= BT_COMPONENT_STATUS_OK
) {
386 /* Report unknown parameters. */
387 status
= bt_value_map_foreach(params
, check_param_exists
, pretty
);
389 case BT_VALUE_STATUS_OK
:
392 ret
= BT_COMPONENT_STATUS_ERROR
;
395 /* Known parameters. */
396 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
397 if (bt_value_map_has_key(params
, "color")) {
398 struct bt_value
*color_value
;
401 color_value
= bt_value_map_get(params
, "color");
406 status
= bt_value_string_get(color_value
, &color
);
408 warn_wrong_color_param(pretty
);
410 if (strcmp(color
, "never") == 0) {
411 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
412 } else if (strcmp(color
, "auto") == 0) {
413 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
414 } else if (strcmp(color
, "always") == 0) {
415 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
417 warn_wrong_color_param(pretty
);
424 ret
= apply_one_string("path", params
, &pretty
->options
.output_path
);
425 if (ret
!= BT_COMPONENT_STATUS_OK
) {
428 ret
= open_output_file(pretty
);
429 if (ret
!= BT_COMPONENT_STATUS_OK
) {
433 value
= false; /* Default. */
434 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
435 if (ret
!= BT_COMPONENT_STATUS_OK
) {
438 pretty
->options
.print_delta_field
= !value
; /* Reverse logic. */
440 value
= false; /* Default. */
441 ret
= apply_one_bool("clock-cycles", params
, &value
, NULL
);
442 if (ret
!= BT_COMPONENT_STATUS_OK
) {
445 pretty
->options
.print_timestamp_cycles
= value
;
447 value
= false; /* Default. */
448 ret
= apply_one_bool("clock-seconds", params
, &value
, NULL
);
449 if (ret
!= BT_COMPONENT_STATUS_OK
) {
452 pretty
->options
.clock_seconds
= value
;
454 value
= false; /* Default. */
455 ret
= apply_one_bool("clock-date", params
, &value
, NULL
);
456 if (ret
!= BT_COMPONENT_STATUS_OK
) {
459 pretty
->options
.clock_date
= value
;
461 value
= false; /* Default. */
462 ret
= apply_one_bool("clock-gmt", params
, &value
, NULL
);
463 if (ret
!= BT_COMPONENT_STATUS_OK
) {
466 pretty
->options
.clock_gmt
= value
;
468 value
= false; /* Default. */
469 ret
= apply_one_bool("verbose", params
, &value
, NULL
);
470 if (ret
!= BT_COMPONENT_STATUS_OK
) {
473 pretty
->options
.verbose
= value
;
476 ret
= apply_one_string("name-default", params
, &str
);
477 if (ret
!= BT_COMPONENT_STATUS_OK
) {
481 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
482 } else if (!strcmp(str
, "show")) {
483 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
484 } else if (!strcmp(str
, "hide")) {
485 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
487 ret
= BT_COMPONENT_STATUS_ERROR
;
493 switch (pretty
->options
.name_default
) {
494 case PRETTY_DEFAULT_UNSET
:
495 pretty
->options
.print_payload_field_names
= true;
496 pretty
->options
.print_context_field_names
= true;
497 pretty
->options
.print_header_field_names
= false;
498 pretty
->options
.print_scope_field_names
= false;
500 case PRETTY_DEFAULT_SHOW
:
501 pretty
->options
.print_payload_field_names
= true;
502 pretty
->options
.print_context_field_names
= true;
503 pretty
->options
.print_header_field_names
= true;
504 pretty
->options
.print_scope_field_names
= true;
506 case PRETTY_DEFAULT_HIDE
:
507 pretty
->options
.print_payload_field_names
= false;
508 pretty
->options
.print_context_field_names
= false;
509 pretty
->options
.print_header_field_names
= false;
510 pretty
->options
.print_scope_field_names
= false;
513 ret
= BT_COMPONENT_STATUS_ERROR
;
519 ret
= apply_one_bool("name-payload", params
, &value
, &found
);
520 if (ret
!= BT_COMPONENT_STATUS_OK
) {
524 pretty
->options
.print_payload_field_names
= value
;
529 ret
= apply_one_bool("name-context", params
, &value
, &found
);
530 if (ret
!= BT_COMPONENT_STATUS_OK
) {
534 pretty
->options
.print_context_field_names
= value
;
539 ret
= apply_one_bool("name-header", params
, &value
, &found
);
540 if (ret
!= BT_COMPONENT_STATUS_OK
) {
544 pretty
->options
.print_header_field_names
= value
;
549 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
550 if (ret
!= BT_COMPONENT_STATUS_OK
) {
554 pretty
->options
.print_scope_field_names
= value
;
558 ret
= apply_one_string("field-default", params
, &str
);
559 if (ret
!= BT_COMPONENT_STATUS_OK
) {
563 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
564 } else if (!strcmp(str
, "show")) {
565 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
566 } else if (!strcmp(str
, "hide")) {
567 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
569 ret
= BT_COMPONENT_STATUS_ERROR
;
575 switch (pretty
->options
.field_default
) {
576 case PRETTY_DEFAULT_UNSET
:
577 pretty
->options
.print_trace_field
= false;
578 pretty
->options
.print_trace_hostname_field
= true;
579 pretty
->options
.print_trace_domain_field
= false;
580 pretty
->options
.print_trace_procname_field
= true;
581 pretty
->options
.print_trace_vpid_field
= true;
582 pretty
->options
.print_loglevel_field
= false;
583 pretty
->options
.print_emf_field
= false;
584 pretty
->options
.print_callsite_field
= false;
586 case PRETTY_DEFAULT_SHOW
:
587 pretty
->options
.print_trace_field
= true;
588 pretty
->options
.print_trace_hostname_field
= true;
589 pretty
->options
.print_trace_domain_field
= true;
590 pretty
->options
.print_trace_procname_field
= true;
591 pretty
->options
.print_trace_vpid_field
= true;
592 pretty
->options
.print_loglevel_field
= true;
593 pretty
->options
.print_emf_field
= true;
594 pretty
->options
.print_callsite_field
= true;
596 case PRETTY_DEFAULT_HIDE
:
597 pretty
->options
.print_trace_field
= false;
598 pretty
->options
.print_trace_hostname_field
= false;
599 pretty
->options
.print_trace_domain_field
= false;
600 pretty
->options
.print_trace_procname_field
= false;
601 pretty
->options
.print_trace_vpid_field
= false;
602 pretty
->options
.print_loglevel_field
= false;
603 pretty
->options
.print_emf_field
= false;
604 pretty
->options
.print_callsite_field
= false;
607 ret
= BT_COMPONENT_STATUS_ERROR
;
613 ret
= apply_one_bool("field-trace", params
, &value
, &found
);
614 if (ret
!= BT_COMPONENT_STATUS_OK
) {
618 pretty
->options
.print_trace_field
= value
;
623 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
624 if (ret
!= BT_COMPONENT_STATUS_OK
) {
628 pretty
->options
.print_trace_hostname_field
= value
;
633 ret
= apply_one_bool("field-trace:domain", params
, &value
, &found
);
634 if (ret
!= BT_COMPONENT_STATUS_OK
) {
638 pretty
->options
.print_trace_domain_field
= value
;
643 ret
= apply_one_bool("field-trace:procname", params
, &value
, &found
);
644 if (ret
!= BT_COMPONENT_STATUS_OK
) {
648 pretty
->options
.print_trace_procname_field
= value
;
653 ret
= apply_one_bool("field-trace:vpid", params
, &value
, &found
);
654 if (ret
!= BT_COMPONENT_STATUS_OK
) {
658 pretty
->options
.print_trace_vpid_field
= value
;
663 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
664 if (ret
!= BT_COMPONENT_STATUS_OK
) {
668 pretty
->options
.print_loglevel_field
= value
;
673 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
674 if (ret
!= BT_COMPONENT_STATUS_OK
) {
678 pretty
->options
.print_emf_field
= value
;
683 ret
= apply_one_bool("field-callsite", params
, &value
, &found
);
684 if (ret
!= BT_COMPONENT_STATUS_OK
) {
688 pretty
->options
.print_callsite_field
= value
;
692 bt_put(pretty
->plugin_opt_map
);
693 pretty
->plugin_opt_map
= NULL
;
699 void set_use_colors(struct pretty_component
*pretty
)
701 switch (pretty
->options
.color
) {
702 case PRETTY_COLOR_OPT_ALWAYS
:
703 pretty
->use_colors
= true;
705 case PRETTY_COLOR_OPT_AUTO
:
706 pretty
->use_colors
= pretty
->out
== stdout
&&
707 bt_common_colors_supported();
709 case PRETTY_COLOR_OPT_NEVER
:
710 pretty
->use_colors
= false;
716 void init_stream_packet_context_quarks(void)
718 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
719 g_quark_from_string("timestamp_begin");
720 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
721 g_quark_from_string("timestamp_begin");
722 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
723 g_quark_from_string("timestamp_end");
724 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
725 g_quark_from_string("events_discarded");
726 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
727 g_quark_from_string("content_size");
728 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
729 g_quark_from_string("packet_size");
730 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
731 g_quark_from_string("packet_seq_num");
735 enum bt_component_status
pretty_init(
736 struct bt_private_component
*component
,
737 struct bt_value
*params
,
738 UNUSED_VAR
void *init_method_data
)
740 enum bt_component_status ret
;
741 struct pretty_component
*pretty
= create_pretty();
744 ret
= BT_COMPONENT_STATUS_NOMEM
;
748 ret
= bt_private_component_sink_add_input_private_port(component
,
750 if (ret
!= BT_COMPONENT_STATUS_OK
) {
754 pretty
->out
= stdout
;
755 pretty
->err
= stderr
;
757 pretty
->delta_cycles
= -1ULL;
758 pretty
->last_cycles_timestamp
= -1ULL;
760 pretty
->delta_real_timestamp
= -1ULL;
761 pretty
->last_real_timestamp
= -1ULL;
763 ret
= apply_params(pretty
, params
);
764 if (ret
!= BT_COMPONENT_STATUS_OK
) {
768 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
);