Fix: return an error if no trace can be opened
[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>
20d24215 42#include <fts.h>
4c8bfb7e 43
a44bc4c9
MD
44#include <babeltrace/ctf-ir/metadata.h> /* for clocks */
45
afb48eae 46#define DEFAULT_FILE_ARRAY_SIZE 1
bbefb8dd
MD
47static char *opt_input_format;
48static char *opt_output_format;
34ac0e6c
MD
49
50static const char *opt_input_path;
51static const char *opt_output_path;
52
afb48eae
AA
53static struct format *fmt_read;
54
bbefb8dd
MD
55void strlower(char *str)
56{
57 while (*str) {
58 *str = tolower(*str);
59 str++;
60 }
61}
62
34ac0e6c
MD
63enum {
64 OPT_NONE = 0,
65 OPT_HELP,
7fb21036 66 OPT_LIST,
34ac0e6c
MD
67 OPT_VERBOSE,
68 OPT_DEBUG,
d63ca2cd 69 OPT_NAMES,
359d7456 70 OPT_FIELDS,
8d8ed9af 71 OPT_NO_DELTA,
11ac6674
MD
72 OPT_CLOCK_OFFSET,
73 OPT_CLOCK_RAW,
74 OPT_CLOCK_SECONDS,
75 OPT_CLOCK_DATE,
76 OPT_CLOCK_GMT,
82ace6d6 77 OPT_CLOCK_FORCE_CORRELATE,
34ac0e6c
MD
78};
79
80static struct poptOption long_options[] = {
81 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
82 { "input-format", 'i', POPT_ARG_STRING, &opt_input_format, OPT_NONE, NULL, NULL },
83 { "output-format", 'o', POPT_ARG_STRING, &opt_output_format, OPT_NONE, NULL, NULL },
84 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
7fb21036 85 { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL },
34ac0e6c
MD
86 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
87 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
cba1661c 88 { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
359d7456 89 { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL },
8d8ed9af 90 { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL },
11ac6674
MD
91 { "clock-offset", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL },
92 { "clock-raw", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_RAW, NULL, NULL },
93 { "clock-seconds", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_SECONDS, NULL, NULL },
94 { "clock-date", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_DATE, NULL, NULL },
95 { "clock-gmt", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_GMT, NULL, NULL },
82ace6d6 96 { "clock-force-correlate", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_FORCE_CORRELATE, NULL, NULL },
34ac0e6c
MD
97 { NULL, 0, 0, NULL, 0, NULL, NULL },
98};
99
7fb21036
MD
100static void list_formats(FILE *fp)
101{
102 fprintf(fp, "\n");
103 bt_fprintf_format_list(fp);
104}
105
34ac0e6c 106static void usage(FILE *fp)
4c8bfb7e 107{
4c15b06b 108 fprintf(fp, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION);
4d5678b9 109 fprintf(fp, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
34ac0e6c 110 fprintf(fp, "\n");
4d5678b9
MD
111 fprintf(fp, " INPUT Input trace path\n");
112 fprintf(fp, " OUTPUT Output trace path (default: stdout)\n");
34ac0e6c 113 fprintf(fp, "\n");
d2b8ea6b
MD
114 fprintf(fp, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
115 fprintf(fp, " -o, --output-format FORMAT Output trace format (default: text)\n");
34ac0e6c 116 fprintf(fp, "\n");
4d5678b9
MD
117 fprintf(fp, " -h, --help This help message\n");
118 fprintf(fp, " -l, --list List available formats\n");
119 fprintf(fp, " -v, --verbose Verbose mode\n");
cba1661c 120 fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n");
4d5678b9 121 fprintf(fp, " -d, --debug Debug mode\n");
cba1661c 122 fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n");
8d8ed9af 123 fprintf(fp, " --no-delta Do not print time delta between consecutive events\n");
359d7456 124 fprintf(fp, " -n, --names name1<,name2,...> Print field names:\n");
82662ad4 125 fprintf(fp, " (payload OR args OR arg)\n");
12c9c3bc
MD
126 fprintf(fp, " none, all, scope, header, (context OR ctx)\n");
127 fprintf(fp, " (default: payload,context)\n");
359d7456
MD
128 fprintf(fp, " -f, --fields name1<,name2,...> Print additional fields:\n");
129 fprintf(fp, " all, trace, trace:domain, trace:procname,\n");
130 fprintf(fp, " trace:vpid, loglevel.\n");
11ac6674
MD
131 fprintf(fp, " --clock-raw Disregard internal clock offset (use raw value)\n");
132 fprintf(fp, " --clock-offset seconds Clock offset in seconds\n");
133 fprintf(fp, " --clock-seconds Print the timestamps as [sec.ns]\n");
134 fprintf(fp, " (default is: [hh:mm:ss.ns])\n");
135 fprintf(fp, " --clock-date Print clock date\n");
136 fprintf(fp, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n");
82ace6d6
MD
137 fprintf(fp, " --clock-force-correlate Assume that clocks are inherently correlated\n");
138 fprintf(fp, " across traces.\n");
7fb21036 139 list_formats(fp);
34ac0e6c
MD
140 fprintf(fp, "\n");
141}
142
cba1661c
MD
143static int get_names_args(poptContext *pc)
144{
145 char *str, *strlist, *strctx;
146
147 opt_payload_field_names = 0;
12c9c3bc 148 opt_context_field_names = 0;
cba1661c
MD
149 strlist = (char *) poptGetOptArg(*pc);
150 if (!strlist) {
151 return -EINVAL;
152 }
153 str = strtok_r(strlist, ",", &strctx);
154 do {
155 if (!strcmp(str, "all"))
156 opt_all_field_names = 1;
157 else if (!strcmp(str, "scope"))
158 opt_scope_field_names = 1;
159 else if (!strcmp(str, "context") || !strcmp(str, "ctx"))
160 opt_context_field_names = 1;
161 else if (!strcmp(str, "header"))
162 opt_header_field_names = 1;
163 else if (!strcmp(str, "payload") || !strcmp(str, "args") || !strcmp(str, "arg"))
164 opt_payload_field_names = 1;
12c9c3bc
MD
165 else if (!strcmp(str, "none")) {
166 opt_all_field_names = 0;
167 opt_scope_field_names = 0;
168 opt_context_field_names = 0;
169 opt_header_field_names = 0;
170 opt_payload_field_names = 0;
171 } else {
359d7456
MD
172 fprintf(stderr, "[error] unknown field name type %s\n", str);
173 return -EINVAL;
174 }
175 } while ((str = strtok_r(NULL, ",", &strctx)));
176 return 0;
177}
178
179static int get_fields_args(poptContext *pc)
180{
181 char *str, *strlist, *strctx;
182
359d7456
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_fields = 1;
82662ad4 191 else if (!strcmp(str, "trace"))
359d7456 192 opt_trace_field = 1;
8c250d87 193 else if (!strcmp(str, "trace:domain"))
359d7456 194 opt_trace_domain_field = 1;
8c250d87 195 else if (!strcmp(str, "trace:procname"))
359d7456 196 opt_trace_procname_field = 1;
8c250d87 197 else if (!strcmp(str, "trace:vpid"))
359d7456 198 opt_trace_vpid_field = 1;
d86d62f8 199 else if (!strcmp(str, "loglevel"))
359d7456 200 opt_loglevel_field = 1;
cba1661c 201 else {
359d7456 202 fprintf(stderr, "[error] unknown field type %s\n", str);
cba1661c
MD
203 return -EINVAL;
204 }
205 } while ((str = strtok_r(NULL, ",", &strctx)));
206 return 0;
207}
208
34ac0e6c
MD
209/*
210 * Return 0 if caller should continue, < 0 if caller should return
211 * error, > 0 if caller should exit without reporting error.
212 */
bbefb8dd 213static int parse_options(int argc, char **argv)
34ac0e6c
MD
214{
215 poptContext pc;
216 int opt, ret = 0;
217
0f980a35
MD
218 if (argc == 1) {
219 usage(stdout);
220 return 1; /* exit cleanly */
221 }
222
bbefb8dd 223 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
34ac0e6c
MD
224 poptReadDefaultConfig(pc, 0);
225
cba1661c 226 /* set default */
e505794f 227 opt_context_field_names = 1;
cba1661c
MD
228 opt_payload_field_names = 1;
229
34ac0e6c
MD
230 while ((opt = poptGetNextOpt(pc)) != -1) {
231 switch (opt) {
232 case OPT_HELP:
7fb21036 233 usage(stdout);
34ac0e6c
MD
234 ret = 1; /* exit cleanly */
235 goto end;
7fb21036
MD
236 case OPT_LIST:
237 list_formats(stdout);
238 ret = 1;
239 goto end;
34ac0e6c
MD
240 case OPT_VERBOSE:
241 babeltrace_verbose = 1;
242 break;
cba1661c
MD
243 case OPT_NAMES:
244 if (get_names_args(&pc)) {
245 ret = -EINVAL;
246 goto end;
247 }
248 break;
359d7456
MD
249 case OPT_FIELDS:
250 if (get_fields_args(&pc)) {
251 ret = -EINVAL;
252 goto end;
253 }
254 break;
34ac0e6c
MD
255 case OPT_DEBUG:
256 babeltrace_debug = 1;
257 break;
8d8ed9af 258 case OPT_NO_DELTA:
359d7456 259 opt_delta_field = 0;
8d8ed9af 260 break;
11ac6674
MD
261 case OPT_CLOCK_RAW:
262 opt_clock_raw = 1;
263 break;
264 case OPT_CLOCK_OFFSET:
265 {
266 char *str, *endptr;
267
268 str = poptGetOptArg(pc);
269 if (!str) {
270 fprintf(stderr, "[error] Missing --clock-offset argument\n");
271 ret = -EINVAL;
272 goto end;
273 }
274 errno = 0;
275 opt_clock_offset = strtoull(str, &endptr, 0);
276 if (*endptr != '\0' || str == endptr || errno != 0) {
277 fprintf(stderr, "[error] Incorrect --clock-offset argument: %s\n", str);
278 ret = -EINVAL;
279 goto end;
280 }
281 break;
282 }
283 case OPT_CLOCK_SECONDS:
284 opt_clock_seconds = 1;
285 break;
286 case OPT_CLOCK_DATE:
287 opt_clock_date = 1;
288 break;
289 case OPT_CLOCK_GMT:
290 opt_clock_gmt = 1;
291 break;
82ace6d6
MD
292 case OPT_CLOCK_FORCE_CORRELATE:
293 opt_clock_force_correlate = 1;
294 break;
11ac6674 295
34ac0e6c
MD
296 default:
297 ret = -EINVAL;
298 goto end;
299 }
300 }
301
302 opt_input_path = poptGetArg(pc);
303 if (!opt_input_path) {
304 ret = -EINVAL;
305 goto end;
306 }
307 opt_output_path = poptGetArg(pc);
cba1661c 308
34ac0e6c
MD
309end:
310 if (pc) {
311 poptFreeContext(pc);
312 }
313 return ret;
314}
315
afb48eae 316
20d24215
YB
317/*
318 * bt_context_add_traces_recursive: Open a trace recursively
319 *
320 * Find each trace present in the subdirectory starting from the given
613f532b
MD
321 * path, and add them to the context. The packet_seek parameter can be
322 * NULL: this specify to use the default format packet_seek.
20d24215
YB
323 *
324 * Return: 0 on success, nonzero on failure.
325 * Unable to open toplevel: failure.
326 * Unable to open some subdirectory or file: warn and continue;
327 */
328int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
613f532b
MD
329 const char *format_str,
330 void (*packet_seek)(struct stream_pos *pos,
331 size_t offset, int whence))
20d24215
YB
332{
333 FTS *tree;
334 FTSENT *node;
335 GArray *trace_ids;
336 char lpath[PATH_MAX];
337 char * const paths[2] = { lpath, NULL };
ec4c2b5c 338 int ret = -1;
20d24215
YB
339
340 /*
341 * Need to copy path, because fts_open can change it.
342 * It is the pointer array, not the strings, that are constant.
343 */
344 strncpy(lpath, path, PATH_MAX);
345 lpath[PATH_MAX - 1] = '\0';
346
347 tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
348 if (tree == NULL) {
349 fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
350 path);
351 return -EINVAL;
352 }
353
354 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
355
356 while ((node = fts_read(tree))) {
357 int dirfd, metafd;
358
359 if (!(node->fts_info & FTS_D))
360 continue;
361
362 dirfd = open(node->fts_accpath, 0);
363 if (dirfd < 0) {
364 fprintf(stderr, "[error] [Context] Unable to open trace "
365 "directory file descriptor.\n");
366 ret = dirfd;
367 goto error;
368 }
369 metafd = openat(dirfd, "metadata", O_RDONLY);
370 if (metafd < 0) {
371 ret = close(dirfd);
372 if (ret < 0) {
373 perror("close");
374 goto error;
375 }
376 } else {
377 int trace_id;
378
379 ret = close(metafd);
380 if (ret < 0) {
381 perror("close");
382 goto error;
383 }
384 ret = close(dirfd);
385 if (ret < 0) {
386 perror("close");
387 goto error;
388 }
389
390 trace_id = bt_context_add_trace(ctx,
613f532b 391 node->fts_accpath, format_str,
0d4c669f 392 packet_seek, NULL, NULL);
20d24215 393 if (trace_id < 0) {
ca718275 394 fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s "
20d24215 395 "for reading.\n", node->fts_accpath, path);
ca718275
MD
396 /* Allow to skip erroneous traces. */
397 continue;
20d24215
YB
398 }
399 g_array_append_val(trace_ids, trace_id);
400 }
401 }
402
20d24215 403error:
27cd3890
MD
404 /*
405 * Return an error if no trace can be opened.
406 */
407 if (ret == 0 && trace_ids->len == 0) {
408 fprintf(stderr, "[error] Cannot open any trace for reading.\n\n");
409 ret = -ENOENT;
410 }
411 g_array_free(trace_ids, TRUE);
20d24215
YB
412 return ret;
413}
a44bc4c9 414
a44bc4c9 415
afb48eae 416
95d36295
JD
417int convert_trace(struct trace_descriptor *td_write,
418 struct bt_context *ctx)
419{
e4195791 420 struct bt_ctf_iter *iter;
95d36295 421 struct ctf_text_stream_pos *sout;
e8c92a62 422 struct bt_iter_pos begin_pos;
c50d2a7a 423 struct bt_ctf_event *ctf_event;
95d36295
JD
424 int ret;
425
426 sout = container_of(td_write, struct ctf_text_stream_pos,
427 trace_descriptor);
428
429 begin_pos.type = BT_SEEK_BEGIN;
e4195791 430 iter = bt_ctf_iter_create(ctx, &begin_pos, NULL);
95d36295
JD
431 if (!iter) {
432 ret = -1;
433 goto error_iter;
434 }
e4195791 435 while ((ctf_event = bt_ctf_iter_read_event(iter))) {
c50d2a7a 436 ret = sout->parent.event_cb(&sout->parent, ctf_event->parent->stream);
95d36295 437 if (ret) {
3394d22e 438 fprintf(stderr, "[error] Writing event failed.\n");
95d36295
JD
439 goto end;
440 }
e4195791 441 ret = bt_iter_next(bt_ctf_get_iter(iter));
95d36295
JD
442 if (ret < 0)
443 goto end;
444 }
445 ret = 0;
446
447end:
e4195791 448 bt_ctf_iter_destroy(iter);
95d36295
JD
449error_iter:
450 return ret;
451}
452
bbefb8dd 453int main(int argc, char **argv)
34ac0e6c
MD
454{
455 int ret;
afb48eae
AA
456 struct format *fmt_write;
457 struct trace_descriptor *td_write;
95d36295 458 struct bt_context *ctx;
34ac0e6c
MD
459
460 ret = parse_options(argc, argv);
461 if (ret < 0) {
3394d22e
MD
462 fprintf(stderr, "Error parsing options.\n\n");
463 usage(stderr);
34ac0e6c
MD
464 exit(EXIT_FAILURE);
465 } else if (ret > 0) {
466 exit(EXIT_SUCCESS);
467 }
468 printf_verbose("Verbose mode active.\n");
469 printf_debug("Debug mode active.\n");
470
bbefb8dd
MD
471 if (opt_input_format)
472 strlower(opt_input_format);
473 if (opt_output_format)
474 strlower(opt_output_format);
475
afb48eae 476 printf_verbose("Converting from directory: %s\n", opt_input_path);
34ac0e6c 477 printf_verbose("Converting from format: %s\n",
b61922b5 478 opt_input_format ? : "ctf <default>");
afb48eae 479 printf_verbose("Converting to directory: %s\n",
478b6389 480 opt_output_path ? : "<stdout>");
34ac0e6c 481 printf_verbose("Converting to format: %s\n",
b61922b5 482 opt_output_format ? : "text <default>");
bbefb8dd 483
b61922b5
MD
484 if (!opt_input_format)
485 opt_input_format = "ctf";
486 if (!opt_output_format)
487 opt_output_format = "text";
bbefb8dd
MD
488 fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
489 if (!fmt_read) {
3394d22e 490 fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n",
bbefb8dd
MD
491 opt_input_format);
492 exit(EXIT_FAILURE);
493 }
bbefb8dd
MD
494 fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
495 if (!fmt_write) {
3394d22e 496 fprintf(stderr, "[error] format \"%s\" is not supported.\n\n",
bbefb8dd
MD
497 opt_output_format);
498 exit(EXIT_FAILURE);
499 }
500
6cba487f
MD
501 ctx = bt_context_create();
502
e669e45e 503 ret = bt_context_add_traces_recursive(ctx, opt_input_path,
613f532b 504 opt_input_format, NULL);
6cba487f 505 if (ret) {
3394d22e 506 fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n",
bbefb8dd
MD
507 opt_input_path);
508 goto error_td_read;
509 }
6cba487f 510
5b80ddfb 511 td_write = fmt_write->open_trace(opt_output_path, O_RDWR, NULL, NULL);
bbefb8dd 512 if (!td_write) {
3394d22e 513 fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n",
b61922b5 514 opt_output_path ? : "<none>");
bbefb8dd
MD
515 goto error_td_write;
516 }
847bf71a 517
95d36295 518 ret = convert_trace(td_write, ctx);
46322b33 519 if (ret) {
3394d22e 520 fprintf(stderr, "Error printing trace.\n\n");
46322b33
MD
521 goto error_copy_trace;
522 }
847bf71a 523
bbefb8dd 524 fmt_write->close_trace(td_write);
6cba487f
MD
525
526 bt_context_put(ctx);
afb48eae
AA
527 printf_verbose("finished converting. Output written to:\n%s\n",
528 opt_output_path ? : "<stdout>");
bbefb8dd 529 exit(EXIT_SUCCESS);
34ac0e6c 530
bbefb8dd 531 /* Error handling */
46322b33 532error_copy_trace:
bbefb8dd
MD
533 fmt_write->close_trace(td_write);
534error_td_write:
6cba487f 535 bt_context_put(ctx);
bbefb8dd
MD
536error_td_read:
537 exit(EXIT_FAILURE);
4c8bfb7e 538}
This page took 0.081764 seconds and 4 git commands to generate.