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 DEFAULT_FILE_ARRAY_SIZE 1
48 static char *opt_input_format
, *opt_output_format
;
49 /* Pointer into const argv */
50 static const char *opt_input_format_arg
, *opt_output_format_arg
;
52 static const char *opt_input_path
;
53 static const char *opt_output_path
;
55 static struct format
*fmt_read
;
58 void strlower(char *str
)
61 *str
= tolower((int) *str
);
80 OPT_CLOCK_FORCE_CORRELATE
,
83 static struct poptOption long_options
[] = {
84 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
85 { "input-format", 'i', POPT_ARG_STRING
, &opt_input_format_arg
, OPT_NONE
, NULL
, NULL
},
86 { "output-format", 'o', POPT_ARG_STRING
, &opt_output_format_arg
, OPT_NONE
, NULL
, NULL
},
87 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
88 { "list", 'l', POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
89 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
90 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
91 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
92 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
93 { "no-delta", 0, POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
94 { "clock-offset", 0, POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
95 { "clock-cycles", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_CYCLES
, NULL
, NULL
},
96 { "clock-seconds", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
97 { "clock-date", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
98 { "clock-gmt", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
99 { "clock-force-correlate", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
100 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
103 static void list_formats(FILE *fp
)
106 bt_fprintf_format_list(fp
);
109 static void usage(FILE *fp
)
111 fprintf(fp
, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION
);
112 fprintf(fp
, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
114 fprintf(fp
, " INPUT Input trace path\n");
115 fprintf(fp
, " OUTPUT Output trace path (default: stdout)\n");
117 fprintf(fp
, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
118 fprintf(fp
, " -o, --output-format FORMAT Output trace format (default: text)\n");
120 fprintf(fp
, " -h, --help This help message\n");
121 fprintf(fp
, " -l, --list List available formats\n");
122 fprintf(fp
, " -v, --verbose Verbose mode\n");
123 fprintf(fp
, " (or set BABELTRACE_VERBOSE environment variable)\n");
124 fprintf(fp
, " -d, --debug Debug mode\n");
125 fprintf(fp
, " (or set BABELTRACE_DEBUG environment variable)\n");
126 fprintf(fp
, " --no-delta Do not print time delta between consecutive events\n");
127 fprintf(fp
, " -n, --names name1<,name2,...> Print field names:\n");
128 fprintf(fp
, " (payload OR args OR arg)\n");
129 fprintf(fp
, " none, all, scope, header, (context OR ctx)\n");
130 fprintf(fp
, " (default: payload,context)\n");
131 fprintf(fp
, " -f, --fields name1<,name2,...> Print additional fields:\n");
132 fprintf(fp
, " all, trace, trace:domain, trace:procname,\n");
133 fprintf(fp
, " trace:vpid, loglevel.\n");
134 fprintf(fp
, " --clock-cycles Timestamp in cycles\n");
135 fprintf(fp
, " --clock-offset seconds Clock offset in seconds\n");
136 fprintf(fp
, " --clock-seconds Print the timestamps as [sec.ns]\n");
137 fprintf(fp
, " (default is: [hh:mm:ss.ns])\n");
138 fprintf(fp
, " --clock-date Print clock date\n");
139 fprintf(fp
, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n");
140 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently correlated\n");
141 fprintf(fp
, " across traces.\n");
146 static int get_names_args(poptContext
*pc
)
148 char *str
, *strlist
, *strctx
;
150 opt_payload_field_names
= 0;
151 opt_context_field_names
= 0;
152 strlist
= (char *) poptGetOptArg(*pc
);
156 str
= strtok_r(strlist
, ",", &strctx
);
158 if (!strcmp(str
, "all"))
159 opt_all_field_names
= 1;
160 else if (!strcmp(str
, "scope"))
161 opt_scope_field_names
= 1;
162 else if (!strcmp(str
, "context") || !strcmp(str
, "ctx"))
163 opt_context_field_names
= 1;
164 else if (!strcmp(str
, "header"))
165 opt_header_field_names
= 1;
166 else if (!strcmp(str
, "payload") || !strcmp(str
, "args") || !strcmp(str
, "arg"))
167 opt_payload_field_names
= 1;
168 else if (!strcmp(str
, "none")) {
169 opt_all_field_names
= 0;
170 opt_scope_field_names
= 0;
171 opt_context_field_names
= 0;
172 opt_header_field_names
= 0;
173 opt_payload_field_names
= 0;
175 fprintf(stderr
, "[error] unknown field name type %s\n", str
);
178 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
182 static int get_fields_args(poptContext
*pc
)
184 char *str
, *strlist
, *strctx
;
186 strlist
= (char *) poptGetOptArg(*pc
);
190 str
= strtok_r(strlist
, ",", &strctx
);
192 if (!strcmp(str
, "all"))
194 else if (!strcmp(str
, "trace"))
196 else if (!strcmp(str
, "trace:domain"))
197 opt_trace_domain_field
= 1;
198 else if (!strcmp(str
, "trace:procname"))
199 opt_trace_procname_field
= 1;
200 else if (!strcmp(str
, "trace:vpid"))
201 opt_trace_vpid_field
= 1;
202 else if (!strcmp(str
, "loglevel"))
203 opt_loglevel_field
= 1;
205 fprintf(stderr
, "[error] unknown field type %s\n", str
);
208 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
213 * Return 0 if caller should continue, < 0 if caller should return
214 * error, > 0 if caller should exit without reporting error.
216 static int parse_options(int argc
, char **argv
)
223 return 1; /* exit cleanly */
226 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
227 poptReadDefaultConfig(pc
, 0);
230 opt_context_field_names
= 1;
231 opt_payload_field_names
= 1;
233 while ((opt
= poptGetNextOpt(pc
)) != -1) {
237 ret
= 1; /* exit cleanly */
240 list_formats(stdout
);
244 babeltrace_verbose
= 1;
247 if (get_names_args(&pc
)) {
253 if (get_fields_args(&pc
)) {
259 babeltrace_debug
= 1;
264 case OPT_CLOCK_CYCLES
:
265 opt_clock_cycles
= 1;
267 case OPT_CLOCK_OFFSET
:
272 str
= poptGetOptArg(pc
);
274 fprintf(stderr
, "[error] Missing --clock-offset argument\n");
279 opt_clock_offset
= strtoull(str
, &endptr
, 0);
280 if (*endptr
!= '\0' || str
== endptr
|| errno
!= 0) {
281 fprintf(stderr
, "[error] Incorrect --clock-offset argument: %s\n", str
);
287 case OPT_CLOCK_SECONDS
:
288 opt_clock_seconds
= 1;
296 case OPT_CLOCK_FORCE_CORRELATE
:
297 opt_clock_force_correlate
= 1;
306 opt_input_path
= poptGetArg(pc
);
307 if (!opt_input_path
) {
311 opt_output_path
= poptGetArg(pc
);
320 static GPtrArray
*traversed_paths
= 0;
323 * traverse_trace_dir() is the callback function for File Tree Walk (nftw).
324 * it receives the path of the current entry (file, dir, link..etc) with
325 * a flag to indicate the type of the entry.
326 * if the entry being visited is a directory and contains a metadata file,
327 * then add the path to a global list to be processed later in
328 * add_traces_recursive.
330 static int traverse_trace_dir(const char *fpath
, const struct stat
*sb
,
331 int tflag
, struct FTW
*ftwbuf
)
339 dirfd
= open(fpath
, 0);
341 fprintf(stderr
, "[error] [Context] Unable to open trace "
342 "directory file descriptor.\n");
343 return 0; /* partial error */
345 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
347 closeret
= close(dirfd
);
352 /* No meta data, just return */
355 closeret
= close(metafd
);
358 return -1; /* failure */
360 closeret
= close(dirfd
);
363 return -1; /* failure */
366 /* Add path to the global list */
367 if (traversed_paths
== NULL
) {
368 fprintf(stderr
, "[error] [Context] Invalid open path array.\n");
371 g_ptr_array_add(traversed_paths
, g_string_new(fpath
));
377 * bt_context_add_traces_recursive: Open a trace recursively
379 * Find each trace present in the subdirectory starting from the given
380 * path, and add them to the context. The packet_seek parameter can be
381 * NULL: this specify to use the default format packet_seek.
383 * Return: 0 on success, < 0 on failure, > 0 on partial failure.
384 * Unable to open toplevel: failure.
385 * Unable to open some subdirectory or file: warn and continue (partial
388 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
389 const char *format_str
,
390 void (*packet_seek
)(struct stream_pos
*pos
,
391 size_t offset
, int whence
))
398 /* Should lock traversed_paths mutex here if used in multithread */
400 traversed_paths
= g_ptr_array_new();
401 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
403 ret
= nftw(path
, traverse_trace_dir
, 10, 0);
405 /* Process the array if ntfw did not return a fatal error */
407 for (i
= 0; i
< traversed_paths
->len
; i
++) {
408 GString
*trace_path
= g_ptr_array_index(traversed_paths
,
410 int trace_id
= bt_context_add_trace(ctx
,
417 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" from %s "
418 "for reading.\n", trace_path
->str
, path
);
419 /* Allow to skip erroneous traces. */
420 ret
= 1; /* partial error */
422 g_array_append_val(trace_ids
, trace_id
);
424 g_string_free(trace_path
, TRUE
);
427 g_ptr_array_free(traversed_paths
, TRUE
);
428 traversed_paths
= NULL
;
430 /* Should unlock traversed paths mutex here if used in multithread */
433 * Return an error if no trace can be opened.
435 if (trace_ids
->len
== 0) {
436 fprintf(stderr
, "[error] Cannot open any trace for reading.\n\n");
437 ret
= -ENOENT
; /* failure */
439 g_array_free(trace_ids
, TRUE
);
443 int convert_trace(struct trace_descriptor
*td_write
,
444 struct bt_context
*ctx
)
446 struct bt_ctf_iter
*iter
;
447 struct ctf_text_stream_pos
*sout
;
448 struct bt_iter_pos begin_pos
;
449 struct bt_ctf_event
*ctf_event
;
452 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
455 begin_pos
.type
= BT_SEEK_BEGIN
;
456 iter
= bt_ctf_iter_create(ctx
, &begin_pos
, NULL
);
461 while ((ctf_event
= bt_ctf_iter_read_event(iter
))) {
462 ret
= sout
->parent
.event_cb(&sout
->parent
, ctf_event
->parent
->stream
);
464 fprintf(stderr
, "[error] Writing event failed.\n");
467 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
474 bt_ctf_iter_destroy(iter
);
479 int main(int argc
, char **argv
)
481 int ret
, partial_error
= 0;
482 struct format
*fmt_write
;
483 struct trace_descriptor
*td_write
;
484 struct bt_context
*ctx
;
486 ret
= parse_options(argc
, argv
);
488 fprintf(stderr
, "Error parsing options.\n\n");
491 } else if (ret
> 0) {
494 printf_verbose("Verbose mode active.\n");
495 printf_debug("Debug mode active.\n");
497 if (opt_input_format_arg
) {
498 opt_input_format
= strdup(opt_input_format_arg
);
499 if (!opt_input_format
) {
503 strlower(opt_input_format
);
505 if (opt_output_format_arg
) {
506 opt_output_format
= strdup(opt_output_format_arg
);
507 if (!opt_output_format
) {
511 strlower(opt_output_format
);
514 printf_verbose("Converting from directory: %s\n", opt_input_path
);
515 printf_verbose("Converting from format: %s\n",
516 opt_input_format
? : "ctf <default>");
517 printf_verbose("Converting to directory: %s\n",
518 opt_output_path
? : "<stdout>");
519 printf_verbose("Converting to format: %s\n",
520 opt_output_format
? : "text <default>");
522 if (!opt_input_format
) {
523 opt_input_format
= strdup("ctf");
524 if (!opt_input_format
) {
529 if (!opt_output_format
) {
530 opt_output_format
= strdup("text");
531 if (!opt_output_format
) {
536 fmt_read
= bt_lookup_format(g_quark_from_static_string(opt_input_format
));
538 fprintf(stderr
, "[error] Format \"%s\" is not supported.\n\n",
543 fmt_write
= bt_lookup_format(g_quark_from_static_string(opt_output_format
));
545 fprintf(stderr
, "[error] format \"%s\" is not supported.\n\n",
551 ctx
= bt_context_create();
556 ret
= bt_context_add_traces_recursive(ctx
, opt_input_path
,
557 opt_input_format
, NULL
);
559 fprintf(stderr
, "[error] opening trace \"%s\" for reading.\n\n",
562 } else if (ret
> 0) {
563 fprintf(stderr
, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n",
568 td_write
= fmt_write
->open_trace(opt_output_path
, O_RDWR
, NULL
, NULL
);
570 fprintf(stderr
, "Error opening trace \"%s\" for writing.\n\n",
571 opt_output_path
? : "<none>");
575 ret
= convert_trace(td_write
, ctx
);
577 fprintf(stderr
, "Error printing trace.\n\n");
578 goto error_copy_trace
;
581 fmt_write
->close_trace(td_write
);
584 printf_verbose("finished converting. Output written to:\n%s\n",
585 opt_output_path
? : "<stdout>");
590 fmt_write
->close_trace(td_write
);
596 /* teardown and exit */
598 free(opt_input_format
);
599 free(opt_output_format
);