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
)
69 static struct poptOption long_options
[] = {
70 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
71 { "input-format", 'i', POPT_ARG_STRING
, &opt_input_format
, OPT_NONE
, NULL
, NULL
},
72 { "output-format", 'o', POPT_ARG_STRING
, &opt_output_format
, OPT_NONE
, NULL
, NULL
},
73 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
74 { "list", 'l', POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
75 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
76 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
77 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
78 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
79 { "no-delta", 0, POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
80 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
83 static void list_formats(FILE *fp
)
86 bt_fprintf_format_list(fp
);
89 static void usage(FILE *fp
)
91 fprintf(fp
, "BabelTrace Trace Converter %s\n\n", VERSION
);
92 fprintf(fp
, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
94 fprintf(fp
, " INPUT Input trace path\n");
95 fprintf(fp
, " OUTPUT Output trace path (default: stdout)\n");
97 fprintf(fp
, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
98 fprintf(fp
, " -o, --output-format FORMAT Output trace format (default: text)\n");
100 fprintf(fp
, " -h, --help This help message\n");
101 fprintf(fp
, " -l, --list List available formats\n");
102 fprintf(fp
, " -v, --verbose Verbose mode\n");
103 fprintf(fp
, " (or set BABELTRACE_VERBOSE environment variable)\n");
104 fprintf(fp
, " -d, --debug Debug mode\n");
105 fprintf(fp
, " (or set BABELTRACE_DEBUG environment variable)\n");
106 fprintf(fp
, " --no-delta Do not print time delta between consecutive events\n");
107 fprintf(fp
, " -n, --names name1<,name2,...> Print field names:\n");
108 fprintf(fp
, " (payload OR args OR arg)\n");
109 fprintf(fp
, " all, scope, header, (context OR ctx)\n");
110 fprintf(fp
, " (payload active by default)\n");
111 fprintf(fp
, " -f, --fields name1<,name2,...> Print additional fields:\n");
112 fprintf(fp
, " all, trace, trace:domain, trace:procname,\n");
113 fprintf(fp
, " trace:vpid, loglevel.\n");
118 static int get_names_args(poptContext
*pc
)
120 char *str
, *strlist
, *strctx
;
122 opt_payload_field_names
= 0;
123 strlist
= (char *) poptGetOptArg(*pc
);
127 str
= strtok_r(strlist
, ",", &strctx
);
129 if (!strcmp(str
, "all"))
130 opt_all_field_names
= 1;
131 else if (!strcmp(str
, "scope"))
132 opt_scope_field_names
= 1;
133 else if (!strcmp(str
, "context") || !strcmp(str
, "ctx"))
134 opt_context_field_names
= 1;
135 else if (!strcmp(str
, "header"))
136 opt_header_field_names
= 1;
137 else if (!strcmp(str
, "payload") || !strcmp(str
, "args") || !strcmp(str
, "arg"))
138 opt_payload_field_names
= 1;
140 fprintf(stderr
, "[error] unknown field name type %s\n", str
);
143 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
147 static int get_fields_args(poptContext
*pc
)
149 char *str
, *strlist
, *strctx
;
151 opt_payload_field_names
= 0;
152 strlist
= (char *) poptGetOptArg(*pc
);
156 str
= strtok_r(strlist
, ",", &strctx
);
158 if (!strcmp(str
, "all"))
160 else if (!strcmp(str
, "trace"))
162 else if (!strcmp(str
, "trace:domain"))
163 opt_trace_domain_field
= 1;
164 else if (!strcmp(str
, "trace:procname"))
165 opt_trace_procname_field
= 1;
166 else if (!strcmp(str
, "trace:vpid"))
167 opt_trace_vpid_field
= 1;
168 else if (!strcmp(str
, "loglevel"))
169 opt_loglevel_field
= 1;
171 fprintf(stderr
, "[error] unknown field type %s\n", str
);
174 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
179 * Return 0 if caller should continue, < 0 if caller should return
180 * error, > 0 if caller should exit without reporting error.
182 static int parse_options(int argc
, char **argv
)
189 return 1; /* exit cleanly */
192 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
193 poptReadDefaultConfig(pc
, 0);
196 opt_payload_field_names
= 1;
198 while ((opt
= poptGetNextOpt(pc
)) != -1) {
202 ret
= 1; /* exit cleanly */
205 list_formats(stdout
);
209 babeltrace_verbose
= 1;
212 if (get_names_args(&pc
)) {
218 if (get_fields_args(&pc
)) {
224 babeltrace_debug
= 1;
235 opt_input_path
= poptGetArg(pc
);
236 if (!opt_input_path
) {
240 opt_output_path
= poptGetArg(pc
);
249 static void init_trace_collection(struct trace_collection
*tc
)
251 tc
->array
= g_ptr_array_sized_new(DEFAULT_FILE_ARRAY_SIZE
);
255 * finalize_trace_collection() closes the opened traces for read
256 * and free the memory allocated for trace collection
258 static void finalize_trace_collection(struct trace_collection
*tc
)
262 for (i
= 0; i
< tc
->array
->len
; i
++) {
263 struct trace_descriptor
*temp
=
264 g_ptr_array_index(tc
->array
, i
);
265 fmt_read
->close_trace(temp
);
267 g_ptr_array_free(tc
->array
, TRUE
);
270 static void trace_collection_add(struct trace_collection
*tc
,
271 struct trace_descriptor
*td
)
273 g_ptr_array_add(tc
->array
, td
);
276 int convert_trace(struct trace_descriptor
*td_write
,
277 struct bt_context
*ctx
)
279 struct babeltrace_iter
*iter
;
280 struct ctf_stream
*stream
;
281 struct ctf_stream_event
*event
;
282 struct ctf_text_stream_pos
*sout
;
283 struct trace_collection_pos begin_pos
;
286 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
289 begin_pos
.type
= BT_SEEK_BEGIN
;
290 iter
= babeltrace_iter_create(ctx
, &begin_pos
, NULL
);
295 while (babeltrace_iter_read_event(iter
, &stream
, &event
) == 0) {
296 ret
= sout
->parent
.event_cb(&sout
->parent
, stream
);
298 fprintf(stderr
, "[error] Writing event failed.\n");
301 ret
= babeltrace_iter_next(iter
);
308 babeltrace_iter_destroy(iter
);
315 * traverse_dir() is the callback functiion for File Tree Walk (nftw).
316 * it receives the path of the current entry (file, dir, link..etc) with
317 * a flag to indicate the type of the entry.
318 * if the entry being visited is a directory and contains a metadata file,
319 * then open it for reading and save a trace_descriptor to that directory
320 * in the read trace collection.
322 static int traverse_dir(const char *fpath
, const struct stat
*sb
,
323 int tflag
, struct FTW
*ftwbuf
)
327 struct trace_descriptor
*td_read
;
331 dirfd
= open(fpath
, 0);
333 fprintf(stderr
, "[error] unable to open trace "
334 "directory file descriptor.\n");
337 fd
= openat(dirfd
, "metadata", O_RDONLY
);
343 td_read
= fmt_read
->open_trace(opt_input_path
,
344 fpath
, O_RDONLY
, ctf_move_pos_slow
,
347 fprintf(stderr
, "Error opening trace \"%s\" "
348 "for reading.\n\n", fpath
);
349 return -1; /* error */
351 trace_collection_add(&trace_collection_read
, td_read
);
353 return 0; /* success */
356 int main(int argc
, char **argv
)
359 struct format
*fmt_write
;
360 struct trace_descriptor
*td_write
;
361 struct bt_context
*ctx
;
363 ret
= parse_options(argc
, argv
);
365 fprintf(stderr
, "Error parsing options.\n\n");
368 } else if (ret
> 0) {
371 printf_verbose("Verbose mode active.\n");
372 printf_debug("Debug mode active.\n");
374 if (opt_input_format
)
375 strlower(opt_input_format
);
376 if (opt_output_format
)
377 strlower(opt_output_format
);
379 printf_verbose("Converting from directory: %s\n", opt_input_path
);
380 printf_verbose("Converting from format: %s\n",
381 opt_input_format
? : "ctf <default>");
382 printf_verbose("Converting to directory: %s\n",
383 opt_output_path
? : "<stdout>");
384 printf_verbose("Converting to format: %s\n",
385 opt_output_format
? : "text <default>");
387 if (!opt_input_format
)
388 opt_input_format
= "ctf";
389 if (!opt_output_format
)
390 opt_output_format
= "text";
391 fmt_read
= bt_lookup_format(g_quark_from_static_string(opt_input_format
));
393 fprintf(stderr
, "[error] Format \"%s\" is not supported.\n\n",
397 fmt_write
= bt_lookup_format(g_quark_from_static_string(opt_output_format
));
399 fprintf(stderr
, "[error] format \"%s\" is not supported.\n\n",
405 * pass the input path to nftw() .
406 * specify traverse_dir() as the callback function.
407 * depth = 10 which is the max number of file descriptors
408 * that nftw() can open at a given time.
409 * flags = 0 check nftw documentation for more info .
411 init_trace_collection(&trace_collection_read
);
412 ret
= nftw(opt_input_path
, traverse_dir
, 10, 0);
414 fprintf(stderr
, "[error] opening trace \"%s\" for reading.\n\n",
418 if (trace_collection_read
.array
->len
== 0) {
419 fprintf(stderr
, "[warning] no metadata file was found."
420 " no output was generated\n");
423 ctx
= bt_context_create(&trace_collection_read
);
425 fprintf(stderr
, "Error allocating a new context\n");
428 td_write
= fmt_write
->open_trace(NULL
, opt_output_path
, O_RDWR
, NULL
, NULL
);
430 fprintf(stderr
, "Error opening trace \"%s\" for writing.\n\n",
431 opt_output_path
? : "<none>");
435 ret
= convert_trace(td_write
, ctx
);
437 fprintf(stderr
, "Error printing trace.\n\n");
438 goto error_copy_trace
;
441 fmt_write
->close_trace(td_write
);
442 finalize_trace_collection(&trace_collection_read
);
443 bt_context_destroy(ctx
);
444 printf_verbose("finished converting. Output written to:\n%s\n",
445 opt_output_path
? : "<stdout>");
450 fmt_write
->close_trace(td_write
);
452 finalize_trace_collection(&trace_collection_read
);