Make babeltrace(1)'s CLI Git-like and implement the list-plugins command
[babeltrace.git] / converter / babeltrace.c
CommitLineData
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>
290725f7 31#include <babeltrace/common-internal.h>
33b34c43 32#include <babeltrace/component/component.h>
d71dcf2c
PP
33#include <babeltrace/component/component-source.h>
34#include <babeltrace/component/component-sink.h>
35#include <babeltrace/component/component-filter.h>
33b34c43
PP
36#include <babeltrace/component/component-class.h>
37#include <babeltrace/component/notification/iterator.h>
2e339de1
JG
38#include <babeltrace/ref.h>
39#include <babeltrace/values.h>
34ac0e6c 40#include <stdlib.h>
a44bc4c9 41#include <babeltrace/ctf-ir/metadata.h> /* for clocks */
7f26a816
PP
42#include <popt.h>
43#include <string.h>
44#include <stdio.h>
33b34c43 45#include <glib.h>
c42c79ea 46#include "babeltrace-cfg.h"
c1870f57 47#include "default-cfg.h"
34ac0e6c 48
33b34c43
PP
49GPtrArray *loaded_plugins;
50
51static
52void init_loaded_plugins_array(void)
53{
54 loaded_plugins = g_ptr_array_new_full(8, bt_put);
55}
56
57static
58void fini_loaded_plugins_array(void)
59{
60 g_ptr_array_free(loaded_plugins, TRUE);
61}
62
63static
64struct bt_plugin *find_plugin(const char *name)
65{
66 int i;
67 struct bt_plugin *plugin = NULL;
68
69 for (i = 0; i < loaded_plugins->len; i++) {
70 plugin = g_ptr_array_index(loaded_plugins, i);
71
72 if (strcmp(name, bt_plugin_get_name(plugin)) == 0) {
73 break;
74 }
75
76 plugin = NULL;
77 }
78
79 return bt_get(plugin);
80}
81
82static
83struct bt_component_class *find_component_class(const char *plugin_name,
84 const char *comp_class_name,
d3e4dcd8 85 enum bt_component_class_type comp_class_type)
33b34c43
PP
86{
87 struct bt_component_class *comp_class = NULL;
88 struct bt_plugin *plugin = find_plugin(plugin_name);
89
90 if (!plugin) {
91 goto end;
92 }
93
94 comp_class = bt_plugin_get_component_class_by_name_and_type(plugin,
95 comp_class_name, comp_class_type);
96 BT_PUT(plugin);
97end:
98 return comp_class;
99}
6c2f3ee5 100
c42c79ea
PP
101static
102void print_indent(size_t indent)
103{
104 size_t i;
105
106 for (i = 0; i < indent; i++) {
00447e45 107 printf(" ");
c42c79ea
PP
108 }
109}
110
111static
290725f7 112void print_value(struct bt_value *, size_t);
c42c79ea
PP
113
114static
115bool print_map_value(const char *key, struct bt_value *object, void *data)
116{
290725f7
PP
117 size_t *indent = data;
118
119 print_indent(*indent);
120 printf("%s: ", key);
121
122 if (bt_value_is_array(object) &&
123 bt_value_array_is_empty(object)) {
124 printf("[ ]\n");
125 return true;
126 }
127
128 if (bt_value_is_map(object) &&
129 bt_value_map_is_empty(object)) {
130 printf("{ }\n");
131 return true;
132 }
c42c79ea 133
290725f7
PP
134 if (bt_value_is_array(object) ||
135 bt_value_is_map(object)) {
136 printf("\n");
137 }
c42c79ea 138
290725f7 139 print_value(object, *indent + 2);
c42c79ea
PP
140 return true;
141}
142
143static
290725f7 144void print_value(struct bt_value *value, size_t indent)
c42c79ea
PP
145{
146 bool bool_val;
147 int64_t int_val;
148 double dbl_val;
149 const char *str_val;
150 int size;
151 int i;
152
153 if (!value) {
154 return;
155 }
156
c42c79ea
PP
157 switch (bt_value_get_type(value)) {
158 case BT_VALUE_TYPE_NULL:
00447e45 159 printf("null\n");
c42c79ea
PP
160 break;
161 case BT_VALUE_TYPE_BOOL:
162 bt_value_bool_get(value, &bool_val);
290725f7 163 printf("%s\n", bool_val ? "yes" : "no");
c42c79ea
PP
164 break;
165 case BT_VALUE_TYPE_INTEGER:
166 bt_value_integer_get(value, &int_val);
00447e45 167 printf("%" PRId64 "\n", int_val);
c42c79ea
PP
168 break;
169 case BT_VALUE_TYPE_FLOAT:
170 bt_value_float_get(value, &dbl_val);
00447e45 171 printf("%lf\n", dbl_val);
c42c79ea
PP
172 break;
173 case BT_VALUE_TYPE_STRING:
174 bt_value_string_get(value, &str_val);
290725f7 175 printf("%s\n", str_val);
c42c79ea
PP
176 break;
177 case BT_VALUE_TYPE_ARRAY:
178 size = bt_value_array_size(value);
290725f7
PP
179 assert(size >= 0);
180
181 if (size == 0) {
182 print_indent(indent);
183 printf("[ ]\n");
184 break;
185 }
c42c79ea
PP
186
187 for (i = 0; i < size; i++) {
188 struct bt_value *element =
189 bt_value_array_get(value, i);
190
290725f7
PP
191 assert(element);
192 print_indent(indent);
193 printf("- ");
194
195 if (bt_value_is_array(element) &&
196 bt_value_array_is_empty(element)) {
197 printf("[ ]\n");
198 continue;
199 }
200
201 if (bt_value_is_map(element) &&
202 bt_value_map_is_empty(element)) {
203 printf("{ }\n");
204 continue;
205 }
206
207 if (bt_value_is_array(element) ||
208 bt_value_is_map(element)) {
209 printf("\n");
210 }
211
212 print_value(element, indent + 2);
c42c79ea
PP
213 BT_PUT(element);
214 }
c42c79ea
PP
215 break;
216 case BT_VALUE_TYPE_MAP:
217 if (bt_value_map_is_empty(value)) {
290725f7
PP
218 print_indent(indent);
219 printf("{ }\n");
220 break;
c42c79ea
PP
221 }
222
290725f7 223 bt_value_map_foreach(value, print_map_value, &indent);
c42c79ea
PP
224 break;
225 default:
226 assert(false);
227 }
228}
229
230static
231void print_bt_config_component(struct bt_config_component *bt_config_component)
232{
290725f7 233 printf(" %s.%s:\n", bt_config_component->plugin_name->str,
c42c79ea 234 bt_config_component->component_name->str);
290725f7
PP
235 printf(" Parameters:\n");
236 print_value(bt_config_component->params, 8);
c42c79ea
PP
237}
238
239static
240void print_bt_config_components(GPtrArray *array)
241{
242 size_t i;
243
244 for (i = 0; i < array->len; i++) {
245 struct bt_config_component *cfg_component =
e5bc7f81 246 bt_config_get_component(array, i);
c42c79ea
PP
247 print_bt_config_component(cfg_component);
248 BT_PUT(cfg_component);
249 }
250}
251
290725f7
PP
252static
253void print_plugin_paths(struct bt_value *plugin_paths)
254{
255 printf(" Plugin paths:\n");
256 print_value(plugin_paths, 4);
257}
258
259static
260void print_cfg_convert(struct bt_config *cfg)
261{
262 printf(" Force correlate: %s\n",
263 cfg->cmd_data.convert.force_correlate ? "yes" : "no");
264 print_plugin_paths(cfg->cmd_data.convert.plugin_paths);
265 printf(" Source component instances:\n");
266 print_bt_config_components(cfg->cmd_data.convert.sources);
267 printf(" Sink component instances:\n");
268 print_bt_config_components(cfg->cmd_data.convert.sinks);
269}
270
271static
272void print_cfg_list_plugins(struct bt_config *cfg)
273{
274 print_plugin_paths(cfg->cmd_data.list_plugins.plugin_paths);
275}
276
c42c79ea
PP
277static
278void print_cfg(struct bt_config *cfg)
279{
00447e45
PP
280 if (!babeltrace_verbose) {
281 return;
282 }
283
290725f7
PP
284 printf("Configuration:\n");
285 printf(" Debug mode: %s\n", cfg->debug ? "yes" : "no");
286 printf(" Verbose mode: %s\n", cfg->verbose ? "yes" : "no");
287
288 switch (cfg->command) {
289 case BT_CONFIG_COMMAND_CONVERT:
290 print_cfg_convert(cfg);
291 break;
292 case BT_CONFIG_COMMAND_LIST_PLUGINS:
293 print_cfg_list_plugins(cfg);
294 break;
295 default:
296 assert(false);
297 }
c42c79ea
PP
298}
299
6c2f3ee5
JG
300static
301struct bt_component *create_trimmer(struct bt_config_component *source_cfg)
302{
303 struct bt_component *trimmer = NULL;
304 struct bt_component_class *trimmer_class = NULL;
305 struct bt_value *trimmer_params = NULL;
528debdf 306 struct bt_value *value;
6c2f3ee5 307
6c2f3ee5
JG
308 trimmer_params = bt_value_map_create();
309 if (!trimmer_params) {
310 goto end;
311 }
312
528debdf
MD
313 value = bt_value_map_get(source_cfg->params, "begin");
314 if (value) {
6c2f3ee5 315 enum bt_value_status ret;
6c2f3ee5 316
528debdf 317 ret = bt_value_map_insert(trimmer_params, "begin",
6c2f3ee5
JG
318 value);
319 BT_PUT(value);
320 if (ret) {
321 goto end;
322 }
323 }
528debdf
MD
324 value = bt_value_map_get(source_cfg->params, "end");
325 if (value) {
6c2f3ee5 326 enum bt_value_status ret;
6c2f3ee5 327
528debdf
MD
328 ret = bt_value_map_insert(trimmer_params, "end",
329 value);
330 BT_PUT(value);
331 if (ret) {
6c2f3ee5
JG
332 goto end;
333 }
528debdf
MD
334 }
335 value = bt_value_map_get(source_cfg->params, "clock-gmt");
336 if (value) {
337 enum bt_value_status ret;
6c2f3ee5 338
528debdf 339 ret = bt_value_map_insert(trimmer_params, "clock-gmt",
6c2f3ee5
JG
340 value);
341 BT_PUT(value);
342 if (ret) {
343 goto end;
344 }
345 }
346
33b34c43 347 trimmer_class = find_component_class("utils", "trimmer",
d3e4dcd8 348 BT_COMPONENT_CLASS_TYPE_FILTER);
6c2f3ee5
JG
349 if (!trimmer_class) {
350 fprintf(stderr, "Could not find trimmer component class. Aborting...\n");
351 goto end;
352 }
353 trimmer = bt_component_create(trimmer_class, "source_trimmer",
354 trimmer_params);
355 if (!trimmer) {
356 goto end;
357 }
358end:
359 bt_put(trimmer_params);
360 bt_put(trimmer_class);
361 return trimmer;
362}
363
364static
365int connect_source_sink(struct bt_component *source,
366 struct bt_config_component *source_cfg,
367 struct bt_component *sink)
368{
369 int ret = 0;
370 enum bt_component_status sink_status;
371 struct bt_component *trimmer = NULL;
372 struct bt_notification_iterator *source_it = NULL;
373 struct bt_notification_iterator *to_sink_it = NULL;
374
c725abdd 375 source_it = bt_component_source_create_notification_iterator(source);
6c2f3ee5
JG
376 if (!source_it) {
377 fprintf(stderr, "Failed to instantiate source iterator. Aborting...\n");
378 ret = -1;
379 goto end;
380 }
381
528debdf
MD
382 if (bt_value_map_has_key(source_cfg->params, "begin")
383 || bt_value_map_has_key(source_cfg->params, "end")) {
6c2f3ee5
JG
384 /* A trimmer must be inserted in the graph. */
385 enum bt_component_status trimmer_status;
386
387 trimmer = create_trimmer(source_cfg);
388 if (!trimmer) {
389 fprintf(stderr, "Failed to create trimmer component. Aborting...\n");
390 ret = -1;
391 goto end;
392 }
393
394 trimmer_status = bt_component_filter_add_iterator(trimmer,
395 source_it);
396 BT_PUT(source_it);
397 if (trimmer_status != BT_COMPONENT_STATUS_OK) {
398 fprintf(stderr, "Failed to connect source to trimmer. Aborting...\n");
399 ret = -1;
400 goto end;
401 }
402
c725abdd 403 to_sink_it = bt_component_filter_create_notification_iterator(trimmer);
6c2f3ee5
JG
404 if (!to_sink_it) {
405 fprintf(stderr, "Failed to instantiate trimmer iterator. Aborting...\n");
406 ret = -1;
407 goto end;
408 }
409 } else {
410 BT_MOVE(to_sink_it, source_it);
411 }
412
413 sink_status = bt_component_sink_add_iterator(sink, to_sink_it);
414 if (sink_status != BT_COMPONENT_STATUS_OK) {
415 fprintf(stderr, "Failed to connect to sink component. Aborting...\n");
416 ret = -1;
417 goto end;
418 }
419end:
420 bt_put(trimmer);
421 bt_put(source_it);
422 bt_put(to_sink_it);
423 return ret;
424}
425
33b34c43
PP
426static
427void add_to_loaded_plugins(struct bt_plugin **plugins)
98ecef32 428{
33b34c43
PP
429 while (*plugins) {
430 struct bt_plugin *plugin = *plugins;
431 /* Check if it's already loaded (from another path). */
432 struct bt_plugin *loaded_plugin =
433 find_plugin(bt_plugin_get_name(plugin));
434
435 if (loaded_plugin) {
436 printf_verbose("Not loading plugin `%s`: already loaded from `%s`\n",
437 bt_plugin_get_path(plugin),
438 bt_plugin_get_path(loaded_plugin));
439 BT_PUT(loaded_plugin);
440 BT_PUT(plugin);
441 } else {
442 /* Transfer ownership to global array. */
443 g_ptr_array_add(loaded_plugins, plugin);
444 }
445 *(plugins++) = NULL;
446 }
447}
448
449static
290725f7 450int load_dynamic_plugins(struct bt_value *plugin_paths)
33b34c43
PP
451{
452 int nr_paths, i, ret = 0;
98ecef32 453
290725f7 454 nr_paths = bt_value_array_size(plugin_paths);
98ecef32 455 if (nr_paths < 0) {
33b34c43
PP
456 ret = -1;
457 goto end;
98ecef32 458 }
33b34c43 459
98ecef32
MD
460 for (i = 0; i < nr_paths; i++) {
461 struct bt_value *plugin_path_value = NULL;
462 const char *plugin_path;
33b34c43 463 struct bt_plugin **plugins;
98ecef32 464
290725f7 465 plugin_path_value = bt_value_array_get(plugin_paths, i);
98ecef32
MD
466 if (bt_value_string_get(plugin_path_value,
467 &plugin_path)) {
468 BT_PUT(plugin_path_value);
469 continue;
470 }
33b34c43 471
5a3ee633 472 plugins = bt_plugin_create_all_from_dir(plugin_path, false);
33b34c43 473 if (!plugins) {
98ecef32
MD
474 printf_debug("Unable to dynamically load plugins from path %s.\n",
475 plugin_path);
33b34c43
PP
476 BT_PUT(plugin_path_value);
477 continue;
98ecef32 478 }
33b34c43
PP
479
480 add_to_loaded_plugins(plugins);
481 free(plugins);
482
98ecef32
MD
483 BT_PUT(plugin_path_value);
484 }
33b34c43
PP
485end:
486 return ret;
487}
488
489static
490int load_static_plugins(void)
491{
492 int ret = 0;
493 struct bt_plugin **plugins;
494
495 plugins = bt_plugin_create_all_from_static();
496 if (!plugins) {
497 printf_debug("Unable to load static plugins.\n");
498 ret = -1;
499 goto end;
500 }
501
502 add_to_loaded_plugins(plugins);
503 free(plugins);
504end:
505 return ret;
98ecef32
MD
506}
507
290725f7
PP
508static
509const char *component_type_str(enum bt_component_class_type type)
34ac0e6c 510{
290725f7
PP
511 switch (type) {
512 case BT_COMPONENT_CLASS_TYPE_SOURCE:
513 return "source";
514 case BT_COMPONENT_CLASS_TYPE_SINK:
515 return "sink";
516 case BT_COMPONENT_CLASS_TYPE_FILTER:
517 return "filter";
518 case BT_COMPONENT_CLASS_TYPE_UNKNOWN:
519 default:
520 return "unknown";
521 }
522}
c42c79ea 523
290725f7
PP
524static int load_all_plugins(struct bt_value *plugin_paths)
525{
526 int ret = 0;
33b34c43 527
290725f7
PP
528 if (load_dynamic_plugins(plugin_paths)) {
529 fprintf(stderr, "Failed to load dynamic plugins.\n");
530 ret = -1;
c1870f57
JG
531 goto end;
532 }
533
290725f7
PP
534 if (load_static_plugins()) {
535 fprintf(stderr, "Failed to load static plugins.\n");
536 ret = -1;
c1870f57
JG
537 goto end;
538 }
539
290725f7
PP
540end:
541 return ret;
542}
543
544static int cmd_list_plugins(struct bt_config *cfg)
545{
546 int ret;
547 int plugins_count, component_classes_count = 0, i;
548
549 ret = load_all_plugins(cfg->cmd_data.list_plugins.plugin_paths);
550 if (ret) {
56a1cced
JG
551 goto end;
552 }
553
290725f7
PP
554 plugins_count = loaded_plugins->len;
555 if (plugins_count == 0) {
556 fprintf(stderr, "%s%sNo plugins found.%s\n",
557 bt_common_color_bold(), bt_common_color_fg_red(),
558 bt_common_color_reset());
559 fprintf(stderr, "\n");
560 fprintf(stderr, "Please make sure your plugin search path is set correctly. You can use\n");
561 fprintf(stderr, "the --plugin-path command-line option or the BABELTRACE_PLUGIN_PATH\n");
562 fprintf(stderr, "environment variable.\n");
56a1cced
JG
563 ret = -1;
564 goto end;
565 }
566
290725f7
PP
567 for (i = 0; i < plugins_count; i++) {
568 struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i);
569
570 component_classes_count += bt_plugin_get_component_class_count(plugin);
571 }
33bceaf8 572
290725f7
PP
573 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
574 bt_common_color_bold(),
575 component_classes_count,
576 bt_common_color_reset(),
577 bt_common_color_bold(),
578 plugins_count,
579 bt_common_color_reset());
580
581 for (i = 0; i < plugins_count; i++) {
582 int j;
583 struct bt_plugin *plugin = g_ptr_array_index(loaded_plugins, i);
584 unsigned int major, minor, patch;
585 const char *extra;
586 enum bt_plugin_status version_status;
587 const char *plugin_name = bt_plugin_get_name(plugin);
588 const char *path = bt_plugin_get_path(plugin);
589 const char *author = bt_plugin_get_author(plugin);
590 const char *license = bt_plugin_get_license(plugin);
591 const char *plugin_description =
592 bt_plugin_get_description(plugin);
593
594 component_classes_count =
595 bt_plugin_get_component_class_count(plugin);
596 version_status = bt_plugin_get_version(plugin, &major, &minor,
597 &patch, &extra);
598
599 printf("\n%s%s%s%s:\n", bt_common_color_bold(),
600 bt_common_color_fg_blue(), plugin_name,
601 bt_common_color_reset());
602 printf(" %sPath%s: %s\n", bt_common_color_bold(),
603 bt_common_color_reset(), path ? path : "(None)");
604
605 if (version_status == BT_PLUGIN_STATUS_OK) {
606 printf(" %sVersion%s: %u.%u.%u",
607 bt_common_color_bold(), bt_common_color_reset(),
608 major, minor, patch);
609
610 if (extra) {
611 printf("%s", extra);
612 }
613
614 printf("\n");
615 }
616
617 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
618 bt_common_color_reset(),
619 plugin_description ? plugin_description : "(None)");
620 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
621 bt_common_color_reset(), author ? author : "(Unknown)");
622 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
623 bt_common_color_reset(),
624 license ? license : "(Unknown)");
625
626 if (component_classes_count == 0) {
627 printf(" %sComponent classes%s: (None)\n",
628 bt_common_color_bold(),
629 bt_common_color_reset());
630 } else {
631 printf(" %sComponent classes%s:\n",
632 bt_common_color_bold(),
633 bt_common_color_reset());
634 }
635
636 for (j = 0; j < component_classes_count; j++) {
637 struct bt_component_class *comp_class =
638 bt_plugin_get_component_class(plugin, j);
639 const char *comp_class_name =
640 bt_component_class_get_name(comp_class);
641 const char *comp_class_description =
642 bt_component_class_get_description(comp_class);
643 enum bt_component_class_type type =
644 bt_component_class_get_type(comp_class);
645
646 printf(" %s%s--%s%s %s%s%s.%s%s%s",
647 bt_common_color_bold(),
648 bt_common_color_fg_cyan(),
649 component_type_str(type),
650 bt_common_color_fg_default(),
651 bt_common_color_fg_blue(),
652 plugin_name,
653 bt_common_color_fg_default(),
654 bt_common_color_fg_yellow(),
655 comp_class_name,
656 bt_common_color_reset());
657
658 if (comp_class_description) {
659 printf(": %s", comp_class_description);
660 }
661
662 printf("\n");
663 bt_put(comp_class);
664 }
665 }
666
667end:
668 return ret;
669}
670
671static int cmd_convert(struct bt_config *cfg)
672{
673 int ret = 0;
674 struct bt_component_class *source_class = NULL;
675 struct bt_component_class *sink_class = NULL;
676 struct bt_component *source = NULL, *sink = NULL;
677 struct bt_value *source_params = NULL, *sink_params = NULL;
678 enum bt_component_status sink_status;
679 struct bt_config_component *source_cfg = NULL, *sink_cfg = NULL;
680
681 /* TODO handle more than 1 source and 1 sink. */
682 if (cfg->cmd_data.convert.sources->len != 1 ||
683 cfg->cmd_data.convert.sinks->len != 1) {
98ecef32
MD
684 ret = -1;
685 goto end;
33bceaf8
JG
686 }
687
290725f7
PP
688 ret = load_all_plugins(cfg->cmd_data.convert.plugin_paths);
689 if (ret) {
cba174d5
JG
690 goto end;
691 }
692
290725f7 693 source_cfg = bt_config_get_component(cfg->cmd_data.convert.sources, 0);
e5bc7f81 694 source_params = bt_get(source_cfg->params);
33b34c43
PP
695 source_class = find_component_class(source_cfg->plugin_name->str,
696 source_cfg->component_name->str,
d3e4dcd8 697 BT_COMPONENT_CLASS_TYPE_SOURCE);
56a1cced 698 if (!source_class) {
e5bc7f81
JG
699 fprintf(stderr, "Could not find %s.%s source component class. Aborting...\n",
700 source_cfg->plugin_name->str,
701 source_cfg->component_name->str);
56a1cced
JG
702 ret = -1;
703 goto end;
704 }
7c7c0433 705
290725f7 706 sink_cfg = bt_config_get_component(cfg->cmd_data.convert.sinks, 0);
e5bc7f81 707 sink_params = bt_get(sink_cfg->params);
33b34c43
PP
708 sink_class = find_component_class(sink_cfg->plugin_name->str,
709 sink_cfg->component_name->str,
d3e4dcd8 710 BT_COMPONENT_CLASS_TYPE_SINK);
7c7c0433 711 if (!sink_class) {
e5bc7f81
JG
712 fprintf(stderr, "Could not find %s.%s output component class. Aborting...\n",
713 sink_cfg->plugin_name->str,
714 sink_cfg->component_name->str);
7c7c0433
JG
715 ret = -1;
716 goto end;
717 }
718
e5bc7f81 719 source = bt_component_create(source_class, "source", source_params);
56a1cced 720 if (!source) {
e5bc7f81 721 fprintf(stderr, "Failed to instantiate selected source component. Aborting...\n");
c42c79ea
PP
722 ret = -1;
723 goto end;
724 }
56a1cced 725
e5bc7f81 726 sink = bt_component_create(sink_class, "sink", sink_params);
7c7c0433 727 if (!sink) {
e5bc7f81 728 fprintf(stderr, "Failed to instantiate selected output component. Aborting...\n");
7c7c0433 729 ret = -1;
2e339de1
JG
730 goto end;
731 }
732
6c2f3ee5
JG
733 ret = connect_source_sink(source, source_cfg, sink);
734 if (ret) {
290725f7 735 ret = -1;
fec2a9f2
JG
736 goto end;
737 }
78586d8a 738
fec2a9f2
JG
739 while (true) {
740 sink_status = bt_component_sink_consume(sink);
fec2a9f2
JG
741 switch (sink_status) {
742 case BT_COMPONENT_STATUS_AGAIN:
743 /* Wait for an arbitraty 500 ms. */
744 usleep(500000);
78586d8a 745 break;
fec2a9f2
JG
746 case BT_COMPONENT_STATUS_OK:
747 break;
748 case BT_COMPONENT_STATUS_END:
749 goto end;
750 default:
751 fprintf(stderr, "Sink component returned an error, aborting...\n");
752 ret = -1;
753 goto end;
78586d8a 754 }
fec2a9f2 755 }
290725f7 756
11e1d048 757end:
7c7c0433
JG
758 BT_PUT(sink_class);
759 BT_PUT(source_class);
760 BT_PUT(source);
761 BT_PUT(sink);
762 BT_PUT(source_params);
763 BT_PUT(sink_params);
e5bc7f81
JG
764 BT_PUT(sink_cfg);
765 BT_PUT(source_cfg);
290725f7
PP
766 return ret;
767}
768
769static void warn_command_name_and_directory_clash(struct bt_config *cfg)
770{
771 if (!cfg->command_name) {
772 return;
773 }
774
775 if (g_file_test(cfg->command_name,
776 G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
777 fprintf(stderr, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
778 cfg->command_name);
779 fprintf(stderr, "trace located in the local `%s` directory, please use:\n",
780 cfg->command_name);
781 fprintf(stderr, "\n");
782 fprintf(stderr, " babeltrace convert %s [OPTIONS]\n",
783 cfg->command_name);
784 }
785}
786
787int main(int argc, const char **argv)
788{
789 int ret;
790 int retcode;
791 struct bt_config *cfg;
792
793 init_loaded_plugins_array();
794 cfg = bt_config_from_args_with_defaults(argc, argv, &retcode);
795
796 if (retcode < 0) {
797 /* Quit without errors; typically usage/version */
798 retcode = 0;
799 goto end;
800 }
801
802 if (retcode > 0) {
803 goto end;
804 }
805
806 if (!cfg) {
807 fprintf(stderr, "Failed to create Babeltrace configuration\n");
808 goto end;
809 }
810
811 babeltrace_debug = cfg->debug;
812 babeltrace_verbose = cfg->verbose;
813 print_cfg(cfg);
814
815 switch (cfg->command) {
816 case BT_CONFIG_COMMAND_CONVERT:
817 ret = cmd_convert(cfg);
818 break;
819 case BT_CONFIG_COMMAND_LIST_PLUGINS:
820 ret = cmd_list_plugins(cfg);
821 break;
822 default:
823 assert(false);
824 }
825
826 warn_command_name_and_directory_clash(cfg);
827 retcode = ret ? 1 : 0;
828
829end:
830 BT_PUT(cfg);
33b34c43 831 fini_loaded_plugins_array();
290725f7 832 return retcode;
4c8bfb7e 833}
This page took 0.079335 seconds and 4 git commands to generate.