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