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/component/component.h>
32 #include <babeltrace/component/component-sink.h>
33 #include <babeltrace/component/notification/notification.h>
34 #include <babeltrace/component/notification/iterator.h>
35 #include <babeltrace/component/notification/event.h>
36 #include <babeltrace/values.h>
37 #include <babeltrace/compiler.h>
38 #include <babeltrace/common-internal.h>
39 #include <plugins-common.h>
46 const char *plugin_options
[] = {
50 "debug-info-target-prefix",
51 "debug-info-full-path",
57 "name-default", /* show/hide */
62 "field-default", /* show/hide */
64 "field-trace:hostname",
66 "field-trace:procname",
73 void destroy_text_data(struct text_component
*text
)
75 (void) g_string_free(text
->string
, TRUE
);
76 g_free(text
->options
.output_path
);
77 g_free(text
->options
.debug_info_dir
);
78 g_free(text
->options
.debug_info_target_prefix
);
83 struct text_component
*create_text(void)
85 struct text_component
*text
;
87 text
= g_new0(struct text_component
, 1);
91 text
->string
= g_string_new("");
104 void destroy_text(struct bt_component
*component
)
106 void *data
= bt_component_get_private_data(component
);
108 destroy_text_data(data
);
112 enum bt_component_status
handle_notification(struct text_component
*text
,
113 struct bt_notification
*notification
)
115 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
118 ret
= BT_COMPONENT_STATUS_ERROR
;
122 switch (bt_notification_get_type(notification
)) {
123 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
126 case BT_NOTIFICATION_TYPE_PACKET_END
:
129 case BT_NOTIFICATION_TYPE_EVENT
:
131 struct bt_ctf_event
*event
= bt_notification_event_get_event(
135 ret
= BT_COMPONENT_STATUS_ERROR
;
138 ret
= text_print_event(text
, event
);
140 if (ret
!= BT_COMPONENT_STATUS_OK
) {
145 case BT_NOTIFICATION_TYPE_STREAM_END
:
149 puts("Unhandled notification type");
156 enum bt_component_status
run(struct bt_component
*component
)
158 enum bt_component_status ret
;
159 struct bt_notification
*notification
= NULL
;
160 struct bt_notification_iterator
*it
;
161 struct text_component
*text
= bt_component_get_private_data(component
);
163 ret
= bt_component_sink_get_input_iterator(component
, 0, &it
);
164 if (ret
!= BT_COMPONENT_STATUS_OK
) {
168 if (likely(text
->processed_first_event
)) {
169 enum bt_notification_iterator_status it_ret
;
171 it_ret
= bt_notification_iterator_next(it
);
173 case BT_NOTIFICATION_ITERATOR_STATUS_ERROR
:
174 ret
= BT_COMPONENT_STATUS_ERROR
;
176 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
177 ret
= BT_COMPONENT_STATUS_END
;
183 notification
= bt_notification_iterator_get_notification(it
);
185 ret
= BT_COMPONENT_STATUS_ERROR
;
189 ret
= handle_notification(text
, notification
);
190 text
->processed_first_event
= true;
193 bt_put(notification
);
198 enum bt_component_status
add_params_to_map(struct bt_value
*plugin_opt_map
)
200 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
203 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
204 const char *key
= plugin_options
[i
];
205 enum bt_value_status status
;
207 status
= bt_value_map_insert(plugin_opt_map
, key
, bt_value_null
);
209 case BT_VALUE_STATUS_OK
:
212 ret
= BT_COMPONENT_STATUS_ERROR
;
221 bool check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
223 struct text_component
*text
= data
;
224 struct bt_value
*plugin_opt_map
= text
->plugin_opt_map
;
226 if (!bt_value_map_get(plugin_opt_map
, key
)) {
228 "[warning] Parameter \"%s\" unknown to \"text\" plugin\n", key
);
234 enum bt_component_status
apply_one_string(const char *key
,
235 struct bt_value
*params
,
238 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
239 struct bt_value
*value
= NULL
;
240 enum bt_value_status status
;
243 value
= bt_value_map_get(params
, key
);
247 if (bt_value_is_null(value
)) {
250 status
= bt_value_string_get(value
, &str
);
252 case BT_VALUE_STATUS_OK
:
255 ret
= BT_COMPONENT_STATUS_ERROR
;
258 *option
= g_strdup(str
);
265 enum bt_component_status
apply_one_bool(const char *key
,
266 struct bt_value
*params
,
270 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
271 struct bt_value
*value
= NULL
;
272 enum bt_value_status status
;
274 value
= bt_value_map_get(params
, key
);
278 status
= bt_value_bool_get(value
, option
);
280 case BT_VALUE_STATUS_OK
:
283 ret
= BT_COMPONENT_STATUS_ERROR
;
295 void warn_wrong_color_param(struct text_component
*text
)
298 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
302 enum bt_component_status
apply_params(struct text_component
*text
,
303 struct bt_value
*params
)
305 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
306 enum bt_value_status status
;
310 text
->plugin_opt_map
= bt_value_map_create();
311 if (!text
->plugin_opt_map
) {
312 ret
= BT_COMPONENT_STATUS_ERROR
;
315 ret
= add_params_to_map(text
->plugin_opt_map
);
316 if (ret
!= BT_COMPONENT_STATUS_OK
) {
319 /* Report unknown parameters. */
320 status
= bt_value_map_foreach(params
, check_param_exists
, text
);
322 case BT_VALUE_STATUS_OK
:
325 ret
= BT_COMPONENT_STATUS_ERROR
;
328 /* Known parameters. */
329 text
->options
.color
= TEXT_COLOR_OPT_AUTO
;
330 if (bt_value_map_has_key(params
, "color")) {
331 struct bt_value
*color_value
;
334 color_value
= bt_value_map_get(params
, "color");
339 ret
= bt_value_string_get(color_value
, &color
);
341 warn_wrong_color_param(text
);
343 if (strcmp(color
, "never") == 0) {
344 text
->options
.color
= TEXT_COLOR_OPT_NEVER
;
345 } else if (strcmp(color
, "auto") == 0) {
346 text
->options
.color
= TEXT_COLOR_OPT_AUTO
;
347 } else if (strcmp(color
, "always") == 0) {
348 text
->options
.color
= TEXT_COLOR_OPT_ALWAYS
;
350 warn_wrong_color_param(text
);
357 ret
= apply_one_string("output-path",
359 &text
->options
.output_path
);
360 if (ret
!= BT_COMPONENT_STATUS_OK
) {
364 ret
= apply_one_string("debug-info-dir",
366 &text
->options
.debug_info_dir
);
367 if (ret
!= BT_COMPONENT_STATUS_OK
) {
371 ret
= apply_one_string("debug-info-target-prefix",
373 &text
->options
.debug_info_target_prefix
);
374 if (ret
!= BT_COMPONENT_STATUS_OK
) {
378 value
= false; /* Default. */
379 ret
= apply_one_bool("debug-info-full-path", params
, &value
, NULL
);
380 if (ret
!= BT_COMPONENT_STATUS_OK
) {
383 text
->options
.debug_info_full_path
= value
;
385 value
= false; /* Default. */
386 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
387 if (ret
!= BT_COMPONENT_STATUS_OK
) {
390 text
->options
.print_delta_field
= !value
; /* Reverse logic. */
392 value
= false; /* Default. */
393 ret
= apply_one_bool("clock-cycles", params
, &value
, NULL
);
394 if (ret
!= BT_COMPONENT_STATUS_OK
) {
397 text
->options
.print_timestamp_cycles
= value
;
399 value
= false; /* Default. */
400 ret
= apply_one_bool("clock-seconds", params
, &value
, NULL
);
401 if (ret
!= BT_COMPONENT_STATUS_OK
) {
404 text
->options
.clock_seconds
= value
;
406 value
= false; /* Default. */
407 ret
= apply_one_bool("clock-date", params
, &value
, NULL
);
408 if (ret
!= BT_COMPONENT_STATUS_OK
) {
411 text
->options
.clock_date
= value
;
413 value
= false; /* Default. */
414 ret
= apply_one_bool("clock-gmt", params
, &value
, NULL
);
415 if (ret
!= BT_COMPONENT_STATUS_OK
) {
418 text
->options
.clock_gmt
= value
;
421 ret
= apply_one_string("name-default", params
, &str
);
422 if (ret
!= BT_COMPONENT_STATUS_OK
) {
426 text
->options
.name_default
= TEXT_DEFAULT_UNSET
;
427 } else if (!strcmp(str
, "show")) {
428 text
->options
.name_default
= TEXT_DEFAULT_SHOW
;
429 } else if (!strcmp(str
, "hide")) {
430 text
->options
.name_default
= TEXT_DEFAULT_HIDE
;
432 ret
= BT_COMPONENT_STATUS_ERROR
;
438 switch (text
->options
.name_default
) {
439 case TEXT_DEFAULT_UNSET
:
440 text
->options
.print_payload_field_names
= true;
441 text
->options
.print_context_field_names
= true;
442 text
->options
.print_header_field_names
= false;
443 text
->options
.print_scope_field_names
= false;
445 case TEXT_DEFAULT_SHOW
:
446 text
->options
.print_payload_field_names
= true;
447 text
->options
.print_context_field_names
= true;
448 text
->options
.print_header_field_names
= true;
449 text
->options
.print_scope_field_names
= true;
451 case TEXT_DEFAULT_HIDE
:
452 text
->options
.print_payload_field_names
= false;
453 text
->options
.print_context_field_names
= false;
454 text
->options
.print_header_field_names
= false;
455 text
->options
.print_scope_field_names
= false;
458 ret
= BT_COMPONENT_STATUS_ERROR
;
464 ret
= apply_one_bool("name-payload", params
, &value
, &found
);
465 if (ret
!= BT_COMPONENT_STATUS_OK
) {
469 text
->options
.print_payload_field_names
= value
;
474 ret
= apply_one_bool("name-context", params
, &value
, &found
);
475 if (ret
!= BT_COMPONENT_STATUS_OK
) {
479 text
->options
.print_context_field_names
= value
;
484 ret
= apply_one_bool("name-header", params
, &value
, &found
);
485 if (ret
!= BT_COMPONENT_STATUS_OK
) {
489 text
->options
.print_header_field_names
= value
;
494 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
495 if (ret
!= BT_COMPONENT_STATUS_OK
) {
499 text
->options
.print_scope_field_names
= value
;
503 ret
= apply_one_string("field-default", params
, &str
);
504 if (ret
!= BT_COMPONENT_STATUS_OK
) {
508 text
->options
.field_default
= TEXT_DEFAULT_UNSET
;
509 } else if (!strcmp(str
, "show")) {
510 text
->options
.field_default
= TEXT_DEFAULT_SHOW
;
511 } else if (!strcmp(str
, "hide")) {
512 text
->options
.field_default
= TEXT_DEFAULT_HIDE
;
514 ret
= BT_COMPONENT_STATUS_ERROR
;
520 switch (text
->options
.field_default
) {
521 case TEXT_DEFAULT_UNSET
:
522 text
->options
.print_trace_field
= false;
523 text
->options
.print_trace_hostname_field
= true;
524 text
->options
.print_trace_domain_field
= false;
525 text
->options
.print_trace_procname_field
= true;
526 text
->options
.print_trace_vpid_field
= true;
527 text
->options
.print_loglevel_field
= false;
528 text
->options
.print_emf_field
= false;
529 text
->options
.print_emf_field
= false;
531 case TEXT_DEFAULT_SHOW
:
532 text
->options
.print_trace_field
= true;
533 text
->options
.print_trace_hostname_field
= true;
534 text
->options
.print_trace_domain_field
= true;
535 text
->options
.print_trace_procname_field
= true;
536 text
->options
.print_trace_vpid_field
= true;
537 text
->options
.print_loglevel_field
= true;
538 text
->options
.print_emf_field
= true;
539 text
->options
.print_emf_field
= true;
541 case TEXT_DEFAULT_HIDE
:
542 text
->options
.print_trace_field
= false;
543 text
->options
.print_trace_hostname_field
= false;
544 text
->options
.print_trace_domain_field
= false;
545 text
->options
.print_trace_procname_field
= false;
546 text
->options
.print_trace_vpid_field
= false;
547 text
->options
.print_loglevel_field
= false;
548 text
->options
.print_emf_field
= false;
549 text
->options
.print_emf_field
= false;
552 ret
= BT_COMPONENT_STATUS_ERROR
;
558 ret
= apply_one_bool("field-trace", params
, &value
, &found
);
559 if (ret
!= BT_COMPONENT_STATUS_OK
) {
563 text
->options
.print_trace_field
= value
;
568 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
569 if (ret
!= BT_COMPONENT_STATUS_OK
) {
573 text
->options
.print_trace_hostname_field
= value
;
578 ret
= apply_one_bool("field-trace:domain", params
, &value
, &found
);
579 if (ret
!= BT_COMPONENT_STATUS_OK
) {
583 text
->options
.print_trace_domain_field
= value
;
588 ret
= apply_one_bool("field-trace:procname", params
, &value
, &found
);
589 if (ret
!= BT_COMPONENT_STATUS_OK
) {
593 text
->options
.print_trace_procname_field
= value
;
598 ret
= apply_one_bool("field-trace:vpid", params
, &value
, &found
);
599 if (ret
!= BT_COMPONENT_STATUS_OK
) {
603 text
->options
.print_trace_vpid_field
= value
;
608 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
609 if (ret
!= BT_COMPONENT_STATUS_OK
) {
613 text
->options
.print_loglevel_field
= value
;
618 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
619 if (ret
!= BT_COMPONENT_STATUS_OK
) {
623 text
->options
.print_emf_field
= value
;
628 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
629 if (ret
!= BT_COMPONENT_STATUS_OK
) {
633 text
->options
.print_emf_field
= value
;
637 bt_put(text
->plugin_opt_map
);
638 text
->plugin_opt_map
= NULL
;
644 void set_use_colors(struct text_component
*text
)
646 switch (text
->options
.color
) {
647 case TEXT_COLOR_OPT_ALWAYS
:
648 text
->use_colors
= true;
650 case TEXT_COLOR_OPT_AUTO
:
651 text
->use_colors
= text
->out
== stdout
&&
652 bt_common_colors_supported();
654 case TEXT_COLOR_OPT_NEVER
:
655 text
->use_colors
= false;
661 enum bt_component_status
text_component_init(
662 struct bt_component
*component
, struct bt_value
*params
,
663 UNUSED_VAR
void *init_method_data
)
665 enum bt_component_status ret
;
666 struct text_component
*text
= create_text();
669 ret
= BT_COMPONENT_STATUS_NOMEM
;
676 text
->delta_cycles
= -1ULL;
677 text
->last_cycles_timestamp
= -1ULL;
679 text
->delta_real_timestamp
= -1ULL;
680 text
->last_real_timestamp
= -1ULL;
682 ret
= apply_params(text
, params
);
683 if (ret
!= BT_COMPONENT_STATUS_OK
) {
687 set_use_colors(text
);
689 ret
= bt_component_set_private_data(component
, text
);
690 if (ret
!= BT_COMPONENT_STATUS_OK
) {
696 destroy_text_data(text
);
700 /* Initialize plug-in entry points. */
702 BT_PLUGIN_DESCRIPTION("Babeltrace text output plug-in.");
703 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
704 BT_PLUGIN_LICENSE("MIT");
705 BT_PLUGIN_SINK_COMPONENT_CLASS(text
, run
);
706 BT_PLUGIN_SINK_COMPONENT_CLASS_INIT_METHOD(text
, text_component_init
);
707 BT_PLUGIN_SINK_COMPONENT_CLASS_DESTROY_METHOD(text
, destroy_text
);
708 BT_PLUGIN_SINK_COMPONENT_CLASS_DESCRIPTION(text
,
709 "Formats CTF-IR to text. Formerly known as ctf-text.");