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/plugin.h>
31 #include <babeltrace/common-internal.h>
32 #include <babeltrace/graph/component.h>
33 #include <babeltrace/graph/component-source.h>
34 #include <babeltrace/graph/component-sink.h>
35 #include <babeltrace/graph/component-filter.h>
36 #include <babeltrace/graph/component-class.h>
37 #include <babeltrace/graph/port.h>
38 #include <babeltrace/graph/graph.h>
39 #include <babeltrace/graph/connection.h>
40 #include <babeltrace/graph/notification-iterator.h>
41 #include <babeltrace/ref.h>
42 #include <babeltrace/values.h>
50 #include "babeltrace-cfg.h"
51 #include "babeltrace-cfg-cli-args.h"
52 #include "babeltrace-cfg-cli-args-default.h"
54 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
56 GPtrArray
*loaded_plugins
;
59 void init_static_data(void)
61 loaded_plugins
= g_ptr_array_new_with_free_func(bt_put
);
65 void fini_static_data(void)
67 g_ptr_array_free(loaded_plugins
, TRUE
);
71 struct bt_plugin
*find_plugin(const char *name
)
74 struct bt_plugin
*plugin
= NULL
;
76 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
77 plugin
= g_ptr_array_index(loaded_plugins
, i
);
79 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
86 return bt_get(plugin
);
90 struct bt_component_class
*find_component_class(const char *plugin_name
,
91 const char *comp_class_name
,
92 enum bt_component_class_type comp_class_type
)
94 struct bt_component_class
*comp_class
= NULL
;
95 struct bt_plugin
*plugin
= find_plugin(plugin_name
);
101 comp_class
= bt_plugin_get_component_class_by_name_and_type(plugin
,
102 comp_class_name
, comp_class_type
);
109 void print_indent(size_t indent
)
113 for (i
= 0; i
< indent
; i
++) {
119 const char *component_type_str(enum bt_component_class_type type
)
122 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
124 case BT_COMPONENT_CLASS_TYPE_SINK
:
126 case BT_COMPONENT_CLASS_TYPE_FILTER
:
128 case BT_COMPONENT_CLASS_TYPE_UNKNOWN
:
135 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
136 const char *comp_cls_name
, enum bt_component_class_type type
)
138 GString
*shell_plugin_name
= NULL
;
139 GString
*shell_comp_cls_name
= NULL
;
141 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
142 if (!shell_plugin_name
) {
146 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
147 if (!shell_comp_cls_name
) {
151 fprintf(fh
, "%s%s--%s%s %s'%s%s%s%s.%s%s%s'",
152 bt_common_color_bold(),
153 bt_common_color_fg_cyan(),
154 component_type_str(type
),
155 bt_common_color_reset(),
156 bt_common_color_fg_default(),
157 bt_common_color_bold(),
158 bt_common_color_fg_blue(),
159 shell_plugin_name
->str
,
160 bt_common_color_fg_default(),
161 bt_common_color_fg_yellow(),
162 shell_comp_cls_name
->str
,
163 bt_common_color_reset());
166 if (shell_plugin_name
) {
167 g_string_free(shell_plugin_name
, TRUE
);
170 if (shell_comp_cls_name
) {
171 g_string_free(shell_comp_cls_name
, TRUE
);
176 void print_value(struct bt_value
*, size_t);
179 void print_value_rec(struct bt_value
*, size_t);
182 bool print_map_value(const char *key
, struct bt_value
*object
, void *data
)
184 size_t *indent
= data
;
186 print_indent(*indent
);
189 if (bt_value_is_array(object
) &&
190 bt_value_array_is_empty(object
)) {
195 if (bt_value_is_map(object
) &&
196 bt_value_map_is_empty(object
)) {
201 if (bt_value_is_array(object
) ||
202 bt_value_is_map(object
)) {
206 print_value_rec(object
, *indent
+ 2);
211 void print_value_rec(struct bt_value
*value
, size_t indent
)
224 switch (bt_value_get_type(value
)) {
225 case BT_VALUE_TYPE_NULL
:
226 printf("%snull%s\n", bt_common_color_bold(),
227 bt_common_color_reset());
229 case BT_VALUE_TYPE_BOOL
:
230 bt_value_bool_get(value
, &bool_val
);
231 printf("%s%s%s%s\n", bt_common_color_bold(),
232 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
233 bt_common_color_reset());
235 case BT_VALUE_TYPE_INTEGER
:
236 bt_value_integer_get(value
, &int_val
);
237 printf("%s%s%" PRId64
"%s\n", bt_common_color_bold(),
238 bt_common_color_fg_red(), int_val
,
239 bt_common_color_reset());
241 case BT_VALUE_TYPE_FLOAT
:
242 bt_value_float_get(value
, &dbl_val
);
243 printf("%s%s%lf%s\n", bt_common_color_bold(),
244 bt_common_color_fg_red(), dbl_val
,
245 bt_common_color_reset());
247 case BT_VALUE_TYPE_STRING
:
248 bt_value_string_get(value
, &str_val
);
249 printf("%s%s%s%s\n", bt_common_color_bold(),
250 bt_common_color_fg_green(), str_val
,
251 bt_common_color_reset());
253 case BT_VALUE_TYPE_ARRAY
:
254 size
= bt_value_array_size(value
);
258 print_indent(indent
);
263 for (i
= 0; i
< size
; i
++) {
264 struct bt_value
*element
=
265 bt_value_array_get(value
, i
);
268 print_indent(indent
);
271 if (bt_value_is_array(element
) &&
272 bt_value_array_is_empty(element
)) {
277 if (bt_value_is_map(element
) &&
278 bt_value_map_is_empty(element
)) {
283 if (bt_value_is_array(element
) ||
284 bt_value_is_map(element
)) {
288 print_value_rec(element
, indent
+ 2);
292 case BT_VALUE_TYPE_MAP
:
293 if (bt_value_map_is_empty(value
)) {
294 print_indent(indent
);
299 bt_value_map_foreach(value
, print_map_value
, &indent
);
307 void print_value(struct bt_value
*value
, size_t indent
)
309 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
310 print_indent(indent
);
313 print_value_rec(value
, indent
);
317 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
320 print_plugin_comp_cls_opt(stdout
, bt_config_component
->plugin_name
->str
,
321 bt_config_component
->comp_cls_name
->str
,
322 bt_config_component
->type
);
325 if (bt_config_component
->instance_name
->len
> 0) {
326 printf(" Name: %s\n",
327 bt_config_component
->instance_name
->str
);
330 printf(" Parameters:\n");
331 print_value(bt_config_component
->params
, 8);
335 void print_bt_config_components(GPtrArray
*array
)
339 for (i
= 0; i
< array
->len
; i
++) {
340 struct bt_config_component
*cfg_component
=
341 bt_config_get_component(array
, i
);
342 print_bt_config_component(cfg_component
);
343 BT_PUT(cfg_component
);
348 void print_plugin_paths(struct bt_value
*plugin_paths
)
350 printf(" Plugin paths:\n");
351 print_value(plugin_paths
, 4);
355 void print_cfg_run(struct bt_config
*cfg
)
359 print_plugin_paths(cfg
->plugin_paths
);
360 printf(" Source component instances:\n");
361 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
363 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
364 printf(" Filter component instances:\n");
365 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
368 printf(" Sink component instances:\n");
369 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
370 printf(" Connections:\n");
372 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
373 struct bt_config_connection
*cfg_connection
=
374 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
377 printf(" %s%s%s -> %s%s%s\n",
378 cfg_connection
->upstream_comp_name
->str
,
379 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
380 cfg_connection
->upstream_port_glob
->str
,
381 cfg_connection
->downstream_comp_name
->str
,
382 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
383 cfg_connection
->downstream_port_glob
->str
);
388 void print_cfg_list_plugins(struct bt_config
*cfg
)
390 print_plugin_paths(cfg
->plugin_paths
);
394 void print_cfg_help(struct bt_config
*cfg
)
396 print_plugin_paths(cfg
->plugin_paths
);
400 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
402 print_plugin_paths(cfg
->plugin_paths
);
403 printf(" Path: %s\n", cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
407 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
409 print_plugin_paths(cfg
->plugin_paths
);
410 printf(" URL: %s\n", cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
414 void print_cfg_query(struct bt_config
*cfg
)
416 print_plugin_paths(cfg
->plugin_paths
);
417 printf(" Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
418 printf(" Component class:\n");
419 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
423 void print_cfg(struct bt_config
*cfg
)
425 if (!babeltrace_verbose
) {
429 printf("Configuration:\n");
430 printf(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
431 printf(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
433 switch (cfg
->command
) {
434 case BT_CONFIG_COMMAND_RUN
:
437 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
438 print_cfg_list_plugins(cfg
);
440 case BT_CONFIG_COMMAND_HELP
:
443 case BT_CONFIG_COMMAND_QUERY
:
444 print_cfg_query(cfg
);
446 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
447 print_cfg_print_ctf_metadata(cfg
);
449 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
450 print_cfg_print_lttng_live_sessions(cfg
);
458 void add_to_loaded_plugins(struct bt_plugin_set
*plugin_set
)
463 count
= bt_plugin_set_get_plugin_count(plugin_set
);
466 for (i
= 0; i
< count
; i
++) {
467 struct bt_plugin
*plugin
=
468 bt_plugin_set_get_plugin(plugin_set
, i
);
469 struct bt_plugin
*loaded_plugin
=
470 find_plugin(bt_plugin_get_name(plugin
));
475 printf_verbose("Not loading plugin `%s`: already loaded from `%s`\n",
476 bt_plugin_get_path(plugin
),
477 bt_plugin_get_path(loaded_plugin
));
478 bt_put(loaded_plugin
);
480 /* Add to global array. */
481 g_ptr_array_add(loaded_plugins
, bt_get(plugin
));
489 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
491 int nr_paths
, i
, ret
= 0;
493 nr_paths
= bt_value_array_size(plugin_paths
);
499 for (i
= 0; i
< nr_paths
; i
++) {
500 struct bt_value
*plugin_path_value
= NULL
;
501 const char *plugin_path
;
502 struct bt_plugin_set
*plugin_set
;
504 plugin_path_value
= bt_value_array_get(plugin_paths
, i
);
505 if (bt_value_string_get(plugin_path_value
,
507 BT_PUT(plugin_path_value
);
511 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
513 printf_debug("Unable to dynamically load plugins from path %s.\n",
515 BT_PUT(plugin_path_value
);
519 add_to_loaded_plugins(plugin_set
);
521 BT_PUT(plugin_path_value
);
528 int load_static_plugins(void)
531 struct bt_plugin_set
*plugin_set
;
533 plugin_set
= bt_plugin_create_all_from_static();
535 printf_debug("Unable to load static plugins.\n");
540 add_to_loaded_plugins(plugin_set
);
547 int load_all_plugins(struct bt_value
*plugin_paths
)
551 if (load_dynamic_plugins(plugin_paths
)) {
552 fprintf(stderr
, "Failed to load dynamic plugins.\n");
557 if (load_static_plugins()) {
558 fprintf(stderr
, "Failed to load static plugins.\n");
568 void print_plugin_info(struct bt_plugin
*plugin
)
570 unsigned int major
, minor
, patch
;
572 enum bt_plugin_status version_status
;
573 const char *plugin_name
;
577 const char *plugin_description
;
579 plugin_name
= bt_plugin_get_name(plugin
);
580 path
= bt_plugin_get_path(plugin
);
581 author
= bt_plugin_get_author(plugin
);
582 license
= bt_plugin_get_license(plugin
);
583 plugin_description
= bt_plugin_get_description(plugin
);
584 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
586 printf("%s%s%s%s:\n", bt_common_color_bold(),
587 bt_common_color_fg_blue(), plugin_name
,
588 bt_common_color_reset());
589 printf(" %sPath%s: %s\n", bt_common_color_bold(),
590 bt_common_color_reset(), path
? path
: "(None)");
592 if (version_status
== BT_PLUGIN_STATUS_OK
) {
593 printf(" %sVersion%s: %u.%u.%u",
594 bt_common_color_bold(), bt_common_color_reset(),
595 major
, minor
, patch
);
604 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
605 bt_common_color_reset(),
606 plugin_description
? plugin_description
: "(None)");
607 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
608 bt_common_color_reset(), author
? author
: "(Unknown)");
609 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
610 bt_common_color_reset(),
611 license
? license
: "(Unknown)");
615 int cmd_query(struct bt_config
*cfg
)
618 struct bt_component_class
*comp_cls
= NULL
;
619 struct bt_value
*results
= NULL
;
621 ret
= load_all_plugins(cfg
->plugin_paths
);
626 comp_cls
= find_component_class(cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
627 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
628 cfg
->cmd_data
.query
.cfg_component
->type
);
630 fprintf(stderr
, "%s%sCannot find component class %s",
631 bt_common_color_bold(),
632 bt_common_color_fg_red(),
633 bt_common_color_reset());
634 print_plugin_comp_cls_opt(stderr
,
635 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
636 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
637 cfg
->cmd_data
.query
.cfg_component
->type
);
638 fprintf(stderr
, "\n");
643 results
= bt_component_class_query(comp_cls
,
644 cfg
->cmd_data
.query
.object
->str
,
645 cfg
->cmd_data
.query
.cfg_component
->params
);
647 fprintf(stderr
, "%s%sFailed to query info to %s",
648 bt_common_color_bold(),
649 bt_common_color_fg_red(),
650 bt_common_color_reset());
651 print_plugin_comp_cls_opt(stderr
,
652 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
653 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
654 cfg
->cmd_data
.query
.cfg_component
->type
);
655 fprintf(stderr
, "%s%s with object `%s`%s\n",
656 bt_common_color_bold(),
657 bt_common_color_fg_red(),
658 cfg
->cmd_data
.query
.object
->str
,
659 bt_common_color_reset());
664 print_value(results
, 0);
673 int cmd_help(struct bt_config
*cfg
)
676 struct bt_plugin
*plugin
= NULL
;
679 ret
= load_all_plugins(cfg
->plugin_paths
);
684 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
686 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
687 bt_common_color_bold(), bt_common_color_fg_red(),
688 bt_common_color_fg_blue(),
689 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
690 bt_common_color_reset());
695 print_plugin_info(plugin
);
696 printf(" %sComponent classes%s: %d\n",
697 bt_common_color_bold(),
698 bt_common_color_reset(),
699 (int) bt_plugin_get_component_class_count(plugin
));
702 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
703 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
704 struct bt_component_class
*needed_comp_cls
=
705 find_component_class(
706 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
707 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
708 cfg
->cmd_data
.help
.cfg_component
->type
);
710 if (!needed_comp_cls
) {
711 fprintf(stderr
, "\n%s%sCannot find component class %s",
712 bt_common_color_bold(),
713 bt_common_color_fg_red(),
714 bt_common_color_reset());
715 print_plugin_comp_cls_opt(stderr
,
716 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
717 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
718 cfg
->cmd_data
.help
.cfg_component
->type
);
719 fprintf(stderr
, "\n");
724 bt_put(needed_comp_cls
);
727 for (i
= 0; i
< bt_plugin_get_component_class_count(plugin
); i
++) {
728 struct bt_component_class
*comp_cls
=
729 bt_plugin_get_component_class_by_index(plugin
, i
);
730 const char *comp_class_name
=
731 bt_component_class_get_name(comp_cls
);
732 const char *comp_class_description
=
733 bt_component_class_get_description(comp_cls
);
734 const char *comp_class_help
=
735 bt_component_class_get_help(comp_cls
);
736 enum bt_component_class_type type
=
737 bt_component_class_get_type(comp_cls
);
741 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
742 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
743 if (strcmp(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
744 comp_class_name
) != 0 &&
746 cfg
->cmd_data
.help
.cfg_component
->type
) {
753 print_plugin_comp_cls_opt(stdout
,
754 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
758 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
759 bt_common_color_reset(),
760 comp_class_description
? comp_class_description
: "(None)");
762 if (comp_class_help
) {
763 printf("\n%s\n", comp_class_help
);
775 int cmd_list_plugins(struct bt_config
*cfg
)
778 int plugins_count
, component_classes_count
= 0, i
;
780 ret
= load_all_plugins(cfg
->plugin_paths
);
785 printf("From the following plugin paths:\n\n");
786 print_value(cfg
->plugin_paths
, 2);
788 plugins_count
= loaded_plugins
->len
;
789 if (plugins_count
== 0) {
790 fprintf(stderr
, "%s%sNo plugins found.%s\n",
791 bt_common_color_bold(), bt_common_color_fg_red(),
792 bt_common_color_reset());
793 fprintf(stderr
, "\n");
794 fprintf(stderr
, "Please make sure your plugin search path is set correctly. You can use\n");
795 fprintf(stderr
, "the --plugin-path command-line option or the BABELTRACE_PLUGIN_PATH\n");
796 fprintf(stderr
, "environment variable.\n");
801 for (i
= 0; i
< plugins_count
; i
++) {
802 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
804 component_classes_count
+= bt_plugin_get_component_class_count(plugin
);
807 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
808 bt_common_color_bold(),
809 component_classes_count
,
810 bt_common_color_reset(),
811 bt_common_color_bold(),
813 bt_common_color_reset());
815 for (i
= 0; i
< plugins_count
; i
++) {
817 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
819 component_classes_count
=
820 bt_plugin_get_component_class_count(plugin
);
822 print_plugin_info(plugin
);
824 if (component_classes_count
== 0) {
825 printf(" %sComponent classes%s: (none)\n",
826 bt_common_color_bold(),
827 bt_common_color_reset());
829 printf(" %sComponent classes%s:\n",
830 bt_common_color_bold(),
831 bt_common_color_reset());
834 for (j
= 0; j
< component_classes_count
; j
++) {
835 struct bt_component_class
*comp_class
=
836 bt_plugin_get_component_class_by_index(
838 const char *comp_class_name
=
839 bt_component_class_get_name(comp_class
);
840 const char *comp_class_description
=
841 bt_component_class_get_description(comp_class
);
842 enum bt_component_class_type type
=
843 bt_component_class_get_type(comp_class
);
846 print_plugin_comp_cls_opt(stdout
,
847 bt_plugin_get_name(plugin
), comp_class_name
,
850 if (comp_class_description
) {
851 printf(": %s", comp_class_description
);
864 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
871 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
874 struct bt_component_class
*comp_cls
= NULL
;
875 struct bt_value
*results
= NULL
;
876 struct bt_value
*params
= NULL
;
877 struct bt_value
*metadata_text_value
= NULL
;
878 const char *metadata_text
= NULL
;
879 static const char * const plugin_name
= "ctf";
880 static const char * const comp_cls_name
= "fs";
881 static const enum bt_component_class_type comp_cls_type
=
882 BT_COMPONENT_CLASS_TYPE_SOURCE
;
884 assert(cfg
->cmd_data
.print_ctf_metadata
.path
);
885 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
888 fprintf(stderr
, "%s%sCannot find component class %s",
889 bt_common_color_bold(),
890 bt_common_color_fg_red(),
891 bt_common_color_reset());
892 print_plugin_comp_cls_opt(stderr
, plugin_name
,
893 comp_cls_name
, comp_cls_type
);
894 fprintf(stderr
, "\n");
899 params
= bt_value_map_create();
905 ret
= bt_value_map_insert_string(params
, "path",
906 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
912 results
= bt_component_class_query(comp_cls
, "metadata-info",
916 fprintf(stderr
, "%s%sFailed to request metadata info%s\n",
917 bt_common_color_bold(),
918 bt_common_color_fg_red(),
919 bt_common_color_reset());
923 metadata_text_value
= bt_value_map_get(results
, "text");
924 if (!metadata_text_value
) {
929 ret
= bt_value_string_get(metadata_text_value
, &metadata_text
);
931 printf("%s\n", metadata_text
);
936 bt_put(metadata_text_value
);
943 GHashTable
*components
;
946 struct bt_graph
*graph
;
949 struct bt_config
*cfg
;
955 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
956 struct cmd_run_ctx
*ctx
, struct bt_component
*upstream_comp
,
957 struct bt_port
*upstream_port
,
958 struct bt_config_connection
*cfg_conn
)
961 GQuark downstreamp_comp_name_quark
;
962 struct bt_component
*downstream_comp
;
963 int64_t downstream_port_count
;
965 int64_t (*port_count_fn
)(struct bt_component
*);
966 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
969 downstreamp_comp_name_quark
= g_quark_from_string(
970 cfg_conn
->downstream_comp_name
->str
);
971 assert(downstreamp_comp_name_quark
> 0);
972 downstream_comp
= g_hash_table_lookup(ctx
->components
,
973 (gpointer
) (long) downstreamp_comp_name_quark
);
974 if (!downstream_comp
) {
975 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
980 if (bt_component_is_filter(downstream_comp
)) {
981 port_count_fn
= bt_component_filter_get_input_port_count
;
982 port_by_index_fn
= bt_component_filter_get_input_port_by_index
;
983 } else if (bt_component_is_sink(downstream_comp
)) {
984 port_count_fn
= bt_component_sink_get_input_port_count
;
985 port_by_index_fn
= bt_component_sink_get_input_port_by_index
;
988 * Should never happen because the connections are
989 * validated before we get here.
994 downstream_port_count
= port_count_fn(downstream_comp
);
995 assert(downstream_port_count
>= 0);
997 for (i
= 0; i
< downstream_port_count
; i
++) {
998 struct bt_port
*downstream_port
=
999 port_by_index_fn(downstream_comp
, i
);
1000 const char *downstream_port_name
;
1002 assert(downstream_port
);
1004 /* Skip port if it's already connected */
1005 if (bt_port_is_connected(downstream_port
)) {
1006 bt_put(downstream_port
);
1010 downstream_port_name
= bt_port_get_name(downstream_port
);
1011 assert(downstream_port_name
);
1013 if (bt_common_star_glob_match(
1014 cfg_conn
->downstream_port_glob
->str
, -1ULL,
1015 downstream_port_name
, -1ULL)) {
1016 /* We have a winner! */
1017 conn
= bt_graph_connect_ports(ctx
->graph
,
1018 upstream_port
, downstream_port
);
1019 bt_put(downstream_port
);
1022 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1023 bt_port_get_name(upstream_port
),
1024 downstream_port_name
,
1025 cfg_conn
->arg
->str
);
1032 bt_put(downstream_port
);
1037 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1038 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1053 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1054 struct bt_port
*upstream_port
)
1057 const char *upstream_port_name
;
1058 const char *upstream_comp_name
;
1059 struct bt_component
*upstream_comp
= NULL
;
1063 assert(upstream_port
);
1064 upstream_port_name
= bt_port_get_name(upstream_port
);
1065 assert(upstream_port_name
);
1066 upstream_comp
= bt_port_get_component(upstream_port
);
1067 if (!upstream_comp
) {
1068 // TODO: log warning
1073 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1074 assert(upstream_comp_name
);
1076 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1077 struct bt_config_connection
*cfg_conn
=
1079 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1081 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1082 upstream_comp_name
) == 0) {
1083 if (bt_common_star_glob_match(
1084 cfg_conn
->upstream_port_glob
->str
,
1085 -1ULL, upstream_port_name
, -1ULL)) {
1086 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1087 ctx
, upstream_comp
, upstream_port
,
1091 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1094 cfg_conn
->arg
->str
);
1104 "Cannot create connection: upstream port `%s` does not match any connection\n",
1105 bt_port_get_name(upstream_port
));
1111 bt_put(upstream_comp
);
1116 void graph_port_added_listener(struct bt_port
*port
, void *data
)
1118 struct bt_component
*comp
= NULL
;
1119 struct cmd_run_ctx
*ctx
= data
;
1121 if (bt_port_is_connected(port
)) {
1122 // TODO: log warning
1126 comp
= bt_port_get_component(port
);
1128 // TODO: log warning
1132 if (!bt_port_is_output(port
)) {
1137 if (cmd_run_ctx_connect_upstream_port(ctx
, port
)) {
1139 fprintf(stderr
, "Added port could not be connected: aborting\n");
1149 void graph_port_removed_listener(struct bt_component
*component
,
1150 struct bt_port
*port
, void *data
)
1156 void graph_ports_connected_listener(struct bt_port
*upstream_port
,
1157 struct bt_port
*downstream_port
, void *data
)
1163 void graph_ports_disconnected_listener(
1164 struct bt_component
*upstream_component
,
1165 struct bt_component
*downstream_component
,
1166 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
1173 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1179 if (ctx
->components
) {
1180 g_hash_table_destroy(ctx
->components
);
1181 ctx
->components
= NULL
;
1189 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1194 ctx
->connect_ports
= false;
1195 ctx
->components
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
1197 if (!ctx
->components
) {
1201 ctx
->graph
= bt_graph_create();
1206 ret
= bt_graph_add_port_added_listener(ctx
->graph
,
1207 graph_port_added_listener
, ctx
);
1212 ret
= bt_graph_add_port_removed_listener(ctx
->graph
,
1213 graph_port_removed_listener
, ctx
);
1218 ret
= bt_graph_add_ports_connected_listener(ctx
->graph
,
1219 graph_ports_connected_listener
, ctx
);
1224 ret
= bt_graph_add_ports_disconnected_listener(ctx
->graph
,
1225 graph_ports_disconnected_listener
, ctx
);
1233 cmd_run_ctx_destroy(ctx
);
1241 int cmd_run_ctx_create_components_from_config_components(
1242 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
1245 struct bt_component_class
*comp_cls
= NULL
;
1246 struct bt_component
*comp
= NULL
;
1249 for (i
= 0; i
< cfg_components
->len
; i
++) {
1250 struct bt_config_component
*cfg_comp
=
1251 g_ptr_array_index(cfg_components
, i
);
1254 comp_cls
= find_component_class(cfg_comp
->plugin_name
->str
,
1255 cfg_comp
->comp_cls_name
->str
, cfg_comp
->type
);
1257 fprintf(stderr
, "%s%sCannot find component class %s",
1258 bt_common_color_bold(),
1259 bt_common_color_fg_red(),
1260 bt_common_color_reset());
1261 print_plugin_comp_cls_opt(stderr
,
1262 cfg_comp
->plugin_name
->str
,
1263 cfg_comp
->comp_cls_name
->str
,
1265 fprintf(stderr
, "\n");
1269 comp
= bt_component_create(comp_cls
,
1270 cfg_comp
->instance_name
->str
, cfg_comp
->params
);
1272 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
1273 bt_common_color_bold(),
1274 bt_common_color_fg_red(),
1275 cfg_comp
->instance_name
->str
,
1276 bt_common_color_reset());
1280 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
1282 g_hash_table_insert(ctx
->components
,
1283 (gpointer
) (long) quark
, comp
);
1300 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
1305 * Make sure that, during this phase, our graph's "port added"
1306 * listener does not connect ports while we are creating the
1307 * components because we have a special, initial phase for
1310 ctx
->connect_ports
= false;
1312 ret
= cmd_run_ctx_create_components_from_config_components(
1313 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
1319 ret
= cmd_run_ctx_create_components_from_config_components(
1320 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
1326 ret
= cmd_run_ctx_create_components_from_config_components(
1327 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
1338 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
1339 struct bt_component
*comp
,
1340 int64_t (*port_count_fn
)(struct bt_component
*),
1341 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t))
1347 count
= port_count_fn(comp
);
1350 for (i
= 0; i
< count
; i
++) {
1351 struct bt_port
*upstream_port
= port_by_index_fn(comp
, i
);
1353 assert(upstream_port
);
1354 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
1355 bt_put(upstream_port
);
1366 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
1369 GHashTableIter iter
;
1370 gpointer g_name_quark
, g_comp
;
1372 ctx
->connect_ports
= true;
1373 g_hash_table_iter_init(&iter
, ctx
->components
);
1375 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
1376 if (bt_component_is_source(g_comp
)) {
1377 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
1378 g_comp
, bt_component_source_get_output_port_count
,
1379 bt_component_source_get_output_port_by_index
);
1380 } else if (bt_component_is_filter(g_comp
)) {
1381 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
1382 g_comp
, bt_component_filter_get_output_port_count
,
1383 bt_component_filter_get_output_port_by_index
);
1396 int cmd_run(struct bt_config
*cfg
)
1399 struct cmd_run_ctx ctx
= { 0 };
1401 ret
= load_all_plugins(cfg
->plugin_paths
);
1406 /* Initialize the command's context and the graph object */
1407 if (cmd_run_ctx_init(&ctx
, cfg
)) {
1408 fprintf(stderr
, "Cannot initialize the command's context\n");
1412 /* Create the requested component instances */
1413 if (cmd_run_ctx_create_components(&ctx
)) {
1414 fprintf(stderr
, "Cannot create components\n");
1418 /* Connect the initially visible component ports */
1419 if (cmd_run_ctx_connect_ports(&ctx
)) {
1420 fprintf(stderr
, "Cannot connect initial component ports\n");
1426 enum bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
1428 switch (graph_status
) {
1429 case BT_GRAPH_STATUS_OK
:
1431 case BT_GRAPH_STATUS_AGAIN
:
1432 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
1433 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
1434 // TODO: check EINTR and signal handler
1438 case BT_COMPONENT_STATUS_END
:
1441 fprintf(stderr
, "Sink component returned an error, aborting...\n");
1454 cmd_run_ctx_destroy(&ctx
);
1459 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
1461 const char *env_clash
;
1463 if (!cfg
->command_name
) {
1467 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
1468 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
1472 if (g_file_test(cfg
->command_name
,
1473 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
1474 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
1476 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
1478 fprintf(stderr
, "\n");
1479 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
1484 int main(int argc
, const char **argv
)
1488 struct bt_config
*cfg
;
1491 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
1494 /* Quit without errors; typically usage/version */
1504 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
1509 babeltrace_debug
= cfg
->debug
;
1510 babeltrace_verbose
= cfg
->verbose
;
1513 if (cfg
->command_needs_plugins
) {
1514 ret
= load_all_plugins(cfg
->plugin_paths
);
1521 switch (cfg
->command
) {
1522 case BT_CONFIG_COMMAND_RUN
:
1525 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
1526 ret
= cmd_list_plugins(cfg
);
1528 case BT_CONFIG_COMMAND_HELP
:
1529 ret
= cmd_help(cfg
);
1531 case BT_CONFIG_COMMAND_QUERY
:
1532 ret
= cmd_query(cfg
);
1534 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
1535 ret
= cmd_print_ctf_metadata(cfg
);
1537 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
1538 ret
= cmd_print_lttng_live_sessions(cfg
);
1544 warn_command_name_and_directory_clash(cfg
);
1545 retcode
= ret
? 1 : 0;