Remove unused array in bt_context_add_traces_recursive
[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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #define _GNU_SOURCE
30 #include <config.h>
31 #include <babeltrace/babeltrace.h>
32 #include <babeltrace/format.h>
33 #include <babeltrace/context.h>
34 #include <babeltrace/context-internal.h>
35 #include <babeltrace/ctf/types.h>
36 #include <babeltrace/ctf/events.h>
37 /* TODO: fix object model for format-agnostic callbacks */
38 #include <babeltrace/ctf/events-internal.h>
39 #include <babeltrace/ctf/iterator.h>
40 #include <babeltrace/ctf-text/types.h>
41 #include <babeltrace/iterator.h>
42 #include <popt.h>
43 #include <errno.h>
44 #include <stdlib.h>
45 #include <ctype.h>
46 #include <sys/stat.h>
47 #include <sys/types.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50 #include <inttypes.h>
51 #include <ftw.h>
52 #include <string.h>
53
54 #include <babeltrace/ctf-ir/metadata.h> /* for clocks */
55
56 #define PARTIAL_ERROR_SLEEP 3 /* 3 seconds */
57
58 #define DEFAULT_FILE_ARRAY_SIZE 1
59
60 static char *opt_input_format, *opt_output_format;
61
62 /*
63 * We are not freeing opt_input_paths ipath elements when exiting from
64 * main() for backward compatibility with libpop 0.13, which does not
65 * allocate copies for arguments returned by poptGetArg(), and for
66 * general compatibility with the documented behavior. This is known to
67 * cause a small memory leak with libpop 0.16.
68 */
69 static GPtrArray *opt_input_paths;
70 static char *opt_output_path;
71
72 static struct bt_format *fmt_read;
73
74 static
75 void strlower(char *str)
76 {
77 while (*str) {
78 *str = tolower((int) *str);
79 str++;
80 }
81 }
82
83 enum {
84 OPT_NONE = 0,
85 OPT_OUTPUT_PATH,
86 OPT_INPUT_FORMAT,
87 OPT_OUTPUT_FORMAT,
88 OPT_HELP,
89 OPT_LIST,
90 OPT_VERBOSE,
91 OPT_DEBUG,
92 OPT_NAMES,
93 OPT_FIELDS,
94 OPT_NO_DELTA,
95 OPT_CLOCK_OFFSET,
96 OPT_CLOCK_OFFSET_NS,
97 OPT_CLOCK_CYCLES,
98 OPT_CLOCK_SECONDS,
99 OPT_CLOCK_DATE,
100 OPT_CLOCK_GMT,
101 OPT_CLOCK_FORCE_CORRELATE,
102 };
103
104 /*
105 * We are _not_ using POPT_ARG_STRING ability to store directly into
106 * variables, because we want to cast the return to non-const, which is
107 * not possible without using poptGetOptArg explicitly. This helps us
108 * controlling memory allocation correctly without making assumptions
109 * about undocumented behaviors. poptGetOptArg is documented as
110 * requiring the returned const char * to be freed by the caller.
111 */
112 static struct poptOption long_options[] = {
113 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
114 { "output", 'w', POPT_ARG_STRING, NULL, OPT_OUTPUT_PATH, NULL, NULL },
115 { "input-format", 'i', POPT_ARG_STRING, NULL, OPT_INPUT_FORMAT, NULL, NULL },
116 { "output-format", 'o', POPT_ARG_STRING, NULL, OPT_OUTPUT_FORMAT, NULL, NULL },
117 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
118 { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL },
119 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
120 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
121 { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
122 { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL },
123 { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL },
124 { "clock-offset", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL },
125 { "clock-offset-ns", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET_NS, NULL, NULL },
126 { "clock-cycles", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_CYCLES, NULL, NULL },
127 { "clock-seconds", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_SECONDS, NULL, NULL },
128 { "clock-date", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_DATE, NULL, NULL },
129 { "clock-gmt", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_GMT, NULL, NULL },
130 { "clock-force-correlate", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_FORCE_CORRELATE, NULL, NULL },
131 { NULL, 0, 0, NULL, 0, NULL, NULL },
132 };
133
134 static void list_formats(FILE *fp)
135 {
136 fprintf(fp, "\n");
137 bt_fprintf_format_list(fp);
138 }
139
140 static void usage(FILE *fp)
141 {
142 fprintf(fp, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION);
143 fprintf(fp, "usage : babeltrace [OPTIONS] FILE...\n");
144 fprintf(fp, "\n");
145 fprintf(fp, " FILE Input trace file(s) and/or directory(ies)\n");
146 fprintf(fp, " (space-separated)\n");
147 fprintf(fp, " -w, --output OUTPUT Output trace path (default: stdout)\n");
148 fprintf(fp, "\n");
149 fprintf(fp, " -i, --input-format FORMAT Input trace format (default: ctf)\n");
150 fprintf(fp, " -o, --output-format FORMAT Output trace format (default: text)\n");
151 fprintf(fp, "\n");
152 fprintf(fp, " -h, --help This help message\n");
153 fprintf(fp, " -l, --list List available formats\n");
154 fprintf(fp, " -v, --verbose Verbose mode\n");
155 fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n");
156 fprintf(fp, " -d, --debug Debug mode\n");
157 fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n");
158 fprintf(fp, " --no-delta Do not print time delta between consecutive events\n");
159 fprintf(fp, " -n, --names name1<,name2,...> Print field names:\n");
160 fprintf(fp, " (payload OR args OR arg)\n");
161 fprintf(fp, " none, all, scope, header, (context OR ctx)\n");
162 fprintf(fp, " (default: payload,context)\n");
163 fprintf(fp, " -f, --fields name1<,name2,...> Print additional fields:\n");
164 fprintf(fp, " all, trace, trace:hostname, trace:domain,\n");
165 fprintf(fp, " trace:procname, trace:vpid, loglevel, emf, callsite.\n");
166 fprintf(fp, " (default: trace:hostname,trace:procname,trace:vpid)\n");
167 fprintf(fp, " --clock-cycles Timestamp in cycles\n");
168 fprintf(fp, " --clock-offset seconds Clock offset in seconds\n");
169 fprintf(fp, " --clock-offset-ns ns Clock offset in nanoseconds\n");
170 fprintf(fp, " --clock-seconds Print the timestamps as [sec.ns]\n");
171 fprintf(fp, " (default is: [hh:mm:ss.ns])\n");
172 fprintf(fp, " --clock-date Print clock date\n");
173 fprintf(fp, " --clock-gmt Print clock in GMT time zone (default: local time zone)\n");
174 fprintf(fp, " --clock-force-correlate Assume that clocks are inherently correlated\n");
175 fprintf(fp, " across traces.\n");
176 list_formats(fp);
177 fprintf(fp, "\n");
178 }
179
180 static int get_names_args(poptContext *pc)
181 {
182 char *str, *strlist, *strctx;
183 int ret = 0;
184
185 opt_payload_field_names = 0;
186 opt_context_field_names = 0;
187 strlist = (char *) poptGetOptArg(*pc);
188 if (!strlist) {
189 return -EINVAL;
190 }
191 str = strtok_r(strlist, ",", &strctx);
192 do {
193 if (!strcmp(str, "all"))
194 opt_all_field_names = 1;
195 else if (!strcmp(str, "scope"))
196 opt_scope_field_names = 1;
197 else if (!strcmp(str, "context") || !strcmp(str, "ctx"))
198 opt_context_field_names = 1;
199 else if (!strcmp(str, "header"))
200 opt_header_field_names = 1;
201 else if (!strcmp(str, "payload") || !strcmp(str, "args") || !strcmp(str, "arg"))
202 opt_payload_field_names = 1;
203 else if (!strcmp(str, "none")) {
204 opt_all_field_names = 0;
205 opt_scope_field_names = 0;
206 opt_context_field_names = 0;
207 opt_header_field_names = 0;
208 opt_payload_field_names = 0;
209 } else {
210 fprintf(stderr, "[error] unknown field name type %s\n", str);
211 ret = -EINVAL;
212 goto end;
213 }
214 } while ((str = strtok_r(NULL, ",", &strctx)));
215 end:
216 free(strlist);
217 return ret;
218 }
219
220 static int get_fields_args(poptContext *pc)
221 {
222 char *str, *strlist, *strctx;
223 int ret = 0;
224
225 strlist = (char *) poptGetOptArg(*pc);
226 if (!strlist) {
227 return -EINVAL;
228 }
229 str = strtok_r(strlist, ",", &strctx);
230 do {
231 opt_trace_default_fields = 0;
232 if (!strcmp(str, "all"))
233 opt_all_fields = 1;
234 else if (!strcmp(str, "trace"))
235 opt_trace_field = 1;
236 else if (!strcmp(str, "trace:hostname"))
237 opt_trace_hostname_field = 1;
238 else if (!strcmp(str, "trace:domain"))
239 opt_trace_domain_field = 1;
240 else if (!strcmp(str, "trace:procname"))
241 opt_trace_procname_field = 1;
242 else if (!strcmp(str, "trace:vpid"))
243 opt_trace_vpid_field = 1;
244 else if (!strcmp(str, "loglevel"))
245 opt_loglevel_field = 1;
246 else if (!strcmp(str, "emf"))
247 opt_emf_field = 1;
248 else if (!strcmp(str, "callsite"))
249 opt_callsite_field = 1;
250 else {
251 fprintf(stderr, "[error] unknown field type %s\n", str);
252 ret = -EINVAL;
253 goto end;
254 }
255 } while ((str = strtok_r(NULL, ",", &strctx)));
256 end:
257 free(strlist);
258 return ret;
259 }
260
261 /*
262 * Return 0 if caller should continue, < 0 if caller should return
263 * error, > 0 if caller should exit without reporting error.
264 */
265 static int parse_options(int argc, char **argv)
266 {
267 poptContext pc;
268 int opt, ret = 0;
269 const char *ipath;
270
271 if (argc == 1) {
272 usage(stdout);
273 return 1; /* exit cleanly */
274 }
275
276 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
277 poptReadDefaultConfig(pc, 0);
278
279 /* set default */
280 opt_context_field_names = 1;
281 opt_payload_field_names = 1;
282
283 while ((opt = poptGetNextOpt(pc)) != -1) {
284 switch (opt) {
285 case OPT_OUTPUT_PATH:
286 opt_output_path = (char *) poptGetOptArg(pc);
287 if (!opt_output_path) {
288 ret = -EINVAL;
289 goto end;
290 }
291 break;
292 case OPT_INPUT_FORMAT:
293 opt_input_format = (char *) poptGetOptArg(pc);
294 if (!opt_input_format) {
295 ret = -EINVAL;
296 goto end;
297 }
298 break;
299 case OPT_OUTPUT_FORMAT:
300 opt_output_format = (char *) poptGetOptArg(pc);
301 if (!opt_output_format) {
302 ret = -EINVAL;
303 goto end;
304 }
305 break;
306 case OPT_HELP:
307 usage(stdout);
308 ret = 1; /* exit cleanly */
309 goto end;
310 case OPT_LIST:
311 list_formats(stdout);
312 ret = 1;
313 goto end;
314 case OPT_VERBOSE:
315 babeltrace_verbose = 1;
316 break;
317 case OPT_NAMES:
318 if (get_names_args(&pc)) {
319 ret = -EINVAL;
320 goto end;
321 }
322 break;
323 case OPT_FIELDS:
324 if (get_fields_args(&pc)) {
325 ret = -EINVAL;
326 goto end;
327 }
328 break;
329 case OPT_DEBUG:
330 babeltrace_debug = 1;
331 break;
332 case OPT_NO_DELTA:
333 opt_delta_field = 0;
334 break;
335 case OPT_CLOCK_CYCLES:
336 opt_clock_cycles = 1;
337 break;
338 case OPT_CLOCK_OFFSET:
339 {
340 char *str;
341 char *endptr;
342
343 str = (char *) poptGetOptArg(pc);
344 if (!str) {
345 fprintf(stderr, "[error] Missing --clock-offset argument\n");
346 ret = -EINVAL;
347 goto end;
348 }
349 errno = 0;
350 opt_clock_offset = strtoull(str, &endptr, 0);
351 if (*endptr != '\0' || str == endptr || errno != 0) {
352 fprintf(stderr, "[error] Incorrect --clock-offset argument: %s\n", str);
353 ret = -EINVAL;
354 free(str);
355 goto end;
356 }
357 free(str);
358 break;
359 }
360 case OPT_CLOCK_SECONDS:
361 opt_clock_seconds = 1;
362 break;
363 case OPT_CLOCK_OFFSET_NS:
364 {
365 char *str;
366 char *endptr;
367
368 str = (char *) poptGetOptArg(pc);
369 if (!str) {
370 fprintf(stderr, "[error] Missing --clock-offset-ns argument\n");
371 ret = -EINVAL;
372 goto end;
373 }
374 errno = 0;
375 opt_clock_offset_ns = strtoull(str, &endptr, 0);
376 if (*endptr != '\0' || str == endptr || errno != 0) {
377 fprintf(stderr, "[error] Incorrect --clock-offset-ns argument: %s\n", str);
378 ret = -EINVAL;
379 free(str);
380 goto end;
381 }
382 free(str);
383 break;
384 }
385
386 case OPT_CLOCK_DATE:
387 opt_clock_date = 1;
388 break;
389 case OPT_CLOCK_GMT:
390 opt_clock_gmt = 1;
391 break;
392 case OPT_CLOCK_FORCE_CORRELATE:
393 opt_clock_force_correlate = 1;
394 break;
395
396 default:
397 ret = -EINVAL;
398 goto end;
399 }
400 }
401
402 do {
403 ipath = poptGetArg(pc);
404 if (ipath)
405 g_ptr_array_add(opt_input_paths, (gpointer) ipath);
406 } while (ipath);
407 if (opt_input_paths->len == 0) {
408 ret = -EINVAL;
409 goto end;
410 }
411
412 end:
413 if (pc) {
414 poptFreeContext(pc);
415 }
416 return ret;
417 }
418
419 static GPtrArray *traversed_paths = 0;
420
421 /*
422 * traverse_trace_dir() is the callback function for File Tree Walk (nftw).
423 * it receives the path of the current entry (file, dir, link..etc) with
424 * a flag to indicate the type of the entry.
425 * if the entry being visited is a directory and contains a metadata file,
426 * then add the path to a global list to be processed later in
427 * add_traces_recursive.
428 */
429 static int traverse_trace_dir(const char *fpath, const struct stat *sb,
430 int tflag, struct FTW *ftwbuf)
431 {
432 int dirfd, metafd;
433 int closeret;
434
435 if (tflag != FTW_D)
436 return 0;
437
438 dirfd = open(fpath, 0);
439 if (dirfd < 0) {
440 fprintf(stderr, "[error] [Context] Unable to open trace "
441 "directory file descriptor.\n");
442 return 0; /* partial error */
443 }
444 metafd = openat(dirfd, "metadata", O_RDONLY);
445 if (metafd < 0) {
446 closeret = close(dirfd);
447 if (closeret < 0) {
448 perror("close");
449 return -1;
450 }
451 /* No meta data, just return */
452 return 0;
453 } else {
454 closeret = close(metafd);
455 if (closeret < 0) {
456 perror("close");
457 return -1; /* failure */
458 }
459 closeret = close(dirfd);
460 if (closeret < 0) {
461 perror("close");
462 return -1; /* failure */
463 }
464
465 /* Add path to the global list */
466 if (traversed_paths == NULL) {
467 fprintf(stderr, "[error] [Context] Invalid open path array.\n");
468 return -1;
469 }
470 g_ptr_array_add(traversed_paths, g_string_new(fpath));
471 }
472
473 return 0;
474 }
475
476 /*
477 * bt_context_add_traces_recursive: Open a trace recursively
478 *
479 * Find each trace present in the subdirectory starting from the given
480 * path, and add them to the context. The packet_seek parameter can be
481 * NULL: this specify to use the default format packet_seek.
482 *
483 * Return: 0 on success, < 0 on failure, > 0 on partial failure.
484 * Unable to open toplevel: failure.
485 * Unable to open some subdirectory or file: warn and continue (partial
486 * failure);
487 */
488 int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
489 const char *format_str,
490 void (*packet_seek)(struct bt_stream_pos *pos,
491 size_t offset, int whence))
492 {
493 int ret = 0, trace_ids = 0;
494
495 /* Should lock traversed_paths mutex here if used in multithread */
496
497 traversed_paths = g_ptr_array_new();
498 ret = nftw(path, traverse_trace_dir, 10, 0);
499
500 /* Process the array if ntfw did not return a fatal error */
501 if (ret >= 0) {
502 int i;
503
504 for (i = 0; i < traversed_paths->len; i++) {
505 GString *trace_path = g_ptr_array_index(traversed_paths,
506 i);
507 int trace_id = bt_context_add_trace(ctx,
508 trace_path->str,
509 format_str,
510 packet_seek,
511 NULL,
512 NULL);
513 if (trace_id < 0) {
514 fprintf(stderr, "[warning] [Context] cannot open trace \"%s\" from %s "
515 "for reading.\n", trace_path->str, path);
516 /* Allow to skip erroneous traces. */
517 ret = 1; /* partial error */
518 } else {
519 trace_ids++;
520 }
521 g_string_free(trace_path, TRUE);
522 }
523 }
524
525 g_ptr_array_free(traversed_paths, TRUE);
526 traversed_paths = NULL;
527
528 /* Should unlock traversed paths mutex here if used in multithread */
529
530 /*
531 * Return an error if no trace can be opened.
532 */
533 if (trace_ids == 0) {
534 fprintf(stderr, "[error] Cannot open any trace for reading.\n\n");
535 ret = -ENOENT; /* failure */
536 }
537 return ret;
538 }
539
540 static
541 int trace_pre_handler(struct bt_trace_descriptor *td_write,
542 struct bt_context *ctx)
543 {
544 struct ctf_text_stream_pos *sout;
545 struct trace_collection *tc;
546 int ret, i;
547
548 sout = container_of(td_write, struct ctf_text_stream_pos,
549 trace_descriptor);
550
551 if (!sout->parent.pre_trace_cb)
552 return 0;
553
554 tc = ctx->tc;
555 for (i = 0; i < tc->array->len; i++) {
556 struct bt_trace_descriptor *td =
557 g_ptr_array_index(tc->array, i);
558
559 ret = sout->parent.pre_trace_cb(&sout->parent, td);
560 if (ret) {
561 fprintf(stderr, "[error] Writing to trace pre handler failed.\n");
562 goto end;
563 }
564 }
565 ret = 0;
566 end:
567 return ret;
568 }
569
570 static
571 int trace_post_handler(struct bt_trace_descriptor *td_write,
572 struct bt_context *ctx)
573 {
574 struct ctf_text_stream_pos *sout;
575 struct trace_collection *tc;
576 int ret, i;
577
578 sout = container_of(td_write, struct ctf_text_stream_pos,
579 trace_descriptor);
580
581 if (!sout->parent.post_trace_cb)
582 return 0;
583
584 tc = ctx->tc;
585 for (i = 0; i < tc->array->len; i++) {
586 struct bt_trace_descriptor *td =
587 g_ptr_array_index(tc->array, i);
588
589 ret = sout->parent.post_trace_cb(&sout->parent, td);
590 if (ret) {
591 fprintf(stderr, "[error] Writing to trace post handler failed.\n");
592 goto end;
593 }
594 }
595 ret = 0;
596 end:
597 return ret;
598 }
599
600 static
601 int convert_trace(struct bt_trace_descriptor *td_write,
602 struct bt_context *ctx)
603 {
604 struct bt_ctf_iter *iter;
605 struct ctf_text_stream_pos *sout;
606 struct bt_iter_pos begin_pos;
607 struct bt_ctf_event *ctf_event;
608 int ret;
609
610 sout = container_of(td_write, struct ctf_text_stream_pos,
611 trace_descriptor);
612
613 if (!sout->parent.event_cb)
614 return 0;
615
616 begin_pos.type = BT_SEEK_BEGIN;
617 iter = bt_ctf_iter_create(ctx, &begin_pos, NULL);
618 if (!iter) {
619 ret = -1;
620 goto error_iter;
621 }
622 while ((ctf_event = bt_ctf_iter_read_event(iter))) {
623 ret = sout->parent.event_cb(&sout->parent, ctf_event->parent->stream);
624 if (ret) {
625 fprintf(stderr, "[error] Writing event failed.\n");
626 goto end;
627 }
628 ret = bt_iter_next(bt_ctf_get_iter(iter));
629 if (ret < 0)
630 goto end;
631 }
632 ret = 0;
633
634 end:
635 bt_ctf_iter_destroy(iter);
636 error_iter:
637 return ret;
638 }
639
640 int main(int argc, char **argv)
641 {
642 int ret, partial_error = 0, open_success = 0;
643 struct bt_format *fmt_write;
644 struct bt_trace_descriptor *td_write;
645 struct bt_context *ctx;
646 int i;
647
648 opt_input_paths = g_ptr_array_new();
649
650 ret = parse_options(argc, argv);
651 if (ret < 0) {
652 fprintf(stderr, "Error parsing options.\n\n");
653 usage(stderr);
654 g_ptr_array_free(opt_input_paths, TRUE);
655 exit(EXIT_FAILURE);
656 } else if (ret > 0) {
657 g_ptr_array_free(opt_input_paths, TRUE);
658 exit(EXIT_SUCCESS);
659 }
660 printf_verbose("Verbose mode active.\n");
661 printf_debug("Debug mode active.\n");
662
663 if (opt_input_format)
664 strlower(opt_input_format);
665 if (opt_output_format)
666 strlower(opt_output_format);
667
668 printf_verbose("Converting from directory(ies):\n");
669 for (i = 0; i < opt_input_paths->len; i++) {
670 const char *ipath = g_ptr_array_index(opt_input_paths, i);
671 printf_verbose(" %s\n", ipath);
672 }
673 printf_verbose("Converting from format: %s\n",
674 opt_input_format ? : "ctf <default>");
675 printf_verbose("Converting to target: %s\n",
676 opt_output_path ? : "<stdout>");
677 printf_verbose("Converting to format: %s\n",
678 opt_output_format ? : "text <default>");
679
680 if (!opt_input_format) {
681 opt_input_format = strdup("ctf");
682 if (!opt_input_format) {
683 partial_error = 1;
684 goto end;
685 }
686 }
687 if (!opt_output_format) {
688 opt_output_format = strdup("text");
689 if (!opt_output_format) {
690 partial_error = 1;
691 goto end;
692 }
693 }
694 fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
695 if (!fmt_read || fmt_read->name != g_quark_from_static_string("ctf")) {
696 fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n",
697 opt_input_format);
698 partial_error = 1;
699 goto end;
700 }
701 fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
702 if (!fmt_write) {
703 fprintf(stderr, "[error] format \"%s\" is not supported.\n\n",
704 opt_output_format);
705 partial_error = 1;
706 goto end;
707 }
708
709 ctx = bt_context_create();
710 if (!ctx) {
711 goto error_td_read;
712 }
713
714 for (i = 0; i < opt_input_paths->len; i++) {
715 const char *ipath = g_ptr_array_index(opt_input_paths, i);
716 ret = bt_context_add_traces_recursive(ctx, ipath,
717 opt_input_format, NULL);
718 if (ret < 0) {
719 fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n",
720 ipath);
721 } else if (ret > 0) {
722 fprintf(stderr, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n",
723 ipath);
724 open_success = 1; /* some traces were OK */
725 partial_error = 1;
726 } else {
727 open_success = 1; /* all traces were OK */
728 }
729 }
730 if (!open_success) {
731 fprintf(stderr, "[error] none of the specified trace paths could be opened.\n\n");
732 goto error_td_read;
733 }
734
735 td_write = fmt_write->open_trace(opt_output_path, O_RDWR, NULL, NULL);
736 if (!td_write) {
737 fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n",
738 opt_output_path ? : "<none>");
739 goto error_td_write;
740 }
741
742 /*
743 * Errors happened when opening traces, but we continue anyway.
744 * sleep to let user see the stderr output before stdout.
745 */
746 if (partial_error)
747 sleep(PARTIAL_ERROR_SLEEP);
748
749 ret = trace_pre_handler(td_write, ctx);
750 if (ret) {
751 fprintf(stderr, "Error in trace pre handle.\n\n");
752 goto error_copy_trace;
753 }
754
755 ret = convert_trace(td_write, ctx);
756 if (ret) {
757 fprintf(stderr, "Error printing trace.\n\n");
758 goto error_copy_trace;
759 }
760
761 ret = trace_post_handler(td_write, ctx);
762 if (ret) {
763 fprintf(stderr, "Error in trace post handle.\n\n");
764 goto error_copy_trace;
765 }
766
767 fmt_write->close_trace(td_write);
768
769 bt_context_put(ctx);
770 printf_verbose("finished converting. Output written to:\n%s\n",
771 opt_output_path ? : "<stdout>");
772 goto end;
773
774 /* Error handling */
775 error_copy_trace:
776 fmt_write->close_trace(td_write);
777 error_td_write:
778 bt_context_put(ctx);
779 error_td_read:
780 partial_error = 1;
781
782 /* teardown and exit */
783 end:
784 free(opt_input_format);
785 free(opt_output_format);
786 free(opt_output_path);
787 g_ptr_array_free(opt_input_paths, TRUE);
788 if (partial_error)
789 exit(EXIT_FAILURE);
790 else
791 exit(EXIT_SUCCESS);
792 }
This page took 0.046437 seconds and 5 git commands to generate.