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
)
68 static struct poptOption long_options
[] = {
69 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
70 { "input-format", 'i', POPT_ARG_STRING
, &opt_input_format
, OPT_NONE
, NULL
, NULL
},
71 { "output-format", 'o', POPT_ARG_STRING
, &opt_output_format
, OPT_NONE
, NULL
, NULL
},
72 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
73 { "list", 'l', POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
74 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
75 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
76 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
77 { "no-delta", 0, POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
78 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
81 static void list_formats(FILE *fp
)
84 bt_fprintf_format_list(fp
);
87 static void usage(FILE *fp
)
89 fprintf(fp
, "BabelTrace Trace Converter %s\n\n", VERSION
);
90 fprintf(fp
, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
92 fprintf(fp
, " INPUT Input trace path\n");
93 fprintf(fp
, " OUTPUT Output trace path (default: stdout)\n");
95 fprintf(fp
, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
96 fprintf(fp
, " -o, --output-format FORMAT Output trace format (default: text)\n");
98 fprintf(fp
, " -h, --help This help message\n");
99 fprintf(fp
, " -l, --list List available formats\n");
100 fprintf(fp
, " -v, --verbose Verbose mode\n");
101 fprintf(fp
, " (or set BABELTRACE_VERBOSE environment variable)\n");
102 fprintf(fp
, " -d, --debug Debug mode\n");
103 fprintf(fp
, " (or set BABELTRACE_DEBUG environment variable)\n");
104 fprintf(fp
, " --no-delta Do not print time delta between consecutive events\n");
105 fprintf(fp
, " -n, --names name1<,name2,...> Print field names.\n");
106 fprintf(fp
, " Available field names:\n");
107 fprintf(fp
, " (payload OR args OR arg)\n");
108 fprintf(fp
, " all, scope, header, (context OR ctx)\n");
109 fprintf(fp
, " trace, trace:domain, trace:procname, trace:vpid,\n");
110 fprintf(fp
, " loglevel.\n");
111 fprintf(fp
, " (payload active by default)\n");
116 static int get_names_args(poptContext
*pc
)
118 char *str
, *strlist
, *strctx
;
120 opt_payload_field_names
= 0;
121 strlist
= (char *) poptGetOptArg(*pc
);
125 str
= strtok_r(strlist
, ",", &strctx
);
127 if (!strcmp(str
, "all"))
128 opt_all_field_names
= 1;
129 else if (!strcmp(str
, "scope"))
130 opt_scope_field_names
= 1;
131 else if (!strcmp(str
, "context") || !strcmp(str
, "ctx"))
132 opt_context_field_names
= 1;
133 else if (!strcmp(str
, "header"))
134 opt_header_field_names
= 1;
135 else if (!strcmp(str
, "payload") || !strcmp(str
, "args") || !strcmp(str
, "arg"))
136 opt_payload_field_names
= 1;
137 else if (!strcmp(str
, "trace"))
139 else if (!strcmp(str
, "trace:domain"))
140 opt_trace_domain
= 1;
141 else if (!strcmp(str
, "trace:procname"))
142 opt_trace_procname
= 1;
143 else if (!strcmp(str
, "trace:vpid"))
145 else if (!strcmp(str
, "loglevel"))
148 fprintf(stderr
, "[error] unknown field name type %s\n", str
);
151 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
156 * Return 0 if caller should continue, < 0 if caller should return
157 * error, > 0 if caller should exit without reporting error.
159 static int parse_options(int argc
, char **argv
)
166 return 1; /* exit cleanly */
169 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
170 poptReadDefaultConfig(pc
, 0);
173 opt_payload_field_names
= 1;
175 while ((opt
= poptGetNextOpt(pc
)) != -1) {
179 ret
= 1; /* exit cleanly */
182 list_formats(stdout
);
186 babeltrace_verbose
= 1;
189 if (get_names_args(&pc
)) {
195 babeltrace_debug
= 1;
206 opt_input_path
= poptGetArg(pc
);
207 if (!opt_input_path
) {
211 opt_output_path
= poptGetArg(pc
);
220 static void init_trace_collection(struct trace_collection
*tc
)
222 tc
->array
= g_ptr_array_sized_new(DEFAULT_FILE_ARRAY_SIZE
);
226 * finalize_trace_collection() closes the opened traces for read
227 * and free the memory allocated for trace collection
229 static void finalize_trace_collection(struct trace_collection
*tc
)
233 for (i
= 0; i
< tc
->array
->len
; i
++) {
234 struct trace_descriptor
*temp
=
235 g_ptr_array_index(tc
->array
, i
);
236 fmt_read
->close_trace(temp
);
238 g_ptr_array_free(tc
->array
, TRUE
);
241 static void trace_collection_add(struct trace_collection
*tc
,
242 struct trace_descriptor
*td
)
244 g_ptr_array_add(tc
->array
, td
);
247 int convert_trace(struct trace_descriptor
*td_write
,
248 struct bt_context
*ctx
)
250 struct babeltrace_iter
*iter
;
251 struct ctf_stream
*stream
;
252 struct ctf_stream_event
*event
;
253 struct ctf_text_stream_pos
*sout
;
254 struct trace_collection_pos begin_pos
;
257 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
260 begin_pos
.type
= BT_SEEK_BEGIN
;
261 iter
= babeltrace_iter_create(ctx
, &begin_pos
, NULL
);
266 while (babeltrace_iter_read_event(iter
, &stream
, &event
) == 0) {
267 ret
= sout
->parent
.event_cb(&sout
->parent
, stream
);
269 fprintf(stderr
, "[error] Writing event failed.\n");
272 ret
= babeltrace_iter_next(iter
);
279 babeltrace_iter_destroy(iter
);
286 * traverse_dir() is the callback functiion for File Tree Walk (nftw).
287 * it receives the path of the current entry (file, dir, link..etc) with
288 * a flag to indicate the type of the entry.
289 * if the entry being visited is a directory and contains a metadata file,
290 * then open it for reading and save a trace_descriptor to that directory
291 * in the read trace collection.
293 static int traverse_dir(const char *fpath
, const struct stat
*sb
,
294 int tflag
, struct FTW
*ftwbuf
)
298 struct trace_descriptor
*td_read
;
302 dirfd
= open(fpath
, 0);
304 fprintf(stderr
, "[error] unable to open trace "
305 "directory file descriptor.\n");
308 fd
= openat(dirfd
, "metadata", O_RDONLY
);
314 td_read
= fmt_read
->open_trace(opt_input_path
,
315 fpath
, O_RDONLY
, ctf_move_pos_slow
,
318 fprintf(stderr
, "Error opening trace \"%s\" "
319 "for reading.\n\n", fpath
);
320 return -1; /* error */
322 trace_collection_add(&trace_collection_read
, td_read
);
324 return 0; /* success */
327 int main(int argc
, char **argv
)
330 struct format
*fmt_write
;
331 struct trace_descriptor
*td_write
;
332 struct bt_context
*ctx
;
334 ret
= parse_options(argc
, argv
);
336 fprintf(stderr
, "Error parsing options.\n\n");
339 } else if (ret
> 0) {
342 printf_verbose("Verbose mode active.\n");
343 printf_debug("Debug mode active.\n");
345 if (opt_input_format
)
346 strlower(opt_input_format
);
347 if (opt_output_format
)
348 strlower(opt_output_format
);
350 printf_verbose("Converting from directory: %s\n", opt_input_path
);
351 printf_verbose("Converting from format: %s\n",
352 opt_input_format
? : "ctf <default>");
353 printf_verbose("Converting to directory: %s\n",
354 opt_output_path
? : "<stdout>");
355 printf_verbose("Converting to format: %s\n",
356 opt_output_format
? : "text <default>");
358 if (!opt_input_format
)
359 opt_input_format
= "ctf";
360 if (!opt_output_format
)
361 opt_output_format
= "text";
362 fmt_read
= bt_lookup_format(g_quark_from_static_string(opt_input_format
));
364 fprintf(stderr
, "[error] Format \"%s\" is not supported.\n\n",
368 fmt_write
= bt_lookup_format(g_quark_from_static_string(opt_output_format
));
370 fprintf(stderr
, "[error] format \"%s\" is not supported.\n\n",
376 * pass the input path to nftw() .
377 * specify traverse_dir() as the callback function.
378 * depth = 10 which is the max number of file descriptors
379 * that nftw() can open at a given time.
380 * flags = 0 check nftw documentation for more info .
382 init_trace_collection(&trace_collection_read
);
383 ret
= nftw(opt_input_path
, traverse_dir
, 10, 0);
385 fprintf(stderr
, "[error] opening trace \"%s\" for reading.\n\n",
389 if (trace_collection_read
.array
->len
== 0) {
390 fprintf(stderr
, "[warning] no metadata file was found."
391 " no output was generated\n");
394 ctx
= bt_context_create(&trace_collection_read
);
396 fprintf(stderr
, "Error allocating a new context\n");
399 td_write
= fmt_write
->open_trace(NULL
, opt_output_path
, O_RDWR
, NULL
, NULL
);
401 fprintf(stderr
, "Error opening trace \"%s\" for writing.\n\n",
402 opt_output_path
? : "<none>");
406 ret
= convert_trace(td_write
, ctx
);
408 fprintf(stderr
, "Error printing trace.\n\n");
409 goto error_copy_trace
;
412 fmt_write
->close_trace(td_write
);
413 finalize_trace_collection(&trace_collection_read
);
414 bt_context_destroy(ctx
);
415 printf_verbose("finished converting. Output written to:\n%s\n",
416 opt_output_path
? : "<stdout>");
421 fmt_write
->close_trace(td_write
);
423 finalize_trace_collection(&trace_collection_read
);