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
);
100 OPT_CLOCK_FORCE_CORRELATE
,
104 * We are _not_ using POPT_ARG_STRING ability to store directly into
105 * variables, because we want to cast the return to non-const, which is
106 * not possible without using poptGetOptArg explicitly. This helps us
107 * controlling memory allocation correctly without making assumptions
108 * about undocumented behaviors. poptGetOptArg is documented as
109 * requiring the returned const char * to be freed by the caller.
111 static struct poptOption long_options
[] = {
112 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
113 { "output", 'w', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_PATH
, NULL
, NULL
},
114 { "input-format", 'i', POPT_ARG_STRING
, NULL
, OPT_INPUT_FORMAT
, NULL
, NULL
},
115 { "output-format", 'o', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_FORMAT
, NULL
, NULL
},
116 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
117 { "list", 'l', POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
118 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
119 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
120 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
121 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
122 { "no-delta", 0, POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
123 { "clock-offset", 0, POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
124 { "clock-cycles", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_CYCLES
, NULL
, NULL
},
125 { "clock-seconds", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
126 { "clock-date", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
127 { "clock-gmt", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
128 { "clock-force-correlate", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
129 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
132 static void list_formats(FILE *fp
)
135 bt_fprintf_format_list(fp
);
138 static void usage(FILE *fp
)
140 fprintf(fp
, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION
);
141 fprintf(fp
, "usage : babeltrace [OPTIONS] FILE...\n");
143 fprintf(fp
, " FILE Input trace file(s) and/or directory(ies)\n");
144 fprintf(fp
, " (space-separated)\n");
145 fprintf(fp
, " -w, --output OUTPUT Output trace path (default: stdout)\n");
147 fprintf(fp
, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
148 fprintf(fp
, " -o, --output-format FORMAT Output trace format (default: text)\n");
150 fprintf(fp
, " -h, --help This help message\n");
151 fprintf(fp
, " -l, --list List available formats\n");
152 fprintf(fp
, " -v, --verbose Verbose mode\n");
153 fprintf(fp
, " (or set BABELTRACE_VERBOSE environment variable)\n");
154 fprintf(fp
, " -d, --debug Debug mode\n");
155 fprintf(fp
, " (or set BABELTRACE_DEBUG environment variable)\n");
156 fprintf(fp
, " --no-delta Do not print time delta between consecutive events\n");
157 fprintf(fp
, " -n, --names name1<,name2,...> Print field names:\n");
158 fprintf(fp
, " (payload OR args OR arg)\n");
159 fprintf(fp
, " none, all, scope, header, (context OR ctx)\n");
160 fprintf(fp
, " (default: payload,context)\n");
161 fprintf(fp
, " -f, --fields name1<,name2,...> Print additional fields:\n");
162 fprintf(fp
, " all, trace, trace:hostname, trace:domain,\n");
163 fprintf(fp
, " trace:procname, trace:vpid, loglevel, emf, callsite.\n");
164 fprintf(fp
, " (default: trace:hostname,trace:procname,trace:vpid)\n");
165 fprintf(fp
, " --clock-cycles Timestamp in cycles\n");
166 fprintf(fp
, " --clock-offset seconds Clock offset in seconds\n");
167 fprintf(fp
, " --clock-seconds Print the timestamps as [sec.ns]\n");
168 fprintf(fp
, " (default is: [hh:mm:ss.ns])\n");
169 fprintf(fp
, " --clock-date Print clock date\n");
170 fprintf(fp
, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n");
171 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently correlated\n");
172 fprintf(fp
, " across traces.\n");
177 static int get_names_args(poptContext
*pc
)
179 char *str
, *strlist
, *strctx
;
182 opt_payload_field_names
= 0;
183 opt_context_field_names
= 0;
184 strlist
= (char *) poptGetOptArg(*pc
);
188 str
= strtok_r(strlist
, ",", &strctx
);
190 if (!strcmp(str
, "all"))
191 opt_all_field_names
= 1;
192 else if (!strcmp(str
, "scope"))
193 opt_scope_field_names
= 1;
194 else if (!strcmp(str
, "context") || !strcmp(str
, "ctx"))
195 opt_context_field_names
= 1;
196 else if (!strcmp(str
, "header"))
197 opt_header_field_names
= 1;
198 else if (!strcmp(str
, "payload") || !strcmp(str
, "args") || !strcmp(str
, "arg"))
199 opt_payload_field_names
= 1;
200 else if (!strcmp(str
, "none")) {
201 opt_all_field_names
= 0;
202 opt_scope_field_names
= 0;
203 opt_context_field_names
= 0;
204 opt_header_field_names
= 0;
205 opt_payload_field_names
= 0;
207 fprintf(stderr
, "[error] unknown field name type %s\n", str
);
211 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
217 static int get_fields_args(poptContext
*pc
)
219 char *str
, *strlist
, *strctx
;
222 strlist
= (char *) poptGetOptArg(*pc
);
226 str
= strtok_r(strlist
, ",", &strctx
);
228 opt_trace_default_fields
= 0;
229 if (!strcmp(str
, "all"))
231 else if (!strcmp(str
, "trace"))
233 else if (!strcmp(str
, "trace:hostname"))
234 opt_trace_hostname_field
= 1;
235 else if (!strcmp(str
, "trace:domain"))
236 opt_trace_domain_field
= 1;
237 else if (!strcmp(str
, "trace:procname"))
238 opt_trace_procname_field
= 1;
239 else if (!strcmp(str
, "trace:vpid"))
240 opt_trace_vpid_field
= 1;
241 else if (!strcmp(str
, "loglevel"))
242 opt_loglevel_field
= 1;
243 else if (!strcmp(str
, "emf"))
245 else if (!strcmp(str
, "callsite"))
246 opt_callsite_field
= 1;
248 fprintf(stderr
, "[error] unknown field type %s\n", str
);
252 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
259 * Return 0 if caller should continue, < 0 if caller should return
260 * error, > 0 if caller should exit without reporting error.
262 static int parse_options(int argc
, char **argv
)
270 return 1; /* exit cleanly */
273 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
274 poptReadDefaultConfig(pc
, 0);
277 opt_context_field_names
= 1;
278 opt_payload_field_names
= 1;
280 while ((opt
= poptGetNextOpt(pc
)) != -1) {
282 case OPT_OUTPUT_PATH
:
283 opt_output_path
= (char *) poptGetOptArg(pc
);
284 if (!opt_output_path
) {
289 case OPT_INPUT_FORMAT
:
290 opt_input_format
= (char *) poptGetOptArg(pc
);
291 if (!opt_input_format
) {
296 case OPT_OUTPUT_FORMAT
:
297 opt_output_format
= (char *) poptGetOptArg(pc
);
298 if (!opt_output_format
) {
305 ret
= 1; /* exit cleanly */
308 list_formats(stdout
);
312 babeltrace_verbose
= 1;
315 if (get_names_args(&pc
)) {
321 if (get_fields_args(&pc
)) {
327 babeltrace_debug
= 1;
332 case OPT_CLOCK_CYCLES
:
333 opt_clock_cycles
= 1;
335 case OPT_CLOCK_OFFSET
:
340 str
= (char *) poptGetOptArg(pc
);
342 fprintf(stderr
, "[error] Missing --clock-offset argument\n");
347 opt_clock_offset
= strtoull(str
, &endptr
, 0);
348 if (*endptr
!= '\0' || str
== endptr
|| errno
!= 0) {
349 fprintf(stderr
, "[error] Incorrect --clock-offset argument: %s\n", str
);
357 case OPT_CLOCK_SECONDS
:
358 opt_clock_seconds
= 1;
366 case OPT_CLOCK_FORCE_CORRELATE
:
367 opt_clock_force_correlate
= 1;
377 ipath
= poptGetArg(pc
);
379 g_ptr_array_add(opt_input_paths
, (gpointer
) ipath
);
381 if (opt_input_paths
->len
== 0) {
393 static GPtrArray
*traversed_paths
= 0;
396 * traverse_trace_dir() is the callback function for File Tree Walk (nftw).
397 * it receives the path of the current entry (file, dir, link..etc) with
398 * a flag to indicate the type of the entry.
399 * if the entry being visited is a directory and contains a metadata file,
400 * then add the path to a global list to be processed later in
401 * add_traces_recursive.
403 static int traverse_trace_dir(const char *fpath
, const struct stat
*sb
,
404 int tflag
, struct FTW
*ftwbuf
)
412 dirfd
= open(fpath
, 0);
414 fprintf(stderr
, "[error] [Context] Unable to open trace "
415 "directory file descriptor.\n");
416 return 0; /* partial error */
418 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
420 closeret
= close(dirfd
);
425 /* No meta data, just return */
428 closeret
= close(metafd
);
431 return -1; /* failure */
433 closeret
= close(dirfd
);
436 return -1; /* failure */
439 /* Add path to the global list */
440 if (traversed_paths
== NULL
) {
441 fprintf(stderr
, "[error] [Context] Invalid open path array.\n");
444 g_ptr_array_add(traversed_paths
, g_string_new(fpath
));
451 * bt_context_add_traces_recursive: Open a trace recursively
453 * Find each trace present in the subdirectory starting from the given
454 * path, and add them to the context. The packet_seek parameter can be
455 * NULL: this specify to use the default format packet_seek.
457 * Return: 0 on success, < 0 on failure, > 0 on partial failure.
458 * Unable to open toplevel: failure.
459 * Unable to open some subdirectory or file: warn and continue (partial
462 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
463 const char *format_str
,
464 void (*packet_seek
)(struct bt_stream_pos
*pos
,
465 size_t offset
, int whence
))
471 /* Should lock traversed_paths mutex here if used in multithread */
473 traversed_paths
= g_ptr_array_new();
474 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
476 ret
= nftw(path
, traverse_trace_dir
, 10, 0);
478 /* Process the array if ntfw did not return a fatal error */
482 for (i
= 0; i
< traversed_paths
->len
; i
++) {
483 GString
*trace_path
= g_ptr_array_index(traversed_paths
,
485 int trace_id
= bt_context_add_trace(ctx
,
492 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" from %s "
493 "for reading.\n", trace_path
->str
, path
);
494 /* Allow to skip erroneous traces. */
495 ret
= 1; /* partial error */
497 g_array_append_val(trace_ids
, trace_id
);
499 g_string_free(trace_path
, TRUE
);
503 g_ptr_array_free(traversed_paths
, TRUE
);
504 traversed_paths
= NULL
;
506 /* Should unlock traversed paths mutex here if used in multithread */
509 * Return an error if no trace can be opened.
511 if (trace_ids
->len
== 0) {
512 fprintf(stderr
, "[error] Cannot open any trace for reading.\n\n");
513 ret
= -ENOENT
; /* failure */
515 g_array_free(trace_ids
, TRUE
);
520 int trace_pre_handler(struct bt_trace_descriptor
*td_write
,
521 struct bt_context
*ctx
)
523 struct ctf_text_stream_pos
*sout
;
524 struct trace_collection
*tc
;
527 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
530 if (!sout
->parent
.pre_trace_cb
)
534 for (i
= 0; i
< tc
->array
->len
; i
++) {
535 struct bt_trace_descriptor
*td
=
536 g_ptr_array_index(tc
->array
, i
);
538 ret
= sout
->parent
.pre_trace_cb(&sout
->parent
, td
);
540 fprintf(stderr
, "[error] Writing to trace pre handler failed.\n");
550 int trace_post_handler(struct bt_trace_descriptor
*td_write
,
551 struct bt_context
*ctx
)
553 struct ctf_text_stream_pos
*sout
;
554 struct trace_collection
*tc
;
557 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
560 if (!sout
->parent
.post_trace_cb
)
564 for (i
= 0; i
< tc
->array
->len
; i
++) {
565 struct bt_trace_descriptor
*td
=
566 g_ptr_array_index(tc
->array
, i
);
568 ret
= sout
->parent
.post_trace_cb(&sout
->parent
, td
);
570 fprintf(stderr
, "[error] Writing to trace post handler failed.\n");
580 int convert_trace(struct bt_trace_descriptor
*td_write
,
581 struct bt_context
*ctx
)
583 struct bt_ctf_iter
*iter
;
584 struct ctf_text_stream_pos
*sout
;
585 struct bt_iter_pos begin_pos
;
586 struct bt_ctf_event
*ctf_event
;
589 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
592 if (!sout
->parent
.event_cb
)
595 begin_pos
.type
= BT_SEEK_BEGIN
;
596 iter
= bt_ctf_iter_create(ctx
, &begin_pos
, NULL
);
601 while ((ctf_event
= bt_ctf_iter_read_event(iter
))) {
602 ret
= sout
->parent
.event_cb(&sout
->parent
, ctf_event
->parent
->stream
);
604 fprintf(stderr
, "[error] Writing event failed.\n");
607 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
614 bt_ctf_iter_destroy(iter
);
619 int main(int argc
, char **argv
)
621 int ret
, partial_error
= 0, open_success
= 0;
622 struct bt_format
*fmt_write
;
623 struct bt_trace_descriptor
*td_write
;
624 struct bt_context
*ctx
;
627 opt_input_paths
= g_ptr_array_new();
629 ret
= parse_options(argc
, argv
);
631 fprintf(stderr
, "Error parsing options.\n\n");
633 g_ptr_array_free(opt_input_paths
, TRUE
);
635 } else if (ret
> 0) {
636 g_ptr_array_free(opt_input_paths
, TRUE
);
639 printf_verbose("Verbose mode active.\n");
640 printf_debug("Debug mode active.\n");
642 if (opt_input_format
)
643 strlower(opt_input_format
);
644 if (opt_output_format
)
645 strlower(opt_output_format
);
647 printf_verbose("Converting from directory(ies):\n");
648 for (i
= 0; i
< opt_input_paths
->len
; i
++) {
649 const char *ipath
= g_ptr_array_index(opt_input_paths
, i
);
650 printf_verbose(" %s\n", ipath
);
652 printf_verbose("Converting from format: %s\n",
653 opt_input_format
? : "ctf <default>");
654 printf_verbose("Converting to target: %s\n",
655 opt_output_path
? : "<stdout>");
656 printf_verbose("Converting to format: %s\n",
657 opt_output_format
? : "text <default>");
659 if (!opt_input_format
) {
660 opt_input_format
= strdup("ctf");
661 if (!opt_input_format
) {
666 if (!opt_output_format
) {
667 opt_output_format
= strdup("text");
668 if (!opt_output_format
) {
673 fmt_read
= bt_lookup_format(g_quark_from_static_string(opt_input_format
));
674 if (!fmt_read
|| fmt_read
->name
!= g_quark_from_static_string("ctf")) {
675 fprintf(stderr
, "[error] Format \"%s\" is not supported.\n\n",
680 fmt_write
= bt_lookup_format(g_quark_from_static_string(opt_output_format
));
682 fprintf(stderr
, "[error] format \"%s\" is not supported.\n\n",
688 ctx
= bt_context_create();
693 for (i
= 0; i
< opt_input_paths
->len
; i
++) {
694 const char *ipath
= g_ptr_array_index(opt_input_paths
, i
);
695 ret
= bt_context_add_traces_recursive(ctx
, ipath
,
696 opt_input_format
, NULL
);
698 fprintf(stderr
, "[error] opening trace \"%s\" for reading.\n\n",
700 } else if (ret
> 0) {
701 fprintf(stderr
, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n",
703 open_success
= 1; /* some traces were OK */
706 open_success
= 1; /* all traces were OK */
710 fprintf(stderr
, "[error] none of the specified trace paths could be opened.\n\n");
714 td_write
= fmt_write
->open_trace(opt_output_path
, O_RDWR
, NULL
, NULL
);
716 fprintf(stderr
, "Error opening trace \"%s\" for writing.\n\n",
717 opt_output_path
? : "<none>");
722 * Errors happened when opening traces, but we continue anyway.
723 * sleep to let user see the stderr output before stdout.
726 sleep(PARTIAL_ERROR_SLEEP
);
728 ret
= trace_pre_handler(td_write
, ctx
);
730 fprintf(stderr
, "Error in trace pre handle.\n\n");
731 goto error_copy_trace
;
734 ret
= convert_trace(td_write
, ctx
);
736 fprintf(stderr
, "Error printing trace.\n\n");
737 goto error_copy_trace
;
740 ret
= trace_post_handler(td_write
, ctx
);
742 fprintf(stderr
, "Error in trace post handle.\n\n");
743 goto error_copy_trace
;
746 fmt_write
->close_trace(td_write
);
749 printf_verbose("finished converting. Output written to:\n%s\n",
750 opt_output_path
? : "<stdout>");
755 fmt_write
->close_trace(td_write
);
761 /* teardown and exit */
763 free(opt_input_format
);
764 free(opt_output_format
);
765 free(opt_output_path
);
766 g_ptr_array_free(opt_input_paths
, TRUE
);