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