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