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