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/component-sink.h>
34 #include <babeltrace/graph/port.h>
35 #include <babeltrace/graph/private-port.h>
36 #include <babeltrace/graph/connection.h>
37 #include <babeltrace/graph/private-connection.h>
38 #include <babeltrace/graph/notification.h>
39 #include <babeltrace/graph/notification-iterator.h>
40 #include <babeltrace/graph/notification-event.h>
41 #include <babeltrace/values.h>
42 #include <babeltrace/compiler-internal.h>
43 #include <babeltrace/common-internal.h>
44 #include <plugins-common.h>
53 const char *plugin_options
[] = {
62 "name-default", /* show/hide */
67 "field-default", /* show/hide */
69 "field-trace:hostname",
71 "field-trace:procname",
79 void destroy_pretty_data(struct pretty_component
*pretty
)
81 bt_put(pretty
->input_iterator
);
82 (void) g_string_free(pretty
->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
) {
117 void pretty_finalize(struct bt_private_component
*component
)
119 void *data
= bt_private_component_get_user_data(component
);
121 destroy_pretty_data(data
);
125 enum bt_component_status
handle_notification(struct pretty_component
*pretty
,
126 struct bt_notification
*notification
)
128 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
132 if (bt_notification_get_type(notification
) == BT_NOTIFICATION_TYPE_EVENT
) {
133 ret
= pretty_print_event(pretty
, notification
);
140 void pretty_port_connected(
141 struct bt_private_component
*component
,
142 struct bt_private_port
*self_port
,
143 struct bt_port
*other_port
)
145 struct bt_private_connection
*connection
;
146 struct pretty_component
*pretty
;
148 pretty
= bt_private_component_get_user_data(component
);
150 assert(!pretty
->input_iterator
);
151 connection
= bt_private_port_get_private_connection(self_port
);
153 pretty
->input_iterator
=
154 bt_private_connection_create_notification_iterator(connection
);
156 if (!pretty
->input_iterator
) {
157 pretty
->error
= true;
164 enum bt_component_status
pretty_consume(struct bt_private_component
*component
)
166 enum bt_component_status ret
;
167 struct bt_notification
*notification
= NULL
;
168 struct bt_notification_iterator
*it
;
169 struct pretty_component
*pretty
=
170 bt_private_component_get_user_data(component
);
171 enum bt_notification_iterator_status it_ret
;
173 if (unlikely(pretty
->error
)) {
174 ret
= BT_COMPONENT_STATUS_ERROR
;
178 it
= pretty
->input_iterator
;
179 it_ret
= bt_notification_iterator_next(it
);
182 case BT_NOTIFICATION_ITERATOR_STATUS_ERROR
:
183 ret
= BT_COMPONENT_STATUS_ERROR
;
185 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
186 ret
= BT_COMPONENT_STATUS_END
;
187 BT_PUT(pretty
->input_iterator
);
193 notification
= bt_notification_iterator_get_notification(it
);
194 assert(notification
);
195 ret
= handle_notification(pretty
, notification
);
197 bt_put(notification
);
202 enum bt_component_status
add_params_to_map(struct bt_value
*plugin_opt_map
)
204 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
207 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
208 const char *key
= plugin_options
[i
];
209 enum bt_value_status status
;
211 status
= bt_value_map_insert(plugin_opt_map
, key
, bt_value_null
);
213 case BT_VALUE_STATUS_OK
:
216 ret
= BT_COMPONENT_STATUS_ERROR
;
225 bool check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
227 struct pretty_component
*pretty
= data
;
228 struct bt_value
*plugin_opt_map
= pretty
->plugin_opt_map
;
230 if (!bt_value_map_get(plugin_opt_map
, key
)) {
232 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key
);
238 enum bt_component_status
apply_one_string(const char *key
,
239 struct bt_value
*params
,
242 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
243 struct bt_value
*value
= NULL
;
244 enum bt_value_status status
;
247 value
= bt_value_map_get(params
, key
);
251 if (bt_value_is_null(value
)) {
254 status
= bt_value_string_get(value
, &str
);
256 case BT_VALUE_STATUS_OK
:
259 ret
= BT_COMPONENT_STATUS_ERROR
;
262 *option
= g_strdup(str
);
269 enum bt_component_status
apply_one_bool(const char *key
,
270 struct bt_value
*params
,
274 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
275 struct bt_value
*value
= NULL
;
276 enum bt_value_status status
;
278 value
= bt_value_map_get(params
, key
);
282 status
= bt_value_bool_get(value
, option
);
284 case BT_VALUE_STATUS_OK
:
287 ret
= BT_COMPONENT_STATUS_ERROR
;
299 void warn_wrong_color_param(struct pretty_component
*pretty
)
302 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
306 enum bt_component_status
open_output_file(struct pretty_component
*pretty
)
308 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
310 if (!pretty
->options
.output_path
) {
314 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
322 ret
= BT_COMPONENT_STATUS_ERROR
;
328 enum bt_component_status
apply_params(struct pretty_component
*pretty
,
329 struct bt_value
*params
)
331 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
332 enum bt_value_status status
;
336 pretty
->plugin_opt_map
= bt_value_map_create();
337 if (!pretty
->plugin_opt_map
) {
338 ret
= BT_COMPONENT_STATUS_ERROR
;
341 ret
= add_params_to_map(pretty
->plugin_opt_map
);
342 if (ret
!= BT_COMPONENT_STATUS_OK
) {
345 /* Report unknown parameters. */
346 status
= bt_value_map_foreach(params
, check_param_exists
, pretty
);
348 case BT_VALUE_STATUS_OK
:
351 ret
= BT_COMPONENT_STATUS_ERROR
;
354 /* Known parameters. */
355 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
356 if (bt_value_map_has_key(params
, "color")) {
357 struct bt_value
*color_value
;
360 color_value
= bt_value_map_get(params
, "color");
365 status
= bt_value_string_get(color_value
, &color
);
367 warn_wrong_color_param(pretty
);
369 if (strcmp(color
, "never") == 0) {
370 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
371 } else if (strcmp(color
, "auto") == 0) {
372 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
373 } else if (strcmp(color
, "always") == 0) {
374 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
376 warn_wrong_color_param(pretty
);
383 ret
= apply_one_string("output-path",
385 &pretty
->options
.output_path
);
386 if (ret
!= BT_COMPONENT_STATUS_OK
) {
389 ret
= open_output_file(pretty
);
390 if (ret
!= BT_COMPONENT_STATUS_OK
) {
394 value
= false; /* Default. */
395 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
396 if (ret
!= BT_COMPONENT_STATUS_OK
) {
399 pretty
->options
.print_delta_field
= !value
; /* Reverse logic. */
401 value
= false; /* Default. */
402 ret
= apply_one_bool("clock-cycles", params
, &value
, NULL
);
403 if (ret
!= BT_COMPONENT_STATUS_OK
) {
406 pretty
->options
.print_timestamp_cycles
= value
;
408 value
= false; /* Default. */
409 ret
= apply_one_bool("clock-seconds", params
, &value
, NULL
);
410 if (ret
!= BT_COMPONENT_STATUS_OK
) {
413 pretty
->options
.clock_seconds
= value
;
415 value
= false; /* Default. */
416 ret
= apply_one_bool("clock-date", params
, &value
, NULL
);
417 if (ret
!= BT_COMPONENT_STATUS_OK
) {
420 pretty
->options
.clock_date
= value
;
422 value
= false; /* Default. */
423 ret
= apply_one_bool("clock-gmt", params
, &value
, NULL
);
424 if (ret
!= BT_COMPONENT_STATUS_OK
) {
427 pretty
->options
.clock_gmt
= value
;
429 value
= false; /* Default. */
430 ret
= apply_one_bool("verbose", params
, &value
, NULL
);
431 if (ret
!= BT_COMPONENT_STATUS_OK
) {
434 pretty
->options
.verbose
= value
;
437 ret
= apply_one_string("name-default", params
, &str
);
438 if (ret
!= BT_COMPONENT_STATUS_OK
) {
442 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
443 } else if (!strcmp(str
, "show")) {
444 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
445 } else if (!strcmp(str
, "hide")) {
446 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
448 ret
= BT_COMPONENT_STATUS_ERROR
;
454 switch (pretty
->options
.name_default
) {
455 case PRETTY_DEFAULT_UNSET
:
456 pretty
->options
.print_payload_field_names
= true;
457 pretty
->options
.print_context_field_names
= true;
458 pretty
->options
.print_header_field_names
= false;
459 pretty
->options
.print_scope_field_names
= false;
461 case PRETTY_DEFAULT_SHOW
:
462 pretty
->options
.print_payload_field_names
= true;
463 pretty
->options
.print_context_field_names
= true;
464 pretty
->options
.print_header_field_names
= true;
465 pretty
->options
.print_scope_field_names
= true;
467 case PRETTY_DEFAULT_HIDE
:
468 pretty
->options
.print_payload_field_names
= false;
469 pretty
->options
.print_context_field_names
= false;
470 pretty
->options
.print_header_field_names
= false;
471 pretty
->options
.print_scope_field_names
= false;
474 ret
= BT_COMPONENT_STATUS_ERROR
;
480 ret
= apply_one_bool("name-payload", params
, &value
, &found
);
481 if (ret
!= BT_COMPONENT_STATUS_OK
) {
485 pretty
->options
.print_payload_field_names
= value
;
490 ret
= apply_one_bool("name-context", params
, &value
, &found
);
491 if (ret
!= BT_COMPONENT_STATUS_OK
) {
495 pretty
->options
.print_context_field_names
= value
;
500 ret
= apply_one_bool("name-header", params
, &value
, &found
);
501 if (ret
!= BT_COMPONENT_STATUS_OK
) {
505 pretty
->options
.print_header_field_names
= value
;
510 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
511 if (ret
!= BT_COMPONENT_STATUS_OK
) {
515 pretty
->options
.print_scope_field_names
= value
;
519 ret
= apply_one_string("field-default", params
, &str
);
520 if (ret
!= BT_COMPONENT_STATUS_OK
) {
524 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
525 } else if (!strcmp(str
, "show")) {
526 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
527 } else if (!strcmp(str
, "hide")) {
528 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
530 ret
= BT_COMPONENT_STATUS_ERROR
;
536 switch (pretty
->options
.field_default
) {
537 case PRETTY_DEFAULT_UNSET
:
538 pretty
->options
.print_trace_field
= false;
539 pretty
->options
.print_trace_hostname_field
= true;
540 pretty
->options
.print_trace_domain_field
= false;
541 pretty
->options
.print_trace_procname_field
= true;
542 pretty
->options
.print_trace_vpid_field
= true;
543 pretty
->options
.print_loglevel_field
= false;
544 pretty
->options
.print_emf_field
= false;
545 pretty
->options
.print_callsite_field
= false;
547 case PRETTY_DEFAULT_SHOW
:
548 pretty
->options
.print_trace_field
= true;
549 pretty
->options
.print_trace_hostname_field
= true;
550 pretty
->options
.print_trace_domain_field
= true;
551 pretty
->options
.print_trace_procname_field
= true;
552 pretty
->options
.print_trace_vpid_field
= true;
553 pretty
->options
.print_loglevel_field
= true;
554 pretty
->options
.print_emf_field
= true;
555 pretty
->options
.print_callsite_field
= true;
557 case PRETTY_DEFAULT_HIDE
:
558 pretty
->options
.print_trace_field
= false;
559 pretty
->options
.print_trace_hostname_field
= false;
560 pretty
->options
.print_trace_domain_field
= false;
561 pretty
->options
.print_trace_procname_field
= false;
562 pretty
->options
.print_trace_vpid_field
= false;
563 pretty
->options
.print_loglevel_field
= false;
564 pretty
->options
.print_emf_field
= false;
565 pretty
->options
.print_callsite_field
= false;
568 ret
= BT_COMPONENT_STATUS_ERROR
;
574 ret
= apply_one_bool("field-trace", params
, &value
, &found
);
575 if (ret
!= BT_COMPONENT_STATUS_OK
) {
579 pretty
->options
.print_trace_field
= value
;
584 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
585 if (ret
!= BT_COMPONENT_STATUS_OK
) {
589 pretty
->options
.print_trace_hostname_field
= value
;
594 ret
= apply_one_bool("field-trace:domain", params
, &value
, &found
);
595 if (ret
!= BT_COMPONENT_STATUS_OK
) {
599 pretty
->options
.print_trace_domain_field
= value
;
604 ret
= apply_one_bool("field-trace:procname", params
, &value
, &found
);
605 if (ret
!= BT_COMPONENT_STATUS_OK
) {
609 pretty
->options
.print_trace_procname_field
= value
;
614 ret
= apply_one_bool("field-trace:vpid", params
, &value
, &found
);
615 if (ret
!= BT_COMPONENT_STATUS_OK
) {
619 pretty
->options
.print_trace_vpid_field
= value
;
624 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
625 if (ret
!= BT_COMPONENT_STATUS_OK
) {
629 pretty
->options
.print_loglevel_field
= value
;
634 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
635 if (ret
!= BT_COMPONENT_STATUS_OK
) {
639 pretty
->options
.print_emf_field
= value
;
644 ret
= apply_one_bool("field-callsite", params
, &value
, &found
);
645 if (ret
!= BT_COMPONENT_STATUS_OK
) {
649 pretty
->options
.print_callsite_field
= value
;
653 bt_put(pretty
->plugin_opt_map
);
654 pretty
->plugin_opt_map
= NULL
;
660 void set_use_colors(struct pretty_component
*pretty
)
662 switch (pretty
->options
.color
) {
663 case PRETTY_COLOR_OPT_ALWAYS
:
664 pretty
->use_colors
= true;
666 case PRETTY_COLOR_OPT_AUTO
:
667 pretty
->use_colors
= pretty
->out
== stdout
&&
668 bt_common_colors_supported();
670 case PRETTY_COLOR_OPT_NEVER
:
671 pretty
->use_colors
= false;
677 void init_stream_packet_context_quarks(void)
679 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
680 g_quark_from_string("timestamp_begin");
681 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
682 g_quark_from_string("timestamp_begin");
683 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
684 g_quark_from_string("timestamp_end");
685 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
686 g_quark_from_string("events_discarded");
687 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
688 g_quark_from_string("content_size");
689 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
690 g_quark_from_string("packet_size");
691 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
692 g_quark_from_string("packet_seq_num");
696 enum bt_component_status
pretty_init(
697 struct bt_private_component
*component
,
698 struct bt_value
*params
,
699 UNUSED_VAR
void *init_method_data
)
701 enum bt_component_status ret
;
702 struct pretty_component
*pretty
= create_pretty();
705 ret
= BT_COMPONENT_STATUS_NOMEM
;
709 pretty
->out
= stdout
;
710 pretty
->err
= stderr
;
712 pretty
->delta_cycles
= -1ULL;
713 pretty
->last_cycles_timestamp
= -1ULL;
715 pretty
->delta_real_timestamp
= -1ULL;
716 pretty
->last_real_timestamp
= -1ULL;
718 ret
= apply_params(pretty
, params
);
719 if (ret
!= BT_COMPONENT_STATUS_OK
) {
723 set_use_colors(pretty
);
725 ret
= bt_private_component_set_user_data(component
, pretty
);
726 if (ret
!= BT_COMPONENT_STATUS_OK
) {
730 init_stream_packet_context_quarks();
735 destroy_pretty_data(pretty
);