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>
33 #include <sys/types.h>
39 #define DEFAULT_FILE_ARRAY_SIZE 1
40 static char *opt_input_format
;
41 static char *opt_output_format
;
43 static const char *opt_input_path
;
44 static const char *opt_output_path
;
46 static struct trace_collection trace_collection_read
;
47 static struct format
*fmt_read
;
49 void strlower(char *str
)
67 static struct poptOption long_options
[] = {
68 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
69 { "input-format", 'i', POPT_ARG_STRING
, &opt_input_format
, OPT_NONE
, NULL
, NULL
},
70 { "output-format", 'o', POPT_ARG_STRING
, &opt_output_format
, OPT_NONE
, NULL
, NULL
},
71 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
72 { "list", 'l', POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
73 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
74 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
75 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
76 { "no-delta", 0, POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
77 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
80 static void list_formats(FILE *fp
)
83 bt_fprintf_format_list(fp
);
86 static void usage(FILE *fp
)
88 fprintf(fp
, "BabelTrace Trace Converter %s\n\n", VERSION
);
89 fprintf(fp
, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
91 fprintf(fp
, " INPUT Input trace path\n");
92 fprintf(fp
, " OUTPUT Output trace path (default: stdout)\n");
94 fprintf(fp
, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
95 fprintf(fp
, " -o, --output-format FORMAT Output trace format (default: text)\n");
97 fprintf(fp
, " -h, --help This help message\n");
98 fprintf(fp
, " -l, --list List available formats\n");
99 fprintf(fp
, " -v, --verbose Verbose mode\n");
100 fprintf(fp
, " (or set BABELTRACE_VERBOSE environment variable)\n");
101 fprintf(fp
, " -d, --debug Debug mode\n");
102 fprintf(fp
, " (or set BABELTRACE_DEBUG environment variable)\n");
103 fprintf(fp
, " --no-delta Do not print time delta between consecutive events\n");
104 fprintf(fp
, " -n, --names name1<,name2,...> Print field names.\n");
105 fprintf(fp
, " Available field names:\n");
106 fprintf(fp
, " (payload OR args OR arg)\n");
107 fprintf(fp
, " all, scope, header, (context OR ctx)\n");
108 fprintf(fp
, " trace, trace:domain, trace:procname, trace:vpid,\n");
109 fprintf(fp
, " loglevel.\n");
110 fprintf(fp
, " (payload active by default)\n");
115 static int get_names_args(poptContext
*pc
)
117 char *str
, *strlist
, *strctx
;
119 opt_payload_field_names
= 0;
120 strlist
= (char *) poptGetOptArg(*pc
);
124 str
= strtok_r(strlist
, ",", &strctx
);
126 if (!strcmp(str
, "all"))
127 opt_all_field_names
= 1;
128 else if (!strcmp(str
, "scope"))
129 opt_scope_field_names
= 1;
130 else if (!strcmp(str
, "context") || !strcmp(str
, "ctx"))
131 opt_context_field_names
= 1;
132 else if (!strcmp(str
, "header"))
133 opt_header_field_names
= 1;
134 else if (!strcmp(str
, "payload") || !strcmp(str
, "args") || !strcmp(str
, "arg"))
135 opt_payload_field_names
= 1;
136 else if (!strcmp(str
, "trace"))
138 else if (!strcmp(str
, "trace:domain"))
139 opt_trace_domain
= 1;
140 else if (!strcmp(str
, "trace:procname"))
141 opt_trace_procname
= 1;
142 else if (!strcmp(str
, "trace:vpid"))
144 else if (!strcmp(str
, "loglevel"))
147 fprintf(stdout
, "[error] unknown field name type %s\n", str
);
150 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
155 * Return 0 if caller should continue, < 0 if caller should return
156 * error, > 0 if caller should exit without reporting error.
158 static int parse_options(int argc
, char **argv
)
165 return 1; /* exit cleanly */
168 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
169 poptReadDefaultConfig(pc
, 0);
172 opt_payload_field_names
= 1;
174 while ((opt
= poptGetNextOpt(pc
)) != -1) {
178 ret
= 1; /* exit cleanly */
181 list_formats(stdout
);
185 babeltrace_verbose
= 1;
188 if (get_names_args(&pc
)) {
194 babeltrace_debug
= 1;
205 opt_input_path
= poptGetArg(pc
);
206 if (!opt_input_path
) {
210 opt_output_path
= poptGetArg(pc
);
219 static void init_trace_collection(struct trace_collection
*tc
)
221 tc
->array
= g_ptr_array_sized_new(DEFAULT_FILE_ARRAY_SIZE
);
225 * finalize_trace_collection() closes the opened traces for read
226 * and free the memory allocated for trace collection
228 static void finalize_trace_collection(struct trace_collection
*tc
)
232 for (i
= 0; i
< tc
->array
->len
; i
++) {
233 struct trace_descriptor
*temp
=
234 g_ptr_array_index(tc
->array
, i
);
235 fmt_read
->close_trace(temp
);
237 g_ptr_array_free(tc
->array
, TRUE
);
240 static void trace_collection_add(struct trace_collection
*tc
,
241 struct trace_descriptor
*td
)
243 g_ptr_array_add(tc
->array
, td
);
246 int convert_trace(struct trace_descriptor
*td_write
,
247 struct bt_context
*ctx
)
249 struct babeltrace_iter
*iter
;
250 struct ctf_stream
*stream
;
251 struct ctf_stream_event
*event
;
252 struct ctf_text_stream_pos
*sout
;
253 struct trace_collection_pos begin_pos
;
256 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
259 begin_pos
.type
= BT_SEEK_BEGIN
;
260 iter
= babeltrace_iter_create(ctx
, &begin_pos
, NULL
);
265 while (babeltrace_iter_read_event(iter
, &stream
, &event
) == 0) {
266 ret
= sout
->parent
.event_cb(&sout
->parent
, stream
);
268 fprintf(stdout
, "[error] Writing event failed.\n");
271 ret
= babeltrace_iter_next(iter
);
278 babeltrace_iter_destroy(iter
);
285 * traverse_dir() is the callback functiion for File Tree Walk (nftw).
286 * it receives the path of the current entry (file, dir, link..etc) with
287 * a flag to indicate the type of the entry.
288 * if the entry being visited is a directory and contains a metadata file,
289 * then open it for reading and save a trace_descriptor to that directory
290 * in the read trace collection.
292 static int traverse_dir(const char *fpath
, const struct stat
*sb
,
293 int tflag
, struct FTW
*ftwbuf
)
297 struct trace_descriptor
*td_read
;
301 dirfd
= open(fpath
, 0);
303 fprintf(stdout
, "[error] unable to open trace "
304 "directory file descriptor.\n");
307 fd
= openat(dirfd
, "metadata", O_RDONLY
);
313 td_read
= fmt_read
->open_trace(opt_input_path
,
314 fpath
, O_RDONLY
, ctf_move_pos_slow
,
317 fprintf(stdout
, "Error opening trace \"%s\" "
318 "for reading.\n\n", fpath
);
319 return -1; /* error */
321 trace_collection_add(&trace_collection_read
, td_read
);
323 return 0; /* success */
326 int main(int argc
, char **argv
)
329 struct format
*fmt_write
;
330 struct trace_descriptor
*td_write
;
331 struct bt_context
*ctx
;
333 ret
= parse_options(argc
, argv
);
335 fprintf(stdout
, "Error parsing options.\n\n");
338 } else if (ret
> 0) {
341 printf_verbose("Verbose mode active.\n");
342 printf_debug("Debug mode active.\n");
344 if (opt_input_format
)
345 strlower(opt_input_format
);
346 if (opt_output_format
)
347 strlower(opt_output_format
);
349 printf_verbose("Converting from directory: %s\n", opt_input_path
);
350 printf_verbose("Converting from format: %s\n",
351 opt_input_format
? : "ctf <default>");
352 printf_verbose("Converting to directory: %s\n",
353 opt_output_path
? : "<stdout>");
354 printf_verbose("Converting to format: %s\n",
355 opt_output_format
? : "text <default>");
357 if (!opt_input_format
)
358 opt_input_format
= "ctf";
359 if (!opt_output_format
)
360 opt_output_format
= "text";
361 fmt_read
= bt_lookup_format(g_quark_from_static_string(opt_input_format
));
363 fprintf(stdout
, "[error] Format \"%s\" is not supported.\n\n",
367 fmt_write
= bt_lookup_format(g_quark_from_static_string(opt_output_format
));
369 fprintf(stdout
, "[error] format \"%s\" is not supported.\n\n",
375 * pass the input path to nftw() .
376 * specify traverse_dir() as the callback function.
377 * depth = 10 which is the max number of file descriptors
378 * that nftw() can open at a given time.
379 * flags = 0 check nftw documentation for more info .
381 init_trace_collection(&trace_collection_read
);
382 ret
= nftw(opt_input_path
, traverse_dir
, 10, 0);
384 fprintf(stdout
, "[error] opening trace \"%s\" for reading.\n\n",
388 if (trace_collection_read
.array
->len
== 0) {
389 fprintf(stdout
, "[warning] no metadata file was found."
390 " no output was generated\n");
393 ctx
= bt_context_create(&trace_collection_read
);
395 fprintf(stdout
, "Error allocating a new context\n");
398 td_write
= fmt_write
->open_trace(NULL
, opt_output_path
, O_RDWR
, NULL
, NULL
);
400 fprintf(stdout
, "Error opening trace \"%s\" for writing.\n\n",
401 opt_output_path
? : "<none>");
405 ret
= convert_trace(td_write
, ctx
);
407 fprintf(stdout
, "Error printing trace.\n\n");
408 goto error_copy_trace
;
411 fmt_write
->close_trace(td_write
);
412 finalize_trace_collection(&trace_collection_read
);
413 bt_context_destroy(ctx
);
414 printf_verbose("finished converting. Output written to:\n%s\n",
415 opt_output_path
? : "<stdout>");
420 fmt_write
->close_trace(td_write
);
422 finalize_trace_collection(&trace_collection_read
);