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/plugin/component-factory.h>
31 #include <babeltrace/plugin/plugin.h>
32 #include <babeltrace/plugin/component-class.h>
33 #include <babeltrace/plugin/notification/iterator.h>
34 #include <babeltrace/ref.h>
35 #include <babeltrace/values.h>
37 #include <babeltrace/ctf-ir/metadata.h> /* for clocks */
41 #include "babeltrace-cfg.h"
44 const char *component_type_str(enum bt_component_type type
)
47 case BT_COMPONENT_TYPE_SOURCE
:
49 case BT_COMPONENT_TYPE_SINK
:
51 case BT_COMPONENT_TYPE_FILTER
:
53 case BT_COMPONENT_TYPE_UNKNOWN
:
60 void print_component_classes_found(struct bt_component_factory
*factory
)
64 if (!babeltrace_verbose
) {
68 count
= bt_component_factory_get_component_class_count(factory
);
70 fprintf(stderr
, "No component classes found. Please make sure your plug-in search path is set correctly.\n");
74 printf_verbose("Found %d component classes.\n", count
);
75 for (i
= 0; i
< count
; i
++) {
76 struct bt_component_class
*component_class
=
77 bt_component_factory_get_component_class_index(
79 struct bt_plugin
*plugin
= bt_component_class_get_plugin(
81 const char *plugin_name
= bt_plugin_get_name(plugin
);
82 const char *component_name
= bt_component_class_get_name(
84 const char *path
= bt_plugin_get_path(plugin
);
85 const char *author
= bt_plugin_get_author(plugin
);
86 const char *license
= bt_plugin_get_license(plugin
);
87 const char *plugin_description
= bt_plugin_get_description(
89 const char *component_description
=
90 bt_component_class_get_description(
92 enum bt_component_type type
= bt_component_class_get_type(
95 printf_verbose("[%s - %s (%s)]\n", plugin_name
, component_name
,
96 component_type_str(type
));
97 printf_verbose("\tpath: %s\n", path
? path
: "None");
98 printf_verbose("\tauthor: %s\n", author
? author
: "Unknown");
99 printf_verbose("\tlicense: %s\n", license
? license
: "Unknown");
100 printf_verbose("\tplugin description: %s\n",
101 plugin_description
? plugin_description
: "None");
102 printf_verbose("\tcomponent description: %s\n",
103 component_description
? component_description
: "None");
106 bt_put(component_class
);
111 void print_indent(size_t indent
)
115 for (i
= 0; i
< indent
; i
++) {
121 void print_value(struct bt_value
*, size_t, bool);
124 bool print_map_value(const char *key
, struct bt_value
*object
, void *data
)
126 size_t indent
= (size_t) data
;
128 print_indent(indent
);
129 printf("\"%s\": ", key
);
130 print_value(object
, indent
, false);
136 void print_value(struct bt_value
*value
, size_t indent
, bool do_indent
)
150 print_indent(indent
);
153 switch (bt_value_get_type(value
)) {
154 case BT_VALUE_TYPE_NULL
:
157 case BT_VALUE_TYPE_BOOL
:
158 bt_value_bool_get(value
, &bool_val
);
159 printf("%s\n", bool_val
? "true" : "false");
161 case BT_VALUE_TYPE_INTEGER
:
162 bt_value_integer_get(value
, &int_val
);
163 printf("%" PRId64
"\n", int_val
);
165 case BT_VALUE_TYPE_FLOAT
:
166 bt_value_float_get(value
, &dbl_val
);
167 printf("%lf\n", dbl_val
);
169 case BT_VALUE_TYPE_STRING
:
170 bt_value_string_get(value
, &str_val
);
171 printf("\"%s\"\n", str_val
);
173 case BT_VALUE_TYPE_ARRAY
:
174 size
= bt_value_array_size(value
);
177 for (i
= 0; i
< size
; i
++) {
178 struct bt_value
*element
=
179 bt_value_array_get(value
, i
);
181 print_value(element
, indent
+ 2, true);
185 print_indent(indent
);
188 case BT_VALUE_TYPE_MAP
:
189 if (bt_value_map_is_empty(value
)) {
195 bt_value_map_foreach(value
, print_map_value
,
196 (void *) (indent
+ 2));
197 print_indent(indent
);
206 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
208 printf(" %s.%s\n", bt_config_component
->plugin_name
->str
,
209 bt_config_component
->component_name
->str
);
210 printf(" begin timestamp: ");
212 if (bt_config_component
->begin_ns
== -1ULL) {
215 printf("%" PRIu64
" ns\n", bt_config_component
->begin_ns
);
218 printf(" end timestamp: ");
220 if (bt_config_component
->end_ns
== -1ULL) {
223 printf("%" PRIu64
" ns\n", bt_config_component
->end_ns
);
226 printf(" params:\n");
227 print_value(bt_config_component
->params
, 6, true);
231 void print_bt_config_components(GPtrArray
*array
)
235 for (i
= 0; i
< array
->len
; i
++) {
236 struct bt_config_component
*cfg_component
=
237 bt_config_get_component(array
, i
);
238 print_bt_config_component(cfg_component
);
239 BT_PUT(cfg_component
);
244 void print_cfg(struct bt_config
*cfg
)
246 printf("debug: %d\n", cfg
->debug
);
247 printf("verbose: %d\n", cfg
->verbose
);
248 printf("do list: %d\n", cfg
->do_list
);
249 printf("force correlate: %d\n", cfg
->force_correlate
);
250 printf("plugin paths:\n");
251 print_value(cfg
->plugin_paths
, 2, true);
252 printf("sources:\n");
253 print_bt_config_components(cfg
->sources
);
255 print_bt_config_components(cfg
->sinks
);
258 int main(int argc
, char **argv
)
261 struct bt_component_factory
*component_factory
= NULL
;
262 struct bt_component_class
*source_class
= NULL
;
263 struct bt_component_class
*sink_class
= NULL
;
264 struct bt_component
*source
= NULL
, *sink
= NULL
;
265 struct bt_value
*source_params
= NULL
, *sink_params
= NULL
;
266 struct bt_config
*cfg
;
267 struct bt_notification_iterator
*it
= NULL
;
268 enum bt_component_status sink_status
;
269 struct bt_value
*first_plugin_path_value
= NULL
;
270 const char *first_plugin_path
;
271 struct bt_config_component
*source_cfg
= NULL
, *sink_cfg
= NULL
;
273 cfg
= bt_config_from_args(argc
, argv
, &ret
);
280 babeltrace_verbose
= cfg
->verbose
;
281 babeltrace_debug
= cfg
->debug
;
283 /* TODO handle more than 1 source and 1 sink. */
284 if (cfg
->sources
->len
!= 1 || cfg
->sinks
->len
!= 1) {
285 fprintf(stderr
, "Unexpected configuration, aborting...\n");
290 printf_verbose("Verbose mode active.\n");
291 printf_debug("Debug mode active.\n");
292 component_factory
= bt_component_factory_create();
293 if (!component_factory
) {
294 fprintf(stderr
, "Failed to create component factory.\n");
299 if (cfg
->plugin_paths
&& !bt_value_array_is_empty(cfg
->plugin_paths
)) {
300 first_plugin_path_value
= bt_value_array_get(
301 cfg
->plugin_paths
, 0);
302 bt_value_string_get(first_plugin_path_value
,
304 ret
= bt_component_factory_load_recursive(component_factory
,
307 fprintf(stderr
, "Failed to dynamically load plugins.\n");
312 ret
= bt_component_factory_load_static(component_factory
);
314 fprintf(stderr
, "Failed to load static plugins.\n");
318 print_component_classes_found(component_factory
);
320 source_cfg
= bt_config_get_component(cfg
->sources
, 0);
321 source_params
= bt_get(source_cfg
->params
);
322 source_class
= bt_component_factory_get_component_class(
323 component_factory
, source_cfg
->plugin_name
->str
,
324 BT_COMPONENT_TYPE_SOURCE
,
325 source_cfg
->component_name
->str
);
327 fprintf(stderr
, "Could not find %s.%s source component class. Aborting...\n",
328 source_cfg
->plugin_name
->str
,
329 source_cfg
->component_name
->str
);
334 sink_cfg
= bt_config_get_component(cfg
->sinks
, 0);
335 sink_params
= bt_get(sink_cfg
->params
);
336 sink_class
= bt_component_factory_get_component_class(component_factory
,
337 sink_cfg
->plugin_name
->str
, BT_COMPONENT_TYPE_SINK
,
338 sink_cfg
->component_name
->str
);
340 fprintf(stderr
, "Could not find %s.%s output component class. Aborting...\n",
341 sink_cfg
->plugin_name
->str
,
342 sink_cfg
->component_name
->str
);
347 source
= bt_component_create(source_class
, "source", source_params
);
349 fprintf(stderr
, "Failed to instantiate selected source component. Aborting...\n");
354 sink
= bt_component_create(sink_class
, "sink", sink_params
);
356 fprintf(stderr
, "Failed to instantiate selected output component. Aborting...\n");
361 it
= bt_component_source_create_iterator(source
);
363 fprintf(stderr
, "Failed to instantiate source iterator. Aborting...\n");
368 sink_status
= bt_component_sink_add_iterator(sink
, it
);
369 if (sink_status
!= BT_COMPONENT_STATUS_OK
) {
375 sink_status
= bt_component_sink_consume(sink
);
376 switch (sink_status
) {
377 case BT_COMPONENT_STATUS_AGAIN
:
378 /* Wait for an arbitraty 500 ms. */
381 case BT_COMPONENT_STATUS_OK
:
383 case BT_COMPONENT_STATUS_END
:
386 fprintf(stderr
, "Sink component returned an error, aborting...\n");
392 BT_PUT(component_factory
);
394 BT_PUT(source_class
);
397 BT_PUT(source_params
);
401 BT_PUT(first_plugin_path_value
);