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_BEGIN
:
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 (likely(text
->processed_first_event
)) {
168 enum bt_notification_iterator_status it_ret
;
170 it_ret
= bt_notification_iterator_next(it
);
172 case BT_NOTIFICATION_ITERATOR_STATUS_ERROR
:
173 ret
= BT_COMPONENT_STATUS_ERROR
;
175 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
176 ret
= BT_COMPONENT_STATUS_END
;
182 notification
= bt_notification_iterator_get_notification(it
);
184 ret
= BT_COMPONENT_STATUS_ERROR
;
188 ret
= handle_notification(text
, notification
);
189 text
->processed_first_event
= true;
192 bt_put(notification
);
197 enum bt_component_status
add_params_to_map(struct bt_value
*plugin_opt_map
)
199 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
202 for (i
= 0; i
< BT_ARRAY_SIZE(plugin_options
); i
++) {
203 const char *key
= plugin_options
[i
];
204 enum bt_value_status status
;
206 status
= bt_value_map_insert(plugin_opt_map
, key
, bt_value_null
);
208 case BT_VALUE_STATUS_OK
:
211 ret
= BT_COMPONENT_STATUS_ERROR
;
220 bool check_param_exists(const char *key
, struct bt_value
*object
, void *data
)
222 struct text_component
*text
= data
;
223 struct bt_value
*plugin_opt_map
= text
->plugin_opt_map
;
225 if (!bt_value_map_get(plugin_opt_map
, key
)) {
227 "[warning] Parameter \"%s\" unknown to \"%s\" plugin\n",
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 enum bt_component_status
apply_params(struct text_component
*text
,
296 struct bt_value
*params
)
298 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
299 enum bt_value_status status
;
303 text
->plugin_opt_map
= bt_value_map_create();
304 if (!text
->plugin_opt_map
) {
305 ret
= BT_COMPONENT_STATUS_ERROR
;
308 ret
= add_params_to_map(text
->plugin_opt_map
);
309 if (ret
!= BT_COMPONENT_STATUS_OK
) {
312 /* Report unknown parameters. */
313 status
= bt_value_map_foreach(params
, check_param_exists
, text
);
315 case BT_VALUE_STATUS_OK
:
318 ret
= BT_COMPONENT_STATUS_ERROR
;
321 /* Known parameters. */
322 ret
= apply_one_string("output-path",
324 &text
->options
.output_path
);
325 if (ret
!= BT_COMPONENT_STATUS_OK
) {
329 ret
= apply_one_string("debug-info-dir",
331 &text
->options
.debug_info_dir
);
332 if (ret
!= BT_COMPONENT_STATUS_OK
) {
336 ret
= apply_one_string("debug-info-target-prefix",
338 &text
->options
.debug_info_target_prefix
);
339 if (ret
!= BT_COMPONENT_STATUS_OK
) {
343 value
= false; /* Default. */
344 ret
= apply_one_bool("debug-info-full-path", params
, &value
, NULL
);
345 if (ret
!= BT_COMPONENT_STATUS_OK
) {
348 text
->options
.debug_info_full_path
= value
;
350 value
= false; /* Default. */
351 ret
= apply_one_bool("no-delta", params
, &value
, NULL
);
352 if (ret
!= BT_COMPONENT_STATUS_OK
) {
355 text
->options
.print_delta_field
= !value
; /* Reverse logic. */
357 value
= false; /* Default. */
358 ret
= apply_one_bool("clock-cycles", params
, &value
, NULL
);
359 if (ret
!= BT_COMPONENT_STATUS_OK
) {
362 text
->options
.print_timestamp_cycles
= value
;
364 value
= false; /* Default. */
365 ret
= apply_one_bool("clock-seconds", params
, &value
, NULL
);
366 if (ret
!= BT_COMPONENT_STATUS_OK
) {
369 text
->options
.clock_seconds
= value
;
371 value
= false; /* Default. */
372 ret
= apply_one_bool("clock-date", params
, &value
, NULL
);
373 if (ret
!= BT_COMPONENT_STATUS_OK
) {
376 text
->options
.clock_date
= value
;
378 value
= false; /* Default. */
379 ret
= apply_one_bool("clock-gmt", params
, &value
, NULL
);
380 if (ret
!= BT_COMPONENT_STATUS_OK
) {
383 text
->options
.clock_gmt
= value
;
386 ret
= apply_one_string("name-default", params
, &str
);
387 if (ret
!= BT_COMPONENT_STATUS_OK
) {
391 text
->options
.name_default
= TEXT_DEFAULT_UNSET
;
392 } else if (!strcmp(str
, "show")) {
393 text
->options
.name_default
= TEXT_DEFAULT_SHOW
;
394 } else if (!strcmp(str
, "hide")) {
395 text
->options
.name_default
= TEXT_DEFAULT_HIDE
;
397 ret
= BT_COMPONENT_STATUS_ERROR
;
403 switch (text
->options
.name_default
) {
404 case TEXT_DEFAULT_UNSET
:
405 text
->options
.print_payload_field_names
= true;
406 text
->options
.print_context_field_names
= true;
407 text
->options
.print_header_field_names
= false;
408 text
->options
.print_scope_field_names
= false;
410 case TEXT_DEFAULT_SHOW
:
411 text
->options
.print_payload_field_names
= true;
412 text
->options
.print_context_field_names
= true;
413 text
->options
.print_header_field_names
= true;
414 text
->options
.print_scope_field_names
= true;
416 case TEXT_DEFAULT_HIDE
:
417 text
->options
.print_payload_field_names
= false;
418 text
->options
.print_context_field_names
= false;
419 text
->options
.print_header_field_names
= false;
420 text
->options
.print_scope_field_names
= false;
423 ret
= BT_COMPONENT_STATUS_ERROR
;
429 ret
= apply_one_bool("name-payload", params
, &value
, &found
);
430 if (ret
!= BT_COMPONENT_STATUS_OK
) {
434 text
->options
.print_payload_field_names
= value
;
439 ret
= apply_one_bool("name-context", params
, &value
, &found
);
440 if (ret
!= BT_COMPONENT_STATUS_OK
) {
444 text
->options
.print_context_field_names
= value
;
449 ret
= apply_one_bool("name-header", params
, &value
, &found
);
450 if (ret
!= BT_COMPONENT_STATUS_OK
) {
454 text
->options
.print_header_field_names
= value
;
459 ret
= apply_one_bool("name-scope", params
, &value
, &found
);
460 if (ret
!= BT_COMPONENT_STATUS_OK
) {
464 text
->options
.print_scope_field_names
= value
;
468 ret
= apply_one_string("field-default", params
, &str
);
469 if (ret
!= BT_COMPONENT_STATUS_OK
) {
473 text
->options
.field_default
= TEXT_DEFAULT_UNSET
;
474 } else if (!strcmp(str
, "show")) {
475 text
->options
.field_default
= TEXT_DEFAULT_SHOW
;
476 } else if (!strcmp(str
, "hide")) {
477 text
->options
.field_default
= TEXT_DEFAULT_HIDE
;
479 ret
= BT_COMPONENT_STATUS_ERROR
;
485 switch (text
->options
.field_default
) {
486 case TEXT_DEFAULT_UNSET
:
487 text
->options
.print_trace_field
= false;
488 text
->options
.print_trace_hostname_field
= true;
489 text
->options
.print_trace_domain_field
= false;
490 text
->options
.print_trace_procname_field
= true;
491 text
->options
.print_trace_vpid_field
= true;
492 text
->options
.print_loglevel_field
= false;
493 text
->options
.print_emf_field
= false;
494 text
->options
.print_emf_field
= false;
496 case TEXT_DEFAULT_SHOW
:
497 text
->options
.print_trace_field
= true;
498 text
->options
.print_trace_hostname_field
= true;
499 text
->options
.print_trace_domain_field
= true;
500 text
->options
.print_trace_procname_field
= true;
501 text
->options
.print_trace_vpid_field
= true;
502 text
->options
.print_loglevel_field
= true;
503 text
->options
.print_emf_field
= true;
504 text
->options
.print_emf_field
= true;
506 case TEXT_DEFAULT_HIDE
:
507 text
->options
.print_trace_field
= false;
508 text
->options
.print_trace_hostname_field
= false;
509 text
->options
.print_trace_domain_field
= false;
510 text
->options
.print_trace_procname_field
= false;
511 text
->options
.print_trace_vpid_field
= false;
512 text
->options
.print_loglevel_field
= false;
513 text
->options
.print_emf_field
= false;
514 text
->options
.print_emf_field
= false;
517 ret
= BT_COMPONENT_STATUS_ERROR
;
523 ret
= apply_one_bool("field-trace", params
, &value
, &found
);
524 if (ret
!= BT_COMPONENT_STATUS_OK
) {
528 text
->options
.print_trace_field
= value
;
533 ret
= apply_one_bool("field-trace:hostname", params
, &value
, &found
);
534 if (ret
!= BT_COMPONENT_STATUS_OK
) {
538 text
->options
.print_trace_hostname_field
= value
;
543 ret
= apply_one_bool("field-trace:domain", params
, &value
, &found
);
544 if (ret
!= BT_COMPONENT_STATUS_OK
) {
548 text
->options
.print_trace_domain_field
= value
;
553 ret
= apply_one_bool("field-trace:procname", params
, &value
, &found
);
554 if (ret
!= BT_COMPONENT_STATUS_OK
) {
558 text
->options
.print_trace_procname_field
= value
;
563 ret
= apply_one_bool("field-trace:vpid", params
, &value
, &found
);
564 if (ret
!= BT_COMPONENT_STATUS_OK
) {
568 text
->options
.print_trace_vpid_field
= value
;
573 ret
= apply_one_bool("field-loglevel", params
, &value
, &found
);
574 if (ret
!= BT_COMPONENT_STATUS_OK
) {
578 text
->options
.print_loglevel_field
= value
;
583 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
584 if (ret
!= BT_COMPONENT_STATUS_OK
) {
588 text
->options
.print_emf_field
= value
;
593 ret
= apply_one_bool("field-emf", params
, &value
, &found
);
594 if (ret
!= BT_COMPONENT_STATUS_OK
) {
598 text
->options
.print_emf_field
= value
;
602 bt_put(text
->plugin_opt_map
);
603 text
->plugin_opt_map
= NULL
;
609 enum bt_component_status
text_component_init(
610 struct bt_component
*component
, struct bt_value
*params
)
612 enum bt_component_status ret
;
613 struct text_component
*text
= create_text();
616 ret
= BT_COMPONENT_STATUS_NOMEM
;
623 text
->delta_cycles
= -1ULL;
624 text
->last_cycles_timestamp
= -1ULL;
626 text
->delta_real_timestamp
= -1ULL;
627 text
->last_real_timestamp
= -1ULL;
629 ret
= apply_params(text
, params
);
630 if (ret
!= BT_COMPONENT_STATUS_OK
) {
634 ret
= bt_component_set_destroy_cb(component
,
636 if (ret
!= BT_COMPONENT_STATUS_OK
) {
640 ret
= bt_component_set_private_data(component
, text
);
641 if (ret
!= BT_COMPONENT_STATUS_OK
) {
645 ret
= bt_component_sink_set_consume_cb(component
,
647 if (ret
!= BT_COMPONENT_STATUS_OK
) {
653 destroy_text_data(text
);
657 /* Initialize plug-in entry points. */
658 BT_PLUGIN_NAME("text");
659 BT_PLUGIN_DESCRIPTION("Babeltrace text output plug-in.");
660 BT_PLUGIN_AUTHOR("Jérémie Galarneau");
661 BT_PLUGIN_LICENSE("MIT");
663 BT_PLUGIN_COMPONENT_CLASSES_BEGIN
664 BT_PLUGIN_SINK_COMPONENT_CLASS_ENTRY(PLUGIN_NAME
,
665 "Formats CTF-IR to text. Formerly known as ctf-text.",
667 BT_PLUGIN_COMPONENT_CLASSES_END