Implement clock mapping within integer type
[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 };
68
69 static 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 },
74 { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL },
75 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
76 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
77 { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
78 { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL },
79 { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL },
80 { NULL, 0, 0, NULL, 0, NULL, NULL },
81 };
82
83 static void list_formats(FILE *fp)
84 {
85 fprintf(fp, "\n");
86 bt_fprintf_format_list(fp);
87 }
88
89 static void usage(FILE *fp)
90 {
91 fprintf(fp, "BabelTrace Trace Converter %s\n\n", VERSION);
92 fprintf(fp, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
93 fprintf(fp, "\n");
94 fprintf(fp, " INPUT Input trace path\n");
95 fprintf(fp, " OUTPUT Output trace path (default: stdout)\n");
96 fprintf(fp, "\n");
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");
99 fprintf(fp, "\n");
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");
103 fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n");
104 fprintf(fp, " -d, --debug Debug mode\n");
105 fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n");
106 fprintf(fp, " --no-delta Do not print time delta between consecutive events\n");
107 fprintf(fp, " -n, --names name1<,name2,...> Print field names:\n");
108 fprintf(fp, " (payload OR args OR arg)\n");
109 fprintf(fp, " all, scope, header, (context OR ctx)\n");
110 fprintf(fp, " (payload active by default)\n");
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");
114 list_formats(fp);
115 fprintf(fp, "\n");
116 }
117
118 static 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;
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
147 static 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;
160 else if (!strcmp(str, "trace"))
161 opt_trace_field = 1;
162 else if (!strcmp(str, "trace:domain"))
163 opt_trace_domain_field = 1;
164 else if (!strcmp(str, "trace:procname"))
165 opt_trace_procname_field = 1;
166 else if (!strcmp(str, "trace:vpid"))
167 opt_trace_vpid_field = 1;
168 else if (!strcmp(str, "loglevel"))
169 opt_loglevel_field = 1;
170 else {
171 fprintf(stderr, "[error] unknown field type %s\n", str);
172 return -EINVAL;
173 }
174 } while ((str = strtok_r(NULL, ",", &strctx)));
175 return 0;
176 }
177
178 /*
179 * Return 0 if caller should continue, < 0 if caller should return
180 * error, > 0 if caller should exit without reporting error.
181 */
182 static int parse_options(int argc, char **argv)
183 {
184 poptContext pc;
185 int opt, ret = 0;
186
187 if (argc == 1) {
188 usage(stdout);
189 return 1; /* exit cleanly */
190 }
191
192 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
193 poptReadDefaultConfig(pc, 0);
194
195 /* set default */
196 opt_payload_field_names = 1;
197
198 while ((opt = poptGetNextOpt(pc)) != -1) {
199 switch (opt) {
200 case OPT_HELP:
201 usage(stdout);
202 ret = 1; /* exit cleanly */
203 goto end;
204 case OPT_LIST:
205 list_formats(stdout);
206 ret = 1;
207 goto end;
208 case OPT_VERBOSE:
209 babeltrace_verbose = 1;
210 break;
211 case OPT_NAMES:
212 if (get_names_args(&pc)) {
213 ret = -EINVAL;
214 goto end;
215 }
216 break;
217 case OPT_FIELDS:
218 if (get_fields_args(&pc)) {
219 ret = -EINVAL;
220 goto end;
221 }
222 break;
223 case OPT_DEBUG:
224 babeltrace_debug = 1;
225 break;
226 case OPT_NO_DELTA:
227 opt_delta_field = 0;
228 break;
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);
241
242 end:
243 if (pc) {
244 poptFreeContext(pc);
245 }
246 return ret;
247 }
248
249 static 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 */
258 static 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
270 static void trace_collection_add(struct trace_collection *tc,
271 struct trace_descriptor *td)
272 {
273 g_ptr_array_add(tc->array, td);
274 }
275
276 int 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) {
298 fprintf(stderr, "[error] Writing event failed.\n");
299 goto end;
300 }
301 ret = babeltrace_iter_next(iter);
302 if (ret < 0)
303 goto end;
304 }
305 ret = 0;
306
307 end:
308 babeltrace_iter_destroy(iter);
309 error_iter:
310 return ret;
311 }
312
313
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 */
322 static 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) {
333 fprintf(stderr, "[error] unable to open trace "
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);
343 td_read = fmt_read->open_trace(opt_input_path,
344 fpath, O_RDONLY, ctf_move_pos_slow,
345 NULL);
346 if (!td_read) {
347 fprintf(stderr, "Error opening trace \"%s\" "
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
356 int main(int argc, char **argv)
357 {
358 int ret;
359 struct format *fmt_write;
360 struct trace_descriptor *td_write;
361 struct bt_context *ctx;
362
363 ret = parse_options(argc, argv);
364 if (ret < 0) {
365 fprintf(stderr, "Error parsing options.\n\n");
366 usage(stderr);
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
374 if (opt_input_format)
375 strlower(opt_input_format);
376 if (opt_output_format)
377 strlower(opt_output_format);
378
379 printf_verbose("Converting from directory: %s\n", opt_input_path);
380 printf_verbose("Converting from format: %s\n",
381 opt_input_format ? : "ctf <default>");
382 printf_verbose("Converting to directory: %s\n",
383 opt_output_path ? : "<stdout>");
384 printf_verbose("Converting to format: %s\n",
385 opt_output_format ? : "text <default>");
386
387 if (!opt_input_format)
388 opt_input_format = "ctf";
389 if (!opt_output_format)
390 opt_output_format = "text";
391 fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
392 if (!fmt_read) {
393 fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n",
394 opt_input_format);
395 exit(EXIT_FAILURE);
396 }
397 fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
398 if (!fmt_write) {
399 fprintf(stderr, "[error] format \"%s\" is not supported.\n\n",
400 opt_output_format);
401 exit(EXIT_FAILURE);
402 }
403
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) {
414 fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n",
415 opt_input_path);
416 goto error_td_read;
417 }
418 if (trace_collection_read.array->len == 0) {
419 fprintf(stderr, "[warning] no metadata file was found."
420 " no output was generated\n");
421 return 0;
422 }
423 ctx = bt_context_create(&trace_collection_read);
424 if (!ctx) {
425 fprintf(stderr, "Error allocating a new context\n");
426 goto error_td_read;
427 }
428 td_write = fmt_write->open_trace(NULL, opt_output_path, O_RDWR, NULL, NULL);
429 if (!td_write) {
430 fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n",
431 opt_output_path ? : "<none>");
432 goto error_td_write;
433 }
434
435 ret = convert_trace(td_write, ctx);
436 if (ret) {
437 fprintf(stderr, "Error printing trace.\n\n");
438 goto error_copy_trace;
439 }
440
441 fmt_write->close_trace(td_write);
442 finalize_trace_collection(&trace_collection_read);
443 bt_context_destroy(ctx);
444 printf_verbose("finished converting. Output written to:\n%s\n",
445 opt_output_path ? : "<stdout>");
446 exit(EXIT_SUCCESS);
447
448 /* Error handling */
449 error_copy_trace:
450 fmt_write->close_trace(td_write);
451 error_td_write:
452 finalize_trace_collection(&trace_collection_read);
453 error_td_read:
454 exit(EXIT_FAILURE);
455 }
This page took 0.038594 seconds and 5 git commands to generate.