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