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