Commit | Line | Data |
---|---|---|
34ac0e6c MD |
1 | /* |
2 | * babeltrace.c | |
3 | * | |
4 | * Babeltrace Trace Converter | |
5 | * | |
64fa3fec MD |
6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
7 | * | |
8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
34ac0e6c MD |
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. | |
c462e188 MD |
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. | |
34ac0e6c | 27 | */ |
4c8bfb7e | 28 | |
95d36295 | 29 | #include <babeltrace/babeltrace.h> |
7c7c0433 | 30 | #include <babeltrace/plugin/plugin.h> |
33b34c43 | 31 | #include <babeltrace/component/component.h> |
d71dcf2c PP |
32 | #include <babeltrace/component/component-source.h> |
33 | #include <babeltrace/component/component-sink.h> | |
34 | #include <babeltrace/component/component-filter.h> | |
33b34c43 PP |
35 | #include <babeltrace/component/component-class.h> |
36 | #include <babeltrace/component/notification/iterator.h> | |
2e339de1 JG |
37 | #include <babeltrace/ref.h> |
38 | #include <babeltrace/values.h> | |
34ac0e6c | 39 | #include <stdlib.h> |
a44bc4c9 | 40 | #include <babeltrace/ctf-ir/metadata.h> /* for clocks */ |
7f26a816 PP |
41 | #include <popt.h> |
42 | #include <string.h> | |
43 | #include <stdio.h> | |
33b34c43 | 44 | #include <glib.h> |
c42c79ea | 45 | #include "babeltrace-cfg.h" |
c1870f57 | 46 | #include "default-cfg.h" |
34ac0e6c | 47 | |
33b34c43 PP |
48 | GPtrArray *loaded_plugins; |
49 | ||
50 | static | |
51 | void init_loaded_plugins_array(void) | |
52 | { | |
53 | loaded_plugins = g_ptr_array_new_full(8, bt_put); | |
54 | } | |
55 | ||
56 | static | |
57 | void fini_loaded_plugins_array(void) | |
58 | { | |
59 | g_ptr_array_free(loaded_plugins, TRUE); | |
60 | } | |
61 | ||
62 | static | |
63 | struct bt_plugin *find_plugin(const char *name) | |
64 | { | |
65 | int i; | |
66 | struct bt_plugin *plugin = NULL; | |
67 | ||
68 | for (i = 0; i < loaded_plugins->len; i++) { | |
69 | plugin = g_ptr_array_index(loaded_plugins, i); | |
70 | ||
71 | if (strcmp(name, bt_plugin_get_name(plugin)) == 0) { | |
72 | break; | |
73 | } | |
74 | ||
75 | plugin = NULL; | |
76 | } | |
77 | ||
78 | return bt_get(plugin); | |
79 | } | |
80 | ||
81 | static | |
82 | struct bt_component_class *find_component_class(const char *plugin_name, | |
83 | const char *comp_class_name, | |
d3e4dcd8 | 84 | enum bt_component_class_type comp_class_type) |
33b34c43 PP |
85 | { |
86 | struct bt_component_class *comp_class = NULL; | |
87 | struct bt_plugin *plugin = find_plugin(plugin_name); | |
88 | ||
89 | if (!plugin) { | |
90 | goto end; | |
91 | } | |
92 | ||
93 | comp_class = bt_plugin_get_component_class_by_name_and_type(plugin, | |
94 | comp_class_name, comp_class_type); | |
95 | BT_PUT(plugin); | |
96 | end: | |
97 | return comp_class; | |
98 | } | |
6c2f3ee5 | 99 | |
7c7c0433 | 100 | static |
d3e4dcd8 | 101 | const char *component_type_str(enum bt_component_class_type type) |
7c7c0433 JG |
102 | { |
103 | switch (type) { | |
d3e4dcd8 | 104 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
7c7c0433 | 105 | return "source"; |
d3e4dcd8 | 106 | case BT_COMPONENT_CLASS_TYPE_SINK: |
7c7c0433 | 107 | return "sink"; |
d3e4dcd8 | 108 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
7c7c0433 | 109 | return "filter"; |
d3e4dcd8 | 110 | case BT_COMPONENT_CLASS_TYPE_UNKNOWN: |
7c7c0433 JG |
111 | default: |
112 | return "unknown"; | |
113 | } | |
114 | } | |
115 | ||
116 | static | |
33b34c43 | 117 | void print_component_classes_found(void) |
7c7c0433 | 118 | { |
33b34c43 | 119 | int plugins_count, component_classes_count = 0, i; |
7c7c0433 JG |
120 | |
121 | if (!babeltrace_verbose) { | |
122 | return; | |
123 | } | |
124 | ||
33b34c43 PP |
125 | plugins_count = loaded_plugins->len; |
126 | if (plugins_count == 0) { | |
127 | fprintf(stderr, "No plugins found. Please make sure your plug-in search path is set correctly.\n"); | |
7c7c0433 JG |
128 | return; |
129 | } | |
130 | ||
33b34c43 PP |
131 | for (i = 0; i < plugins_count; i++) { |
132 | struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i); | |
133 | ||
134 | component_classes_count += bt_plugin_get_component_class_count(plugin); | |
135 | } | |
0cc9e945 | 136 | |
33b34c43 PP |
137 | printf_verbose("Found %d component classes in %d plugins.\n", |
138 | component_classes_count, plugins_count); | |
139 | ||
140 | for (i = 0; i < plugins_count; i++) { | |
141 | int j; | |
142 | struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i); | |
90b5d437 PP |
143 | unsigned int major, minor, patch; |
144 | const char *extra; | |
145 | enum bt_plugin_status version_status; | |
33b34c43 PP |
146 | |
147 | component_classes_count = | |
148 | bt_plugin_get_component_class_count(plugin); | |
90b5d437 PP |
149 | version_status = bt_plugin_get_version(plugin, &major, &minor, |
150 | &patch, &extra); | |
33b34c43 PP |
151 | |
152 | for (j = 0; j < component_classes_count; j++) { | |
153 | struct bt_component_class *comp_class = | |
154 | bt_plugin_get_component_class(plugin, j); | |
155 | const char *plugin_name = bt_plugin_get_name(plugin); | |
156 | const char *comp_class_name = | |
157 | bt_component_class_get_name(comp_class); | |
158 | const char *path = bt_plugin_get_path(plugin); | |
159 | const char *author = bt_plugin_get_author(plugin); | |
160 | const char *license = bt_plugin_get_license(plugin); | |
161 | const char *plugin_description = | |
162 | bt_plugin_get_description(plugin); | |
163 | const char *comp_class_description = | |
164 | bt_component_class_get_description(comp_class); | |
d3e4dcd8 | 165 | enum bt_component_class_type type = |
33b34c43 PP |
166 | bt_component_class_get_type(comp_class); |
167 | ||
168 | printf_verbose("[%s - %s (%s)]\n", plugin_name, | |
169 | comp_class_name, component_type_str(type)); | |
170 | printf_verbose("\tpath: %s\n", path ? path : "None"); | |
171 | printf_verbose("\tauthor: %s\n", | |
172 | author ? author : "Unknown"); | |
173 | printf_verbose("\tlicense: %s\n", | |
174 | license ? license : "Unknown"); | |
175 | printf_verbose("\tplugin description: %s\n", | |
176 | plugin_description ? plugin_description : "None"); | |
90b5d437 PP |
177 | |
178 | if (version_status == BT_PLUGIN_STATUS_OK) { | |
179 | printf_verbose("\tplugin version: %u.%u.%u", | |
180 | major, minor, patch); | |
181 | ||
182 | if (extra) { | |
183 | printf("%s", extra); | |
184 | } | |
185 | ||
186 | printf("\n"); | |
187 | } | |
188 | ||
33b34c43 PP |
189 | printf_verbose("\tcomponent description: %s\n", |
190 | comp_class_description ? comp_class_description : "None"); | |
191 | bt_put(comp_class); | |
192 | } | |
7c7c0433 JG |
193 | } |
194 | } | |
195 | ||
c42c79ea PP |
196 | static |
197 | void print_indent(size_t indent) | |
198 | { | |
199 | size_t i; | |
200 | ||
201 | for (i = 0; i < indent; i++) { | |
8d24f1c0 | 202 | printf_verbose(" "); |
c42c79ea PP |
203 | } |
204 | } | |
205 | ||
206 | static | |
207 | void print_value(struct bt_value *, size_t, bool); | |
208 | ||
209 | static | |
210 | bool print_map_value(const char *key, struct bt_value *object, void *data) | |
211 | { | |
212 | size_t indent = (size_t) data; | |
213 | ||
214 | print_indent(indent); | |
8d24f1c0 | 215 | printf_verbose("\"%s\": ", key); |
c42c79ea PP |
216 | print_value(object, indent, false); |
217 | ||
218 | return true; | |
219 | } | |
220 | ||
221 | static | |
222 | void print_value(struct bt_value *value, size_t indent, bool do_indent) | |
223 | { | |
224 | bool bool_val; | |
225 | int64_t int_val; | |
226 | double dbl_val; | |
227 | const char *str_val; | |
228 | int size; | |
229 | int i; | |
230 | ||
231 | if (!value) { | |
232 | return; | |
233 | } | |
234 | ||
235 | if (do_indent) { | |
236 | print_indent(indent); | |
237 | } | |
238 | ||
239 | switch (bt_value_get_type(value)) { | |
240 | case BT_VALUE_TYPE_NULL: | |
8d24f1c0 | 241 | printf_verbose("null\n"); |
c42c79ea PP |
242 | break; |
243 | case BT_VALUE_TYPE_BOOL: | |
244 | bt_value_bool_get(value, &bool_val); | |
8d24f1c0 | 245 | printf_verbose("%s\n", bool_val ? "true" : "false"); |
c42c79ea PP |
246 | break; |
247 | case BT_VALUE_TYPE_INTEGER: | |
248 | bt_value_integer_get(value, &int_val); | |
8d24f1c0 | 249 | printf_verbose("%" PRId64 "\n", int_val); |
c42c79ea PP |
250 | break; |
251 | case BT_VALUE_TYPE_FLOAT: | |
252 | bt_value_float_get(value, &dbl_val); | |
8d24f1c0 | 253 | printf_verbose("%lf\n", dbl_val); |
c42c79ea PP |
254 | break; |
255 | case BT_VALUE_TYPE_STRING: | |
256 | bt_value_string_get(value, &str_val); | |
8d24f1c0 | 257 | printf_verbose("\"%s\"\n", str_val); |
c42c79ea PP |
258 | break; |
259 | case BT_VALUE_TYPE_ARRAY: | |
260 | size = bt_value_array_size(value); | |
8d24f1c0 | 261 | printf_verbose("[\n"); |
c42c79ea PP |
262 | |
263 | for (i = 0; i < size; i++) { | |
264 | struct bt_value *element = | |
265 | bt_value_array_get(value, i); | |
266 | ||
267 | print_value(element, indent + 2, true); | |
268 | BT_PUT(element); | |
269 | } | |
270 | ||
271 | print_indent(indent); | |
8d24f1c0 | 272 | printf_verbose("]\n"); |
c42c79ea PP |
273 | break; |
274 | case BT_VALUE_TYPE_MAP: | |
275 | if (bt_value_map_is_empty(value)) { | |
8d24f1c0 | 276 | printf_verbose("{}\n"); |
c42c79ea PP |
277 | return; |
278 | } | |
279 | ||
8d24f1c0 | 280 | printf_verbose("{\n"); |
c42c79ea PP |
281 | bt_value_map_foreach(value, print_map_value, |
282 | (void *) (indent + 2)); | |
283 | print_indent(indent); | |
8d24f1c0 | 284 | printf_verbose("}\n"); |
c42c79ea PP |
285 | break; |
286 | default: | |
287 | assert(false); | |
288 | } | |
289 | } | |
290 | ||
291 | static | |
292 | void print_bt_config_component(struct bt_config_component *bt_config_component) | |
293 | { | |
8d24f1c0 | 294 | printf_verbose(" %s.%s\n", bt_config_component->plugin_name->str, |
c42c79ea | 295 | bt_config_component->component_name->str); |
8d24f1c0 | 296 | printf_verbose(" params:\n"); |
c42c79ea PP |
297 | print_value(bt_config_component->params, 6, true); |
298 | } | |
299 | ||
300 | static | |
301 | void print_bt_config_components(GPtrArray *array) | |
302 | { | |
303 | size_t i; | |
304 | ||
305 | for (i = 0; i < array->len; i++) { | |
306 | struct bt_config_component *cfg_component = | |
e5bc7f81 | 307 | bt_config_get_component(array, i); |
c42c79ea PP |
308 | print_bt_config_component(cfg_component); |
309 | BT_PUT(cfg_component); | |
310 | } | |
311 | } | |
312 | ||
313 | static | |
314 | void print_cfg(struct bt_config *cfg) | |
315 | { | |
8d24f1c0 JG |
316 | printf_verbose("debug: %d\n", cfg->debug); |
317 | printf_verbose("verbose: %d\n", cfg->verbose); | |
318 | printf_verbose("do list: %d\n", cfg->do_list); | |
319 | printf_verbose("force correlate: %d\n", cfg->force_correlate); | |
320 | printf_verbose("plugin paths:\n"); | |
c42c79ea | 321 | print_value(cfg->plugin_paths, 2, true); |
8d24f1c0 | 322 | printf_verbose("sources:\n"); |
c42c79ea | 323 | print_bt_config_components(cfg->sources); |
8d24f1c0 | 324 | printf_verbose("sinks:\n"); |
c42c79ea PP |
325 | print_bt_config_components(cfg->sinks); |
326 | } | |
327 | ||
6c2f3ee5 JG |
328 | static |
329 | struct bt_component *create_trimmer(struct bt_config_component *source_cfg) | |
330 | { | |
331 | struct bt_component *trimmer = NULL; | |
332 | struct bt_component_class *trimmer_class = NULL; | |
333 | struct bt_value *trimmer_params = NULL; | |
528debdf | 334 | struct bt_value *value; |
6c2f3ee5 | 335 | |
6c2f3ee5 JG |
336 | trimmer_params = bt_value_map_create(); |
337 | if (!trimmer_params) { | |
338 | goto end; | |
339 | } | |
340 | ||
528debdf MD |
341 | value = bt_value_map_get(source_cfg->params, "begin"); |
342 | if (value) { | |
6c2f3ee5 | 343 | enum bt_value_status ret; |
6c2f3ee5 | 344 | |
528debdf | 345 | ret = bt_value_map_insert(trimmer_params, "begin", |
6c2f3ee5 JG |
346 | value); |
347 | BT_PUT(value); | |
348 | if (ret) { | |
349 | goto end; | |
350 | } | |
351 | } | |
528debdf MD |
352 | value = bt_value_map_get(source_cfg->params, "end"); |
353 | if (value) { | |
6c2f3ee5 | 354 | enum bt_value_status ret; |
6c2f3ee5 | 355 | |
528debdf MD |
356 | ret = bt_value_map_insert(trimmer_params, "end", |
357 | value); | |
358 | BT_PUT(value); | |
359 | if (ret) { | |
6c2f3ee5 JG |
360 | goto end; |
361 | } | |
528debdf MD |
362 | } |
363 | value = bt_value_map_get(source_cfg->params, "clock-gmt"); | |
364 | if (value) { | |
365 | enum bt_value_status ret; | |
6c2f3ee5 | 366 | |
528debdf | 367 | ret = bt_value_map_insert(trimmer_params, "clock-gmt", |
6c2f3ee5 JG |
368 | value); |
369 | BT_PUT(value); | |
370 | if (ret) { | |
371 | goto end; | |
372 | } | |
373 | } | |
374 | ||
33b34c43 | 375 | trimmer_class = find_component_class("utils", "trimmer", |
d3e4dcd8 | 376 | BT_COMPONENT_CLASS_TYPE_FILTER); |
6c2f3ee5 JG |
377 | if (!trimmer_class) { |
378 | fprintf(stderr, "Could not find trimmer component class. Aborting...\n"); | |
379 | goto end; | |
380 | } | |
381 | trimmer = bt_component_create(trimmer_class, "source_trimmer", | |
382 | trimmer_params); | |
383 | if (!trimmer) { | |
384 | goto end; | |
385 | } | |
386 | end: | |
387 | bt_put(trimmer_params); | |
388 | bt_put(trimmer_class); | |
389 | return trimmer; | |
390 | } | |
391 | ||
392 | static | |
393 | int connect_source_sink(struct bt_component *source, | |
394 | struct bt_config_component *source_cfg, | |
395 | struct bt_component *sink) | |
396 | { | |
397 | int ret = 0; | |
398 | enum bt_component_status sink_status; | |
399 | struct bt_component *trimmer = NULL; | |
400 | struct bt_notification_iterator *source_it = NULL; | |
401 | struct bt_notification_iterator *to_sink_it = NULL; | |
402 | ||
c725abdd | 403 | source_it = bt_component_source_create_notification_iterator(source); |
6c2f3ee5 JG |
404 | if (!source_it) { |
405 | fprintf(stderr, "Failed to instantiate source iterator. Aborting...\n"); | |
406 | ret = -1; | |
407 | goto end; | |
408 | } | |
409 | ||
528debdf MD |
410 | if (bt_value_map_has_key(source_cfg->params, "begin") |
411 | || bt_value_map_has_key(source_cfg->params, "end")) { | |
6c2f3ee5 JG |
412 | /* A trimmer must be inserted in the graph. */ |
413 | enum bt_component_status trimmer_status; | |
414 | ||
415 | trimmer = create_trimmer(source_cfg); | |
416 | if (!trimmer) { | |
417 | fprintf(stderr, "Failed to create trimmer component. Aborting...\n"); | |
418 | ret = -1; | |
419 | goto end; | |
420 | } | |
421 | ||
422 | trimmer_status = bt_component_filter_add_iterator(trimmer, | |
423 | source_it); | |
424 | BT_PUT(source_it); | |
425 | if (trimmer_status != BT_COMPONENT_STATUS_OK) { | |
426 | fprintf(stderr, "Failed to connect source to trimmer. Aborting...\n"); | |
427 | ret = -1; | |
428 | goto end; | |
429 | } | |
430 | ||
c725abdd | 431 | to_sink_it = bt_component_filter_create_notification_iterator(trimmer); |
6c2f3ee5 JG |
432 | if (!to_sink_it) { |
433 | fprintf(stderr, "Failed to instantiate trimmer iterator. Aborting...\n"); | |
434 | ret = -1; | |
435 | goto end; | |
436 | } | |
437 | } else { | |
438 | BT_MOVE(to_sink_it, source_it); | |
439 | } | |
440 | ||
441 | sink_status = bt_component_sink_add_iterator(sink, to_sink_it); | |
442 | if (sink_status != BT_COMPONENT_STATUS_OK) { | |
443 | fprintf(stderr, "Failed to connect to sink component. Aborting...\n"); | |
444 | ret = -1; | |
445 | goto end; | |
446 | } | |
447 | end: | |
448 | bt_put(trimmer); | |
449 | bt_put(source_it); | |
450 | bt_put(to_sink_it); | |
451 | return ret; | |
452 | } | |
453 | ||
33b34c43 PP |
454 | static |
455 | void add_to_loaded_plugins(struct bt_plugin **plugins) | |
98ecef32 | 456 | { |
33b34c43 PP |
457 | while (*plugins) { |
458 | struct bt_plugin *plugin = *plugins; | |
459 | /* Check if it's already loaded (from another path). */ | |
460 | struct bt_plugin *loaded_plugin = | |
461 | find_plugin(bt_plugin_get_name(plugin)); | |
462 | ||
463 | if (loaded_plugin) { | |
464 | printf_verbose("Not loading plugin `%s`: already loaded from `%s`\n", | |
465 | bt_plugin_get_path(plugin), | |
466 | bt_plugin_get_path(loaded_plugin)); | |
467 | BT_PUT(loaded_plugin); | |
468 | BT_PUT(plugin); | |
469 | } else { | |
470 | /* Transfer ownership to global array. */ | |
471 | g_ptr_array_add(loaded_plugins, plugin); | |
472 | } | |
473 | *(plugins++) = NULL; | |
474 | } | |
475 | } | |
476 | ||
477 | static | |
478 | int load_dynamic_plugins(struct bt_config *cfg) | |
479 | { | |
480 | int nr_paths, i, ret = 0; | |
98ecef32 MD |
481 | |
482 | nr_paths = bt_value_array_size(cfg->plugin_paths); | |
483 | if (nr_paths < 0) { | |
33b34c43 PP |
484 | ret = -1; |
485 | goto end; | |
98ecef32 | 486 | } |
33b34c43 | 487 | |
98ecef32 MD |
488 | for (i = 0; i < nr_paths; i++) { |
489 | struct bt_value *plugin_path_value = NULL; | |
490 | const char *plugin_path; | |
33b34c43 | 491 | struct bt_plugin **plugins; |
98ecef32 MD |
492 | |
493 | plugin_path_value = bt_value_array_get(cfg->plugin_paths, i); | |
494 | if (bt_value_string_get(plugin_path_value, | |
495 | &plugin_path)) { | |
496 | BT_PUT(plugin_path_value); | |
497 | continue; | |
498 | } | |
33b34c43 PP |
499 | |
500 | plugins = bt_plugin_create_all_from_dir(plugin_path, true); | |
501 | if (!plugins) { | |
98ecef32 MD |
502 | printf_debug("Unable to dynamically load plugins from path %s.\n", |
503 | plugin_path); | |
33b34c43 PP |
504 | BT_PUT(plugin_path_value); |
505 | continue; | |
98ecef32 | 506 | } |
33b34c43 PP |
507 | |
508 | add_to_loaded_plugins(plugins); | |
509 | free(plugins); | |
510 | ||
98ecef32 MD |
511 | BT_PUT(plugin_path_value); |
512 | } | |
33b34c43 PP |
513 | end: |
514 | return ret; | |
515 | } | |
516 | ||
517 | static | |
518 | int load_static_plugins(void) | |
519 | { | |
520 | int ret = 0; | |
521 | struct bt_plugin **plugins; | |
522 | ||
523 | plugins = bt_plugin_create_all_from_static(); | |
524 | if (!plugins) { | |
525 | printf_debug("Unable to load static plugins.\n"); | |
526 | ret = -1; | |
527 | goto end; | |
528 | } | |
529 | ||
530 | add_to_loaded_plugins(plugins); | |
531 | free(plugins); | |
532 | end: | |
533 | return ret; | |
98ecef32 MD |
534 | } |
535 | ||
528debdf | 536 | int main(int argc, const char **argv) |
34ac0e6c | 537 | { |
7f26a816 | 538 | int ret; |
c42c79ea PP |
539 | struct bt_component_class *source_class = NULL; |
540 | struct bt_component_class *sink_class = NULL; | |
7c7c0433 JG |
541 | struct bt_component *source = NULL, *sink = NULL; |
542 | struct bt_value *source_params = NULL, *sink_params = NULL; | |
c42c79ea | 543 | struct bt_config *cfg; |
fec2a9f2 | 544 | enum bt_component_status sink_status; |
e5bc7f81 | 545 | struct bt_config_component *source_cfg = NULL, *sink_cfg = NULL; |
c42c79ea | 546 | |
33b34c43 PP |
547 | init_loaded_plugins_array(); |
548 | ||
c1870f57 JG |
549 | cfg = bt_config_create(); |
550 | if (!cfg) { | |
551 | fprintf(stderr, "Failed to create Babeltrace configuration\n"); | |
552 | ret = 1; | |
553 | goto end; | |
554 | } | |
555 | ||
556 | ret = set_default_config(cfg); | |
557 | if (ret) { | |
558 | goto end; | |
559 | } | |
560 | ||
33b34c43 | 561 | ret = bt_config_init_from_args(cfg, argc, argv); |
c1870f57 | 562 | if (ret == 0) { |
8d24f1c0 JG |
563 | babeltrace_verbose = cfg->verbose; |
564 | babeltrace_debug = cfg->debug; | |
c42c79ea PP |
565 | print_cfg(cfg); |
566 | } else { | |
56a1cced JG |
567 | goto end; |
568 | } | |
569 | ||
c42c79ea PP |
570 | /* TODO handle more than 1 source and 1 sink. */ |
571 | if (cfg->sources->len != 1 || cfg->sinks->len != 1) { | |
56a1cced JG |
572 | ret = -1; |
573 | goto end; | |
574 | } | |
575 | ||
34ac0e6c MD |
576 | printf_verbose("Verbose mode active.\n"); |
577 | printf_debug("Debug mode active.\n"); | |
33bceaf8 | 578 | |
33b34c43 PP |
579 | if (load_dynamic_plugins(cfg)) { |
580 | fprintf(stderr, "Failed to load dynamic plugins.\n"); | |
98ecef32 MD |
581 | ret = -1; |
582 | goto end; | |
33bceaf8 JG |
583 | } |
584 | ||
33b34c43 | 585 | if (load_static_plugins()) { |
cba174d5 JG |
586 | fprintf(stderr, "Failed to load static plugins.\n"); |
587 | goto end; | |
588 | } | |
589 | ||
33b34c43 | 590 | print_component_classes_found(); |
e5bc7f81 JG |
591 | source_cfg = bt_config_get_component(cfg->sources, 0); |
592 | source_params = bt_get(source_cfg->params); | |
33b34c43 PP |
593 | source_class = find_component_class(source_cfg->plugin_name->str, |
594 | source_cfg->component_name->str, | |
d3e4dcd8 | 595 | BT_COMPONENT_CLASS_TYPE_SOURCE); |
56a1cced | 596 | if (!source_class) { |
e5bc7f81 JG |
597 | fprintf(stderr, "Could not find %s.%s source component class. Aborting...\n", |
598 | source_cfg->plugin_name->str, | |
599 | source_cfg->component_name->str); | |
56a1cced JG |
600 | ret = -1; |
601 | goto end; | |
602 | } | |
7c7c0433 | 603 | |
e5bc7f81 JG |
604 | sink_cfg = bt_config_get_component(cfg->sinks, 0); |
605 | sink_params = bt_get(sink_cfg->params); | |
33b34c43 PP |
606 | sink_class = find_component_class(sink_cfg->plugin_name->str, |
607 | sink_cfg->component_name->str, | |
d3e4dcd8 | 608 | BT_COMPONENT_CLASS_TYPE_SINK); |
7c7c0433 | 609 | if (!sink_class) { |
e5bc7f81 JG |
610 | fprintf(stderr, "Could not find %s.%s output component class. Aborting...\n", |
611 | sink_cfg->plugin_name->str, | |
612 | sink_cfg->component_name->str); | |
7c7c0433 JG |
613 | ret = -1; |
614 | goto end; | |
615 | } | |
616 | ||
e5bc7f81 | 617 | source = bt_component_create(source_class, "source", source_params); |
56a1cced | 618 | if (!source) { |
e5bc7f81 | 619 | fprintf(stderr, "Failed to instantiate selected source component. Aborting...\n"); |
c42c79ea PP |
620 | ret = -1; |
621 | goto end; | |
622 | } | |
56a1cced | 623 | |
e5bc7f81 | 624 | sink = bt_component_create(sink_class, "sink", sink_params); |
7c7c0433 | 625 | if (!sink) { |
e5bc7f81 | 626 | fprintf(stderr, "Failed to instantiate selected output component. Aborting...\n"); |
7c7c0433 | 627 | ret = -1; |
2e339de1 JG |
628 | goto end; |
629 | } | |
630 | ||
6c2f3ee5 JG |
631 | ret = connect_source_sink(source, source_cfg, sink); |
632 | if (ret) { | |
fec2a9f2 JG |
633 | goto end; |
634 | } | |
78586d8a | 635 | |
fec2a9f2 JG |
636 | while (true) { |
637 | sink_status = bt_component_sink_consume(sink); | |
fec2a9f2 JG |
638 | switch (sink_status) { |
639 | case BT_COMPONENT_STATUS_AGAIN: | |
640 | /* Wait for an arbitraty 500 ms. */ | |
641 | usleep(500000); | |
78586d8a | 642 | break; |
fec2a9f2 JG |
643 | case BT_COMPONENT_STATUS_OK: |
644 | break; | |
645 | case BT_COMPONENT_STATUS_END: | |
646 | goto end; | |
647 | default: | |
648 | fprintf(stderr, "Sink component returned an error, aborting...\n"); | |
649 | ret = -1; | |
650 | goto end; | |
78586d8a | 651 | } |
fec2a9f2 | 652 | } |
11e1d048 | 653 | end: |
7c7c0433 JG |
654 | BT_PUT(sink_class); |
655 | BT_PUT(source_class); | |
656 | BT_PUT(source); | |
657 | BT_PUT(sink); | |
658 | BT_PUT(source_params); | |
659 | BT_PUT(sink_params); | |
c42c79ea | 660 | BT_PUT(cfg); |
e5bc7f81 JG |
661 | BT_PUT(sink_cfg); |
662 | BT_PUT(source_cfg); | |
33b34c43 | 663 | fini_loaded_plugins_array(); |
7f26a816 | 664 | return ret ? 1 : 0; |
4c8bfb7e | 665 | } |