Fix: Show context field name by default
[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
MD
125 fprintf(fp, " (payload OR args OR arg)\n");
126 fprintf(fp, " all, scope, header, (context OR ctx)\n");
cba1661c 127 fprintf(fp, " (payload active by default)\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;
148 strlist = (char *) poptGetOptArg(*pc);
149 if (!strlist) {
150 return -EINVAL;
151 }
152 str = strtok_r(strlist, ",", &strctx);
153 do {
154 if (!strcmp(str, "all"))
155 opt_all_field_names = 1;
156 else if (!strcmp(str, "scope"))
157 opt_scope_field_names = 1;
158 else if (!strcmp(str, "context") || !strcmp(str, "ctx"))
159 opt_context_field_names = 1;
160 else if (!strcmp(str, "header"))
161 opt_header_field_names = 1;
162 else if (!strcmp(str, "payload") || !strcmp(str, "args") || !strcmp(str, "arg"))
163 opt_payload_field_names = 1;
359d7456
MD
164 else {
165 fprintf(stderr, "[error] unknown field name type %s\n", str);
166 return -EINVAL;
167 }
168 } while ((str = strtok_r(NULL, ",", &strctx)));
169 return 0;
170}
171
172static int get_fields_args(poptContext *pc)
173{
174 char *str, *strlist, *strctx;
175
359d7456
MD
176 strlist = (char *) poptGetOptArg(*pc);
177 if (!strlist) {
178 return -EINVAL;
179 }
180 str = strtok_r(strlist, ",", &strctx);
181 do {
182 if (!strcmp(str, "all"))
183 opt_all_fields = 1;
82662ad4 184 else if (!strcmp(str, "trace"))
359d7456 185 opt_trace_field = 1;
8c250d87 186 else if (!strcmp(str, "trace:domain"))
359d7456 187 opt_trace_domain_field = 1;
8c250d87 188 else if (!strcmp(str, "trace:procname"))
359d7456 189 opt_trace_procname_field = 1;
8c250d87 190 else if (!strcmp(str, "trace:vpid"))
359d7456 191 opt_trace_vpid_field = 1;
d86d62f8 192 else if (!strcmp(str, "loglevel"))
359d7456 193 opt_loglevel_field = 1;
cba1661c 194 else {
359d7456 195 fprintf(stderr, "[error] unknown field type %s\n", str);
cba1661c
MD
196 return -EINVAL;
197 }
198 } while ((str = strtok_r(NULL, ",", &strctx)));
199 return 0;
200}
201
34ac0e6c
MD
202/*
203 * Return 0 if caller should continue, < 0 if caller should return
204 * error, > 0 if caller should exit without reporting error.
205 */
bbefb8dd 206static int parse_options(int argc, char **argv)
34ac0e6c
MD
207{
208 poptContext pc;
209 int opt, ret = 0;
210
0f980a35
MD
211 if (argc == 1) {
212 usage(stdout);
213 return 1; /* exit cleanly */
214 }
215
bbefb8dd 216 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
34ac0e6c
MD
217 poptReadDefaultConfig(pc, 0);
218
cba1661c 219 /* set default */
e505794f 220 opt_context_field_names = 1;
cba1661c
MD
221 opt_payload_field_names = 1;
222
34ac0e6c
MD
223 while ((opt = poptGetNextOpt(pc)) != -1) {
224 switch (opt) {
225 case OPT_HELP:
7fb21036 226 usage(stdout);
34ac0e6c
MD
227 ret = 1; /* exit cleanly */
228 goto end;
7fb21036
MD
229 case OPT_LIST:
230 list_formats(stdout);
231 ret = 1;
232 goto end;
34ac0e6c
MD
233 case OPT_VERBOSE:
234 babeltrace_verbose = 1;
235 break;
cba1661c
MD
236 case OPT_NAMES:
237 if (get_names_args(&pc)) {
238 ret = -EINVAL;
239 goto end;
240 }
241 break;
359d7456
MD
242 case OPT_FIELDS:
243 if (get_fields_args(&pc)) {
244 ret = -EINVAL;
245 goto end;
246 }
247 break;
34ac0e6c
MD
248 case OPT_DEBUG:
249 babeltrace_debug = 1;
250 break;
8d8ed9af 251 case OPT_NO_DELTA:
359d7456 252 opt_delta_field = 0;
8d8ed9af 253 break;
11ac6674
MD
254 case OPT_CLOCK_RAW:
255 opt_clock_raw = 1;
256 break;
257 case OPT_CLOCK_OFFSET:
258 {
259 char *str, *endptr;
260
261 str = poptGetOptArg(pc);
262 if (!str) {
263 fprintf(stderr, "[error] Missing --clock-offset argument\n");
264 ret = -EINVAL;
265 goto end;
266 }
267 errno = 0;
268 opt_clock_offset = strtoull(str, &endptr, 0);
269 if (*endptr != '\0' || str == endptr || errno != 0) {
270 fprintf(stderr, "[error] Incorrect --clock-offset argument: %s\n", str);
271 ret = -EINVAL;
272 goto end;
273 }
274 break;
275 }
276 case OPT_CLOCK_SECONDS:
277 opt_clock_seconds = 1;
278 break;
279 case OPT_CLOCK_DATE:
280 opt_clock_date = 1;
281 break;
282 case OPT_CLOCK_GMT:
283 opt_clock_gmt = 1;
284 break;
82ace6d6
MD
285 case OPT_CLOCK_FORCE_CORRELATE:
286 opt_clock_force_correlate = 1;
287 break;
11ac6674 288
34ac0e6c
MD
289 default:
290 ret = -EINVAL;
291 goto end;
292 }
293 }
294
295 opt_input_path = poptGetArg(pc);
296 if (!opt_input_path) {
297 ret = -EINVAL;
298 goto end;
299 }
300 opt_output_path = poptGetArg(pc);
cba1661c 301
34ac0e6c
MD
302end:
303 if (pc) {
304 poptFreeContext(pc);
305 }
306 return ret;
307}
308
afb48eae 309
20d24215
YB
310/*
311 * bt_context_add_traces_recursive: Open a trace recursively
312 *
313 * Find each trace present in the subdirectory starting from the given
613f532b
MD
314 * path, and add them to the context. The packet_seek parameter can be
315 * NULL: this specify to use the default format packet_seek.
20d24215
YB
316 *
317 * Return: 0 on success, nonzero on failure.
318 * Unable to open toplevel: failure.
319 * Unable to open some subdirectory or file: warn and continue;
320 */
321int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
613f532b
MD
322 const char *format_str,
323 void (*packet_seek)(struct stream_pos *pos,
324 size_t offset, int whence))
20d24215
YB
325{
326 FTS *tree;
327 FTSENT *node;
328 GArray *trace_ids;
329 char lpath[PATH_MAX];
330 char * const paths[2] = { lpath, NULL };
ec4c2b5c 331 int ret = -1;
20d24215
YB
332
333 /*
334 * Need to copy path, because fts_open can change it.
335 * It is the pointer array, not the strings, that are constant.
336 */
337 strncpy(lpath, path, PATH_MAX);
338 lpath[PATH_MAX - 1] = '\0';
339
340 tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
341 if (tree == NULL) {
342 fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
343 path);
344 return -EINVAL;
345 }
346
347 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
348
349 while ((node = fts_read(tree))) {
350 int dirfd, metafd;
351
352 if (!(node->fts_info & FTS_D))
353 continue;
354
355 dirfd = open(node->fts_accpath, 0);
356 if (dirfd < 0) {
357 fprintf(stderr, "[error] [Context] Unable to open trace "
358 "directory file descriptor.\n");
359 ret = dirfd;
360 goto error;
361 }
362 metafd = openat(dirfd, "metadata", O_RDONLY);
363 if (metafd < 0) {
364 ret = close(dirfd);
365 if (ret < 0) {
366 perror("close");
367 goto error;
368 }
369 } else {
370 int trace_id;
371
372 ret = close(metafd);
373 if (ret < 0) {
374 perror("close");
375 goto error;
376 }
377 ret = close(dirfd);
378 if (ret < 0) {
379 perror("close");
380 goto error;
381 }
382
383 trace_id = bt_context_add_trace(ctx,
613f532b 384 node->fts_accpath, format_str,
0d4c669f 385 packet_seek, NULL, NULL);
20d24215 386 if (trace_id < 0) {
ca718275 387 fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s "
20d24215 388 "for reading.\n", node->fts_accpath, path);
ca718275
MD
389 /* Allow to skip erroneous traces. */
390 continue;
20d24215
YB
391 }
392 g_array_append_val(trace_ids, trace_id);
393 }
394 }
395
396 g_array_free(trace_ids, TRUE);
ec4c2b5c 397 return ret;
20d24215
YB
398
399error:
400 return ret;
401}
a44bc4c9 402
a44bc4c9 403
afb48eae 404
95d36295
JD
405int convert_trace(struct trace_descriptor *td_write,
406 struct bt_context *ctx)
407{
e4195791 408 struct bt_ctf_iter *iter;
95d36295 409 struct ctf_text_stream_pos *sout;
e8c92a62 410 struct bt_iter_pos begin_pos;
c50d2a7a 411 struct bt_ctf_event *ctf_event;
95d36295
JD
412 int ret;
413
414 sout = container_of(td_write, struct ctf_text_stream_pos,
415 trace_descriptor);
416
417 begin_pos.type = BT_SEEK_BEGIN;
e4195791 418 iter = bt_ctf_iter_create(ctx, &begin_pos, NULL);
95d36295
JD
419 if (!iter) {
420 ret = -1;
421 goto error_iter;
422 }
e4195791 423 while ((ctf_event = bt_ctf_iter_read_event(iter))) {
c50d2a7a 424 ret = sout->parent.event_cb(&sout->parent, ctf_event->parent->stream);
95d36295 425 if (ret) {
3394d22e 426 fprintf(stderr, "[error] Writing event failed.\n");
95d36295
JD
427 goto end;
428 }
e4195791 429 ret = bt_iter_next(bt_ctf_get_iter(iter));
95d36295
JD
430 if (ret < 0)
431 goto end;
432 }
433 ret = 0;
434
435end:
e4195791 436 bt_ctf_iter_destroy(iter);
95d36295
JD
437error_iter:
438 return ret;
439}
440
bbefb8dd 441int main(int argc, char **argv)
34ac0e6c
MD
442{
443 int ret;
afb48eae
AA
444 struct format *fmt_write;
445 struct trace_descriptor *td_write;
95d36295 446 struct bt_context *ctx;
34ac0e6c
MD
447
448 ret = parse_options(argc, argv);
449 if (ret < 0) {
3394d22e
MD
450 fprintf(stderr, "Error parsing options.\n\n");
451 usage(stderr);
34ac0e6c
MD
452 exit(EXIT_FAILURE);
453 } else if (ret > 0) {
454 exit(EXIT_SUCCESS);
455 }
456 printf_verbose("Verbose mode active.\n");
457 printf_debug("Debug mode active.\n");
458
bbefb8dd
MD
459 if (opt_input_format)
460 strlower(opt_input_format);
461 if (opt_output_format)
462 strlower(opt_output_format);
463
afb48eae 464 printf_verbose("Converting from directory: %s\n", opt_input_path);
34ac0e6c 465 printf_verbose("Converting from format: %s\n",
b61922b5 466 opt_input_format ? : "ctf <default>");
afb48eae 467 printf_verbose("Converting to directory: %s\n",
478b6389 468 opt_output_path ? : "<stdout>");
34ac0e6c 469 printf_verbose("Converting to format: %s\n",
b61922b5 470 opt_output_format ? : "text <default>");
bbefb8dd 471
b61922b5
MD
472 if (!opt_input_format)
473 opt_input_format = "ctf";
474 if (!opt_output_format)
475 opt_output_format = "text";
bbefb8dd
MD
476 fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
477 if (!fmt_read) {
3394d22e 478 fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n",
bbefb8dd
MD
479 opt_input_format);
480 exit(EXIT_FAILURE);
481 }
bbefb8dd
MD
482 fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
483 if (!fmt_write) {
3394d22e 484 fprintf(stderr, "[error] format \"%s\" is not supported.\n\n",
bbefb8dd
MD
485 opt_output_format);
486 exit(EXIT_FAILURE);
487 }
488
6cba487f
MD
489 ctx = bt_context_create();
490
e669e45e 491 ret = bt_context_add_traces_recursive(ctx, opt_input_path,
613f532b 492 opt_input_format, NULL);
6cba487f 493 if (ret) {
3394d22e 494 fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n",
bbefb8dd
MD
495 opt_input_path);
496 goto error_td_read;
497 }
6cba487f 498
5b80ddfb 499 td_write = fmt_write->open_trace(opt_output_path, O_RDWR, NULL, NULL);
bbefb8dd 500 if (!td_write) {
3394d22e 501 fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n",
b61922b5 502 opt_output_path ? : "<none>");
bbefb8dd
MD
503 goto error_td_write;
504 }
847bf71a 505
95d36295 506 ret = convert_trace(td_write, ctx);
46322b33 507 if (ret) {
3394d22e 508 fprintf(stderr, "Error printing trace.\n\n");
46322b33
MD
509 goto error_copy_trace;
510 }
847bf71a 511
bbefb8dd 512 fmt_write->close_trace(td_write);
6cba487f
MD
513
514 bt_context_put(ctx);
afb48eae
AA
515 printf_verbose("finished converting. Output written to:\n%s\n",
516 opt_output_path ? : "<stdout>");
bbefb8dd 517 exit(EXIT_SUCCESS);
34ac0e6c 518
bbefb8dd 519 /* Error handling */
46322b33 520error_copy_trace:
bbefb8dd
MD
521 fmt_write->close_trace(td_write);
522error_td_write:
6cba487f 523 bt_context_put(ctx);
bbefb8dd
MD
524error_td_read:
525 exit(EXIT_FAILURE);
4c8bfb7e 526}
This page took 0.081637 seconds and 4 git commands to generate.