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