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"
42 #include "default-cfg.h"
44 static struct bt_component_factory
*component_factory
;
47 const char *component_type_str(enum bt_component_type type
)
50 case BT_COMPONENT_TYPE_SOURCE
:
52 case BT_COMPONENT_TYPE_SINK
:
54 case BT_COMPONENT_TYPE_FILTER
:
56 case BT_COMPONENT_TYPE_UNKNOWN
:
63 void print_component_classes_found(struct bt_component_factory
*factory
)
67 if (!babeltrace_verbose
) {
71 count
= bt_component_factory_get_component_class_count(factory
);
73 fprintf(stderr
, "No component classes found. Please make sure your plug-in search path is set correctly.\n");
77 printf_verbose("Found %d component classes.\n", count
);
78 for (i
= 0; i
< count
; i
++) {
79 struct bt_component_class
*component_class
=
80 bt_component_factory_get_component_class_index(
82 struct bt_plugin
*plugin
= bt_component_class_get_plugin(
84 const char *plugin_name
= bt_plugin_get_name(plugin
);
85 const char *component_name
= bt_component_class_get_name(
87 const char *path
= bt_plugin_get_path(plugin
);
88 const char *author
= bt_plugin_get_author(plugin
);
89 const char *license
= bt_plugin_get_license(plugin
);
90 const char *plugin_description
= bt_plugin_get_description(
92 const char *component_description
=
93 bt_component_class_get_description(
95 enum bt_component_type type
= bt_component_class_get_type(
98 printf_verbose("[%s - %s (%s)]\n", plugin_name
, component_name
,
99 component_type_str(type
));
100 printf_verbose("\tpath: %s\n", path
? path
: "None");
101 printf_verbose("\tauthor: %s\n", author
? author
: "Unknown");
102 printf_verbose("\tlicense: %s\n", license
? license
: "Unknown");
103 printf_verbose("\tplugin description: %s\n",
104 plugin_description
? plugin_description
: "None");
105 printf_verbose("\tcomponent description: %s\n",
106 component_description
? component_description
: "None");
109 bt_put(component_class
);
114 void print_indent(size_t indent
)
118 for (i
= 0; i
< indent
; i
++) {
124 void print_value(struct bt_value
*, size_t, bool);
127 bool print_map_value(const char *key
, struct bt_value
*object
, void *data
)
129 size_t indent
= (size_t) data
;
131 print_indent(indent
);
132 printf_verbose("\"%s\": ", key
);
133 print_value(object
, indent
, false);
139 void print_value(struct bt_value
*value
, size_t indent
, bool do_indent
)
153 print_indent(indent
);
156 switch (bt_value_get_type(value
)) {
157 case BT_VALUE_TYPE_NULL
:
158 printf_verbose("null\n");
160 case BT_VALUE_TYPE_BOOL
:
161 bt_value_bool_get(value
, &bool_val
);
162 printf_verbose("%s\n", bool_val
? "true" : "false");
164 case BT_VALUE_TYPE_INTEGER
:
165 bt_value_integer_get(value
, &int_val
);
166 printf_verbose("%" PRId64
"\n", int_val
);
168 case BT_VALUE_TYPE_FLOAT
:
169 bt_value_float_get(value
, &dbl_val
);
170 printf_verbose("%lf\n", dbl_val
);
172 case BT_VALUE_TYPE_STRING
:
173 bt_value_string_get(value
, &str_val
);
174 printf_verbose("\"%s\"\n", str_val
);
176 case BT_VALUE_TYPE_ARRAY
:
177 size
= bt_value_array_size(value
);
178 printf_verbose("[\n");
180 for (i
= 0; i
< size
; i
++) {
181 struct bt_value
*element
=
182 bt_value_array_get(value
, i
);
184 print_value(element
, indent
+ 2, true);
188 print_indent(indent
);
189 printf_verbose("]\n");
191 case BT_VALUE_TYPE_MAP
:
192 if (bt_value_map_is_empty(value
)) {
193 printf_verbose("{}\n");
197 printf_verbose("{\n");
198 bt_value_map_foreach(value
, print_map_value
,
199 (void *) (indent
+ 2));
200 print_indent(indent
);
201 printf_verbose("}\n");
209 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
211 printf_verbose(" %s.%s\n", bt_config_component
->plugin_name
->str
,
212 bt_config_component
->component_name
->str
);
213 printf_verbose(" params:\n");
214 print_value(bt_config_component
->params
, 6, true);
218 void print_bt_config_components(GPtrArray
*array
)
222 for (i
= 0; i
< array
->len
; i
++) {
223 struct bt_config_component
*cfg_component
=
224 bt_config_get_component(array
, i
);
225 print_bt_config_component(cfg_component
);
226 BT_PUT(cfg_component
);
231 void print_cfg(struct bt_config
*cfg
)
233 printf_verbose("debug: %d\n", cfg
->debug
);
234 printf_verbose("verbose: %d\n", cfg
->verbose
);
235 printf_verbose("do list: %d\n", cfg
->do_list
);
236 printf_verbose("force correlate: %d\n", cfg
->force_correlate
);
237 printf_verbose("plugin paths:\n");
238 print_value(cfg
->plugin_paths
, 2, true);
239 printf_verbose("sources:\n");
240 print_bt_config_components(cfg
->sources
);
241 printf_verbose("sinks:\n");
242 print_bt_config_components(cfg
->sinks
);
246 struct bt_component
*create_trimmer(struct bt_config_component
*source_cfg
)
248 struct bt_component
*trimmer
= NULL
;
249 struct bt_component_class
*trimmer_class
= NULL
;
250 struct bt_value
*trimmer_params
= NULL
;
251 struct bt_value
*value
;
253 assert(component_factory
);
254 trimmer_params
= bt_value_map_create();
255 if (!trimmer_params
) {
259 value
= bt_value_map_get(source_cfg
->params
, "begin");
261 enum bt_value_status ret
;
263 ret
= bt_value_map_insert(trimmer_params
, "begin",
270 value
= bt_value_map_get(source_cfg
->params
, "end");
272 enum bt_value_status ret
;
274 ret
= bt_value_map_insert(trimmer_params
, "end",
281 value
= bt_value_map_get(source_cfg
->params
, "clock-gmt");
283 enum bt_value_status ret
;
285 ret
= bt_value_map_insert(trimmer_params
, "clock-gmt",
293 trimmer_class
= bt_component_factory_get_component_class(
294 component_factory
, "utils", BT_COMPONENT_TYPE_FILTER
,
296 if (!trimmer_class
) {
297 fprintf(stderr
, "Could not find trimmer component class. Aborting...\n");
300 trimmer
= bt_component_create(trimmer_class
, "source_trimmer",
306 bt_put(trimmer_params
);
307 bt_put(trimmer_class
);
312 int connect_source_sink(struct bt_component
*source
,
313 struct bt_config_component
*source_cfg
,
314 struct bt_component
*sink
)
317 enum bt_component_status sink_status
;
318 struct bt_component
*trimmer
= NULL
;
319 struct bt_notification_iterator
*source_it
= NULL
;
320 struct bt_notification_iterator
*to_sink_it
= NULL
;
322 source_it
= bt_component_source_create_iterator(source
);
324 fprintf(stderr
, "Failed to instantiate source iterator. Aborting...\n");
329 if (bt_value_map_has_key(source_cfg
->params
, "begin")
330 || bt_value_map_has_key(source_cfg
->params
, "end")) {
331 /* A trimmer must be inserted in the graph. */
332 enum bt_component_status trimmer_status
;
334 trimmer
= create_trimmer(source_cfg
);
336 fprintf(stderr
, "Failed to create trimmer component. Aborting...\n");
341 trimmer_status
= bt_component_filter_add_iterator(trimmer
,
344 if (trimmer_status
!= BT_COMPONENT_STATUS_OK
) {
345 fprintf(stderr
, "Failed to connect source to trimmer. Aborting...\n");
350 to_sink_it
= bt_component_filter_create_iterator(trimmer
);
352 fprintf(stderr
, "Failed to instantiate trimmer iterator. Aborting...\n");
357 BT_MOVE(to_sink_it
, source_it
);
360 sink_status
= bt_component_sink_add_iterator(sink
, to_sink_it
);
361 if (sink_status
!= BT_COMPONENT_STATUS_OK
) {
362 fprintf(stderr
, "Failed to connect to sink component. Aborting...\n");
373 static int load_plugins(struct bt_config
*cfg
)
377 nr_paths
= bt_value_array_size(cfg
->plugin_paths
);
381 for (i
= 0; i
< nr_paths
; i
++) {
382 struct bt_value
*plugin_path_value
= NULL
;
383 const char *plugin_path
;
385 plugin_path_value
= bt_value_array_get(cfg
->plugin_paths
, i
);
386 if (bt_value_string_get(plugin_path_value
,
388 BT_PUT(plugin_path_value
);
391 if (bt_component_factory_load_recursive(component_factory
,
393 printf_debug("Unable to dynamically load plugins from path %s.\n",
396 BT_PUT(plugin_path_value
);
401 int main(int argc
, const char **argv
)
404 struct bt_component_class
*source_class
= NULL
;
405 struct bt_component_class
*sink_class
= NULL
;
406 struct bt_component
*source
= NULL
, *sink
= NULL
;
407 struct bt_value
*source_params
= NULL
, *sink_params
= NULL
;
408 struct bt_config
*cfg
;
409 enum bt_component_status sink_status
;
410 struct bt_config_component
*source_cfg
= NULL
, *sink_cfg
= NULL
;
412 cfg
= bt_config_create();
414 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
419 ret
= set_default_config(cfg
);
424 ret
= bt_config_init_from_args(cfg
, argc
, argv
);
426 babeltrace_verbose
= cfg
->verbose
;
427 babeltrace_debug
= cfg
->debug
;
433 /* TODO handle more than 1 source and 1 sink. */
434 if (cfg
->sources
->len
!= 1 || cfg
->sinks
->len
!= 1) {
439 printf_verbose("Verbose mode active.\n");
440 printf_debug("Debug mode active.\n");
441 component_factory
= bt_component_factory_create();
442 if (!component_factory
) {
443 fprintf(stderr
, "Failed to create component factory.\n");
448 if (load_plugins(cfg
)) {
449 fprintf(stderr
, "Failed to load plugins.\n");
454 ret
= bt_component_factory_load_static(component_factory
);
456 fprintf(stderr
, "Failed to load static plugins.\n");
460 print_component_classes_found(component_factory
);
462 source_cfg
= bt_config_get_component(cfg
->sources
, 0);
463 source_params
= bt_get(source_cfg
->params
);
464 source_class
= bt_component_factory_get_component_class(
465 component_factory
, source_cfg
->plugin_name
->str
,
466 BT_COMPONENT_TYPE_SOURCE
,
467 source_cfg
->component_name
->str
);
469 fprintf(stderr
, "Could not find %s.%s source component class. Aborting...\n",
470 source_cfg
->plugin_name
->str
,
471 source_cfg
->component_name
->str
);
476 sink_cfg
= bt_config_get_component(cfg
->sinks
, 0);
477 sink_params
= bt_get(sink_cfg
->params
);
478 sink_class
= bt_component_factory_get_component_class(component_factory
,
479 sink_cfg
->plugin_name
->str
, BT_COMPONENT_TYPE_SINK
,
480 sink_cfg
->component_name
->str
);
482 fprintf(stderr
, "Could not find %s.%s output component class. Aborting...\n",
483 sink_cfg
->plugin_name
->str
,
484 sink_cfg
->component_name
->str
);
489 source
= bt_component_create(source_class
, "source", source_params
);
491 fprintf(stderr
, "Failed to instantiate selected source component. Aborting...\n");
496 sink
= bt_component_create(sink_class
, "sink", sink_params
);
498 fprintf(stderr
, "Failed to instantiate selected output component. Aborting...\n");
503 ret
= connect_source_sink(source
, source_cfg
, sink
);
509 sink_status
= bt_component_sink_consume(sink
);
510 switch (sink_status
) {
511 case BT_COMPONENT_STATUS_AGAIN
:
512 /* Wait for an arbitraty 500 ms. */
515 case BT_COMPONENT_STATUS_OK
:
517 case BT_COMPONENT_STATUS_END
:
520 fprintf(stderr
, "Sink component returned an error, aborting...\n");
526 BT_PUT(component_factory
);
528 BT_PUT(source_class
);
531 BT_PUT(source_params
);