Cleanup: fix cppcheck warnings
[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;
20d24215 462
a99343cf 463 /* Should lock traversed_paths mutex here if used in multithread */
20d24215 464
a99343cf 465 traversed_paths = g_ptr_array_new();
20d24215
YB
466 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
467
a99343cf
YB
468 ret = nftw(path, traverse_trace_dir, 10, 0);
469
470 /* Process the array if ntfw did not return a fatal error */
471 if (ret >= 0) {
08c82b90
MD
472 int i;
473
a99343cf
YB
474 for (i = 0; i < traversed_paths->len; i++) {
475 GString *trace_path = g_ptr_array_index(traversed_paths,
476 i);
477 int trace_id = bt_context_add_trace(ctx,
478 trace_path->str,
479 format_str,
480 packet_seek,
481 NULL,
482 NULL);
20d24215 483 if (trace_id < 0) {
7f89ddce 484 fprintf(stderr, "[warning] [Context] cannot open trace \"%s\" from %s "
a99343cf 485 "for reading.\n", trace_path->str, path);
ca718275 486 /* Allow to skip erroneous traces. */
b945d4a5 487 ret = 1; /* partial error */
a99343cf
YB
488 } else {
489 g_array_append_val(trace_ids, trace_id);
20d24215 490 }
a99343cf 491 g_string_free(trace_path, TRUE);
20d24215
YB
492 }
493 }
d0d82191 494
a99343cf
YB
495 g_ptr_array_free(traversed_paths, TRUE);
496 traversed_paths = NULL;
497
498 /* Should unlock traversed paths mutex here if used in multithread */
20d24215 499
27cd3890
MD
500 /*
501 * Return an error if no trace can be opened.
502 */
b945d4a5 503 if (trace_ids->len == 0) {
27cd3890 504 fprintf(stderr, "[error] Cannot open any trace for reading.\n\n");
b945d4a5 505 ret = -ENOENT; /* failure */
27cd3890
MD
506 }
507 g_array_free(trace_ids, TRUE);
20d24215
YB
508 return ret;
509}
a44bc4c9 510
95d36295
JD
511int convert_trace(struct trace_descriptor *td_write,
512 struct bt_context *ctx)
513{
e4195791 514 struct bt_ctf_iter *iter;
95d36295 515 struct ctf_text_stream_pos *sout;
e8c92a62 516 struct bt_iter_pos begin_pos;
c50d2a7a 517 struct bt_ctf_event *ctf_event;
95d36295
JD
518 int ret;
519
520 sout = container_of(td_write, struct ctf_text_stream_pos,
521 trace_descriptor);
522
523 begin_pos.type = BT_SEEK_BEGIN;
e4195791 524 iter = bt_ctf_iter_create(ctx, &begin_pos, NULL);
95d36295
JD
525 if (!iter) {
526 ret = -1;
527 goto error_iter;
528 }
e4195791 529 while ((ctf_event = bt_ctf_iter_read_event(iter))) {
c50d2a7a 530 ret = sout->parent.event_cb(&sout->parent, ctf_event->parent->stream);
95d36295 531 if (ret) {
3394d22e 532 fprintf(stderr, "[error] Writing event failed.\n");
95d36295
JD
533 goto end;
534 }
e4195791 535 ret = bt_iter_next(bt_ctf_get_iter(iter));
95d36295
JD
536 if (ret < 0)
537 goto end;
538 }
539 ret = 0;
540
541end:
e4195791 542 bt_ctf_iter_destroy(iter);
95d36295
JD
543error_iter:
544 return ret;
545}
546
bbefb8dd 547int main(int argc, char **argv)
34ac0e6c 548{
4e85cbfd 549 int ret, partial_error = 0, open_success = 0;
afb48eae
AA
550 struct format *fmt_write;
551 struct trace_descriptor *td_write;
95d36295 552 struct bt_context *ctx;
4e85cbfd
MD
553 int i;
554
555 opt_input_paths = g_ptr_array_new();
34ac0e6c
MD
556
557 ret = parse_options(argc, argv);
558 if (ret < 0) {
3394d22e
MD
559 fprintf(stderr, "Error parsing options.\n\n");
560 usage(stderr);
4e85cbfd 561 g_ptr_array_free(opt_input_paths, TRUE);
34ac0e6c
MD
562 exit(EXIT_FAILURE);
563 } else if (ret > 0) {
4e85cbfd 564 g_ptr_array_free(opt_input_paths, TRUE);
34ac0e6c
MD
565 exit(EXIT_SUCCESS);
566 }
567 printf_verbose("Verbose mode active.\n");
568 printf_debug("Debug mode active.\n");
569
9fe26ec7 570 if (opt_input_format)
bbefb8dd 571 strlower(opt_input_format);
9fe26ec7 572 if (opt_output_format)
bbefb8dd
MD
573 strlower(opt_output_format);
574
4e85cbfd
MD
575 printf_verbose("Converting from directory(ies):\n");
576 for (i = 0; i < opt_input_paths->len; i++) {
577 const char *ipath = g_ptr_array_index(opt_input_paths, i);
578 printf_verbose(" %s\n", ipath);
579 }
34ac0e6c 580 printf_verbose("Converting from format: %s\n",
b61922b5 581 opt_input_format ? : "ctf <default>");
afb48eae 582 printf_verbose("Converting to directory: %s\n",
478b6389 583 opt_output_path ? : "<stdout>");
34ac0e6c 584 printf_verbose("Converting to format: %s\n",
b61922b5 585 opt_output_format ? : "text <default>");
bbefb8dd 586
11e1d048
MD
587 if (!opt_input_format) {
588 opt_input_format = strdup("ctf");
589 if (!opt_input_format) {
590 partial_error = 1;
591 goto end;
592 }
593 }
594 if (!opt_output_format) {
595 opt_output_format = strdup("text");
596 if (!opt_output_format) {
597 partial_error = 1;
598 goto end;
599 }
600 }
bbefb8dd
MD
601 fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
602 if (!fmt_read) {
3394d22e 603 fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n",
bbefb8dd 604 opt_input_format);
11e1d048
MD
605 partial_error = 1;
606 goto end;
bbefb8dd 607 }
bbefb8dd
MD
608 fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
609 if (!fmt_write) {
3394d22e 610 fprintf(stderr, "[error] format \"%s\" is not supported.\n\n",
bbefb8dd 611 opt_output_format);
11e1d048
MD
612 partial_error = 1;
613 goto end;
bbefb8dd
MD
614 }
615
6cba487f 616 ctx = bt_context_create();
d63c2d51
JD
617 if (!ctx) {
618 goto error_td_read;
619 }
6cba487f 620
4e85cbfd
MD
621 for (i = 0; i < opt_input_paths->len; i++) {
622 const char *ipath = g_ptr_array_index(opt_input_paths, i);
623 ret = bt_context_add_traces_recursive(ctx, ipath,
624 opt_input_format, NULL);
625 if (ret < 0) {
626 fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n",
627 ipath);
628 } else if (ret > 0) {
629 fprintf(stderr, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n",
630 ipath);
631 open_success = 1; /* some traces were OK */
632 partial_error = 1;
633 } else {
634 open_success = 1; /* all traces were OK */
635 }
636 }
637 if (!open_success) {
638 fprintf(stderr, "[error] none of the specified trace paths could be opened.\n\n");
bbefb8dd
MD
639 goto error_td_read;
640 }
6cba487f 641
5b80ddfb 642 td_write = fmt_write->open_trace(opt_output_path, O_RDWR, NULL, NULL);
bbefb8dd 643 if (!td_write) {
3394d22e 644 fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n",
b61922b5 645 opt_output_path ? : "<none>");
bbefb8dd
MD
646 goto error_td_write;
647 }
847bf71a 648
24d5af5b
MD
649 /*
650 * Errors happened when opening traces, but we continue anyway.
651 * sleep to let user see the stderr output before stdout.
652 */
4e85cbfd 653 if (partial_error)
24d5af5b 654 sleep(PARTIAL_ERROR_SLEEP);
24d5af5b 655
95d36295 656 ret = convert_trace(td_write, ctx);
46322b33 657 if (ret) {
3394d22e 658 fprintf(stderr, "Error printing trace.\n\n");
46322b33
MD
659 goto error_copy_trace;
660 }
847bf71a 661
bbefb8dd 662 fmt_write->close_trace(td_write);
6cba487f
MD
663
664 bt_context_put(ctx);
afb48eae
AA
665 printf_verbose("finished converting. Output written to:\n%s\n",
666 opt_output_path ? : "<stdout>");
11e1d048 667 goto end;
34ac0e6c 668
bbefb8dd 669 /* Error handling */
46322b33 670error_copy_trace:
bbefb8dd
MD
671 fmt_write->close_trace(td_write);
672error_td_write:
6cba487f 673 bt_context_put(ctx);
bbefb8dd 674error_td_read:
11e1d048
MD
675 partial_error = 1;
676
677 /* teardown and exit */
678end:
679 free(opt_input_format);
680 free(opt_output_format);
9fe26ec7 681 free(opt_output_path);
4e85cbfd 682 g_ptr_array_free(opt_input_paths, TRUE);
11e1d048
MD
683 if (partial_error)
684 exit(EXIT_FAILURE);
685 else
686 exit(EXIT_SUCCESS);
4c8bfb7e 687}
This page took 0.062204 seconds and 4 git commands to generate.