Fix: leak of stream intersection positions
[babeltrace.git] / converter / babeltrace.c
CommitLineData
34ac0e6c
MD
1/*
2 * babeltrace.c
3 *
4 * Babeltrace Trace Converter
5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
34ac0e6c
MD
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.
c462e188
MD
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.
34ac0e6c 27 */
4c8bfb7e 28
95d36295 29#include <babeltrace/babeltrace.h>
7fb21036 30#include <babeltrace/format.h>
95d36295 31#include <babeltrace/context.h>
7237592a 32#include <babeltrace/context-internal.h>
95d36295 33#include <babeltrace/ctf/types.h>
9843982d 34#include <babeltrace/ctf/events.h>
04ae3991
MD
35/* TODO: fix object model for format-agnostic callbacks */
36#include <babeltrace/ctf/events-internal.h>
9efd5d76 37#include <babeltrace/ctf/iterator.h>
31bdef5c 38#include <babeltrace/ctf-text/types.h>
6204d33c 39#include <babeltrace/iterator.h>
34ac0e6c
MD
40#include <popt.h>
41#include <errno.h>
42#include <stdlib.h>
bbefb8dd
MD
43#include <ctype.h>
44#include <sys/stat.h>
45#include <sys/types.h>
46#include <fcntl.h>
afb48eae 47#include <unistd.h>
70accc14 48#include <inttypes.h>
a99343cf 49#include <ftw.h>
11e1d048 50#include <string.h>
4c8bfb7e 51
a44bc4c9
MD
52#include <babeltrace/ctf-ir/metadata.h> /* for clocks */
53
24d5af5b
MD
54#define PARTIAL_ERROR_SLEEP 3 /* 3 seconds */
55
afb48eae 56#define DEFAULT_FILE_ARRAY_SIZE 1
9fe26ec7 57
34d99418
JD
58#define NET_URL_PREFIX "net://"
59#define NET4_URL_PREFIX "net4://"
60#define NET6_URL_PREFIX "net6://"
61
11e1d048 62static char *opt_input_format, *opt_output_format;
34ac0e6c 63
41075e78
MD
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 */
4e85cbfd 71static GPtrArray *opt_input_paths;
9fe26ec7 72static char *opt_output_path;
37ea109a 73int opt_stream_intersection;
34ac0e6c 74
37b99bdb 75static struct bt_format *fmt_read;
afb48eae 76
11e1d048 77static
bbefb8dd
MD
78void strlower(char *str)
79{
80 while (*str) {
34224260 81 *str = tolower((int) *str);
bbefb8dd
MD
82 str++;
83 }
84}
85
34ac0e6c
MD
86enum {
87 OPT_NONE = 0,
9fe26ec7
MD
88 OPT_OUTPUT_PATH,
89 OPT_INPUT_FORMAT,
90 OPT_OUTPUT_FORMAT,
34ac0e6c 91 OPT_HELP,
7fb21036 92 OPT_LIST,
34ac0e6c
MD
93 OPT_VERBOSE,
94 OPT_DEBUG,
d63ca2cd 95 OPT_NAMES,
359d7456 96 OPT_FIELDS,
8d8ed9af 97 OPT_NO_DELTA,
11ac6674 98 OPT_CLOCK_OFFSET,
65923160 99 OPT_CLOCK_OFFSET_NS,
03798a93 100 OPT_CLOCK_CYCLES,
11ac6674
MD
101 OPT_CLOCK_SECONDS,
102 OPT_CLOCK_DATE,
103 OPT_CLOCK_GMT,
82ace6d6 104 OPT_CLOCK_FORCE_CORRELATE,
37ea109a 105 OPT_STREAM_INTERSECTION,
34ac0e6c
MD
106};
107
9fe26ec7
MD
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
41075e78
MD
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.
9fe26ec7 115 */
34ac0e6c
MD
116static struct poptOption long_options[] = {
117 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
c790c052 118 { "output", 'w', POPT_ARG_STRING, NULL, OPT_OUTPUT_PATH, NULL, NULL },
9fe26ec7
MD
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 },
34ac0e6c 121 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
7fb21036 122 { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL },
34ac0e6c
MD
123 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
124 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
cba1661c 125 { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
359d7456 126 { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL },
8d8ed9af 127 { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL },
11ac6674 128 { "clock-offset", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL },
65923160 129 { "clock-offset-ns", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET_NS, NULL, NULL },
03798a93 130 { "clock-cycles", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_CYCLES, NULL, NULL },
11ac6674
MD
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 },
82ace6d6 134 { "clock-force-correlate", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_FORCE_CORRELATE, NULL, NULL },
37ea109a 135 { "stream-intersection", 0, POPT_ARG_NONE, NULL, OPT_STREAM_INTERSECTION, NULL, NULL },
34ac0e6c
MD
136 { NULL, 0, 0, NULL, 0, NULL, NULL },
137};
138
7fb21036
MD
139static void list_formats(FILE *fp)
140{
141 fprintf(fp, "\n");
142 bt_fprintf_format_list(fp);
143}
144
34ac0e6c 145static void usage(FILE *fp)
4c8bfb7e 146{
4c15b06b 147 fprintf(fp, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION);
4e85cbfd 148 fprintf(fp, "usage : babeltrace [OPTIONS] FILE...\n");
34ac0e6c 149 fprintf(fp, "\n");
4e85cbfd
MD
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");
34ac0e6c 153 fprintf(fp, "\n");
d2b8ea6b
MD
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");
34ac0e6c 156 fprintf(fp, "\n");
4d5678b9
MD
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");
cba1661c 160 fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n");
4d5678b9 161 fprintf(fp, " -d, --debug Debug mode\n");
cba1661c 162 fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n");
8d8ed9af 163 fprintf(fp, " --no-delta Do not print time delta between consecutive events\n");
359d7456 164 fprintf(fp, " -n, --names name1<,name2,...> Print field names:\n");
82662ad4 165 fprintf(fp, " (payload OR args OR arg)\n");
12c9c3bc
MD
166 fprintf(fp, " none, all, scope, header, (context OR ctx)\n");
167 fprintf(fp, " (default: payload,context)\n");
359d7456 168 fprintf(fp, " -f, --fields name1<,name2,...> Print additional fields:\n");
32cfb8ad 169 fprintf(fp, " all, trace, trace:hostname, trace:domain,\n");
f133896d 170 fprintf(fp, " trace:procname, trace:vpid, loglevel, emf, callsite.\n");
c3815874 171 fprintf(fp, " (default: trace:hostname,trace:procname,trace:vpid)\n");
03798a93 172 fprintf(fp, " --clock-cycles Timestamp in cycles\n");
11ac6674 173 fprintf(fp, " --clock-offset seconds Clock offset in seconds\n");
65923160 174 fprintf(fp, " --clock-offset-ns ns Clock offset in nanoseconds\n");
11ac6674
MD
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");
82ace6d6
MD
179 fprintf(fp, " --clock-force-correlate Assume that clocks are inherently correlated\n");
180 fprintf(fp, " across traces.\n");
37ea109a 181 fprintf(fp, " --stream-intersection Only print events when all streams are active.\n");
7fb21036 182 list_formats(fp);
34ac0e6c
MD
183 fprintf(fp, "\n");
184}
185
cba1661c
MD
186static int get_names_args(poptContext *pc)
187{
188 char *str, *strlist, *strctx;
9f2c779c 189 int ret = 0;
cba1661c
MD
190
191 opt_payload_field_names = 0;
12c9c3bc 192 opt_context_field_names = 0;
cba1661c
MD
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;
12c9c3bc
MD
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 {
359d7456 216 fprintf(stderr, "[error] unknown field name type %s\n", str);
9f2c779c
MD
217 ret = -EINVAL;
218 goto end;
359d7456
MD
219 }
220 } while ((str = strtok_r(NULL, ",", &strctx)));
9f2c779c
MD
221end:
222 free(strlist);
223 return ret;
359d7456
MD
224}
225
226static int get_fields_args(poptContext *pc)
227{
228 char *str, *strlist, *strctx;
9f2c779c 229 int ret = 0;
359d7456 230
359d7456
MD
231 strlist = (char *) poptGetOptArg(*pc);
232 if (!strlist) {
233 return -EINVAL;
234 }
235 str = strtok_r(strlist, ",", &strctx);
236 do {
c3815874 237 opt_trace_default_fields = 0;
359d7456
MD
238 if (!strcmp(str, "all"))
239 opt_all_fields = 1;
82662ad4 240 else if (!strcmp(str, "trace"))
359d7456 241 opt_trace_field = 1;
c3815874
MD
242 else if (!strcmp(str, "trace:hostname"))
243 opt_trace_hostname_field = 1;
8c250d87 244 else if (!strcmp(str, "trace:domain"))
359d7456 245 opt_trace_domain_field = 1;
8c250d87 246 else if (!strcmp(str, "trace:procname"))
359d7456 247 opt_trace_procname_field = 1;
8c250d87 248 else if (!strcmp(str, "trace:vpid"))
359d7456 249 opt_trace_vpid_field = 1;
d86d62f8 250 else if (!strcmp(str, "loglevel"))
359d7456 251 opt_loglevel_field = 1;
f6714e20
MD
252 else if (!strcmp(str, "emf"))
253 opt_emf_field = 1;
f133896d
MD
254 else if (!strcmp(str, "callsite"))
255 opt_callsite_field = 1;
cba1661c 256 else {
359d7456 257 fprintf(stderr, "[error] unknown field type %s\n", str);
9f2c779c
MD
258 ret = -EINVAL;
259 goto end;
cba1661c
MD
260 }
261 } while ((str = strtok_r(NULL, ",", &strctx)));
9f2c779c
MD
262end:
263 free(strlist);
264 return ret;
cba1661c
MD
265}
266
34ac0e6c
MD
267/*
268 * Return 0 if caller should continue, < 0 if caller should return
269 * error, > 0 if caller should exit without reporting error.
270 */
bbefb8dd 271static int parse_options(int argc, char **argv)
34ac0e6c
MD
272{
273 poptContext pc;
274 int opt, ret = 0;
41075e78 275 const char *ipath;
34ac0e6c 276
0f980a35
MD
277 if (argc == 1) {
278 usage(stdout);
279 return 1; /* exit cleanly */
280 }
281
bbefb8dd 282 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
34ac0e6c
MD
283 poptReadDefaultConfig(pc, 0);
284
cba1661c 285 /* set default */
e505794f 286 opt_context_field_names = 1;
cba1661c
MD
287 opt_payload_field_names = 1;
288
34ac0e6c
MD
289 while ((opt = poptGetNextOpt(pc)) != -1) {
290 switch (opt) {
9fe26ec7
MD
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;
34ac0e6c 312 case OPT_HELP:
7fb21036 313 usage(stdout);
34ac0e6c
MD
314 ret = 1; /* exit cleanly */
315 goto end;
7fb21036
MD
316 case OPT_LIST:
317 list_formats(stdout);
318 ret = 1;
319 goto end;
34ac0e6c
MD
320 case OPT_VERBOSE:
321 babeltrace_verbose = 1;
322 break;
cba1661c
MD
323 case OPT_NAMES:
324 if (get_names_args(&pc)) {
325 ret = -EINVAL;
326 goto end;
327 }
328 break;
359d7456
MD
329 case OPT_FIELDS:
330 if (get_fields_args(&pc)) {
331 ret = -EINVAL;
332 goto end;
333 }
334 break;
34ac0e6c
MD
335 case OPT_DEBUG:
336 babeltrace_debug = 1;
337 break;
8d8ed9af 338 case OPT_NO_DELTA:
359d7456 339 opt_delta_field = 0;
8d8ed9af 340 break;
03798a93
JD
341 case OPT_CLOCK_CYCLES:
342 opt_clock_cycles = 1;
11ac6674
MD
343 break;
344 case OPT_CLOCK_OFFSET:
345 {
9f2c779c 346 char *str;
34224260 347 char *endptr;
11ac6674 348
9f2c779c 349 str = (char *) poptGetOptArg(pc);
11ac6674
MD
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;
9f2c779c 360 free(str);
11ac6674
MD
361 goto end;
362 }
9f2c779c 363 free(str);
11ac6674
MD
364 break;
365 }
366 case OPT_CLOCK_SECONDS:
367 opt_clock_seconds = 1;
368 break;
65923160
IJ
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
11ac6674
MD
392 case OPT_CLOCK_DATE:
393 opt_clock_date = 1;
394 break;
395 case OPT_CLOCK_GMT:
396 opt_clock_gmt = 1;
397 break;
82ace6d6
MD
398 case OPT_CLOCK_FORCE_CORRELATE:
399 opt_clock_force_correlate = 1;
400 break;
37ea109a
JD
401 case OPT_STREAM_INTERSECTION:
402 opt_stream_intersection = 1;
403 break;
11ac6674 404
34ac0e6c
MD
405 default:
406 ret = -EINVAL;
407 goto end;
408 }
409 }
410
4e85cbfd 411 do {
41075e78 412 ipath = poptGetArg(pc);
4e85cbfd
MD
413 if (ipath)
414 g_ptr_array_add(opt_input_paths, (gpointer) ipath);
415 } while (ipath);
416 if (opt_input_paths->len == 0) {
34ac0e6c
MD
417 ret = -EINVAL;
418 goto end;
419 }
cba1661c 420
34ac0e6c
MD
421end:
422 if (pc) {
423 poptFreeContext(pc);
424 }
425 return ret;
426}
427
a99343cf 428static GPtrArray *traversed_paths = 0;
afb48eae 429
a99343cf
YB
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 */
438static 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 {
9544a40b
MD
463 int err_close = 0;
464
a99343cf
YB
465 closeret = close(metafd);
466 if (closeret < 0) {
467 perror("close");
9544a40b 468 err_close = 1;
a99343cf
YB
469 }
470 closeret = close(dirfd);
471 if (closeret < 0) {
472 perror("close");
9544a40b
MD
473 err_close = 1;
474 }
475 if (err_close) {
476 return -1;
a99343cf
YB
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}
d0d82191 489
20d24215
YB
490/*
491 * bt_context_add_traces_recursive: Open a trace recursively
492 *
493 * Find each trace present in the subdirectory starting from the given
613f532b
MD
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.
20d24215 496 *
b945d4a5 497 * Return: 0 on success, < 0 on failure, > 0 on partial failure.
20d24215 498 * Unable to open toplevel: failure.
b945d4a5
MD
499 * Unable to open some subdirectory or file: warn and continue (partial
500 * failure);
20d24215
YB
501 */
502int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
613f532b 503 const char *format_str,
1cf393f6 504 void (*packet_seek)(struct bt_stream_pos *pos,
613f532b 505 size_t offset, int whence))
20d24215 506{
fd94f11c 507 int ret = 0, trace_ids = 0;
20d24215 508
34d99418
JD
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);
34d99418
JD
517 }
518 return ret;
519 }
a99343cf 520 /* Should lock traversed_paths mutex here if used in multithread */
20d24215 521
a99343cf 522 traversed_paths = g_ptr_array_new();
a99343cf
YB
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) {
08c82b90
MD
527 int i;
528
a99343cf
YB
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);
20d24215 538 if (trace_id < 0) {
7f89ddce 539 fprintf(stderr, "[warning] [Context] cannot open trace \"%s\" from %s "
a99343cf 540 "for reading.\n", trace_path->str, path);
ca718275 541 /* Allow to skip erroneous traces. */
b945d4a5 542 ret = 1; /* partial error */
a99343cf 543 } else {
fd94f11c 544 trace_ids++;
20d24215 545 }
a99343cf 546 g_string_free(trace_path, TRUE);
20d24215
YB
547 }
548 }
d0d82191 549
a99343cf
YB
550 g_ptr_array_free(traversed_paths, TRUE);
551 traversed_paths = NULL;
552
553 /* Should unlock traversed paths mutex here if used in multithread */
20d24215 554
27cd3890
MD
555 /*
556 * Return an error if no trace can be opened.
557 */
fd94f11c 558 if (trace_ids == 0) {
27cd3890 559 fprintf(stderr, "[error] Cannot open any trace for reading.\n\n");
b945d4a5 560 ret = -ENOENT; /* failure */
27cd3890 561 }
20d24215
YB
562 return ret;
563}
a44bc4c9 564
7237592a
MD
565static
566int 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;
591end:
592 return ret;
593}
594
595static
596int 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;
621end:
622 return ret;
623}
624
37ea109a
JD
625static
626struct bt_ctf_iter *iter_create_intersect(struct bt_context *ctx,
c67280bf
JG
627 struct bt_iter_pos **inter_begin_pos,
628 struct bt_iter_pos **inter_end_pos)
37ea109a
JD
629{
630 uint64_t begin = 0, end = ULLONG_MAX;
37ea109a
JD
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 }
c67280bf 641 *inter_begin_pos = bt_iter_create_time_pos(NULL, begin);
37ea109a
JD
642 if (!inter_begin_pos) {
643 goto error;
644 }
c67280bf 645 *inter_end_pos = bt_iter_create_time_pos(NULL, end);
37ea109a
JD
646 if (!inter_end_pos) {
647 goto error;
648 }
649
c67280bf
JG
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);
37ea109a
JD
657error:
658 return NULL;
659}
660
7237592a 661static
1b8455b7 662int convert_trace(struct bt_trace_descriptor *td_write,
95d36295
JD
663 struct bt_context *ctx)
664{
e4195791 665 struct bt_ctf_iter *iter;
95d36295 666 struct ctf_text_stream_pos *sout;
c67280bf 667 struct bt_iter_pos *begin_pos = NULL, *end_pos = NULL;
c50d2a7a 668 struct bt_ctf_event *ctf_event;
95d36295
JD
669 int ret;
670
671 sout = container_of(td_write, struct ctf_text_stream_pos,
672 trace_descriptor);
673
37ea109a 674 if (!sout->parent.event_cb) {
7237592a 675 return 0;
37ea109a 676 }
7237592a 677
37ea109a 678 if (opt_stream_intersection) {
c67280bf 679 iter = iter_create_intersect(ctx, &begin_pos, &end_pos);
37ea109a 680 } else {
c67280bf
JG
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);
37ea109a 684 }
95d36295
JD
685 if (!iter) {
686 ret = -1;
687 goto error_iter;
688 }
e4195791 689 while ((ctf_event = bt_ctf_iter_read_event(iter))) {
c50d2a7a 690 ret = sout->parent.event_cb(&sout->parent, ctf_event->parent->stream);
95d36295 691 if (ret) {
3394d22e 692 fprintf(stderr, "[error] Writing event failed.\n");
95d36295
JD
693 goto end;
694 }
e4195791 695 ret = bt_iter_next(bt_ctf_get_iter(iter));
37ea109a 696 if (ret < 0) {
95d36295 697 goto end;
37ea109a 698 }
95d36295
JD
699 }
700 ret = 0;
701
702end:
e4195791 703 bt_ctf_iter_destroy(iter);
95d36295 704error_iter:
c67280bf
JG
705 bt_iter_free_pos(begin_pos);
706 bt_iter_free_pos(end_pos);
95d36295
JD
707 return ret;
708}
709
bbefb8dd 710int main(int argc, char **argv)
34ac0e6c 711{
4e85cbfd 712 int ret, partial_error = 0, open_success = 0;
37b99bdb 713 struct bt_format *fmt_write;
1b8455b7 714 struct bt_trace_descriptor *td_write;
95d36295 715 struct bt_context *ctx;
4e85cbfd
MD
716 int i;
717
718 opt_input_paths = g_ptr_array_new();
34ac0e6c
MD
719
720 ret = parse_options(argc, argv);
721 if (ret < 0) {
3394d22e
MD
722 fprintf(stderr, "Error parsing options.\n\n");
723 usage(stderr);
4e85cbfd 724 g_ptr_array_free(opt_input_paths, TRUE);
34ac0e6c
MD
725 exit(EXIT_FAILURE);
726 } else if (ret > 0) {
4e85cbfd 727 g_ptr_array_free(opt_input_paths, TRUE);
34ac0e6c
MD
728 exit(EXIT_SUCCESS);
729 }
730 printf_verbose("Verbose mode active.\n");
731 printf_debug("Debug mode active.\n");
732
9fe26ec7 733 if (opt_input_format)
bbefb8dd 734 strlower(opt_input_format);
9fe26ec7 735 if (opt_output_format)
bbefb8dd
MD
736 strlower(opt_output_format);
737
4e85cbfd
MD
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 }
34ac0e6c 743 printf_verbose("Converting from format: %s\n",
b61922b5 744 opt_input_format ? : "ctf <default>");
c3e4d5dc 745 printf_verbose("Converting to target: %s\n",
478b6389 746 opt_output_path ? : "<stdout>");
34ac0e6c 747 printf_verbose("Converting to format: %s\n",
b61922b5 748 opt_output_format ? : "text <default>");
bbefb8dd 749
11e1d048
MD
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 }
bbefb8dd 764 fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
34d99418 765 if (!fmt_read) {
3394d22e 766 fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n",
bbefb8dd 767 opt_input_format);
11e1d048
MD
768 partial_error = 1;
769 goto end;
bbefb8dd 770 }
bbefb8dd
MD
771 fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
772 if (!fmt_write) {
3394d22e 773 fprintf(stderr, "[error] format \"%s\" is not supported.\n\n",
bbefb8dd 774 opt_output_format);
11e1d048
MD
775 partial_error = 1;
776 goto end;
bbefb8dd
MD
777 }
778
6cba487f 779 ctx = bt_context_create();
d63c2d51
JD
780 if (!ctx) {
781 goto error_td_read;
782 }
6cba487f 783
4e85cbfd
MD
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");
bbefb8dd
MD
802 goto error_td_read;
803 }
6cba487f 804
5b80ddfb 805 td_write = fmt_write->open_trace(opt_output_path, O_RDWR, NULL, NULL);
bbefb8dd 806 if (!td_write) {
3394d22e 807 fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n",
b61922b5 808 opt_output_path ? : "<none>");
bbefb8dd
MD
809 goto error_td_write;
810 }
847bf71a 811
24d5af5b
MD
812 /*
813 * Errors happened when opening traces, but we continue anyway.
814 * sleep to let user see the stderr output before stdout.
815 */
4e85cbfd 816 if (partial_error)
24d5af5b 817 sleep(PARTIAL_ERROR_SLEEP);
24d5af5b 818
7237592a
MD
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
34d99418
JD
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 }
46322b33 832 }
847bf71a 833
7237592a
MD
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
bbefb8dd 840 fmt_write->close_trace(td_write);
6cba487f
MD
841
842 bt_context_put(ctx);
afb48eae
AA
843 printf_verbose("finished converting. Output written to:\n%s\n",
844 opt_output_path ? : "<stdout>");
11e1d048 845 goto end;
34ac0e6c 846
bbefb8dd 847 /* Error handling */
46322b33 848error_copy_trace:
bbefb8dd
MD
849 fmt_write->close_trace(td_write);
850error_td_write:
6cba487f 851 bt_context_put(ctx);
bbefb8dd 852error_td_read:
11e1d048
MD
853 partial_error = 1;
854
855 /* teardown and exit */
856end:
857 free(opt_input_format);
858 free(opt_output_format);
9fe26ec7 859 free(opt_output_path);
4e85cbfd 860 g_ptr_array_free(opt_input_paths, TRUE);
11e1d048
MD
861 if (partial_error)
862 exit(EXIT_FAILURE);
863 else
864 exit(EXIT_SUCCESS);
4c8bfb7e 865}
This page took 0.074334 seconds and 4 git commands to generate.