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