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 GPtrArray
*opt_input_paths
;
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 { "output", 'w', POPT_ARG_STRING
, &opt_output_path
, OPT_NONE
, NULL
, NULL
},
88 { "input-format", 'i', POPT_ARG_STRING
, &opt_input_format_arg
, OPT_NONE
, NULL
, NULL
},
89 { "output-format", 'o', POPT_ARG_STRING
, &opt_output_format_arg
, OPT_NONE
, NULL
, NULL
},
90 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
91 { "list", 'l', POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
92 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
93 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
94 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
95 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
96 { "no-delta", 0, POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
97 { "clock-offset", 0, POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
98 { "clock-cycles", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_CYCLES
, NULL
, NULL
},
99 { "clock-seconds", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
100 { "clock-date", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
101 { "clock-gmt", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
102 { "clock-force-correlate", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
103 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
106 static void list_formats(FILE *fp
)
109 bt_fprintf_format_list(fp
);
112 static void usage(FILE *fp
)
114 fprintf(fp
, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION
);
115 fprintf(fp
, "usage : babeltrace [OPTIONS] FILE...\n");
117 fprintf(fp
, " FILE Input trace file(s) and/or directory(ies)\n");
118 fprintf(fp
, " (space-separated)\n");
119 fprintf(fp
, " -w, --output OUTPUT Output trace path (default: stdout)\n");
121 fprintf(fp
, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
122 fprintf(fp
, " -o, --output-format FORMAT Output trace format (default: text)\n");
124 fprintf(fp
, " -h, --help This help message\n");
125 fprintf(fp
, " -l, --list List available formats\n");
126 fprintf(fp
, " -v, --verbose Verbose mode\n");
127 fprintf(fp
, " (or set BABELTRACE_VERBOSE environment variable)\n");
128 fprintf(fp
, " -d, --debug Debug mode\n");
129 fprintf(fp
, " (or set BABELTRACE_DEBUG environment variable)\n");
130 fprintf(fp
, " --no-delta Do not print time delta between consecutive events\n");
131 fprintf(fp
, " -n, --names name1<,name2,...> Print field names:\n");
132 fprintf(fp
, " (payload OR args OR arg)\n");
133 fprintf(fp
, " none, all, scope, header, (context OR ctx)\n");
134 fprintf(fp
, " (default: payload,context)\n");
135 fprintf(fp
, " -f, --fields name1<,name2,...> Print additional fields:\n");
136 fprintf(fp
, " all, trace, trace:hostname, trace:domain,\n");
137 fprintf(fp
, " trace:procname, trace:vpid, loglevel, emf, callsite.\n");
138 fprintf(fp
, " (default: trace:hostname,trace:procname,trace:vpid)\n");
139 fprintf(fp
, " --clock-cycles Timestamp in cycles\n");
140 fprintf(fp
, " --clock-offset seconds Clock offset in seconds\n");
141 fprintf(fp
, " --clock-seconds Print the timestamps as [sec.ns]\n");
142 fprintf(fp
, " (default is: [hh:mm:ss.ns])\n");
143 fprintf(fp
, " --clock-date Print clock date\n");
144 fprintf(fp
, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n");
145 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently correlated\n");
146 fprintf(fp
, " across traces.\n");
151 static int get_names_args(poptContext
*pc
)
153 char *str
, *strlist
, *strctx
;
156 opt_payload_field_names
= 0;
157 opt_context_field_names
= 0;
158 strlist
= (char *) poptGetOptArg(*pc
);
162 str
= strtok_r(strlist
, ",", &strctx
);
164 if (!strcmp(str
, "all"))
165 opt_all_field_names
= 1;
166 else if (!strcmp(str
, "scope"))
167 opt_scope_field_names
= 1;
168 else if (!strcmp(str
, "context") || !strcmp(str
, "ctx"))
169 opt_context_field_names
= 1;
170 else if (!strcmp(str
, "header"))
171 opt_header_field_names
= 1;
172 else if (!strcmp(str
, "payload") || !strcmp(str
, "args") || !strcmp(str
, "arg"))
173 opt_payload_field_names
= 1;
174 else if (!strcmp(str
, "none")) {
175 opt_all_field_names
= 0;
176 opt_scope_field_names
= 0;
177 opt_context_field_names
= 0;
178 opt_header_field_names
= 0;
179 opt_payload_field_names
= 0;
181 fprintf(stderr
, "[error] unknown field name type %s\n", str
);
186 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
192 static int get_fields_args(poptContext
*pc
)
194 char *str
, *strlist
, *strctx
;
197 strlist
= (char *) poptGetOptArg(*pc
);
201 str
= strtok_r(strlist
, ",", &strctx
);
203 opt_trace_default_fields
= 0;
204 if (!strcmp(str
, "all"))
206 else if (!strcmp(str
, "trace"))
208 else if (!strcmp(str
, "trace:hostname"))
209 opt_trace_hostname_field
= 1;
210 else if (!strcmp(str
, "trace:domain"))
211 opt_trace_domain_field
= 1;
212 else if (!strcmp(str
, "trace:procname"))
213 opt_trace_procname_field
= 1;
214 else if (!strcmp(str
, "trace:vpid"))
215 opt_trace_vpid_field
= 1;
216 else if (!strcmp(str
, "loglevel"))
217 opt_loglevel_field
= 1;
218 else if (!strcmp(str
, "emf"))
220 else if (!strcmp(str
, "callsite"))
221 opt_callsite_field
= 1;
223 fprintf(stderr
, "[error] unknown field type %s\n", str
);
227 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
234 * Return 0 if caller should continue, < 0 if caller should return
235 * error, > 0 if caller should exit without reporting error.
237 static int parse_options(int argc
, char **argv
)
245 return 1; /* exit cleanly */
248 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
249 poptReadDefaultConfig(pc
, 0);
252 opt_context_field_names
= 1;
253 opt_payload_field_names
= 1;
255 while ((opt
= poptGetNextOpt(pc
)) != -1) {
259 ret
= 1; /* exit cleanly */
262 list_formats(stdout
);
266 babeltrace_verbose
= 1;
269 if (get_names_args(&pc
)) {
275 if (get_fields_args(&pc
)) {
281 babeltrace_debug
= 1;
286 case OPT_CLOCK_CYCLES
:
287 opt_clock_cycles
= 1;
289 case OPT_CLOCK_OFFSET
:
294 str
= (char *) poptGetOptArg(pc
);
296 fprintf(stderr
, "[error] Missing --clock-offset argument\n");
301 opt_clock_offset
= strtoull(str
, &endptr
, 0);
302 if (*endptr
!= '\0' || str
== endptr
|| errno
!= 0) {
303 fprintf(stderr
, "[error] Incorrect --clock-offset argument: %s\n", str
);
311 case OPT_CLOCK_SECONDS
:
312 opt_clock_seconds
= 1;
320 case OPT_CLOCK_FORCE_CORRELATE
:
321 opt_clock_force_correlate
= 1;
331 ipath
= poptGetArg(pc
);
333 g_ptr_array_add(opt_input_paths
, (gpointer
) ipath
);
335 if (opt_input_paths
->len
== 0) {
347 static GPtrArray
*traversed_paths
= 0;
350 * traverse_trace_dir() is the callback function for File Tree Walk (nftw).
351 * it receives the path of the current entry (file, dir, link..etc) with
352 * a flag to indicate the type of the entry.
353 * if the entry being visited is a directory and contains a metadata file,
354 * then add the path to a global list to be processed later in
355 * add_traces_recursive.
357 static int traverse_trace_dir(const char *fpath
, const struct stat
*sb
,
358 int tflag
, struct FTW
*ftwbuf
)
366 dirfd
= open(fpath
, 0);
368 fprintf(stderr
, "[error] [Context] Unable to open trace "
369 "directory file descriptor.\n");
370 return 0; /* partial error */
372 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
374 closeret
= close(dirfd
);
379 /* No meta data, just return */
382 closeret
= close(metafd
);
385 return -1; /* failure */
387 closeret
= close(dirfd
);
390 return -1; /* failure */
393 /* Add path to the global list */
394 if (traversed_paths
== NULL
) {
395 fprintf(stderr
, "[error] [Context] Invalid open path array.\n");
398 g_ptr_array_add(traversed_paths
, g_string_new(fpath
));
404 * bt_context_add_traces_recursive: Open a trace recursively
406 * Find each trace present in the subdirectory starting from the given
407 * path, and add them to the context. The packet_seek parameter can be
408 * NULL: this specify to use the default format packet_seek.
410 * Return: 0 on success, < 0 on failure, > 0 on partial failure.
411 * Unable to open toplevel: failure.
412 * Unable to open some subdirectory or file: warn and continue (partial
415 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
416 const char *format_str
,
417 void (*packet_seek
)(struct stream_pos
*pos
,
418 size_t offset
, int whence
))
425 /* Should lock traversed_paths mutex here if used in multithread */
427 traversed_paths
= g_ptr_array_new();
428 trace_ids
= g_array_new(FALSE
, TRUE
, sizeof(int));
430 ret
= nftw(path
, traverse_trace_dir
, 10, 0);
432 /* Process the array if ntfw did not return a fatal error */
434 for (i
= 0; i
< traversed_paths
->len
; i
++) {
435 GString
*trace_path
= g_ptr_array_index(traversed_paths
,
437 int trace_id
= bt_context_add_trace(ctx
,
444 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" from %s "
445 "for reading.\n", trace_path
->str
, path
);
446 /* Allow to skip erroneous traces. */
447 ret
= 1; /* partial error */
449 g_array_append_val(trace_ids
, trace_id
);
451 g_string_free(trace_path
, TRUE
);
454 g_ptr_array_free(traversed_paths
, TRUE
);
455 traversed_paths
= NULL
;
457 /* Should unlock traversed paths mutex here if used in multithread */
460 * Return an error if no trace can be opened.
462 if (trace_ids
->len
== 0) {
463 fprintf(stderr
, "[error] Cannot open any trace for reading.\n\n");
464 ret
= -ENOENT
; /* failure */
466 g_array_free(trace_ids
, TRUE
);
470 int convert_trace(struct trace_descriptor
*td_write
,
471 struct bt_context
*ctx
)
473 struct bt_ctf_iter
*iter
;
474 struct ctf_text_stream_pos
*sout
;
475 struct bt_iter_pos begin_pos
;
476 struct bt_ctf_event
*ctf_event
;
479 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
482 begin_pos
.type
= BT_SEEK_BEGIN
;
483 iter
= bt_ctf_iter_create(ctx
, &begin_pos
, NULL
);
488 while ((ctf_event
= bt_ctf_iter_read_event(iter
))) {
489 ret
= sout
->parent
.event_cb(&sout
->parent
, ctf_event
->parent
->stream
);
491 fprintf(stderr
, "[error] Writing event failed.\n");
494 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
501 bt_ctf_iter_destroy(iter
);
506 int main(int argc
, char **argv
)
508 int ret
, partial_error
= 0, open_success
= 0;
509 struct format
*fmt_write
;
510 struct trace_descriptor
*td_write
;
511 struct bt_context
*ctx
;
514 opt_input_paths
= g_ptr_array_new();
516 ret
= parse_options(argc
, argv
);
518 fprintf(stderr
, "Error parsing options.\n\n");
520 g_ptr_array_free(opt_input_paths
, TRUE
);
522 } else if (ret
> 0) {
523 g_ptr_array_free(opt_input_paths
, TRUE
);
526 printf_verbose("Verbose mode active.\n");
527 printf_debug("Debug mode active.\n");
529 if (opt_input_format_arg
) {
530 opt_input_format
= strdup(opt_input_format_arg
);
531 if (!opt_input_format
) {
535 strlower(opt_input_format
);
537 if (opt_output_format_arg
) {
538 opt_output_format
= strdup(opt_output_format_arg
);
539 if (!opt_output_format
) {
543 strlower(opt_output_format
);
546 printf_verbose("Converting from directory(ies):\n");
547 for (i
= 0; i
< opt_input_paths
->len
; i
++) {
548 const char *ipath
= g_ptr_array_index(opt_input_paths
, i
);
549 printf_verbose(" %s\n", ipath
);
551 printf_verbose("Converting from format: %s\n",
552 opt_input_format
? : "ctf <default>");
553 printf_verbose("Converting to directory: %s\n",
554 opt_output_path
? : "<stdout>");
555 printf_verbose("Converting to format: %s\n",
556 opt_output_format
? : "text <default>");
558 if (!opt_input_format
) {
559 opt_input_format
= strdup("ctf");
560 if (!opt_input_format
) {
565 if (!opt_output_format
) {
566 opt_output_format
= strdup("text");
567 if (!opt_output_format
) {
572 fmt_read
= bt_lookup_format(g_quark_from_static_string(opt_input_format
));
574 fprintf(stderr
, "[error] Format \"%s\" is not supported.\n\n",
579 fmt_write
= bt_lookup_format(g_quark_from_static_string(opt_output_format
));
581 fprintf(stderr
, "[error] format \"%s\" is not supported.\n\n",
587 ctx
= bt_context_create();
592 for (i
= 0; i
< opt_input_paths
->len
; i
++) {
593 const char *ipath
= g_ptr_array_index(opt_input_paths
, i
);
594 ret
= bt_context_add_traces_recursive(ctx
, ipath
,
595 opt_input_format
, NULL
);
597 fprintf(stderr
, "[error] opening trace \"%s\" for reading.\n\n",
599 } else if (ret
> 0) {
600 fprintf(stderr
, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n",
602 open_success
= 1; /* some traces were OK */
605 open_success
= 1; /* all traces were OK */
609 fprintf(stderr
, "[error] none of the specified trace paths could be opened.\n\n");
613 td_write
= fmt_write
->open_trace(opt_output_path
, O_RDWR
, NULL
, NULL
);
615 fprintf(stderr
, "Error opening trace \"%s\" for writing.\n\n",
616 opt_output_path
? : "<none>");
621 * Errors happened when opening traces, but we continue anyway.
622 * sleep to let user see the stderr output before stdout.
625 sleep(PARTIAL_ERROR_SLEEP
);
627 ret
= convert_trace(td_write
, ctx
);
629 fprintf(stderr
, "Error printing trace.\n\n");
630 goto error_copy_trace
;
633 fmt_write
->close_trace(td_write
);
636 printf_verbose("finished converting. Output written to:\n%s\n",
637 opt_output_path
? : "<stdout>");
642 fmt_write
->close_trace(td_write
);
648 /* teardown and exit */
650 free(opt_input_format
);
651 free(opt_output_format
);
652 g_ptr_array_free(opt_input_paths
, TRUE
);