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