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