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>
44 const char *plugin_options
[] = {
47 "debug-info-target-prefix",
48 "debug-info-full-path",
54 "name-default", /* show/hide */
59 "field-default", /* show/hide */
61 "field-trace:hostname",
63 "field-trace:procname",
70 void destroy_text_data(struct text_component
*text
)
72 (void) g_string_free(text
->string
, TRUE
);
73 g_free(text
->options
.output_path
);
74 g_free(text
->options
.debug_info_dir
);
75 g_free(text
->options
.debug_info_target_prefix
);
80 struct text_component
*create_text(void)
82 struct text_component
*text
;
84 text
= g_new0(struct text_component
, 1);
88 text
->string
= g_string_new("");
101 void destroy_text(struct bt_component
*component
)
103 void *data
= bt_component_get_private_data(component
);
105 destroy_text_data(data
);
109 enum bt_component_status
handle_notification(struct text_component
*text
,
110 struct bt_notification
*notification
)
112 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
115 ret
= BT_COMPONENT_STATUS_ERROR
;
119 switch (bt_notification_get_type(notification
)) {
120 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
123 case BT_NOTIFICATION_TYPE_PACKET_END
:
126 case BT_NOTIFICATION_TYPE_EVENT
:
128 struct bt_ctf_event
*event
= bt_notification_event_get_event(
132 ret
= BT_COMPONENT_STATUS_ERROR
;
135 ret
= text_print_event(text
, event
);
137 if (ret
!= BT_COMPONENT_STATUS_OK
) {
142 case BT_NOTIFICATION_TYPE_STREAM_END
:
146 puts("Unhandled notification type");
153 enum bt_component_status
run(struct bt_component
*component
)
155 enum bt_component_status ret
;
156 struct bt_notification
*notification
= NULL
;
157 struct bt_notification_iterator
*it
;
158 struct text_component
*text
= bt_component_get_private_data(component
);
160 ret
= bt_component_sink_get_input_iterator(component
, 0, &it
);
161 if (ret
!= BT_COMPONENT_STATUS_OK
) {
165 if (likely(text
->processed_first_event
)) {
166 enum bt_notification_iterator_status it_ret
;
168 it_ret
= bt_notification_iterator_next(it
);
170 case BT_NOTIFICATION_ITERATOR_STATUS_ERROR
:
171 ret
= BT_COMPONENT_STATUS_ERROR
;
173 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
174 ret
= BT_COMPONENT_STATUS_END
;
180 notification
= bt_notification_iterator_get_notification(it
);
182 ret
= BT_COMPONENT_STATUS_ERROR
;
186 ret
= handle_notification(text
, notification
);
187 text
->processed_first_event
= true;
190 bt_put(notification
);
195 enum bt_component_status
add_params_to_map(struct bt_value
*plugin_opt_map
)
197 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
200 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
201 const char *key
= plugin_options
[i
];
202 enum bt_value_status status
;
204 status
= bt_value_map_insert(plugin_opt_map
, key
, bt_value_null
);
206 case BT_VALUE_STATUS_OK
:
209 ret
= BT_COMPONENT_STATUS_ERROR
;
218 bool check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
220 struct text_component
*text
= data
;
221 struct bt_value
*plugin_opt_map
= text
->plugin_opt_map
;
223 if (!bt_value_map_get(plugin_opt_map
, key
)) {
225 "[warning] Parameter \"%s\" unknown to \"text\" plugin\n", key
);
231 enum bt_component_status
apply_one_string(const char *key
,
232 struct bt_value
*params
,
235 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
236 struct bt_value
*value
= NULL
;
237 enum bt_value_status status
;
240 value
= bt_value_map_get(params
, key
);
244 if (bt_value_is_null(value
)) {
247 status
= bt_value_string_get(value
, &str
);
249 case BT_VALUE_STATUS_OK
:
252 ret
= BT_COMPONENT_STATUS_ERROR
;
255 *option
= g_strdup(str
);
262 enum bt_component_status
apply_one_bool(const char *key
,
263 struct bt_value
*params
,
267 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
268 struct bt_value
*value
= NULL
;
269 enum bt_value_status status
;
271 value
= bt_value_map_get(params
, key
);
275 status
= bt_value_bool_get(value
, option
);
277 case BT_VALUE_STATUS_OK
:
280 ret
= BT_COMPONENT_STATUS_ERROR
;
292 enum bt_component_status
apply_params(struct text_component
*text
,
293 struct bt_value
*params
)
295 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
296 enum bt_value_status status
;
300 text
->plugin_opt_map
= bt_value_map_create();
301 if (!text
->plugin_opt_map
) {
302 ret
= BT_COMPONENT_STATUS_ERROR
;
305 ret
= add_params_to_map(text
->plugin_opt_map
);
306 if (ret
!= BT_COMPONENT_STATUS_OK
) {
309 /* Report unknown parameters. */
310 status
= bt_value_map_foreach(params
, check_param_exists
, text
);
312 case BT_VALUE_STATUS_OK
:
315 ret
= BT_COMPONENT_STATUS_ERROR
;
318 /* Known parameters. */
319 ret
= apply_one_string("output-path",
321 &text
->options
.output_path
);
322 if (ret
!= BT_COMPONENT_STATUS_OK
) {
326 ret
= apply_one_string("debug-info-dir",
328 &text
->options
.debug_info_dir
);
329 if (ret
!= BT_COMPONENT_STATUS_OK
) {
333 ret
= apply_one_string("debug-info-target-prefix",
335 &text
->options
.debug_info_target_prefix
);
336 if (ret
!= BT_COMPONENT_STATUS_OK
) {
340 value
= false; /* Default. */
341 ret
= apply_one_bool("debug-info-full-path", params
, &value
, NULL
);
342 if (ret
!= BT_COMPONENT_STATUS_OK
) {
345 text
->options
.debug_info_full_path
= value
;
347 value
= false; /* Default. */
348 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
349 if (ret
!= BT_COMPONENT_STATUS_OK
) {
352 text
->options
.print_delta_field
= !value
; /* Reverse logic. */
354 value
= false; /* Default. */
355 ret
= apply_one_bool("clock-cycles", params
, &value
, NULL
);
356 if (ret
!= BT_COMPONENT_STATUS_OK
) {
359 text
->options
.print_timestamp_cycles
= value
;
361 value
= false; /* Default. */
362 ret
= apply_one_bool("clock-seconds", params
, &value
, NULL
);
363 if (ret
!= BT_COMPONENT_STATUS_OK
) {
366 text
->options
.clock_seconds
= value
;
368 value
= false; /* Default. */
369 ret
= apply_one_bool("clock-date", params
, &value
, NULL
);
370 if (ret
!= BT_COMPONENT_STATUS_OK
) {
373 text
->options
.clock_date
= value
;
375 value
= false; /* Default. */
376 ret
= apply_one_bool("clock-gmt", params
, &value
, NULL
);
377 if (ret
!= BT_COMPONENT_STATUS_OK
) {
380 text
->options
.clock_gmt
= value
;
383 ret
= apply_one_string("name-default", params
, &str
);
384 if (ret
!= BT_COMPONENT_STATUS_OK
) {
388 text
->options
.name_default
= TEXT_DEFAULT_UNSET
;
389 } else if (!strcmp(str
, "show")) {
390 text
->options
.name_default
= TEXT_DEFAULT_SHOW
;
391 } else if (!strcmp(str
, "hide")) {
392 text
->options
.name_default
= TEXT_DEFAULT_HIDE
;
394 ret
= BT_COMPONENT_STATUS_ERROR
;
400 switch (text
->options
.name_default
) {
401 case TEXT_DEFAULT_UNSET
:
402 text
->options
.print_payload_field_names
= true;
403 text
->options
.print_context_field_names
= true;
404 text
->options
.print_header_field_names
= false;
405 text
->options
.print_scope_field_names
= false;
407 case TEXT_DEFAULT_SHOW
:
408 text
->options
.print_payload_field_names
= true;
409 text
->options
.print_context_field_names
= true;
410 text
->options
.print_header_field_names
= true;
411 text
->options
.print_scope_field_names
= true;
413 case TEXT_DEFAULT_HIDE
:
414 text
->options
.print_payload_field_names
= false;
415 text
->options
.print_context_field_names
= false;
416 text
->options
.print_header_field_names
= false;
417 text
->options
.print_scope_field_names
= false;
420 ret
= BT_COMPONENT_STATUS_ERROR
;
426 ret
= apply_one_bool("name-payload", params
, &value
, &found
);
427 if (ret
!= BT_COMPONENT_STATUS_OK
) {
431 text
->options
.print_payload_field_names
= value
;
436 ret
= apply_one_bool("name-context", params
, &value
, &found
);
437 if (ret
!= BT_COMPONENT_STATUS_OK
) {
441 text
->options
.print_context_field_names
= value
;
446 ret
= apply_one_bool("name-header", params
, &value
, &found
);
447 if (ret
!= BT_COMPONENT_STATUS_OK
) {
451 text
->options
.print_header_field_names
= value
;
456 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
457 if (ret
!= BT_COMPONENT_STATUS_OK
) {
461 text
->options
.print_scope_field_names
= value
;
465 ret
= apply_one_string("field-default", params
, &str
);
466 if (ret
!= BT_COMPONENT_STATUS_OK
) {
470 text
->options
.field_default
= TEXT_DEFAULT_UNSET
;
471 } else if (!strcmp(str
, "show")) {
472 text
->options
.field_default
= TEXT_DEFAULT_SHOW
;
473 } else if (!strcmp(str
, "hide")) {
474 text
->options
.field_default
= TEXT_DEFAULT_HIDE
;
476 ret
= BT_COMPONENT_STATUS_ERROR
;
482 switch (text
->options
.field_default
) {
483 case TEXT_DEFAULT_UNSET
:
484 text
->options
.print_trace_field
= false;
485 text
->options
.print_trace_hostname_field
= true;
486 text
->options
.print_trace_domain_field
= false;
487 text
->options
.print_trace_procname_field
= true;
488 text
->options
.print_trace_vpid_field
= true;
489 text
->options
.print_loglevel_field
= false;
490 text
->options
.print_emf_field
= false;
491 text
->options
.print_emf_field
= false;
493 case TEXT_DEFAULT_SHOW
:
494 text
->options
.print_trace_field
= true;
495 text
->options
.print_trace_hostname_field
= true;
496 text
->options
.print_trace_domain_field
= true;
497 text
->options
.print_trace_procname_field
= true;
498 text
->options
.print_trace_vpid_field
= true;
499 text
->options
.print_loglevel_field
= true;
500 text
->options
.print_emf_field
= true;
501 text
->options
.print_emf_field
= true;
503 case TEXT_DEFAULT_HIDE
:
504 text
->options
.print_trace_field
= false;
505 text
->options
.print_trace_hostname_field
= false;
506 text
->options
.print_trace_domain_field
= false;
507 text
->options
.print_trace_procname_field
= false;
508 text
->options
.print_trace_vpid_field
= false;
509 text
->options
.print_loglevel_field
= false;
510 text
->options
.print_emf_field
= false;
511 text
->options
.print_emf_field
= false;
514 ret
= BT_COMPONENT_STATUS_ERROR
;
520 ret
= apply_one_bool("field-trace", params
, &value
, &found
);
521 if (ret
!= BT_COMPONENT_STATUS_OK
) {
525 text
->options
.print_trace_field
= value
;
530 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
531 if (ret
!= BT_COMPONENT_STATUS_OK
) {
535 text
->options
.print_trace_hostname_field
= value
;
540 ret
= apply_one_bool("field-trace:domain", params
, &value
, &found
);
541 if (ret
!= BT_COMPONENT_STATUS_OK
) {
545 text
->options
.print_trace_domain_field
= value
;
550 ret
= apply_one_bool("field-trace:procname", params
, &value
, &found
);
551 if (ret
!= BT_COMPONENT_STATUS_OK
) {
555 text
->options
.print_trace_procname_field
= value
;
560 ret
= apply_one_bool("field-trace:vpid", params
, &value
, &found
);
561 if (ret
!= BT_COMPONENT_STATUS_OK
) {
565 text
->options
.print_trace_vpid_field
= value
;
570 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
571 if (ret
!= BT_COMPONENT_STATUS_OK
) {
575 text
->options
.print_loglevel_field
= value
;
580 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
581 if (ret
!= BT_COMPONENT_STATUS_OK
) {
585 text
->options
.print_emf_field
= value
;
590 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
591 if (ret
!= BT_COMPONENT_STATUS_OK
) {
595 text
->options
.print_emf_field
= value
;
599 bt_put(text
->plugin_opt_map
);
600 text
->plugin_opt_map
= NULL
;
606 enum bt_component_status
text_component_init(
607 struct bt_component
*component
, struct bt_value
*params
,
608 void *init_method_data
)
610 enum bt_component_status ret
;
611 struct text_component
*text
= create_text();
612 (void) init_method_data
;
615 ret
= BT_COMPONENT_STATUS_NOMEM
;
622 text
->delta_cycles
= -1ULL;
623 text
->last_cycles_timestamp
= -1ULL;
625 text
->delta_real_timestamp
= -1ULL;
626 text
->last_real_timestamp
= -1ULL;
628 ret
= apply_params(text
, params
);
629 if (ret
!= BT_COMPONENT_STATUS_OK
) {
633 ret
= bt_component_set_private_data(component
, text
);
634 if (ret
!= BT_COMPONENT_STATUS_OK
) {
640 destroy_text_data(text
);
644 /* Initialize plug-in entry points. */
646 BT_PLUGIN_DESCRIPTION("Babeltrace text output plug-in.");
647 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
648 BT_PLUGIN_LICENSE("MIT");
649 BT_PLUGIN_SINK_COMPONENT_CLASS(text
, run
);
650 BT_PLUGIN_SINK_COMPONENT_CLASS_INIT_METHOD(text
, text_component_init
);
651 BT_PLUGIN_SINK_COMPONENT_CLASS_DESTROY_METHOD(text
, destroy_text
);
652 BT_PLUGIN_SINK_COMPONENT_CLASS_DESCRIPTION(text
,
653 "Formats CTF-IR to text. Formerly known as ctf-text.");