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