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