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.
23 #include <babeltrace/babeltrace.h>
24 #include <babeltrace/format.h>
25 #include <babeltrace/context.h>
26 #include <babeltrace/ctf/types.h>
27 #include <babeltrace/ctf/events.h>
28 /* TODO: fix object model for format-agnostic callbacks */
29 #include <babeltrace/ctf/events-internal.h>
30 #include <babeltrace/ctf/iterator.h>
31 #include <babeltrace/ctf-text/types.h>
32 #include <babeltrace/iterator.h>
38 #include <sys/types.h>
45 #include <babeltrace/ctf-ir/metadata.h> /* for clocks */
47 #define PARTIAL_ERROR_SLEEP 3 /* 3 seconds */
49 #define DEFAULT_FILE_ARRAY_SIZE 1
51 static char *opt_input_format
, *opt_output_format
;
54 * We are not freeing opt_input_paths ipath elements when exiting from
55 * main() for backward compatibility with libpop 0.13, which does not
56 * allocate copies for arguments returned by poptGetArg(), and for
57 * general compatibility with the documented behavior. This is known to
58 * cause a small memory leak with libpop 0.16.
60 static GPtrArray
*opt_input_paths
;
61 static char *opt_output_path
;
63 static struct format
*fmt_read
;
66 void strlower(char *str
)
69 *str
= tolower((int) *str
);
91 OPT_CLOCK_FORCE_CORRELATE
,
95 * We are _not_ using POPT_ARG_STRING ability to store directly into
96 * variables, because we want to cast the return to non-const, which is
97 * not possible without using poptGetOptArg explicitly. This helps us
98 * controlling memory allocation correctly without making assumptions
99 * about undocumented behaviors. poptGetOptArg is documented as
100 * requiring the returned const char * to be freed by the caller.
102 static struct poptOption long_options
[] = {
103 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
104 { "output", 'w', POPT_ARG_STRING
, NULL
, OPT_NONE
, NULL
, NULL
},
105 { "input-format", 'i', POPT_ARG_STRING
, NULL
, OPT_INPUT_FORMAT
, NULL
, NULL
},
106 { "output-format", 'o', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_FORMAT
, NULL
, NULL
},
107 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
108 { "list", 'l', POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
109 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
110 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
111 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
112 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
113 { "no-delta", 0, POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
114 { "clock-offset", 0, POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
115 { "clock-cycles", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_CYCLES
, NULL
, NULL
},
116 { "clock-seconds", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
117 { "clock-date", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
118 { "clock-gmt", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
119 { "clock-force-correlate", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
120 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
123 static void list_formats(FILE *fp
)
126 bt_fprintf_format_list(fp
);
129 static void usage(FILE *fp
)
131 fprintf(fp
, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION
);
132 fprintf(fp
, "usage : babeltrace [OPTIONS] FILE...\n");
134 fprintf(fp
, " FILE Input trace file(s) and/or directory(ies)\n");
135 fprintf(fp
, " (space-separated)\n");
136 fprintf(fp
, " -w, --output OUTPUT Output trace path (default: stdout)\n");
138 fprintf(fp
, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
139 fprintf(fp
, " -o, --output-format FORMAT Output trace format (default: text)\n");
141 fprintf(fp
, " -h, --help This help message\n");
142 fprintf(fp
, " -l, --list List available formats\n");
143 fprintf(fp
, " -v, --verbose Verbose mode\n");
144 fprintf(fp
, " (or set BABELTRACE_VERBOSE environment variable)\n");
145 fprintf(fp
, " -d, --debug Debug mode\n");
146 fprintf(fp
, " (or set BABELTRACE_DEBUG environment variable)\n");
147 fprintf(fp
, " --no-delta Do not print time delta between consecutive events\n");
148 fprintf(fp
, " -n, --names name1<,name2,...> Print field names:\n");
149 fprintf(fp
, " (payload OR args OR arg)\n");
150 fprintf(fp
, " none, all, scope, header, (context OR ctx)\n");
151 fprintf(fp
, " (default: payload,context)\n");
152 fprintf(fp
, " -f, --fields name1<,name2,...> Print additional fields:\n");
153 fprintf(fp
, " all, trace, trace:hostname, trace:domain,\n");
154 fprintf(fp
, " trace:procname, trace:vpid, loglevel, emf, callsite.\n");
155 fprintf(fp
, " (default: trace:hostname,trace:procname,trace:vpid)\n");
156 fprintf(fp
, " --clock-cycles Timestamp in cycles\n");
157 fprintf(fp
, " --clock-offset seconds Clock offset in seconds\n");
158 fprintf(fp
, " --clock-seconds Print the timestamps as [sec.ns]\n");
159 fprintf(fp
, " (default is: [hh:mm:ss.ns])\n");
160 fprintf(fp
, " --clock-date Print clock date\n");
161 fprintf(fp
, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n");
162 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently correlated\n");
163 fprintf(fp
, " across traces.\n");
168 static int get_names_args(poptContext
*pc
)
170 char *str
, *strlist
, *strctx
;
173 opt_payload_field_names
= 0;
174 opt_context_field_names
= 0;
175 strlist
= (char *) poptGetOptArg(*pc
);
179 str
= strtok_r(strlist
, ",", &strctx
);
181 if (!strcmp(str
, "all"))
182 opt_all_field_names
= 1;
183 else if (!strcmp(str
, "scope"))
184 opt_scope_field_names
= 1;
185 else if (!strcmp(str
, "context") || !strcmp(str
, "ctx"))
186 opt_context_field_names
= 1;
187 else if (!strcmp(str
, "header"))
188 opt_header_field_names
= 1;
189 else if (!strcmp(str
, "payload") || !strcmp(str
, "args") || !strcmp(str
, "arg"))
190 opt_payload_field_names
= 1;
191 else if (!strcmp(str
, "none")) {
192 opt_all_field_names
= 0;
193 opt_scope_field_names
= 0;
194 opt_context_field_names
= 0;
195 opt_header_field_names
= 0;
196 opt_payload_field_names
= 0;
198 fprintf(stderr
, "[error] unknown field name type %s\n", str
);
203 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
209 static int get_fields_args(poptContext
*pc
)
211 char *str
, *strlist
, *strctx
;
214 strlist
= (char *) poptGetOptArg(*pc
);
218 str
= strtok_r(strlist
, ",", &strctx
);
220 opt_trace_default_fields
= 0;
221 if (!strcmp(str
, "all"))
223 else if (!strcmp(str
, "trace"))
225 else if (!strcmp(str
, "trace:hostname"))
226 opt_trace_hostname_field
= 1;
227 else if (!strcmp(str
, "trace:domain"))
228 opt_trace_domain_field
= 1;
229 else if (!strcmp(str
, "trace:procname"))
230 opt_trace_procname_field
= 1;
231 else if (!strcmp(str
, "trace:vpid"))
232 opt_trace_vpid_field
= 1;
233 else if (!strcmp(str
, "loglevel"))
234 opt_loglevel_field
= 1;
235 else if (!strcmp(str
, "emf"))
237 else if (!strcmp(str
, "callsite"))
238 opt_callsite_field
= 1;
240 fprintf(stderr
, "[error] unknown field type %s\n", str
);
244 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
251 * Return 0 if caller should continue, < 0 if caller should return
252 * error, > 0 if caller should exit without reporting error.
254 static int parse_options(int argc
, char **argv
)
262 return 1; /* exit cleanly */
265 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
266 poptReadDefaultConfig(pc
, 0);
269 opt_context_field_names
= 1;
270 opt_payload_field_names
= 1;
272 while ((opt
= poptGetNextOpt(pc
)) != -1) {
274 case OPT_OUTPUT_PATH
:
275 opt_output_path
= (char *) poptGetOptArg(pc
);
276 if (!opt_output_path
) {
281 case OPT_INPUT_FORMAT
:
282 opt_input_format
= (char *) poptGetOptArg(pc
);
283 if (!opt_input_format
) {
288 case OPT_OUTPUT_FORMAT
:
289 opt_output_format
= (char *) poptGetOptArg(pc
);
290 if (!opt_output_format
) {
297 ret
= 1; /* exit cleanly */
300 list_formats(stdout
);
304 babeltrace_verbose
= 1;
307 if (get_names_args(&pc
)) {
313 if (get_fields_args(&pc
)) {
319 babeltrace_debug
= 1;
324 case OPT_CLOCK_CYCLES
:
325 opt_clock_cycles
= 1;
327 case OPT_CLOCK_OFFSET
:
332 str
= (char *) poptGetOptArg(pc
);
334 fprintf(stderr
, "[error] Missing --clock-offset argument\n");
339 opt_clock_offset
= strtoull(str
, &endptr
, 0);
340 if (*endptr
!= '\0' || str
== endptr
|| errno
!= 0) {
341 fprintf(stderr
, "[error] Incorrect --clock-offset argument: %s\n", str
);
349 case OPT_CLOCK_SECONDS
:
350 opt_clock_seconds
= 1;
358 case OPT_CLOCK_FORCE_CORRELATE
:
359 opt_clock_force_correlate
= 1;
369 ipath
= poptGetArg(pc
);
371 g_ptr_array_add(opt_input_paths
, (gpointer
) ipath
);
373 if (opt_input_paths
->len
== 0) {
385 static GPtrArray
*traversed_paths
= 0;
388 * traverse_trace_dir() is the callback function for File Tree Walk (nftw).
389 * it receives the path of the current entry (file, dir, link..etc) with
390 * a flag to indicate the type of the entry.
391 * if the entry being visited is a directory and contains a metadata file,
392 * then add the path to a global list to be processed later in
393 * add_traces_recursive.
395 static int traverse_trace_dir(const char *fpath
, const struct stat
*sb
,
396 int tflag
, struct FTW
*ftwbuf
)
404 dirfd
= open(fpath
, 0);
406 fprintf(stderr
, "[error] [Context] Unable to open trace "
407 "directory file descriptor.\n");
408 return 0; /* partial error */
410 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
412 closeret
= close(dirfd
);
417 /* No meta data, just return */
420 closeret
= close(metafd
);
423 return -1; /* failure */
425 closeret
= close(dirfd
);
428 return -1; /* failure */
431 /* Add path to the global list */
432 if (traversed_paths
== NULL
) {
433 fprintf(stderr
, "[error] [Context] Invalid open path array.\n");
436 g_ptr_array_add(traversed_paths
, g_string_new(fpath
));
443 * bt_context_add_traces_recursive: Open a trace recursively
445 * Find each trace present in the subdirectory starting from the given
446 * path, and add them to the context. The packet_seek parameter can be
447 * NULL: this specify to use the default format packet_seek.
449 * Return: 0 on success, < 0 on failure, > 0 on partial failure.
450 * Unable to open toplevel: failure.
451 * Unable to open some subdirectory or file: warn and continue (partial
454 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
455 const char *format_str
,
456 void (*packet_seek
)(struct stream_pos
*pos
,
457 size_t offset
, int whence
))
464 /* Should lock traversed_paths mutex here if used in multithread */
466 traversed_paths
= g_ptr_array_new();
467 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
469 ret
= nftw(path
, traverse_trace_dir
, 10, 0);
471 /* Process the array if ntfw did not return a fatal error */
473 for (i
= 0; i
< traversed_paths
->len
; i
++) {
474 GString
*trace_path
= g_ptr_array_index(traversed_paths
,
476 int trace_id
= bt_context_add_trace(ctx
,
483 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" from %s "
484 "for reading.\n", trace_path
->str
, path
);
485 /* Allow to skip erroneous traces. */
486 ret
= 1; /* partial error */
488 g_array_append_val(trace_ids
, trace_id
);
490 g_string_free(trace_path
, TRUE
);
494 g_ptr_array_free(traversed_paths
, TRUE
);
495 traversed_paths
= NULL
;
497 /* Should unlock traversed paths mutex here if used in multithread */
500 * Return an error if no trace can be opened.
502 if (trace_ids
->len
== 0) {
503 fprintf(stderr
, "[error] Cannot open any trace for reading.\n\n");
504 ret
= -ENOENT
; /* failure */
506 g_array_free(trace_ids
, TRUE
);
510 int convert_trace(struct trace_descriptor
*td_write
,
511 struct bt_context
*ctx
)
513 struct bt_ctf_iter
*iter
;
514 struct ctf_text_stream_pos
*sout
;
515 struct bt_iter_pos begin_pos
;
516 struct bt_ctf_event
*ctf_event
;
519 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
522 begin_pos
.type
= BT_SEEK_BEGIN
;
523 iter
= bt_ctf_iter_create(ctx
, &begin_pos
, NULL
);
528 while ((ctf_event
= bt_ctf_iter_read_event(iter
))) {
529 ret
= sout
->parent
.event_cb(&sout
->parent
, ctf_event
->parent
->stream
);
531 fprintf(stderr
, "[error] Writing event failed.\n");
534 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
541 bt_ctf_iter_destroy(iter
);
546 int main(int argc
, char **argv
)
548 int ret
, partial_error
= 0, open_success
= 0;
549 struct format
*fmt_write
;
550 struct trace_descriptor
*td_write
;
551 struct bt_context
*ctx
;
554 opt_input_paths
= g_ptr_array_new();
556 ret
= parse_options(argc
, argv
);
558 fprintf(stderr
, "Error parsing options.\n\n");
560 g_ptr_array_free(opt_input_paths
, TRUE
);
562 } else if (ret
> 0) {
563 g_ptr_array_free(opt_input_paths
, TRUE
);
566 printf_verbose("Verbose mode active.\n");
567 printf_debug("Debug mode active.\n");
569 if (opt_input_format
)
570 strlower(opt_input_format
);
571 if (opt_output_format
)
572 strlower(opt_output_format
);
574 printf_verbose("Converting from directory(ies):\n");
575 for (i
= 0; i
< opt_input_paths
->len
; i
++) {
576 const char *ipath
= g_ptr_array_index(opt_input_paths
, i
);
577 printf_verbose(" %s\n", ipath
);
579 printf_verbose("Converting from format: %s\n",
580 opt_input_format
? : "ctf <default>");
581 printf_verbose("Converting to directory: %s\n",
582 opt_output_path
? : "<stdout>");
583 printf_verbose("Converting to format: %s\n",
584 opt_output_format
? : "text <default>");
586 if (!opt_input_format
) {
587 opt_input_format
= strdup("ctf");
588 if (!opt_input_format
) {
593 if (!opt_output_format
) {
594 opt_output_format
= strdup("text");
595 if (!opt_output_format
) {
600 fmt_read
= bt_lookup_format(g_quark_from_static_string(opt_input_format
));
602 fprintf(stderr
, "[error] Format \"%s\" is not supported.\n\n",
607 fmt_write
= bt_lookup_format(g_quark_from_static_string(opt_output_format
));
609 fprintf(stderr
, "[error] format \"%s\" is not supported.\n\n",
615 ctx
= bt_context_create();
620 for (i
= 0; i
< opt_input_paths
->len
; i
++) {
621 const char *ipath
= g_ptr_array_index(opt_input_paths
, i
);
622 ret
= bt_context_add_traces_recursive(ctx
, ipath
,
623 opt_input_format
, NULL
);
625 fprintf(stderr
, "[error] opening trace \"%s\" for reading.\n\n",
627 } else if (ret
> 0) {
628 fprintf(stderr
, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n",
630 open_success
= 1; /* some traces were OK */
633 open_success
= 1; /* all traces were OK */
637 fprintf(stderr
, "[error] none of the specified trace paths could be opened.\n\n");
641 td_write
= fmt_write
->open_trace(opt_output_path
, O_RDWR
, NULL
, NULL
);
643 fprintf(stderr
, "Error opening trace \"%s\" for writing.\n\n",
644 opt_output_path
? : "<none>");
649 * Errors happened when opening traces, but we continue anyway.
650 * sleep to let user see the stderr output before stdout.
653 sleep(PARTIAL_ERROR_SLEEP
);
655 ret
= convert_trace(td_write
, ctx
);
657 fprintf(stderr
, "Error printing trace.\n\n");
658 goto error_copy_trace
;
661 fmt_write
->close_trace(td_write
);
664 printf_verbose("finished converting. Output written to:\n%s\n",
665 opt_output_path
? : "<stdout>");
670 fmt_write
->close_trace(td_write
);
676 /* teardown and exit */
678 free(opt_input_format
);
679 free(opt_output_format
);
680 free(opt_output_path
);
681 g_ptr_array_free(opt_input_paths
, TRUE
);