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