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