2 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #include <babeltrace/babeltrace.h>
27 #include <babeltrace/compiler-internal.h>
28 #include <babeltrace/common-internal.h>
29 #include <plugins-common.h>
33 #include <babeltrace/assert-internal.h>
37 GQuark stream_packet_context_quarks
[STREAM_PACKET_CONTEXT_QUARKS_LEN
];
40 const char *plugin_options
[] = {
49 "name-default", /* show/hide */
54 "field-default", /* show/hide */
56 "field-trace:hostname",
58 "field-trace:procname",
66 void destroy_pretty_data(struct pretty_component
*pretty
)
68 bt_object_put_ref(pretty
->iterator
);
71 (void) g_string_free(pretty
->string
, TRUE
);
74 if (pretty
->tmp_string
) {
75 (void) g_string_free(pretty
->tmp_string
, TRUE
);
78 if (pretty
->out
!= stdout
) {
81 ret
= fclose(pretty
->out
);
83 perror("close output file");
86 g_free(pretty
->options
.output_path
);
91 struct pretty_component
*create_pretty(void)
93 struct pretty_component
*pretty
;
95 pretty
= g_new0(struct pretty_component
, 1);
99 pretty
->string
= g_string_new("");
100 if (!pretty
->string
) {
103 pretty
->tmp_string
= g_string_new("");
104 if (!pretty
->tmp_string
) {
116 void pretty_finalize(struct bt_self_component_sink
*comp
)
119 bt_self_component_get_data(
120 bt_self_component_sink_as_self_component(comp
)));
124 enum bt_self_component_status
handle_notification(
125 struct pretty_component
*pretty
,
126 struct bt_notification
*notification
)
128 enum bt_self_component_status ret
= BT_SELF_COMPONENT_STATUS_OK
;
132 switch (bt_notification_get_type(notification
)) {
133 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
134 if (pretty_print_packet(pretty
, notification
)) {
135 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
138 case BT_NOTIFICATION_TYPE_EVENT
:
139 if (pretty_print_event(pretty
, notification
)) {
140 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
143 case BT_NOTIFICATION_TYPE_INACTIVITY
:
144 fprintf(stderr
, "Inactivity notification\n");
154 enum bt_self_component_status
pretty_port_connected(
155 struct bt_self_component_sink
*comp
,
156 struct bt_self_component_port_input
*self_port
,
157 struct bt_port_output
*other_port
)
159 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
160 struct pretty_component
*pretty
;
162 pretty
= bt_self_component_get_data(
163 bt_self_component_sink_as_self_component(comp
));
165 BT_ASSERT(!pretty
->iterator
);
166 pretty
->iterator
= bt_self_component_port_input_notification_iterator_create(
168 if (!pretty
->iterator
) {
169 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
176 enum bt_self_component_status
pretty_consume(
177 struct bt_self_component_sink
*comp
)
179 enum bt_self_component_status ret
;
180 bt_notification_array notifs
;
181 struct bt_self_component_port_input_notification_iterator
*it
;
182 struct pretty_component
*pretty
= bt_self_component_get_data(
183 bt_self_component_sink_as_self_component(comp
));
184 enum bt_notification_iterator_status it_ret
;
188 it
= pretty
->iterator
;
189 it_ret
= bt_self_component_port_input_notification_iterator_next(it
,
193 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
195 case BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
:
196 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
198 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
199 ret
= BT_SELF_COMPONENT_STATUS_AGAIN
;
201 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
202 ret
= BT_SELF_COMPONENT_STATUS_END
;
203 BT_OBJECT_PUT_REF_AND_RESET(pretty
->iterator
);
206 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
210 BT_ASSERT(it_ret
== BT_NOTIFICATION_ITERATOR_STATUS_OK
);
212 for (i
= 0; i
< count
; i
++) {
213 ret
= handle_notification(pretty
, notifs
[i
]);
218 bt_object_put_ref(notifs
[i
]);
222 for (; i
< count
; i
++) {
223 bt_object_put_ref(notifs
[i
]);
230 int add_params_to_map(struct bt_value
*plugin_opt_map
)
235 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
236 const char *key
= plugin_options
[i
];
237 enum bt_value_status status
;
239 status
= bt_value_map_insert_entry(plugin_opt_map
, key
,
242 case BT_VALUE_STATUS_OK
:
254 bt_bool
check_param_exists(const char *key
, const struct bt_value
*object
,
257 struct pretty_component
*pretty
= data
;
259 if (!bt_value_map_has_entry(pretty
->plugin_opt_map
,
262 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key
);
268 void apply_one_string(const char *key
, const struct bt_value
*params
, char **option
)
270 const struct bt_value
*value
= NULL
;
273 value
= bt_value_map_borrow_entry_value_const(params
, key
);
277 if (bt_value_is_null(value
)) {
280 str
= bt_value_string_get(value
);
281 *option
= g_strdup(str
);
288 void apply_one_bool(const char *key
, const struct bt_value
*params
, bool *option
,
291 const struct bt_value
*value
= NULL
;
294 value
= bt_value_map_borrow_entry_value_const(params
, key
);
298 bool_val
= bt_value_bool_get(value
);
299 *option
= (bool) bool_val
;
309 void warn_wrong_color_param(struct pretty_component
*pretty
)
312 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
316 int open_output_file(struct pretty_component
*pretty
)
320 if (!pretty
->options
.output_path
) {
324 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
339 int apply_params(struct pretty_component
*pretty
, const struct bt_value
*params
)
342 enum bt_value_status status
;
346 pretty
->plugin_opt_map
= bt_value_map_create();
347 if (!pretty
->plugin_opt_map
) {
351 ret
= add_params_to_map(pretty
->plugin_opt_map
);
355 /* Report unknown parameters. */
356 status
= bt_value_map_foreach_entry_const(params
,
357 check_param_exists
, pretty
);
359 case BT_VALUE_STATUS_OK
:
365 /* Known parameters. */
366 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
367 if (bt_value_map_has_entry(params
, "color")) {
368 const struct bt_value
*color_value
;
371 color_value
= bt_value_map_borrow_entry_value_const(params
,
377 color
= bt_value_string_get(color_value
);
379 if (strcmp(color
, "never") == 0) {
380 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
381 } else if (strcmp(color
, "auto") == 0) {
382 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
383 } else if (strcmp(color
, "always") == 0) {
384 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
386 warn_wrong_color_param(pretty
);
390 apply_one_string("path", params
, &pretty
->options
.output_path
);
391 ret
= open_output_file(pretty
);
396 value
= false; /* Default. */
397 apply_one_bool("no-delta", params
, &value
, NULL
);
398 pretty
->options
.print_delta_field
= !value
; /* Reverse logic. */
400 value
= false; /* Default. */
401 apply_one_bool("clock-cycles", params
, &value
, NULL
);
402 pretty
->options
.print_timestamp_cycles
= value
;
404 value
= false; /* Default. */
405 apply_one_bool("clock-seconds", params
, &value
, NULL
);
406 pretty
->options
.clock_seconds
= value
;
408 value
= false; /* Default. */
409 apply_one_bool("clock-date", params
, &value
, NULL
);
410 pretty
->options
.clock_date
= value
;
412 value
= false; /* Default. */
413 apply_one_bool("clock-gmt", params
, &value
, NULL
);
414 pretty
->options
.clock_gmt
= value
;
416 value
= false; /* Default. */
417 apply_one_bool("verbose", params
, &value
, NULL
);
418 pretty
->options
.verbose
= value
;
421 apply_one_string("name-default", params
, &str
);
423 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
424 } else if (!strcmp(str
, "show")) {
425 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
426 } else if (!strcmp(str
, "hide")) {
427 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
435 switch (pretty
->options
.name_default
) {
436 case PRETTY_DEFAULT_UNSET
:
437 pretty
->options
.print_payload_field_names
= true;
438 pretty
->options
.print_context_field_names
= true;
439 pretty
->options
.print_header_field_names
= false;
440 pretty
->options
.print_scope_field_names
= false;
442 case PRETTY_DEFAULT_SHOW
:
443 pretty
->options
.print_payload_field_names
= true;
444 pretty
->options
.print_context_field_names
= true;
445 pretty
->options
.print_header_field_names
= true;
446 pretty
->options
.print_scope_field_names
= true;
448 case PRETTY_DEFAULT_HIDE
:
449 pretty
->options
.print_payload_field_names
= false;
450 pretty
->options
.print_context_field_names
= false;
451 pretty
->options
.print_header_field_names
= false;
452 pretty
->options
.print_scope_field_names
= false;
461 apply_one_bool("name-payload", params
, &value
, &found
);
463 pretty
->options
.print_payload_field_names
= value
;
468 apply_one_bool("name-context", params
, &value
, &found
);
470 pretty
->options
.print_context_field_names
= value
;
475 apply_one_bool("name-header", params
, &value
, &found
);
477 pretty
->options
.print_header_field_names
= value
;
482 apply_one_bool("name-scope", params
, &value
, &found
);
484 pretty
->options
.print_scope_field_names
= value
;
488 apply_one_string("field-default", params
, &str
);
490 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
491 } else if (!strcmp(str
, "show")) {
492 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
493 } else if (!strcmp(str
, "hide")) {
494 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
502 switch (pretty
->options
.field_default
) {
503 case PRETTY_DEFAULT_UNSET
:
504 pretty
->options
.print_trace_field
= false;
505 pretty
->options
.print_trace_hostname_field
= true;
506 pretty
->options
.print_trace_domain_field
= false;
507 pretty
->options
.print_trace_procname_field
= true;
508 pretty
->options
.print_trace_vpid_field
= true;
509 pretty
->options
.print_loglevel_field
= false;
510 pretty
->options
.print_emf_field
= false;
511 pretty
->options
.print_callsite_field
= false;
513 case PRETTY_DEFAULT_SHOW
:
514 pretty
->options
.print_trace_field
= true;
515 pretty
->options
.print_trace_hostname_field
= true;
516 pretty
->options
.print_trace_domain_field
= true;
517 pretty
->options
.print_trace_procname_field
= true;
518 pretty
->options
.print_trace_vpid_field
= true;
519 pretty
->options
.print_loglevel_field
= true;
520 pretty
->options
.print_emf_field
= true;
521 pretty
->options
.print_callsite_field
= true;
523 case PRETTY_DEFAULT_HIDE
:
524 pretty
->options
.print_trace_field
= false;
525 pretty
->options
.print_trace_hostname_field
= false;
526 pretty
->options
.print_trace_domain_field
= false;
527 pretty
->options
.print_trace_procname_field
= false;
528 pretty
->options
.print_trace_vpid_field
= false;
529 pretty
->options
.print_loglevel_field
= false;
530 pretty
->options
.print_emf_field
= false;
531 pretty
->options
.print_callsite_field
= false;
540 apply_one_bool("field-trace", params
, &value
, &found
);
542 pretty
->options
.print_trace_field
= value
;
547 apply_one_bool("field-trace:hostname", params
, &value
, &found
);
549 pretty
->options
.print_trace_hostname_field
= value
;
554 apply_one_bool("field-trace:domain", params
, &value
, &found
);
556 pretty
->options
.print_trace_domain_field
= value
;
561 apply_one_bool("field-trace:procname", params
, &value
, &found
);
563 pretty
->options
.print_trace_procname_field
= value
;
568 apply_one_bool("field-trace:vpid", params
, &value
, &found
);
570 pretty
->options
.print_trace_vpid_field
= value
;
575 apply_one_bool("field-loglevel", params
, &value
, &found
);
577 pretty
->options
.print_loglevel_field
= value
;
582 apply_one_bool("field-emf", params
, &value
, &found
);
584 pretty
->options
.print_emf_field
= value
;
589 apply_one_bool("field-callsite", params
, &value
, &found
);
591 pretty
->options
.print_callsite_field
= value
;
595 bt_object_put_ref(pretty
->plugin_opt_map
);
596 pretty
->plugin_opt_map
= NULL
;
602 void set_use_colors(struct pretty_component
*pretty
)
604 switch (pretty
->options
.color
) {
605 case PRETTY_COLOR_OPT_ALWAYS
:
606 pretty
->use_colors
= true;
608 case PRETTY_COLOR_OPT_AUTO
:
609 pretty
->use_colors
= pretty
->out
== stdout
&&
610 bt_common_colors_supported();
612 case PRETTY_COLOR_OPT_NEVER
:
613 pretty
->use_colors
= false;
619 void init_stream_packet_context_quarks(void)
621 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
622 g_quark_from_string("timestamp_begin");
623 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
624 g_quark_from_string("timestamp_begin");
625 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
626 g_quark_from_string("timestamp_end");
627 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
628 g_quark_from_string("events_discarded");
629 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
630 g_quark_from_string("content_size");
631 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
632 g_quark_from_string("packet_size");
633 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
634 g_quark_from_string("packet_seq_num");
638 enum bt_self_component_status
pretty_init(
639 struct bt_self_component_sink
*comp
,
640 const struct bt_value
*params
,
641 UNUSED_VAR
void *init_method_data
)
643 enum bt_self_component_status ret
;
644 struct pretty_component
*pretty
= create_pretty();
647 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
651 ret
= bt_self_component_sink_add_input_port(comp
, "in", NULL
, NULL
);
652 if (ret
!= BT_SELF_COMPONENT_STATUS_OK
) {
656 pretty
->out
= stdout
;
657 pretty
->err
= stderr
;
659 pretty
->delta_cycles
= -1ULL;
660 pretty
->last_cycles_timestamp
= -1ULL;
662 pretty
->delta_real_timestamp
= -1ULL;
663 pretty
->last_real_timestamp
= -1ULL;
665 if (apply_params(pretty
, params
)) {
666 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
670 set_use_colors(pretty
);
671 bt_self_component_set_data(
672 bt_self_component_sink_as_self_component(comp
), pretty
);
673 init_stream_packet_context_quarks();
679 destroy_pretty_data(pretty
);