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>
44 #include <babeltrace/ctf-ir/metadata.h> /* for clocks */
49 #include "babeltrace-cfg.h"
50 #include "babeltrace-cfg-connect.h"
51 #include "default-cfg.h"
53 GPtrArray
*loaded_plugins
;
56 void init_loaded_plugins_array(void)
58 loaded_plugins
= g_ptr_array_new_full(8, bt_put
);
62 void fini_loaded_plugins_array(void)
64 g_ptr_array_free(loaded_plugins
, TRUE
);
68 struct bt_plugin
*find_plugin(const char *name
)
71 struct bt_plugin
*plugin
= NULL
;
73 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
74 plugin
= g_ptr_array_index(loaded_plugins
, i
);
76 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
83 return bt_get(plugin
);
87 struct bt_component_class
*find_component_class(const char *plugin_name
,
88 const char *comp_class_name
,
89 enum bt_component_class_type comp_class_type
)
91 struct bt_component_class
*comp_class
= NULL
;
92 struct bt_plugin
*plugin
= find_plugin(plugin_name
);
98 comp_class
= bt_plugin_get_component_class_by_name_and_type(plugin
,
99 comp_class_name
, comp_class_type
);
106 void print_indent(size_t indent
)
110 for (i
= 0; i
< indent
; i
++) {
115 static char *escape_name_for_shell(const char *input
)
117 char *output
= g_malloc0(strlen(input
) * 5 + 1);
125 for (in
= input
; *in
!= '\0'; in
++) {
153 const char *component_type_str(enum bt_component_class_type type
)
156 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
158 case BT_COMPONENT_CLASS_TYPE_SINK
:
160 case BT_COMPONENT_CLASS_TYPE_FILTER
:
162 case BT_COMPONENT_CLASS_TYPE_UNKNOWN
:
168 static void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
169 const char *comp_cls_name
, enum bt_component_class_type type
)
171 char *shell_plugin_name
= NULL
;
172 char *shell_comp_cls_name
= NULL
;
174 shell_plugin_name
= escape_name_for_shell(plugin_name
);
175 if (!shell_plugin_name
) {
179 shell_comp_cls_name
= escape_name_for_shell(comp_cls_name
);
180 if (!shell_comp_cls_name
) {
184 fprintf(fh
, "%s%s--%s%s %s'%s%s%s%s.%s%s%s'",
185 bt_common_color_bold(),
186 bt_common_color_fg_cyan(),
187 component_type_str(type
),
188 bt_common_color_reset(),
189 bt_common_color_fg_default(),
190 bt_common_color_bold(),
191 bt_common_color_fg_blue(),
193 bt_common_color_fg_default(),
194 bt_common_color_fg_yellow(),
196 bt_common_color_reset());
199 g_free(shell_plugin_name
);
200 g_free(shell_comp_cls_name
);
204 void print_value(struct bt_value
*, size_t);
207 void print_value_rec(struct bt_value
*, size_t);
210 bool print_map_value(const char *key
, struct bt_value
*object
, void *data
)
212 size_t *indent
= data
;
214 print_indent(*indent
);
217 if (bt_value_is_array(object
) &&
218 bt_value_array_is_empty(object
)) {
223 if (bt_value_is_map(object
) &&
224 bt_value_map_is_empty(object
)) {
229 if (bt_value_is_array(object
) ||
230 bt_value_is_map(object
)) {
234 print_value_rec(object
, *indent
+ 2);
239 void print_value_rec(struct bt_value
*value
, size_t indent
)
252 switch (bt_value_get_type(value
)) {
253 case BT_VALUE_TYPE_NULL
:
254 printf("%snull%s\n", bt_common_color_bold(),
255 bt_common_color_reset());
257 case BT_VALUE_TYPE_BOOL
:
258 bt_value_bool_get(value
, &bool_val
);
259 printf("%s%s%s%s\n", bt_common_color_bold(),
260 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
261 bt_common_color_reset());
263 case BT_VALUE_TYPE_INTEGER
:
264 bt_value_integer_get(value
, &int_val
);
265 printf("%s%s%" PRId64
"%s\n", bt_common_color_bold(),
266 bt_common_color_fg_red(), int_val
,
267 bt_common_color_reset());
269 case BT_VALUE_TYPE_FLOAT
:
270 bt_value_float_get(value
, &dbl_val
);
271 printf("%s%s%lf%s\n", bt_common_color_bold(),
272 bt_common_color_fg_red(), dbl_val
,
273 bt_common_color_reset());
275 case BT_VALUE_TYPE_STRING
:
276 bt_value_string_get(value
, &str_val
);
277 printf("%s%s%s%s\n", bt_common_color_bold(),
278 bt_common_color_fg_green(), str_val
,
279 bt_common_color_reset());
281 case BT_VALUE_TYPE_ARRAY
:
282 size
= bt_value_array_size(value
);
286 print_indent(indent
);
291 for (i
= 0; i
< size
; i
++) {
292 struct bt_value
*element
=
293 bt_value_array_get(value
, i
);
296 print_indent(indent
);
299 if (bt_value_is_array(element
) &&
300 bt_value_array_is_empty(element
)) {
305 if (bt_value_is_map(element
) &&
306 bt_value_map_is_empty(element
)) {
311 if (bt_value_is_array(element
) ||
312 bt_value_is_map(element
)) {
316 print_value_rec(element
, indent
+ 2);
320 case BT_VALUE_TYPE_MAP
:
321 if (bt_value_map_is_empty(value
)) {
322 print_indent(indent
);
327 bt_value_map_foreach(value
, print_map_value
, &indent
);
335 void print_value(struct bt_value
*value
, size_t indent
)
337 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
338 print_indent(indent
);
341 print_value_rec(value
, indent
);
345 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
348 print_plugin_comp_cls_opt(stdout
, bt_config_component
->plugin_name
->str
,
349 bt_config_component
->comp_cls_name
->str
,
350 bt_config_component
->type
);
353 if (bt_config_component
->instance_name
->len
> 0) {
354 printf(" Name: %s\n",
355 bt_config_component
->instance_name
->str
);
358 printf(" Parameters:\n");
359 print_value(bt_config_component
->params
, 8);
363 void print_bt_config_components(GPtrArray
*array
)
367 for (i
= 0; i
< array
->len
; i
++) {
368 struct bt_config_component
*cfg_component
=
369 bt_config_get_component(array
, i
);
370 print_bt_config_component(cfg_component
);
371 BT_PUT(cfg_component
);
376 void print_plugin_paths(struct bt_value
*plugin_paths
)
378 printf(" Plugin paths:\n");
379 print_value(plugin_paths
, 4);
383 void print_cfg_run(struct bt_config
*cfg
)
387 print_plugin_paths(cfg
->plugin_paths
);
388 printf(" Source component instances:\n");
389 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
391 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
392 printf(" Filter component instances:\n");
393 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
396 printf(" Sink component instances:\n");
397 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
398 printf(" Connections:\n");
400 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
401 struct bt_config_connection
*cfg_connection
=
402 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
405 printf(" %s%s%s -> %s%s%s\n",
406 cfg_connection
->src_instance_name
->str
,
407 cfg_connection
->src_port_name
->len
> 0 ? "." : "",
408 cfg_connection
->src_port_name
->str
,
409 cfg_connection
->dst_instance_name
->str
,
410 cfg_connection
->dst_port_name
->len
> 0 ? "." : "",
411 cfg_connection
->dst_port_name
->str
);
416 void print_cfg_list_plugins(struct bt_config
*cfg
)
418 print_plugin_paths(cfg
->plugin_paths
);
422 void print_cfg_help(struct bt_config
*cfg
)
424 print_plugin_paths(cfg
->plugin_paths
);
428 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
430 print_plugin_paths(cfg
->plugin_paths
);
431 printf(" Path: %s\n", cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
435 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
437 print_plugin_paths(cfg
->plugin_paths
);
438 printf(" URL: %s\n", cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
442 void print_cfg_query(struct bt_config
*cfg
)
444 print_plugin_paths(cfg
->plugin_paths
);
445 printf(" Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
446 printf(" Component class:\n");
447 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
451 void print_cfg(struct bt_config
*cfg
)
453 if (!babeltrace_verbose
) {
457 printf("Configuration:\n");
458 printf(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
459 printf(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
461 switch (cfg
->command
) {
462 case BT_CONFIG_COMMAND_RUN
:
465 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
466 print_cfg_list_plugins(cfg
);
468 case BT_CONFIG_COMMAND_HELP
:
471 case BT_CONFIG_COMMAND_QUERY
:
472 print_cfg_query(cfg
);
474 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
475 print_cfg_print_ctf_metadata(cfg
);
477 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
478 print_cfg_print_lttng_live_sessions(cfg
);
486 struct bt_component
*create_trimmer(struct bt_config_component
*source_cfg
)
488 struct bt_component
*trimmer
= NULL
;
489 struct bt_component_class
*trimmer_class
= NULL
;
490 struct bt_value
*trimmer_params
= NULL
;
491 struct bt_value
*value
;
493 trimmer_params
= bt_value_map_create();
494 if (!trimmer_params
) {
498 value
= bt_value_map_get(source_cfg
->params
, "begin");
500 enum bt_value_status ret
;
502 ret
= bt_value_map_insert(trimmer_params
, "begin",
509 value
= bt_value_map_get(source_cfg
->params
, "end");
511 enum bt_value_status ret
;
513 ret
= bt_value_map_insert(trimmer_params
, "end",
520 value
= bt_value_map_get(source_cfg
->params
, "clock-gmt");
522 enum bt_value_status ret
;
524 ret
= bt_value_map_insert(trimmer_params
, "clock-gmt",
532 trimmer_class
= find_component_class("utils", "trimmer",
533 BT_COMPONENT_CLASS_TYPE_FILTER
);
534 if (!trimmer_class
) {
535 fprintf(stderr
, "Could not find trimmer component class. Aborting...\n");
538 trimmer
= bt_component_create(trimmer_class
, "source_trimmer",
544 bt_put(trimmer_params
);
545 bt_put(trimmer_class
);
550 int connect_source_sink(struct bt_graph
*graph
,
551 struct bt_component
*source
,
552 struct bt_config_component
*source_cfg
,
553 struct bt_component
*sink
)
556 struct bt_connection
*connection
= NULL
;
557 struct bt_component
*trimmer
= NULL
;
558 struct bt_port
*source_port
=
559 bt_component_source_get_default_output_port(source
);
560 struct bt_port
*sink_port
=
561 bt_component_sink_get_default_input_port(sink
);
562 struct bt_port
*to_sink_port
= NULL
;
563 struct bt_port
*trimmer_input_port
= NULL
;
566 fprintf(stderr
, "Failed to find default source output port. Aborting...\n");
571 fprintf(stderr
, "Failed to find default sink input port. Aborting...\n");
576 if (bt_value_map_has_key(source_cfg
->params
, "begin")
577 || bt_value_map_has_key(source_cfg
->params
, "end")) {
578 /* A trimmer must be inserted in the graph. */
579 trimmer
= create_trimmer(source_cfg
);
581 fprintf(stderr
, "Failed to create trimmer component. Aborting...\n");
586 trimmer_input_port
= bt_component_filter_get_default_input_port(
588 if (!trimmer_input_port
) {
589 fprintf(stderr
, "Failed to find trimmer input port. Aborting...\n");
593 to_sink_port
= bt_component_filter_get_default_output_port(
596 fprintf(stderr
, "Failed to find trimmer output port. Aborting...\n");
601 connection
= bt_graph_connect_ports(graph
, source_port
,
604 fprintf(stderr
, "Failed to connect source to trimmer. Aborting...\n");
610 BT_MOVE(to_sink_port
, source_port
);
613 connection
= bt_graph_connect_ports(graph
, to_sink_port
, sink_port
);
615 fprintf(stderr
, "Failed to connect to sink. Aborting...\n");
623 bt_put(to_sink_port
);
629 void add_to_loaded_plugins(struct bt_plugin
**plugins
)
632 struct bt_plugin
*plugin
= *plugins
;
633 /* Check if it's already loaded (from another path). */
634 struct bt_plugin
*loaded_plugin
=
635 find_plugin(bt_plugin_get_name(plugin
));
638 printf_verbose("Not loading plugin `%s`: already loaded from `%s`\n",
639 bt_plugin_get_path(plugin
),
640 bt_plugin_get_path(loaded_plugin
));
641 BT_PUT(loaded_plugin
);
644 /* Transfer ownership to global array. */
645 g_ptr_array_add(loaded_plugins
, plugin
);
652 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
654 int nr_paths
, i
, ret
= 0;
656 nr_paths
= bt_value_array_size(plugin_paths
);
662 for (i
= 0; i
< nr_paths
; i
++) {
663 struct bt_value
*plugin_path_value
= NULL
;
664 const char *plugin_path
;
665 struct bt_plugin
**plugins
;
667 plugin_path_value
= bt_value_array_get(plugin_paths
, i
);
668 if (bt_value_string_get(plugin_path_value
,
670 BT_PUT(plugin_path_value
);
674 plugins
= bt_plugin_create_all_from_dir(plugin_path
, false);
676 printf_debug("Unable to dynamically load plugins from path %s.\n",
678 BT_PUT(plugin_path_value
);
682 add_to_loaded_plugins(plugins
);
685 BT_PUT(plugin_path_value
);
692 int load_static_plugins(void)
695 struct bt_plugin
**plugins
;
697 plugins
= bt_plugin_create_all_from_static();
699 printf_debug("Unable to load static plugins.\n");
704 add_to_loaded_plugins(plugins
);
710 static int load_all_plugins(struct bt_value
*plugin_paths
)
714 if (load_dynamic_plugins(plugin_paths
)) {
715 fprintf(stderr
, "Failed to load dynamic plugins.\n");
720 if (load_static_plugins()) {
721 fprintf(stderr
, "Failed to load static plugins.\n");
730 static void print_plugin_info(struct bt_plugin
*plugin
)
732 unsigned int major
, minor
, patch
;
734 enum bt_plugin_status version_status
;
735 const char *plugin_name
;
739 const char *plugin_description
;
741 plugin_name
= bt_plugin_get_name(plugin
);
742 path
= bt_plugin_get_path(plugin
);
743 author
= bt_plugin_get_author(plugin
);
744 license
= bt_plugin_get_license(plugin
);
745 plugin_description
= bt_plugin_get_description(plugin
);
746 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
748 printf("%s%s%s%s:\n", bt_common_color_bold(),
749 bt_common_color_fg_blue(), plugin_name
,
750 bt_common_color_reset());
751 printf(" %sPath%s: %s\n", bt_common_color_bold(),
752 bt_common_color_reset(), path
? path
: "(None)");
754 if (version_status
== BT_PLUGIN_STATUS_OK
) {
755 printf(" %sVersion%s: %u.%u.%u",
756 bt_common_color_bold(), bt_common_color_reset(),
757 major
, minor
, patch
);
766 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
767 bt_common_color_reset(),
768 plugin_description
? plugin_description
: "(None)");
769 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
770 bt_common_color_reset(), author
? author
: "(Unknown)");
771 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
772 bt_common_color_reset(),
773 license
? license
: "(Unknown)");
776 static int cmd_query(struct bt_config
*cfg
)
779 struct bt_component_class
*comp_cls
= NULL
;
780 struct bt_value
*results
= NULL
;
782 ret
= load_all_plugins(cfg
->plugin_paths
);
787 comp_cls
= find_component_class(cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
788 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
789 cfg
->cmd_data
.query
.cfg_component
->type
);
791 fprintf(stderr
, "%s%sCannot find component class %s",
792 bt_common_color_bold(),
793 bt_common_color_fg_red(),
794 bt_common_color_reset());
795 print_plugin_comp_cls_opt(stderr
,
796 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
797 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
798 cfg
->cmd_data
.query
.cfg_component
->type
);
799 fprintf(stderr
, "\n");
804 results
= bt_component_class_query(comp_cls
,
805 cfg
->cmd_data
.query
.object
->str
,
806 cfg
->cmd_data
.query
.cfg_component
->params
);
808 fprintf(stderr
, "%s%sFailed to query info to %s",
809 bt_common_color_bold(),
810 bt_common_color_fg_red(),
811 bt_common_color_reset());
812 print_plugin_comp_cls_opt(stderr
,
813 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
814 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
815 cfg
->cmd_data
.query
.cfg_component
->type
);
816 fprintf(stderr
, "%s%s with object `%s`%s\n",
817 bt_common_color_bold(),
818 bt_common_color_fg_red(),
819 cfg
->cmd_data
.query
.object
->str
,
820 bt_common_color_reset());
825 print_value(results
, 0);
833 static int cmd_help(struct bt_config
*cfg
)
836 struct bt_plugin
*plugin
= NULL
;
839 ret
= load_all_plugins(cfg
->plugin_paths
);
844 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
846 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
847 bt_common_color_bold(), bt_common_color_fg_red(),
848 bt_common_color_fg_blue(),
849 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
850 bt_common_color_reset());
855 print_plugin_info(plugin
);
856 printf(" %sComponent classes%s: %d\n",
857 bt_common_color_bold(),
858 bt_common_color_reset(),
859 bt_plugin_get_component_class_count(plugin
));
862 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
863 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
864 struct bt_component_class
*needed_comp_cls
=
865 find_component_class(
866 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
867 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
868 cfg
->cmd_data
.help
.cfg_component
->type
);
870 if (!needed_comp_cls
) {
871 fprintf(stderr
, "\n%s%sCannot find component class %s",
872 bt_common_color_bold(),
873 bt_common_color_fg_red(),
874 bt_common_color_reset());
875 print_plugin_comp_cls_opt(stderr
,
876 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
877 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
878 cfg
->cmd_data
.help
.cfg_component
->type
);
879 fprintf(stderr
, "\n");
884 bt_put(needed_comp_cls
);
887 for (i
= 0; i
< bt_plugin_get_component_class_count(plugin
); i
++) {
888 struct bt_component_class
*comp_cls
=
889 bt_plugin_get_component_class(plugin
, i
);
890 const char *comp_class_name
=
891 bt_component_class_get_name(comp_cls
);
892 const char *comp_class_description
=
893 bt_component_class_get_description(comp_cls
);
894 const char *comp_class_help
=
895 bt_component_class_get_help(comp_cls
);
896 enum bt_component_class_type type
=
897 bt_component_class_get_type(comp_cls
);
901 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
902 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
903 if (strcmp(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
904 comp_class_name
) != 0 &&
906 cfg
->cmd_data
.help
.cfg_component
->type
) {
913 print_plugin_comp_cls_opt(stdout
,
914 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
918 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
919 bt_common_color_reset(),
920 comp_class_description
? comp_class_description
: "(None)");
922 if (comp_class_help
) {
923 printf("\n%s\n", comp_class_help
);
934 static int cmd_list_plugins(struct bt_config
*cfg
)
937 int plugins_count
, component_classes_count
= 0, i
;
939 ret
= load_all_plugins(cfg
->plugin_paths
);
944 printf("From the following plugin paths:\n\n");
945 print_value(cfg
->plugin_paths
, 2);
947 plugins_count
= loaded_plugins
->len
;
948 if (plugins_count
== 0) {
949 fprintf(stderr
, "%s%sNo plugins found.%s\n",
950 bt_common_color_bold(), bt_common_color_fg_red(),
951 bt_common_color_reset());
952 fprintf(stderr
, "\n");
953 fprintf(stderr
, "Please make sure your plugin search path is set correctly. You can use\n");
954 fprintf(stderr
, "the --plugin-path command-line option or the BABELTRACE_PLUGIN_PATH\n");
955 fprintf(stderr
, "environment variable.\n");
960 for (i
= 0; i
< plugins_count
; i
++) {
961 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
963 component_classes_count
+= bt_plugin_get_component_class_count(plugin
);
966 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
967 bt_common_color_bold(),
968 component_classes_count
,
969 bt_common_color_reset(),
970 bt_common_color_bold(),
972 bt_common_color_reset());
974 for (i
= 0; i
< plugins_count
; i
++) {
976 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
978 component_classes_count
=
979 bt_plugin_get_component_class_count(plugin
);
981 print_plugin_info(plugin
);
983 if (component_classes_count
== 0) {
984 printf(" %sComponent classes%s: (None)\n",
985 bt_common_color_bold(),
986 bt_common_color_reset());
988 printf(" %sComponent classes%s:\n",
989 bt_common_color_bold(),
990 bt_common_color_reset());
993 for (j
= 0; j
< component_classes_count
; j
++) {
994 struct bt_component_class
*comp_class
=
995 bt_plugin_get_component_class(plugin
, j
);
996 const char *comp_class_name
=
997 bt_component_class_get_name(comp_class
);
998 const char *comp_class_description
=
999 bt_component_class_get_description(comp_class
);
1000 enum bt_component_class_type type
=
1001 bt_component_class_get_type(comp_class
);
1004 print_plugin_comp_cls_opt(stdout
,
1005 bt_plugin_get_name(plugin
), comp_class_name
,
1008 if (comp_class_description
) {
1009 printf(": %s", comp_class_description
);
1021 static int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1027 static int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1030 struct bt_component_class
*comp_cls
= NULL
;
1031 struct bt_value
*results
= NULL
;
1032 struct bt_value
*params
= NULL
;
1033 struct bt_value
*metadata_text_value
= NULL
;
1034 const char *metadata_text
= NULL
;
1035 static const char * const plugin_name
= "ctf";
1036 static const char * const comp_cls_name
= "fs";
1037 static const enum bt_component_class_type comp_cls_type
=
1038 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1040 assert(cfg
->cmd_data
.print_ctf_metadata
.path
);
1041 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1044 fprintf(stderr
, "%s%sCannot find component class %s",
1045 bt_common_color_bold(),
1046 bt_common_color_fg_red(),
1047 bt_common_color_reset());
1048 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1049 comp_cls_name
, comp_cls_type
);
1050 fprintf(stderr
, "\n");
1055 params
= bt_value_map_create();
1061 ret
= bt_value_map_insert_string(params
, "path",
1062 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1068 results
= bt_component_class_query(comp_cls
, "metadata-info",
1072 fprintf(stderr
, "%s%sFailed to request metadata info%s\n",
1073 bt_common_color_bold(),
1074 bt_common_color_fg_red(),
1075 bt_common_color_reset());
1079 metadata_text_value
= bt_value_map_get(results
, "text");
1080 if (!metadata_text_value
) {
1085 ret
= bt_value_string_get(metadata_text_value
, &metadata_text
);
1087 printf("%s\n", metadata_text
);
1092 bt_put(metadata_text_value
);
1097 static int cmd_run(struct bt_config
*cfg
)
1100 struct bt_component_class
*source_class
= NULL
;
1101 struct bt_component_class
*sink_class
= NULL
;
1102 struct bt_component
*source
= NULL
, *sink
= NULL
;
1103 struct bt_value
*source_params
= NULL
, *sink_params
= NULL
;
1104 struct bt_config_component
*source_cfg
= NULL
, *sink_cfg
= NULL
;
1105 struct bt_graph
*graph
= NULL
;
1107 ret
= load_all_plugins(cfg
->plugin_paths
);
1112 /* TODO handle more than 1 source and 1 sink. */
1113 if (cfg
->cmd_data
.run
.sources
->len
!= 1 ||
1114 cfg
->cmd_data
.run
.sinks
->len
!= 1) {
1115 fprintf(stderr
, "Only one source and one sink component class are supported. Aborting...\n");
1120 source_cfg
= bt_config_get_component(cfg
->cmd_data
.run
.sources
, 0);
1121 source_params
= bt_get(source_cfg
->params
);
1122 source_class
= find_component_class(source_cfg
->plugin_name
->str
,
1123 source_cfg
->comp_cls_name
->str
,
1124 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1125 if (!source_class
) {
1126 fprintf(stderr
, "Could not find ");
1127 print_plugin_comp_cls_opt(stderr
, source_cfg
->plugin_name
->str
,
1128 source_cfg
->comp_cls_name
->str
, BT_COMPONENT_CLASS_TYPE_SOURCE
);
1129 fprintf(stderr
, ". Aborting...\n");
1134 sink_cfg
= bt_config_get_component(cfg
->cmd_data
.run
.sinks
, 0);
1135 sink_params
= bt_get(sink_cfg
->params
);
1136 sink_class
= find_component_class(sink_cfg
->plugin_name
->str
,
1137 sink_cfg
->comp_cls_name
->str
,
1138 BT_COMPONENT_CLASS_TYPE_SINK
);
1140 fprintf(stderr
, "Could not find ");
1141 print_plugin_comp_cls_opt(stderr
, sink_cfg
->plugin_name
->str
,
1142 sink_cfg
->comp_cls_name
->str
, BT_COMPONENT_CLASS_TYPE_SINK
);
1143 fprintf(stderr
, ". Aborting...\n");
1148 graph
= bt_graph_create();
1154 source
= bt_component_create(source_class
, "source", source_params
);
1156 fprintf(stderr
, "Failed to instantiate selected source component. Aborting...\n");
1161 sink
= bt_component_create(sink_class
, "sink", sink_params
);
1163 fprintf(stderr
, "Failed to instantiate selected output component. Aborting...\n");
1168 ret
= connect_source_sink(graph
, source
, source_cfg
, sink
);
1175 enum bt_graph_status graph_status
;
1177 graph_status
= bt_graph_run(graph
);
1178 switch (graph_status
) {
1179 case BT_GRAPH_STATUS_AGAIN
:
1180 /* Wait for an arbitraty 500 ms. */
1183 case BT_COMPONENT_STATUS_END
:
1186 fprintf(stderr
, "Sink component returned an error, aborting...\n");
1194 bt_put(source_class
);
1197 bt_put(source_params
);
1198 bt_put(sink_params
);
1205 static void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
1207 if (!cfg
->command_name
) {
1211 if (g_file_test(cfg
->command_name
,
1212 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
1213 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
1215 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
1217 fprintf(stderr
, "\n");
1218 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
1223 int main(int argc
, const char **argv
)
1227 struct bt_config
*cfg
;
1229 init_loaded_plugins_array();
1230 cfg
= bt_config_from_args_with_defaults(argc
, argv
, &retcode
);
1233 /* Quit without errors; typically usage/version */
1243 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
1248 babeltrace_debug
= cfg
->debug
;
1249 babeltrace_verbose
= cfg
->verbose
;
1252 if (cfg
->command_needs_plugins
) {
1253 ret
= load_all_plugins(cfg
->plugin_paths
);
1260 switch (cfg
->command
) {
1261 case BT_CONFIG_COMMAND_RUN
:
1264 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
1265 ret
= cmd_list_plugins(cfg
);
1267 case BT_CONFIG_COMMAND_HELP
:
1268 ret
= cmd_help(cfg
);
1270 case BT_CONFIG_COMMAND_QUERY
:
1271 ret
= cmd_query(cfg
);
1273 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
1274 ret
= cmd_print_ctf_metadata(cfg
);
1276 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
1277 ret
= cmd_print_lttng_live_sessions(cfg
);
1283 warn_command_name_and_directory_clash(cfg
);
1284 retcode
= ret
? 1 : 0;
1288 fini_loaded_plugins_array();