Fix erroneous use of "-n" for additional fields, add "-f"
[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>
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>
4c8bfb7e 39
afb48eae 40#define DEFAULT_FILE_ARRAY_SIZE 1
bbefb8dd
MD
41static char *opt_input_format;
42static char *opt_output_format;
34ac0e6c
MD
43
44static const char *opt_input_path;
45static const char *opt_output_path;
46
afb48eae
AA
47static struct trace_collection trace_collection_read;
48static struct format *fmt_read;
49
bbefb8dd
MD
50void strlower(char *str)
51{
52 while (*str) {
53 *str = tolower(*str);
54 str++;
55 }
56}
57
34ac0e6c
MD
58enum {
59 OPT_NONE = 0,
60 OPT_HELP,
7fb21036 61 OPT_LIST,
34ac0e6c
MD
62 OPT_VERBOSE,
63 OPT_DEBUG,
d63ca2cd 64 OPT_NAMES,
359d7456 65 OPT_FIELDS,
8d8ed9af 66 OPT_NO_DELTA,
34ac0e6c
MD
67};
68
69static struct poptOption long_options[] = {
70 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
71 { "input-format", 'i', POPT_ARG_STRING, &opt_input_format, OPT_NONE, NULL, NULL },
72 { "output-format", 'o', POPT_ARG_STRING, &opt_output_format, OPT_NONE, NULL, NULL },
73 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
7fb21036 74 { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL },
34ac0e6c
MD
75 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
76 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
cba1661c 77 { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
359d7456 78 { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL },
8d8ed9af 79 { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL },
34ac0e6c
MD
80 { NULL, 0, 0, NULL, 0, NULL, NULL },
81};
82
7fb21036
MD
83static void list_formats(FILE *fp)
84{
85 fprintf(fp, "\n");
86 bt_fprintf_format_list(fp);
87}
88
34ac0e6c 89static void usage(FILE *fp)
4c8bfb7e 90{
00f7fbf0 91 fprintf(fp, "BabelTrace Trace Converter %s\n\n", VERSION);
4d5678b9 92 fprintf(fp, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
34ac0e6c 93 fprintf(fp, "\n");
4d5678b9
MD
94 fprintf(fp, " INPUT Input trace path\n");
95 fprintf(fp, " OUTPUT Output trace path (default: stdout)\n");
34ac0e6c 96 fprintf(fp, "\n");
d2b8ea6b
MD
97 fprintf(fp, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
98 fprintf(fp, " -o, --output-format FORMAT Output trace format (default: text)\n");
34ac0e6c 99 fprintf(fp, "\n");
4d5678b9
MD
100 fprintf(fp, " -h, --help This help message\n");
101 fprintf(fp, " -l, --list List available formats\n");
102 fprintf(fp, " -v, --verbose Verbose mode\n");
cba1661c 103 fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n");
4d5678b9 104 fprintf(fp, " -d, --debug Debug mode\n");
cba1661c 105 fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n");
8d8ed9af 106 fprintf(fp, " --no-delta Do not print time delta between consecutive events\n");
359d7456 107 fprintf(fp, " -n, --names name1<,name2,...> Print field names:\n");
82662ad4
MD
108 fprintf(fp, " (payload OR args OR arg)\n");
109 fprintf(fp, " all, scope, header, (context OR ctx)\n");
cba1661c 110 fprintf(fp, " (payload active by default)\n");
359d7456
MD
111 fprintf(fp, " -f, --fields name1<,name2,...> Print additional fields:\n");
112 fprintf(fp, " all, trace, trace:domain, trace:procname,\n");
113 fprintf(fp, " trace:vpid, loglevel.\n");
7fb21036 114 list_formats(fp);
34ac0e6c
MD
115 fprintf(fp, "\n");
116}
117
cba1661c
MD
118static int get_names_args(poptContext *pc)
119{
120 char *str, *strlist, *strctx;
121
122 opt_payload_field_names = 0;
123 strlist = (char *) poptGetOptArg(*pc);
124 if (!strlist) {
125 return -EINVAL;
126 }
127 str = strtok_r(strlist, ",", &strctx);
128 do {
129 if (!strcmp(str, "all"))
130 opt_all_field_names = 1;
131 else if (!strcmp(str, "scope"))
132 opt_scope_field_names = 1;
133 else if (!strcmp(str, "context") || !strcmp(str, "ctx"))
134 opt_context_field_names = 1;
135 else if (!strcmp(str, "header"))
136 opt_header_field_names = 1;
137 else if (!strcmp(str, "payload") || !strcmp(str, "args") || !strcmp(str, "arg"))
138 opt_payload_field_names = 1;
359d7456
MD
139 else {
140 fprintf(stderr, "[error] unknown field name type %s\n", str);
141 return -EINVAL;
142 }
143 } while ((str = strtok_r(NULL, ",", &strctx)));
144 return 0;
145}
146
147static int get_fields_args(poptContext *pc)
148{
149 char *str, *strlist, *strctx;
150
151 opt_payload_field_names = 0;
152 strlist = (char *) poptGetOptArg(*pc);
153 if (!strlist) {
154 return -EINVAL;
155 }
156 str = strtok_r(strlist, ",", &strctx);
157 do {
158 if (!strcmp(str, "all"))
159 opt_all_fields = 1;
82662ad4 160 else if (!strcmp(str, "trace"))
359d7456 161 opt_trace_field = 1;
8c250d87 162 else if (!strcmp(str, "trace:domain"))
359d7456 163 opt_trace_domain_field = 1;
8c250d87 164 else if (!strcmp(str, "trace:procname"))
359d7456 165 opt_trace_procname_field = 1;
8c250d87 166 else if (!strcmp(str, "trace:vpid"))
359d7456 167 opt_trace_vpid_field = 1;
d86d62f8 168 else if (!strcmp(str, "loglevel"))
359d7456 169 opt_loglevel_field = 1;
cba1661c 170 else {
359d7456 171 fprintf(stderr, "[error] unknown field type %s\n", str);
cba1661c
MD
172 return -EINVAL;
173 }
174 } while ((str = strtok_r(NULL, ",", &strctx)));
175 return 0;
176}
177
34ac0e6c
MD
178/*
179 * Return 0 if caller should continue, < 0 if caller should return
180 * error, > 0 if caller should exit without reporting error.
181 */
bbefb8dd 182static int parse_options(int argc, char **argv)
34ac0e6c
MD
183{
184 poptContext pc;
185 int opt, ret = 0;
186
0f980a35
MD
187 if (argc == 1) {
188 usage(stdout);
189 return 1; /* exit cleanly */
190 }
191
bbefb8dd 192 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
34ac0e6c
MD
193 poptReadDefaultConfig(pc, 0);
194
cba1661c
MD
195 /* set default */
196 opt_payload_field_names = 1;
197
34ac0e6c
MD
198 while ((opt = poptGetNextOpt(pc)) != -1) {
199 switch (opt) {
200 case OPT_HELP:
7fb21036 201 usage(stdout);
34ac0e6c
MD
202 ret = 1; /* exit cleanly */
203 goto end;
7fb21036
MD
204 case OPT_LIST:
205 list_formats(stdout);
206 ret = 1;
207 goto end;
34ac0e6c
MD
208 case OPT_VERBOSE:
209 babeltrace_verbose = 1;
210 break;
cba1661c
MD
211 case OPT_NAMES:
212 if (get_names_args(&pc)) {
213 ret = -EINVAL;
214 goto end;
215 }
216 break;
359d7456
MD
217 case OPT_FIELDS:
218 if (get_fields_args(&pc)) {
219 ret = -EINVAL;
220 goto end;
221 }
222 break;
34ac0e6c
MD
223 case OPT_DEBUG:
224 babeltrace_debug = 1;
225 break;
8d8ed9af 226 case OPT_NO_DELTA:
359d7456 227 opt_delta_field = 0;
8d8ed9af 228 break;
34ac0e6c
MD
229 default:
230 ret = -EINVAL;
231 goto end;
232 }
233 }
234
235 opt_input_path = poptGetArg(pc);
236 if (!opt_input_path) {
237 ret = -EINVAL;
238 goto end;
239 }
240 opt_output_path = poptGetArg(pc);
cba1661c 241
34ac0e6c
MD
242end:
243 if (pc) {
244 poptFreeContext(pc);
245 }
246 return ret;
247}
248
afb48eae
AA
249static void init_trace_collection(struct trace_collection *tc)
250{
251 tc->array = g_ptr_array_sized_new(DEFAULT_FILE_ARRAY_SIZE);
252}
253
254/*
255 * finalize_trace_collection() closes the opened traces for read
256 * and free the memory allocated for trace collection
257 */
258static void finalize_trace_collection(struct trace_collection *tc)
259{
260 int i;
261
262 for (i = 0; i < tc->array->len; i++) {
263 struct trace_descriptor *temp =
264 g_ptr_array_index(tc->array, i);
265 fmt_read->close_trace(temp);
266 }
267 g_ptr_array_free(tc->array, TRUE);
268}
269
270static void trace_collection_add(struct trace_collection *tc,
271 struct trace_descriptor *td)
272{
273 g_ptr_array_add(tc->array, td);
274}
275
95d36295
JD
276int convert_trace(struct trace_descriptor *td_write,
277 struct bt_context *ctx)
278{
279 struct babeltrace_iter *iter;
280 struct ctf_stream *stream;
281 struct ctf_stream_event *event;
282 struct ctf_text_stream_pos *sout;
283 struct trace_collection_pos begin_pos;
284 int ret;
285
286 sout = container_of(td_write, struct ctf_text_stream_pos,
287 trace_descriptor);
288
289 begin_pos.type = BT_SEEK_BEGIN;
290 iter = babeltrace_iter_create(ctx, &begin_pos, NULL);
291 if (!iter) {
292 ret = -1;
293 goto error_iter;
294 }
295 while (babeltrace_iter_read_event(iter, &stream, &event) == 0) {
296 ret = sout->parent.event_cb(&sout->parent, stream);
297 if (ret) {
3394d22e 298 fprintf(stderr, "[error] Writing event failed.\n");
95d36295
JD
299 goto end;
300 }
301 ret = babeltrace_iter_next(iter);
302 if (ret < 0)
303 goto end;
304 }
305 ret = 0;
306
307end:
308 babeltrace_iter_destroy(iter);
309error_iter:
310 return ret;
311}
312
313
afb48eae
AA
314/*
315 * traverse_dir() is the callback functiion for File Tree Walk (nftw).
316 * it receives the path of the current entry (file, dir, link..etc) with
317 * a flag to indicate the type of the entry.
318 * if the entry being visited is a directory and contains a metadata file,
319 * then open it for reading and save a trace_descriptor to that directory
320 * in the read trace collection.
321 */
322static int traverse_dir(const char *fpath, const struct stat *sb,
323 int tflag, struct FTW *ftwbuf)
324{
325 int dirfd;
326 int fd;
327 struct trace_descriptor *td_read;
328
329 if (tflag != FTW_D)
330 return 0;
331 dirfd = open(fpath, 0);
332 if (dirfd < 0) {
3394d22e 333 fprintf(stderr, "[error] unable to open trace "
afb48eae
AA
334 "directory file descriptor.\n");
335 return -1;
336 }
337 fd = openat(dirfd, "metadata", O_RDONLY);
338 if (fd < 0) {
339 close(dirfd);
340 } else {
341 close(fd);
342 close(dirfd);
8c250d87
MD
343 td_read = fmt_read->open_trace(opt_input_path,
344 fpath, O_RDONLY, ctf_move_pos_slow,
ae23d232 345 NULL);
afb48eae 346 if (!td_read) {
3394d22e 347 fprintf(stderr, "Error opening trace \"%s\" "
afb48eae
AA
348 "for reading.\n\n", fpath);
349 return -1; /* error */
350 }
351 trace_collection_add(&trace_collection_read, td_read);
352 }
353 return 0; /* success */
354}
355
bbefb8dd 356int main(int argc, char **argv)
34ac0e6c
MD
357{
358 int ret;
afb48eae
AA
359 struct format *fmt_write;
360 struct trace_descriptor *td_write;
95d36295 361 struct bt_context *ctx;
34ac0e6c
MD
362
363 ret = parse_options(argc, argv);
364 if (ret < 0) {
3394d22e
MD
365 fprintf(stderr, "Error parsing options.\n\n");
366 usage(stderr);
34ac0e6c
MD
367 exit(EXIT_FAILURE);
368 } else if (ret > 0) {
369 exit(EXIT_SUCCESS);
370 }
371 printf_verbose("Verbose mode active.\n");
372 printf_debug("Debug mode active.\n");
373
bbefb8dd
MD
374 if (opt_input_format)
375 strlower(opt_input_format);
376 if (opt_output_format)
377 strlower(opt_output_format);
378
afb48eae 379 printf_verbose("Converting from directory: %s\n", opt_input_path);
34ac0e6c 380 printf_verbose("Converting from format: %s\n",
b61922b5 381 opt_input_format ? : "ctf <default>");
afb48eae 382 printf_verbose("Converting to directory: %s\n",
478b6389 383 opt_output_path ? : "<stdout>");
34ac0e6c 384 printf_verbose("Converting to format: %s\n",
b61922b5 385 opt_output_format ? : "text <default>");
bbefb8dd 386
b61922b5
MD
387 if (!opt_input_format)
388 opt_input_format = "ctf";
389 if (!opt_output_format)
390 opt_output_format = "text";
bbefb8dd
MD
391 fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
392 if (!fmt_read) {
3394d22e 393 fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n",
bbefb8dd
MD
394 opt_input_format);
395 exit(EXIT_FAILURE);
396 }
bbefb8dd
MD
397 fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
398 if (!fmt_write) {
3394d22e 399 fprintf(stderr, "[error] format \"%s\" is not supported.\n\n",
bbefb8dd
MD
400 opt_output_format);
401 exit(EXIT_FAILURE);
402 }
403
afb48eae
AA
404 /*
405 * pass the input path to nftw() .
406 * specify traverse_dir() as the callback function.
407 * depth = 10 which is the max number of file descriptors
408 * that nftw() can open at a given time.
409 * flags = 0 check nftw documentation for more info .
410 */
411 init_trace_collection(&trace_collection_read);
412 ret = nftw(opt_input_path, traverse_dir, 10, 0);
413 if (ret != 0) {
3394d22e 414 fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n",
bbefb8dd
MD
415 opt_input_path);
416 goto error_td_read;
417 }
afb48eae 418 if (trace_collection_read.array->len == 0) {
3394d22e 419 fprintf(stderr, "[warning] no metadata file was found."
afb48eae
AA
420 " no output was generated\n");
421 return 0;
422 }
95d36295
JD
423 ctx = bt_context_create(&trace_collection_read);
424 if (!ctx) {
3394d22e 425 fprintf(stderr, "Error allocating a new context\n");
95d36295
JD
426 goto error_td_read;
427 }
8c250d87 428 td_write = fmt_write->open_trace(NULL, opt_output_path, O_RDWR, NULL, NULL);
bbefb8dd 429 if (!td_write) {
3394d22e 430 fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n",
b61922b5 431 opt_output_path ? : "<none>");
bbefb8dd
MD
432 goto error_td_write;
433 }
847bf71a 434
95d36295 435 ret = convert_trace(td_write, ctx);
46322b33 436 if (ret) {
3394d22e 437 fprintf(stderr, "Error printing trace.\n\n");
46322b33
MD
438 goto error_copy_trace;
439 }
847bf71a 440
bbefb8dd 441 fmt_write->close_trace(td_write);
afb48eae 442 finalize_trace_collection(&trace_collection_read);
95d36295 443 bt_context_destroy(ctx);
afb48eae
AA
444 printf_verbose("finished converting. Output written to:\n%s\n",
445 opt_output_path ? : "<stdout>");
bbefb8dd 446 exit(EXIT_SUCCESS);
34ac0e6c 447
bbefb8dd 448 /* Error handling */
46322b33 449error_copy_trace:
bbefb8dd
MD
450 fmt_write->close_trace(td_write);
451error_td_write:
afb48eae 452 finalize_trace_collection(&trace_collection_read);
bbefb8dd
MD
453error_td_read:
454 exit(EXIT_FAILURE);
4c8bfb7e 455}
This page took 0.044302 seconds and 4 git commands to generate.