4 * Babeltrace Trace Converter
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@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
31 #include <babeltrace/babeltrace.h>
32 #include <babeltrace/format.h>
33 #include <babeltrace/context.h>
34 #include <babeltrace/context-internal.h>
35 #include <babeltrace/ctf/types.h>
36 #include <babeltrace/ctf/events.h>
37 /* TODO: fix object model for format-agnostic callbacks */
38 #include <babeltrace/ctf/events-internal.h>
39 #include <babeltrace/ctf/iterator.h>
40 #include <babeltrace/ctf-text/types.h>
41 #include <babeltrace/iterator.h>
47 #include <sys/types.h>
54 #include <babeltrace/ctf-ir/metadata.h> /* for clocks */
56 #define PARTIAL_ERROR_SLEEP 3 /* 3 seconds */
58 #define DEFAULT_FILE_ARRAY_SIZE 1
60 static char *opt_input_format
, *opt_output_format
;
63 * We are not freeing opt_input_paths ipath elements when exiting from
64 * main() for backward compatibility with libpop 0.13, which does not
65 * allocate copies for arguments returned by poptGetArg(), and for
66 * general compatibility with the documented behavior. This is known to
67 * cause a small memory leak with libpop 0.16.
69 static GPtrArray
*opt_input_paths
;
70 static char *opt_output_path
;
72 static struct bt_format
*fmt_read
;
75 void strlower(char *str
)
78 *str
= tolower((int) *str
);
101 OPT_CLOCK_FORCE_CORRELATE
,
105 * We are _not_ using POPT_ARG_STRING ability to store directly into
106 * variables, because we want to cast the return to non-const, which is
107 * not possible without using poptGetOptArg explicitly. This helps us
108 * controlling memory allocation correctly without making assumptions
109 * about undocumented behaviors. poptGetOptArg is documented as
110 * requiring the returned const char * to be freed by the caller.
112 static struct poptOption long_options
[] = {
113 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
114 { "output", 'w', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_PATH
, NULL
, NULL
},
115 { "input-format", 'i', POPT_ARG_STRING
, NULL
, OPT_INPUT_FORMAT
, NULL
, NULL
},
116 { "output-format", 'o', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_FORMAT
, NULL
, NULL
},
117 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
118 { "list", 'l', POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
119 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
120 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
121 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
122 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
123 { "no-delta", 0, POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
124 { "clock-offset", 0, POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
125 { "clock-offset-ns", 0, POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET_NS
, NULL
, NULL
},
126 { "clock-cycles", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_CYCLES
, NULL
, NULL
},
127 { "clock-seconds", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
128 { "clock-date", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
129 { "clock-gmt", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
130 { "clock-force-correlate", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
131 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
134 static void list_formats(FILE *fp
)
137 bt_fprintf_format_list(fp
);
140 static void usage(FILE *fp
)
142 fprintf(fp
, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION
);
143 fprintf(fp
, "usage : babeltrace [OPTIONS] FILE...\n");
145 fprintf(fp
, " FILE Input trace file(s) and/or directory(ies)\n");
146 fprintf(fp
, " (space-separated)\n");
147 fprintf(fp
, " -w, --output OUTPUT Output trace path (default: stdout)\n");
149 fprintf(fp
, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
150 fprintf(fp
, " -o, --output-format FORMAT Output trace format (default: text)\n");
152 fprintf(fp
, " -h, --help This help message\n");
153 fprintf(fp
, " -l, --list List available formats\n");
154 fprintf(fp
, " -v, --verbose Verbose mode\n");
155 fprintf(fp
, " (or set BABELTRACE_VERBOSE environment variable)\n");
156 fprintf(fp
, " -d, --debug Debug mode\n");
157 fprintf(fp
, " (or set BABELTRACE_DEBUG environment variable)\n");
158 fprintf(fp
, " --no-delta Do not print time delta between consecutive events\n");
159 fprintf(fp
, " -n, --names name1<,name2,...> Print field names:\n");
160 fprintf(fp
, " (payload OR args OR arg)\n");
161 fprintf(fp
, " none, all, scope, header, (context OR ctx)\n");
162 fprintf(fp
, " (default: payload,context)\n");
163 fprintf(fp
, " -f, --fields name1<,name2,...> Print additional fields:\n");
164 fprintf(fp
, " all, trace, trace:hostname, trace:domain,\n");
165 fprintf(fp
, " trace:procname, trace:vpid, loglevel, emf, callsite.\n");
166 fprintf(fp
, " (default: trace:hostname,trace:procname,trace:vpid)\n");
167 fprintf(fp
, " --clock-cycles Timestamp in cycles\n");
168 fprintf(fp
, " --clock-offset seconds Clock offset in seconds\n");
169 fprintf(fp
, " --clock-offset-ns ns Clock offset in nanoseconds\n");
170 fprintf(fp
, " --clock-seconds Print the timestamps as [sec.ns]\n");
171 fprintf(fp
, " (default is: [hh:mm:ss.ns])\n");
172 fprintf(fp
, " --clock-date Print clock date\n");
173 fprintf(fp
, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n");
174 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently correlated\n");
175 fprintf(fp
, " across traces.\n");
180 static int get_names_args(poptContext
*pc
)
182 char *str
, *strlist
, *strctx
;
185 opt_payload_field_names
= 0;
186 opt_context_field_names
= 0;
187 strlist
= (char *) poptGetOptArg(*pc
);
191 str
= strtok_r(strlist
, ",", &strctx
);
193 if (!strcmp(str
, "all"))
194 opt_all_field_names
= 1;
195 else if (!strcmp(str
, "scope"))
196 opt_scope_field_names
= 1;
197 else if (!strcmp(str
, "context") || !strcmp(str
, "ctx"))
198 opt_context_field_names
= 1;
199 else if (!strcmp(str
, "header"))
200 opt_header_field_names
= 1;
201 else if (!strcmp(str
, "payload") || !strcmp(str
, "args") || !strcmp(str
, "arg"))
202 opt_payload_field_names
= 1;
203 else if (!strcmp(str
, "none")) {
204 opt_all_field_names
= 0;
205 opt_scope_field_names
= 0;
206 opt_context_field_names
= 0;
207 opt_header_field_names
= 0;
208 opt_payload_field_names
= 0;
210 fprintf(stderr
, "[error] unknown field name type %s\n", str
);
214 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
220 static int get_fields_args(poptContext
*pc
)
222 char *str
, *strlist
, *strctx
;
225 strlist
= (char *) poptGetOptArg(*pc
);
229 str
= strtok_r(strlist
, ",", &strctx
);
231 opt_trace_default_fields
= 0;
232 if (!strcmp(str
, "all"))
234 else if (!strcmp(str
, "trace"))
236 else if (!strcmp(str
, "trace:hostname"))
237 opt_trace_hostname_field
= 1;
238 else if (!strcmp(str
, "trace:domain"))
239 opt_trace_domain_field
= 1;
240 else if (!strcmp(str
, "trace:procname"))
241 opt_trace_procname_field
= 1;
242 else if (!strcmp(str
, "trace:vpid"))
243 opt_trace_vpid_field
= 1;
244 else if (!strcmp(str
, "loglevel"))
245 opt_loglevel_field
= 1;
246 else if (!strcmp(str
, "emf"))
248 else if (!strcmp(str
, "callsite"))
249 opt_callsite_field
= 1;
251 fprintf(stderr
, "[error] unknown field type %s\n", str
);
255 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
262 * Return 0 if caller should continue, < 0 if caller should return
263 * error, > 0 if caller should exit without reporting error.
265 static int parse_options(int argc
, char **argv
)
273 return 1; /* exit cleanly */
276 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
277 poptReadDefaultConfig(pc
, 0);
280 opt_context_field_names
= 1;
281 opt_payload_field_names
= 1;
283 while ((opt
= poptGetNextOpt(pc
)) != -1) {
285 case OPT_OUTPUT_PATH
:
286 opt_output_path
= (char *) poptGetOptArg(pc
);
287 if (!opt_output_path
) {
292 case OPT_INPUT_FORMAT
:
293 opt_input_format
= (char *) poptGetOptArg(pc
);
294 if (!opt_input_format
) {
299 case OPT_OUTPUT_FORMAT
:
300 opt_output_format
= (char *) poptGetOptArg(pc
);
301 if (!opt_output_format
) {
308 ret
= 1; /* exit cleanly */
311 list_formats(stdout
);
315 babeltrace_verbose
= 1;
318 if (get_names_args(&pc
)) {
324 if (get_fields_args(&pc
)) {
330 babeltrace_debug
= 1;
335 case OPT_CLOCK_CYCLES
:
336 opt_clock_cycles
= 1;
338 case OPT_CLOCK_OFFSET
:
343 str
= (char *) poptGetOptArg(pc
);
345 fprintf(stderr
, "[error] Missing --clock-offset argument\n");
350 opt_clock_offset
= strtoull(str
, &endptr
, 0);
351 if (*endptr
!= '\0' || str
== endptr
|| errno
!= 0) {
352 fprintf(stderr
, "[error] Incorrect --clock-offset argument: %s\n", str
);
360 case OPT_CLOCK_SECONDS
:
361 opt_clock_seconds
= 1;
363 case OPT_CLOCK_OFFSET_NS
:
368 str
= (char *) poptGetOptArg(pc
);
370 fprintf(stderr
, "[error] Missing --clock-offset-ns argument\n");
375 opt_clock_offset_ns
= strtoull(str
, &endptr
, 0);
376 if (*endptr
!= '\0' || str
== endptr
|| errno
!= 0) {
377 fprintf(stderr
, "[error] Incorrect --clock-offset-ns argument: %s\n", str
);
392 case OPT_CLOCK_FORCE_CORRELATE
:
393 opt_clock_force_correlate
= 1;
403 ipath
= poptGetArg(pc
);
405 g_ptr_array_add(opt_input_paths
, (gpointer
) ipath
);
407 if (opt_input_paths
->len
== 0) {
419 static GPtrArray
*traversed_paths
= 0;
422 * traverse_trace_dir() is the callback function for File Tree Walk (nftw).
423 * it receives the path of the current entry (file, dir, link..etc) with
424 * a flag to indicate the type of the entry.
425 * if the entry being visited is a directory and contains a metadata file,
426 * then add the path to a global list to be processed later in
427 * add_traces_recursive.
429 static int traverse_trace_dir(const char *fpath
, const struct stat
*sb
,
430 int tflag
, struct FTW
*ftwbuf
)
438 dirfd
= open(fpath
, 0);
440 fprintf(stderr
, "[error] [Context] Unable to open trace "
441 "directory file descriptor.\n");
442 return 0; /* partial error */
444 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
446 closeret
= close(dirfd
);
451 /* No meta data, just return */
456 closeret
= close(metafd
);
461 closeret
= close(dirfd
);
470 /* Add path to the global list */
471 if (traversed_paths
== NULL
) {
472 fprintf(stderr
, "[error] [Context] Invalid open path array.\n");
475 g_ptr_array_add(traversed_paths
, g_string_new(fpath
));
482 * bt_context_add_traces_recursive: Open a trace recursively
484 * Find each trace present in the subdirectory starting from the given
485 * path, and add them to the context. The packet_seek parameter can be
486 * NULL: this specify to use the default format packet_seek.
488 * Return: 0 on success, < 0 on failure, > 0 on partial failure.
489 * Unable to open toplevel: failure.
490 * Unable to open some subdirectory or file: warn and continue (partial
493 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
494 const char *format_str
,
495 void (*packet_seek
)(struct bt_stream_pos
*pos
,
496 size_t offset
, int whence
))
498 int ret
= 0, trace_ids
= 0;
500 /* Should lock traversed_paths mutex here if used in multithread */
502 traversed_paths
= g_ptr_array_new();
503 ret
= nftw(path
, traverse_trace_dir
, 10, 0);
505 /* Process the array if ntfw did not return a fatal error */
509 for (i
= 0; i
< traversed_paths
->len
; i
++) {
510 GString
*trace_path
= g_ptr_array_index(traversed_paths
,
512 int trace_id
= bt_context_add_trace(ctx
,
519 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" from %s "
520 "for reading.\n", trace_path
->str
, path
);
521 /* Allow to skip erroneous traces. */
522 ret
= 1; /* partial error */
526 g_string_free(trace_path
, TRUE
);
530 g_ptr_array_free(traversed_paths
, TRUE
);
531 traversed_paths
= NULL
;
533 /* Should unlock traversed paths mutex here if used in multithread */
536 * Return an error if no trace can be opened.
538 if (trace_ids
== 0) {
539 fprintf(stderr
, "[error] Cannot open any trace for reading.\n\n");
540 ret
= -ENOENT
; /* failure */
546 int trace_pre_handler(struct bt_trace_descriptor
*td_write
,
547 struct bt_context
*ctx
)
549 struct ctf_text_stream_pos
*sout
;
550 struct trace_collection
*tc
;
553 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
556 if (!sout
->parent
.pre_trace_cb
)
560 for (i
= 0; i
< tc
->array
->len
; i
++) {
561 struct bt_trace_descriptor
*td
=
562 g_ptr_array_index(tc
->array
, i
);
564 ret
= sout
->parent
.pre_trace_cb(&sout
->parent
, td
);
566 fprintf(stderr
, "[error] Writing to trace pre handler failed.\n");
576 int trace_post_handler(struct bt_trace_descriptor
*td_write
,
577 struct bt_context
*ctx
)
579 struct ctf_text_stream_pos
*sout
;
580 struct trace_collection
*tc
;
583 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
586 if (!sout
->parent
.post_trace_cb
)
590 for (i
= 0; i
< tc
->array
->len
; i
++) {
591 struct bt_trace_descriptor
*td
=
592 g_ptr_array_index(tc
->array
, i
);
594 ret
= sout
->parent
.post_trace_cb(&sout
->parent
, td
);
596 fprintf(stderr
, "[error] Writing to trace post handler failed.\n");
606 int convert_trace(struct bt_trace_descriptor
*td_write
,
607 struct bt_context
*ctx
)
609 struct bt_ctf_iter
*iter
;
610 struct ctf_text_stream_pos
*sout
;
611 struct bt_iter_pos begin_pos
;
612 struct bt_ctf_event
*ctf_event
;
615 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
618 if (!sout
->parent
.event_cb
)
621 begin_pos
.type
= BT_SEEK_BEGIN
;
622 iter
= bt_ctf_iter_create(ctx
, &begin_pos
, NULL
);
627 while ((ctf_event
= bt_ctf_iter_read_event(iter
))) {
628 ret
= sout
->parent
.event_cb(&sout
->parent
, ctf_event
->parent
->stream
);
630 fprintf(stderr
, "[error] Writing event failed.\n");
633 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
640 bt_ctf_iter_destroy(iter
);
645 int main(int argc
, char **argv
)
647 int ret
, partial_error
= 0, open_success
= 0;
648 struct bt_format
*fmt_write
;
649 struct bt_trace_descriptor
*td_write
;
650 struct bt_context
*ctx
;
653 opt_input_paths
= g_ptr_array_new();
655 ret
= parse_options(argc
, argv
);
657 fprintf(stderr
, "Error parsing options.\n\n");
659 g_ptr_array_free(opt_input_paths
, TRUE
);
661 } else if (ret
> 0) {
662 g_ptr_array_free(opt_input_paths
, TRUE
);
665 printf_verbose("Verbose mode active.\n");
666 printf_debug("Debug mode active.\n");
668 if (opt_input_format
)
669 strlower(opt_input_format
);
670 if (opt_output_format
)
671 strlower(opt_output_format
);
673 printf_verbose("Converting from directory(ies):\n");
674 for (i
= 0; i
< opt_input_paths
->len
; i
++) {
675 const char *ipath
= g_ptr_array_index(opt_input_paths
, i
);
676 printf_verbose(" %s\n", ipath
);
678 printf_verbose("Converting from format: %s\n",
679 opt_input_format
? : "ctf <default>");
680 printf_verbose("Converting to target: %s\n",
681 opt_output_path
? : "<stdout>");
682 printf_verbose("Converting to format: %s\n",
683 opt_output_format
? : "text <default>");
685 if (!opt_input_format
) {
686 opt_input_format
= strdup("ctf");
687 if (!opt_input_format
) {
692 if (!opt_output_format
) {
693 opt_output_format
= strdup("text");
694 if (!opt_output_format
) {
699 fmt_read
= bt_lookup_format(g_quark_from_static_string(opt_input_format
));
700 if (!fmt_read
|| fmt_read
->name
!= g_quark_from_static_string("ctf")) {
701 fprintf(stderr
, "[error] Format \"%s\" is not supported.\n\n",
706 fmt_write
= bt_lookup_format(g_quark_from_static_string(opt_output_format
));
708 fprintf(stderr
, "[error] format \"%s\" is not supported.\n\n",
714 ctx
= bt_context_create();
719 for (i
= 0; i
< opt_input_paths
->len
; i
++) {
720 const char *ipath
= g_ptr_array_index(opt_input_paths
, i
);
721 ret
= bt_context_add_traces_recursive(ctx
, ipath
,
722 opt_input_format
, NULL
);
724 fprintf(stderr
, "[error] opening trace \"%s\" for reading.\n\n",
726 } else if (ret
> 0) {
727 fprintf(stderr
, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n",
729 open_success
= 1; /* some traces were OK */
732 open_success
= 1; /* all traces were OK */
736 fprintf(stderr
, "[error] none of the specified trace paths could be opened.\n\n");
740 td_write
= fmt_write
->open_trace(opt_output_path
, O_RDWR
, NULL
, NULL
);
742 fprintf(stderr
, "Error opening trace \"%s\" for writing.\n\n",
743 opt_output_path
? : "<none>");
748 * Errors happened when opening traces, but we continue anyway.
749 * sleep to let user see the stderr output before stdout.
752 sleep(PARTIAL_ERROR_SLEEP
);
754 ret
= trace_pre_handler(td_write
, ctx
);
756 fprintf(stderr
, "Error in trace pre handle.\n\n");
757 goto error_copy_trace
;
760 ret
= convert_trace(td_write
, ctx
);
762 fprintf(stderr
, "Error printing trace.\n\n");
763 goto error_copy_trace
;
766 ret
= trace_post_handler(td_write
, ctx
);
768 fprintf(stderr
, "Error in trace post handle.\n\n");
769 goto error_copy_trace
;
772 fmt_write
->close_trace(td_write
);
775 printf_verbose("finished converting. Output written to:\n%s\n",
776 opt_output_path
? : "<stdout>");
781 fmt_write
->close_trace(td_write
);
787 /* teardown and exit */
789 free(opt_input_format
);
790 free(opt_output_format
);
791 free(opt_output_path
);
792 g_ptr_array_free(opt_input_paths
, TRUE
);