4 * Babeltrace CTF Text Output Plugin
6 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <babeltrace/plugin/plugin-macros.h>
30 #include <babeltrace/plugin/component.h>
31 #include <babeltrace/plugin/sink.h>
32 #include <babeltrace/plugin/notification/notification.h>
33 #include <babeltrace/plugin/notification/iterator.h>
34 #include <babeltrace/plugin/notification/event.h>
35 #include <babeltrace/values.h>
36 #include <babeltrace/compiler.h>
42 #define PLUGIN_NAME "text"
45 const char *plugin_options
[] = {
48 "debug-info-target-prefix",
49 "debug-info-full-path",
55 "name-default", /* show/hide */
60 "field-default", /* show/hide */
62 "field-trace:hostname",
64 "field-trace:procname",
72 const char *loglevel_str
[] = {
73 [LOGLEVEL_EMERG
] = "TRACE_EMERG",
74 [LOGLEVEL_ALERT
] = "TRACE_ALERT",
75 [LOGLEVEL_CRIT
] = "TRACE_CRIT",
76 [LOGLEVEL_ERR
] = "TRACE_ERR",
77 [LOGLEVEL_WARNING
] = "TRACE_WARNING",
78 [LOGLEVEL_NOTICE
] = "TRACE_NOTICE",
79 [LOGLEVEL_INFO
] = "TRACE_INFO",
80 [LOGLEVEL_DEBUG_SYSTEM
] = "TRACE_DEBUG_SYSTEM",
81 [LOGLEVEL_DEBUG_PROGRAM
] = "TRACE_DEBUG_PROGRAM",
82 [LOGLEVEL_DEBUG_PROCESS
] = "TRACE_DEBUG_PROCESS",
83 [LOGLEVEL_DEBUG_MODULE
] = "TRACE_DEBUG_MODULE",
84 [LOGLEVEL_DEBUG_UNIT
] = "TRACE_DEBUG_UNIT",
85 [LOGLEVEL_DEBUG_FUNCTION
] = "TRACE_DEBUG_FUNCTION",
86 [LOGLEVEL_DEBUG_LINE
] = "TRACE_DEBUG_LINE",
87 [LOGLEVEL_DEBUG
] = "TRACE_DEBUG",
91 void destroy_text_data(struct text_component
*text
)
93 (void) g_string_free(text
->string
, TRUE
);
94 g_free(text
->options
.output_path
);
95 g_free(text
->options
.debug_info_dir
);
96 g_free(text
->options
.debug_info_target_prefix
);
101 struct text_component
*create_text(void)
103 struct text_component
*text
;
105 text
= g_new0(struct text_component
, 1);
109 text
->string
= g_string_new("");
122 void destroy_text(struct bt_component
*component
)
124 void *data
= bt_component_get_private_data(component
);
126 destroy_text_data(data
);
130 enum bt_component_status
handle_notification(struct text_component
*text
,
131 struct bt_notification
*notification
)
133 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
136 ret
= BT_COMPONENT_STATUS_ERROR
;
140 switch (bt_notification_get_type(notification
)) {
141 case BT_NOTIFICATION_TYPE_PACKET_START
:
144 case BT_NOTIFICATION_TYPE_PACKET_END
:
147 case BT_NOTIFICATION_TYPE_EVENT
:
149 struct bt_ctf_event
*event
= bt_notification_event_get_event(
153 ret
= BT_COMPONENT_STATUS_ERROR
;
156 ret
= text_print_event(text
, event
);
158 if (ret
!= BT_COMPONENT_STATUS_OK
) {
163 case BT_NOTIFICATION_TYPE_STREAM_END
:
167 puts("Unhandled notification type");
174 enum bt_component_status
run(struct bt_component
*component
)
176 enum bt_component_status ret
;
177 struct bt_notification
*notification
= NULL
;
178 struct bt_notification_iterator
*it
;
179 struct text_component
*text
= bt_component_get_private_data(component
);
181 ret
= bt_component_sink_get_input_iterator(component
, 0, &it
);
182 if (ret
!= BT_COMPONENT_STATUS_OK
) {
186 if (!text
->processed_first_event
) {
187 ret
= bt_notification_iterator_next(it
);
188 if (ret
!= BT_COMPONENT_STATUS_OK
) {
192 text
->processed_first_event
= true;
195 notification
= bt_notification_iterator_get_notification(it
);
197 ret
= BT_COMPONENT_STATUS_ERROR
;
201 ret
= handle_notification(text
, notification
);
204 bt_put(notification
);
209 enum bt_component_status
add_params_to_map(struct bt_value
*plugin_opt_map
)
211 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
214 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
215 const char *key
= plugin_options
[i
];
216 enum bt_value_status status
;
218 status
= bt_value_map_insert(plugin_opt_map
, key
, bt_value_null
);
220 case BT_VALUE_STATUS_OK
:
223 ret
= BT_COMPONENT_STATUS_ERROR
;
232 bool check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
234 struct text_component
*text
= data
;
235 struct bt_value
*plugin_opt_map
= text
->plugin_opt_map
;
237 if (!bt_value_map_get(plugin_opt_map
, key
)) {
239 "[warning] Parameter \"%s\" unknown to \"%s\" plugin\n",
246 enum bt_component_status
apply_one_string(const char *key
,
247 struct bt_value
*params
,
250 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
251 struct bt_value
*value
= NULL
;
252 enum bt_value_status status
;
255 value
= bt_value_map_get(params
, key
);
259 if (bt_value_is_null(value
)) {
262 status
= bt_value_string_get(value
, &str
);
264 case BT_VALUE_STATUS_OK
:
267 ret
= BT_COMPONENT_STATUS_ERROR
;
270 *option
= g_strdup(str
);
277 enum bt_component_status
apply_one_bool(const char *key
,
278 struct bt_value
*params
,
282 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
283 struct bt_value
*value
= NULL
;
284 enum bt_value_status status
;
286 value
= bt_value_map_get(params
, key
);
290 status
= bt_value_bool_get(value
, option
);
292 case BT_VALUE_STATUS_OK
:
295 ret
= BT_COMPONENT_STATUS_ERROR
;
307 enum bt_component_status
apply_params(struct text_component
*text
,
308 struct bt_value
*params
)
310 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
311 enum bt_value_status status
;
315 text
->plugin_opt_map
= bt_value_map_create();
316 if (!text
->plugin_opt_map
) {
317 ret
= BT_COMPONENT_STATUS_ERROR
;
320 ret
= add_params_to_map(text
->plugin_opt_map
);
321 if (ret
!= BT_COMPONENT_STATUS_OK
) {
324 /* Report unknown parameters. */
325 status
= bt_value_map_foreach(params
, check_param_exists
, text
);
327 case BT_VALUE_STATUS_OK
:
330 ret
= BT_COMPONENT_STATUS_ERROR
;
333 /* Known parameters. */
334 ret
= apply_one_string("output-path",
336 &text
->options
.output_path
);
337 if (ret
!= BT_COMPONENT_STATUS_OK
) {
341 ret
= apply_one_string("debug-info-dir",
343 &text
->options
.debug_info_dir
);
344 if (ret
!= BT_COMPONENT_STATUS_OK
) {
348 ret
= apply_one_string("debug-info-target-prefix",
350 &text
->options
.debug_info_target_prefix
);
351 if (ret
!= BT_COMPONENT_STATUS_OK
) {
355 value
= false; /* Default. */
356 ret
= apply_one_bool("debug-info-full-path", params
, &value
, NULL
);
357 if (ret
!= BT_COMPONENT_STATUS_OK
) {
360 text
->options
.debug_info_full_path
= value
;
362 value
= false; /* Default. */
363 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
364 if (ret
!= BT_COMPONENT_STATUS_OK
) {
367 text
->options
.print_delta_field
= !value
; /* Reverse logic. */
369 value
= false; /* Default. */
370 ret
= apply_one_bool("clock-cycles", params
, &value
, NULL
);
371 if (ret
!= BT_COMPONENT_STATUS_OK
) {
374 text
->options
.print_timestamp_cycles
= value
;
376 value
= false; /* Default. */
377 ret
= apply_one_bool("clock-seconds", params
, &value
, NULL
);
378 if (ret
!= BT_COMPONENT_STATUS_OK
) {
381 text
->options
.clock_seconds
= value
;
383 value
= false; /* Default. */
384 ret
= apply_one_bool("clock-date", params
, &value
, NULL
);
385 if (ret
!= BT_COMPONENT_STATUS_OK
) {
388 text
->options
.clock_date
= value
;
390 value
= false; /* Default. */
391 ret
= apply_one_bool("clock-gmt", params
, &value
, NULL
);
392 if (ret
!= BT_COMPONENT_STATUS_OK
) {
395 text
->options
.clock_gmt
= value
;
398 ret
= apply_one_string("name-default", params
, &str
);
399 if (ret
!= BT_COMPONENT_STATUS_OK
) {
403 text
->options
.name_default
= TEXT_DEFAULT_UNSET
;
404 } else if (!strcmp(str
, "show")) {
405 text
->options
.name_default
= TEXT_DEFAULT_SHOW
;
406 } else if (!strcmp(str
, "hide")) {
407 text
->options
.name_default
= TEXT_DEFAULT_HIDE
;
409 ret
= BT_COMPONENT_STATUS_ERROR
;
415 switch (text
->options
.name_default
) {
416 case TEXT_DEFAULT_UNSET
:
417 text
->options
.print_payload_field_names
= true;
418 text
->options
.print_context_field_names
= true;
419 text
->options
.print_header_field_names
= false;
420 text
->options
.print_scope_field_names
= false;
422 case TEXT_DEFAULT_SHOW
:
423 text
->options
.print_payload_field_names
= true;
424 text
->options
.print_context_field_names
= true;
425 text
->options
.print_header_field_names
= true;
426 text
->options
.print_scope_field_names
= true;
428 case TEXT_DEFAULT_HIDE
:
429 text
->options
.print_payload_field_names
= false;
430 text
->options
.print_context_field_names
= false;
431 text
->options
.print_header_field_names
= false;
432 text
->options
.print_scope_field_names
= false;
435 ret
= BT_COMPONENT_STATUS_ERROR
;
441 ret
= apply_one_bool("name-payload", params
, &value
, &found
);
442 if (ret
!= BT_COMPONENT_STATUS_OK
) {
446 text
->options
.print_payload_field_names
= value
;
451 ret
= apply_one_bool("name-context", params
, &value
, &found
);
452 if (ret
!= BT_COMPONENT_STATUS_OK
) {
456 text
->options
.print_context_field_names
= value
;
461 ret
= apply_one_bool("name-header", params
, &value
, &found
);
462 if (ret
!= BT_COMPONENT_STATUS_OK
) {
466 text
->options
.print_header_field_names
= value
;
471 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
472 if (ret
!= BT_COMPONENT_STATUS_OK
) {
476 text
->options
.print_scope_field_names
= value
;
480 ret
= apply_one_string("field-default", params
, &str
);
481 if (ret
!= BT_COMPONENT_STATUS_OK
) {
485 text
->options
.field_default
= TEXT_DEFAULT_UNSET
;
486 } else if (!strcmp(str
, "show")) {
487 text
->options
.field_default
= TEXT_DEFAULT_SHOW
;
488 } else if (!strcmp(str
, "hide")) {
489 text
->options
.field_default
= TEXT_DEFAULT_HIDE
;
491 ret
= BT_COMPONENT_STATUS_ERROR
;
497 switch (text
->options
.field_default
) {
498 case TEXT_DEFAULT_UNSET
:
499 text
->options
.print_trace_field
= false;
500 text
->options
.print_trace_hostname_field
= true;
501 text
->options
.print_trace_domain_field
= false;
502 text
->options
.print_trace_procname_field
= true;
503 text
->options
.print_trace_vpid_field
= true;
504 text
->options
.print_loglevel_field
= false;
505 text
->options
.print_emf_field
= false;
506 text
->options
.print_emf_field
= false;
507 text
->options
.print_callsite_field
= false;
509 case TEXT_DEFAULT_SHOW
:
510 text
->options
.print_trace_field
= true;
511 text
->options
.print_trace_hostname_field
= true;
512 text
->options
.print_trace_domain_field
= true;
513 text
->options
.print_trace_procname_field
= true;
514 text
->options
.print_trace_vpid_field
= true;
515 text
->options
.print_loglevel_field
= true;
516 text
->options
.print_emf_field
= true;
517 text
->options
.print_emf_field
= true;
518 text
->options
.print_callsite_field
= true;
520 case TEXT_DEFAULT_HIDE
:
521 text
->options
.print_trace_field
= false;
522 text
->options
.print_trace_hostname_field
= false;
523 text
->options
.print_trace_domain_field
= false;
524 text
->options
.print_trace_procname_field
= false;
525 text
->options
.print_trace_vpid_field
= false;
526 text
->options
.print_loglevel_field
= false;
527 text
->options
.print_emf_field
= false;
528 text
->options
.print_emf_field
= false;
529 text
->options
.print_callsite_field
= false;
532 ret
= BT_COMPONENT_STATUS_ERROR
;
538 ret
= apply_one_bool("field-trace", params
, &value
, &found
);
539 if (ret
!= BT_COMPONENT_STATUS_OK
) {
543 text
->options
.print_trace_field
= value
;
548 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
549 if (ret
!= BT_COMPONENT_STATUS_OK
) {
553 text
->options
.print_trace_hostname_field
= value
;
558 ret
= apply_one_bool("field-trace:domain", params
, &value
, &found
);
559 if (ret
!= BT_COMPONENT_STATUS_OK
) {
563 text
->options
.print_trace_domain_field
= value
;
568 ret
= apply_one_bool("field-trace:procname", params
, &value
, &found
);
569 if (ret
!= BT_COMPONENT_STATUS_OK
) {
573 text
->options
.print_trace_procname_field
= value
;
578 ret
= apply_one_bool("field-trace:vpid", params
, &value
, &found
);
579 if (ret
!= BT_COMPONENT_STATUS_OK
) {
583 text
->options
.print_trace_vpid_field
= value
;
588 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
589 if (ret
!= BT_COMPONENT_STATUS_OK
) {
593 text
->options
.print_loglevel_field
= value
;
598 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
599 if (ret
!= BT_COMPONENT_STATUS_OK
) {
603 text
->options
.print_emf_field
= value
;
608 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
609 if (ret
!= BT_COMPONENT_STATUS_OK
) {
613 text
->options
.print_emf_field
= value
;
618 ret
= apply_one_bool("field-callsite", params
, &value
, &found
);
619 if (ret
!= BT_COMPONENT_STATUS_OK
) {
623 text
->options
.print_callsite_field
= value
;
626 bt_put(text
->plugin_opt_map
);
627 text
->plugin_opt_map
= NULL
;
633 enum bt_component_status
text_component_init(
634 struct bt_component
*component
, struct bt_value
*params
)
636 enum bt_component_status ret
;
637 struct text_component
*text
= create_text();
640 ret
= BT_COMPONENT_STATUS_NOMEM
;
647 ret
= apply_params(text
, params
);
648 if (ret
!= BT_COMPONENT_STATUS_OK
) {
652 ret
= bt_component_set_destroy_cb(component
,
654 if (ret
!= BT_COMPONENT_STATUS_OK
) {
658 ret
= bt_component_set_private_data(component
, text
);
659 if (ret
!= BT_COMPONENT_STATUS_OK
) {
663 ret
= bt_component_sink_set_consume_cb(component
,
665 if (ret
!= BT_COMPONENT_STATUS_OK
) {
671 destroy_text_data(text
);
675 /* Initialize plug-in entry points. */
676 BT_PLUGIN_NAME("text");
677 BT_PLUGIN_DESCRIPTION("Babeltrace text output plug-in.");
678 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
679 BT_PLUGIN_LICENSE("MIT");
681 BT_PLUGIN_COMPONENT_CLASSES_BEGIN
682 BT_PLUGIN_SINK_COMPONENT_CLASS_ENTRY(PLUGIN_NAME
,
683 "Formats CTF-IR to text. Formerly known as ctf-text.",
685 BT_PLUGIN_COMPONENT_CLASSES_END