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>
53 GQuark stream_packet_context_quarks
[STREAM_PACKET_CONTEXT_QUARKS_LEN
];
56 const char *plugin_options
[] = {
65 "name-default", /* show/hide */
70 "field-default", /* show/hide */
72 "field-trace:hostname",
74 "field-trace:procname",
82 void destroy_pretty_data(struct pretty_component
*pretty
)
84 bt_put(pretty
->input_iterator
);
87 (void) g_string_free(pretty
->string
, TRUE
);
90 if (pretty
->tmp_string
) {
91 (void) g_string_free(pretty
->tmp_string
, TRUE
);
94 if (pretty
->out
!= stdout
) {
97 ret
= fclose(pretty
->out
);
99 perror("close output file");
102 g_free(pretty
->options
.output_path
);
107 struct pretty_component
*create_pretty(void)
109 struct pretty_component
*pretty
;
111 pretty
= g_new0(struct pretty_component
, 1);
115 pretty
->string
= g_string_new("");
116 if (!pretty
->string
) {
119 pretty
->tmp_string
= g_string_new("");
120 if (!pretty
->tmp_string
) {
132 void pretty_finalize(struct bt_private_component
*component
)
134 void *data
= bt_private_component_get_user_data(component
);
136 destroy_pretty_data(data
);
140 enum bt_component_status
handle_notification(struct pretty_component
*pretty
,
141 struct bt_notification
*notification
)
143 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
147 switch (bt_notification_get_type(notification
)) {
148 case BT_NOTIFICATION_TYPE_EVENT
:
149 ret
= pretty_print_event(pretty
, notification
);
151 case BT_NOTIFICATION_TYPE_INACTIVITY
:
152 fprintf(stderr
, "Inactivity notification\n");
154 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
155 case BT_NOTIFICATION_TYPE_PACKET_END
:
156 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
157 case BT_NOTIFICATION_TYPE_STREAM_END
:
159 case BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
:
160 case BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
:
161 ret
= pretty_print_discarded_elements(pretty
, notification
);
164 fprintf(stderr
, "Unhandled notification type\n");
171 void pretty_port_connected(
172 struct bt_private_component
*component
,
173 struct bt_private_port
*self_port
,
174 struct bt_port
*other_port
)
176 enum bt_connection_status conn_status
;
177 struct bt_private_connection
*connection
;
178 struct pretty_component
*pretty
;
179 static const enum bt_notification_type notif_types
[] = {
180 BT_NOTIFICATION_TYPE_EVENT
,
181 BT_NOTIFICATION_TYPE_DISCARDED_PACKETS
,
182 BT_NOTIFICATION_TYPE_DISCARDED_EVENTS
,
183 BT_NOTIFICATION_TYPE_SENTINEL
,
186 pretty
= bt_private_component_get_user_data(component
);
188 assert(!pretty
->input_iterator
);
189 connection
= bt_private_port_get_private_connection(self_port
);
191 conn_status
= bt_private_connection_create_notification_iterator(
192 connection
, notif_types
, &pretty
->input_iterator
);
193 if (conn_status
!= BT_CONNECTION_STATUS_OK
) {
194 pretty
->error
= true;
201 enum bt_component_status
pretty_consume(struct bt_private_component
*component
)
203 enum bt_component_status ret
;
204 struct bt_notification
*notification
= NULL
;
205 struct bt_notification_iterator
*it
;
206 struct pretty_component
*pretty
=
207 bt_private_component_get_user_data(component
);
208 enum bt_notification_iterator_status it_ret
;
210 if (unlikely(pretty
->error
)) {
211 ret
= BT_COMPONENT_STATUS_ERROR
;
215 it
= pretty
->input_iterator
;
216 it_ret
= bt_notification_iterator_next(it
);
219 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
220 ret
= BT_COMPONENT_STATUS_END
;
221 BT_PUT(pretty
->input_iterator
);
223 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
224 ret
= BT_COMPONENT_STATUS_AGAIN
;
226 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
229 ret
= BT_COMPONENT_STATUS_ERROR
;
233 notification
= bt_notification_iterator_get_notification(it
);
234 assert(notification
);
235 ret
= handle_notification(pretty
, notification
);
238 bt_put(notification
);
243 enum bt_component_status
add_params_to_map(struct bt_value
*plugin_opt_map
)
245 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
248 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
249 const char *key
= plugin_options
[i
];
250 enum bt_value_status status
;
252 status
= bt_value_map_insert(plugin_opt_map
, key
, bt_value_null
);
254 case BT_VALUE_STATUS_OK
:
257 ret
= BT_COMPONENT_STATUS_ERROR
;
266 bt_bool
check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
268 struct pretty_component
*pretty
= data
;
269 struct bt_value
*plugin_opt_map
= pretty
->plugin_opt_map
;
271 if (!bt_value_map_get(plugin_opt_map
, key
)) {
273 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key
);
279 enum bt_component_status
apply_one_string(const char *key
,
280 struct bt_value
*params
,
283 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
284 struct bt_value
*value
= NULL
;
285 enum bt_value_status status
;
288 value
= bt_value_map_get(params
, key
);
292 if (bt_value_is_null(value
)) {
295 status
= bt_value_string_get(value
, &str
);
297 case BT_VALUE_STATUS_OK
:
300 ret
= BT_COMPONENT_STATUS_ERROR
;
303 *option
= g_strdup(str
);
310 enum bt_component_status
apply_one_bool(const char *key
,
311 struct bt_value
*params
,
315 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
316 struct bt_value
*value
= NULL
;
317 enum bt_value_status status
;
320 value
= bt_value_map_get(params
, key
);
324 status
= bt_value_bool_get(value
, &bool_val
);
326 case BT_VALUE_STATUS_OK
:
329 ret
= BT_COMPONENT_STATUS_ERROR
;
332 *option
= (bool) bool_val
;
342 void warn_wrong_color_param(struct pretty_component
*pretty
)
345 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
349 enum bt_component_status
open_output_file(struct pretty_component
*pretty
)
351 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
353 if (!pretty
->options
.output_path
) {
357 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
365 ret
= BT_COMPONENT_STATUS_ERROR
;
371 enum bt_component_status
apply_params(struct pretty_component
*pretty
,
372 struct bt_value
*params
)
374 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
375 enum bt_value_status status
;
379 pretty
->plugin_opt_map
= bt_value_map_create();
380 if (!pretty
->plugin_opt_map
) {
381 ret
= BT_COMPONENT_STATUS_ERROR
;
384 ret
= add_params_to_map(pretty
->plugin_opt_map
);
385 if (ret
!= BT_COMPONENT_STATUS_OK
) {
388 /* Report unknown parameters. */
389 status
= bt_value_map_foreach(params
, check_param_exists
, pretty
);
391 case BT_VALUE_STATUS_OK
:
394 ret
= BT_COMPONENT_STATUS_ERROR
;
397 /* Known parameters. */
398 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
399 if (bt_value_map_has_key(params
, "color")) {
400 struct bt_value
*color_value
;
403 color_value
= bt_value_map_get(params
, "color");
408 status
= bt_value_string_get(color_value
, &color
);
410 warn_wrong_color_param(pretty
);
412 if (strcmp(color
, "never") == 0) {
413 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
414 } else if (strcmp(color
, "auto") == 0) {
415 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
416 } else if (strcmp(color
, "always") == 0) {
417 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
419 warn_wrong_color_param(pretty
);
426 ret
= apply_one_string("path", params
, &pretty
->options
.output_path
);
427 if (ret
!= BT_COMPONENT_STATUS_OK
) {
430 ret
= open_output_file(pretty
);
431 if (ret
!= BT_COMPONENT_STATUS_OK
) {
435 value
= false; /* Default. */
436 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
437 if (ret
!= BT_COMPONENT_STATUS_OK
) {
440 pretty
->options
.print_delta_field
= !value
; /* Reverse logic. */
442 value
= false; /* Default. */
443 ret
= apply_one_bool("clock-cycles", params
, &value
, NULL
);
444 if (ret
!= BT_COMPONENT_STATUS_OK
) {
447 pretty
->options
.print_timestamp_cycles
= value
;
449 value
= false; /* Default. */
450 ret
= apply_one_bool("clock-seconds", params
, &value
, NULL
);
451 if (ret
!= BT_COMPONENT_STATUS_OK
) {
454 pretty
->options
.clock_seconds
= value
;
456 value
= false; /* Default. */
457 ret
= apply_one_bool("clock-date", params
, &value
, NULL
);
458 if (ret
!= BT_COMPONENT_STATUS_OK
) {
461 pretty
->options
.clock_date
= value
;
463 value
= false; /* Default. */
464 ret
= apply_one_bool("clock-gmt", params
, &value
, NULL
);
465 if (ret
!= BT_COMPONENT_STATUS_OK
) {
468 pretty
->options
.clock_gmt
= value
;
470 value
= false; /* Default. */
471 ret
= apply_one_bool("verbose", params
, &value
, NULL
);
472 if (ret
!= BT_COMPONENT_STATUS_OK
) {
475 pretty
->options
.verbose
= value
;
478 ret
= apply_one_string("name-default", params
, &str
);
479 if (ret
!= BT_COMPONENT_STATUS_OK
) {
483 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
484 } else if (!strcmp(str
, "show")) {
485 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
486 } else if (!strcmp(str
, "hide")) {
487 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
489 ret
= BT_COMPONENT_STATUS_ERROR
;
495 switch (pretty
->options
.name_default
) {
496 case PRETTY_DEFAULT_UNSET
:
497 pretty
->options
.print_payload_field_names
= true;
498 pretty
->options
.print_context_field_names
= true;
499 pretty
->options
.print_header_field_names
= false;
500 pretty
->options
.print_scope_field_names
= false;
502 case PRETTY_DEFAULT_SHOW
:
503 pretty
->options
.print_payload_field_names
= true;
504 pretty
->options
.print_context_field_names
= true;
505 pretty
->options
.print_header_field_names
= true;
506 pretty
->options
.print_scope_field_names
= true;
508 case PRETTY_DEFAULT_HIDE
:
509 pretty
->options
.print_payload_field_names
= false;
510 pretty
->options
.print_context_field_names
= false;
511 pretty
->options
.print_header_field_names
= false;
512 pretty
->options
.print_scope_field_names
= false;
515 ret
= BT_COMPONENT_STATUS_ERROR
;
521 ret
= apply_one_bool("name-payload", params
, &value
, &found
);
522 if (ret
!= BT_COMPONENT_STATUS_OK
) {
526 pretty
->options
.print_payload_field_names
= value
;
531 ret
= apply_one_bool("name-context", params
, &value
, &found
);
532 if (ret
!= BT_COMPONENT_STATUS_OK
) {
536 pretty
->options
.print_context_field_names
= value
;
541 ret
= apply_one_bool("name-header", params
, &value
, &found
);
542 if (ret
!= BT_COMPONENT_STATUS_OK
) {
546 pretty
->options
.print_header_field_names
= value
;
551 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
552 if (ret
!= BT_COMPONENT_STATUS_OK
) {
556 pretty
->options
.print_scope_field_names
= value
;
560 ret
= apply_one_string("field-default", params
, &str
);
561 if (ret
!= BT_COMPONENT_STATUS_OK
) {
565 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
566 } else if (!strcmp(str
, "show")) {
567 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
568 } else if (!strcmp(str
, "hide")) {
569 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
571 ret
= BT_COMPONENT_STATUS_ERROR
;
577 switch (pretty
->options
.field_default
) {
578 case PRETTY_DEFAULT_UNSET
:
579 pretty
->options
.print_trace_field
= false;
580 pretty
->options
.print_trace_hostname_field
= true;
581 pretty
->options
.print_trace_domain_field
= false;
582 pretty
->options
.print_trace_procname_field
= true;
583 pretty
->options
.print_trace_vpid_field
= true;
584 pretty
->options
.print_loglevel_field
= false;
585 pretty
->options
.print_emf_field
= false;
586 pretty
->options
.print_callsite_field
= false;
588 case PRETTY_DEFAULT_SHOW
:
589 pretty
->options
.print_trace_field
= true;
590 pretty
->options
.print_trace_hostname_field
= true;
591 pretty
->options
.print_trace_domain_field
= true;
592 pretty
->options
.print_trace_procname_field
= true;
593 pretty
->options
.print_trace_vpid_field
= true;
594 pretty
->options
.print_loglevel_field
= true;
595 pretty
->options
.print_emf_field
= true;
596 pretty
->options
.print_callsite_field
= true;
598 case PRETTY_DEFAULT_HIDE
:
599 pretty
->options
.print_trace_field
= false;
600 pretty
->options
.print_trace_hostname_field
= false;
601 pretty
->options
.print_trace_domain_field
= false;
602 pretty
->options
.print_trace_procname_field
= false;
603 pretty
->options
.print_trace_vpid_field
= false;
604 pretty
->options
.print_loglevel_field
= false;
605 pretty
->options
.print_emf_field
= false;
606 pretty
->options
.print_callsite_field
= false;
609 ret
= BT_COMPONENT_STATUS_ERROR
;
615 ret
= apply_one_bool("field-trace", params
, &value
, &found
);
616 if (ret
!= BT_COMPONENT_STATUS_OK
) {
620 pretty
->options
.print_trace_field
= value
;
625 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
626 if (ret
!= BT_COMPONENT_STATUS_OK
) {
630 pretty
->options
.print_trace_hostname_field
= value
;
635 ret
= apply_one_bool("field-trace:domain", params
, &value
, &found
);
636 if (ret
!= BT_COMPONENT_STATUS_OK
) {
640 pretty
->options
.print_trace_domain_field
= value
;
645 ret
= apply_one_bool("field-trace:procname", params
, &value
, &found
);
646 if (ret
!= BT_COMPONENT_STATUS_OK
) {
650 pretty
->options
.print_trace_procname_field
= value
;
655 ret
= apply_one_bool("field-trace:vpid", params
, &value
, &found
);
656 if (ret
!= BT_COMPONENT_STATUS_OK
) {
660 pretty
->options
.print_trace_vpid_field
= value
;
665 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
666 if (ret
!= BT_COMPONENT_STATUS_OK
) {
670 pretty
->options
.print_loglevel_field
= value
;
675 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
676 if (ret
!= BT_COMPONENT_STATUS_OK
) {
680 pretty
->options
.print_emf_field
= value
;
685 ret
= apply_one_bool("field-callsite", params
, &value
, &found
);
686 if (ret
!= BT_COMPONENT_STATUS_OK
) {
690 pretty
->options
.print_callsite_field
= value
;
694 bt_put(pretty
->plugin_opt_map
);
695 pretty
->plugin_opt_map
= NULL
;
701 void set_use_colors(struct pretty_component
*pretty
)
703 switch (pretty
->options
.color
) {
704 case PRETTY_COLOR_OPT_ALWAYS
:
705 pretty
->use_colors
= true;
707 case PRETTY_COLOR_OPT_AUTO
:
708 pretty
->use_colors
= pretty
->out
== stdout
&&
709 bt_common_colors_supported();
711 case PRETTY_COLOR_OPT_NEVER
:
712 pretty
->use_colors
= false;
718 void init_stream_packet_context_quarks(void)
720 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
721 g_quark_from_string("timestamp_begin");
722 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
723 g_quark_from_string("timestamp_begin");
724 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
725 g_quark_from_string("timestamp_end");
726 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
727 g_quark_from_string("events_discarded");
728 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
729 g_quark_from_string("content_size");
730 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
731 g_quark_from_string("packet_size");
732 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
733 g_quark_from_string("packet_seq_num");
737 enum bt_component_status
pretty_init(
738 struct bt_private_component
*component
,
739 struct bt_value
*params
,
740 UNUSED_VAR
void *init_method_data
)
742 enum bt_component_status ret
;
743 struct pretty_component
*pretty
= create_pretty();
746 ret
= BT_COMPONENT_STATUS_NOMEM
;
750 ret
= bt_private_component_sink_add_input_private_port(component
,
752 if (ret
!= BT_COMPONENT_STATUS_OK
) {
756 pretty
->out
= stdout
;
757 pretty
->err
= stderr
;
759 pretty
->delta_cycles
= -1ULL;
760 pretty
->last_cycles_timestamp
= -1ULL;
762 pretty
->delta_real_timestamp
= -1ULL;
763 pretty
->last_real_timestamp
= -1ULL;
765 ret
= apply_params(pretty
, params
);
766 if (ret
!= BT_COMPONENT_STATUS_OK
) {
770 set_use_colors(pretty
);
771 ret
= bt_private_component_set_user_data(component
, pretty
);
772 if (ret
!= BT_COMPONENT_STATUS_OK
) {
776 init_stream_packet_context_quarks();
781 destroy_pretty_data(pretty
);