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-macros.h>
31 #include <babeltrace/plugin/component.h>
32 #include <babeltrace/plugin/sink.h>
33 #include <babeltrace/plugin/notification/notification.h>
34 #include <babeltrace/plugin/notification/iterator.h>
35 #include <babeltrace/plugin/notification/event.h>
36 #include <babeltrace/values.h>
37 #include <babeltrace/compiler.h>
43 #define PLUGIN_NAME "text"
46 const char *plugin_options
[] = {
49 "debug-info-target-prefix",
50 "debug-info-full-path",
56 "name-default", /* show/hide */
61 "field-default", /* show/hide */
63 "field-trace:hostname",
65 "field-trace:procname",
72 void destroy_text_data(struct text_component
*text
)
74 (void) g_string_free(text
->string
, TRUE
);
75 g_free(text
->options
.output_path
);
76 g_free(text
->options
.debug_info_dir
);
77 g_free(text
->options
.debug_info_target_prefix
);
82 struct text_component
*create_text(void)
84 struct text_component
*text
;
86 text
= g_new0(struct text_component
, 1);
90 text
->string
= g_string_new("");
103 void destroy_text(struct bt_component
*component
)
105 void *data
= bt_component_get_private_data(component
);
107 destroy_text_data(data
);
111 enum bt_component_status
handle_notification(struct text_component
*text
,
112 struct bt_notification
*notification
)
114 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
117 ret
= BT_COMPONENT_STATUS_ERROR
;
121 switch (bt_notification_get_type(notification
)) {
122 case BT_NOTIFICATION_TYPE_PACKET_START
:
125 case BT_NOTIFICATION_TYPE_PACKET_END
:
128 case BT_NOTIFICATION_TYPE_EVENT
:
130 struct bt_ctf_event
*event
= bt_notification_event_get_event(
134 ret
= BT_COMPONENT_STATUS_ERROR
;
137 ret
= text_print_event(text
, event
);
139 if (ret
!= BT_COMPONENT_STATUS_OK
) {
144 case BT_NOTIFICATION_TYPE_STREAM_END
:
148 puts("Unhandled notification type");
155 enum bt_component_status
run(struct bt_component
*component
)
157 enum bt_component_status ret
;
158 struct bt_notification
*notification
= NULL
;
159 struct bt_notification_iterator
*it
;
160 struct text_component
*text
= bt_component_get_private_data(component
);
162 ret
= bt_component_sink_get_input_iterator(component
, 0, &it
);
163 if (ret
!= BT_COMPONENT_STATUS_OK
) {
167 if (!text
->processed_first_event
) {
168 ret
= bt_notification_iterator_next(it
);
169 if (ret
!= BT_COMPONENT_STATUS_OK
) {
173 text
->processed_first_event
= true;
176 notification
= bt_notification_iterator_get_notification(it
);
178 ret
= BT_COMPONENT_STATUS_ERROR
;
182 ret
= handle_notification(text
, notification
);
185 bt_put(notification
);
190 enum bt_component_status
add_params_to_map(struct bt_value
*plugin_opt_map
)
192 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
195 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
196 const char *key
= plugin_options
[i
];
197 enum bt_value_status status
;
199 status
= bt_value_map_insert(plugin_opt_map
, key
, bt_value_null
);
201 case BT_VALUE_STATUS_OK
:
204 ret
= BT_COMPONENT_STATUS_ERROR
;
213 bool check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
215 struct text_component
*text
= data
;
216 struct bt_value
*plugin_opt_map
= text
->plugin_opt_map
;
218 if (!bt_value_map_get(plugin_opt_map
, key
)) {
220 "[warning] Parameter \"%s\" unknown to \"%s\" plugin\n",
227 enum bt_component_status
apply_one_string(const char *key
,
228 struct bt_value
*params
,
231 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
232 struct bt_value
*value
= NULL
;
233 enum bt_value_status status
;
236 value
= bt_value_map_get(params
, key
);
240 if (bt_value_is_null(value
)) {
243 status
= bt_value_string_get(value
, &str
);
245 case BT_VALUE_STATUS_OK
:
248 ret
= BT_COMPONENT_STATUS_ERROR
;
251 *option
= g_strdup(str
);
258 enum bt_component_status
apply_one_bool(const char *key
,
259 struct bt_value
*params
,
263 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
264 struct bt_value
*value
= NULL
;
265 enum bt_value_status status
;
267 value
= bt_value_map_get(params
, key
);
271 status
= bt_value_bool_get(value
, option
);
273 case BT_VALUE_STATUS_OK
:
276 ret
= BT_COMPONENT_STATUS_ERROR
;
288 enum bt_component_status
apply_params(struct text_component
*text
,
289 struct bt_value
*params
)
291 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
292 enum bt_value_status status
;
296 text
->plugin_opt_map
= bt_value_map_create();
297 if (!text
->plugin_opt_map
) {
298 ret
= BT_COMPONENT_STATUS_ERROR
;
301 ret
= add_params_to_map(text
->plugin_opt_map
);
302 if (ret
!= BT_COMPONENT_STATUS_OK
) {
305 /* Report unknown parameters. */
306 status
= bt_value_map_foreach(params
, check_param_exists
, text
);
308 case BT_VALUE_STATUS_OK
:
311 ret
= BT_COMPONENT_STATUS_ERROR
;
314 /* Known parameters. */
315 ret
= apply_one_string("output-path",
317 &text
->options
.output_path
);
318 if (ret
!= BT_COMPONENT_STATUS_OK
) {
322 ret
= apply_one_string("debug-info-dir",
324 &text
->options
.debug_info_dir
);
325 if (ret
!= BT_COMPONENT_STATUS_OK
) {
329 ret
= apply_one_string("debug-info-target-prefix",
331 &text
->options
.debug_info_target_prefix
);
332 if (ret
!= BT_COMPONENT_STATUS_OK
) {
336 value
= false; /* Default. */
337 ret
= apply_one_bool("debug-info-full-path", params
, &value
, NULL
);
338 if (ret
!= BT_COMPONENT_STATUS_OK
) {
341 text
->options
.debug_info_full_path
= value
;
343 value
= false; /* Default. */
344 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
345 if (ret
!= BT_COMPONENT_STATUS_OK
) {
348 text
->options
.print_delta_field
= !value
; /* Reverse logic. */
350 value
= false; /* Default. */
351 ret
= apply_one_bool("clock-cycles", params
, &value
, NULL
);
352 if (ret
!= BT_COMPONENT_STATUS_OK
) {
355 text
->options
.print_timestamp_cycles
= value
;
357 value
= false; /* Default. */
358 ret
= apply_one_bool("clock-seconds", params
, &value
, NULL
);
359 if (ret
!= BT_COMPONENT_STATUS_OK
) {
362 text
->options
.clock_seconds
= value
;
364 value
= false; /* Default. */
365 ret
= apply_one_bool("clock-date", params
, &value
, NULL
);
366 if (ret
!= BT_COMPONENT_STATUS_OK
) {
369 text
->options
.clock_date
= value
;
371 value
= false; /* Default. */
372 ret
= apply_one_bool("clock-gmt", params
, &value
, NULL
);
373 if (ret
!= BT_COMPONENT_STATUS_OK
) {
376 text
->options
.clock_gmt
= value
;
379 ret
= apply_one_string("name-default", params
, &str
);
380 if (ret
!= BT_COMPONENT_STATUS_OK
) {
384 text
->options
.name_default
= TEXT_DEFAULT_UNSET
;
385 } else if (!strcmp(str
, "show")) {
386 text
->options
.name_default
= TEXT_DEFAULT_SHOW
;
387 } else if (!strcmp(str
, "hide")) {
388 text
->options
.name_default
= TEXT_DEFAULT_HIDE
;
390 ret
= BT_COMPONENT_STATUS_ERROR
;
396 switch (text
->options
.name_default
) {
397 case TEXT_DEFAULT_UNSET
:
398 text
->options
.print_payload_field_names
= true;
399 text
->options
.print_context_field_names
= true;
400 text
->options
.print_header_field_names
= false;
401 text
->options
.print_scope_field_names
= false;
403 case TEXT_DEFAULT_SHOW
:
404 text
->options
.print_payload_field_names
= true;
405 text
->options
.print_context_field_names
= true;
406 text
->options
.print_header_field_names
= true;
407 text
->options
.print_scope_field_names
= true;
409 case TEXT_DEFAULT_HIDE
:
410 text
->options
.print_payload_field_names
= false;
411 text
->options
.print_context_field_names
= false;
412 text
->options
.print_header_field_names
= false;
413 text
->options
.print_scope_field_names
= false;
416 ret
= BT_COMPONENT_STATUS_ERROR
;
422 ret
= apply_one_bool("name-payload", params
, &value
, &found
);
423 if (ret
!= BT_COMPONENT_STATUS_OK
) {
427 text
->options
.print_payload_field_names
= value
;
432 ret
= apply_one_bool("name-context", params
, &value
, &found
);
433 if (ret
!= BT_COMPONENT_STATUS_OK
) {
437 text
->options
.print_context_field_names
= value
;
442 ret
= apply_one_bool("name-header", params
, &value
, &found
);
443 if (ret
!= BT_COMPONENT_STATUS_OK
) {
447 text
->options
.print_header_field_names
= value
;
452 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
453 if (ret
!= BT_COMPONENT_STATUS_OK
) {
457 text
->options
.print_scope_field_names
= value
;
461 ret
= apply_one_string("field-default", params
, &str
);
462 if (ret
!= BT_COMPONENT_STATUS_OK
) {
466 text
->options
.field_default
= TEXT_DEFAULT_UNSET
;
467 } else if (!strcmp(str
, "show")) {
468 text
->options
.field_default
= TEXT_DEFAULT_SHOW
;
469 } else if (!strcmp(str
, "hide")) {
470 text
->options
.field_default
= TEXT_DEFAULT_HIDE
;
472 ret
= BT_COMPONENT_STATUS_ERROR
;
478 switch (text
->options
.field_default
) {
479 case TEXT_DEFAULT_UNSET
:
480 text
->options
.print_trace_field
= false;
481 text
->options
.print_trace_hostname_field
= true;
482 text
->options
.print_trace_domain_field
= false;
483 text
->options
.print_trace_procname_field
= true;
484 text
->options
.print_trace_vpid_field
= true;
485 text
->options
.print_loglevel_field
= false;
486 text
->options
.print_emf_field
= false;
487 text
->options
.print_emf_field
= false;
489 case TEXT_DEFAULT_SHOW
:
490 text
->options
.print_trace_field
= true;
491 text
->options
.print_trace_hostname_field
= true;
492 text
->options
.print_trace_domain_field
= true;
493 text
->options
.print_trace_procname_field
= true;
494 text
->options
.print_trace_vpid_field
= true;
495 text
->options
.print_loglevel_field
= true;
496 text
->options
.print_emf_field
= true;
497 text
->options
.print_emf_field
= true;
499 case TEXT_DEFAULT_HIDE
:
500 text
->options
.print_trace_field
= false;
501 text
->options
.print_trace_hostname_field
= false;
502 text
->options
.print_trace_domain_field
= false;
503 text
->options
.print_trace_procname_field
= false;
504 text
->options
.print_trace_vpid_field
= false;
505 text
->options
.print_loglevel_field
= false;
506 text
->options
.print_emf_field
= false;
507 text
->options
.print_emf_field
= false;
510 ret
= BT_COMPONENT_STATUS_ERROR
;
516 ret
= apply_one_bool("field-trace", params
, &value
, &found
);
517 if (ret
!= BT_COMPONENT_STATUS_OK
) {
521 text
->options
.print_trace_field
= value
;
526 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
527 if (ret
!= BT_COMPONENT_STATUS_OK
) {
531 text
->options
.print_trace_hostname_field
= value
;
536 ret
= apply_one_bool("field-trace:domain", params
, &value
, &found
);
537 if (ret
!= BT_COMPONENT_STATUS_OK
) {
541 text
->options
.print_trace_domain_field
= value
;
546 ret
= apply_one_bool("field-trace:procname", params
, &value
, &found
);
547 if (ret
!= BT_COMPONENT_STATUS_OK
) {
551 text
->options
.print_trace_procname_field
= value
;
556 ret
= apply_one_bool("field-trace:vpid", params
, &value
, &found
);
557 if (ret
!= BT_COMPONENT_STATUS_OK
) {
561 text
->options
.print_trace_vpid_field
= value
;
566 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
567 if (ret
!= BT_COMPONENT_STATUS_OK
) {
571 text
->options
.print_loglevel_field
= value
;
576 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
577 if (ret
!= BT_COMPONENT_STATUS_OK
) {
581 text
->options
.print_emf_field
= value
;
586 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
587 if (ret
!= BT_COMPONENT_STATUS_OK
) {
591 text
->options
.print_emf_field
= value
;
595 bt_put(text
->plugin_opt_map
);
596 text
->plugin_opt_map
= NULL
;
602 enum bt_component_status
text_component_init(
603 struct bt_component
*component
, struct bt_value
*params
)
605 enum bt_component_status ret
;
606 struct text_component
*text
= create_text();
609 ret
= BT_COMPONENT_STATUS_NOMEM
;
616 text
->delta_cycles
= -1ULL;
617 text
->last_cycles_timestamp
= -1ULL;
619 text
->delta_real_timestamp
= -1ULL;
620 text
->last_real_timestamp
= -1ULL;
622 ret
= apply_params(text
, params
);
623 if (ret
!= BT_COMPONENT_STATUS_OK
) {
627 ret
= bt_component_set_destroy_cb(component
,
629 if (ret
!= BT_COMPONENT_STATUS_OK
) {
633 ret
= bt_component_set_private_data(component
, text
);
634 if (ret
!= BT_COMPONENT_STATUS_OK
) {
638 ret
= bt_component_sink_set_consume_cb(component
,
640 if (ret
!= BT_COMPONENT_STATUS_OK
) {
646 destroy_text_data(text
);
650 /* Initialize plug-in entry points. */
651 BT_PLUGIN_NAME("text");
652 BT_PLUGIN_DESCRIPTION("Babeltrace text output plug-in.");
653 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
654 BT_PLUGIN_LICENSE("MIT");
656 BT_PLUGIN_COMPONENT_CLASSES_BEGIN
657 BT_PLUGIN_SINK_COMPONENT_CLASS_ENTRY(PLUGIN_NAME
,
658 "Formats CTF-IR to text. Formerly known as ctf-text.",
660 BT_PLUGIN_COMPONENT_CLASSES_END