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.
21 #define _XOPEN_SOURCE 700
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-text/types.h>
28 #include <babeltrace/iterator.h>
34 #include <sys/types.h>
40 #define DEFAULT_FILE_ARRAY_SIZE 1
41 static char *opt_input_format
;
42 static char *opt_output_format
;
44 static const char *opt_input_path
;
45 static const char *opt_output_path
;
47 static struct trace_collection trace_collection_read
;
48 static struct format
*fmt_read
;
50 void strlower(char *str
)
74 static struct poptOption long_options
[] = {
75 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
76 { "input-format", 'i', POPT_ARG_STRING
, &opt_input_format
, OPT_NONE
, NULL
, NULL
},
77 { "output-format", 'o', POPT_ARG_STRING
, &opt_output_format
, OPT_NONE
, NULL
, NULL
},
78 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
79 { "list", 'l', POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
80 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
81 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
82 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
83 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
84 { "no-delta", 0, POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
85 { "clock-offset", 0, POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
86 { "clock-raw", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_RAW
, NULL
, NULL
},
87 { "clock-seconds", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
88 { "clock-date", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
89 { "clock-gmt", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
90 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
93 static void list_formats(FILE *fp
)
96 bt_fprintf_format_list(fp
);
99 static void usage(FILE *fp
)
101 fprintf(fp
, "BabelTrace Trace Converter %s\n\n", VERSION
);
102 fprintf(fp
, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
104 fprintf(fp
, " INPUT Input trace path\n");
105 fprintf(fp
, " OUTPUT Output trace path (default: stdout)\n");
107 fprintf(fp
, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
108 fprintf(fp
, " -o, --output-format FORMAT Output trace format (default: text)\n");
110 fprintf(fp
, " -h, --help This help message\n");
111 fprintf(fp
, " -l, --list List available formats\n");
112 fprintf(fp
, " -v, --verbose Verbose mode\n");
113 fprintf(fp
, " (or set BABELTRACE_VERBOSE environment variable)\n");
114 fprintf(fp
, " -d, --debug Debug mode\n");
115 fprintf(fp
, " (or set BABELTRACE_DEBUG environment variable)\n");
116 fprintf(fp
, " --no-delta Do not print time delta between consecutive events\n");
117 fprintf(fp
, " -n, --names name1<,name2,...> Print field names:\n");
118 fprintf(fp
, " (payload OR args OR arg)\n");
119 fprintf(fp
, " all, scope, header, (context OR ctx)\n");
120 fprintf(fp
, " (payload active by default)\n");
121 fprintf(fp
, " -f, --fields name1<,name2,...> Print additional fields:\n");
122 fprintf(fp
, " all, trace, trace:domain, trace:procname,\n");
123 fprintf(fp
, " trace:vpid, loglevel.\n");
124 fprintf(fp
, " --clock-raw Disregard internal clock offset (use raw value)\n");
125 fprintf(fp
, " --clock-offset seconds Clock offset in seconds\n");
126 fprintf(fp
, " --clock-seconds Print the timestamps as [sec.ns]\n");
127 fprintf(fp
, " (default is: [hh:mm:ss.ns])\n");
128 fprintf(fp
, " --clock-date Print clock date\n");
129 fprintf(fp
, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n");
134 static int get_names_args(poptContext
*pc
)
136 char *str
, *strlist
, *strctx
;
138 opt_payload_field_names
= 0;
139 strlist
= (char *) poptGetOptArg(*pc
);
143 str
= strtok_r(strlist
, ",", &strctx
);
145 if (!strcmp(str
, "all"))
146 opt_all_field_names
= 1;
147 else if (!strcmp(str
, "scope"))
148 opt_scope_field_names
= 1;
149 else if (!strcmp(str
, "context") || !strcmp(str
, "ctx"))
150 opt_context_field_names
= 1;
151 else if (!strcmp(str
, "header"))
152 opt_header_field_names
= 1;
153 else if (!strcmp(str
, "payload") || !strcmp(str
, "args") || !strcmp(str
, "arg"))
154 opt_payload_field_names
= 1;
156 fprintf(stderr
, "[error] unknown field name type %s\n", str
);
159 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
163 static int get_fields_args(poptContext
*pc
)
165 char *str
, *strlist
, *strctx
;
167 opt_payload_field_names
= 0;
168 strlist
= (char *) poptGetOptArg(*pc
);
172 str
= strtok_r(strlist
, ",", &strctx
);
174 if (!strcmp(str
, "all"))
176 else if (!strcmp(str
, "trace"))
178 else if (!strcmp(str
, "trace:domain"))
179 opt_trace_domain_field
= 1;
180 else if (!strcmp(str
, "trace:procname"))
181 opt_trace_procname_field
= 1;
182 else if (!strcmp(str
, "trace:vpid"))
183 opt_trace_vpid_field
= 1;
184 else if (!strcmp(str
, "loglevel"))
185 opt_loglevel_field
= 1;
187 fprintf(stderr
, "[error] unknown field type %s\n", str
);
190 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
195 * Return 0 if caller should continue, < 0 if caller should return
196 * error, > 0 if caller should exit without reporting error.
198 static int parse_options(int argc
, char **argv
)
205 return 1; /* exit cleanly */
208 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
209 poptReadDefaultConfig(pc
, 0);
212 opt_payload_field_names
= 1;
214 while ((opt
= poptGetNextOpt(pc
)) != -1) {
218 ret
= 1; /* exit cleanly */
221 list_formats(stdout
);
225 babeltrace_verbose
= 1;
228 if (get_names_args(&pc
)) {
234 if (get_fields_args(&pc
)) {
240 babeltrace_debug
= 1;
248 case OPT_CLOCK_OFFSET
:
252 str
= poptGetOptArg(pc
);
254 fprintf(stderr
, "[error] Missing --clock-offset argument\n");
259 opt_clock_offset
= strtoull(str
, &endptr
, 0);
260 if (*endptr
!= '\0' || str
== endptr
|| errno
!= 0) {
261 fprintf(stderr
, "[error] Incorrect --clock-offset argument: %s\n", str
);
267 case OPT_CLOCK_SECONDS
:
268 opt_clock_seconds
= 1;
283 opt_input_path
= poptGetArg(pc
);
284 if (!opt_input_path
) {
288 opt_output_path
= poptGetArg(pc
);
297 static void init_trace_collection(struct trace_collection
*tc
)
299 tc
->array
= g_ptr_array_sized_new(DEFAULT_FILE_ARRAY_SIZE
);
303 * finalize_trace_collection() closes the opened traces for read
304 * and free the memory allocated for trace collection
306 static void finalize_trace_collection(struct trace_collection
*tc
)
310 for (i
= 0; i
< tc
->array
->len
; i
++) {
311 struct trace_descriptor
*temp
=
312 g_ptr_array_index(tc
->array
, i
);
313 fmt_read
->close_trace(temp
);
315 g_ptr_array_free(tc
->array
, TRUE
);
318 static void trace_collection_add(struct trace_collection
*tc
,
319 struct trace_descriptor
*td
)
321 g_ptr_array_add(tc
->array
, td
);
324 int convert_trace(struct trace_descriptor
*td_write
,
325 struct bt_context
*ctx
)
327 struct babeltrace_iter
*iter
;
328 struct ctf_stream
*stream
;
329 struct ctf_stream_event
*event
;
330 struct ctf_text_stream_pos
*sout
;
331 struct trace_collection_pos begin_pos
;
334 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
337 begin_pos
.type
= BT_SEEK_BEGIN
;
338 iter
= babeltrace_iter_create(ctx
, &begin_pos
, NULL
);
343 while (babeltrace_iter_read_event(iter
, &stream
, &event
) == 0) {
344 ret
= sout
->parent
.event_cb(&sout
->parent
, stream
);
346 fprintf(stderr
, "[error] Writing event failed.\n");
349 ret
= babeltrace_iter_next(iter
);
356 babeltrace_iter_destroy(iter
);
363 * traverse_dir() is the callback functiion for File Tree Walk (nftw).
364 * it receives the path of the current entry (file, dir, link..etc) with
365 * a flag to indicate the type of the entry.
366 * if the entry being visited is a directory and contains a metadata file,
367 * then open it for reading and save a trace_descriptor to that directory
368 * in the read trace collection.
370 static int traverse_dir(const char *fpath
, const struct stat
*sb
,
371 int tflag
, struct FTW
*ftwbuf
)
375 struct trace_descriptor
*td_read
;
379 dirfd
= open(fpath
, 0);
381 fprintf(stderr
, "[error] unable to open trace "
382 "directory file descriptor.\n");
385 fd
= openat(dirfd
, "metadata", O_RDONLY
);
391 td_read
= fmt_read
->open_trace(opt_input_path
,
392 fpath
, O_RDONLY
, ctf_move_pos_slow
,
395 fprintf(stderr
, "Error opening trace \"%s\" "
396 "for reading.\n\n", fpath
);
397 return -1; /* error */
399 trace_collection_add(&trace_collection_read
, td_read
);
401 return 0; /* success */
404 int main(int argc
, char **argv
)
407 struct format
*fmt_write
;
408 struct trace_descriptor
*td_write
;
409 struct bt_context
*ctx
;
411 ret
= parse_options(argc
, argv
);
413 fprintf(stderr
, "Error parsing options.\n\n");
416 } else if (ret
> 0) {
419 printf_verbose("Verbose mode active.\n");
420 printf_debug("Debug mode active.\n");
422 if (opt_input_format
)
423 strlower(opt_input_format
);
424 if (opt_output_format
)
425 strlower(opt_output_format
);
427 printf_verbose("Converting from directory: %s\n", opt_input_path
);
428 printf_verbose("Converting from format: %s\n",
429 opt_input_format
? : "ctf <default>");
430 printf_verbose("Converting to directory: %s\n",
431 opt_output_path
? : "<stdout>");
432 printf_verbose("Converting to format: %s\n",
433 opt_output_format
? : "text <default>");
435 if (!opt_input_format
)
436 opt_input_format
= "ctf";
437 if (!opt_output_format
)
438 opt_output_format
= "text";
439 fmt_read
= bt_lookup_format(g_quark_from_static_string(opt_input_format
));
441 fprintf(stderr
, "[error] Format \"%s\" is not supported.\n\n",
445 fmt_write
= bt_lookup_format(g_quark_from_static_string(opt_output_format
));
447 fprintf(stderr
, "[error] format \"%s\" is not supported.\n\n",
453 * pass the input path to nftw() .
454 * specify traverse_dir() as the callback function.
455 * depth = 10 which is the max number of file descriptors
456 * that nftw() can open at a given time.
457 * flags = 0 check nftw documentation for more info .
459 init_trace_collection(&trace_collection_read
);
460 ret
= nftw(opt_input_path
, traverse_dir
, 10, 0);
462 fprintf(stderr
, "[error] opening trace \"%s\" for reading.\n\n",
466 if (trace_collection_read
.array
->len
== 0) {
467 fprintf(stderr
, "[warning] no metadata file was found."
468 " no output was generated\n");
471 ctx
= bt_context_create(&trace_collection_read
);
473 fprintf(stderr
, "Error allocating a new context\n");
476 td_write
= fmt_write
->open_trace(NULL
, opt_output_path
, O_RDWR
, NULL
, NULL
);
478 fprintf(stderr
, "Error opening trace \"%s\" for writing.\n\n",
479 opt_output_path
? : "<none>");
483 ret
= convert_trace(td_write
, ctx
);
485 fprintf(stderr
, "Error printing trace.\n\n");
486 goto error_copy_trace
;
489 fmt_write
->close_trace(td_write
);
490 finalize_trace_collection(&trace_collection_read
);
491 bt_context_destroy(ctx
);
492 printf_verbose("finished converting. Output written to:\n%s\n",
493 opt_output_path
? : "<stdout>");
498 fmt_write
->close_trace(td_write
);
500 finalize_trace_collection(&trace_collection_read
);