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