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