Commit | Line | Data |
---|---|---|
34ac0e6c MD |
1 | /* |
2 | * babeltrace.c | |
3 | * | |
4 | * Babeltrace Trace Converter | |
5 | * | |
64fa3fec MD |
6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
7 | * | |
8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
34ac0e6c MD |
9 | * |
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: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | */ | |
4c8bfb7e | 20 | |
afb48eae | 21 | #define _XOPEN_SOURCE 700 |
00f7fbf0 | 22 | #include <config.h> |
95d36295 | 23 | #include <babeltrace/babeltrace.h> |
7fb21036 | 24 | #include <babeltrace/format.h> |
95d36295 JD |
25 | #include <babeltrace/context.h> |
26 | #include <babeltrace/ctf/types.h> | |
27 | #include <babeltrace/ctf-text/types.h> | |
6204d33c | 28 | #include <babeltrace/iterator.h> |
34ac0e6c MD |
29 | #include <popt.h> |
30 | #include <errno.h> | |
31 | #include <stdlib.h> | |
bbefb8dd MD |
32 | #include <ctype.h> |
33 | #include <sys/stat.h> | |
34 | #include <sys/types.h> | |
35 | #include <fcntl.h> | |
afb48eae AA |
36 | #include <ftw.h> |
37 | #include <dirent.h> | |
38 | #include <unistd.h> | |
4c8bfb7e | 39 | |
a44bc4c9 MD |
40 | #include <babeltrace/ctf-ir/metadata.h> /* for clocks */ |
41 | ||
afb48eae | 42 | #define DEFAULT_FILE_ARRAY_SIZE 1 |
bbefb8dd MD |
43 | static char *opt_input_format; |
44 | static char *opt_output_format; | |
34ac0e6c MD |
45 | |
46 | static const char *opt_input_path; | |
47 | static const char *opt_output_path; | |
48 | ||
afb48eae AA |
49 | static struct trace_collection trace_collection_read; |
50 | static struct format *fmt_read; | |
51 | ||
bbefb8dd MD |
52 | void strlower(char *str) |
53 | { | |
54 | while (*str) { | |
55 | *str = tolower(*str); | |
56 | str++; | |
57 | } | |
58 | } | |
59 | ||
34ac0e6c MD |
60 | enum { |
61 | OPT_NONE = 0, | |
62 | OPT_HELP, | |
7fb21036 | 63 | OPT_LIST, |
34ac0e6c MD |
64 | OPT_VERBOSE, |
65 | OPT_DEBUG, | |
d63ca2cd | 66 | OPT_NAMES, |
359d7456 | 67 | OPT_FIELDS, |
8d8ed9af | 68 | OPT_NO_DELTA, |
11ac6674 MD |
69 | OPT_CLOCK_OFFSET, |
70 | OPT_CLOCK_RAW, | |
71 | OPT_CLOCK_SECONDS, | |
72 | OPT_CLOCK_DATE, | |
73 | OPT_CLOCK_GMT, | |
34ac0e6c MD |
74 | }; |
75 | ||
76 | static struct poptOption long_options[] = { | |
77 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
78 | { "input-format", 'i', POPT_ARG_STRING, &opt_input_format, OPT_NONE, NULL, NULL }, | |
79 | { "output-format", 'o', POPT_ARG_STRING, &opt_output_format, OPT_NONE, NULL, NULL }, | |
80 | { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL }, | |
7fb21036 | 81 | { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL }, |
34ac0e6c MD |
82 | { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL }, |
83 | { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL }, | |
cba1661c | 84 | { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL }, |
359d7456 | 85 | { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL }, |
8d8ed9af | 86 | { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL }, |
11ac6674 MD |
87 | { "clock-offset", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL }, |
88 | { "clock-raw", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_RAW, NULL, NULL }, | |
89 | { "clock-seconds", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_SECONDS, NULL, NULL }, | |
90 | { "clock-date", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_DATE, NULL, NULL }, | |
91 | { "clock-gmt", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_GMT, NULL, NULL }, | |
34ac0e6c MD |
92 | { NULL, 0, 0, NULL, 0, NULL, NULL }, |
93 | }; | |
94 | ||
7fb21036 MD |
95 | static void list_formats(FILE *fp) |
96 | { | |
97 | fprintf(fp, "\n"); | |
98 | bt_fprintf_format_list(fp); | |
99 | } | |
100 | ||
34ac0e6c | 101 | static void usage(FILE *fp) |
4c8bfb7e | 102 | { |
00f7fbf0 | 103 | fprintf(fp, "BabelTrace Trace Converter %s\n\n", VERSION); |
4d5678b9 | 104 | fprintf(fp, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n"); |
34ac0e6c | 105 | fprintf(fp, "\n"); |
4d5678b9 MD |
106 | fprintf(fp, " INPUT Input trace path\n"); |
107 | fprintf(fp, " OUTPUT Output trace path (default: stdout)\n"); | |
34ac0e6c | 108 | fprintf(fp, "\n"); |
d2b8ea6b MD |
109 | fprintf(fp, " -i, --input-format FORMAT Input trace format (default: ctf)\n"); |
110 | fprintf(fp, " -o, --output-format FORMAT Output trace format (default: text)\n"); | |
34ac0e6c | 111 | fprintf(fp, "\n"); |
4d5678b9 MD |
112 | fprintf(fp, " -h, --help This help message\n"); |
113 | fprintf(fp, " -l, --list List available formats\n"); | |
114 | fprintf(fp, " -v, --verbose Verbose mode\n"); | |
cba1661c | 115 | fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n"); |
4d5678b9 | 116 | fprintf(fp, " -d, --debug Debug mode\n"); |
cba1661c | 117 | fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n"); |
8d8ed9af | 118 | fprintf(fp, " --no-delta Do not print time delta between consecutive events\n"); |
359d7456 | 119 | fprintf(fp, " -n, --names name1<,name2,...> Print field names:\n"); |
82662ad4 MD |
120 | fprintf(fp, " (payload OR args OR arg)\n"); |
121 | fprintf(fp, " all, scope, header, (context OR ctx)\n"); | |
cba1661c | 122 | fprintf(fp, " (payload active by default)\n"); |
359d7456 MD |
123 | fprintf(fp, " -f, --fields name1<,name2,...> Print additional fields:\n"); |
124 | fprintf(fp, " all, trace, trace:domain, trace:procname,\n"); | |
125 | fprintf(fp, " trace:vpid, loglevel.\n"); | |
11ac6674 MD |
126 | fprintf(fp, " --clock-raw Disregard internal clock offset (use raw value)\n"); |
127 | fprintf(fp, " --clock-offset seconds Clock offset in seconds\n"); | |
128 | fprintf(fp, " --clock-seconds Print the timestamps as [sec.ns]\n"); | |
129 | fprintf(fp, " (default is: [hh:mm:ss.ns])\n"); | |
130 | fprintf(fp, " --clock-date Print clock date\n"); | |
131 | fprintf(fp, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n"); | |
7fb21036 | 132 | list_formats(fp); |
34ac0e6c MD |
133 | fprintf(fp, "\n"); |
134 | } | |
135 | ||
cba1661c MD |
136 | static int get_names_args(poptContext *pc) |
137 | { | |
138 | char *str, *strlist, *strctx; | |
139 | ||
140 | opt_payload_field_names = 0; | |
141 | strlist = (char *) poptGetOptArg(*pc); | |
142 | if (!strlist) { | |
143 | return -EINVAL; | |
144 | } | |
145 | str = strtok_r(strlist, ",", &strctx); | |
146 | do { | |
147 | if (!strcmp(str, "all")) | |
148 | opt_all_field_names = 1; | |
149 | else if (!strcmp(str, "scope")) | |
150 | opt_scope_field_names = 1; | |
151 | else if (!strcmp(str, "context") || !strcmp(str, "ctx")) | |
152 | opt_context_field_names = 1; | |
153 | else if (!strcmp(str, "header")) | |
154 | opt_header_field_names = 1; | |
155 | else if (!strcmp(str, "payload") || !strcmp(str, "args") || !strcmp(str, "arg")) | |
156 | opt_payload_field_names = 1; | |
359d7456 MD |
157 | else { |
158 | fprintf(stderr, "[error] unknown field name type %s\n", str); | |
159 | return -EINVAL; | |
160 | } | |
161 | } while ((str = strtok_r(NULL, ",", &strctx))); | |
162 | return 0; | |
163 | } | |
164 | ||
165 | static int get_fields_args(poptContext *pc) | |
166 | { | |
167 | char *str, *strlist, *strctx; | |
168 | ||
169 | opt_payload_field_names = 0; | |
170 | strlist = (char *) poptGetOptArg(*pc); | |
171 | if (!strlist) { | |
172 | return -EINVAL; | |
173 | } | |
174 | str = strtok_r(strlist, ",", &strctx); | |
175 | do { | |
176 | if (!strcmp(str, "all")) | |
177 | opt_all_fields = 1; | |
82662ad4 | 178 | else if (!strcmp(str, "trace")) |
359d7456 | 179 | opt_trace_field = 1; |
8c250d87 | 180 | else if (!strcmp(str, "trace:domain")) |
359d7456 | 181 | opt_trace_domain_field = 1; |
8c250d87 | 182 | else if (!strcmp(str, "trace:procname")) |
359d7456 | 183 | opt_trace_procname_field = 1; |
8c250d87 | 184 | else if (!strcmp(str, "trace:vpid")) |
359d7456 | 185 | opt_trace_vpid_field = 1; |
d86d62f8 | 186 | else if (!strcmp(str, "loglevel")) |
359d7456 | 187 | opt_loglevel_field = 1; |
cba1661c | 188 | else { |
359d7456 | 189 | fprintf(stderr, "[error] unknown field type %s\n", str); |
cba1661c MD |
190 | return -EINVAL; |
191 | } | |
192 | } while ((str = strtok_r(NULL, ",", &strctx))); | |
193 | return 0; | |
194 | } | |
195 | ||
34ac0e6c MD |
196 | /* |
197 | * Return 0 if caller should continue, < 0 if caller should return | |
198 | * error, > 0 if caller should exit without reporting error. | |
199 | */ | |
bbefb8dd | 200 | static int parse_options(int argc, char **argv) |
34ac0e6c MD |
201 | { |
202 | poptContext pc; | |
203 | int opt, ret = 0; | |
204 | ||
0f980a35 MD |
205 | if (argc == 1) { |
206 | usage(stdout); | |
207 | return 1; /* exit cleanly */ | |
208 | } | |
209 | ||
bbefb8dd | 210 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); |
34ac0e6c MD |
211 | poptReadDefaultConfig(pc, 0); |
212 | ||
cba1661c MD |
213 | /* set default */ |
214 | opt_payload_field_names = 1; | |
215 | ||
34ac0e6c MD |
216 | while ((opt = poptGetNextOpt(pc)) != -1) { |
217 | switch (opt) { | |
218 | case OPT_HELP: | |
7fb21036 | 219 | usage(stdout); |
34ac0e6c MD |
220 | ret = 1; /* exit cleanly */ |
221 | goto end; | |
7fb21036 MD |
222 | case OPT_LIST: |
223 | list_formats(stdout); | |
224 | ret = 1; | |
225 | goto end; | |
34ac0e6c MD |
226 | case OPT_VERBOSE: |
227 | babeltrace_verbose = 1; | |
228 | break; | |
cba1661c MD |
229 | case OPT_NAMES: |
230 | if (get_names_args(&pc)) { | |
231 | ret = -EINVAL; | |
232 | goto end; | |
233 | } | |
234 | break; | |
359d7456 MD |
235 | case OPT_FIELDS: |
236 | if (get_fields_args(&pc)) { | |
237 | ret = -EINVAL; | |
238 | goto end; | |
239 | } | |
240 | break; | |
34ac0e6c MD |
241 | case OPT_DEBUG: |
242 | babeltrace_debug = 1; | |
243 | break; | |
8d8ed9af | 244 | case OPT_NO_DELTA: |
359d7456 | 245 | opt_delta_field = 0; |
8d8ed9af | 246 | break; |
11ac6674 MD |
247 | case OPT_CLOCK_RAW: |
248 | opt_clock_raw = 1; | |
249 | break; | |
250 | case OPT_CLOCK_OFFSET: | |
251 | { | |
252 | char *str, *endptr; | |
253 | ||
254 | str = poptGetOptArg(pc); | |
255 | if (!str) { | |
256 | fprintf(stderr, "[error] Missing --clock-offset argument\n"); | |
257 | ret = -EINVAL; | |
258 | goto end; | |
259 | } | |
260 | errno = 0; | |
261 | opt_clock_offset = strtoull(str, &endptr, 0); | |
262 | if (*endptr != '\0' || str == endptr || errno != 0) { | |
263 | fprintf(stderr, "[error] Incorrect --clock-offset argument: %s\n", str); | |
264 | ret = -EINVAL; | |
265 | goto end; | |
266 | } | |
267 | break; | |
268 | } | |
269 | case OPT_CLOCK_SECONDS: | |
270 | opt_clock_seconds = 1; | |
271 | break; | |
272 | case OPT_CLOCK_DATE: | |
273 | opt_clock_date = 1; | |
274 | break; | |
275 | case OPT_CLOCK_GMT: | |
276 | opt_clock_gmt = 1; | |
277 | break; | |
278 | ||
34ac0e6c MD |
279 | default: |
280 | ret = -EINVAL; | |
281 | goto end; | |
282 | } | |
283 | } | |
284 | ||
285 | opt_input_path = poptGetArg(pc); | |
286 | if (!opt_input_path) { | |
287 | ret = -EINVAL; | |
288 | goto end; | |
289 | } | |
290 | opt_output_path = poptGetArg(pc); | |
cba1661c | 291 | |
34ac0e6c MD |
292 | end: |
293 | if (pc) { | |
294 | poptFreeContext(pc); | |
295 | } | |
296 | return ret; | |
297 | } | |
298 | ||
afb48eae AA |
299 | static void init_trace_collection(struct trace_collection *tc) |
300 | { | |
301 | tc->array = g_ptr_array_sized_new(DEFAULT_FILE_ARRAY_SIZE); | |
a44bc4c9 | 302 | tc->clocks = g_hash_table_new(g_direct_hash, g_direct_equal); |
afb48eae AA |
303 | } |
304 | ||
305 | /* | |
306 | * finalize_trace_collection() closes the opened traces for read | |
307 | * and free the memory allocated for trace collection | |
308 | */ | |
309 | static void finalize_trace_collection(struct trace_collection *tc) | |
310 | { | |
311 | int i; | |
312 | ||
313 | for (i = 0; i < tc->array->len; i++) { | |
314 | struct trace_descriptor *temp = | |
315 | g_ptr_array_index(tc->array, i); | |
316 | fmt_read->close_trace(temp); | |
317 | } | |
318 | g_ptr_array_free(tc->array, TRUE); | |
a44bc4c9 MD |
319 | g_hash_table_destroy(tc->clocks); |
320 | } | |
321 | ||
322 | struct clock_match { | |
323 | GHashTable *clocks; | |
324 | struct ctf_clock *clock_match; | |
325 | }; | |
326 | ||
327 | static void check_clock_match(gpointer key, gpointer value, gpointer user_data) | |
328 | { | |
329 | struct clock_match *match = user_data; | |
330 | struct ctf_clock *clock_a = value, *clock_b; | |
331 | ||
332 | if (clock_a->uuid != 0) { | |
333 | /* | |
334 | * Lookup the the trace clocks into the collection | |
335 | * clocks. | |
336 | */ | |
337 | clock_b = g_hash_table_lookup(match->clocks, | |
338 | (gpointer) (unsigned long) clock_a->uuid); | |
339 | if (clock_b) { | |
340 | match->clock_match = clock_b; | |
341 | return; | |
342 | } | |
343 | } else if (clock_a->absolute) { | |
344 | /* | |
345 | * Absolute time references, such as NTP, are looked up | |
346 | * by clock name. | |
347 | */ | |
348 | clock_b = g_hash_table_lookup(match->clocks, | |
349 | (gpointer) (unsigned long) clock_a->name); | |
350 | if (clock_b) { | |
351 | match->clock_match = clock_b; | |
352 | return; | |
353 | } | |
354 | } | |
355 | } | |
356 | ||
357 | static void clock_add(gpointer key, gpointer value, gpointer user_data) | |
358 | { | |
359 | GHashTable *tc_clocks = user_data; | |
360 | struct ctf_clock *t_clock = value; | |
361 | GQuark v; | |
362 | ||
363 | if (t_clock->absolute) | |
364 | v = t_clock->name; | |
365 | else | |
366 | v = t_clock->uuid; | |
367 | if (v) | |
368 | g_hash_table_insert(tc_clocks, | |
369 | (gpointer) (unsigned long) v, | |
370 | value); | |
afb48eae AA |
371 | } |
372 | ||
a44bc4c9 MD |
373 | /* |
374 | * Whenever we add a trace to the trace collection, check that we can | |
375 | * correlate this trace with at least one other clock in the trace. | |
376 | */ | |
377 | static int trace_collection_add(struct trace_collection *tc, | |
378 | struct trace_descriptor *td) | |
afb48eae | 379 | { |
a44bc4c9 MD |
380 | struct ctf_trace *trace = container_of(td, struct ctf_trace, parent); |
381 | ||
afb48eae | 382 | g_ptr_array_add(tc->array, td); |
a44bc4c9 MD |
383 | |
384 | if (tc->array->len > 1) { | |
385 | struct clock_match clock_match = { | |
386 | .clocks = tc->clocks, | |
387 | .clock_match = NULL, | |
388 | }; | |
389 | ||
390 | /* | |
391 | * With two or more traces, we need correlation info | |
392 | * avalable. | |
393 | */ | |
394 | g_hash_table_foreach(trace->clocks, | |
395 | check_clock_match, | |
396 | &clock_match); | |
397 | if (!clock_match.clock_match) { | |
398 | fprintf(stderr, "[error] No clocks can be correlated and multiple traces are added to the collection.\n"); | |
399 | goto error; | |
400 | } | |
401 | } | |
402 | /* | |
403 | * Add each clock from the trace clocks into the trace | |
404 | * collection clocks. | |
405 | */ | |
406 | g_hash_table_foreach(trace->clocks, | |
407 | clock_add, | |
408 | tc->clocks); | |
409 | return 0; | |
410 | error: | |
411 | return -EPERM; | |
afb48eae AA |
412 | } |
413 | ||
95d36295 JD |
414 | int convert_trace(struct trace_descriptor *td_write, |
415 | struct bt_context *ctx) | |
416 | { | |
417 | struct babeltrace_iter *iter; | |
418 | struct ctf_stream *stream; | |
419 | struct ctf_stream_event *event; | |
420 | struct ctf_text_stream_pos *sout; | |
421 | struct trace_collection_pos begin_pos; | |
422 | int ret; | |
423 | ||
424 | sout = container_of(td_write, struct ctf_text_stream_pos, | |
425 | trace_descriptor); | |
426 | ||
427 | begin_pos.type = BT_SEEK_BEGIN; | |
428 | iter = babeltrace_iter_create(ctx, &begin_pos, NULL); | |
429 | if (!iter) { | |
430 | ret = -1; | |
431 | goto error_iter; | |
432 | } | |
433 | while (babeltrace_iter_read_event(iter, &stream, &event) == 0) { | |
434 | ret = sout->parent.event_cb(&sout->parent, stream); | |
435 | if (ret) { | |
3394d22e | 436 | fprintf(stderr, "[error] Writing event failed.\n"); |
95d36295 JD |
437 | goto end; |
438 | } | |
439 | ret = babeltrace_iter_next(iter); | |
440 | if (ret < 0) | |
441 | goto end; | |
442 | } | |
443 | ret = 0; | |
444 | ||
445 | end: | |
446 | babeltrace_iter_destroy(iter); | |
447 | error_iter: | |
448 | return ret; | |
449 | } | |
450 | ||
451 | ||
afb48eae AA |
452 | /* |
453 | * traverse_dir() is the callback functiion for File Tree Walk (nftw). | |
454 | * it receives the path of the current entry (file, dir, link..etc) with | |
455 | * a flag to indicate the type of the entry. | |
456 | * if the entry being visited is a directory and contains a metadata file, | |
457 | * then open it for reading and save a trace_descriptor to that directory | |
458 | * in the read trace collection. | |
459 | */ | |
460 | static int traverse_dir(const char *fpath, const struct stat *sb, | |
461 | int tflag, struct FTW *ftwbuf) | |
462 | { | |
463 | int dirfd; | |
464 | int fd; | |
465 | struct trace_descriptor *td_read; | |
a44bc4c9 | 466 | int ret; |
afb48eae AA |
467 | |
468 | if (tflag != FTW_D) | |
469 | return 0; | |
470 | dirfd = open(fpath, 0); | |
471 | if (dirfd < 0) { | |
3394d22e | 472 | fprintf(stderr, "[error] unable to open trace " |
afb48eae AA |
473 | "directory file descriptor.\n"); |
474 | return -1; | |
475 | } | |
476 | fd = openat(dirfd, "metadata", O_RDONLY); | |
477 | if (fd < 0) { | |
478 | close(dirfd); | |
479 | } else { | |
480 | close(fd); | |
481 | close(dirfd); | |
8c250d87 MD |
482 | td_read = fmt_read->open_trace(opt_input_path, |
483 | fpath, O_RDONLY, ctf_move_pos_slow, | |
ae23d232 | 484 | NULL); |
afb48eae | 485 | if (!td_read) { |
3394d22e | 486 | fprintf(stderr, "Error opening trace \"%s\" " |
afb48eae AA |
487 | "for reading.\n\n", fpath); |
488 | return -1; /* error */ | |
489 | } | |
a44bc4c9 MD |
490 | ret = trace_collection_add(&trace_collection_read, td_read); |
491 | if (ret) { | |
492 | return -1; | |
493 | } | |
afb48eae AA |
494 | } |
495 | return 0; /* success */ | |
496 | } | |
497 | ||
bbefb8dd | 498 | int main(int argc, char **argv) |
34ac0e6c MD |
499 | { |
500 | int ret; | |
afb48eae AA |
501 | struct format *fmt_write; |
502 | struct trace_descriptor *td_write; | |
95d36295 | 503 | struct bt_context *ctx; |
34ac0e6c MD |
504 | |
505 | ret = parse_options(argc, argv); | |
506 | if (ret < 0) { | |
3394d22e MD |
507 | fprintf(stderr, "Error parsing options.\n\n"); |
508 | usage(stderr); | |
34ac0e6c MD |
509 | exit(EXIT_FAILURE); |
510 | } else if (ret > 0) { | |
511 | exit(EXIT_SUCCESS); | |
512 | } | |
513 | printf_verbose("Verbose mode active.\n"); | |
514 | printf_debug("Debug mode active.\n"); | |
515 | ||
bbefb8dd MD |
516 | if (opt_input_format) |
517 | strlower(opt_input_format); | |
518 | if (opt_output_format) | |
519 | strlower(opt_output_format); | |
520 | ||
afb48eae | 521 | printf_verbose("Converting from directory: %s\n", opt_input_path); |
34ac0e6c | 522 | printf_verbose("Converting from format: %s\n", |
b61922b5 | 523 | opt_input_format ? : "ctf <default>"); |
afb48eae | 524 | printf_verbose("Converting to directory: %s\n", |
478b6389 | 525 | opt_output_path ? : "<stdout>"); |
34ac0e6c | 526 | printf_verbose("Converting to format: %s\n", |
b61922b5 | 527 | opt_output_format ? : "text <default>"); |
bbefb8dd | 528 | |
b61922b5 MD |
529 | if (!opt_input_format) |
530 | opt_input_format = "ctf"; | |
531 | if (!opt_output_format) | |
532 | opt_output_format = "text"; | |
bbefb8dd MD |
533 | fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format)); |
534 | if (!fmt_read) { | |
3394d22e | 535 | fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n", |
bbefb8dd MD |
536 | opt_input_format); |
537 | exit(EXIT_FAILURE); | |
538 | } | |
bbefb8dd MD |
539 | fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format)); |
540 | if (!fmt_write) { | |
3394d22e | 541 | fprintf(stderr, "[error] format \"%s\" is not supported.\n\n", |
bbefb8dd MD |
542 | opt_output_format); |
543 | exit(EXIT_FAILURE); | |
544 | } | |
545 | ||
afb48eae AA |
546 | /* |
547 | * pass the input path to nftw() . | |
548 | * specify traverse_dir() as the callback function. | |
549 | * depth = 10 which is the max number of file descriptors | |
550 | * that nftw() can open at a given time. | |
551 | * flags = 0 check nftw documentation for more info . | |
552 | */ | |
553 | init_trace_collection(&trace_collection_read); | |
554 | ret = nftw(opt_input_path, traverse_dir, 10, 0); | |
555 | if (ret != 0) { | |
3394d22e | 556 | fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n", |
bbefb8dd MD |
557 | opt_input_path); |
558 | goto error_td_read; | |
559 | } | |
afb48eae | 560 | if (trace_collection_read.array->len == 0) { |
3394d22e | 561 | fprintf(stderr, "[warning] no metadata file was found." |
afb48eae AA |
562 | " no output was generated\n"); |
563 | return 0; | |
564 | } | |
95d36295 JD |
565 | ctx = bt_context_create(&trace_collection_read); |
566 | if (!ctx) { | |
3394d22e | 567 | fprintf(stderr, "Error allocating a new context\n"); |
95d36295 JD |
568 | goto error_td_read; |
569 | } | |
8c250d87 | 570 | td_write = fmt_write->open_trace(NULL, opt_output_path, O_RDWR, NULL, NULL); |
bbefb8dd | 571 | if (!td_write) { |
3394d22e | 572 | fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n", |
b61922b5 | 573 | opt_output_path ? : "<none>"); |
bbefb8dd MD |
574 | goto error_td_write; |
575 | } | |
847bf71a | 576 | |
95d36295 | 577 | ret = convert_trace(td_write, ctx); |
46322b33 | 578 | if (ret) { |
3394d22e | 579 | fprintf(stderr, "Error printing trace.\n\n"); |
46322b33 MD |
580 | goto error_copy_trace; |
581 | } | |
847bf71a | 582 | |
bbefb8dd | 583 | fmt_write->close_trace(td_write); |
afb48eae | 584 | finalize_trace_collection(&trace_collection_read); |
95d36295 | 585 | bt_context_destroy(ctx); |
afb48eae AA |
586 | printf_verbose("finished converting. Output written to:\n%s\n", |
587 | opt_output_path ? : "<stdout>"); | |
bbefb8dd | 588 | exit(EXIT_SUCCESS); |
34ac0e6c | 589 | |
bbefb8dd | 590 | /* Error handling */ |
46322b33 | 591 | error_copy_trace: |
bbefb8dd MD |
592 | fmt_write->close_trace(td_write); |
593 | error_td_write: | |
afb48eae | 594 | finalize_trace_collection(&trace_collection_read); |
bbefb8dd MD |
595 | error_td_read: |
596 | exit(EXIT_FAILURE); | |
4c8bfb7e | 597 | } |