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