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