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"
43 static struct bt_component_factory
*component_factory
;
46 const char *component_type_str(enum bt_component_type type
)
49 case BT_COMPONENT_TYPE_SOURCE
:
51 case BT_COMPONENT_TYPE_SINK
:
53 case BT_COMPONENT_TYPE_FILTER
:
55 case BT_COMPONENT_TYPE_UNKNOWN
:
62 void print_component_classes_found(struct bt_component_factory
*factory
)
66 if (!babeltrace_verbose
) {
70 count
= bt_component_factory_get_component_class_count(factory
);
72 fprintf(stderr
, "No component classes found. Please make sure your plug-in search path is set correctly.\n");
76 printf_verbose("Found %d component classes.\n", count
);
77 for (i
= 0; i
< count
; i
++) {
78 struct bt_component_class
*component_class
=
79 bt_component_factory_get_component_class_index(
81 struct bt_plugin
*plugin
= bt_component_class_get_plugin(
83 const char *plugin_name
= bt_plugin_get_name(plugin
);
84 const char *component_name
= bt_component_class_get_name(
86 const char *path
= bt_plugin_get_path(plugin
);
87 const char *author
= bt_plugin_get_author(plugin
);
88 const char *license
= bt_plugin_get_license(plugin
);
89 const char *plugin_description
= bt_plugin_get_description(
91 const char *component_description
=
92 bt_component_class_get_description(
94 enum bt_component_type type
= bt_component_class_get_type(
97 printf_verbose("[%s - %s (%s)]\n", plugin_name
, component_name
,
98 component_type_str(type
));
99 printf_verbose("\tpath: %s\n", path
? path
: "None");
100 printf_verbose("\tauthor: %s\n", author
? author
: "Unknown");
101 printf_verbose("\tlicense: %s\n", license
? license
: "Unknown");
102 printf_verbose("\tplugin description: %s\n",
103 plugin_description
? plugin_description
: "None");
104 printf_verbose("\tcomponent description: %s\n",
105 component_description
? component_description
: "None");
108 bt_put(component_class
);
113 void print_indent(size_t indent
)
117 for (i
= 0; i
< indent
; i
++) {
123 void print_value(struct bt_value
*, size_t, bool);
126 bool print_map_value(const char *key
, struct bt_value
*object
, void *data
)
128 size_t indent
= (size_t) data
;
130 print_indent(indent
);
131 printf("\"%s\": ", key
);
132 print_value(object
, indent
, false);
138 void print_value(struct bt_value
*value
, size_t indent
, bool do_indent
)
152 print_indent(indent
);
155 switch (bt_value_get_type(value
)) {
156 case BT_VALUE_TYPE_NULL
:
159 case BT_VALUE_TYPE_BOOL
:
160 bt_value_bool_get(value
, &bool_val
);
161 printf("%s\n", bool_val
? "true" : "false");
163 case BT_VALUE_TYPE_INTEGER
:
164 bt_value_integer_get(value
, &int_val
);
165 printf("%" PRId64
"\n", int_val
);
167 case BT_VALUE_TYPE_FLOAT
:
168 bt_value_float_get(value
, &dbl_val
);
169 printf("%lf\n", dbl_val
);
171 case BT_VALUE_TYPE_STRING
:
172 bt_value_string_get(value
, &str_val
);
173 printf("\"%s\"\n", str_val
);
175 case BT_VALUE_TYPE_ARRAY
:
176 size
= bt_value_array_size(value
);
179 for (i
= 0; i
< size
; i
++) {
180 struct bt_value
*element
=
181 bt_value_array_get(value
, i
);
183 print_value(element
, indent
+ 2, true);
187 print_indent(indent
);
190 case BT_VALUE_TYPE_MAP
:
191 if (bt_value_map_is_empty(value
)) {
197 bt_value_map_foreach(value
, print_map_value
,
198 (void *) (indent
+ 2));
199 print_indent(indent
);
208 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
210 printf(" %s.%s\n", bt_config_component
->plugin_name
->str
,
211 bt_config_component
->component_name
->str
);
212 printf(" params:\n");
213 print_value(bt_config_component
->params
, 6, true);
217 void print_bt_config_components(GPtrArray
*array
)
221 for (i
= 0; i
< array
->len
; i
++) {
222 struct bt_config_component
*cfg_component
=
223 bt_config_get_component(array
, i
);
224 print_bt_config_component(cfg_component
);
225 BT_PUT(cfg_component
);
230 void print_cfg(struct bt_config
*cfg
)
232 printf("debug: %d\n", cfg
->debug
);
233 printf("verbose: %d\n", cfg
->verbose
);
234 printf("do list: %d\n", cfg
->do_list
);
235 printf("force correlate: %d\n", cfg
->force_correlate
);
236 printf("plugin paths:\n");
237 print_value(cfg
->plugin_paths
, 2, true);
238 printf("sources:\n");
239 print_bt_config_components(cfg
->sources
);
241 print_bt_config_components(cfg
->sinks
);
245 struct bt_component
*create_trimmer(struct bt_config_component
*source_cfg
)
247 struct bt_component
*trimmer
= NULL
;
248 struct bt_component_class
*trimmer_class
= NULL
;
249 struct bt_value
*trimmer_params
= NULL
;
250 struct bt_value
*value
;
252 assert(component_factory
);
253 trimmer_params
= bt_value_map_create();
254 if (!trimmer_params
) {
258 value
= bt_value_map_get(source_cfg
->params
, "begin");
260 enum bt_value_status ret
;
262 ret
= bt_value_map_insert(trimmer_params
, "begin",
269 value
= bt_value_map_get(source_cfg
->params
, "end");
271 enum bt_value_status ret
;
273 ret
= bt_value_map_insert(trimmer_params
, "end",
280 value
= bt_value_map_get(source_cfg
->params
, "clock-gmt");
282 enum bt_value_status ret
;
284 ret
= bt_value_map_insert(trimmer_params
, "clock-gmt",
292 trimmer_class
= bt_component_factory_get_component_class(
293 component_factory
, "utils", BT_COMPONENT_TYPE_FILTER
,
295 if (!trimmer_class
) {
296 fprintf(stderr
, "Could not find trimmer component class. Aborting...\n");
299 trimmer
= bt_component_create(trimmer_class
, "source_trimmer",
305 bt_put(trimmer_params
);
306 bt_put(trimmer_class
);
311 int connect_source_sink(struct bt_component
*source
,
312 struct bt_config_component
*source_cfg
,
313 struct bt_component
*sink
)
316 enum bt_component_status sink_status
;
317 struct bt_component
*trimmer
= NULL
;
318 struct bt_notification_iterator
*source_it
= NULL
;
319 struct bt_notification_iterator
*to_sink_it
= NULL
;
321 source_it
= bt_component_source_create_iterator(source
);
323 fprintf(stderr
, "Failed to instantiate source iterator. Aborting...\n");
328 if (bt_value_map_has_key(source_cfg
->params
, "begin")
329 || bt_value_map_has_key(source_cfg
->params
, "end")) {
330 /* A trimmer must be inserted in the graph. */
331 enum bt_component_status trimmer_status
;
333 trimmer
= create_trimmer(source_cfg
);
335 fprintf(stderr
, "Failed to create trimmer component. Aborting...\n");
340 trimmer_status
= bt_component_filter_add_iterator(trimmer
,
343 if (trimmer_status
!= BT_COMPONENT_STATUS_OK
) {
344 fprintf(stderr
, "Failed to connect source to trimmer. Aborting...\n");
349 to_sink_it
= bt_component_filter_create_iterator(trimmer
);
351 fprintf(stderr
, "Failed to instantiate trimmer iterator. Aborting...\n");
356 BT_MOVE(to_sink_it
, source_it
);
359 sink_status
= bt_component_sink_add_iterator(sink
, to_sink_it
);
360 if (sink_status
!= BT_COMPONENT_STATUS_OK
) {
361 fprintf(stderr
, "Failed to connect to sink component. Aborting...\n");
372 int main(int argc
, const char **argv
)
375 struct bt_component_class
*source_class
= NULL
;
376 struct bt_component_class
*sink_class
= NULL
;
377 struct bt_component
*source
= NULL
, *sink
= NULL
;
378 struct bt_value
*source_params
= NULL
, *sink_params
= NULL
;
379 struct bt_config
*cfg
;
380 enum bt_component_status sink_status
;
381 struct bt_value
*first_plugin_path_value
= NULL
;
382 const char *first_plugin_path
;
383 struct bt_config_component
*source_cfg
= NULL
, *sink_cfg
= NULL
;
385 cfg
= bt_config_from_args(argc
, argv
, &ret
);
392 babeltrace_verbose
= cfg
->verbose
;
393 babeltrace_debug
= cfg
->debug
;
395 /* TODO handle more than 1 source and 1 sink. */
396 if (cfg
->sources
->len
!= 1 || cfg
->sinks
->len
!= 1) {
397 fprintf(stderr
, "Unexpected configuration, aborting...\n");
402 printf_verbose("Verbose mode active.\n");
403 printf_debug("Debug mode active.\n");
404 component_factory
= bt_component_factory_create();
405 if (!component_factory
) {
406 fprintf(stderr
, "Failed to create component factory.\n");
411 if (cfg
->plugin_paths
&& !bt_value_array_is_empty(cfg
->plugin_paths
)) {
412 first_plugin_path_value
= bt_value_array_get(
413 cfg
->plugin_paths
, 0);
414 bt_value_string_get(first_plugin_path_value
,
416 ret
= bt_component_factory_load_recursive(component_factory
,
419 fprintf(stderr
, "Failed to dynamically load plugins.\n");
424 ret
= bt_component_factory_load_static(component_factory
);
426 fprintf(stderr
, "Failed to load static plugins.\n");
430 print_component_classes_found(component_factory
);
432 source_cfg
= bt_config_get_component(cfg
->sources
, 0);
433 source_params
= bt_get(source_cfg
->params
);
434 source_class
= bt_component_factory_get_component_class(
435 component_factory
, source_cfg
->plugin_name
->str
,
436 BT_COMPONENT_TYPE_SOURCE
,
437 source_cfg
->component_name
->str
);
439 fprintf(stderr
, "Could not find %s.%s source component class. Aborting...\n",
440 source_cfg
->plugin_name
->str
,
441 source_cfg
->component_name
->str
);
446 sink_cfg
= bt_config_get_component(cfg
->sinks
, 0);
447 sink_params
= bt_get(sink_cfg
->params
);
448 sink_class
= bt_component_factory_get_component_class(component_factory
,
449 sink_cfg
->plugin_name
->str
, BT_COMPONENT_TYPE_SINK
,
450 sink_cfg
->component_name
->str
);
452 fprintf(stderr
, "Could not find %s.%s output component class. Aborting...\n",
453 sink_cfg
->plugin_name
->str
,
454 sink_cfg
->component_name
->str
);
459 source
= bt_component_create(source_class
, "source", source_params
);
461 fprintf(stderr
, "Failed to instantiate selected source component. Aborting...\n");
466 sink
= bt_component_create(sink_class
, "sink", sink_params
);
468 fprintf(stderr
, "Failed to instantiate selected output component. Aborting...\n");
473 ret
= connect_source_sink(source
, source_cfg
, sink
);
479 sink_status
= bt_component_sink_consume(sink
);
480 switch (sink_status
) {
481 case BT_COMPONENT_STATUS_AGAIN
:
482 /* Wait for an arbitraty 500 ms. */
485 case BT_COMPONENT_STATUS_OK
:
487 case BT_COMPONENT_STATUS_END
:
490 fprintf(stderr
, "Sink component returned an error, aborting...\n");
496 BT_PUT(component_factory
);
498 BT_PUT(source_class
);
501 BT_PUT(source_params
);
504 BT_PUT(first_plugin_path_value
);