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