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