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/values.h>
28 #include <babeltrace/compiler-internal.h>
29 #include <babeltrace/common-internal.h>
30 #include <plugins-common.h>
34 #include <babeltrace/assert-internal.h>
38 GQuark stream_packet_context_quarks
[STREAM_PACKET_CONTEXT_QUARKS_LEN
];
41 const char *plugin_options
[] = {
50 "name-default", /* show/hide */
55 "field-default", /* show/hide */
57 "field-trace:hostname",
59 "field-trace:procname",
67 void destroy_pretty_data(struct pretty_component
*pretty
)
69 bt_object_put_ref(pretty
->iterator
);
72 (void) g_string_free(pretty
->string
, TRUE
);
75 if (pretty
->tmp_string
) {
76 (void) g_string_free(pretty
->tmp_string
, TRUE
);
79 if (pretty
->out
!= stdout
) {
82 ret
= fclose(pretty
->out
);
84 perror("close output file");
87 g_free(pretty
->options
.output_path
);
92 struct pretty_component
*create_pretty(void)
94 struct pretty_component
*pretty
;
96 pretty
= g_new0(struct pretty_component
, 1);
100 pretty
->string
= g_string_new("");
101 if (!pretty
->string
) {
104 pretty
->tmp_string
= g_string_new("");
105 if (!pretty
->tmp_string
) {
117 void pretty_finalize(struct bt_self_component_sink
*comp
)
120 bt_self_component_get_data(
121 bt_self_component_sink_as_self_component(comp
)));
125 enum bt_self_component_status
handle_notification(
126 struct pretty_component
*pretty
,
127 struct bt_notification
*notification
)
129 enum bt_self_component_status ret
= BT_SELF_COMPONENT_STATUS_OK
;
133 switch (bt_notification_get_type(notification
)) {
134 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
135 if (pretty_print_packet(pretty
, notification
)) {
136 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
139 case BT_NOTIFICATION_TYPE_EVENT
:
140 if (pretty_print_event(pretty
, notification
)) {
141 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
144 case BT_NOTIFICATION_TYPE_INACTIVITY
:
145 fprintf(stderr
, "Inactivity notification\n");
155 enum bt_self_component_status
pretty_port_connected(
156 struct bt_self_component_sink
*comp
,
157 struct bt_self_component_port_input
*self_port
,
158 struct bt_port_output
*other_port
)
160 enum bt_self_component_status status
= BT_SELF_COMPONENT_STATUS_OK
;
161 struct pretty_component
*pretty
;
163 pretty
= bt_self_component_get_data(
164 bt_self_component_sink_as_self_component(comp
));
166 BT_ASSERT(!pretty
->iterator
);
167 pretty
->iterator
= bt_self_component_port_input_notification_iterator_create(
169 if (!pretty
->iterator
) {
170 status
= BT_SELF_COMPONENT_STATUS_NOMEM
;
177 enum bt_self_component_status
pretty_consume(
178 struct bt_self_component_sink
*comp
)
180 enum bt_self_component_status ret
;
181 bt_notification_array notifs
;
182 struct bt_self_component_port_input_notification_iterator
*it
;
183 struct pretty_component
*pretty
= bt_self_component_get_data(
184 bt_self_component_sink_as_self_component(comp
));
185 enum bt_notification_iterator_status it_ret
;
189 it
= pretty
->iterator
;
190 it_ret
= bt_self_component_port_input_notification_iterator_next(it
,
194 case BT_NOTIFICATION_ITERATOR_STATUS_OK
:
196 case BT_NOTIFICATION_ITERATOR_STATUS_NOMEM
:
197 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
199 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
200 ret
= BT_SELF_COMPONENT_STATUS_AGAIN
;
202 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
203 ret
= BT_SELF_COMPONENT_STATUS_END
;
204 BT_OBJECT_PUT_REF_AND_RESET(pretty
->iterator
);
207 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
211 BT_ASSERT(it_ret
== BT_NOTIFICATION_ITERATOR_STATUS_OK
);
213 for (i
= 0; i
< count
; i
++) {
214 ret
= handle_notification(pretty
, notifs
[i
]);
219 bt_object_put_ref(notifs
[i
]);
223 for (; i
< count
; i
++) {
224 bt_object_put_ref(notifs
[i
]);
231 int add_params_to_map(struct bt_private_value
*plugin_opt_map
)
236 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
237 const char *key
= plugin_options
[i
];
238 enum bt_value_status status
;
240 status
= bt_private_value_map_insert_entry(plugin_opt_map
, key
,
243 case BT_VALUE_STATUS_OK
:
255 bt_bool
check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
257 struct pretty_component
*pretty
= data
;
259 if (!bt_value_map_has_entry(
260 bt_private_value_as_value(pretty
->plugin_opt_map
),
263 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key
);
269 void apply_one_string(const char *key
, struct bt_value
*params
, char **option
)
271 struct bt_value
*value
= NULL
;
274 value
= bt_value_map_borrow_entry_value(params
, key
);
278 if (bt_value_is_null(value
)) {
281 str
= bt_value_string_get(value
);
282 *option
= g_strdup(str
);
289 void apply_one_bool(const char *key
, struct bt_value
*params
, bool *option
,
292 struct bt_value
*value
= NULL
;
295 value
= bt_value_map_borrow_entry_value(params
, key
);
299 bool_val
= bt_value_bool_get(value
);
300 *option
= (bool) bool_val
;
310 void warn_wrong_color_param(struct pretty_component
*pretty
)
313 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
317 int open_output_file(struct pretty_component
*pretty
)
321 if (!pretty
->options
.output_path
) {
325 pretty
->out
= fopen(pretty
->options
.output_path
, "w");
340 int apply_params(struct pretty_component
*pretty
, struct bt_value
*params
)
343 enum bt_value_status status
;
347 pretty
->plugin_opt_map
= bt_private_value_map_create();
348 if (!pretty
->plugin_opt_map
) {
352 ret
= add_params_to_map(pretty
->plugin_opt_map
);
356 /* Report unknown parameters. */
357 status
= bt_value_map_foreach_entry(params
, 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 struct bt_value
*color_value
;
371 color_value
= bt_value_map_borrow_entry_value(params
, "color");
376 color
= bt_value_string_get(color_value
);
378 if (strcmp(color
, "never") == 0) {
379 pretty
->options
.color
= PRETTY_COLOR_OPT_NEVER
;
380 } else if (strcmp(color
, "auto") == 0) {
381 pretty
->options
.color
= PRETTY_COLOR_OPT_AUTO
;
382 } else if (strcmp(color
, "always") == 0) {
383 pretty
->options
.color
= PRETTY_COLOR_OPT_ALWAYS
;
385 warn_wrong_color_param(pretty
);
389 apply_one_string("path", params
, &pretty
->options
.output_path
);
390 ret
= open_output_file(pretty
);
395 value
= false; /* Default. */
396 apply_one_bool("no-delta", params
, &value
, NULL
);
397 pretty
->options
.print_delta_field
= !value
; /* Reverse logic. */
399 value
= false; /* Default. */
400 apply_one_bool("clock-cycles", params
, &value
, NULL
);
401 pretty
->options
.print_timestamp_cycles
= value
;
403 value
= false; /* Default. */
404 apply_one_bool("clock-seconds", params
, &value
, NULL
);
405 pretty
->options
.clock_seconds
= value
;
407 value
= false; /* Default. */
408 apply_one_bool("clock-date", params
, &value
, NULL
);
409 pretty
->options
.clock_date
= value
;
411 value
= false; /* Default. */
412 apply_one_bool("clock-gmt", params
, &value
, NULL
);
413 pretty
->options
.clock_gmt
= value
;
415 value
= false; /* Default. */
416 apply_one_bool("verbose", params
, &value
, NULL
);
417 pretty
->options
.verbose
= value
;
420 apply_one_string("name-default", params
, &str
);
422 pretty
->options
.name_default
= PRETTY_DEFAULT_UNSET
;
423 } else if (!strcmp(str
, "show")) {
424 pretty
->options
.name_default
= PRETTY_DEFAULT_SHOW
;
425 } else if (!strcmp(str
, "hide")) {
426 pretty
->options
.name_default
= PRETTY_DEFAULT_HIDE
;
434 switch (pretty
->options
.name_default
) {
435 case PRETTY_DEFAULT_UNSET
:
436 pretty
->options
.print_payload_field_names
= true;
437 pretty
->options
.print_context_field_names
= true;
438 pretty
->options
.print_header_field_names
= false;
439 pretty
->options
.print_scope_field_names
= false;
441 case PRETTY_DEFAULT_SHOW
:
442 pretty
->options
.print_payload_field_names
= true;
443 pretty
->options
.print_context_field_names
= true;
444 pretty
->options
.print_header_field_names
= true;
445 pretty
->options
.print_scope_field_names
= true;
447 case PRETTY_DEFAULT_HIDE
:
448 pretty
->options
.print_payload_field_names
= false;
449 pretty
->options
.print_context_field_names
= false;
450 pretty
->options
.print_header_field_names
= false;
451 pretty
->options
.print_scope_field_names
= false;
460 apply_one_bool("name-payload", params
, &value
, &found
);
462 pretty
->options
.print_payload_field_names
= value
;
467 apply_one_bool("name-context", params
, &value
, &found
);
469 pretty
->options
.print_context_field_names
= value
;
474 apply_one_bool("name-header", params
, &value
, &found
);
476 pretty
->options
.print_header_field_names
= value
;
481 apply_one_bool("name-scope", params
, &value
, &found
);
483 pretty
->options
.print_scope_field_names
= value
;
487 apply_one_string("field-default", params
, &str
);
489 pretty
->options
.field_default
= PRETTY_DEFAULT_UNSET
;
490 } else if (!strcmp(str
, "show")) {
491 pretty
->options
.field_default
= PRETTY_DEFAULT_SHOW
;
492 } else if (!strcmp(str
, "hide")) {
493 pretty
->options
.field_default
= PRETTY_DEFAULT_HIDE
;
501 switch (pretty
->options
.field_default
) {
502 case PRETTY_DEFAULT_UNSET
:
503 pretty
->options
.print_trace_field
= false;
504 pretty
->options
.print_trace_hostname_field
= true;
505 pretty
->options
.print_trace_domain_field
= false;
506 pretty
->options
.print_trace_procname_field
= true;
507 pretty
->options
.print_trace_vpid_field
= true;
508 pretty
->options
.print_loglevel_field
= false;
509 pretty
->options
.print_emf_field
= false;
510 pretty
->options
.print_callsite_field
= false;
512 case PRETTY_DEFAULT_SHOW
:
513 pretty
->options
.print_trace_field
= true;
514 pretty
->options
.print_trace_hostname_field
= true;
515 pretty
->options
.print_trace_domain_field
= true;
516 pretty
->options
.print_trace_procname_field
= true;
517 pretty
->options
.print_trace_vpid_field
= true;
518 pretty
->options
.print_loglevel_field
= true;
519 pretty
->options
.print_emf_field
= true;
520 pretty
->options
.print_callsite_field
= true;
522 case PRETTY_DEFAULT_HIDE
:
523 pretty
->options
.print_trace_field
= false;
524 pretty
->options
.print_trace_hostname_field
= false;
525 pretty
->options
.print_trace_domain_field
= false;
526 pretty
->options
.print_trace_procname_field
= false;
527 pretty
->options
.print_trace_vpid_field
= false;
528 pretty
->options
.print_loglevel_field
= false;
529 pretty
->options
.print_emf_field
= false;
530 pretty
->options
.print_callsite_field
= false;
539 apply_one_bool("field-trace", params
, &value
, &found
);
541 pretty
->options
.print_trace_field
= value
;
546 apply_one_bool("field-trace:hostname", params
, &value
, &found
);
548 pretty
->options
.print_trace_hostname_field
= value
;
553 apply_one_bool("field-trace:domain", params
, &value
, &found
);
555 pretty
->options
.print_trace_domain_field
= value
;
560 apply_one_bool("field-trace:procname", params
, &value
, &found
);
562 pretty
->options
.print_trace_procname_field
= value
;
567 apply_one_bool("field-trace:vpid", params
, &value
, &found
);
569 pretty
->options
.print_trace_vpid_field
= value
;
574 apply_one_bool("field-loglevel", params
, &value
, &found
);
576 pretty
->options
.print_loglevel_field
= value
;
581 apply_one_bool("field-emf", params
, &value
, &found
);
583 pretty
->options
.print_emf_field
= value
;
588 apply_one_bool("field-callsite", params
, &value
, &found
);
590 pretty
->options
.print_callsite_field
= value
;
594 bt_object_put_ref(pretty
->plugin_opt_map
);
595 pretty
->plugin_opt_map
= NULL
;
601 void set_use_colors(struct pretty_component
*pretty
)
603 switch (pretty
->options
.color
) {
604 case PRETTY_COLOR_OPT_ALWAYS
:
605 pretty
->use_colors
= true;
607 case PRETTY_COLOR_OPT_AUTO
:
608 pretty
->use_colors
= pretty
->out
== stdout
&&
609 bt_common_colors_supported();
611 case PRETTY_COLOR_OPT_NEVER
:
612 pretty
->use_colors
= false;
618 void init_stream_packet_context_quarks(void)
620 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
621 g_quark_from_string("timestamp_begin");
622 stream_packet_context_quarks
[Q_TIMESTAMP_BEGIN
] =
623 g_quark_from_string("timestamp_begin");
624 stream_packet_context_quarks
[Q_TIMESTAMP_END
] =
625 g_quark_from_string("timestamp_end");
626 stream_packet_context_quarks
[Q_EVENTS_DISCARDED
] =
627 g_quark_from_string("events_discarded");
628 stream_packet_context_quarks
[Q_CONTENT_SIZE
] =
629 g_quark_from_string("content_size");
630 stream_packet_context_quarks
[Q_PACKET_SIZE
] =
631 g_quark_from_string("packet_size");
632 stream_packet_context_quarks
[Q_PACKET_SEQ_NUM
] =
633 g_quark_from_string("packet_seq_num");
637 enum bt_self_component_status
pretty_init(
638 struct bt_self_component_sink
*comp
,
639 struct bt_value
*params
,
640 UNUSED_VAR
void *init_method_data
)
642 enum bt_self_component_status ret
;
643 struct pretty_component
*pretty
= create_pretty();
646 ret
= BT_SELF_COMPONENT_STATUS_NOMEM
;
650 ret
= bt_self_component_sink_add_input_port(comp
, "in", NULL
, NULL
);
651 if (ret
!= BT_SELF_COMPONENT_STATUS_OK
) {
655 pretty
->out
= stdout
;
656 pretty
->err
= stderr
;
658 pretty
->delta_cycles
= -1ULL;
659 pretty
->last_cycles_timestamp
= -1ULL;
661 pretty
->delta_real_timestamp
= -1ULL;
662 pretty
->last_real_timestamp
= -1ULL;
664 if (apply_params(pretty
, params
)) {
665 ret
= BT_SELF_COMPONENT_STATUS_ERROR
;
669 set_use_colors(pretty
);
670 bt_self_component_set_data(
671 bt_self_component_sink_as_self_component(comp
), pretty
);
672 init_stream_packet_context_quarks();
678 destroy_pretty_data(pretty
);