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