Cleanup: do not overwrite const argv parameters
[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 <fts.h>
43 #include <string.h>
44
45 #include <babeltrace/ctf-ir/metadata.h> /* for clocks */
46
47 #define DEFAULT_FILE_ARRAY_SIZE 1
48 static char *opt_input_format, *opt_output_format;
49 /* Pointer into const argv */
50 static const char *opt_input_format_arg, *opt_output_format_arg;
51
52 static const char *opt_input_path;
53 static const char *opt_output_path;
54
55 static struct format *fmt_read;
56
57 static
58 void strlower(char *str)
59 {
60 while (*str) {
61 *str = tolower(*str);
62 str++;
63 }
64 }
65
66 enum {
67 OPT_NONE = 0,
68 OPT_HELP,
69 OPT_LIST,
70 OPT_VERBOSE,
71 OPT_DEBUG,
72 OPT_NAMES,
73 OPT_FIELDS,
74 OPT_NO_DELTA,
75 OPT_CLOCK_OFFSET,
76 OPT_CLOCK_RAW,
77 OPT_CLOCK_SECONDS,
78 OPT_CLOCK_DATE,
79 OPT_CLOCK_GMT,
80 OPT_CLOCK_FORCE_CORRELATE,
81 };
82
83 static struct poptOption long_options[] = {
84 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
85 { "input-format", 'i', POPT_ARG_STRING, &opt_input_format_arg, OPT_NONE, NULL, NULL },
86 { "output-format", 'o', POPT_ARG_STRING, &opt_output_format_arg, OPT_NONE, NULL, NULL },
87 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
88 { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL },
89 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
90 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
91 { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
92 { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL },
93 { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL },
94 { "clock-offset", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL },
95 { "clock-raw", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_RAW, NULL, NULL },
96 { "clock-seconds", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_SECONDS, NULL, NULL },
97 { "clock-date", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_DATE, NULL, NULL },
98 { "clock-gmt", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_GMT, NULL, NULL },
99 { "clock-force-correlate", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_FORCE_CORRELATE, NULL, NULL },
100 { NULL, 0, 0, NULL, 0, NULL, NULL },
101 };
102
103 static void list_formats(FILE *fp)
104 {
105 fprintf(fp, "\n");
106 bt_fprintf_format_list(fp);
107 }
108
109 static void usage(FILE *fp)
110 {
111 fprintf(fp, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION);
112 fprintf(fp, "usage : babeltrace [OPTIONS] INPUT <OUTPUT>\n");
113 fprintf(fp, "\n");
114 fprintf(fp, " INPUT Input trace path\n");
115 fprintf(fp, " OUTPUT Output trace path (default: stdout)\n");
116 fprintf(fp, "\n");
117 fprintf(fp, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
118 fprintf(fp, " -o, --output-format FORMAT Output trace format (default: text)\n");
119 fprintf(fp, "\n");
120 fprintf(fp, " -h, --help This help message\n");
121 fprintf(fp, " -l, --list List available formats\n");
122 fprintf(fp, " -v, --verbose Verbose mode\n");
123 fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n");
124 fprintf(fp, " -d, --debug Debug mode\n");
125 fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n");
126 fprintf(fp, " --no-delta Do not print time delta between consecutive events\n");
127 fprintf(fp, " -n, --names name1<,name2,...> Print field names:\n");
128 fprintf(fp, " (payload OR args OR arg)\n");
129 fprintf(fp, " none, all, scope, header, (context OR ctx)\n");
130 fprintf(fp, " (default: payload,context)\n");
131 fprintf(fp, " -f, --fields name1<,name2,...> Print additional fields:\n");
132 fprintf(fp, " all, trace, trace:domain, trace:procname,\n");
133 fprintf(fp, " trace:vpid, loglevel.\n");
134 fprintf(fp, " --clock-raw Disregard internal clock offset (use raw value)\n");
135 fprintf(fp, " --clock-offset seconds Clock offset in seconds\n");
136 fprintf(fp, " --clock-seconds Print the timestamps as [sec.ns]\n");
137 fprintf(fp, " (default is: [hh:mm:ss.ns])\n");
138 fprintf(fp, " --clock-date Print clock date\n");
139 fprintf(fp, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n");
140 fprintf(fp, " --clock-force-correlate Assume that clocks are inherently correlated\n");
141 fprintf(fp, " across traces.\n");
142 list_formats(fp);
143 fprintf(fp, "\n");
144 }
145
146 static int get_names_args(poptContext *pc)
147 {
148 char *str, *strlist, *strctx;
149
150 opt_payload_field_names = 0;
151 opt_context_field_names = 0;
152 strlist = (char *) poptGetOptArg(*pc);
153 if (!strlist) {
154 return -EINVAL;
155 }
156 str = strtok_r(strlist, ",", &strctx);
157 do {
158 if (!strcmp(str, "all"))
159 opt_all_field_names = 1;
160 else if (!strcmp(str, "scope"))
161 opt_scope_field_names = 1;
162 else if (!strcmp(str, "context") || !strcmp(str, "ctx"))
163 opt_context_field_names = 1;
164 else if (!strcmp(str, "header"))
165 opt_header_field_names = 1;
166 else if (!strcmp(str, "payload") || !strcmp(str, "args") || !strcmp(str, "arg"))
167 opt_payload_field_names = 1;
168 else if (!strcmp(str, "none")) {
169 opt_all_field_names = 0;
170 opt_scope_field_names = 0;
171 opt_context_field_names = 0;
172 opt_header_field_names = 0;
173 opt_payload_field_names = 0;
174 } else {
175 fprintf(stderr, "[error] unknown field name type %s\n", str);
176 return -EINVAL;
177 }
178 } while ((str = strtok_r(NULL, ",", &strctx)));
179 return 0;
180 }
181
182 static int get_fields_args(poptContext *pc)
183 {
184 char *str, *strlist, *strctx;
185
186 strlist = (char *) poptGetOptArg(*pc);
187 if (!strlist) {
188 return -EINVAL;
189 }
190 str = strtok_r(strlist, ",", &strctx);
191 do {
192 if (!strcmp(str, "all"))
193 opt_all_fields = 1;
194 else if (!strcmp(str, "trace"))
195 opt_trace_field = 1;
196 else if (!strcmp(str, "trace:domain"))
197 opt_trace_domain_field = 1;
198 else if (!strcmp(str, "trace:procname"))
199 opt_trace_procname_field = 1;
200 else if (!strcmp(str, "trace:vpid"))
201 opt_trace_vpid_field = 1;
202 else if (!strcmp(str, "loglevel"))
203 opt_loglevel_field = 1;
204 else {
205 fprintf(stderr, "[error] unknown field type %s\n", str);
206 return -EINVAL;
207 }
208 } while ((str = strtok_r(NULL, ",", &strctx)));
209 return 0;
210 }
211
212 /*
213 * Return 0 if caller should continue, < 0 if caller should return
214 * error, > 0 if caller should exit without reporting error.
215 */
216 static int parse_options(int argc, char **argv)
217 {
218 poptContext pc;
219 int opt, ret = 0;
220
221 if (argc == 1) {
222 usage(stdout);
223 return 1; /* exit cleanly */
224 }
225
226 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
227 poptReadDefaultConfig(pc, 0);
228
229 /* set default */
230 opt_context_field_names = 1;
231 opt_payload_field_names = 1;
232
233 while ((opt = poptGetNextOpt(pc)) != -1) {
234 switch (opt) {
235 case OPT_HELP:
236 usage(stdout);
237 ret = 1; /* exit cleanly */
238 goto end;
239 case OPT_LIST:
240 list_formats(stdout);
241 ret = 1;
242 goto end;
243 case OPT_VERBOSE:
244 babeltrace_verbose = 1;
245 break;
246 case OPT_NAMES:
247 if (get_names_args(&pc)) {
248 ret = -EINVAL;
249 goto end;
250 }
251 break;
252 case OPT_FIELDS:
253 if (get_fields_args(&pc)) {
254 ret = -EINVAL;
255 goto end;
256 }
257 break;
258 case OPT_DEBUG:
259 babeltrace_debug = 1;
260 break;
261 case OPT_NO_DELTA:
262 opt_delta_field = 0;
263 break;
264 case OPT_CLOCK_RAW:
265 opt_clock_raw = 1;
266 break;
267 case OPT_CLOCK_OFFSET:
268 {
269 char *str, *endptr;
270
271 str = poptGetOptArg(pc);
272 if (!str) {
273 fprintf(stderr, "[error] Missing --clock-offset argument\n");
274 ret = -EINVAL;
275 goto end;
276 }
277 errno = 0;
278 opt_clock_offset = strtoull(str, &endptr, 0);
279 if (*endptr != '\0' || str == endptr || errno != 0) {
280 fprintf(stderr, "[error] Incorrect --clock-offset argument: %s\n", str);
281 ret = -EINVAL;
282 goto end;
283 }
284 break;
285 }
286 case OPT_CLOCK_SECONDS:
287 opt_clock_seconds = 1;
288 break;
289 case OPT_CLOCK_DATE:
290 opt_clock_date = 1;
291 break;
292 case OPT_CLOCK_GMT:
293 opt_clock_gmt = 1;
294 break;
295 case OPT_CLOCK_FORCE_CORRELATE:
296 opt_clock_force_correlate = 1;
297 break;
298
299 default:
300 ret = -EINVAL;
301 goto end;
302 }
303 }
304
305 opt_input_path = poptGetArg(pc);
306 if (!opt_input_path) {
307 ret = -EINVAL;
308 goto end;
309 }
310 opt_output_path = poptGetArg(pc);
311
312 end:
313 if (pc) {
314 poptFreeContext(pc);
315 }
316 return ret;
317 }
318
319
320 /*
321 * bt_context_add_traces_recursive: Open a trace recursively
322 *
323 * Find each trace present in the subdirectory starting from the given
324 * path, and add them to the context. The packet_seek parameter can be
325 * NULL: this specify to use the default format packet_seek.
326 *
327 * Return: 0 on success, < 0 on failure, > 0 on partial failure.
328 * Unable to open toplevel: failure.
329 * Unable to open some subdirectory or file: warn and continue (partial
330 * failure);
331 */
332 int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
333 const char *format_str,
334 void (*packet_seek)(struct stream_pos *pos,
335 size_t offset, int whence))
336 {
337 FTS *tree;
338 FTSENT *node;
339 GArray *trace_ids;
340 char lpath[PATH_MAX];
341 char * const paths[2] = { lpath, NULL };
342 int ret = 0;
343
344 /*
345 * Need to copy path, because fts_open can change it.
346 * It is the pointer array, not the strings, that are constant.
347 */
348 strncpy(lpath, path, PATH_MAX);
349 lpath[PATH_MAX - 1] = '\0';
350
351 tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
352 if (tree == NULL) {
353 fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n",
354 path);
355 return -EINVAL;
356 }
357
358 trace_ids = g_array_new(FALSE, TRUE, sizeof(int));
359
360 while ((node = fts_read(tree))) {
361 int dirfd, metafd;
362 int closeret;
363
364 if (!(node->fts_info & FTS_D))
365 continue;
366
367 dirfd = open(node->fts_accpath, 0);
368 if (dirfd < 0) {
369 fprintf(stderr, "[error] [Context] Unable to open trace "
370 "directory file descriptor.\n");
371 ret = 1; /* partial error */
372 goto error;
373 }
374 metafd = openat(dirfd, "metadata", O_RDONLY);
375 if (metafd < 0) {
376 closeret = close(dirfd);
377 if (closeret < 0) {
378 perror("close");
379 ret = -1; /* failure */
380 goto error;
381 }
382 continue;
383 } else {
384 int trace_id;
385
386 closeret = close(metafd);
387 if (closeret < 0) {
388 perror("close");
389 ret = -1; /* failure */
390 goto error;
391 }
392 closeret = close(dirfd);
393 if (closeret < 0) {
394 perror("close");
395 ret = -1; /* failure */
396 goto error;
397 }
398
399 trace_id = bt_context_add_trace(ctx,
400 node->fts_accpath, format_str,
401 packet_seek, NULL, NULL);
402 if (trace_id < 0) {
403 fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s "
404 "for reading.\n", node->fts_accpath, path);
405 /* Allow to skip erroneous traces. */
406 ret = 1; /* partial error */
407 continue;
408 }
409 g_array_append_val(trace_ids, trace_id);
410 }
411 }
412
413 error:
414 /*
415 * Return an error if no trace can be opened.
416 */
417 if (trace_ids->len == 0) {
418 fprintf(stderr, "[error] Cannot open any trace for reading.\n\n");
419 ret = -ENOENT; /* failure */
420 }
421 g_array_free(trace_ids, TRUE);
422 return ret;
423 }
424
425
426
427 int convert_trace(struct trace_descriptor *td_write,
428 struct bt_context *ctx)
429 {
430 struct bt_ctf_iter *iter;
431 struct ctf_text_stream_pos *sout;
432 struct bt_iter_pos begin_pos;
433 struct bt_ctf_event *ctf_event;
434 int ret;
435
436 sout = container_of(td_write, struct ctf_text_stream_pos,
437 trace_descriptor);
438
439 begin_pos.type = BT_SEEK_BEGIN;
440 iter = bt_ctf_iter_create(ctx, &begin_pos, NULL);
441 if (!iter) {
442 ret = -1;
443 goto error_iter;
444 }
445 while ((ctf_event = bt_ctf_iter_read_event(iter))) {
446 ret = sout->parent.event_cb(&sout->parent, ctf_event->parent->stream);
447 if (ret) {
448 fprintf(stderr, "[error] Writing event failed.\n");
449 goto end;
450 }
451 ret = bt_iter_next(bt_ctf_get_iter(iter));
452 if (ret < 0)
453 goto end;
454 }
455 ret = 0;
456
457 end:
458 bt_ctf_iter_destroy(iter);
459 error_iter:
460 return ret;
461 }
462
463 int main(int argc, char **argv)
464 {
465 int ret, partial_error = 0;
466 struct format *fmt_write;
467 struct trace_descriptor *td_write;
468 struct bt_context *ctx;
469
470 ret = parse_options(argc, argv);
471 if (ret < 0) {
472 fprintf(stderr, "Error parsing options.\n\n");
473 usage(stderr);
474 exit(EXIT_FAILURE);
475 } else if (ret > 0) {
476 exit(EXIT_SUCCESS);
477 }
478 printf_verbose("Verbose mode active.\n");
479 printf_debug("Debug mode active.\n");
480
481 if (opt_input_format_arg) {
482 opt_input_format = strdup(opt_input_format_arg);
483 if (!opt_input_format) {
484 partial_error = 1;
485 goto end;
486 }
487 strlower(opt_input_format);
488 }
489 if (opt_output_format) {
490 opt_output_format = strdup(opt_output_format_arg);
491 if (!opt_output_format) {
492 partial_error = 1;
493 goto end;
494 }
495 strlower(opt_output_format);
496 }
497
498 printf_verbose("Converting from directory: %s\n", opt_input_path);
499 printf_verbose("Converting from format: %s\n",
500 opt_input_format ? : "ctf <default>");
501 printf_verbose("Converting to directory: %s\n",
502 opt_output_path ? : "<stdout>");
503 printf_verbose("Converting to format: %s\n",
504 opt_output_format ? : "text <default>");
505
506 if (!opt_input_format) {
507 opt_input_format = strdup("ctf");
508 if (!opt_input_format) {
509 partial_error = 1;
510 goto end;
511 }
512 }
513 if (!opt_output_format) {
514 opt_output_format = strdup("text");
515 if (!opt_output_format) {
516 partial_error = 1;
517 goto end;
518 }
519 }
520 fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
521 if (!fmt_read) {
522 fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n",
523 opt_input_format);
524 partial_error = 1;
525 goto end;
526 }
527 fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
528 if (!fmt_write) {
529 fprintf(stderr, "[error] format \"%s\" is not supported.\n\n",
530 opt_output_format);
531 partial_error = 1;
532 goto end;
533 }
534
535 ctx = bt_context_create();
536
537 ret = bt_context_add_traces_recursive(ctx, opt_input_path,
538 opt_input_format, NULL);
539 if (ret < 0) {
540 fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n",
541 opt_input_path);
542 goto error_td_read;
543 } else if (ret > 0) {
544 fprintf(stderr, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n",
545 opt_input_path);
546 partial_error = 1;
547 }
548
549 td_write = fmt_write->open_trace(opt_output_path, O_RDWR, NULL, NULL);
550 if (!td_write) {
551 fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n",
552 opt_output_path ? : "<none>");
553 goto error_td_write;
554 }
555
556 ret = convert_trace(td_write, ctx);
557 if (ret) {
558 fprintf(stderr, "Error printing trace.\n\n");
559 goto error_copy_trace;
560 }
561
562 fmt_write->close_trace(td_write);
563
564 bt_context_put(ctx);
565 printf_verbose("finished converting. Output written to:\n%s\n",
566 opt_output_path ? : "<stdout>");
567 goto end;
568
569 /* Error handling */
570 error_copy_trace:
571 fmt_write->close_trace(td_write);
572 error_td_write:
573 bt_context_put(ctx);
574 error_td_read:
575 partial_error = 1;
576
577 /* teardown and exit */
578 end:
579 free(opt_input_format);
580 free(opt_output_format);
581 if (partial_error)
582 exit(EXIT_FAILURE);
583 else
584 exit(EXIT_SUCCESS);
585 }
This page took 0.041193 seconds and 5 git commands to generate.