API : split iterator headers from babeltrace.h
[babeltrace.git] / converter / babeltrace.c
1 /*
2 * babeltrace.c
3 *
4 * Babeltrace Trace Converter
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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 */
20
21 #define _XOPEN_SOURCE 700
22 #include <config.h>
23 #include <babeltrace/babeltrace.h>
24 #include <babeltrace/format.h>
25 #include <babeltrace/context.h>
26 #include <babeltrace/ctf/types.h>
27 #include <babeltrace/ctf-text/types.h>
28 #include <babeltrace/iterator.h>
29 #include <popt.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <fcntl.h>
36 #include <ftw.h>
37 #include <dirent.h>
38 #include <unistd.h>
39
40 #define DEFAULT_FILE_ARRAY_SIZE 1
41 static char *opt_input_format;
42 static char *opt_output_format;
43
44 static const char *opt_input_path;
45 static const char *opt_output_path;
46
47 static struct trace_collection trace_collection_read;
48 static struct format *fmt_read;
49
50 void strlower(char *str)
51 {
52 while (*str) {
53 *str = tolower(*str);
54 str++;
55 }
56 }
57
58 enum {
59 OPT_NONE = 0,
60 OPT_HELP,
61 OPT_LIST,
62 OPT_VERBOSE,
63 OPT_DEBUG,
64 OPT_NAMES,
65 OPT_NO_DELTA,
66 };
67
68 static 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 },
73 { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL },
74 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
75 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
76 { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
77 { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL },
78 { NULL, 0, 0, NULL, 0, NULL, NULL },
79 };
80
81 static void list_formats(FILE *fp)
82 {
83 fprintf(fp, "\n");
84 bt_fprintf_format_list(fp);
85 }
86
87 static void usage(FILE *fp)
88 {
89 fprintf(fp, "BabelTrace Trace Converter %s\n\n", VERSION);
90 fprintf(fp, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
91 fprintf(fp, "\n");
92 fprintf(fp, " INPUT Input trace path\n");
93 fprintf(fp, " OUTPUT Output trace path (default: stdout)\n");
94 fprintf(fp, "\n");
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");
97 fprintf(fp, "\n");
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");
101 fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n");
102 fprintf(fp, " -d, --debug Debug mode\n");
103 fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n");
104 fprintf(fp, " --no-delta Do not print time delta between consecutive events\n");
105 fprintf(fp, " -n, --names name1<,name2,...> Print field names.\n");
106 fprintf(fp, " Available field names:\n");
107 fprintf(fp, " (payload OR args OR arg)\n");
108 fprintf(fp, " all, scope, header, (context OR ctx)\n");
109 fprintf(fp, " trace, trace:domain, trace:procname, trace:vpid,\n");
110 fprintf(fp, " loglevel.\n");
111 fprintf(fp, " (payload active by default)\n");
112 list_formats(fp);
113 fprintf(fp, "\n");
114 }
115
116 static 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;
137 else if (!strcmp(str, "trace"))
138 opt_trace_name = 1;
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;
145 else if (!strcmp(str, "loglevel"))
146 opt_loglevel = 1;
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
155 /*
156 * Return 0 if caller should continue, < 0 if caller should return
157 * error, > 0 if caller should exit without reporting error.
158 */
159 static int parse_options(int argc, char **argv)
160 {
161 poptContext pc;
162 int opt, ret = 0;
163
164 if (argc == 1) {
165 usage(stdout);
166 return 1; /* exit cleanly */
167 }
168
169 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
170 poptReadDefaultConfig(pc, 0);
171
172 /* set default */
173 opt_payload_field_names = 1;
174
175 while ((opt = poptGetNextOpt(pc)) != -1) {
176 switch (opt) {
177 case OPT_HELP:
178 usage(stdout);
179 ret = 1; /* exit cleanly */
180 goto end;
181 case OPT_LIST:
182 list_formats(stdout);
183 ret = 1;
184 goto end;
185 case OPT_VERBOSE:
186 babeltrace_verbose = 1;
187 break;
188 case OPT_NAMES:
189 if (get_names_args(&pc)) {
190 ret = -EINVAL;
191 goto end;
192 }
193 break;
194 case OPT_DEBUG:
195 babeltrace_debug = 1;
196 break;
197 case OPT_NO_DELTA:
198 opt_delta = 0;
199 break;
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);
212
213 end:
214 if (pc) {
215 poptFreeContext(pc);
216 }
217 return ret;
218 }
219
220 static 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 */
229 static 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
241 static void trace_collection_add(struct trace_collection *tc,
242 struct trace_descriptor *td)
243 {
244 g_ptr_array_add(tc->array, td);
245 }
246
247 int 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
278 end:
279 babeltrace_iter_destroy(iter);
280 error_iter:
281 return ret;
282 }
283
284
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 */
293 static 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);
314 td_read = fmt_read->open_trace(opt_input_path,
315 fpath, O_RDONLY, ctf_move_pos_slow,
316 NULL);
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
327 int main(int argc, char **argv)
328 {
329 int ret;
330 struct format *fmt_write;
331 struct trace_descriptor *td_write;
332 struct bt_context *ctx;
333
334 ret = parse_options(argc, argv);
335 if (ret < 0) {
336 fprintf(stdout, "Error parsing options.\n\n");
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
345 if (opt_input_format)
346 strlower(opt_input_format);
347 if (opt_output_format)
348 strlower(opt_output_format);
349
350 printf_verbose("Converting from directory: %s\n", opt_input_path);
351 printf_verbose("Converting from format: %s\n",
352 opt_input_format ? : "ctf <default>");
353 printf_verbose("Converting to directory: %s\n",
354 opt_output_path ? : "<stdout>");
355 printf_verbose("Converting to format: %s\n",
356 opt_output_format ? : "text <default>");
357
358 if (!opt_input_format)
359 opt_input_format = "ctf";
360 if (!opt_output_format)
361 opt_output_format = "text";
362 fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
363 if (!fmt_read) {
364 fprintf(stdout, "[error] Format \"%s\" is not supported.\n\n",
365 opt_input_format);
366 exit(EXIT_FAILURE);
367 }
368 fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
369 if (!fmt_write) {
370 fprintf(stdout, "[error] format \"%s\" is not supported.\n\n",
371 opt_output_format);
372 exit(EXIT_FAILURE);
373 }
374
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) {
385 fprintf(stdout, "[error] opening trace \"%s\" for reading.\n\n",
386 opt_input_path);
387 goto error_td_read;
388 }
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 }
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 }
399 td_write = fmt_write->open_trace(NULL, opt_output_path, O_RDWR, NULL, NULL);
400 if (!td_write) {
401 fprintf(stdout, "Error opening trace \"%s\" for writing.\n\n",
402 opt_output_path ? : "<none>");
403 goto error_td_write;
404 }
405
406 ret = convert_trace(td_write, ctx);
407 if (ret) {
408 fprintf(stdout, "Error printing trace.\n\n");
409 goto error_copy_trace;
410 }
411
412 fmt_write->close_trace(td_write);
413 finalize_trace_collection(&trace_collection_read);
414 bt_context_destroy(ctx);
415 printf_verbose("finished converting. Output written to:\n%s\n",
416 opt_output_path ? : "<stdout>");
417 exit(EXIT_SUCCESS);
418
419 /* Error handling */
420 error_copy_trace:
421 fmt_write->close_trace(td_write);
422 error_td_write:
423 finalize_trace_collection(&trace_collection_read);
424 error_td_read:
425 exit(EXIT_FAILURE);
426 }
This page took 0.038392 seconds and 5 git commands to generate.