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