Fix: make warnings (partial errors) visible
[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 <ftw.h>
43 #include <string.h>
44
45 #include <babeltrace/ctf-ir/metadata.h> /* for clocks */
46
47 #define PARTIAL_ERROR_SLEEP 3 /* 3 seconds */
48
49 #define DEFAULT_FILE_ARRAY_SIZE 1
50 static char *opt_input_format, *opt_output_format;
51 /* Pointer into const argv */
52 static const char *opt_input_format_arg, *opt_output_format_arg;
53
54 static const char *opt_input_path;
55 static const char *opt_output_path;
56
57 static struct format *fmt_read;
58
59 static
60 void strlower(char *str)
61 {
62 while (*str) {
63 *str = tolower((int) *str);
64 str++;
65 }
66 }
67
68 enum {
69 OPT_NONE = 0,
70 OPT_HELP,
71 OPT_LIST,
72 OPT_VERBOSE,
73 OPT_DEBUG,
74 OPT_NAMES,
75 OPT_FIELDS,
76 OPT_NO_DELTA,
77 OPT_CLOCK_OFFSET,
78 OPT_CLOCK_CYCLES,
79 OPT_CLOCK_SECONDS,
80 OPT_CLOCK_DATE,
81 OPT_CLOCK_GMT,
82 OPT_CLOCK_FORCE_CORRELATE,
83 };
84
85 static struct poptOption long_options[] = {
86 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
87 { "input-format", 'i', POPT_ARG_STRING, &opt_input_format_arg, OPT_NONE, NULL, NULL },
88 { "output-format", 'o', POPT_ARG_STRING, &opt_output_format_arg, OPT_NONE, NULL, NULL },
89 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
90 { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL },
91 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
92 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
93 { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
94 { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL },
95 { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL },
96 { "clock-offset", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL },
97 { "clock-cycles", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_CYCLES, NULL, NULL },
98 { "clock-seconds", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_SECONDS, NULL, NULL },
99 { "clock-date", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_DATE, NULL, NULL },
100 { "clock-gmt", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_GMT, NULL, NULL },
101 { "clock-force-correlate", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_FORCE_CORRELATE, NULL, NULL },
102 { NULL, 0, 0, NULL, 0, NULL, NULL },
103 };
104
105 static void list_formats(FILE *fp)
106 {
107 fprintf(fp, "\n");
108 bt_fprintf_format_list(fp);
109 }
110
111 static void usage(FILE *fp)
112 {
113 fprintf(fp, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION);
114 fprintf(fp, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
115 fprintf(fp, "\n");
116 fprintf(fp, " INPUT Input trace path\n");
117 fprintf(fp, " OUTPUT Output trace path (default: stdout)\n");
118 fprintf(fp, "\n");
119 fprintf(fp, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
120 fprintf(fp, " -o, --output-format FORMAT Output trace format (default: text)\n");
121 fprintf(fp, "\n");
122 fprintf(fp, " -h, --help This help message\n");
123 fprintf(fp, " -l, --list List available formats\n");
124 fprintf(fp, " -v, --verbose Verbose mode\n");
125 fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n");
126 fprintf(fp, " -d, --debug Debug mode\n");
127 fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n");
128 fprintf(fp, " --no-delta Do not print time delta between consecutive events\n");
129 fprintf(fp, " -n, --names name1<,name2,...> Print field names:\n");
130 fprintf(fp, " (payload OR args OR arg)\n");
131 fprintf(fp, " none, all, scope, header, (context OR ctx)\n");
132 fprintf(fp, " (default: payload,context)\n");
133 fprintf(fp, " -f, --fields name1<,name2,...> Print additional fields:\n");
134 fprintf(fp, " all, trace, trace:domain, trace:procname,\n");
135 fprintf(fp, " trace:vpid, loglevel.\n");
136 fprintf(fp, " --clock-cycles Timestamp in cycles\n");
137 fprintf(fp, " --clock-offset seconds Clock offset in seconds\n");
138 fprintf(fp, " --clock-seconds Print the timestamps as [sec.ns]\n");
139 fprintf(fp, " (default is: [hh:mm:ss.ns])\n");
140 fprintf(fp, " --clock-date Print clock date\n");
141 fprintf(fp, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n");
142 fprintf(fp, " --clock-force-correlate Assume that clocks are inherently correlated\n");
143 fprintf(fp, " across traces.\n");
144 list_formats(fp);
145 fprintf(fp, "\n");
146 }
147
148 static int get_names_args(poptContext *pc)
149 {
150 char *str, *strlist, *strctx;
151
152 opt_payload_field_names = 0;
153 opt_context_field_names = 0;
154 strlist = (char *) poptGetOptArg(*pc);
155 if (!strlist) {
156 return -EINVAL;
157 }
158 str = strtok_r(strlist, ",", &strctx);
159 do {
160 if (!strcmp(str, "all"))
161 opt_all_field_names = 1;
162 else if (!strcmp(str, "scope"))
163 opt_scope_field_names = 1;
164 else if (!strcmp(str, "context") || !strcmp(str, "ctx"))
165 opt_context_field_names = 1;
166 else if (!strcmp(str, "header"))
167 opt_header_field_names = 1;
168 else if (!strcmp(str, "payload") || !strcmp(str, "args") || !strcmp(str, "arg"))
169 opt_payload_field_names = 1;
170 else if (!strcmp(str, "none")) {
171 opt_all_field_names = 0;
172 opt_scope_field_names = 0;
173 opt_context_field_names = 0;
174 opt_header_field_names = 0;
175 opt_payload_field_names = 0;
176 } else {
177 fprintf(stderr, "[error] unknown field name type %s\n", str);
178 return -EINVAL;
179 }
180 } while ((str = strtok_r(NULL, ",", &strctx)));
181 return 0;
182 }
183
184 static int get_fields_args(poptContext *pc)
185 {
186 char *str, *strlist, *strctx;
187
188 strlist = (char *) poptGetOptArg(*pc);
189 if (!strlist) {
190 return -EINVAL;
191 }
192 str = strtok_r(strlist, ",", &strctx);
193 do {
194 if (!strcmp(str, "all"))
195 opt_all_fields = 1;
196 else if (!strcmp(str, "trace"))
197 opt_trace_field = 1;
198 else if (!strcmp(str, "trace:domain"))
199 opt_trace_domain_field = 1;
200 else if (!strcmp(str, "trace:procname"))
201 opt_trace_procname_field = 1;
202 else if (!strcmp(str, "trace:vpid"))
203 opt_trace_vpid_field = 1;
204 else if (!strcmp(str, "loglevel"))
205 opt_loglevel_field = 1;
206 else {
207 fprintf(stderr, "[error] unknown field type %s\n", str);
208 return -EINVAL;
209 }
210 } while ((str = strtok_r(NULL, ",", &strctx)));
211 return 0;
212 }
213
214 /*
215 * Return 0 if caller should continue, < 0 if caller should return
216 * error, > 0 if caller should exit without reporting error.
217 */
218 static int parse_options(int argc, char **argv)
219 {
220 poptContext pc;
221 int opt, ret = 0;
222
223 if (argc == 1) {
224 usage(stdout);
225 return 1; /* exit cleanly */
226 }
227
228 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
229 poptReadDefaultConfig(pc, 0);
230
231 /* set default */
232 opt_context_field_names = 1;
233 opt_payload_field_names = 1;
234
235 while ((opt = poptGetNextOpt(pc)) != -1) {
236 switch (opt) {
237 case OPT_HELP:
238 usage(stdout);
239 ret = 1; /* exit cleanly */
240 goto end;
241 case OPT_LIST:
242 list_formats(stdout);
243 ret = 1;
244 goto end;
245 case OPT_VERBOSE:
246 babeltrace_verbose = 1;
247 break;
248 case OPT_NAMES:
249 if (get_names_args(&pc)) {
250 ret = -EINVAL;
251 goto end;
252 }
253 break;
254 case OPT_FIELDS:
255 if (get_fields_args(&pc)) {
256 ret = -EINVAL;
257 goto end;
258 }
259 break;
260 case OPT_DEBUG:
261 babeltrace_debug = 1;
262 break;
263 case OPT_NO_DELTA:
264 opt_delta_field = 0;
265 break;
266 case OPT_CLOCK_CYCLES:
267 opt_clock_cycles = 1;
268 break;
269 case OPT_CLOCK_OFFSET:
270 {
271 const char *str;
272 char *endptr;
273
274 str = poptGetOptArg(pc);
275 if (!str) {
276 fprintf(stderr, "[error] Missing --clock-offset argument\n");
277 ret = -EINVAL;
278 goto end;
279 }
280 errno = 0;
281 opt_clock_offset = strtoull(str, &endptr, 0);
282 if (*endptr != '\0' || str == endptr || errno != 0) {
283 fprintf(stderr, "[error] Incorrect --clock-offset argument: %s\n", str);
284 ret = -EINVAL;
285 goto end;
286 }
287 break;
288 }
289 case OPT_CLOCK_SECONDS:
290 opt_clock_seconds = 1;
291 break;
292 case OPT_CLOCK_DATE:
293 opt_clock_date = 1;
294 break;
295 case OPT_CLOCK_GMT:
296 opt_clock_gmt = 1;
297 break;
298 case OPT_CLOCK_FORCE_CORRELATE:
299 opt_clock_force_correlate = 1;
300 break;
301
302 default:
303 ret = -EINVAL;
304 goto end;
305 }
306 }
307
308 opt_input_path = poptGetArg(pc);
309 if (!opt_input_path) {
310 ret = -EINVAL;
311 goto end;
312 }
313 opt_output_path = poptGetArg(pc);
314
315 end:
316 if (pc) {
317 poptFreeContext(pc);
318 }
319 return ret;
320 }
321
322 static GPtrArray *traversed_paths = 0;
323
324 /*
325 * traverse_trace_dir() is the callback function for File Tree Walk (nftw).
326 * it receives the path of the current entry (file, dir, link..etc) with
327 * a flag to indicate the type of the entry.
328 * if the entry being visited is a directory and contains a metadata file,
329 * then add the path to a global list to be processed later in
330 * add_traces_recursive.
331 */
332 static int traverse_trace_dir(const char *fpath, const struct stat *sb,
333 int tflag, struct FTW *ftwbuf)
334 {
335 int dirfd, metafd;
336 int closeret;
337
338 if (tflag != FTW_D)
339 return 0;
340
341 dirfd = open(fpath, 0);
342 if (dirfd < 0) {
343 fprintf(stderr, "[error] [Context] Unable to open trace "
344 "directory file descriptor.\n");
345 return 0; /* partial error */
346 }
347 metafd = openat(dirfd, "metadata", O_RDONLY);
348 if (metafd < 0) {
349 closeret = close(dirfd);
350 if (closeret < 0) {
351 perror("close");
352 return -1;
353 }
354 /* No meta data, just return */
355 return 0;
356 } else {
357 closeret = close(metafd);
358 if (closeret < 0) {
359 perror("close");
360 return -1; /* failure */
361 }
362 closeret = close(dirfd);
363 if (closeret < 0) {
364 perror("close");
365 return -1; /* failure */
366 }
367
368 /* Add path to the global list */
369 if (traversed_paths == NULL) {
370 fprintf(stderr, "[error] [Context] Invalid open path array.\n");
371 return -1;
372 }
373 g_ptr_array_add(traversed_paths, g_string_new(fpath));
374 }
375
376 return 0;
377 }
378 /*
379 * bt_context_add_traces_recursive: Open a trace recursively
380 *
381 * Find each trace present in the subdirectory starting from the given
382 * path, and add them to the context. The packet_seek parameter can be
383 * NULL: this specify to use the default format packet_seek.
384 *
385 * Return: 0 on success, < 0 on failure, > 0 on partial failure.
386 * Unable to open toplevel: failure.
387 * Unable to open some subdirectory or file: warn and continue (partial
388 * failure);
389 */
390 int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
391 const char *format_str,
392 void (*packet_seek)(struct stream_pos *pos,
393 size_t offset, int whence))
394 {
395
396 GArray *trace_ids;
397 int ret = 0;
398 int i;
399
400 /* Should lock traversed_paths mutex here if used in multithread */
401
402 traversed_paths = g_ptr_array_new();
403 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
404
405 ret = nftw(path, traverse_trace_dir, 10, 0);
406
407 /* Process the array if ntfw did not return a fatal error */
408 if (ret >= 0) {
409 for (i = 0; i < traversed_paths->len; i++) {
410 GString *trace_path = g_ptr_array_index(traversed_paths,
411 i);
412 int trace_id = bt_context_add_trace(ctx,
413 trace_path->str,
414 format_str,
415 packet_seek,
416 NULL,
417 NULL);
418 if (trace_id < 0) {
419 fprintf(stderr, "[warning] [Context] cannot open trace \"%s\" from %s "
420 "for reading.\n", trace_path->str, path);
421 /* Allow to skip erroneous traces. */
422 ret = 1; /* partial error */
423 } else {
424 g_array_append_val(trace_ids, trace_id);
425 }
426 g_string_free(trace_path, TRUE);
427 }
428 }
429 g_ptr_array_free(traversed_paths, TRUE);
430 traversed_paths = NULL;
431
432 /* Should unlock traversed paths mutex here if used in multithread */
433
434 /*
435 * Return an error if no trace can be opened.
436 */
437 if (trace_ids->len == 0) {
438 fprintf(stderr, "[error] Cannot open any trace for reading.\n\n");
439 ret = -ENOENT; /* failure */
440 }
441 g_array_free(trace_ids, TRUE);
442 return ret;
443 }
444
445 int convert_trace(struct trace_descriptor *td_write,
446 struct bt_context *ctx)
447 {
448 struct bt_ctf_iter *iter;
449 struct ctf_text_stream_pos *sout;
450 struct bt_iter_pos begin_pos;
451 struct bt_ctf_event *ctf_event;
452 int ret;
453
454 sout = container_of(td_write, struct ctf_text_stream_pos,
455 trace_descriptor);
456
457 begin_pos.type = BT_SEEK_BEGIN;
458 iter = bt_ctf_iter_create(ctx, &begin_pos, NULL);
459 if (!iter) {
460 ret = -1;
461 goto error_iter;
462 }
463 while ((ctf_event = bt_ctf_iter_read_event(iter))) {
464 ret = sout->parent.event_cb(&sout->parent, ctf_event->parent->stream);
465 if (ret) {
466 fprintf(stderr, "[error] Writing event failed.\n");
467 goto end;
468 }
469 ret = bt_iter_next(bt_ctf_get_iter(iter));
470 if (ret < 0)
471 goto end;
472 }
473 ret = 0;
474
475 end:
476 bt_ctf_iter_destroy(iter);
477 error_iter:
478 return ret;
479 }
480
481 int main(int argc, char **argv)
482 {
483 int ret, partial_error = 0;
484 struct format *fmt_write;
485 struct trace_descriptor *td_write;
486 struct bt_context *ctx;
487
488 ret = parse_options(argc, argv);
489 if (ret < 0) {
490 fprintf(stderr, "Error parsing options.\n\n");
491 usage(stderr);
492 exit(EXIT_FAILURE);
493 } else if (ret > 0) {
494 exit(EXIT_SUCCESS);
495 }
496 printf_verbose("Verbose mode active.\n");
497 printf_debug("Debug mode active.\n");
498
499 if (opt_input_format_arg) {
500 opt_input_format = strdup(opt_input_format_arg);
501 if (!opt_input_format) {
502 partial_error = 1;
503 goto end;
504 }
505 strlower(opt_input_format);
506 }
507 if (opt_output_format_arg) {
508 opt_output_format = strdup(opt_output_format_arg);
509 if (!opt_output_format) {
510 partial_error = 1;
511 goto end;
512 }
513 strlower(opt_output_format);
514 }
515
516 printf_verbose("Converting from directory: %s\n", opt_input_path);
517 printf_verbose("Converting from format: %s\n",
518 opt_input_format ? : "ctf <default>");
519 printf_verbose("Converting to directory: %s\n",
520 opt_output_path ? : "<stdout>");
521 printf_verbose("Converting to format: %s\n",
522 opt_output_format ? : "text <default>");
523
524 if (!opt_input_format) {
525 opt_input_format = strdup("ctf");
526 if (!opt_input_format) {
527 partial_error = 1;
528 goto end;
529 }
530 }
531 if (!opt_output_format) {
532 opt_output_format = strdup("text");
533 if (!opt_output_format) {
534 partial_error = 1;
535 goto end;
536 }
537 }
538 fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
539 if (!fmt_read) {
540 fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n",
541 opt_input_format);
542 partial_error = 1;
543 goto end;
544 }
545 fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
546 if (!fmt_write) {
547 fprintf(stderr, "[error] format \"%s\" is not supported.\n\n",
548 opt_output_format);
549 partial_error = 1;
550 goto end;
551 }
552
553 ctx = bt_context_create();
554 if (!ctx) {
555 goto error_td_read;
556 }
557
558 ret = bt_context_add_traces_recursive(ctx, opt_input_path,
559 opt_input_format, NULL);
560 if (ret < 0) {
561 fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n",
562 opt_input_path);
563 goto error_td_read;
564 } else if (ret > 0) {
565 fprintf(stderr, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n",
566 opt_input_path);
567 partial_error = 1;
568 }
569
570 td_write = fmt_write->open_trace(opt_output_path, O_RDWR, NULL, NULL);
571 if (!td_write) {
572 fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n",
573 opt_output_path ? : "<none>");
574 goto error_td_write;
575 }
576
577 /*
578 * Errors happened when opening traces, but we continue anyway.
579 * sleep to let user see the stderr output before stdout.
580 */
581 if (partial_error) {
582 sleep(PARTIAL_ERROR_SLEEP);
583 }
584
585 ret = convert_trace(td_write, ctx);
586 if (ret) {
587 fprintf(stderr, "Error printing trace.\n\n");
588 goto error_copy_trace;
589 }
590
591 fmt_write->close_trace(td_write);
592
593 bt_context_put(ctx);
594 printf_verbose("finished converting. Output written to:\n%s\n",
595 opt_output_path ? : "<stdout>");
596 goto end;
597
598 /* Error handling */
599 error_copy_trace:
600 fmt_write->close_trace(td_write);
601 error_td_write:
602 bt_context_put(ctx);
603 error_td_read:
604 partial_error = 1;
605
606 /* teardown and exit */
607 end:
608 free(opt_input_format);
609 free(opt_output_format);
610 if (partial_error)
611 exit(EXIT_FAILURE);
612 else
613 exit(EXIT_SUCCESS);
614 }
This page took 0.042127 seconds and 5 git commands to generate.