Fix API: rename unclear move_pos_slow to packet_seek
[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 _GNU_SOURCE
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/events.h>
28 #include <babeltrace/ctf-text/types.h>
29 #include <babeltrace/iterator.h>
30 #include <popt.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <inttypes.h>
39 #include <fts.h>
40
41 #include <babeltrace/ctf-ir/metadata.h> /* for clocks */
42
43 #define DEFAULT_FILE_ARRAY_SIZE 1
44 static char *opt_input_format;
45 static char *opt_output_format;
46
47 static const char *opt_input_path;
48 static const char *opt_output_path;
49
50 static struct format *fmt_read;
51
52 void strlower(char *str)
53 {
54 while (*str) {
55 *str = tolower(*str);
56 str++;
57 }
58 }
59
60 enum {
61 OPT_NONE = 0,
62 OPT_HELP,
63 OPT_LIST,
64 OPT_VERBOSE,
65 OPT_DEBUG,
66 OPT_NAMES,
67 OPT_FIELDS,
68 OPT_NO_DELTA,
69 OPT_CLOCK_OFFSET,
70 OPT_CLOCK_RAW,
71 OPT_CLOCK_SECONDS,
72 OPT_CLOCK_DATE,
73 OPT_CLOCK_GMT,
74 OPT_CLOCK_FORCE_CORRELATE,
75 };
76
77 static struct poptOption long_options[] = {
78 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
79 { "input-format", 'i', POPT_ARG_STRING, &opt_input_format, OPT_NONE, NULL, NULL },
80 { "output-format", 'o', POPT_ARG_STRING, &opt_output_format, OPT_NONE, NULL, NULL },
81 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
82 { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL },
83 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
84 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
85 { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
86 { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL },
87 { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL },
88 { "clock-offset", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL },
89 { "clock-raw", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_RAW, NULL, NULL },
90 { "clock-seconds", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_SECONDS, NULL, NULL },
91 { "clock-date", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_DATE, NULL, NULL },
92 { "clock-gmt", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_GMT, NULL, NULL },
93 { "clock-force-correlate", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_FORCE_CORRELATE, NULL, NULL },
94 { NULL, 0, 0, NULL, 0, NULL, NULL },
95 };
96
97 static void list_formats(FILE *fp)
98 {
99 fprintf(fp, "\n");
100 bt_fprintf_format_list(fp);
101 }
102
103 static void usage(FILE *fp)
104 {
105 fprintf(fp, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION);
106 fprintf(fp, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
107 fprintf(fp, "\n");
108 fprintf(fp, " INPUT Input trace path\n");
109 fprintf(fp, " OUTPUT Output trace path (default: stdout)\n");
110 fprintf(fp, "\n");
111 fprintf(fp, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
112 fprintf(fp, " -o, --output-format FORMAT Output trace format (default: text)\n");
113 fprintf(fp, "\n");
114 fprintf(fp, " -h, --help This help message\n");
115 fprintf(fp, " -l, --list List available formats\n");
116 fprintf(fp, " -v, --verbose Verbose mode\n");
117 fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n");
118 fprintf(fp, " -d, --debug Debug mode\n");
119 fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n");
120 fprintf(fp, " --no-delta Do not print time delta between consecutive events\n");
121 fprintf(fp, " -n, --names name1<,name2,...> Print field names:\n");
122 fprintf(fp, " (payload OR args OR arg)\n");
123 fprintf(fp, " all, scope, header, (context OR ctx)\n");
124 fprintf(fp, " (payload active by default)\n");
125 fprintf(fp, " -f, --fields name1<,name2,...> Print additional fields:\n");
126 fprintf(fp, " all, trace, trace:domain, trace:procname,\n");
127 fprintf(fp, " trace:vpid, loglevel.\n");
128 fprintf(fp, " --clock-raw Disregard internal clock offset (use raw value)\n");
129 fprintf(fp, " --clock-offset seconds Clock offset in seconds\n");
130 fprintf(fp, " --clock-seconds Print the timestamps as [sec.ns]\n");
131 fprintf(fp, " (default is: [hh:mm:ss.ns])\n");
132 fprintf(fp, " --clock-date Print clock date\n");
133 fprintf(fp, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n");
134 fprintf(fp, " --clock-force-correlate Assume that clocks are inherently correlated\n");
135 fprintf(fp, " across traces.\n");
136 list_formats(fp);
137 fprintf(fp, "\n");
138 }
139
140 static int get_names_args(poptContext *pc)
141 {
142 char *str, *strlist, *strctx;
143
144 opt_payload_field_names = 0;
145 strlist = (char *) poptGetOptArg(*pc);
146 if (!strlist) {
147 return -EINVAL;
148 }
149 str = strtok_r(strlist, ",", &strctx);
150 do {
151 if (!strcmp(str, "all"))
152 opt_all_field_names = 1;
153 else if (!strcmp(str, "scope"))
154 opt_scope_field_names = 1;
155 else if (!strcmp(str, "context") || !strcmp(str, "ctx"))
156 opt_context_field_names = 1;
157 else if (!strcmp(str, "header"))
158 opt_header_field_names = 1;
159 else if (!strcmp(str, "payload") || !strcmp(str, "args") || !strcmp(str, "arg"))
160 opt_payload_field_names = 1;
161 else {
162 fprintf(stderr, "[error] unknown field name type %s\n", str);
163 return -EINVAL;
164 }
165 } while ((str = strtok_r(NULL, ",", &strctx)));
166 return 0;
167 }
168
169 static int get_fields_args(poptContext *pc)
170 {
171 char *str, *strlist, *strctx;
172
173 strlist = (char *) poptGetOptArg(*pc);
174 if (!strlist) {
175 return -EINVAL;
176 }
177 str = strtok_r(strlist, ",", &strctx);
178 do {
179 if (!strcmp(str, "all"))
180 opt_all_fields = 1;
181 else if (!strcmp(str, "trace"))
182 opt_trace_field = 1;
183 else if (!strcmp(str, "trace:domain"))
184 opt_trace_domain_field = 1;
185 else if (!strcmp(str, "trace:procname"))
186 opt_trace_procname_field = 1;
187 else if (!strcmp(str, "trace:vpid"))
188 opt_trace_vpid_field = 1;
189 else if (!strcmp(str, "loglevel"))
190 opt_loglevel_field = 1;
191 else {
192 fprintf(stderr, "[error] unknown field type %s\n", str);
193 return -EINVAL;
194 }
195 } while ((str = strtok_r(NULL, ",", &strctx)));
196 return 0;
197 }
198
199 /*
200 * Return 0 if caller should continue, < 0 if caller should return
201 * error, > 0 if caller should exit without reporting error.
202 */
203 static int parse_options(int argc, char **argv)
204 {
205 poptContext pc;
206 int opt, ret = 0;
207
208 if (argc == 1) {
209 usage(stdout);
210 return 1; /* exit cleanly */
211 }
212
213 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
214 poptReadDefaultConfig(pc, 0);
215
216 /* set default */
217 opt_payload_field_names = 1;
218
219 while ((opt = poptGetNextOpt(pc)) != -1) {
220 switch (opt) {
221 case OPT_HELP:
222 usage(stdout);
223 ret = 1; /* exit cleanly */
224 goto end;
225 case OPT_LIST:
226 list_formats(stdout);
227 ret = 1;
228 goto end;
229 case OPT_VERBOSE:
230 babeltrace_verbose = 1;
231 break;
232 case OPT_NAMES:
233 if (get_names_args(&pc)) {
234 ret = -EINVAL;
235 goto end;
236 }
237 break;
238 case OPT_FIELDS:
239 if (get_fields_args(&pc)) {
240 ret = -EINVAL;
241 goto end;
242 }
243 break;
244 case OPT_DEBUG:
245 babeltrace_debug = 1;
246 break;
247 case OPT_NO_DELTA:
248 opt_delta_field = 0;
249 break;
250 case OPT_CLOCK_RAW:
251 opt_clock_raw = 1;
252 break;
253 case OPT_CLOCK_OFFSET:
254 {
255 char *str, *endptr;
256
257 str = poptGetOptArg(pc);
258 if (!str) {
259 fprintf(stderr, "[error] Missing --clock-offset argument\n");
260 ret = -EINVAL;
261 goto end;
262 }
263 errno = 0;
264 opt_clock_offset = strtoull(str, &endptr, 0);
265 if (*endptr != '\0' || str == endptr || errno != 0) {
266 fprintf(stderr, "[error] Incorrect --clock-offset argument: %s\n", str);
267 ret = -EINVAL;
268 goto end;
269 }
270 break;
271 }
272 case OPT_CLOCK_SECONDS:
273 opt_clock_seconds = 1;
274 break;
275 case OPT_CLOCK_DATE:
276 opt_clock_date = 1;
277 break;
278 case OPT_CLOCK_GMT:
279 opt_clock_gmt = 1;
280 break;
281 case OPT_CLOCK_FORCE_CORRELATE:
282 opt_clock_force_correlate = 1;
283 break;
284
285 default:
286 ret = -EINVAL;
287 goto end;
288 }
289 }
290
291 opt_input_path = poptGetArg(pc);
292 if (!opt_input_path) {
293 ret = -EINVAL;
294 goto end;
295 }
296 opt_output_path = poptGetArg(pc);
297
298 end:
299 if (pc) {
300 poptFreeContext(pc);
301 }
302 return ret;
303 }
304
305
306 /*
307 * bt_context_add_traces_recursive: Open a trace recursively
308 *
309 * Find each trace present in the subdirectory starting from the given
310 * path, and add them to the context.
311 *
312 * Return: 0 on success, nonzero on failure.
313 * Unable to open toplevel: failure.
314 * Unable to open some subdirectory or file: warn and continue;
315 */
316 int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
317 const char *format_str)
318 {
319 FTS *tree;
320 FTSENT *node;
321 GArray *trace_ids;
322 char lpath[PATH_MAX];
323 char * const paths[2] = { lpath, NULL };
324 int ret;
325
326 /*
327 * Need to copy path, because fts_open can change it.
328 * It is the pointer array, not the strings, that are constant.
329 */
330 strncpy(lpath, path, PATH_MAX);
331 lpath[PATH_MAX - 1] = '\0';
332
333 tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
334 if (tree == NULL) {
335 fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
336 path);
337 return -EINVAL;
338 }
339
340 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
341
342 while ((node = fts_read(tree))) {
343 int dirfd, metafd;
344
345 if (!(node->fts_info & FTS_D))
346 continue;
347
348 dirfd = open(node->fts_accpath, 0);
349 if (dirfd < 0) {
350 fprintf(stderr, "[error] [Context] Unable to open trace "
351 "directory file descriptor.\n");
352 ret = dirfd;
353 goto error;
354 }
355 metafd = openat(dirfd, "metadata", O_RDONLY);
356 if (metafd < 0) {
357 ret = close(dirfd);
358 if (ret < 0) {
359 perror("close");
360 goto error;
361 }
362 } else {
363 int trace_id;
364
365 ret = close(metafd);
366 if (ret < 0) {
367 perror("close");
368 goto error;
369 }
370 ret = close(dirfd);
371 if (ret < 0) {
372 perror("close");
373 goto error;
374 }
375
376 trace_id = bt_context_add_trace(ctx,
377 node->fts_accpath, format_str);
378 if (trace_id < 0) {
379 fprintf(stderr, "[error] [Context] opening trace \"%s\" from %s "
380 "for reading.\n", node->fts_accpath, path);
381 ret = trace_id;
382 goto error;
383 }
384 g_array_append_val(trace_ids, trace_id);
385 }
386 }
387
388 g_array_free(trace_ids, TRUE);
389 return 0;
390
391 error:
392 return ret;
393 }
394
395
396
397 int convert_trace(struct trace_descriptor *td_write,
398 struct bt_context *ctx)
399 {
400 struct bt_iter *iter;
401 struct ctf_text_stream_pos *sout;
402 struct bt_iter_pos begin_pos;
403 struct bt_ctf_event *ctf_event;
404 int ret;
405
406 sout = container_of(td_write, struct ctf_text_stream_pos,
407 trace_descriptor);
408
409 begin_pos.type = BT_SEEK_BEGIN;
410 iter = bt_iter_create(ctx, &begin_pos, NULL);
411 if (!iter) {
412 ret = -1;
413 goto error_iter;
414 }
415 while ((ctf_event = bt_iter_read_ctf_event(iter))) {
416 ret = sout->parent.event_cb(&sout->parent, ctf_event->stream);
417 if (ret) {
418 fprintf(stderr, "[error] Writing event failed.\n");
419 goto end;
420 }
421 ret = bt_iter_next(iter);
422 if (ret < 0)
423 goto end;
424 }
425 ret = 0;
426
427 end:
428 bt_iter_destroy(iter);
429 error_iter:
430 return ret;
431 }
432
433 int main(int argc, char **argv)
434 {
435 int ret;
436 struct format *fmt_write;
437 struct trace_descriptor *td_write;
438 struct bt_context *ctx;
439
440 ret = parse_options(argc, argv);
441 if (ret < 0) {
442 fprintf(stderr, "Error parsing options.\n\n");
443 usage(stderr);
444 exit(EXIT_FAILURE);
445 } else if (ret > 0) {
446 exit(EXIT_SUCCESS);
447 }
448 printf_verbose("Verbose mode active.\n");
449 printf_debug("Debug mode active.\n");
450
451 if (opt_input_format)
452 strlower(opt_input_format);
453 if (opt_output_format)
454 strlower(opt_output_format);
455
456 printf_verbose("Converting from directory: %s\n", opt_input_path);
457 printf_verbose("Converting from format: %s\n",
458 opt_input_format ? : "ctf <default>");
459 printf_verbose("Converting to directory: %s\n",
460 opt_output_path ? : "<stdout>");
461 printf_verbose("Converting to format: %s\n",
462 opt_output_format ? : "text <default>");
463
464 if (!opt_input_format)
465 opt_input_format = "ctf";
466 if (!opt_output_format)
467 opt_output_format = "text";
468 fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
469 if (!fmt_read) {
470 fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n",
471 opt_input_format);
472 exit(EXIT_FAILURE);
473 }
474 fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
475 if (!fmt_write) {
476 fprintf(stderr, "[error] format \"%s\" is not supported.\n\n",
477 opt_output_format);
478 exit(EXIT_FAILURE);
479 }
480
481 ctx = bt_context_create();
482
483 ret = bt_context_add_traces_recursive(ctx, opt_input_path,
484 opt_input_format);
485 if (ret) {
486 fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n",
487 opt_input_path);
488 goto error_td_read;
489 }
490
491 td_write = fmt_write->open_trace(opt_output_path, O_RDWR, NULL, NULL);
492 if (!td_write) {
493 fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n",
494 opt_output_path ? : "<none>");
495 goto error_td_write;
496 }
497
498 ret = convert_trace(td_write, ctx);
499 if (ret) {
500 fprintf(stderr, "Error printing trace.\n\n");
501 goto error_copy_trace;
502 }
503
504 fmt_write->close_trace(td_write);
505
506 bt_context_put(ctx);
507 printf_verbose("finished converting. Output written to:\n%s\n",
508 opt_output_path ? : "<stdout>");
509 exit(EXIT_SUCCESS);
510
511 /* Error handling */
512 error_copy_trace:
513 fmt_write->close_trace(td_write);
514 error_td_write:
515 bt_context_put(ctx);
516 error_td_read:
517 exit(EXIT_FAILURE);
518 }
This page took 0.042294 seconds and 4 git commands to generate.