Fix: test all close/fclose ret val, fix double close
[babeltrace.git] / converter / babeltrace.c
CommitLineData
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
20d24215 21#define _GNU_SOURCE
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>
9843982d 27#include <babeltrace/ctf/events.h>
04ae3991
MD
28/* TODO: fix object model for format-agnostic callbacks */
29#include <babeltrace/ctf/events-internal.h>
9efd5d76 30#include <babeltrace/ctf/iterator.h>
31bdef5c 31#include <babeltrace/ctf-text/types.h>
6204d33c 32#include <babeltrace/iterator.h>
34ac0e6c
MD
33#include <popt.h>
34#include <errno.h>
35#include <stdlib.h>
bbefb8dd
MD
36#include <ctype.h>
37#include <sys/stat.h>
38#include <sys/types.h>
39#include <fcntl.h>
afb48eae 40#include <unistd.h>
70accc14 41#include <inttypes.h>
a99343cf 42#include <ftw.h>
11e1d048 43#include <string.h>
4c8bfb7e 44
a44bc4c9
MD
45#include <babeltrace/ctf-ir/metadata.h> /* for clocks */
46
24d5af5b
MD
47#define PARTIAL_ERROR_SLEEP 3 /* 3 seconds */
48
afb48eae 49#define DEFAULT_FILE_ARRAY_SIZE 1
9fe26ec7
MD
50
51/*
52 * casting these to const char * to please libpopt. It allocates memory
53 * behind our back (this is of course not documented).
54 */
11e1d048 55static char *opt_input_format, *opt_output_format;
34ac0e6c 56
4e85cbfd 57static GPtrArray *opt_input_paths;
9fe26ec7 58static char *opt_output_path;
34ac0e6c 59
afb48eae
AA
60static struct format *fmt_read;
61
11e1d048 62static
bbefb8dd
MD
63void strlower(char *str)
64{
65 while (*str) {
34224260 66 *str = tolower((int) *str);
bbefb8dd
MD
67 str++;
68 }
69}
70
34ac0e6c
MD
71enum {
72 OPT_NONE = 0,
9fe26ec7
MD
73 OPT_OUTPUT_PATH,
74 OPT_INPUT_FORMAT,
75 OPT_OUTPUT_FORMAT,
34ac0e6c 76 OPT_HELP,
7fb21036 77 OPT_LIST,
34ac0e6c
MD
78 OPT_VERBOSE,
79 OPT_DEBUG,
d63ca2cd 80 OPT_NAMES,
359d7456 81 OPT_FIELDS,
8d8ed9af 82 OPT_NO_DELTA,
11ac6674 83 OPT_CLOCK_OFFSET,
03798a93 84 OPT_CLOCK_CYCLES,
11ac6674
MD
85 OPT_CLOCK_SECONDS,
86 OPT_CLOCK_DATE,
87 OPT_CLOCK_GMT,
82ace6d6 88 OPT_CLOCK_FORCE_CORRELATE,
34ac0e6c
MD
89};
90
9fe26ec7
MD
91/*
92 * We are _not_ using POPT_ARG_STRING ability to store directly into
93 * variables, because we want to cast the return to non-const, which is
94 * not possible without using poptGetOptArg explicitly.
95 */
34ac0e6c
MD
96static struct poptOption long_options[] = {
97 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
9fe26ec7
MD
98 { "output", 'w', POPT_ARG_STRING, NULL, OPT_NONE, NULL, NULL },
99 { "input-format", 'i', POPT_ARG_STRING, NULL, OPT_INPUT_FORMAT, NULL, NULL },
100 { "output-format", 'o', POPT_ARG_STRING, NULL, OPT_OUTPUT_FORMAT, NULL, NULL },
34ac0e6c 101 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
7fb21036 102 { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL },
34ac0e6c
MD
103 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
104 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
cba1661c 105 { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
359d7456 106 { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL },
8d8ed9af 107 { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL },
11ac6674 108 { "clock-offset", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL },
03798a93 109 { "clock-cycles", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_CYCLES, NULL, NULL },
11ac6674
MD
110 { "clock-seconds", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_SECONDS, NULL, NULL },
111 { "clock-date", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_DATE, NULL, NULL },
112 { "clock-gmt", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_GMT, NULL, NULL },
82ace6d6 113 { "clock-force-correlate", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_FORCE_CORRELATE, NULL, NULL },
34ac0e6c
MD
114 { NULL, 0, 0, NULL, 0, NULL, NULL },
115};
116
7fb21036
MD
117static void list_formats(FILE *fp)
118{
119 fprintf(fp, "\n");
120 bt_fprintf_format_list(fp);
121}
122
34ac0e6c 123static void usage(FILE *fp)
4c8bfb7e 124{
4c15b06b 125 fprintf(fp, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION);
4e85cbfd 126 fprintf(fp, "usage : babeltrace [OPTIONS] FILE...\n");
34ac0e6c 127 fprintf(fp, "\n");
4e85cbfd
MD
128 fprintf(fp, " FILE Input trace file(s) and/or directory(ies)\n");
129 fprintf(fp, " (space-separated)\n");
130 fprintf(fp, " -w, --output OUTPUT Output trace path (default: stdout)\n");
34ac0e6c 131 fprintf(fp, "\n");
d2b8ea6b
MD
132 fprintf(fp, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
133 fprintf(fp, " -o, --output-format FORMAT Output trace format (default: text)\n");
34ac0e6c 134 fprintf(fp, "\n");
4d5678b9
MD
135 fprintf(fp, " -h, --help This help message\n");
136 fprintf(fp, " -l, --list List available formats\n");
137 fprintf(fp, " -v, --verbose Verbose mode\n");
cba1661c 138 fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n");
4d5678b9 139 fprintf(fp, " -d, --debug Debug mode\n");
cba1661c 140 fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n");
8d8ed9af 141 fprintf(fp, " --no-delta Do not print time delta between consecutive events\n");
359d7456 142 fprintf(fp, " -n, --names name1<,name2,...> Print field names:\n");
82662ad4 143 fprintf(fp, " (payload OR args OR arg)\n");
12c9c3bc
MD
144 fprintf(fp, " none, all, scope, header, (context OR ctx)\n");
145 fprintf(fp, " (default: payload,context)\n");
359d7456 146 fprintf(fp, " -f, --fields name1<,name2,...> Print additional fields:\n");
32cfb8ad 147 fprintf(fp, " all, trace, trace:hostname, trace:domain,\n");
f133896d 148 fprintf(fp, " trace:procname, trace:vpid, loglevel, emf, callsite.\n");
c3815874 149 fprintf(fp, " (default: trace:hostname,trace:procname,trace:vpid)\n");
03798a93 150 fprintf(fp, " --clock-cycles Timestamp in cycles\n");
11ac6674
MD
151 fprintf(fp, " --clock-offset seconds Clock offset in seconds\n");
152 fprintf(fp, " --clock-seconds Print the timestamps as [sec.ns]\n");
153 fprintf(fp, " (default is: [hh:mm:ss.ns])\n");
154 fprintf(fp, " --clock-date Print clock date\n");
155 fprintf(fp, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n");
82ace6d6
MD
156 fprintf(fp, " --clock-force-correlate Assume that clocks are inherently correlated\n");
157 fprintf(fp, " across traces.\n");
7fb21036 158 list_formats(fp);
34ac0e6c
MD
159 fprintf(fp, "\n");
160}
161
cba1661c
MD
162static int get_names_args(poptContext *pc)
163{
164 char *str, *strlist, *strctx;
9f2c779c 165 int ret = 0;
cba1661c
MD
166
167 opt_payload_field_names = 0;
12c9c3bc 168 opt_context_field_names = 0;
cba1661c
MD
169 strlist = (char *) poptGetOptArg(*pc);
170 if (!strlist) {
171 return -EINVAL;
172 }
173 str = strtok_r(strlist, ",", &strctx);
174 do {
175 if (!strcmp(str, "all"))
176 opt_all_field_names = 1;
177 else if (!strcmp(str, "scope"))
178 opt_scope_field_names = 1;
179 else if (!strcmp(str, "context") || !strcmp(str, "ctx"))
180 opt_context_field_names = 1;
181 else if (!strcmp(str, "header"))
182 opt_header_field_names = 1;
183 else if (!strcmp(str, "payload") || !strcmp(str, "args") || !strcmp(str, "arg"))
184 opt_payload_field_names = 1;
12c9c3bc
MD
185 else if (!strcmp(str, "none")) {
186 opt_all_field_names = 0;
187 opt_scope_field_names = 0;
188 opt_context_field_names = 0;
189 opt_header_field_names = 0;
190 opt_payload_field_names = 0;
191 } else {
359d7456 192 fprintf(stderr, "[error] unknown field name type %s\n", str);
9f2c779c
MD
193 free(strlist);
194 ret = -EINVAL;
195 goto end;
359d7456
MD
196 }
197 } while ((str = strtok_r(NULL, ",", &strctx)));
9f2c779c
MD
198end:
199 free(strlist);
200 return ret;
359d7456
MD
201}
202
203static int get_fields_args(poptContext *pc)
204{
205 char *str, *strlist, *strctx;
9f2c779c 206 int ret = 0;
359d7456 207
359d7456
MD
208 strlist = (char *) poptGetOptArg(*pc);
209 if (!strlist) {
210 return -EINVAL;
211 }
212 str = strtok_r(strlist, ",", &strctx);
213 do {
c3815874 214 opt_trace_default_fields = 0;
359d7456
MD
215 if (!strcmp(str, "all"))
216 opt_all_fields = 1;
82662ad4 217 else if (!strcmp(str, "trace"))
359d7456 218 opt_trace_field = 1;
c3815874
MD
219 else if (!strcmp(str, "trace:hostname"))
220 opt_trace_hostname_field = 1;
8c250d87 221 else if (!strcmp(str, "trace:domain"))
359d7456 222 opt_trace_domain_field = 1;
8c250d87 223 else if (!strcmp(str, "trace:procname"))
359d7456 224 opt_trace_procname_field = 1;
8c250d87 225 else if (!strcmp(str, "trace:vpid"))
359d7456 226 opt_trace_vpid_field = 1;
d86d62f8 227 else if (!strcmp(str, "loglevel"))
359d7456 228 opt_loglevel_field = 1;
f6714e20
MD
229 else if (!strcmp(str, "emf"))
230 opt_emf_field = 1;
f133896d
MD
231 else if (!strcmp(str, "callsite"))
232 opt_callsite_field = 1;
cba1661c 233 else {
359d7456 234 fprintf(stderr, "[error] unknown field type %s\n", str);
9f2c779c
MD
235 ret = -EINVAL;
236 goto end;
cba1661c
MD
237 }
238 } while ((str = strtok_r(NULL, ",", &strctx)));
9f2c779c
MD
239end:
240 free(strlist);
241 return ret;
cba1661c
MD
242}
243
34ac0e6c
MD
244/*
245 * Return 0 if caller should continue, < 0 if caller should return
246 * error, > 0 if caller should exit without reporting error.
247 */
bbefb8dd 248static int parse_options(int argc, char **argv)
34ac0e6c
MD
249{
250 poptContext pc;
251 int opt, ret = 0;
9fe26ec7 252 char *ipath;
34ac0e6c 253
0f980a35
MD
254 if (argc == 1) {
255 usage(stdout);
256 return 1; /* exit cleanly */
257 }
258
bbefb8dd 259 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
34ac0e6c
MD
260 poptReadDefaultConfig(pc, 0);
261
cba1661c 262 /* set default */
e505794f 263 opt_context_field_names = 1;
cba1661c
MD
264 opt_payload_field_names = 1;
265
34ac0e6c
MD
266 while ((opt = poptGetNextOpt(pc)) != -1) {
267 switch (opt) {
9fe26ec7
MD
268 case OPT_OUTPUT_PATH:
269 opt_output_path = (char *) poptGetOptArg(pc);
270 if (!opt_output_path) {
271 ret = -EINVAL;
272 goto end;
273 }
274 break;
275 case OPT_INPUT_FORMAT:
276 opt_input_format = (char *) poptGetOptArg(pc);
277 if (!opt_input_format) {
278 ret = -EINVAL;
279 goto end;
280 }
281 break;
282 case OPT_OUTPUT_FORMAT:
283 opt_output_format = (char *) poptGetOptArg(pc);
284 if (!opt_output_format) {
285 ret = -EINVAL;
286 goto end;
287 }
288 break;
34ac0e6c 289 case OPT_HELP:
7fb21036 290 usage(stdout);
34ac0e6c
MD
291 ret = 1; /* exit cleanly */
292 goto end;
7fb21036
MD
293 case OPT_LIST:
294 list_formats(stdout);
295 ret = 1;
296 goto end;
34ac0e6c
MD
297 case OPT_VERBOSE:
298 babeltrace_verbose = 1;
299 break;
cba1661c
MD
300 case OPT_NAMES:
301 if (get_names_args(&pc)) {
302 ret = -EINVAL;
303 goto end;
304 }
305 break;
359d7456
MD
306 case OPT_FIELDS:
307 if (get_fields_args(&pc)) {
308 ret = -EINVAL;
309 goto end;
310 }
311 break;
34ac0e6c
MD
312 case OPT_DEBUG:
313 babeltrace_debug = 1;
314 break;
8d8ed9af 315 case OPT_NO_DELTA:
359d7456 316 opt_delta_field = 0;
8d8ed9af 317 break;
03798a93
JD
318 case OPT_CLOCK_CYCLES:
319 opt_clock_cycles = 1;
11ac6674
MD
320 break;
321 case OPT_CLOCK_OFFSET:
322 {
9f2c779c 323 char *str;
34224260 324 char *endptr;
11ac6674 325
9f2c779c 326 str = (char *) poptGetOptArg(pc);
11ac6674
MD
327 if (!str) {
328 fprintf(stderr, "[error] Missing --clock-offset argument\n");
329 ret = -EINVAL;
330 goto end;
331 }
332 errno = 0;
333 opt_clock_offset = strtoull(str, &endptr, 0);
334 if (*endptr != '\0' || str == endptr || errno != 0) {
335 fprintf(stderr, "[error] Incorrect --clock-offset argument: %s\n", str);
336 ret = -EINVAL;
9f2c779c 337 free(str);
11ac6674
MD
338 goto end;
339 }
9f2c779c 340 free(str);
11ac6674
MD
341 break;
342 }
343 case OPT_CLOCK_SECONDS:
344 opt_clock_seconds = 1;
345 break;
346 case OPT_CLOCK_DATE:
347 opt_clock_date = 1;
348 break;
349 case OPT_CLOCK_GMT:
350 opt_clock_gmt = 1;
351 break;
82ace6d6
MD
352 case OPT_CLOCK_FORCE_CORRELATE:
353 opt_clock_force_correlate = 1;
354 break;
11ac6674 355
34ac0e6c
MD
356 default:
357 ret = -EINVAL;
358 goto end;
359 }
360 }
361
4e85cbfd 362 do {
9fe26ec7 363 ipath = (char *) poptGetArg(pc);
4e85cbfd
MD
364 if (ipath)
365 g_ptr_array_add(opt_input_paths, (gpointer) ipath);
366 } while (ipath);
367 if (opt_input_paths->len == 0) {
34ac0e6c
MD
368 ret = -EINVAL;
369 goto end;
370 }
cba1661c 371
34ac0e6c
MD
372end:
373 if (pc) {
374 poptFreeContext(pc);
375 }
376 return ret;
377}
378
a99343cf 379static GPtrArray *traversed_paths = 0;
afb48eae 380
a99343cf
YB
381/*
382 * traverse_trace_dir() is the callback function for File Tree Walk (nftw).
383 * it receives the path of the current entry (file, dir, link..etc) with
384 * a flag to indicate the type of the entry.
385 * if the entry being visited is a directory and contains a metadata file,
386 * then add the path to a global list to be processed later in
387 * add_traces_recursive.
388 */
389static int traverse_trace_dir(const char *fpath, const struct stat *sb,
390 int tflag, struct FTW *ftwbuf)
391{
392 int dirfd, metafd;
393 int closeret;
394
395 if (tflag != FTW_D)
396 return 0;
397
398 dirfd = open(fpath, 0);
399 if (dirfd < 0) {
400 fprintf(stderr, "[error] [Context] Unable to open trace "
401 "directory file descriptor.\n");
402 return 0; /* partial error */
403 }
404 metafd = openat(dirfd, "metadata", O_RDONLY);
405 if (metafd < 0) {
406 closeret = close(dirfd);
407 if (closeret < 0) {
408 perror("close");
409 return -1;
410 }
411 /* No meta data, just return */
412 return 0;
413 } else {
414 closeret = close(metafd);
415 if (closeret < 0) {
416 perror("close");
417 return -1; /* failure */
418 }
419 closeret = close(dirfd);
420 if (closeret < 0) {
421 perror("close");
422 return -1; /* failure */
423 }
424
425 /* Add path to the global list */
426 if (traversed_paths == NULL) {
427 fprintf(stderr, "[error] [Context] Invalid open path array.\n");
428 return -1;
429 }
430 g_ptr_array_add(traversed_paths, g_string_new(fpath));
431 }
432
433 return 0;
434}
d0d82191 435
20d24215
YB
436/*
437 * bt_context_add_traces_recursive: Open a trace recursively
438 *
439 * Find each trace present in the subdirectory starting from the given
613f532b
MD
440 * path, and add them to the context. The packet_seek parameter can be
441 * NULL: this specify to use the default format packet_seek.
20d24215 442 *
b945d4a5 443 * Return: 0 on success, < 0 on failure, > 0 on partial failure.
20d24215 444 * Unable to open toplevel: failure.
b945d4a5
MD
445 * Unable to open some subdirectory or file: warn and continue (partial
446 * failure);
20d24215
YB
447 */
448int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
613f532b
MD
449 const char *format_str,
450 void (*packet_seek)(struct stream_pos *pos,
451 size_t offset, int whence))
20d24215 452{
a99343cf 453
20d24215 454 GArray *trace_ids;
b945d4a5 455 int ret = 0;
a99343cf 456 int i;
20d24215 457
a99343cf 458 /* Should lock traversed_paths mutex here if used in multithread */
20d24215 459
a99343cf 460 traversed_paths = g_ptr_array_new();
20d24215
YB
461 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
462
a99343cf
YB
463 ret = nftw(path, traverse_trace_dir, 10, 0);
464
465 /* Process the array if ntfw did not return a fatal error */
466 if (ret >= 0) {
467 for (i = 0; i < traversed_paths->len; i++) {
468 GString *trace_path = g_ptr_array_index(traversed_paths,
469 i);
470 int trace_id = bt_context_add_trace(ctx,
471 trace_path->str,
472 format_str,
473 packet_seek,
474 NULL,
475 NULL);
20d24215 476 if (trace_id < 0) {
7f89ddce 477 fprintf(stderr, "[warning] [Context] cannot open trace \"%s\" from %s "
a99343cf 478 "for reading.\n", trace_path->str, path);
ca718275 479 /* Allow to skip erroneous traces. */
b945d4a5 480 ret = 1; /* partial error */
a99343cf
YB
481 } else {
482 g_array_append_val(trace_ids, trace_id);
20d24215 483 }
a99343cf 484 g_string_free(trace_path, TRUE);
20d24215
YB
485 }
486 }
d0d82191 487
a99343cf
YB
488 g_ptr_array_free(traversed_paths, TRUE);
489 traversed_paths = NULL;
490
491 /* Should unlock traversed paths mutex here if used in multithread */
20d24215 492
27cd3890
MD
493 /*
494 * Return an error if no trace can be opened.
495 */
b945d4a5 496 if (trace_ids->len == 0) {
27cd3890 497 fprintf(stderr, "[error] Cannot open any trace for reading.\n\n");
b945d4a5 498 ret = -ENOENT; /* failure */
27cd3890
MD
499 }
500 g_array_free(trace_ids, TRUE);
20d24215
YB
501 return ret;
502}
a44bc4c9 503
95d36295
JD
504int convert_trace(struct trace_descriptor *td_write,
505 struct bt_context *ctx)
506{
e4195791 507 struct bt_ctf_iter *iter;
95d36295 508 struct ctf_text_stream_pos *sout;
e8c92a62 509 struct bt_iter_pos begin_pos;
c50d2a7a 510 struct bt_ctf_event *ctf_event;
95d36295
JD
511 int ret;
512
513 sout = container_of(td_write, struct ctf_text_stream_pos,
514 trace_descriptor);
515
516 begin_pos.type = BT_SEEK_BEGIN;
e4195791 517 iter = bt_ctf_iter_create(ctx, &begin_pos, NULL);
95d36295
JD
518 if (!iter) {
519 ret = -1;
520 goto error_iter;
521 }
e4195791 522 while ((ctf_event = bt_ctf_iter_read_event(iter))) {
c50d2a7a 523 ret = sout->parent.event_cb(&sout->parent, ctf_event->parent->stream);
95d36295 524 if (ret) {
3394d22e 525 fprintf(stderr, "[error] Writing event failed.\n");
95d36295
JD
526 goto end;
527 }
e4195791 528 ret = bt_iter_next(bt_ctf_get_iter(iter));
95d36295
JD
529 if (ret < 0)
530 goto end;
531 }
532 ret = 0;
533
534end:
e4195791 535 bt_ctf_iter_destroy(iter);
95d36295
JD
536error_iter:
537 return ret;
538}
539
bbefb8dd 540int main(int argc, char **argv)
34ac0e6c 541{
4e85cbfd 542 int ret, partial_error = 0, open_success = 0;
afb48eae
AA
543 struct format *fmt_write;
544 struct trace_descriptor *td_write;
95d36295 545 struct bt_context *ctx;
4e85cbfd
MD
546 int i;
547
548 opt_input_paths = g_ptr_array_new();
34ac0e6c
MD
549
550 ret = parse_options(argc, argv);
551 if (ret < 0) {
3394d22e
MD
552 fprintf(stderr, "Error parsing options.\n\n");
553 usage(stderr);
4e85cbfd 554 g_ptr_array_free(opt_input_paths, TRUE);
34ac0e6c
MD
555 exit(EXIT_FAILURE);
556 } else if (ret > 0) {
4e85cbfd 557 g_ptr_array_free(opt_input_paths, TRUE);
34ac0e6c
MD
558 exit(EXIT_SUCCESS);
559 }
560 printf_verbose("Verbose mode active.\n");
561 printf_debug("Debug mode active.\n");
562
9fe26ec7 563 if (opt_input_format)
bbefb8dd 564 strlower(opt_input_format);
9fe26ec7 565 if (opt_output_format)
bbefb8dd
MD
566 strlower(opt_output_format);
567
4e85cbfd
MD
568 printf_verbose("Converting from directory(ies):\n");
569 for (i = 0; i < opt_input_paths->len; i++) {
570 const char *ipath = g_ptr_array_index(opt_input_paths, i);
571 printf_verbose(" %s\n", ipath);
572 }
34ac0e6c 573 printf_verbose("Converting from format: %s\n",
b61922b5 574 opt_input_format ? : "ctf <default>");
afb48eae 575 printf_verbose("Converting to directory: %s\n",
478b6389 576 opt_output_path ? : "<stdout>");
34ac0e6c 577 printf_verbose("Converting to format: %s\n",
b61922b5 578 opt_output_format ? : "text <default>");
bbefb8dd 579
11e1d048
MD
580 if (!opt_input_format) {
581 opt_input_format = strdup("ctf");
582 if (!opt_input_format) {
583 partial_error = 1;
584 goto end;
585 }
586 }
587 if (!opt_output_format) {
588 opt_output_format = strdup("text");
589 if (!opt_output_format) {
590 partial_error = 1;
591 goto end;
592 }
593 }
bbefb8dd
MD
594 fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
595 if (!fmt_read) {
3394d22e 596 fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n",
bbefb8dd 597 opt_input_format);
11e1d048
MD
598 partial_error = 1;
599 goto end;
bbefb8dd 600 }
bbefb8dd
MD
601 fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
602 if (!fmt_write) {
3394d22e 603 fprintf(stderr, "[error] format \"%s\" is not supported.\n\n",
bbefb8dd 604 opt_output_format);
11e1d048
MD
605 partial_error = 1;
606 goto end;
bbefb8dd
MD
607 }
608
6cba487f 609 ctx = bt_context_create();
d63c2d51
JD
610 if (!ctx) {
611 goto error_td_read;
612 }
6cba487f 613
4e85cbfd
MD
614 for (i = 0; i < opt_input_paths->len; i++) {
615 const char *ipath = g_ptr_array_index(opt_input_paths, i);
616 ret = bt_context_add_traces_recursive(ctx, ipath,
617 opt_input_format, NULL);
618 if (ret < 0) {
619 fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n",
620 ipath);
621 } else if (ret > 0) {
622 fprintf(stderr, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n",
623 ipath);
624 open_success = 1; /* some traces were OK */
625 partial_error = 1;
626 } else {
627 open_success = 1; /* all traces were OK */
628 }
629 }
630 if (!open_success) {
631 fprintf(stderr, "[error] none of the specified trace paths could be opened.\n\n");
bbefb8dd
MD
632 goto error_td_read;
633 }
6cba487f 634
5b80ddfb 635 td_write = fmt_write->open_trace(opt_output_path, O_RDWR, NULL, NULL);
bbefb8dd 636 if (!td_write) {
3394d22e 637 fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n",
b61922b5 638 opt_output_path ? : "<none>");
bbefb8dd
MD
639 goto error_td_write;
640 }
847bf71a 641
24d5af5b
MD
642 /*
643 * Errors happened when opening traces, but we continue anyway.
644 * sleep to let user see the stderr output before stdout.
645 */
4e85cbfd 646 if (partial_error)
24d5af5b 647 sleep(PARTIAL_ERROR_SLEEP);
24d5af5b 648
95d36295 649 ret = convert_trace(td_write, ctx);
46322b33 650 if (ret) {
3394d22e 651 fprintf(stderr, "Error printing trace.\n\n");
46322b33
MD
652 goto error_copy_trace;
653 }
847bf71a 654
bbefb8dd 655 fmt_write->close_trace(td_write);
6cba487f
MD
656
657 bt_context_put(ctx);
afb48eae
AA
658 printf_verbose("finished converting. Output written to:\n%s\n",
659 opt_output_path ? : "<stdout>");
11e1d048 660 goto end;
34ac0e6c 661
bbefb8dd 662 /* Error handling */
46322b33 663error_copy_trace:
bbefb8dd
MD
664 fmt_write->close_trace(td_write);
665error_td_write:
6cba487f 666 bt_context_put(ctx);
bbefb8dd 667error_td_read:
11e1d048
MD
668 partial_error = 1;
669
670 /* teardown and exit */
671end:
672 free(opt_input_format);
673 free(opt_output_format);
9fe26ec7
MD
674 free(opt_output_path);
675 for (i = 0; i < opt_input_paths->len; i++) {
676 char *ipath = g_ptr_array_index(opt_input_paths, i);
677 free(ipath);
678 }
4e85cbfd 679 g_ptr_array_free(opt_input_paths, TRUE);
11e1d048
MD
680 if (partial_error)
681 exit(EXIT_FAILURE);
682 else
683 exit(EXIT_SUCCESS);
4c8bfb7e 684}
This page took 0.062357 seconds and 4 git commands to generate.