4 * Babeltrace Trace Converter
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
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>
41 #include <babeltrace/iterator.h>
42 #include <babeltrace/plugin/component-factory.h>
43 #include <babeltrace/plugin/plugin.h>
44 #include <babeltrace/plugin/component-class.h>
45 #include <babeltrace/ref.h>
46 #include <babeltrace/values.h>
52 #include <sys/types.h>
59 #include <babeltrace/ctf-ir/metadata.h> /* for clocks */
61 #define PARTIAL_ERROR_SLEEP 3 /* 3 seconds */
63 #define DEFAULT_FILE_ARRAY_SIZE 1
65 #define NET_URL_PREFIX "net://"
66 #define NET4_URL_PREFIX "net4://"
67 #define NET6_URL_PREFIX "net6://"
69 static char *opt_input_format
, *opt_output_format
;
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.
78 static GPtrArray
*opt_input_paths
;
79 static char *opt_output_path
;
80 static int opt_stream_intersection
;
81 static char *opt_plugin_path
;
83 static struct bt_format
*fmt_read
;
85 void bt_dummy_hook(void);
86 void bt_lttng_live_hook(void);
87 void bt_ctf_hook(void);
88 void bt_ctf_text_hook(void);
89 void bt_ctf_metadata_hook(void);
92 void strlower(char *str
)
95 *str
= tolower((int) *str
);
119 OPT_CLOCK_FORCE_CORRELATE
,
120 OPT_STREAM_INTERSECTION
,
122 OPT_DEBUG_INFO_FULL_PATH
,
123 OPT_DEBUG_INFO_TARGET_PREFIX
,
127 * We are _not_ using POPT_ARG_STRING's ability to store directly into
128 * variables, because we want to cast the return to non-const, which is
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.
134 static struct poptOption long_options
[] = {
135 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
136 { "output", 'w', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_PATH
, NULL
, NULL
},
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
},
139 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
140 { "list", 'l', POPT_ARG_NONE
, NULL
, OPT_LIST
, NULL
, NULL
},
141 { "plugin-path", 0, POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
142 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
143 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
144 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
145 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
146 { "no-delta", 0, POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
147 { "clock-offset", 0, POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
148 { "clock-offset-ns", 0, POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET_NS
, NULL
, NULL
},
149 { "clock-cycles", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_CYCLES
, NULL
, NULL
},
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
},
153 { "clock-force-correlate", 0, POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
154 { "stream-intersection", 0, POPT_ARG_NONE
, NULL
, OPT_STREAM_INTERSECTION
, NULL
, NULL
},
155 #ifdef ENABLE_DEBUG_INFO
156 { "debug-info-dir", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_DIR
, NULL
, NULL
},
157 { "debug-info-full-path", 0, POPT_ARG_NONE
, NULL
, OPT_DEBUG_INFO_FULL_PATH
, NULL
, NULL
},
158 { "debug-info-target-prefix", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_TARGET_PREFIX
, NULL
, NULL
},
160 { NULL
, 0, 0, NULL
, 0, NULL
, NULL
},
163 static void list_formats(FILE *fp
)
166 bt_fprintf_format_list(fp
);
169 static void usage(FILE *fp
)
171 fprintf(fp
, "BabelTrace Trace Viewer and Converter %s\n\n", VERSION
);
172 fprintf(fp
, "usage : babeltrace [OPTIONS] FILE...\n");
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");
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");
181 fprintf(fp
, " -h, --help This help message\n");
182 fprintf(fp
, " -l, --list List available formats\n");
183 fprintf(fp
, " --plugin-path Supplementary plug-in path\n");
184 fprintf(fp
, " -v, --verbose Verbose mode\n");
185 fprintf(fp
, " (or set BABELTRACE_VERBOSE environment variable)\n");
186 fprintf(fp
, " -d, --debug Debug mode\n");
187 fprintf(fp
, " (or set BABELTRACE_DEBUG environment variable)\n");
188 fprintf(fp
, " --no-delta Do not print time delta between consecutive events\n");
189 fprintf(fp
, " -n, --names name1<,name2,...> Print field names:\n");
190 fprintf(fp
, " (payload OR args OR arg)\n");
191 fprintf(fp
, " none, all, scope, header, (context OR ctx)\n");
192 fprintf(fp
, " (default: payload,context)\n");
193 fprintf(fp
, " -f, --fields name1<,name2,...> Print additional fields:\n");
194 fprintf(fp
, " all, trace, trace:hostname, trace:domain,\n");
195 fprintf(fp
, " trace:procname, trace:vpid, loglevel, emf, callsite.\n");
196 fprintf(fp
, " (default: trace:hostname,trace:procname,trace:vpid)\n");
197 fprintf(fp
, " --clock-cycles Timestamp in cycles\n");
198 fprintf(fp
, " --clock-offset seconds Clock offset in seconds\n");
199 fprintf(fp
, " --clock-offset-ns ns Clock offset in nanoseconds\n");
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");
204 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently correlated\n");
205 fprintf(fp
, " across traces.\n");
206 fprintf(fp
, " --stream-intersection Only print events when all streams are active.\n");
207 #ifdef ENABLE_DEBUG_INFO
208 fprintf(fp
, " --debug-info-dir Directory in which to look for debugging information\n");
209 fprintf(fp
, " files. (default: /usr/lib/debug/)\n");
210 fprintf(fp
, " --debug-info-target-prefix Directory to use as a prefix for executable lookup\n");
211 fprintf(fp
, " --debug-info-full-path Show full debug info source and binary paths (if available)\n");
217 static int get_names_args(poptContext
*pc
)
219 char *str
, *strlist
, *strctx
;
222 opt_payload_field_names
= 0;
223 opt_context_field_names
= 0;
224 strlist
= (char *) poptGetOptArg(*pc
);
228 str
= strtok_r(strlist
, ",", &strctx
);
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;
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;
247 fprintf(stderr
, "[error] unknown field name type %s\n", str
);
251 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
257 static int get_fields_args(poptContext
*pc
)
259 char *str
, *strlist
, *strctx
;
262 strlist
= (char *) poptGetOptArg(*pc
);
266 str
= strtok_r(strlist
, ",", &strctx
);
268 opt_trace_default_fields
= 0;
269 if (!strcmp(str
, "all"))
271 else if (!strcmp(str
, "trace"))
273 else if (!strcmp(str
, "trace:hostname"))
274 opt_trace_hostname_field
= 1;
275 else if (!strcmp(str
, "trace:domain"))
276 opt_trace_domain_field
= 1;
277 else if (!strcmp(str
, "trace:procname"))
278 opt_trace_procname_field
= 1;
279 else if (!strcmp(str
, "trace:vpid"))
280 opt_trace_vpid_field
= 1;
281 else if (!strcmp(str
, "loglevel"))
282 opt_loglevel_field
= 1;
283 else if (!strcmp(str
, "emf"))
285 else if (!strcmp(str
, "callsite"))
286 opt_callsite_field
= 1;
288 fprintf(stderr
, "[error] unknown field type %s\n", str
);
292 } while ((str
= strtok_r(NULL
, ",", &strctx
)));
299 * Return 0 if caller should continue, < 0 if caller should return
300 * error, > 0 if caller should exit without reporting error.
302 static int parse_options(int argc
, char **argv
)
310 return 1; /* exit cleanly */
313 pc
= poptGetContext(NULL
, argc
, (const char **) argv
, long_options
, 0);
314 poptReadDefaultConfig(pc
, 0);
317 opt_context_field_names
= 1;
318 opt_payload_field_names
= 1;
320 while ((opt
= poptGetNextOpt(pc
)) != -1) {
322 case OPT_OUTPUT_PATH
:
323 opt_output_path
= (char *) poptGetOptArg(pc
);
324 if (!opt_output_path
) {
329 case OPT_INPUT_FORMAT
:
330 opt_input_format
= (char *) poptGetOptArg(pc
);
331 if (!opt_input_format
) {
336 case OPT_OUTPUT_FORMAT
:
337 opt_output_format
= (char *) poptGetOptArg(pc
);
338 if (!opt_output_format
) {
345 ret
= 1; /* exit cleanly */
348 list_formats(stdout
);
351 case OPT_PLUGIN_PATH
:
352 opt_plugin_path
= (char *) poptGetOptArg(pc
);
353 if (!opt_plugin_path
) {
359 babeltrace_verbose
= 1;
362 if (get_names_args(&pc
)) {
368 if (get_fields_args(&pc
)) {
374 babeltrace_debug
= 1;
379 case OPT_CLOCK_CYCLES
:
380 opt_clock_cycles
= 1;
382 case OPT_CLOCK_OFFSET
:
387 str
= (char *) poptGetOptArg(pc
);
389 fprintf(stderr
, "[error] Missing --clock-offset argument\n");
394 opt_clock_offset
= strtoll(str
, &endptr
, 0);
395 if (*endptr
!= '\0' || str
== endptr
|| errno
!= 0) {
396 fprintf(stderr
, "[error] Incorrect --clock-offset argument: %s\n", str
);
404 case OPT_CLOCK_SECONDS
:
405 opt_clock_seconds
= 1;
407 case OPT_CLOCK_OFFSET_NS
:
412 str
= (char *) poptGetOptArg(pc
);
414 fprintf(stderr
, "[error] Missing --clock-offset-ns argument\n");
419 opt_clock_offset_ns
= strtoll(str
, &endptr
, 0);
420 if (*endptr
!= '\0' || str
== endptr
|| errno
!= 0) {
421 fprintf(stderr
, "[error] Incorrect --clock-offset-ns argument: %s\n", str
);
436 case OPT_CLOCK_FORCE_CORRELATE
:
437 opt_clock_force_correlate
= 1;
439 case OPT_STREAM_INTERSECTION
:
440 opt_stream_intersection
= 1;
442 case OPT_DEBUG_INFO_DIR
:
443 opt_debug_info_dir
= (char *) poptGetOptArg(pc
);
444 if (!opt_debug_info_dir
) {
449 case OPT_DEBUG_INFO_FULL_PATH
:
450 opt_debug_info_full_path
= 1;
452 case OPT_DEBUG_INFO_TARGET_PREFIX
:
453 opt_debug_info_target_prefix
= (char *) poptGetOptArg(pc
);
454 if (!opt_debug_info_target_prefix
) {
466 ipath
= poptGetArg(pc
);
468 g_ptr_array_add(opt_input_paths
, (gpointer
) ipath
);
478 static GPtrArray
*traversed_paths
= 0;
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.
488 static int traverse_trace_dir(const char *fpath
, const struct stat
*sb
,
489 int tflag
, struct FTW
*ftwbuf
)
497 dirfd
= open(fpath
, 0);
499 fprintf(stderr
, "[error] [Context] Unable to open trace "
500 "directory file descriptor.\n");
501 return 0; /* partial error */
503 metafd
= openat(dirfd
, "metadata", O_RDONLY
);
505 closeret
= close(dirfd
);
510 /* No meta data, just return */
515 closeret
= close(metafd
);
520 closeret
= close(dirfd
);
529 /* Add path to the global list */
530 if (traversed_paths
== NULL
) {
531 fprintf(stderr
, "[error] [Context] Invalid open path array.\n");
534 g_ptr_array_add(traversed_paths
, g_string_new(fpath
));
541 * bt_context_add_traces_recursive: Open a trace recursively
543 * Find each trace present in the subdirectory starting from the given
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.
547 * Return: 0 on success, < 0 on failure, > 0 on partial failure.
548 * Unable to open toplevel: failure.
549 * Unable to open some subdirectory or file: warn and continue (partial
552 int bt_context_add_traces_recursive(struct bt_context
*ctx
, const char *path
,
553 const char *format_str
,
554 void (*packet_seek
)(struct bt_stream_pos
*pos
,
555 size_t offset
, int whence
))
557 int ret
= 0, trace_ids
= 0;
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
);
565 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" "
566 "for reading.\n", path
);
570 /* Should lock traversed_paths mutex here if used in multithread */
572 traversed_paths
= g_ptr_array_new();
573 ret
= nftw(path
, traverse_trace_dir
, 10, 0);
575 /* Process the array if ntfw did not return a fatal error */
579 for (i
= 0; i
< traversed_paths
->len
; i
++) {
580 GString
*trace_path
= g_ptr_array_index(traversed_paths
,
582 int trace_id
= bt_context_add_trace(ctx
,
589 fprintf(stderr
, "[warning] [Context] cannot open trace \"%s\" from %s "
590 "for reading.\n", trace_path
->str
, path
);
591 /* Allow to skip erroneous traces. */
592 ret
= 1; /* partial error */
596 g_string_free(trace_path
, TRUE
);
600 g_ptr_array_free(traversed_paths
, TRUE
);
601 traversed_paths
= NULL
;
603 /* Should unlock traversed paths mutex here if used in multithread */
606 * Return an error if no trace can be opened.
608 if (trace_ids
== 0) {
609 fprintf(stderr
, "[error] Cannot open any trace for reading.\n\n");
610 ret
= -ENOENT
; /* failure */
616 int trace_pre_handler(struct bt_trace_descriptor
*td_write
,
617 struct bt_context
*ctx
)
619 struct ctf_text_stream_pos
*sout
;
620 struct trace_collection
*tc
;
623 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
626 if (!sout
->parent
.pre_trace_cb
)
630 for (i
= 0; i
< tc
->array
->len
; i
++) {
631 struct bt_trace_descriptor
*td
=
632 g_ptr_array_index(tc
->array
, i
);
634 ret
= sout
->parent
.pre_trace_cb(&sout
->parent
, td
);
636 fprintf(stderr
, "[error] Writing to trace pre handler failed.\n");
646 int trace_post_handler(struct bt_trace_descriptor
*td_write
,
647 struct bt_context
*ctx
)
649 struct ctf_text_stream_pos
*sout
;
650 struct trace_collection
*tc
;
653 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
656 if (!sout
->parent
.post_trace_cb
)
660 for (i
= 0; i
< tc
->array
->len
; i
++) {
661 struct bt_trace_descriptor
*td
=
662 g_ptr_array_index(tc
->array
, i
);
664 ret
= sout
->parent
.post_trace_cb(&sout
->parent
, td
);
666 fprintf(stderr
, "[error] Writing to trace post handler failed.\n");
676 int convert_trace(struct bt_trace_descriptor
*td_write
,
677 struct bt_context
*ctx
)
679 struct bt_ctf_iter
*iter
;
680 struct ctf_text_stream_pos
*sout
;
681 struct bt_iter_pos
*begin_pos
= NULL
, *end_pos
= NULL
;
682 struct bt_ctf_event
*ctf_event
;
685 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
688 if (!sout
->parent
.event_cb
) {
692 if (opt_stream_intersection
) {
693 iter
= bt_ctf_iter_create_intersect(ctx
, &begin_pos
, &end_pos
);
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
);
703 while ((ctf_event
= bt_ctf_iter_read_event(iter
))) {
704 ret
= sout
->parent
.event_cb(&sout
->parent
, ctf_event
->parent
->stream
);
706 fprintf(stderr
, "[error] Writing event failed.\n");
709 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
717 bt_ctf_iter_destroy(iter
);
719 bt_iter_free_pos(begin_pos
);
720 bt_iter_free_pos(end_pos
);
725 const char *component_type_str(enum bt_component_type type
)
728 case BT_COMPONENT_TYPE_SOURCE
:
730 case BT_COMPONENT_TYPE_SINK
:
732 case BT_COMPONENT_TYPE_FILTER
:
734 case BT_COMPONENT_TYPE_UNKNOWN
:
741 void print_detected_component_classes(struct bt_component_factory
*factory
)
745 if (!babeltrace_verbose
) {
749 count
= bt_component_factory_get_component_class_count(factory
);
751 fprintf(stderr
, "No component classes found. Please make sure your plug-in search path is set correctly.");
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(
760 struct bt_plugin
*plugin
= bt_component_class_get_plugin(
762 const char *plugin_name
= bt_plugin_get_name(plugin
);
763 const char *component_name
= bt_component_class_get_name(
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(
770 const char *component_description
=
771 bt_component_class_get_description(
773 enum bt_component_type type
= bt_component_class_get_type(
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");
789 void test_sink_notifications(struct bt_component
*sink
)
794 int main(int argc
, char **argv
)
796 int ret
, partial_error
= 0, open_success
= 0;
797 struct bt_format
*fmt_write
;
798 struct bt_trace_descriptor
*td_write
;
799 struct bt_context
*ctx
;
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
;
807 call_plugins_hooks();
809 opt_input_paths
= g_ptr_array_new();
811 ret
= parse_options(argc
, argv
);
813 fprintf(stderr
, "Error parsing options.\n\n");
815 g_ptr_array_free(opt_input_paths
, TRUE
);
817 } else if (ret
> 0) {
818 g_ptr_array_free(opt_input_paths
, TRUE
);
821 printf_verbose("Verbose mode active.\n");
822 printf_debug("Debug mode active.\n");
824 if (!opt_plugin_path
) {
825 fprintf(stderr
, "No plugin path specified, aborting...\n");
829 printf_verbose("Looking-up plugins at %s\n",
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");
838 ret
= bt_component_factory_load(component_factory
, opt_plugin_path
);
840 fprintf(stderr
, "Failed to load plugins.\n");
844 print_detected_component_classes(component_factory
);
846 sink_class
= bt_component_factory_get_component_class(component_factory
,
847 NULL
, BT_COMPONENT_TYPE_SINK
, "text");
849 fprintf(stderr
, "Could not find text output component class. Aborting...\n");
854 sink
= bt_component_create(sink_class
, "bt_text_output", sink_params
);
856 fprintf(stderr
, "Failed to instanciate text output. Aborting...\n");
861 test_sink_notifications(sink
);
864 if (opt_input_paths
->len
== 0) {
869 if (opt_input_format
)
870 strlower(opt_input_format
);
871 if (opt_output_format
)
872 strlower(opt_output_format
);
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
);
879 printf_verbose("Converting from format: %s\n",
880 opt_input_format
? : "ctf <default>");
881 printf_verbose("Converting to target: %s\n",
882 opt_output_path
? : "<stdout>");
883 printf_verbose("Converting to format: %s\n",
884 opt_output_format
? : "text <default>");
886 if (!opt_input_format
) {
887 opt_input_format
= strdup("ctf");
888 if (!opt_input_format
) {
893 if (!opt_output_format
) {
894 opt_output_format
= strdup("text");
895 if (!opt_output_format
) {
900 fmt_read
= bt_lookup_format(g_quark_from_static_string(opt_input_format
));
902 fprintf(stderr
, "[error] Format \"%s\" is not supported.\n\n",
907 fmt_write
= bt_lookup_format(g_quark_from_static_string(opt_output_format
));
909 fprintf(stderr
, "[error] format \"%s\" is not supported.\n\n",
915 ctx
= bt_context_create();
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
);
925 fprintf(stderr
, "[error] opening trace \"%s\" for reading.\n\n",
927 } else if (ret
> 0) {
928 fprintf(stderr
, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n",
930 open_success
= 1; /* some traces were OK */
933 open_success
= 1; /* all traces were OK */
937 fprintf(stderr
, "[error] none of the specified trace paths could be opened.\n\n");
941 td_write
= fmt_write
->open_trace(opt_output_path
, O_RDWR
, NULL
, NULL
);
943 fprintf(stderr
, "Error opening trace \"%s\" for writing.\n\n",
944 opt_output_path
? : "<none>");
949 * Errors happened when opening traces, but we continue anyway.
950 * sleep to let user see the stderr output before stdout.
953 sleep(PARTIAL_ERROR_SLEEP
);
955 ret
= trace_pre_handler(td_write
, ctx
);
957 fprintf(stderr
, "Error in trace pre handle.\n\n");
958 goto error_copy_trace
;
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
);
965 fprintf(stderr
, "Error printing trace.\n\n");
966 goto error_copy_trace
;
970 ret
= trace_post_handler(td_write
, ctx
);
972 fprintf(stderr
, "Error in trace post handle.\n\n");
973 goto error_copy_trace
;
976 fmt_write
->close_trace(td_write
);
979 printf_verbose("finished converting. Output written to:\n%s\n",
980 opt_output_path
? : "<stdout>");
985 fmt_write
->close_trace(td_write
);
991 /* teardown and exit */
993 free(opt_input_format
);
994 free(opt_output_format
);
995 free(opt_output_path
);
996 free(opt_debug_info_dir
);
997 free(opt_debug_info_target_prefix
);
998 g_ptr_array_free(opt_input_paths
, TRUE
);
999 BT_PUT(component_factory
);
1001 BT_PUT(source_class
);
1004 BT_PUT(source_params
);
1005 BT_PUT(sink_params
);