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