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