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
29 #include <babeltrace/babeltrace.h>
30 #include <babeltrace/format.h>
31 #include <babeltrace/context.h>
32 #include <babeltrace/context-internal.h>
33 #include <babeltrace/ctf/types.h>
34 #include <babeltrace/ctf/events.h>
35 /* TODO: fix object model for format-agnostic callbacks */
36 #include <babeltrace/ctf/events-internal.h>
37 #include <babeltrace/ctf/iterator.h>
38 #include <babeltrace/ctf-text/types.h>
39 #include <babeltrace/iterator.h>
45 #include <sys/types.h>
52 #include <babeltrace/ctf-ir/metadata.h> /* for clocks */
54 #define PARTIAL_ERROR_SLEEP 3 /* 3 seconds */
56 #define DEFAULT_FILE_ARRAY_SIZE 1
58 #define NET_URL_PREFIX "net://"
59 #define NET4_URL_PREFIX "net4://"
60 #define NET6_URL_PREFIX "net6://"
62 static char *opt_input_format
, *opt_output_format
;
65 * We are not freeing opt_input_paths ipath elements when exiting from
66 * main() for backward compatibility with libpop 0.13, which does not
67 * allocate copies for arguments returned by poptGetArg(), and for
68 * general compatibility with the documented behavior. This is known to
69 * cause a small memory leak with libpop 0.16.
71 static GPtrArray
*opt_input_paths
;
72 static char *opt_output_path
;
74 static struct bt_format
*fmt_read
;
77 void strlower(char *str
)
80 *str
= tolower((int) *str
);
103 OPT_CLOCK_FORCE_CORRELATE
,
107 * We are _not_ using POPT_ARG_STRING ability to store directly into
108 * variables, because we want to cast the return to non-const, which is
109 * not possible without using poptGetOptArg explicitly. This helps us
110 * controlling memory allocation correctly without making assumptions
111 * about undocumented behaviors. poptGetOptArg is documented as
112 * requiring the returned const char * to be freed by the caller.
114 static struct poptOption long_options
[] = {
115 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
116 { "output", 'w', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_PATH
, NULL
, NULL
},
117 { "input-format", 'i', POPT_ARG_STRING
, NULL
, OPT_INPUT_FORMAT
, NULL
, NULL
},
118 { "output-format", 'o', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_FORMAT
, NULL
, NULL
},
119 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
120 { "list", 'l', POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
121 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
122 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
123 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
124 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
125 { "no-delta", 0, POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
126 { "clock-offset", 0, POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
127 { "clock-offset-ns", 0, POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET_NS
, NULL
, NULL
},
128 { "clock-cycles", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_CYCLES
, NULL
, NULL
},
129 { "clock-seconds", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
130 { "clock-date", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
131 { "clock-gmt", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
132 { "clock-force-correlate", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
133 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
136 static void list_formats(FILE *fp
)
139 bt_fprintf_format_list(fp
);
142 static void usage(FILE *fp
)
144 fprintf(fp
, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION
);
145 fprintf(fp
, "usage : babeltrace [OPTIONS] FILE...\n");
147 fprintf(fp
, " FILE Input trace file(s) and/or directory(ies)\n");
148 fprintf(fp
, " (space-separated)\n");
149 fprintf(fp
, " -w, --output OUTPUT Output trace path (default: stdout)\n");
151 fprintf(fp
, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
152 fprintf(fp
, " -o, --output-format FORMAT Output trace format (default: text)\n");
154 fprintf(fp
, " -h, --help This help message\n");
155 fprintf(fp
, " -l, --list List available formats\n");
156 fprintf(fp
, " -v, --verbose Verbose mode\n");
157 fprintf(fp
, " (or set BABELTRACE_VERBOSE environment variable)\n");
158 fprintf(fp
, " -d, --debug Debug mode\n");
159 fprintf(fp
, " (or set BABELTRACE_DEBUG environment variable)\n");
160 fprintf(fp
, " --no-delta Do not print time delta between consecutive events\n");
161 fprintf(fp
, " -n, --names name1<,name2,...> Print field names:\n");
162 fprintf(fp
, " (payload OR args OR arg)\n");
163 fprintf(fp
, " none, all, scope, header, (context OR ctx)\n");
164 fprintf(fp
, " (default: payload,context)\n");
165 fprintf(fp
, " -f, --fields name1<,name2,...> Print additional fields:\n");
166 fprintf(fp
, " all, trace, trace:hostname, trace:domain,\n");
167 fprintf(fp
, " trace:procname, trace:vpid, loglevel, emf, callsite.\n");
168 fprintf(fp
, " (default: trace:hostname,trace:procname,trace:vpid)\n");
169 fprintf(fp
, " --clock-cycles Timestamp in cycles\n");
170 fprintf(fp
, " --clock-offset seconds Clock offset in seconds\n");
171 fprintf(fp
, " --clock-offset-ns ns Clock offset in nanoseconds\n");
172 fprintf(fp
, " --clock-seconds Print the timestamps as [sec.ns]\n");
173 fprintf(fp
, " (default is: [hh:mm:ss.ns])\n");
174 fprintf(fp
, " --clock-date Print clock date\n");
175 fprintf(fp
, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n");
176 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently correlated\n");
177 fprintf(fp
, " across traces.\n");
182 static int get_names_args(poptContext
*pc
)
184 char *str
, *strlist
, *strctx
;
187 opt_payload_field_names
= 0;
188 opt_context_field_names
= 0;
189 strlist
= (char *) poptGetOptArg(*pc
);
193 str
= strtok_r(strlist
, ",", &strctx
);
195 if (!strcmp(str
, "all"))
196 opt_all_field_names
= 1;
197 else if (!strcmp(str
, "scope"))
198 opt_scope_field_names
= 1;
199 else if (!strcmp(str
, "context") || !strcmp(str
, "ctx"))
200 opt_context_field_names
= 1;
201 else if (!strcmp(str
, "header"))
202 opt_header_field_names
= 1;
203 else if (!strcmp(str
, "payload") || !strcmp(str
, "args") || !strcmp(str
, "arg"))
204 opt_payload_field_names
= 1;
205 else if (!strcmp(str
, "none")) {
206 opt_all_field_names
= 0;
207 opt_scope_field_names
= 0;
208 opt_context_field_names
= 0;
209 opt_header_field_names
= 0;
210 opt_payload_field_names
= 0;
212 fprintf(stderr
, "[error] unknown field name type %s\n", str
);
216 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
222 static int get_fields_args(poptContext
*pc
)
224 char *str
, *strlist
, *strctx
;
227 strlist
= (char *) poptGetOptArg(*pc
);
231 str
= strtok_r(strlist
, ",", &strctx
);
233 opt_trace_default_fields
= 0;
234 if (!strcmp(str
, "all"))
236 else if (!strcmp(str
, "trace"))
238 else if (!strcmp(str
, "trace:hostname"))
239 opt_trace_hostname_field
= 1;
240 else if (!strcmp(str
, "trace:domain"))
241 opt_trace_domain_field
= 1;
242 else if (!strcmp(str
, "trace:procname"))
243 opt_trace_procname_field
= 1;
244 else if (!strcmp(str
, "trace:vpid"))
245 opt_trace_vpid_field
= 1;
246 else if (!strcmp(str
, "loglevel"))
247 opt_loglevel_field
= 1;
248 else if (!strcmp(str
, "emf"))
250 else if (!strcmp(str
, "callsite"))
251 opt_callsite_field
= 1;
253 fprintf(stderr
, "[error] unknown field type %s\n", str
);
257 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
264 * Return 0 if caller should continue, < 0 if caller should return
265 * error, > 0 if caller should exit without reporting error.
267 static int parse_options(int argc
, char **argv
)
275 return 1; /* exit cleanly */
278 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
279 poptReadDefaultConfig(pc
, 0);
282 opt_context_field_names
= 1;
283 opt_payload_field_names
= 1;
285 while ((opt
= poptGetNextOpt(pc
)) != -1) {
287 case OPT_OUTPUT_PATH
:
288 opt_output_path
= (char *) poptGetOptArg(pc
);
289 if (!opt_output_path
) {
294 case OPT_INPUT_FORMAT
:
295 opt_input_format
= (char *) poptGetOptArg(pc
);
296 if (!opt_input_format
) {
301 case OPT_OUTPUT_FORMAT
:
302 opt_output_format
= (char *) poptGetOptArg(pc
);
303 if (!opt_output_format
) {
310 ret
= 1; /* exit cleanly */
313 list_formats(stdout
);
317 babeltrace_verbose
= 1;
320 if (get_names_args(&pc
)) {
326 if (get_fields_args(&pc
)) {
332 babeltrace_debug
= 1;
337 case OPT_CLOCK_CYCLES
:
338 opt_clock_cycles
= 1;
340 case OPT_CLOCK_OFFSET
:
345 str
= (char *) poptGetOptArg(pc
);
347 fprintf(stderr
, "[error] Missing --clock-offset argument\n");
352 opt_clock_offset
= strtoll(str
, &endptr
, 0);
353 if (*endptr
!= '\0' || str
== endptr
|| errno
!= 0) {
354 fprintf(stderr
, "[error] Incorrect --clock-offset argument: %s\n", str
);
362 case OPT_CLOCK_SECONDS
:
363 opt_clock_seconds
= 1;
365 case OPT_CLOCK_OFFSET_NS
:
370 str
= (char *) poptGetOptArg(pc
);
372 fprintf(stderr
, "[error] Missing --clock-offset-ns argument\n");
377 opt_clock_offset_ns
= strtoll(str
, &endptr
, 0);
378 if (*endptr
!= '\0' || str
== endptr
|| errno
!= 0) {
379 fprintf(stderr
, "[error] Incorrect --clock-offset-ns argument: %s\n", str
);
394 case OPT_CLOCK_FORCE_CORRELATE
:
395 opt_clock_force_correlate
= 1;
405 ipath
= poptGetArg(pc
);
407 g_ptr_array_add(opt_input_paths
, (gpointer
) ipath
);
409 if (opt_input_paths
->len
== 0) {
421 static GPtrArray
*traversed_paths
= 0;
424 * traverse_trace_dir() is the callback function for File Tree Walk (nftw).
425 * it receives the path of the current entry (file, dir, link..etc) with
426 * a flag to indicate the type of the entry.
427 * if the entry being visited is a directory and contains a metadata file,
428 * then add the path to a global list to be processed later in
429 * add_traces_recursive.
431 static int traverse_trace_dir(const char *fpath
, const struct stat
*sb
,
432 int tflag
, struct FTW
*ftwbuf
)
440 dirfd
= open(fpath
, 0);
442 fprintf(stderr
, "[error] [Context] Unable to open trace "
443 "directory file descriptor.\n");
444 return 0; /* partial error */
446 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
448 closeret
= close(dirfd
);
453 /* No meta data, just return */
458 closeret
= close(metafd
);
463 closeret
= close(dirfd
);
472 /* Add path to the global list */
473 if (traversed_paths
== NULL
) {
474 fprintf(stderr
, "[error] [Context] Invalid open path array.\n");
477 g_ptr_array_add(traversed_paths
, g_string_new(fpath
));
484 * bt_context_add_traces_recursive: Open a trace recursively
486 * Find each trace present in the subdirectory starting from the given
487 * path, and add them to the context. The packet_seek parameter can be
488 * NULL: this specify to use the default format packet_seek.
490 * Return: 0 on success, < 0 on failure, > 0 on partial failure.
491 * Unable to open toplevel: failure.
492 * Unable to open some subdirectory or file: warn and continue (partial
495 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
496 const char *format_str
,
497 void (*packet_seek
)(struct bt_stream_pos
*pos
,
498 size_t offset
, int whence
))
500 int ret
= 0, trace_ids
= 0;
502 if ((strncmp(path
, NET4_URL_PREFIX
, sizeof(NET4_URL_PREFIX
) - 1)) == 0 ||
503 (strncmp(path
, NET6_URL_PREFIX
, sizeof(NET6_URL_PREFIX
) - 1)) == 0 ||
504 (strncmp(path
, NET_URL_PREFIX
, sizeof(NET_URL_PREFIX
) - 1)) == 0) {
505 ret
= bt_context_add_trace(ctx
,
506 path
, format_str
, packet_seek
, NULL
, NULL
);
508 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" "
509 "for reading.\n", path
);
513 /* Should lock traversed_paths mutex here if used in multithread */
515 traversed_paths
= g_ptr_array_new();
516 ret
= nftw(path
, traverse_trace_dir
, 10, 0);
518 /* Process the array if ntfw did not return a fatal error */
522 for (i
= 0; i
< traversed_paths
->len
; i
++) {
523 GString
*trace_path
= g_ptr_array_index(traversed_paths
,
525 int trace_id
= bt_context_add_trace(ctx
,
532 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" from %s "
533 "for reading.\n", trace_path
->str
, path
);
534 /* Allow to skip erroneous traces. */
535 ret
= 1; /* partial error */
539 g_string_free(trace_path
, TRUE
);
543 g_ptr_array_free(traversed_paths
, TRUE
);
544 traversed_paths
= NULL
;
546 /* Should unlock traversed paths mutex here if used in multithread */
549 * Return an error if no trace can be opened.
551 if (trace_ids
== 0) {
552 fprintf(stderr
, "[error] Cannot open any trace for reading.\n\n");
553 ret
= -ENOENT
; /* failure */
559 int trace_pre_handler(struct bt_trace_descriptor
*td_write
,
560 struct bt_context
*ctx
)
562 struct ctf_text_stream_pos
*sout
;
563 struct trace_collection
*tc
;
566 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
569 if (!sout
->parent
.pre_trace_cb
)
573 for (i
= 0; i
< tc
->array
->len
; i
++) {
574 struct bt_trace_descriptor
*td
=
575 g_ptr_array_index(tc
->array
, i
);
577 ret
= sout
->parent
.pre_trace_cb(&sout
->parent
, td
);
579 fprintf(stderr
, "[error] Writing to trace pre handler failed.\n");
589 int trace_post_handler(struct bt_trace_descriptor
*td_write
,
590 struct bt_context
*ctx
)
592 struct ctf_text_stream_pos
*sout
;
593 struct trace_collection
*tc
;
596 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
599 if (!sout
->parent
.post_trace_cb
)
603 for (i
= 0; i
< tc
->array
->len
; i
++) {
604 struct bt_trace_descriptor
*td
=
605 g_ptr_array_index(tc
->array
, i
);
607 ret
= sout
->parent
.post_trace_cb(&sout
->parent
, td
);
609 fprintf(stderr
, "[error] Writing to trace post handler failed.\n");
619 int convert_trace(struct bt_trace_descriptor
*td_write
,
620 struct bt_context
*ctx
)
622 struct bt_ctf_iter
*iter
;
623 struct ctf_text_stream_pos
*sout
;
624 struct bt_iter_pos begin_pos
;
625 struct bt_ctf_event
*ctf_event
;
628 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
631 if (!sout
->parent
.event_cb
)
634 begin_pos
.type
= BT_SEEK_BEGIN
;
635 iter
= bt_ctf_iter_create(ctx
, &begin_pos
, NULL
);
640 while ((ctf_event
= bt_ctf_iter_read_event(iter
))) {
641 ret
= sout
->parent
.event_cb(&sout
->parent
, ctf_event
->parent
->stream
);
643 fprintf(stderr
, "[error] Writing event failed.\n");
646 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
653 bt_ctf_iter_destroy(iter
);
658 int main(int argc
, char **argv
)
660 int ret
, partial_error
= 0, open_success
= 0;
661 struct bt_format
*fmt_write
;
662 struct bt_trace_descriptor
*td_write
;
663 struct bt_context
*ctx
;
666 opt_input_paths
= g_ptr_array_new();
668 ret
= parse_options(argc
, argv
);
670 fprintf(stderr
, "Error parsing options.\n\n");
672 g_ptr_array_free(opt_input_paths
, TRUE
);
674 } else if (ret
> 0) {
675 g_ptr_array_free(opt_input_paths
, TRUE
);
678 printf_verbose("Verbose mode active.\n");
679 printf_debug("Debug mode active.\n");
681 if (opt_input_format
)
682 strlower(opt_input_format
);
683 if (opt_output_format
)
684 strlower(opt_output_format
);
686 printf_verbose("Converting from directory(ies):\n");
687 for (i
= 0; i
< opt_input_paths
->len
; i
++) {
688 const char *ipath
= g_ptr_array_index(opt_input_paths
, i
);
689 printf_verbose(" %s\n", ipath
);
691 printf_verbose("Converting from format: %s\n",
692 opt_input_format
? : "ctf <default>");
693 printf_verbose("Converting to target: %s\n",
694 opt_output_path
? : "<stdout>");
695 printf_verbose("Converting to format: %s\n",
696 opt_output_format
? : "text <default>");
698 if (!opt_input_format
) {
699 opt_input_format
= strdup("ctf");
700 if (!opt_input_format
) {
705 if (!opt_output_format
) {
706 opt_output_format
= strdup("text");
707 if (!opt_output_format
) {
712 fmt_read
= bt_lookup_format(g_quark_from_static_string(opt_input_format
));
714 fprintf(stderr
, "[error] Format \"%s\" is not supported.\n\n",
719 fmt_write
= bt_lookup_format(g_quark_from_static_string(opt_output_format
));
721 fprintf(stderr
, "[error] format \"%s\" is not supported.\n\n",
727 ctx
= bt_context_create();
732 for (i
= 0; i
< opt_input_paths
->len
; i
++) {
733 const char *ipath
= g_ptr_array_index(opt_input_paths
, i
);
734 ret
= bt_context_add_traces_recursive(ctx
, ipath
,
735 opt_input_format
, NULL
);
737 fprintf(stderr
, "[error] opening trace \"%s\" for reading.\n\n",
739 } else if (ret
> 0) {
740 fprintf(stderr
, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n",
742 open_success
= 1; /* some traces were OK */
745 open_success
= 1; /* all traces were OK */
749 fprintf(stderr
, "[error] none of the specified trace paths could be opened.\n\n");
753 td_write
= fmt_write
->open_trace(opt_output_path
, O_RDWR
, NULL
, NULL
);
755 fprintf(stderr
, "Error opening trace \"%s\" for writing.\n\n",
756 opt_output_path
? : "<none>");
761 * Errors happened when opening traces, but we continue anyway.
762 * sleep to let user see the stderr output before stdout.
765 sleep(PARTIAL_ERROR_SLEEP
);
767 ret
= trace_pre_handler(td_write
, ctx
);
769 fprintf(stderr
, "Error in trace pre handle.\n\n");
770 goto error_copy_trace
;
773 /* For now, we support only CTF iterators */
774 if (fmt_read
->name
== g_quark_from_static_string("ctf")) {
775 ret
= convert_trace(td_write
, ctx
);
777 fprintf(stderr
, "Error printing trace.\n\n");
778 goto error_copy_trace
;
782 ret
= trace_post_handler(td_write
, ctx
);
784 fprintf(stderr
, "Error in trace post handle.\n\n");
785 goto error_copy_trace
;
788 fmt_write
->close_trace(td_write
);
791 printf_verbose("finished converting. Output written to:\n%s\n",
792 opt_output_path
? : "<stdout>");
797 fmt_write
->close_trace(td_write
);
803 /* teardown and exit */
805 free(opt_input_format
);
806 free(opt_output_format
);
807 free(opt_output_path
);
808 g_ptr_array_free(opt_input_paths
, TRUE
);