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