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