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