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