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