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 #define BT_LOG_TAG "CLI"
32 #include <babeltrace/babeltrace.h>
33 #include <babeltrace/plugin/plugin.h>
34 #include <babeltrace/common-internal.h>
35 #include <babeltrace/graph/component.h>
36 #include <babeltrace/graph/component-source.h>
37 #include <babeltrace/graph/component-sink.h>
38 #include <babeltrace/graph/component-filter.h>
39 #include <babeltrace/graph/component-class.h>
40 #include <babeltrace/graph/port.h>
41 #include <babeltrace/graph/graph.h>
42 #include <babeltrace/graph/connection.h>
43 #include <babeltrace/graph/notification-iterator.h>
44 #include <babeltrace/ref.h>
45 #include <babeltrace/values.h>
46 #include <babeltrace/logging.h>
56 #include "babeltrace-cfg.h"
57 #include "babeltrace-cfg-cli-args.h"
58 #include "babeltrace-cfg-cli-args-default.h"
60 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
62 /* Application's processing graph (weak) */
63 static struct bt_graph
*the_graph
;
64 static bool canceled
= false;
66 GPtrArray
*loaded_plugins
;
69 void sigint_handler(int signum
)
71 if (signum
!= SIGINT
) {
76 bt_graph_cancel(the_graph
);
83 void init_static_data(void)
85 loaded_plugins
= g_ptr_array_new_with_free_func(bt_put
);
89 void fini_static_data(void)
91 g_ptr_array_free(loaded_plugins
, TRUE
);
95 struct bt_plugin
*find_plugin(const char *name
)
98 struct bt_plugin
*plugin
= NULL
;
101 BT_LOGD("Finding plugin: name=\"%s\"", name
);
103 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
104 plugin
= g_ptr_array_index(loaded_plugins
, i
);
106 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
113 if (BT_LOG_ON_DEBUG
) {
115 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
117 BT_LOGD("Cannot find plugin.");
121 return bt_get(plugin
);
125 struct bt_component_class
*find_component_class(const char *plugin_name
,
126 const char *comp_class_name
,
127 enum bt_component_class_type comp_class_type
)
129 struct bt_component_class
*comp_class
= NULL
;
130 struct bt_plugin
*plugin
;
132 BT_LOGD("Finding component class: plugin-name=\"%s\", "
133 "comp-cls-name=\"%s\", comp-cls-type=%d",
134 plugin_name
, comp_class_name
, comp_class_type
);
136 plugin
= find_plugin(plugin_name
);
142 comp_class
= bt_plugin_get_component_class_by_name_and_type(plugin
,
143 comp_class_name
, comp_class_type
);
147 if (BT_LOG_ON_DEBUG
) {
149 BT_LOGD("Found component class: comp-cls-addr=%p",
152 BT_LOGD("Cannot find component class.");
160 void print_indent(FILE *fp
, size_t indent
)
164 for (i
= 0; i
< indent
; i
++) {
170 const char *component_type_str(enum bt_component_class_type type
)
173 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
175 case BT_COMPONENT_CLASS_TYPE_SINK
:
177 case BT_COMPONENT_CLASS_TYPE_FILTER
:
179 case BT_COMPONENT_CLASS_TYPE_UNKNOWN
:
186 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
187 const char *comp_cls_name
, enum bt_component_class_type type
)
189 GString
*shell_plugin_name
= NULL
;
190 GString
*shell_comp_cls_name
= NULL
;
192 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
193 if (!shell_plugin_name
) {
197 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
198 if (!shell_comp_cls_name
) {
202 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
203 bt_common_color_bold(),
204 bt_common_color_fg_cyan(),
205 component_type_str(type
),
206 bt_common_color_fg_default(),
207 bt_common_color_fg_blue(),
208 shell_plugin_name
->str
,
209 bt_common_color_fg_default(),
210 bt_common_color_fg_yellow(),
211 shell_comp_cls_name
->str
,
212 bt_common_color_reset());
215 if (shell_plugin_name
) {
216 g_string_free(shell_plugin_name
, TRUE
);
219 if (shell_comp_cls_name
) {
220 g_string_free(shell_comp_cls_name
, TRUE
);
225 void print_value(FILE *, struct bt_value
*, size_t);
228 void print_value_rec(FILE *, struct bt_value
*, size_t);
230 struct print_map_value_data
{
236 bt_bool
print_map_value(const char *key
, struct bt_value
*object
, void *data
)
238 struct print_map_value_data
*print_map_value_data
= data
;
240 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
241 fprintf(print_map_value_data
->fp
, "%s: ", key
);
243 if (bt_value_is_array(object
) &&
244 bt_value_array_is_empty(object
)) {
245 fprintf(print_map_value_data
->fp
, "[ ]\n");
249 if (bt_value_is_map(object
) &&
250 bt_value_map_is_empty(object
)) {
251 fprintf(print_map_value_data
->fp
, "{ }\n");
255 if (bt_value_is_array(object
) ||
256 bt_value_is_map(object
)) {
257 fprintf(print_map_value_data
->fp
, "\n");
260 print_value_rec(print_map_value_data
->fp
, object
,
261 print_map_value_data
->indent
+ 2);
266 void print_value_rec(FILE *fp
, struct bt_value
*value
, size_t indent
)
279 switch (bt_value_get_type(value
)) {
280 case BT_VALUE_TYPE_NULL
:
281 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
282 bt_common_color_reset());
284 case BT_VALUE_TYPE_BOOL
:
285 bt_value_bool_get(value
, &bool_val
);
286 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
287 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
288 bt_common_color_reset());
290 case BT_VALUE_TYPE_INTEGER
:
291 bt_value_integer_get(value
, &int_val
);
292 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
293 bt_common_color_fg_red(), int_val
,
294 bt_common_color_reset());
296 case BT_VALUE_TYPE_FLOAT
:
297 bt_value_float_get(value
, &dbl_val
);
298 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
299 bt_common_color_fg_red(), dbl_val
,
300 bt_common_color_reset());
302 case BT_VALUE_TYPE_STRING
:
303 bt_value_string_get(value
, &str_val
);
304 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
305 bt_common_color_fg_green(), str_val
,
306 bt_common_color_reset());
308 case BT_VALUE_TYPE_ARRAY
:
309 size
= bt_value_array_size(value
);
313 print_indent(fp
, indent
);
314 fprintf(fp
, "[ ]\n");
318 for (i
= 0; i
< size
; i
++) {
319 struct bt_value
*element
=
320 bt_value_array_get(value
, i
);
323 print_indent(fp
, indent
);
326 if (bt_value_is_array(element
) &&
327 bt_value_array_is_empty(element
)) {
328 fprintf(fp
, "[ ]\n");
332 if (bt_value_is_map(element
) &&
333 bt_value_map_is_empty(element
)) {
334 fprintf(fp
, "{ }\n");
338 if (bt_value_is_array(element
) ||
339 bt_value_is_map(element
)) {
343 print_value_rec(fp
, element
, indent
+ 2);
347 case BT_VALUE_TYPE_MAP
:
349 struct print_map_value_data data
= {
354 if (bt_value_map_is_empty(value
)) {
355 print_indent(fp
, indent
);
356 fprintf(fp
, "{ }\n");
360 bt_value_map_foreach(value
, print_map_value
, &data
);
369 void print_value(FILE *fp
, struct bt_value
*value
, size_t indent
)
371 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
372 print_indent(fp
, indent
);
375 print_value_rec(fp
, value
, indent
);
379 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
381 fprintf(stderr
, " ");
382 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
383 bt_config_component
->comp_cls_name
->str
,
384 bt_config_component
->type
);
385 fprintf(stderr
, ":\n");
387 if (bt_config_component
->instance_name
->len
> 0) {
388 fprintf(stderr
, " Name: %s\n",
389 bt_config_component
->instance_name
->str
);
392 fprintf(stderr
, " Parameters:\n");
393 print_value(stderr
, bt_config_component
->params
, 8);
397 void print_bt_config_components(GPtrArray
*array
)
401 for (i
= 0; i
< array
->len
; i
++) {
402 struct bt_config_component
*cfg_component
=
403 bt_config_get_component(array
, i
);
404 print_bt_config_component(cfg_component
);
405 BT_PUT(cfg_component
);
410 void print_plugin_paths(struct bt_value
*plugin_paths
)
412 fprintf(stderr
, " Plugin paths:\n");
413 print_value(stderr
, plugin_paths
, 4);
417 void print_cfg_run(struct bt_config
*cfg
)
421 print_plugin_paths(cfg
->plugin_paths
);
422 fprintf(stderr
, " Source component instances:\n");
423 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
425 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
426 fprintf(stderr
, " Filter component instances:\n");
427 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
430 fprintf(stderr
, " Sink component instances:\n");
431 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
432 fprintf(stderr
, " Connections:\n");
434 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
435 struct bt_config_connection
*cfg_connection
=
436 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
439 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
440 cfg_connection
->upstream_comp_name
->str
,
441 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
442 cfg_connection
->upstream_port_glob
->str
,
443 cfg_connection
->downstream_comp_name
->str
,
444 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
445 cfg_connection
->downstream_port_glob
->str
);
450 void print_cfg_list_plugins(struct bt_config
*cfg
)
452 print_plugin_paths(cfg
->plugin_paths
);
456 void print_cfg_help(struct bt_config
*cfg
)
458 print_plugin_paths(cfg
->plugin_paths
);
462 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
464 print_plugin_paths(cfg
->plugin_paths
);
465 fprintf(stderr
, " Path: %s\n",
466 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
470 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
472 print_plugin_paths(cfg
->plugin_paths
);
473 fprintf(stderr
, " URL: %s\n",
474 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
478 void print_cfg_query(struct bt_config
*cfg
)
480 print_plugin_paths(cfg
->plugin_paths
);
481 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
482 fprintf(stderr
, " Component class:\n");
483 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
487 void print_cfg(struct bt_config
*cfg
)
489 if (!BT_LOG_ON_INFO
) {
493 BT_LOGI_STR("Configuration:");
494 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
495 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
497 switch (cfg
->command
) {
498 case BT_CONFIG_COMMAND_RUN
:
501 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
502 print_cfg_list_plugins(cfg
);
504 case BT_CONFIG_COMMAND_HELP
:
507 case BT_CONFIG_COMMAND_QUERY
:
508 print_cfg_query(cfg
);
510 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
511 print_cfg_print_ctf_metadata(cfg
);
513 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
514 print_cfg_print_lttng_live_sessions(cfg
);
522 void add_to_loaded_plugins(struct bt_plugin_set
*plugin_set
)
527 count
= bt_plugin_set_get_plugin_count(plugin_set
);
530 for (i
= 0; i
< count
; i
++) {
531 struct bt_plugin
*plugin
=
532 bt_plugin_set_get_plugin(plugin_set
, i
);
533 struct bt_plugin
*loaded_plugin
=
534 find_plugin(bt_plugin_get_name(plugin
));
539 BT_LOGI("Not using plugin: another one already exists with the same name: "
540 "plugin-name=\"%s\", plugin-path=\"%s\", "
541 "existing-plugin-path=\"%s\"",
542 bt_plugin_get_name(plugin
),
543 bt_plugin_get_path(plugin
),
544 bt_plugin_get_path(loaded_plugin
));
545 bt_put(loaded_plugin
);
547 /* Add to global array. */
548 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
549 bt_plugin_get_name(plugin
));
550 g_ptr_array_add(loaded_plugins
, bt_get(plugin
));
558 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
560 int nr_paths
, i
, ret
= 0;
562 nr_paths
= bt_value_array_size(plugin_paths
);
564 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
569 BT_LOGI("Loading dynamic plugins.");
571 for (i
= 0; i
< nr_paths
; i
++) {
572 struct bt_value
*plugin_path_value
= NULL
;
573 const char *plugin_path
;
574 struct bt_plugin_set
*plugin_set
;
576 plugin_path_value
= bt_value_array_get(plugin_paths
, i
);
577 bt_value_string_get(plugin_path_value
, &plugin_path
);
581 * Skip this if the directory does not exist because
582 * bt_plugin_create_all_from_dir() expects an existing
585 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
586 BT_LOGV("Skipping nonexistent directory path: "
587 "path=\"%s\"", plugin_path
);
588 BT_PUT(plugin_path_value
);
592 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
594 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
596 BT_PUT(plugin_path_value
);
600 add_to_loaded_plugins(plugin_set
);
602 BT_PUT(plugin_path_value
);
609 int load_static_plugins(void)
612 struct bt_plugin_set
*plugin_set
;
614 BT_LOGI("Loading static plugins.");
615 plugin_set
= bt_plugin_create_all_from_static();
617 BT_LOGE("Unable to load static plugins.");
622 add_to_loaded_plugins(plugin_set
);
629 int load_all_plugins(struct bt_value
*plugin_paths
)
633 if (load_dynamic_plugins(plugin_paths
)) {
638 if (load_static_plugins()) {
643 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
650 void print_plugin_info(struct bt_plugin
*plugin
)
652 unsigned int major
, minor
, patch
;
654 enum bt_plugin_status version_status
;
655 const char *plugin_name
;
659 const char *plugin_description
;
661 plugin_name
= bt_plugin_get_name(plugin
);
662 path
= bt_plugin_get_path(plugin
);
663 author
= bt_plugin_get_author(plugin
);
664 license
= bt_plugin_get_license(plugin
);
665 plugin_description
= bt_plugin_get_description(plugin
);
666 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
668 printf("%s%s%s%s:\n", bt_common_color_bold(),
669 bt_common_color_fg_blue(), plugin_name
,
670 bt_common_color_reset());
671 printf(" %sPath%s: %s\n", bt_common_color_bold(),
672 bt_common_color_reset(), path
? path
: "(None)");
674 if (version_status
== BT_PLUGIN_STATUS_OK
) {
675 printf(" %sVersion%s: %u.%u.%u",
676 bt_common_color_bold(), bt_common_color_reset(),
677 major
, minor
, patch
);
686 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
687 bt_common_color_reset(),
688 plugin_description
? plugin_description
: "(None)");
689 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
690 bt_common_color_reset(), author
? author
: "(Unknown)");
691 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
692 bt_common_color_reset(),
693 license
? license
: "(Unknown)");
697 int cmd_query(struct bt_config
*cfg
)
700 struct bt_component_class
*comp_cls
= NULL
;
701 struct bt_value
*results
= NULL
;
703 comp_cls
= find_component_class(cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
704 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
705 cfg
->cmd_data
.query
.cfg_component
->type
);
707 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
708 "comp-cls-name=\"%s\", comp-cls-type=%d",
709 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
710 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
711 cfg
->cmd_data
.query
.cfg_component
->type
);
712 fprintf(stderr
, "%s%sCannot find component class %s",
713 bt_common_color_bold(),
714 bt_common_color_fg_red(),
715 bt_common_color_reset());
716 print_plugin_comp_cls_opt(stderr
,
717 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
718 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
719 cfg
->cmd_data
.query
.cfg_component
->type
);
720 fprintf(stderr
, "\n");
725 results
= bt_component_class_query(comp_cls
,
726 cfg
->cmd_data
.query
.object
->str
,
727 cfg
->cmd_data
.query
.cfg_component
->params
);
729 BT_LOGE("Failed to query component class: plugin-name=\"%s\", "
730 "comp-cls-name=\"%s\", comp-cls-type=%d "
732 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
733 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
734 cfg
->cmd_data
.query
.cfg_component
->type
,
735 cfg
->cmd_data
.query
.object
->str
);
736 fprintf(stderr
, "%s%sFailed to query info to %s",
737 bt_common_color_bold(),
738 bt_common_color_fg_red(),
739 bt_common_color_reset());
740 print_plugin_comp_cls_opt(stderr
,
741 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
742 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
743 cfg
->cmd_data
.query
.cfg_component
->type
);
744 fprintf(stderr
, "%s%s with object `%s`%s\n",
745 bt_common_color_bold(),
746 bt_common_color_fg_red(),
747 cfg
->cmd_data
.query
.object
->str
,
748 bt_common_color_reset());
753 print_value(stdout
, results
, 0);
762 int cmd_help(struct bt_config
*cfg
)
765 struct bt_plugin
*plugin
= NULL
;
768 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
770 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
771 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
772 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
773 bt_common_color_bold(), bt_common_color_fg_red(),
774 bt_common_color_fg_blue(),
775 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
776 bt_common_color_reset());
781 print_plugin_info(plugin
);
782 printf(" %sComponent classes%s: %d\n",
783 bt_common_color_bold(),
784 bt_common_color_reset(),
785 (int) bt_plugin_get_component_class_count(plugin
));
788 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
789 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
790 struct bt_component_class
*needed_comp_cls
=
791 find_component_class(
792 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
793 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
794 cfg
->cmd_data
.help
.cfg_component
->type
);
796 if (!needed_comp_cls
) {
797 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
798 "comp-cls-name=\"%s\", comp-cls-type=%d",
799 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
800 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
801 cfg
->cmd_data
.help
.cfg_component
->type
);
802 fprintf(stderr
, "\n%s%sCannot find component class %s",
803 bt_common_color_bold(),
804 bt_common_color_fg_red(),
805 bt_common_color_reset());
806 print_plugin_comp_cls_opt(stderr
,
807 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
808 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
809 cfg
->cmd_data
.help
.cfg_component
->type
);
810 fprintf(stderr
, "\n");
815 bt_put(needed_comp_cls
);
818 for (i
= 0; i
< bt_plugin_get_component_class_count(plugin
); i
++) {
819 struct bt_component_class
*comp_cls
=
820 bt_plugin_get_component_class_by_index(plugin
, i
);
821 const char *comp_class_name
=
822 bt_component_class_get_name(comp_cls
);
823 const char *comp_class_description
=
824 bt_component_class_get_description(comp_cls
);
825 const char *comp_class_help
=
826 bt_component_class_get_help(comp_cls
);
827 enum bt_component_class_type type
=
828 bt_component_class_get_type(comp_cls
);
832 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
833 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
834 if (strcmp(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
835 comp_class_name
) != 0 &&
837 cfg
->cmd_data
.help
.cfg_component
->type
) {
844 print_plugin_comp_cls_opt(stdout
,
845 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
849 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
850 bt_common_color_reset(),
851 comp_class_description
? comp_class_description
: "(None)");
853 if (comp_class_help
) {
854 printf("\n%s\n", comp_class_help
);
866 int cmd_list_plugins(struct bt_config
*cfg
)
869 int plugins_count
, component_classes_count
= 0, i
;
871 printf("From the following plugin paths:\n\n");
872 print_value(stdout
, cfg
->plugin_paths
, 2);
874 plugins_count
= loaded_plugins
->len
;
875 if (plugins_count
== 0) {
876 printf("No plugins found.\n");
880 for (i
= 0; i
< plugins_count
; i
++) {
881 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
883 component_classes_count
+= bt_plugin_get_component_class_count(plugin
);
886 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
887 bt_common_color_bold(),
888 component_classes_count
,
889 bt_common_color_reset(),
890 bt_common_color_bold(),
892 bt_common_color_reset());
894 for (i
= 0; i
< plugins_count
; i
++) {
896 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
898 component_classes_count
=
899 bt_plugin_get_component_class_count(plugin
);
901 print_plugin_info(plugin
);
903 if (component_classes_count
== 0) {
904 printf(" %sComponent classes%s: (none)\n",
905 bt_common_color_bold(),
906 bt_common_color_reset());
908 printf(" %sComponent classes%s:\n",
909 bt_common_color_bold(),
910 bt_common_color_reset());
913 for (j
= 0; j
< component_classes_count
; j
++) {
914 struct bt_component_class
*comp_class
=
915 bt_plugin_get_component_class_by_index(
917 const char *comp_class_name
=
918 bt_component_class_get_name(comp_class
);
919 const char *comp_class_description
=
920 bt_component_class_get_description(comp_class
);
921 enum bt_component_class_type type
=
922 bt_component_class_get_type(comp_class
);
925 print_plugin_comp_cls_opt(stdout
,
926 bt_plugin_get_name(plugin
), comp_class_name
,
929 if (comp_class_description
) {
930 printf(": %s", comp_class_description
);
943 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
946 struct bt_component_class
*comp_cls
= NULL
;
947 struct bt_value
*results
= NULL
;
948 struct bt_value
*params
= NULL
;
949 struct bt_value
*map
= NULL
;
950 struct bt_value
*v
= NULL
;
951 static const char * const plugin_name
= "ctf";
952 static const char * const comp_cls_name
= "lttng-live";
953 static const enum bt_component_class_type comp_cls_type
=
954 BT_COMPONENT_CLASS_TYPE_SOURCE
;
955 int64_t array_size
, i
;
957 assert(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
958 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
961 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
962 "comp-cls-name=\"%s\", comp-cls-type=%d",
963 plugin_name
, comp_cls_name
,
964 BT_COMPONENT_CLASS_TYPE_SOURCE
);
965 fprintf(stderr
, "%s%sCannot find component class %s",
966 bt_common_color_bold(),
967 bt_common_color_fg_red(),
968 bt_common_color_reset());
969 print_plugin_comp_cls_opt(stderr
, plugin_name
,
970 comp_cls_name
, comp_cls_type
);
971 fprintf(stderr
, "\n");
975 params
= bt_value_map_create();
980 ret
= bt_value_map_insert_string(params
, "url",
981 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
986 results
= bt_component_class_query(comp_cls
, "sessions",
989 BT_LOGE_STR("Failed to query for sessions.");
990 fprintf(stderr
, "%s%sFailed to request sessions%s\n",
991 bt_common_color_bold(),
992 bt_common_color_fg_red(),
993 bt_common_color_reset());
997 if (!bt_value_is_array(results
)) {
998 BT_LOGE_STR("Expecting an array for sessions query.");
999 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1000 bt_common_color_bold(),
1001 bt_common_color_fg_red(),
1002 bt_common_color_reset());
1006 array_size
= bt_value_array_size(results
);
1007 for (i
= 0; i
< array_size
; i
++) {
1008 const char *url_text
;
1009 int64_t timer_us
, streams
, clients
;
1011 map
= bt_value_array_get(results
, i
);
1013 BT_LOGE_STR("Unexpected empty array entry.");
1016 if (!bt_value_is_map(map
)) {
1017 BT_LOGE_STR("Unexpected entry type.");
1021 v
= bt_value_map_get(map
, "url");
1023 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1026 ret
= bt_value_string_get(v
, &url_text
);
1028 printf("%s", url_text
);
1031 v
= bt_value_map_get(map
, "timer-us");
1033 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1036 ret
= bt_value_integer_get(v
, &timer_us
);
1038 printf(" (timer = %" PRIu64
", ", timer_us
);
1041 v
= bt_value_map_get(map
, "stream-count");
1043 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1046 ret
= bt_value_integer_get(v
, &streams
);
1048 printf("%" PRIu64
" stream(s), ", streams
);
1051 v
= bt_value_map_get(map
, "client-count");
1053 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1056 ret
= bt_value_integer_get(v
, &clients
);
1058 printf("%" PRIu64
" client(s) connected)\n", clients
);
1077 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1080 struct bt_component_class
*comp_cls
= NULL
;
1081 struct bt_value
*results
= NULL
;
1082 struct bt_value
*params
= NULL
;
1083 struct bt_value
*metadata_text_value
= NULL
;
1084 const char *metadata_text
= NULL
;
1085 static const char * const plugin_name
= "ctf";
1086 static const char * const comp_cls_name
= "fs";
1087 static const enum bt_component_class_type comp_cls_type
=
1088 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1090 assert(cfg
->cmd_data
.print_ctf_metadata
.path
);
1091 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1094 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1095 "comp-cls-name=\"%s\", comp-cls-type=%d",
1096 plugin_name
, comp_cls_name
,
1097 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1098 fprintf(stderr
, "%s%sCannot find component class %s",
1099 bt_common_color_bold(),
1100 bt_common_color_fg_red(),
1101 bt_common_color_reset());
1102 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1103 comp_cls_name
, comp_cls_type
);
1104 fprintf(stderr
, "\n");
1109 params
= bt_value_map_create();
1115 ret
= bt_value_map_insert_string(params
, "path",
1116 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1122 results
= bt_component_class_query(comp_cls
, "metadata-info",
1126 BT_LOGE_STR("Failed to query for metadata info.");
1127 fprintf(stderr
, "%s%sFailed to request metadata info%s\n",
1128 bt_common_color_bold(),
1129 bt_common_color_fg_red(),
1130 bt_common_color_reset());
1134 metadata_text_value
= bt_value_map_get(results
, "text");
1135 if (!metadata_text_value
) {
1136 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1141 ret
= bt_value_string_get(metadata_text_value
, &metadata_text
);
1143 printf("%s\n", metadata_text
);
1148 bt_put(metadata_text_value
);
1153 struct cmd_run_ctx
{
1155 GHashTable
*components
;
1158 struct bt_graph
*graph
;
1161 struct bt_config
*cfg
;
1167 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1168 struct cmd_run_ctx
*ctx
, struct bt_component
*upstream_comp
,
1169 struct bt_port
*upstream_port
,
1170 struct bt_config_connection
*cfg_conn
)
1173 GQuark downstreamp_comp_name_quark
;
1174 struct bt_component
*downstream_comp
;
1175 int64_t downstream_port_count
;
1177 int64_t (*port_count_fn
)(struct bt_component
*);
1178 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
1179 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1181 BT_LOGI("Connecting upstream port to the next available downstream port: "
1182 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1183 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1184 upstream_port
, bt_port_get_name(upstream_port
),
1185 cfg_conn
->downstream_comp_name
->str
,
1186 cfg_conn
->arg
->str
);
1187 downstreamp_comp_name_quark
= g_quark_from_string(
1188 cfg_conn
->downstream_comp_name
->str
);
1189 assert(downstreamp_comp_name_quark
> 0);
1190 downstream_comp
= g_hash_table_lookup(ctx
->components
,
1191 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1192 if (!downstream_comp
) {
1193 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1194 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1195 cfg_conn
->arg
->str
);
1196 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1197 cfg_conn
->arg
->str
);
1201 if (bt_component_is_filter(downstream_comp
)) {
1202 port_count_fn
= bt_component_filter_get_input_port_count
;
1203 port_by_index_fn
= bt_component_filter_get_input_port_by_index
;
1204 } else if (bt_component_is_sink(downstream_comp
)) {
1205 port_count_fn
= bt_component_sink_get_input_port_count
;
1206 port_by_index_fn
= bt_component_sink_get_input_port_by_index
;
1209 * Should never happen because the connections are
1210 * validated before we get here.
1212 BT_LOGF("Invalid connection: downstream component is a source: "
1213 "conn-arg=\"%s\"", cfg_conn
->arg
->str
);
1217 downstream_port_count
= port_count_fn(downstream_comp
);
1218 assert(downstream_port_count
>= 0);
1220 for (i
= 0; i
< downstream_port_count
; i
++) {
1221 struct bt_port
*downstream_port
=
1222 port_by_index_fn(downstream_comp
, i
);
1223 const char *downstream_port_name
;
1225 assert(downstream_port
);
1227 /* Skip port if it's already connected */
1228 if (bt_port_is_connected(downstream_port
)) {
1229 bt_put(downstream_port
);
1230 BT_LOGD("Skipping downstream port: already connected: "
1231 "port-addr=%p, port-name=\"%s\"",
1233 bt_port_get_name(downstream_port
));
1237 downstream_port_name
= bt_port_get_name(downstream_port
);
1238 assert(downstream_port_name
);
1240 if (bt_common_star_glob_match(
1241 cfg_conn
->downstream_port_glob
->str
, -1ULL,
1242 downstream_port_name
, -1ULL)) {
1243 /* We have a winner! */
1244 status
= bt_graph_connect_ports(ctx
->graph
,
1245 upstream_port
, downstream_port
, NULL
);
1246 bt_put(downstream_port
);
1248 case BT_GRAPH_STATUS_OK
:
1250 case BT_GRAPH_STATUS_CANCELED
:
1251 BT_LOGI_STR("Graph was canceled by user.");
1252 status
= BT_GRAPH_STATUS_OK
;
1254 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1255 BT_LOGE("A component refused a connection to one of its ports: "
1256 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1257 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1258 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1259 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1261 upstream_comp
, bt_component_get_name(upstream_comp
),
1262 upstream_port
, bt_port_get_name(upstream_port
),
1263 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1264 downstream_port
, downstream_port_name
,
1265 cfg_conn
->arg
->str
);
1267 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1268 bt_port_get_name(upstream_port
),
1269 downstream_port_name
,
1270 cfg_conn
->arg
->str
);
1273 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1274 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1275 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1276 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1277 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1279 upstream_comp
, bt_component_get_name(upstream_comp
),
1280 upstream_port
, bt_port_get_name(upstream_port
),
1281 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1282 downstream_port
, downstream_port_name
,
1283 cfg_conn
->arg
->str
);
1285 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1286 bt_port_get_name(upstream_port
),
1287 downstream_port_name
,
1288 cfg_conn
->arg
->str
);
1292 BT_LOGI("Connected component ports: "
1293 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1294 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1295 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1296 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1298 upstream_comp
, bt_component_get_name(upstream_comp
),
1299 upstream_port
, bt_port_get_name(upstream_port
),
1300 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1301 downstream_port
, downstream_port_name
,
1302 cfg_conn
->arg
->str
);
1307 bt_put(downstream_port
);
1310 if (status
!= BT_GRAPH_STATUS_OK
) {
1311 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1312 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1313 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1314 upstream_port
, bt_port_get_name(upstream_port
),
1315 cfg_conn
->downstream_comp_name
->str
,
1316 cfg_conn
->arg
->str
);
1318 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1319 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1333 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1334 struct bt_port
*upstream_port
)
1337 const char *upstream_port_name
;
1338 const char *upstream_comp_name
;
1339 struct bt_component
*upstream_comp
= NULL
;
1343 assert(upstream_port
);
1344 upstream_port_name
= bt_port_get_name(upstream_port
);
1345 assert(upstream_port_name
);
1346 upstream_comp
= bt_port_get_component(upstream_port
);
1347 if (!upstream_comp
) {
1348 BT_LOGW("Upstream port to connect is not part of a component: "
1349 "port-addr=%p, port-name=\"%s\"",
1350 upstream_port
, upstream_port_name
);
1355 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1356 assert(upstream_comp_name
);
1357 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1358 "port-addr=%p, port-name=\"%s\"",
1359 upstream_comp
, upstream_comp_name
,
1360 upstream_port
, upstream_port_name
);
1362 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1363 struct bt_config_connection
*cfg_conn
=
1365 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1367 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1368 upstream_comp_name
) == 0) {
1369 if (bt_common_star_glob_match(
1370 cfg_conn
->upstream_port_glob
->str
,
1371 -1ULL, upstream_port_name
, -1ULL)) {
1372 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1373 ctx
, upstream_comp
, upstream_port
,
1376 BT_LOGE("Cannot connect upstream port: "
1377 "port-addr=%p, port-name=\"%s\"",
1379 upstream_port_name
);
1381 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1384 cfg_conn
->arg
->str
);
1393 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1394 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1395 upstream_port_name
);
1397 "Cannot create connection: upstream port `%s` does not match any connection\n",
1398 upstream_port_name
);
1404 bt_put(upstream_comp
);
1409 void graph_port_added_listener(struct bt_port
*port
, void *data
)
1411 struct bt_component
*comp
= NULL
;
1412 struct cmd_run_ctx
*ctx
= data
;
1414 comp
= bt_port_get_component(port
);
1415 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1416 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1417 comp
, comp
? bt_component_get_name(comp
) : "",
1418 port
, bt_port_get_name(port
));
1420 if (!ctx
->connect_ports
) {
1425 BT_LOGW_STR("Port has no component.");
1429 if (bt_port_is_connected(port
)) {
1430 BT_LOGW_STR("Port is already connected.");
1434 if (!bt_port_is_output(port
)) {
1435 BT_LOGI_STR("Skipping input port.");
1439 if (cmd_run_ctx_connect_upstream_port(ctx
, port
)) {
1440 BT_LOGF_STR("Cannot connect upstream port.");
1441 fprintf(stderr
, "Added port could not be connected: aborting\n");
1451 void graph_port_removed_listener(struct bt_component
*component
,
1452 struct bt_port
*port
, void *data
)
1454 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1455 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1456 component
, bt_component_get_name(component
),
1457 port
, bt_port_get_name(port
));
1461 void graph_ports_connected_listener(struct bt_port
*upstream_port
,
1462 struct bt_port
*downstream_port
, void *data
)
1464 struct bt_component
*upstream_comp
= bt_port_get_component(upstream_port
);
1465 struct bt_component
*downstream_comp
= bt_port_get_component(downstream_port
);
1467 assert(upstream_comp
);
1468 assert(downstream_comp
);
1469 BT_LOGI("Graph's component ports connected: "
1470 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1471 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1472 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1473 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1474 upstream_comp
, bt_component_get_name(upstream_comp
),
1475 upstream_port
, bt_port_get_name(upstream_port
),
1476 downstream_comp
, bt_component_get_name(downstream_comp
),
1477 downstream_port
, bt_port_get_name(downstream_port
));
1478 bt_put(upstream_comp
);
1479 bt_put(downstream_comp
);
1483 void graph_ports_disconnected_listener(
1484 struct bt_component
*upstream_component
,
1485 struct bt_component
*downstream_component
,
1486 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
1489 BT_LOGI("Graph's component ports disconnected: "
1490 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1491 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1492 upstream_port
, bt_port_get_name(upstream_port
),
1493 downstream_port
, bt_port_get_name(downstream_port
));
1497 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1503 if (ctx
->components
) {
1504 g_hash_table_destroy(ctx
->components
);
1505 ctx
->components
= NULL
;
1514 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1519 ctx
->connect_ports
= false;
1520 ctx
->components
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
1522 if (!ctx
->components
) {
1526 ctx
->graph
= bt_graph_create();
1531 the_graph
= ctx
->graph
;
1532 ret
= bt_graph_add_port_added_listener(ctx
->graph
,
1533 graph_port_added_listener
, ctx
);
1535 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
1539 ret
= bt_graph_add_port_removed_listener(ctx
->graph
,
1540 graph_port_removed_listener
, ctx
);
1542 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
1546 ret
= bt_graph_add_ports_connected_listener(ctx
->graph
,
1547 graph_ports_connected_listener
, ctx
);
1549 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
1553 ret
= bt_graph_add_ports_disconnected_listener(ctx
->graph
,
1554 graph_ports_disconnected_listener
, ctx
);
1556 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
1563 cmd_run_ctx_destroy(ctx
);
1571 int cmd_run_ctx_create_components_from_config_components(
1572 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
1575 struct bt_component_class
*comp_cls
= NULL
;
1576 struct bt_component
*comp
= NULL
;
1579 for (i
= 0; i
< cfg_components
->len
; i
++) {
1580 struct bt_config_component
*cfg_comp
=
1581 g_ptr_array_index(cfg_components
, i
);
1584 comp_cls
= find_component_class(cfg_comp
->plugin_name
->str
,
1585 cfg_comp
->comp_cls_name
->str
, cfg_comp
->type
);
1587 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1588 "comp-cls-name=\"%s\", comp-cls-type=%d",
1589 cfg_comp
->plugin_name
->str
,
1590 cfg_comp
->comp_cls_name
->str
,
1592 fprintf(stderr
, "%s%sCannot find component class %s",
1593 bt_common_color_bold(),
1594 bt_common_color_fg_red(),
1595 bt_common_color_reset());
1596 print_plugin_comp_cls_opt(stderr
,
1597 cfg_comp
->plugin_name
->str
,
1598 cfg_comp
->comp_cls_name
->str
,
1600 fprintf(stderr
, "\n");
1604 ret
= bt_graph_add_component(ctx
->graph
, comp_cls
,
1605 cfg_comp
->instance_name
->str
, cfg_comp
->params
, &comp
);
1607 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
1608 "comp-cls-name=\"%s\", comp-cls-type=%d, "
1610 cfg_comp
->plugin_name
->str
,
1611 cfg_comp
->comp_cls_name
->str
,
1612 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
1613 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
1614 bt_common_color_bold(),
1615 bt_common_color_fg_red(),
1616 cfg_comp
->instance_name
->str
,
1617 bt_common_color_reset());
1621 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
1622 comp
, cfg_comp
->instance_name
->str
);
1623 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
1625 g_hash_table_insert(ctx
->components
,
1626 GUINT_TO_POINTER(quark
), comp
);
1643 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
1648 * Make sure that, during this phase, our graph's "port added"
1649 * listener does not connect ports while we are creating the
1650 * components because we have a special, initial phase for
1653 ctx
->connect_ports
= false;
1655 ret
= cmd_run_ctx_create_components_from_config_components(
1656 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
1662 ret
= cmd_run_ctx_create_components_from_config_components(
1663 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
1669 ret
= cmd_run_ctx_create_components_from_config_components(
1670 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
1681 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
1682 struct bt_component
*comp
,
1683 int64_t (*port_count_fn
)(struct bt_component
*),
1684 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t))
1690 count
= port_count_fn(comp
);
1693 for (i
= 0; i
< count
; i
++) {
1694 struct bt_port
*upstream_port
= port_by_index_fn(comp
, i
);
1696 assert(upstream_port
);
1697 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
1698 bt_put(upstream_port
);
1709 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
1712 GHashTableIter iter
;
1713 gpointer g_name_quark
, g_comp
;
1715 ctx
->connect_ports
= true;
1716 g_hash_table_iter_init(&iter
, ctx
->components
);
1718 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
1719 if (bt_component_is_source(g_comp
)) {
1720 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
1721 g_comp
, bt_component_source_get_output_port_count
,
1722 bt_component_source_get_output_port_by_index
);
1723 } else if (bt_component_is_filter(g_comp
)) {
1724 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
1725 g_comp
, bt_component_filter_get_output_port_count
,
1726 bt_component_filter_get_output_port_by_index
);
1739 const char *bt_graph_status_str(enum bt_graph_status status
)
1742 case BT_GRAPH_STATUS_CANCELED
:
1743 return "BT_GRAPH_STATUS_CANCELED";
1744 case BT_GRAPH_STATUS_AGAIN
:
1745 return "BT_GRAPH_STATUS_AGAIN";
1746 case BT_GRAPH_STATUS_END
:
1747 return "BT_GRAPH_STATUS_END";
1748 case BT_GRAPH_STATUS_OK
:
1749 return "BT_GRAPH_STATUS_OK";
1750 case BT_GRAPH_STATUS_INVALID
:
1751 return "BT_GRAPH_STATUS_INVALID";
1752 case BT_GRAPH_STATUS_NO_SINK
:
1753 return "BT_GRAPH_STATUS_NO_SINK";
1754 case BT_GRAPH_STATUS_ERROR
:
1755 return "BT_GRAPH_STATUS_ERROR";
1762 int cmd_run(struct bt_config
*cfg
)
1765 struct cmd_run_ctx ctx
= { 0 };
1767 /* Initialize the command's context and the graph object */
1768 if (cmd_run_ctx_init(&ctx
, cfg
)) {
1769 BT_LOGE_STR("Cannot initialize the command's context.");
1770 fprintf(stderr
, "Cannot initialize the command's context\n");
1775 BT_LOGI_STR("Canceled by user before creating components.");
1779 BT_LOGI_STR("Creating components.");
1781 /* Create the requested component instances */
1782 if (cmd_run_ctx_create_components(&ctx
)) {
1783 BT_LOGE_STR("Cannot create components.");
1784 fprintf(stderr
, "Cannot create components\n");
1789 BT_LOGI_STR("Canceled by user before connecting components.");
1793 BT_LOGI_STR("Connecting components.");
1795 /* Connect the initially visible component ports */
1796 if (cmd_run_ctx_connect_ports(&ctx
)) {
1797 BT_LOGE_STR("Cannot connect initial component ports.");
1798 fprintf(stderr
, "Cannot connect initial component ports\n");
1803 BT_LOGI_STR("Canceled by user before running the graph.");
1807 BT_LOGI_STR("Running the graph.");
1811 enum bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
1814 * Reset console in case something messed with console
1815 * codes during the graph's execution.
1817 printf("%s", bt_common_color_reset());
1819 fprintf(stderr
, "%s", bt_common_color_reset());
1820 BT_LOGV("bt_graph_run() returned: status=%s",
1821 bt_graph_status_str(graph_status
));
1823 switch (graph_status
) {
1824 case BT_GRAPH_STATUS_OK
:
1826 case BT_GRAPH_STATUS_CANCELED
:
1827 BT_LOGI_STR("Graph was canceled by user.");
1829 case BT_GRAPH_STATUS_AGAIN
:
1830 if (bt_graph_is_canceled(ctx
.graph
)) {
1831 BT_LOGI_STR("Graph was canceled by user.");
1835 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
1836 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
1838 cfg
->cmd_data
.run
.retry_duration_us
);
1840 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
1841 if (bt_graph_is_canceled(ctx
.graph
)) {
1842 BT_LOGI_STR("Graph was canceled by user.");
1848 case BT_COMPONENT_STATUS_END
:
1851 BT_LOGE_STR("Graph failed to complete successfully");
1852 fprintf(stderr
, "Graph failed to complete successfully\n");
1865 cmd_run_ctx_destroy(&ctx
);
1870 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
1872 const char *env_clash
;
1874 if (!cfg
->command_name
) {
1878 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
1879 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
1883 if (g_file_test(cfg
->command_name
,
1884 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
1885 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
1887 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
1889 fprintf(stderr
, "\n");
1890 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
1896 void init_log_level(void)
1898 bt_cli_log_level
= bt_log_get_level_from_env("BABELTRACE_CLI_LOG_LEVEL");
1901 void set_sigint_handler(void)
1903 struct sigaction new_action
, old_action
;
1905 new_action
.sa_handler
= sigint_handler
;
1906 sigemptyset(&new_action
.sa_mask
);
1907 new_action
.sa_flags
= 0;
1908 sigaction(SIGINT
, NULL
, &old_action
);
1910 if (old_action
.sa_handler
!= SIG_IGN
) {
1911 sigaction(SIGINT
, &new_action
, NULL
);
1915 int main(int argc
, const char **argv
)
1919 struct bt_config
*cfg
;
1922 set_sigint_handler();
1924 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
1927 /* Quit without errors; typically usage/version */
1929 BT_LOGI_STR("Quitting without errors.");
1934 BT_LOGE("Command-line error: retcode=%d", retcode
);
1939 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
1940 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
1946 bt_cli_log_level
= BT_LOGGING_LEVEL_VERBOSE
;
1947 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
1948 // TODO: for backward compat., set the log level
1949 // environment variables of the known plugins
1951 } else if (cfg
->debug
) {
1952 bt_cli_log_level
= BT_LOGGING_LEVEL_DEBUG
;
1953 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
1954 // TODO: for backward compat., set the log level
1955 // environment variables of the known plugins
1959 babeltrace_debug
= cfg
->debug
;
1960 babeltrace_verbose
= cfg
->verbose
;
1963 if (cfg
->command_needs_plugins
) {
1964 ret
= load_all_plugins(cfg
->plugin_paths
);
1966 BT_LOGE("Failed to load plugins: ret=%d", ret
);
1972 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
1973 cfg
->command
, cfg
->command_name
);
1975 switch (cfg
->command
) {
1976 case BT_CONFIG_COMMAND_RUN
:
1979 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
1980 ret
= cmd_list_plugins(cfg
);
1982 case BT_CONFIG_COMMAND_HELP
:
1983 ret
= cmd_help(cfg
);
1985 case BT_CONFIG_COMMAND_QUERY
:
1986 ret
= cmd_query(cfg
);
1988 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
1989 ret
= cmd_print_ctf_metadata(cfg
);
1991 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
1992 ret
= cmd_print_lttng_live_sessions(cfg
);
1995 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
1999 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2000 cfg
->command
, cfg
->command_name
, ret
);
2001 warn_command_name_and_directory_clash(cfg
);
2002 retcode
= ret
? 1 : 0;