Plugin symbol resolving fix
[babeltrace.git] / converter / babeltrace.c
1 /*
2 * babeltrace.c
3 *
4 * Babeltrace Trace Converter
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
27 */
28
29 #include <babeltrace/babeltrace.h>
30 #include <babeltrace/format.h>
31 #include <babeltrace/context.h>
32 #include <babeltrace/context-internal.h>
33 #include <babeltrace/ctf/types.h>
34 #include <babeltrace/ctf/events.h>
35 /* TODO: fix object model for format-agnostic callbacks */
36 #include <babeltrace/ctf/events-internal.h>
37 #include <babeltrace/ctf/iterator.h>
38 #include <babeltrace/ctf-text/types.h>
39 #include <babeltrace/debug-info.h>
40
41 #include <babeltrace/iterator.h>
42 #include <babeltrace/plugin/component-factory.h>
43 #include <babeltrace/ref.h>
44 #include <babeltrace/values.h>
45 #include <popt.h>
46 #include <errno.h>
47 #include <stdlib.h>
48 #include <ctype.h>
49 #include <sys/stat.h>
50 #include <sys/types.h>
51 #include <fcntl.h>
52 #include <unistd.h>
53 #include <inttypes.h>
54 #include <ftw.h>
55 #include <string.h>
56
57 #include <babeltrace/ctf-ir/metadata.h> /* for clocks */
58
59 #define PARTIAL_ERROR_SLEEP 3 /* 3 seconds */
60
61 #define DEFAULT_FILE_ARRAY_SIZE 1
62
63 #define NET_URL_PREFIX "net://"
64 #define NET4_URL_PREFIX "net4://"
65 #define NET6_URL_PREFIX "net6://"
66
67 static char *opt_input_format, *opt_output_format;
68
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 */
76 static GPtrArray *opt_input_paths;
77 static char *opt_output_path;
78 static int opt_stream_intersection;
79 static char *opt_plugin_path;
80
81 static struct bt_format *fmt_read;
82
83 void bt_dummy_hook(void);
84 void bt_lttng_live_hook(void);
85 void bt_ctf_hook(void);
86 void bt_ctf_text_hook(void);
87 void bt_ctf_metadata_hook(void);
88
89 static
90 void strlower(char *str)
91 {
92 while (*str) {
93 *str = tolower((int) *str);
94 str++;
95 }
96 }
97
98 enum {
99 OPT_NONE = 0,
100 OPT_OUTPUT_PATH,
101 OPT_INPUT_FORMAT,
102 OPT_OUTPUT_FORMAT,
103 OPT_HELP,
104 OPT_LIST,
105 OPT_PLUGIN_PATH,
106 OPT_VERBOSE,
107 OPT_DEBUG,
108 OPT_NAMES,
109 OPT_FIELDS,
110 OPT_NO_DELTA,
111 OPT_CLOCK_OFFSET,
112 OPT_CLOCK_OFFSET_NS,
113 OPT_CLOCK_CYCLES,
114 OPT_CLOCK_SECONDS,
115 OPT_CLOCK_DATE,
116 OPT_CLOCK_GMT,
117 OPT_CLOCK_FORCE_CORRELATE,
118 OPT_STREAM_INTERSECTION,
119 OPT_DEBUG_INFO_DIR,
120 OPT_DEBUG_INFO_FULL_PATH,
121 OPT_DEBUG_INFO_TARGET_PREFIX,
122 };
123
124 /*
125 * We are _not_ using POPT_ARG_STRING's ability to store directly into
126 * variables, because we want to cast the return to non-const, which is
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.
131 */
132 static struct poptOption long_options[] = {
133 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
134 { "output", 'w', POPT_ARG_STRING, NULL, OPT_OUTPUT_PATH, NULL, NULL },
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 },
137 { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
138 { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL },
139 { "plugin-path", 0, POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
140 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
141 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
142 { "names", 'n', POPT_ARG_STRING, NULL, OPT_NAMES, NULL, NULL },
143 { "fields", 'f', POPT_ARG_STRING, NULL, OPT_FIELDS, NULL, NULL },
144 { "no-delta", 0, POPT_ARG_NONE, NULL, OPT_NO_DELTA, NULL, NULL },
145 { "clock-offset", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET, NULL, NULL },
146 { "clock-offset-ns", 0, POPT_ARG_STRING, NULL, OPT_CLOCK_OFFSET_NS, NULL, NULL },
147 { "clock-cycles", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_CYCLES, NULL, NULL },
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 },
151 { "clock-force-correlate", 0, POPT_ARG_NONE, NULL, OPT_CLOCK_FORCE_CORRELATE, NULL, NULL },
152 { "stream-intersection", 0, POPT_ARG_NONE, NULL, OPT_STREAM_INTERSECTION, NULL, NULL },
153 #ifdef ENABLE_DEBUG_INFO
154 { "debug-info-dir", 0, POPT_ARG_STRING, NULL, OPT_DEBUG_INFO_DIR, NULL, NULL },
155 { "debug-info-full-path", 0, POPT_ARG_NONE, NULL, OPT_DEBUG_INFO_FULL_PATH, NULL, NULL },
156 { "debug-info-target-prefix", 0, POPT_ARG_STRING, NULL, OPT_DEBUG_INFO_TARGET_PREFIX, NULL, NULL },
157 #endif
158 { NULL, 0, 0, NULL, 0, NULL, NULL },
159 };
160
161 static void list_formats(FILE *fp)
162 {
163 fprintf(fp, "\n");
164 bt_fprintf_format_list(fp);
165 }
166
167 static void usage(FILE *fp)
168 {
169 fprintf(fp, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION);
170 fprintf(fp, "usage : babeltrace [OPTIONS] FILE...\n");
171 fprintf(fp, "\n");
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");
175 fprintf(fp, "\n");
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");
178 fprintf(fp, "\n");
179 fprintf(fp, " -h, --help This help message\n");
180 fprintf(fp, " -l, --list List available formats\n");
181 fprintf(fp, " --plugin-path Supplementary plug-in path\n");
182 fprintf(fp, " -v, --verbose Verbose mode\n");
183 fprintf(fp, " (or set BABELTRACE_VERBOSE environment variable)\n");
184 fprintf(fp, " -d, --debug Debug mode\n");
185 fprintf(fp, " (or set BABELTRACE_DEBUG environment variable)\n");
186 fprintf(fp, " --no-delta Do not print time delta between consecutive events\n");
187 fprintf(fp, " -n, --names name1<,name2,...> Print field names:\n");
188 fprintf(fp, " (payload OR args OR arg)\n");
189 fprintf(fp, " none, all, scope, header, (context OR ctx)\n");
190 fprintf(fp, " (default: payload,context)\n");
191 fprintf(fp, " -f, --fields name1<,name2,...> Print additional fields:\n");
192 fprintf(fp, " all, trace, trace:hostname, trace:domain,\n");
193 fprintf(fp, " trace:procname, trace:vpid, loglevel, emf, callsite.\n");
194 fprintf(fp, " (default: trace:hostname,trace:procname,trace:vpid)\n");
195 fprintf(fp, " --clock-cycles Timestamp in cycles\n");
196 fprintf(fp, " --clock-offset seconds Clock offset in seconds\n");
197 fprintf(fp, " --clock-offset-ns ns Clock offset in nanoseconds\n");
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");
202 fprintf(fp, " --clock-force-correlate Assume that clocks are inherently correlated\n");
203 fprintf(fp, " across traces.\n");
204 fprintf(fp, " --stream-intersection Only print events when all streams are active.\n");
205 #ifdef ENABLE_DEBUG_INFO
206 fprintf(fp, " --debug-info-dir Directory in which to look for debugging information\n");
207 fprintf(fp, " files. (default: /usr/lib/debug/)\n");
208 fprintf(fp, " --debug-info-target-prefix Directory to use as a prefix for executable lookup\n");
209 fprintf(fp, " --debug-info-full-path Show full debug info source and binary paths (if available)\n");
210 #endif
211 list_formats(fp);
212 fprintf(fp, "\n");
213 }
214
215 static int get_names_args(poptContext *pc)
216 {
217 char *str, *strlist, *strctx;
218 int ret = 0;
219
220 opt_payload_field_names = 0;
221 opt_context_field_names = 0;
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;
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 {
245 fprintf(stderr, "[error] unknown field name type %s\n", str);
246 ret = -EINVAL;
247 goto end;
248 }
249 } while ((str = strtok_r(NULL, ",", &strctx)));
250 end:
251 free(strlist);
252 return ret;
253 }
254
255 static int get_fields_args(poptContext *pc)
256 {
257 char *str, *strlist, *strctx;
258 int ret = 0;
259
260 strlist = (char *) poptGetOptArg(*pc);
261 if (!strlist) {
262 return -EINVAL;
263 }
264 str = strtok_r(strlist, ",", &strctx);
265 do {
266 opt_trace_default_fields = 0;
267 if (!strcmp(str, "all"))
268 opt_all_fields = 1;
269 else if (!strcmp(str, "trace"))
270 opt_trace_field = 1;
271 else if (!strcmp(str, "trace:hostname"))
272 opt_trace_hostname_field = 1;
273 else if (!strcmp(str, "trace:domain"))
274 opt_trace_domain_field = 1;
275 else if (!strcmp(str, "trace:procname"))
276 opt_trace_procname_field = 1;
277 else if (!strcmp(str, "trace:vpid"))
278 opt_trace_vpid_field = 1;
279 else if (!strcmp(str, "loglevel"))
280 opt_loglevel_field = 1;
281 else if (!strcmp(str, "emf"))
282 opt_emf_field = 1;
283 else if (!strcmp(str, "callsite"))
284 opt_callsite_field = 1;
285 else {
286 fprintf(stderr, "[error] unknown field type %s\n", str);
287 ret = -EINVAL;
288 goto end;
289 }
290 } while ((str = strtok_r(NULL, ",", &strctx)));
291 end:
292 free(strlist);
293 return ret;
294 }
295
296 /*
297 * Return 0 if caller should continue, < 0 if caller should return
298 * error, > 0 if caller should exit without reporting error.
299 */
300 static int parse_options(int argc, char **argv)
301 {
302 poptContext pc;
303 int opt, ret = 0;
304 const char *ipath;
305
306 if (argc == 1) {
307 usage(stdout);
308 return 1; /* exit cleanly */
309 }
310
311 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
312 poptReadDefaultConfig(pc, 0);
313
314 /* set default */
315 opt_context_field_names = 1;
316 opt_payload_field_names = 1;
317
318 while ((opt = poptGetNextOpt(pc)) != -1) {
319 switch (opt) {
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;
341 case OPT_HELP:
342 usage(stdout);
343 ret = 1; /* exit cleanly */
344 goto end;
345 case OPT_LIST:
346 list_formats(stdout);
347 ret = 1;
348 goto end;
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;
356 case OPT_VERBOSE:
357 babeltrace_verbose = 1;
358 break;
359 case OPT_NAMES:
360 if (get_names_args(&pc)) {
361 ret = -EINVAL;
362 goto end;
363 }
364 break;
365 case OPT_FIELDS:
366 if (get_fields_args(&pc)) {
367 ret = -EINVAL;
368 goto end;
369 }
370 break;
371 case OPT_DEBUG:
372 babeltrace_debug = 1;
373 break;
374 case OPT_NO_DELTA:
375 opt_delta_field = 0;
376 break;
377 case OPT_CLOCK_CYCLES:
378 opt_clock_cycles = 1;
379 break;
380 case OPT_CLOCK_OFFSET:
381 {
382 char *str;
383 char *endptr;
384
385 str = (char *) poptGetOptArg(pc);
386 if (!str) {
387 fprintf(stderr, "[error] Missing --clock-offset argument\n");
388 ret = -EINVAL;
389 goto end;
390 }
391 errno = 0;
392 opt_clock_offset = strtoll(str, &endptr, 0);
393 if (*endptr != '\0' || str == endptr || errno != 0) {
394 fprintf(stderr, "[error] Incorrect --clock-offset argument: %s\n", str);
395 ret = -EINVAL;
396 free(str);
397 goto end;
398 }
399 free(str);
400 break;
401 }
402 case OPT_CLOCK_SECONDS:
403 opt_clock_seconds = 1;
404 break;
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;
417 opt_clock_offset_ns = strtoll(str, &endptr, 0);
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
428 case OPT_CLOCK_DATE:
429 opt_clock_date = 1;
430 break;
431 case OPT_CLOCK_GMT:
432 opt_clock_gmt = 1;
433 break;
434 case OPT_CLOCK_FORCE_CORRELATE:
435 opt_clock_force_correlate = 1;
436 break;
437 case OPT_STREAM_INTERSECTION:
438 opt_stream_intersection = 1;
439 break;
440 case OPT_DEBUG_INFO_DIR:
441 opt_debug_info_dir = (char *) poptGetOptArg(pc);
442 if (!opt_debug_info_dir) {
443 ret = -EINVAL;
444 goto end;
445 }
446 break;
447 case OPT_DEBUG_INFO_FULL_PATH:
448 opt_debug_info_full_path = 1;
449 break;
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;
457 default:
458 ret = -EINVAL;
459 goto end;
460 }
461 }
462
463 do {
464 ipath = poptGetArg(pc);
465 if (ipath)
466 g_ptr_array_add(opt_input_paths, (gpointer) ipath);
467 } while (ipath);
468
469 end:
470 if (pc) {
471 poptFreeContext(pc);
472 }
473 return ret;
474 }
475
476 static GPtrArray *traversed_paths = 0;
477
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 */
486 static 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 {
511 int err_close = 0;
512
513 closeret = close(metafd);
514 if (closeret < 0) {
515 perror("close");
516 err_close = 1;
517 }
518 closeret = close(dirfd);
519 if (closeret < 0) {
520 perror("close");
521 err_close = 1;
522 }
523 if (err_close) {
524 return -1;
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 }
537
538 /*
539 * bt_context_add_traces_recursive: Open a trace recursively
540 *
541 * Find each trace present in the subdirectory starting from the given
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.
544 *
545 * Return: 0 on success, < 0 on failure, > 0 on partial failure.
546 * Unable to open toplevel: failure.
547 * Unable to open some subdirectory or file: warn and continue (partial
548 * failure);
549 */
550 int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path,
551 const char *format_str,
552 void (*packet_seek)(struct bt_stream_pos *pos,
553 size_t offset, int whence))
554 {
555 int ret = 0, trace_ids = 0;
556
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);
565 }
566 return ret;
567 }
568 /* Should lock traversed_paths mutex here if used in multithread */
569
570 traversed_paths = g_ptr_array_new();
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) {
575 int i;
576
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);
586 if (trace_id < 0) {
587 fprintf(stderr, "[warning] [Context] cannot open trace \"%s\" from %s "
588 "for reading.\n", trace_path->str, path);
589 /* Allow to skip erroneous traces. */
590 ret = 1; /* partial error */
591 } else {
592 trace_ids++;
593 }
594 g_string_free(trace_path, TRUE);
595 }
596 }
597
598 g_ptr_array_free(traversed_paths, TRUE);
599 traversed_paths = NULL;
600
601 /* Should unlock traversed paths mutex here if used in multithread */
602
603 /*
604 * Return an error if no trace can be opened.
605 */
606 if (trace_ids == 0) {
607 fprintf(stderr, "[error] Cannot open any trace for reading.\n\n");
608 ret = -ENOENT; /* failure */
609 }
610 return ret;
611 }
612
613 static
614 int 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;
639 end:
640 return ret;
641 }
642
643 static
644 int 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;
669 end:
670 return ret;
671 }
672
673 static
674 int convert_trace(struct bt_trace_descriptor *td_write,
675 struct bt_context *ctx)
676 {
677 struct bt_ctf_iter *iter;
678 struct ctf_text_stream_pos *sout;
679 struct bt_iter_pos *begin_pos = NULL, *end_pos = NULL;
680 struct bt_ctf_event *ctf_event;
681 int ret;
682
683 sout = container_of(td_write, struct ctf_text_stream_pos,
684 trace_descriptor);
685
686 if (!sout->parent.event_cb) {
687 return 0;
688 }
689
690 if (opt_stream_intersection) {
691 iter = bt_ctf_iter_create_intersect(ctx, &begin_pos, &end_pos);
692 } else {
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);
696 }
697 if (!iter) {
698 ret = -1;
699 goto error_iter;
700 }
701 while ((ctf_event = bt_ctf_iter_read_event(iter))) {
702 ret = sout->parent.event_cb(&sout->parent, ctf_event->parent->stream);
703 if (ret) {
704 fprintf(stderr, "[error] Writing event failed.\n");
705 goto end;
706 }
707 ret = bt_iter_next(bt_ctf_get_iter(iter));
708 if (ret < 0) {
709 goto end;
710 }
711 }
712 ret = 0;
713
714 end:
715 bt_ctf_iter_destroy(iter);
716 error_iter:
717 bt_iter_free_pos(begin_pos);
718 bt_iter_free_pos(end_pos);
719 return ret;
720 }
721
722 void 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
731 int main(int argc, char **argv)
732 {
733 int ret, partial_error = 0, open_success = 0;
734 struct bt_format *fmt_write;
735 struct bt_trace_descriptor *td_write;
736 struct bt_context *ctx;
737 struct bt_component_factory *component_factory;
738 struct bt_value *components = NULL;
739 int i;
740
741 call_plugins_hooks();
742
743 opt_input_paths = g_ptr_array_new();
744
745 ret = parse_options(argc, argv);
746 if (ret < 0) {
747 fprintf(stderr, "Error parsing options.\n\n");
748 usage(stderr);
749 g_ptr_array_free(opt_input_paths, TRUE);
750 exit(EXIT_FAILURE);
751 } else if (ret > 0) {
752 g_ptr_array_free(opt_input_paths, TRUE);
753 exit(EXIT_SUCCESS);
754 }
755 printf_verbose("Verbose mode active.\n");
756 printf_debug("Debug mode active.\n");
757
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
778 components = bt_component_factory_get_components(component_factory);
779 if (!components || bt_value_array_is_empty(components)) {
780 printf_error("No plugins found, exiting.");
781 ret = -1;
782 goto end;
783 }
784
785 if (opt_input_paths->len == 0) {
786 ret = -1;
787 goto end;
788 }
789
790 if (opt_input_format)
791 strlower(opt_input_format);
792 if (opt_output_format)
793 strlower(opt_output_format);
794
795 printf_verbose("Converting from directory(ies):\n");
796 for (i = 0; i < opt_input_paths->len; i++) {
797 const char *ipath = g_ptr_array_index(opt_input_paths, i);
798 printf_verbose(" %s\n", ipath);
799 }
800 printf_verbose("Converting from format: %s\n",
801 opt_input_format ? : "ctf <default>");
802 printf_verbose("Converting to target: %s\n",
803 opt_output_path ? : "<stdout>");
804 printf_verbose("Converting to format: %s\n",
805 opt_output_format ? : "text <default>");
806
807 if (!opt_input_format) {
808 opt_input_format = strdup("ctf");
809 if (!opt_input_format) {
810 partial_error = 1;
811 goto end;
812 }
813 }
814 if (!opt_output_format) {
815 opt_output_format = strdup("text");
816 if (!opt_output_format) {
817 partial_error = 1;
818 goto end;
819 }
820 }
821 fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
822 if (!fmt_read) {
823 fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n",
824 opt_input_format);
825 partial_error = 1;
826 goto end;
827 }
828 fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
829 if (!fmt_write) {
830 fprintf(stderr, "[error] format \"%s\" is not supported.\n\n",
831 opt_output_format);
832 partial_error = 1;
833 goto end;
834 }
835
836 ctx = bt_context_create();
837 if (!ctx) {
838 goto error_td_read;
839 }
840
841 for (i = 0; i < opt_input_paths->len; i++) {
842 const char *ipath = g_ptr_array_index(opt_input_paths, i);
843 ret = bt_context_add_traces_recursive(ctx, ipath,
844 opt_input_format, NULL);
845 if (ret < 0) {
846 fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n",
847 ipath);
848 } else if (ret > 0) {
849 fprintf(stderr, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n",
850 ipath);
851 open_success = 1; /* some traces were OK */
852 partial_error = 1;
853 } else {
854 open_success = 1; /* all traces were OK */
855 }
856 }
857 if (!open_success) {
858 fprintf(stderr, "[error] none of the specified trace paths could be opened.\n\n");
859 goto error_td_read;
860 }
861
862 td_write = fmt_write->open_trace(opt_output_path, O_RDWR, NULL, NULL);
863 if (!td_write) {
864 fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n",
865 opt_output_path ? : "<none>");
866 goto error_td_write;
867 }
868
869 /*
870 * Errors happened when opening traces, but we continue anyway.
871 * sleep to let user see the stderr output before stdout.
872 */
873 if (partial_error)
874 sleep(PARTIAL_ERROR_SLEEP);
875
876 ret = trace_pre_handler(td_write, ctx);
877 if (ret) {
878 fprintf(stderr, "Error in trace pre handle.\n\n");
879 goto error_copy_trace;
880 }
881
882 /* For now, we support only CTF iterators */
883 if (fmt_read->name == g_quark_from_static_string("ctf")) {
884 ret = convert_trace(td_write, ctx);
885 if (ret) {
886 fprintf(stderr, "Error printing trace.\n\n");
887 goto error_copy_trace;
888 }
889 }
890
891 ret = trace_post_handler(td_write, ctx);
892 if (ret) {
893 fprintf(stderr, "Error in trace post handle.\n\n");
894 goto error_copy_trace;
895 }
896
897 fmt_write->close_trace(td_write);
898
899 bt_context_put(ctx);
900 printf_verbose("finished converting. Output written to:\n%s\n",
901 opt_output_path ? : "<stdout>");
902 goto end;
903
904 /* Error handling */
905 error_copy_trace:
906 fmt_write->close_trace(td_write);
907 error_td_write:
908 bt_context_put(ctx);
909 error_td_read:
910 partial_error = 1;
911
912 /* teardown and exit */
913 end:
914 free(opt_input_format);
915 free(opt_output_format);
916 free(opt_output_path);
917 free(opt_debug_info_dir);
918 free(opt_debug_info_target_prefix);
919 g_ptr_array_free(opt_input_paths, TRUE);
920 BT_PUT(components);
921 BT_PUT(component_factory);
922 if (partial_error)
923 exit(EXIT_FAILURE);
924 else
925 exit(EXIT_SUCCESS);
926 }
This page took 0.08146 seconds and 4 git commands to generate.