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
50 static char *opt_input_format
, *opt_output_format
;
51 /* Pointer into const argv */
52 static const char *opt_input_format_arg
, *opt_output_format_arg
;
54 static const char *opt_input_path
;
55 static const char *opt_output_path
;
57 static struct format
*fmt_read
;
60 void strlower(char *str
)
63 *str
= tolower((int) *str
);
82 OPT_CLOCK_FORCE_CORRELATE
,
85 static struct poptOption long_options
[] = {
86 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
87 { "input-format", 'i', POPT_ARG_STRING
, &opt_input_format_arg
, OPT_NONE
, NULL
, NULL
},
88 { "output-format", 'o', POPT_ARG_STRING
, &opt_output_format_arg
, OPT_NONE
, NULL
, NULL
},
89 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
90 { "list", 'l', POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
91 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
92 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
93 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
94 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
95 { "no-delta", 0, POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
96 { "clock-offset", 0, POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
97 { "clock-cycles", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_CYCLES
, NULL
, NULL
},
98 { "clock-seconds", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
99 { "clock-date", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
100 { "clock-gmt", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
101 { "clock-force-correlate", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
102 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
105 static void list_formats(FILE *fp
)
108 bt_fprintf_format_list(fp
);
111 static void usage(FILE *fp
)
113 fprintf(fp
, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION
);
114 fprintf(fp
, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
116 fprintf(fp
, " INPUT Input trace path\n");
117 fprintf(fp
, " OUTPUT Output trace path (default: stdout)\n");
119 fprintf(fp
, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
120 fprintf(fp
, " -o, --output-format FORMAT Output trace format (default: text)\n");
122 fprintf(fp
, " -h, --help This help message\n");
123 fprintf(fp
, " -l, --list List available formats\n");
124 fprintf(fp
, " -v, --verbose Verbose mode\n");
125 fprintf(fp
, " (or set BABELTRACE_VERBOSE environment variable)\n");
126 fprintf(fp
, " -d, --debug Debug mode\n");
127 fprintf(fp
, " (or set BABELTRACE_DEBUG environment variable)\n");
128 fprintf(fp
, " --no-delta Do not print time delta between consecutive events\n");
129 fprintf(fp
, " -n, --names name1<,name2,...> Print field names:\n");
130 fprintf(fp
, " (payload OR args OR arg)\n");
131 fprintf(fp
, " none, all, scope, header, (context OR ctx)\n");
132 fprintf(fp
, " (default: payload,context)\n");
133 fprintf(fp
, " -f, --fields name1<,name2,...> Print additional fields:\n");
134 fprintf(fp
, " all, trace, trace:domain, trace:procname,\n");
135 fprintf(fp
, " trace:vpid, loglevel.\n");
136 fprintf(fp
, " --clock-cycles Timestamp in cycles\n");
137 fprintf(fp
, " --clock-offset seconds Clock offset in seconds\n");
138 fprintf(fp
, " --clock-seconds Print the timestamps as [sec.ns]\n");
139 fprintf(fp
, " (default is: [hh:mm:ss.ns])\n");
140 fprintf(fp
, " --clock-date Print clock date\n");
141 fprintf(fp
, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n");
142 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently correlated\n");
143 fprintf(fp
, " across traces.\n");
148 static int get_names_args(poptContext
*pc
)
150 char *str
, *strlist
, *strctx
;
152 opt_payload_field_names
= 0;
153 opt_context_field_names
= 0;
154 strlist
= (char *) poptGetOptArg(*pc
);
158 str
= strtok_r(strlist
, ",", &strctx
);
160 if (!strcmp(str
, "all"))
161 opt_all_field_names
= 1;
162 else if (!strcmp(str
, "scope"))
163 opt_scope_field_names
= 1;
164 else if (!strcmp(str
, "context") || !strcmp(str
, "ctx"))
165 opt_context_field_names
= 1;
166 else if (!strcmp(str
, "header"))
167 opt_header_field_names
= 1;
168 else if (!strcmp(str
, "payload") || !strcmp(str
, "args") || !strcmp(str
, "arg"))
169 opt_payload_field_names
= 1;
170 else if (!strcmp(str
, "none")) {
171 opt_all_field_names
= 0;
172 opt_scope_field_names
= 0;
173 opt_context_field_names
= 0;
174 opt_header_field_names
= 0;
175 opt_payload_field_names
= 0;
177 fprintf(stderr
, "[error] unknown field name type %s\n", str
);
180 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
184 static int get_fields_args(poptContext
*pc
)
186 char *str
, *strlist
, *strctx
;
188 strlist
= (char *) poptGetOptArg(*pc
);
192 str
= strtok_r(strlist
, ",", &strctx
);
194 if (!strcmp(str
, "all"))
196 else if (!strcmp(str
, "trace"))
198 else if (!strcmp(str
, "trace:domain"))
199 opt_trace_domain_field
= 1;
200 else if (!strcmp(str
, "trace:procname"))
201 opt_trace_procname_field
= 1;
202 else if (!strcmp(str
, "trace:vpid"))
203 opt_trace_vpid_field
= 1;
204 else if (!strcmp(str
, "loglevel"))
205 opt_loglevel_field
= 1;
207 fprintf(stderr
, "[error] unknown field type %s\n", str
);
210 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
215 * Return 0 if caller should continue, < 0 if caller should return
216 * error, > 0 if caller should exit without reporting error.
218 static int parse_options(int argc
, char **argv
)
225 return 1; /* exit cleanly */
228 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
229 poptReadDefaultConfig(pc
, 0);
232 opt_context_field_names
= 1;
233 opt_payload_field_names
= 1;
235 while ((opt
= poptGetNextOpt(pc
)) != -1) {
239 ret
= 1; /* exit cleanly */
242 list_formats(stdout
);
246 babeltrace_verbose
= 1;
249 if (get_names_args(&pc
)) {
255 if (get_fields_args(&pc
)) {
261 babeltrace_debug
= 1;
266 case OPT_CLOCK_CYCLES
:
267 opt_clock_cycles
= 1;
269 case OPT_CLOCK_OFFSET
:
274 str
= poptGetOptArg(pc
);
276 fprintf(stderr
, "[error] Missing --clock-offset argument\n");
281 opt_clock_offset
= strtoull(str
, &endptr
, 0);
282 if (*endptr
!= '\0' || str
== endptr
|| errno
!= 0) {
283 fprintf(stderr
, "[error] Incorrect --clock-offset argument: %s\n", str
);
289 case OPT_CLOCK_SECONDS
:
290 opt_clock_seconds
= 1;
298 case OPT_CLOCK_FORCE_CORRELATE
:
299 opt_clock_force_correlate
= 1;
308 opt_input_path
= poptGetArg(pc
);
309 if (!opt_input_path
) {
313 opt_output_path
= poptGetArg(pc
);
322 static GPtrArray
*traversed_paths
= 0;
325 * traverse_trace_dir() is the callback function for File Tree Walk (nftw).
326 * it receives the path of the current entry (file, dir, link..etc) with
327 * a flag to indicate the type of the entry.
328 * if the entry being visited is a directory and contains a metadata file,
329 * then add the path to a global list to be processed later in
330 * add_traces_recursive.
332 static int traverse_trace_dir(const char *fpath
, const struct stat
*sb
,
333 int tflag
, struct FTW
*ftwbuf
)
341 dirfd
= open(fpath
, 0);
343 fprintf(stderr
, "[error] [Context] Unable to open trace "
344 "directory file descriptor.\n");
345 return 0; /* partial error */
347 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
349 closeret
= close(dirfd
);
354 /* No meta data, just return */
357 closeret
= close(metafd
);
360 return -1; /* failure */
362 closeret
= close(dirfd
);
365 return -1; /* failure */
368 /* Add path to the global list */
369 if (traversed_paths
== NULL
) {
370 fprintf(stderr
, "[error] [Context] Invalid open path array.\n");
373 g_ptr_array_add(traversed_paths
, g_string_new(fpath
));
379 * bt_context_add_traces_recursive: Open a trace recursively
381 * Find each trace present in the subdirectory starting from the given
382 * path, and add them to the context. The packet_seek parameter can be
383 * NULL: this specify to use the default format packet_seek.
385 * Return: 0 on success, < 0 on failure, > 0 on partial failure.
386 * Unable to open toplevel: failure.
387 * Unable to open some subdirectory or file: warn and continue (partial
390 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
391 const char *format_str
,
392 void (*packet_seek
)(struct stream_pos
*pos
,
393 size_t offset
, int whence
))
400 /* Should lock traversed_paths mutex here if used in multithread */
402 traversed_paths
= g_ptr_array_new();
403 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
405 ret
= nftw(path
, traverse_trace_dir
, 10, 0);
407 /* Process the array if ntfw did not return a fatal error */
409 for (i
= 0; i
< traversed_paths
->len
; i
++) {
410 GString
*trace_path
= g_ptr_array_index(traversed_paths
,
412 int trace_id
= bt_context_add_trace(ctx
,
419 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" from %s "
420 "for reading.\n", trace_path
->str
, path
);
421 /* Allow to skip erroneous traces. */
422 ret
= 1; /* partial error */
424 g_array_append_val(trace_ids
, trace_id
);
426 g_string_free(trace_path
, TRUE
);
429 g_ptr_array_free(traversed_paths
, TRUE
);
430 traversed_paths
= NULL
;
432 /* Should unlock traversed paths mutex here if used in multithread */
435 * Return an error if no trace can be opened.
437 if (trace_ids
->len
== 0) {
438 fprintf(stderr
, "[error] Cannot open any trace for reading.\n\n");
439 ret
= -ENOENT
; /* failure */
441 g_array_free(trace_ids
, TRUE
);
445 int convert_trace(struct trace_descriptor
*td_write
,
446 struct bt_context
*ctx
)
448 struct bt_ctf_iter
*iter
;
449 struct ctf_text_stream_pos
*sout
;
450 struct bt_iter_pos begin_pos
;
451 struct bt_ctf_event
*ctf_event
;
454 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
457 begin_pos
.type
= BT_SEEK_BEGIN
;
458 iter
= bt_ctf_iter_create(ctx
, &begin_pos
, NULL
);
463 while ((ctf_event
= bt_ctf_iter_read_event(iter
))) {
464 ret
= sout
->parent
.event_cb(&sout
->parent
, ctf_event
->parent
->stream
);
466 fprintf(stderr
, "[error] Writing event failed.\n");
469 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
476 bt_ctf_iter_destroy(iter
);
481 int main(int argc
, char **argv
)
483 int ret
, partial_error
= 0;
484 struct format
*fmt_write
;
485 struct trace_descriptor
*td_write
;
486 struct bt_context
*ctx
;
488 ret
= parse_options(argc
, argv
);
490 fprintf(stderr
, "Error parsing options.\n\n");
493 } else if (ret
> 0) {
496 printf_verbose("Verbose mode active.\n");
497 printf_debug("Debug mode active.\n");
499 if (opt_input_format_arg
) {
500 opt_input_format
= strdup(opt_input_format_arg
);
501 if (!opt_input_format
) {
505 strlower(opt_input_format
);
507 if (opt_output_format_arg
) {
508 opt_output_format
= strdup(opt_output_format_arg
);
509 if (!opt_output_format
) {
513 strlower(opt_output_format
);
516 printf_verbose("Converting from directory: %s\n", opt_input_path
);
517 printf_verbose("Converting from format: %s\n",
518 opt_input_format
? : "ctf <default>");
519 printf_verbose("Converting to directory: %s\n",
520 opt_output_path
? : "<stdout>");
521 printf_verbose("Converting to format: %s\n",
522 opt_output_format
? : "text <default>");
524 if (!opt_input_format
) {
525 opt_input_format
= strdup("ctf");
526 if (!opt_input_format
) {
531 if (!opt_output_format
) {
532 opt_output_format
= strdup("text");
533 if (!opt_output_format
) {
538 fmt_read
= bt_lookup_format(g_quark_from_static_string(opt_input_format
));
540 fprintf(stderr
, "[error] Format \"%s\" is not supported.\n\n",
545 fmt_write
= bt_lookup_format(g_quark_from_static_string(opt_output_format
));
547 fprintf(stderr
, "[error] format \"%s\" is not supported.\n\n",
553 ctx
= bt_context_create();
558 ret
= bt_context_add_traces_recursive(ctx
, opt_input_path
,
559 opt_input_format
, NULL
);
561 fprintf(stderr
, "[error] opening trace \"%s\" for reading.\n\n",
564 } else if (ret
> 0) {
565 fprintf(stderr
, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n",
570 td_write
= fmt_write
->open_trace(opt_output_path
, O_RDWR
, NULL
, NULL
);
572 fprintf(stderr
, "Error opening trace \"%s\" for writing.\n\n",
573 opt_output_path
? : "<none>");
578 * Errors happened when opening traces, but we continue anyway.
579 * sleep to let user see the stderr output before stdout.
582 sleep(PARTIAL_ERROR_SLEEP
);
585 ret
= convert_trace(td_write
, ctx
);
587 fprintf(stderr
, "Error printing trace.\n\n");
588 goto error_copy_trace
;
591 fmt_write
->close_trace(td_write
);
594 printf_verbose("finished converting. Output written to:\n%s\n",
595 opt_output_path
? : "<stdout>");
600 fmt_write
->close_trace(td_write
);
606 /* teardown and exit */
608 free(opt_input_format
);
609 free(opt_output_format
);