2 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
4 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "CLI"
28 #include <babeltrace2/babeltrace.h>
29 #include "common/common.h"
39 #include "babeltrace2-cfg.h"
40 #include "babeltrace2-cfg-cli-args.h"
41 #include "babeltrace2-cfg-cli-args-default.h"
43 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
44 #define ENV_BABELTRACE_CLI_LOG_LEVEL "BABELTRACE_CLI_LOG_LEVEL"
45 #define NSEC_PER_SEC 1000000000LL
48 * Known environment variable names for the log levels of the project's
51 static const char* log_level_env_var_names
[] = {
52 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
53 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
57 /* Application's processing graph (weak) */
58 static bt_graph
*the_graph
;
59 static bt_query_executor
*the_query_executor
;
60 static bool canceled
= false;
62 GPtrArray
*loaded_plugins
;
69 BOOL WINAPI
signal_handler(DWORD signal
) {
71 bt_graph_cancel(the_graph
);
80 void set_signal_handler(void)
82 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
83 BT_LOGE("Failed to set the Ctrl+C handler.");
87 #else /* __MINGW32__ */
90 void signal_handler(int signum
)
92 if (signum
!= SIGINT
) {
97 bt_graph_cancel(the_graph
);
100 if (the_query_executor
) {
101 bt_query_executor_cancel(the_query_executor
);
108 void set_signal_handler(void)
110 struct sigaction new_action
, old_action
;
112 new_action
.sa_handler
= signal_handler
;
113 sigemptyset(&new_action
.sa_mask
);
114 new_action
.sa_flags
= 0;
115 sigaction(SIGINT
, NULL
, &old_action
);
117 if (old_action
.sa_handler
!= SIG_IGN
) {
118 sigaction(SIGINT
, &new_action
, NULL
);
122 #endif /* __MINGW32__ */
125 void init_static_data(void)
127 loaded_plugins
= g_ptr_array_new_with_free_func(
128 (GDestroyNotify
) bt_object_put_ref
);
132 void fini_static_data(void)
134 g_ptr_array_free(loaded_plugins
, TRUE
);
138 int create_the_query_executor(void)
142 the_query_executor
= bt_query_executor_create();
143 if (!the_query_executor
) {
144 BT_CLI_LOGE_APPEND_CAUSE("Cannot create a query executor.");
152 void destroy_the_query_executor(void)
154 BT_QUERY_EXECUTOR_PUT_REF_AND_RESET(the_query_executor
);
158 int query(struct bt_config
*cfg
, const bt_component_class
*comp_cls
,
159 const char *obj
, const bt_value
*params
,
160 const bt_value
**user_result
, const char **fail_reason
)
162 const bt_value
*result
= NULL
;
163 bt_query_executor_query_status query_status
;
164 *fail_reason
= "unknown error";
167 BT_ASSERT(fail_reason
);
168 BT_ASSERT(user_result
);
169 ret
= create_the_query_executor();
171 /* create_the_query_executor() logs errors */
176 BT_CLI_LOGW_APPEND_CAUSE(
177 "Canceled by user before executing the query: "
178 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
179 "query-obj=\"%s\"", comp_cls
,
180 bt_component_class_get_name(comp_cls
), obj
);
181 *fail_reason
= "canceled by user";
186 query_status
= bt_query_executor_query(
187 the_query_executor
, comp_cls
, obj
, params
,
188 cfg
->log_level
, &result
);
189 switch (query_status
) {
190 case BT_QUERY_EXECUTOR_QUERY_STATUS_OK
:
192 case BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN
:
194 const uint64_t sleep_time_us
= 100000;
196 /* Wait 100 ms and retry */
197 BT_LOGD("Got BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN: sleeping: "
198 "time-us=%" PRIu64
, sleep_time_us
);
200 if (usleep(sleep_time_us
)) {
201 if (bt_query_executor_is_canceled(the_query_executor
)) {
202 BT_CLI_LOGW_APPEND_CAUSE(
203 "Query was canceled by user: "
204 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
205 "query-obj=\"%s\"", comp_cls
,
206 bt_component_class_get_name(comp_cls
),
208 *fail_reason
= "canceled by user";
215 case BT_QUERY_EXECUTOR_QUERY_STATUS_CANCELED
:
216 *fail_reason
= "canceled by user";
218 case BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR
:
220 case BT_QUERY_EXECUTOR_QUERY_STATUS_INVALID_OBJECT
:
221 *fail_reason
= "invalid or unknown query object";
223 case BT_QUERY_EXECUTOR_QUERY_STATUS_INVALID_PARAMS
:
224 *fail_reason
= "invalid query parameters";
226 case BT_QUERY_EXECUTOR_QUERY_STATUS_UNSUPPORTED
:
227 *fail_reason
= "unsupported action";
229 case BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR
:
230 *fail_reason
= "not enough memory";
233 BT_LOGF("Unknown query status: status=%s",
234 bt_common_func_status_string(query_status
));
240 *user_result
= result
;
248 destroy_the_query_executor();
249 bt_value_put_ref(result
);
254 const bt_plugin
*find_plugin(const char *name
)
257 const bt_plugin
*plugin
= NULL
;
260 BT_LOGI("Finding plugin: name=\"%s\"", name
);
262 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
263 plugin
= g_ptr_array_index(loaded_plugins
, i
);
265 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
273 BT_LOGI("Found plugin: name=\"%s\", plugin-addr=%p",
276 BT_LOGI("Cannot find plugin: name=\"%s\"", name
);
279 bt_plugin_get_ref(plugin
);
283 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
284 const bt_plugin
*, const char *);
287 const void *find_component_class_from_plugin(const char *plugin_name
,
288 const char *comp_class_name
,
289 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
291 const void *comp_class
= NULL
;
292 const bt_plugin
*plugin
;
294 BT_LOGI("Finding component class: plugin-name=\"%s\", "
295 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
297 plugin
= find_plugin(plugin_name
);
302 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
303 bt_object_get_ref(comp_class
);
304 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
308 BT_LOGI("Found component class: plugin-name=\"%s\", "
309 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
311 BT_LOGI("Cannot find source component class: "
312 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
313 plugin_name
, comp_class_name
);
320 const bt_component_class_source
*find_source_component_class(
321 const char *plugin_name
, const char *comp_class_name
)
323 return (const void *) find_component_class_from_plugin(
324 plugin_name
, comp_class_name
,
325 (plugin_borrow_comp_cls_func_t
)
326 bt_plugin_borrow_source_component_class_by_name_const
);
330 const bt_component_class_filter
*find_filter_component_class(
331 const char *plugin_name
, const char *comp_class_name
)
333 return (const void *) find_component_class_from_plugin(
334 plugin_name
, comp_class_name
,
335 (plugin_borrow_comp_cls_func_t
)
336 bt_plugin_borrow_filter_component_class_by_name_const
);
340 const bt_component_class_sink
*find_sink_component_class(
341 const char *plugin_name
, const char *comp_class_name
)
343 return (const void *) find_component_class_from_plugin(plugin_name
,
345 (plugin_borrow_comp_cls_func_t
)
346 bt_plugin_borrow_sink_component_class_by_name_const
);
350 const bt_component_class
*find_component_class(const char *plugin_name
,
351 const char *comp_class_name
,
352 bt_component_class_type comp_class_type
)
354 const bt_component_class
*comp_cls
= NULL
;
356 switch (comp_class_type
) {
357 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
358 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
360 case BT_COMPONENT_CLASS_TYPE_FILTER
:
361 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
363 case BT_COMPONENT_CLASS_TYPE_SINK
:
364 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
374 void print_indent(FILE *fp
, size_t indent
)
378 for (i
= 0; i
< indent
; i
++) {
384 const char *component_type_str(bt_component_class_type type
)
387 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
389 case BT_COMPONENT_CLASS_TYPE_SINK
:
391 case BT_COMPONENT_CLASS_TYPE_FILTER
:
399 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
400 const char *comp_cls_name
, bt_component_class_type type
)
402 GString
*shell_plugin_name
= NULL
;
403 GString
*shell_comp_cls_name
= NULL
;
406 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
407 if (!shell_plugin_name
) {
412 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
413 if (!shell_comp_cls_name
) {
417 fprintf(fh
, "'%s%s%s%s",
418 bt_common_color_bold(),
419 bt_common_color_fg_cyan(),
420 component_type_str(type
),
421 bt_common_color_fg_default());
423 if (shell_plugin_name
) {
424 fprintf(fh
, ".%s%s%s",
425 bt_common_color_fg_blue(),
426 shell_plugin_name
->str
,
427 bt_common_color_fg_default());
430 fprintf(fh
, ".%s%s%s'",
431 bt_common_color_fg_yellow(),
432 shell_comp_cls_name
->str
,
433 bt_common_color_reset());
436 if (shell_plugin_name
) {
437 g_string_free(shell_plugin_name
, TRUE
);
440 if (shell_comp_cls_name
) {
441 g_string_free(shell_comp_cls_name
, TRUE
);
446 void print_value(FILE *, const bt_value
*, size_t);
449 void print_value_rec(FILE *, const bt_value
*, size_t);
451 struct print_map_value_data
{
457 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
460 struct print_map_value_data
*print_map_value_data
= data
;
462 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
463 fprintf(print_map_value_data
->fp
, "%s: ", key
);
466 if (bt_value_is_array(object
) &&
467 bt_value_array_is_empty(object
)) {
468 fprintf(print_map_value_data
->fp
, "[ ]\n");
472 if (bt_value_is_map(object
) &&
473 bt_value_map_is_empty(object
)) {
474 fprintf(print_map_value_data
->fp
, "{ }\n");
478 if (bt_value_is_array(object
) ||
479 bt_value_is_map(object
)) {
480 fprintf(print_map_value_data
->fp
, "\n");
483 print_value_rec(print_map_value_data
->fp
, object
,
484 print_map_value_data
->indent
+ 2);
489 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
503 switch (bt_value_get_type(value
)) {
504 case BT_VALUE_TYPE_NULL
:
505 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
506 bt_common_color_reset());
508 case BT_VALUE_TYPE_BOOL
:
509 bool_val
= bt_value_bool_get(value
);
510 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
511 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
512 bt_common_color_reset());
514 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
515 uint_val
= bt_value_unsigned_integer_get(value
);
516 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
517 bt_common_color_fg_red(), uint_val
,
518 bt_common_color_reset());
520 case BT_VALUE_TYPE_SIGNED_INTEGER
:
521 int_val
= bt_value_signed_integer_get(value
);
522 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
523 bt_common_color_fg_red(), int_val
,
524 bt_common_color_reset());
526 case BT_VALUE_TYPE_REAL
:
527 dbl_val
= bt_value_real_get(value
);
528 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
529 bt_common_color_fg_red(), dbl_val
,
530 bt_common_color_reset());
532 case BT_VALUE_TYPE_STRING
:
533 str_val
= bt_value_string_get(value
);
534 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
535 bt_common_color_fg_green(), str_val
,
536 bt_common_color_reset());
538 case BT_VALUE_TYPE_ARRAY
:
539 size
= bt_value_array_get_size(value
);
545 print_indent(fp
, indent
);
546 fprintf(fp
, "[ ]\n");
550 for (i
= 0; i
< size
; i
++) {
551 const bt_value
*element
=
552 bt_value_array_borrow_element_by_index_const(
558 print_indent(fp
, indent
);
561 if (bt_value_is_array(element
) &&
562 bt_value_array_is_empty(element
)) {
563 fprintf(fp
, "[ ]\n");
567 if (bt_value_is_map(element
) &&
568 bt_value_map_is_empty(element
)) {
569 fprintf(fp
, "{ }\n");
573 if (bt_value_is_array(element
) ||
574 bt_value_is_map(element
)) {
578 print_value_rec(fp
, element
, indent
+ 2);
581 case BT_VALUE_TYPE_MAP
:
583 struct print_map_value_data data
= {
588 if (bt_value_map_is_empty(value
)) {
589 print_indent(fp
, indent
);
590 fprintf(fp
, "{ }\n");
594 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
603 BT_LOGE("Error printing value of type %s.",
604 bt_common_value_type_string(bt_value_get_type(value
)));
608 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
610 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
611 print_indent(fp
, indent
);
614 print_value_rec(fp
, value
, indent
);
618 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
620 fprintf(stderr
, " ");
621 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
622 bt_config_component
->comp_cls_name
->str
,
623 bt_config_component
->type
);
624 fprintf(stderr
, ":\n");
626 if (bt_config_component
->instance_name
->len
> 0) {
627 fprintf(stderr
, " Name: %s\n",
628 bt_config_component
->instance_name
->str
);
631 fprintf(stderr
, " Parameters:\n");
632 print_value(stderr
, bt_config_component
->params
, 8);
636 void print_bt_config_components(GPtrArray
*array
)
640 for (i
= 0; i
< array
->len
; i
++) {
641 struct bt_config_component
*cfg_component
=
642 bt_config_get_component(array
, i
);
643 print_bt_config_component(cfg_component
);
644 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
649 void print_plugin_paths(const bt_value
*plugin_paths
)
651 fprintf(stderr
, " Plugin paths:\n");
652 print_value(stderr
, plugin_paths
, 4);
656 void print_cfg_run(struct bt_config
*cfg
)
660 print_plugin_paths(cfg
->plugin_paths
);
661 fprintf(stderr
, " Source component instances:\n");
662 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
664 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
665 fprintf(stderr
, " Filter component instances:\n");
666 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
669 fprintf(stderr
, " Sink component instances:\n");
670 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
671 fprintf(stderr
, " Connections:\n");
673 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
674 struct bt_config_connection
*cfg_connection
=
675 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
678 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
679 cfg_connection
->upstream_comp_name
->str
,
680 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
681 cfg_connection
->upstream_port_glob
->str
,
682 cfg_connection
->downstream_comp_name
->str
,
683 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
684 cfg_connection
->downstream_port_glob
->str
);
689 void print_cfg_list_plugins(struct bt_config
*cfg
)
691 print_plugin_paths(cfg
->plugin_paths
);
695 void print_cfg_help(struct bt_config
*cfg
)
697 print_plugin_paths(cfg
->plugin_paths
);
701 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
703 print_plugin_paths(cfg
->plugin_paths
);
704 fprintf(stderr
, " Path: %s\n",
705 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
709 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
711 print_plugin_paths(cfg
->plugin_paths
);
712 fprintf(stderr
, " URL: %s\n",
713 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
717 void print_cfg_query(struct bt_config
*cfg
)
719 print_plugin_paths(cfg
->plugin_paths
);
720 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
721 fprintf(stderr
, " Component class:\n");
722 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
726 void print_cfg(struct bt_config
*cfg
)
728 if (!BT_LOG_ON_INFO
) {
732 BT_LOGI_STR("CLI configuration:");
733 BT_LOGI(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
734 BT_LOGI(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
736 switch (cfg
->command
) {
737 case BT_CONFIG_COMMAND_RUN
:
740 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
741 print_cfg_list_plugins(cfg
);
743 case BT_CONFIG_COMMAND_HELP
:
746 case BT_CONFIG_COMMAND_QUERY
:
747 print_cfg_query(cfg
);
749 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
750 print_cfg_print_ctf_metadata(cfg
);
752 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
753 print_cfg_print_lttng_live_sessions(cfg
);
761 void add_to_loaded_plugins(const bt_plugin_set
*plugin_set
)
766 count
= bt_plugin_set_get_plugin_count(plugin_set
);
767 BT_ASSERT(count
>= 0);
769 for (i
= 0; i
< count
; i
++) {
770 const bt_plugin
*plugin
=
771 bt_plugin_set_borrow_plugin_by_index_const(plugin_set
, i
);
772 const bt_plugin
*loaded_plugin
=
773 find_plugin(bt_plugin_get_name(plugin
));
778 BT_LOGI("Not using plugin: another one already exists with the same name: "
779 "plugin-name=\"%s\", plugin-path=\"%s\", "
780 "existing-plugin-path=\"%s\"",
781 bt_plugin_get_name(plugin
),
782 bt_plugin_get_path(plugin
),
783 bt_plugin_get_path(loaded_plugin
));
784 bt_plugin_put_ref(loaded_plugin
);
786 /* Add to global array. */
787 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
788 bt_plugin_get_name(plugin
));
789 bt_plugin_get_ref(plugin
);
790 g_ptr_array_add(loaded_plugins
, (void *) plugin
);
796 int load_dynamic_plugins(const bt_value
*plugin_paths
)
798 int nr_paths
, i
, ret
= 0;
800 nr_paths
= bt_value_array_get_size(plugin_paths
);
802 BT_CLI_LOGE_APPEND_CAUSE(
803 "Cannot load dynamic plugins: no plugin path.");
808 BT_LOGI_STR("Loading dynamic plugins.");
810 for (i
= 0; i
< nr_paths
; i
++) {
811 const bt_value
*plugin_path_value
= NULL
;
812 const char *plugin_path
;
813 const bt_plugin_set
*plugin_set
= NULL
;
814 bt_plugin_find_all_from_dir_status status
;
817 bt_value_array_borrow_element_by_index_const(
819 plugin_path
= bt_value_string_get(plugin_path_value
);
822 * Skip this if the directory does not exist because
823 * bt_plugin_find_all_from_dir() expects an existing
826 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
827 BT_LOGI("Skipping nonexistent directory path: "
828 "path=\"%s\"", plugin_path
);
832 status
= bt_plugin_find_all_from_dir(plugin_path
, BT_FALSE
,
833 BT_FALSE
, &plugin_set
);
835 BT_CLI_LOGE_APPEND_CAUSE(
836 "Unable to load dynamic plugins from directory: "
837 "path=\"%s\"", plugin_path
);
841 BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_NOT_FOUND
) {
842 BT_LOGI("No plugins found in directory: path=\"%s\"",
847 BT_ASSERT(status
== BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_OK
);
848 BT_ASSERT(plugin_set
);
849 add_to_loaded_plugins(plugin_set
);
850 bt_plugin_set_put_ref(plugin_set
);
857 int load_static_plugins(void)
860 const bt_plugin_set
*plugin_set
;
861 bt_plugin_find_all_from_static_status status
;
863 BT_LOGI("Loading static plugins.");
864 status
= bt_plugin_find_all_from_static(BT_FALSE
, &plugin_set
);
866 BT_CLI_LOGE_APPEND_CAUSE("Unable to load static plugins.");
870 BT_PLUGIN_FIND_ALL_FROM_STATIC_STATUS_NOT_FOUND
) {
871 BT_LOGI("No static plugins found.");
875 BT_ASSERT(status
== BT_PLUGIN_FIND_ALL_FROM_STATIC_STATUS_OK
);
876 BT_ASSERT(plugin_set
);
877 add_to_loaded_plugins(plugin_set
);
878 bt_plugin_set_put_ref(plugin_set
);
885 int load_all_plugins(const bt_value
*plugin_paths
)
889 if (load_dynamic_plugins(plugin_paths
)) {
894 if (load_static_plugins()) {
899 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
906 void print_plugin_info(const bt_plugin
*plugin
)
908 unsigned int major
, minor
, patch
;
910 bt_property_availability version_avail
;
911 const char *plugin_name
;
915 const char *plugin_description
;
917 plugin_name
= bt_plugin_get_name(plugin
);
918 path
= bt_plugin_get_path(plugin
);
919 author
= bt_plugin_get_author(plugin
);
920 license
= bt_plugin_get_license(plugin
);
921 plugin_description
= bt_plugin_get_description(plugin
);
922 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
924 printf("%s%s%s%s:\n", bt_common_color_bold(),
925 bt_common_color_fg_blue(), plugin_name
,
926 bt_common_color_reset());
928 printf(" %sPath%s: %s\n", bt_common_color_bold(),
929 bt_common_color_reset(), path
);
934 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
935 printf(" %sVersion%s: %u.%u.%u",
936 bt_common_color_bold(), bt_common_color_reset(),
937 major
, minor
, patch
);
946 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
947 bt_common_color_reset(),
948 plugin_description
? plugin_description
: "(None)");
949 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
950 bt_common_color_reset(), author
? author
: "(Unknown)");
951 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
952 bt_common_color_reset(),
953 license
? license
: "(Unknown)");
957 int cmd_query(struct bt_config
*cfg
)
960 const bt_component_class
*comp_cls
= NULL
;
961 const bt_value
*results
= NULL
;
962 const char *fail_reason
= NULL
;
964 comp_cls
= find_component_class(
965 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
966 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
967 cfg
->cmd_data
.query
.cfg_component
->type
);
969 BT_CLI_LOGE_APPEND_CAUSE(
970 "Cannot find component class: plugin-name=\"%s\", "
971 "comp-cls-name=\"%s\", comp-cls-type=%d",
972 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
973 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
974 cfg
->cmd_data
.query
.cfg_component
->type
);
979 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
980 cfg
->cmd_data
.query
.cfg_component
->params
,
981 &results
, &fail_reason
);
986 print_value(stdout
, results
, 0);
990 BT_CLI_LOGE_APPEND_CAUSE(
991 "Failed to query component class: %s: plugin-name=\"%s\", "
992 "comp-cls-name=\"%s\", comp-cls-type=%d "
993 "object=\"%s\"", fail_reason
,
994 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
995 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
996 cfg
->cmd_data
.query
.cfg_component
->type
,
997 cfg
->cmd_data
.query
.object
->str
);
1001 bt_component_class_put_ref(comp_cls
);
1002 bt_value_put_ref(results
);
1007 void print_component_class_help(const char *plugin_name
,
1008 const bt_component_class
*comp_cls
)
1010 const char *comp_class_name
=
1011 bt_component_class_get_name(comp_cls
);
1012 const char *comp_class_description
=
1013 bt_component_class_get_description(comp_cls
);
1014 const char *comp_class_help
=
1015 bt_component_class_get_help(comp_cls
);
1016 bt_component_class_type type
=
1017 bt_component_class_get_type(comp_cls
);
1019 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
1021 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1022 bt_common_color_reset(),
1023 comp_class_description
? comp_class_description
: "(None)");
1025 if (comp_class_help
) {
1026 printf("\n%s\n", comp_class_help
);
1031 int cmd_help(struct bt_config
*cfg
)
1034 const bt_plugin
*plugin
= NULL
;
1035 const bt_component_class
*needed_comp_cls
= NULL
;
1037 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1039 BT_CLI_LOGE_APPEND_CAUSE(
1040 "Cannot find plugin: plugin-name=\"%s\"",
1041 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1046 print_plugin_info(plugin
);
1047 printf(" %sSource component classes%s: %d\n",
1048 bt_common_color_bold(),
1049 bt_common_color_reset(),
1050 (int) bt_plugin_get_source_component_class_count(plugin
));
1051 printf(" %sFilter component classes%s: %d\n",
1052 bt_common_color_bold(),
1053 bt_common_color_reset(),
1054 (int) bt_plugin_get_filter_component_class_count(plugin
));
1055 printf(" %sSink component classes%s: %d\n",
1056 bt_common_color_bold(),
1057 bt_common_color_reset(),
1058 (int) bt_plugin_get_sink_component_class_count(plugin
));
1060 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
1061 /* Plugin help only */
1065 needed_comp_cls
= find_component_class(
1066 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1067 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1068 cfg
->cmd_data
.help
.cfg_component
->type
);
1069 if (!needed_comp_cls
) {
1070 BT_CLI_LOGE_APPEND_CAUSE(
1071 "Cannot find component class: plugin-name=\"%s\", "
1072 "comp-cls-name=\"%s\", comp-cls-type=%d",
1073 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1074 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1075 cfg
->cmd_data
.help
.cfg_component
->type
);
1081 print_component_class_help(
1082 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1086 bt_component_class_put_ref(needed_comp_cls
);
1087 bt_plugin_put_ref(plugin
);
1091 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
1093 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
1096 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
1097 const char *cc_type_name
, uint64_t count
,
1098 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
1099 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
1104 printf(" %s%s component classes%s: (none)\n",
1105 bt_common_color_bold(),
1107 bt_common_color_reset());
1110 printf(" %s%s component classes%s:\n",
1111 bt_common_color_bold(),
1113 bt_common_color_reset());
1116 for (i
= 0; i
< count
; i
++) {
1117 const bt_component_class
*comp_class
=
1118 spec_comp_cls_borrow_comp_cls_func(
1119 borrow_comp_cls_by_index_func(plugin
, i
));
1120 const char *comp_class_name
=
1121 bt_component_class_get_name(comp_class
);
1122 const char *comp_class_description
=
1123 bt_component_class_get_description(comp_class
);
1124 bt_component_class_type type
=
1125 bt_component_class_get_type(comp_class
);
1128 print_plugin_comp_cls_opt(stdout
,
1129 bt_plugin_get_name(plugin
), comp_class_name
,
1132 if (comp_class_description
) {
1133 printf(": %s", comp_class_description
);
1144 int cmd_list_plugins(struct bt_config
*cfg
)
1147 int plugins_count
, component_classes_count
= 0, i
;
1149 printf("From the following plugin paths:\n\n");
1150 print_value(stdout
, cfg
->plugin_paths
, 2);
1152 plugins_count
= loaded_plugins
->len
;
1153 if (plugins_count
== 0) {
1154 printf("No plugins found.\n");
1158 for (i
= 0; i
< plugins_count
; i
++) {
1159 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1161 component_classes_count
+=
1162 bt_plugin_get_source_component_class_count(plugin
) +
1163 bt_plugin_get_filter_component_class_count(plugin
) +
1164 bt_plugin_get_sink_component_class_count(plugin
);
1167 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1168 bt_common_color_bold(),
1169 component_classes_count
,
1170 bt_common_color_reset(),
1171 bt_common_color_bold(),
1173 bt_common_color_reset());
1175 for (i
= 0; i
< plugins_count
; i
++) {
1176 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1179 print_plugin_info(plugin
);
1180 cmd_list_plugins_print_component_classes(plugin
, "Source",
1181 bt_plugin_get_source_component_class_count(plugin
),
1182 (plugin_borrow_comp_cls_by_index_func_t
)
1183 bt_plugin_borrow_source_component_class_by_index_const
,
1184 (spec_comp_cls_borrow_comp_cls_func_t
)
1185 bt_component_class_source_as_component_class
);
1186 cmd_list_plugins_print_component_classes(plugin
, "Filter",
1187 bt_plugin_get_filter_component_class_count(plugin
),
1188 (plugin_borrow_comp_cls_by_index_func_t
)
1189 bt_plugin_borrow_filter_component_class_by_index_const
,
1190 (spec_comp_cls_borrow_comp_cls_func_t
)
1191 bt_component_class_filter_as_component_class
);
1192 cmd_list_plugins_print_component_classes(plugin
, "Sink",
1193 bt_plugin_get_sink_component_class_count(plugin
),
1194 (plugin_borrow_comp_cls_by_index_func_t
)
1195 bt_plugin_borrow_sink_component_class_by_index_const
,
1196 (spec_comp_cls_borrow_comp_cls_func_t
)
1197 bt_component_class_sink_as_component_class
);
1205 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1208 const bt_component_class
*comp_cls
= NULL
;
1209 const bt_value
*results
= NULL
;
1210 bt_value
*params
= NULL
;
1211 const bt_value
*map
= NULL
;
1212 const bt_value
*v
= NULL
;
1213 static const char * const plugin_name
= "ctf";
1214 static const char * const comp_cls_name
= "lttng-live";
1215 static const bt_component_class_type comp_cls_type
=
1216 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1217 int64_t array_size
, i
;
1218 const char *fail_reason
= NULL
;
1219 FILE *out_stream
= stdout
;
1221 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1222 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1225 BT_CLI_LOGE_APPEND_CAUSE(
1226 "Cannot find component class: plugin-name=\"%s\", "
1227 "comp-cls-name=\"%s\", comp-cls-type=%d",
1228 plugin_name
, comp_cls_name
,
1229 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1233 params
= bt_value_map_create();
1238 ret
= bt_value_map_insert_string_entry(params
, "url",
1239 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1244 ret
= query(cfg
, comp_cls
, "sessions", params
,
1245 &results
, &fail_reason
);
1252 if (!bt_value_is_array(results
)) {
1253 BT_CLI_LOGE_APPEND_CAUSE(
1254 "Expecting an array for LTTng live `sessions` query.");
1258 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1260 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1264 BT_LOGE_ERRNO("Cannot open file for writing",
1266 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1267 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1269 "Cannot open file for writing: path=\"%s\"",
1270 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1275 array_size
= bt_value_array_get_size(results
);
1276 for (i
= 0; i
< array_size
; i
++) {
1277 const char *url_text
;
1278 int64_t timer_us
, streams
, clients
;
1280 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1282 BT_CLI_LOGE_APPEND_CAUSE("Unexpected empty array entry.");
1285 if (!bt_value_is_map(map
)) {
1286 BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type.");
1290 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1292 BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry.");
1295 url_text
= bt_value_string_get(v
);
1296 fprintf(out_stream
, "%s", url_text
);
1297 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1299 BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry.");
1302 timer_us
= bt_value_signed_integer_get(v
);
1303 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1304 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1306 BT_CLI_LOGE_APPEND_CAUSE(
1307 "Missing `stream-count` entry.");
1310 streams
= bt_value_signed_integer_get(v
);
1311 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1312 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1314 BT_CLI_LOGE_APPEND_CAUSE(
1315 "Missing `client-count` entry.");
1318 clients
= bt_value_signed_integer_get(v
);
1319 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1325 BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s",
1332 bt_value_put_ref(results
);
1333 bt_value_put_ref(params
);
1334 bt_component_class_put_ref(comp_cls
);
1336 if (out_stream
&& out_stream
!= stdout
) {
1337 int fclose_ret
= fclose(out_stream
);
1340 BT_LOGE_ERRNO("Cannot close file stream",
1342 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1350 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1353 const bt_component_class
*comp_cls
= NULL
;
1354 const bt_value
*results
= NULL
;
1355 bt_value
*params
= NULL
;
1356 const bt_value
*metadata_text_value
= NULL
;
1357 const char *metadata_text
= NULL
;
1358 static const char * const plugin_name
= "ctf";
1359 static const char * const comp_cls_name
= "fs";
1360 static const bt_component_class_type comp_cls_type
=
1361 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1362 const char *fail_reason
= NULL
;
1363 FILE *out_stream
= stdout
;
1365 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1366 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1369 BT_CLI_LOGE_APPEND_CAUSE(
1370 "Cannot find component class: plugin-name=\"%s\", "
1371 "comp-cls-name=\"%s\", comp-cls-type=%d",
1372 plugin_name
, comp_cls_name
,
1373 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1378 params
= bt_value_map_create();
1384 ret
= bt_value_map_insert_string_entry(params
, "path",
1385 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1391 ret
= query(cfg
, comp_cls
, "metadata-info",
1392 params
, &results
, &fail_reason
);
1397 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1399 if (!metadata_text_value
) {
1400 BT_CLI_LOGE_APPEND_CAUSE(
1401 "Cannot find `text` string value in the resulting metadata info object.");
1406 metadata_text
= bt_value_string_get(metadata_text_value
);
1408 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1410 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1413 BT_LOGE_ERRNO("Cannot open file for writing",
1415 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1416 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1418 "Cannot open file for writing: path=\"%s\"",
1419 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1425 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1427 BT_CLI_LOGE_APPEND_CAUSE(
1428 "Cannot write whole metadata text to output stream: "
1438 BT_CLI_LOGE_APPEND_CAUSE(
1439 "Failed to query `metadata-info` object: %s", fail_reason
);
1442 bt_value_put_ref(results
);
1443 bt_value_put_ref(params
);
1444 bt_component_class_put_ref(comp_cls
);
1446 if (out_stream
&& out_stream
!= stdout
) {
1447 int fclose_ret
= fclose(out_stream
);
1450 BT_LOGE_ERRNO("Cannot close file stream",
1452 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1460 char *instance_name
;
1464 struct trace_range
{
1465 uint64_t intersection_range_begin_ns
;
1466 uint64_t intersection_range_end_ns
;
1470 guint
port_id_hash(gconstpointer v
)
1472 const struct port_id
*id
= v
;
1474 BT_ASSERT(id
->instance_name
);
1475 BT_ASSERT(id
->port_name
);
1477 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1481 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1483 const struct port_id
*id1
= v1
;
1484 const struct port_id
*id2
= v2
;
1486 return strcmp(id1
->instance_name
, id2
->instance_name
) == 0 &&
1487 strcmp(id1
->port_name
, id2
->port_name
) == 0;
1491 void port_id_destroy(gpointer data
)
1493 struct port_id
*id
= data
;
1495 free(id
->instance_name
);
1496 free(id
->port_name
);
1501 void trace_range_destroy(gpointer data
)
1506 struct cmd_run_ctx
{
1508 GHashTable
*src_components
;
1511 GHashTable
*flt_components
;
1514 GHashTable
*sink_components
;
1520 struct bt_config
*cfg
;
1524 bool stream_intersection_mode
;
1527 * Association of struct port_id -> struct trace_range.
1529 GHashTable
*intersections
;
1532 /* Returns a timestamp of the form "(-)s.ns" */
1534 char *s_from_ns(int64_t ns
)
1539 int64_t ts_sec_abs
, ts_nsec_abs
;
1540 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1541 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1543 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1544 is_negative
= false;
1545 ts_sec_abs
= ts_sec
;
1546 ts_nsec_abs
= ts_nsec
;
1547 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1548 is_negative
= false;
1549 ts_sec_abs
= ts_sec
- 1;
1550 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1551 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1553 ts_sec_abs
= ts_sec
;
1554 ts_nsec_abs
= -ts_nsec
;
1555 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1557 ts_sec_abs
= -(ts_sec
+ 1);
1558 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1559 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1561 ts_sec_abs
= -ts_sec
;
1562 ts_nsec_abs
= ts_nsec
;
1563 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1565 ts_sec_abs
= -ts_sec
;
1566 ts_nsec_abs
= -ts_nsec
;
1569 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1570 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1578 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1579 struct cmd_run_ctx
*ctx
,
1580 const bt_component
*upstream_comp
,
1581 const bt_port_output
*out_upstream_port
,
1582 struct bt_config_connection
*cfg_conn
)
1584 typedef uint64_t (*input_port_count_func_t
)(void *);
1585 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1586 const void *, uint64_t);
1587 const bt_port
*upstream_port
=
1588 bt_port_output_as_port_const(out_upstream_port
);
1591 GQuark downstreamp_comp_name_quark
;
1592 void *downstream_comp
;
1593 uint64_t downstream_port_count
;
1595 input_port_count_func_t port_count_fn
;
1596 borrow_input_port_by_index_func_t port_by_index_fn
;
1597 bt_graph_connect_ports_status connect_ports_status
=
1598 BT_GRAPH_CONNECT_PORTS_STATUS_OK
;
1599 bool insert_trimmer
= false;
1600 bt_value
*trimmer_params
= NULL
;
1601 char *intersection_begin
= NULL
;
1602 char *intersection_end
= NULL
;
1603 const bt_component_filter
*trimmer
= NULL
;
1604 const bt_component_class_filter
*trimmer_class
= NULL
;
1605 const bt_port_input
*trimmer_input
= NULL
;
1606 const bt_port_output
*trimmer_output
= NULL
;
1608 if (ctx
->intersections
&&
1609 bt_component_get_class_type(upstream_comp
) ==
1610 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1611 struct trace_range
*range
;
1612 struct port_id port_id
= {
1613 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1614 .port_name
= (char *) bt_port_get_name(upstream_port
)
1617 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1621 range
= (struct trace_range
*) g_hash_table_lookup(
1622 ctx
->intersections
, &port_id
);
1624 bt_value_map_insert_entry_status insert_status
;
1626 intersection_begin
= s_from_ns(
1627 range
->intersection_range_begin_ns
);
1628 intersection_end
= s_from_ns(
1629 range
->intersection_range_end_ns
);
1630 if (!intersection_begin
|| !intersection_end
) {
1631 BT_CLI_LOGE_APPEND_CAUSE(
1632 "Cannot create trimmer argument timestamp string.");
1636 insert_trimmer
= true;
1637 trimmer_params
= bt_value_map_create();
1638 if (!trimmer_params
) {
1642 insert_status
= bt_value_map_insert_string_entry(
1643 trimmer_params
, "begin", intersection_begin
);
1644 if (insert_status
< 0) {
1647 insert_status
= bt_value_map_insert_string_entry(
1649 "end", intersection_end
);
1650 if (insert_status
< 0) {
1655 trimmer_class
= find_filter_component_class("utils", "trimmer");
1656 if (!trimmer_class
) {
1661 BT_LOGI("Connecting upstream port to the next available downstream port: "
1662 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1663 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1664 upstream_port
, bt_port_get_name(upstream_port
),
1665 cfg_conn
->downstream_comp_name
->str
,
1666 cfg_conn
->arg
->str
);
1667 downstreamp_comp_name_quark
= g_quark_from_string(
1668 cfg_conn
->downstream_comp_name
->str
);
1669 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1670 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1671 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1672 port_count_fn
= (input_port_count_func_t
)
1673 bt_component_filter_get_input_port_count
;
1674 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1675 bt_component_filter_borrow_input_port_by_index_const
;
1677 if (!downstream_comp
) {
1678 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1679 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1680 port_count_fn
= (input_port_count_func_t
)
1681 bt_component_sink_get_input_port_count
;
1682 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1683 bt_component_sink_borrow_input_port_by_index_const
;
1686 if (!downstream_comp
) {
1687 BT_CLI_LOGE_APPEND_CAUSE("Cannot find downstream component: "
1688 "comp-name=\"%s\", conn-arg=\"%s\"",
1689 cfg_conn
->downstream_comp_name
->str
,
1690 cfg_conn
->arg
->str
);
1694 downstream_port_count
= port_count_fn(downstream_comp
);
1696 for (i
= 0; i
< downstream_port_count
; i
++) {
1697 const bt_port_input
*in_downstream_port
=
1698 port_by_index_fn(downstream_comp
, i
);
1699 const bt_port
*downstream_port
=
1700 bt_port_input_as_port_const(in_downstream_port
);
1701 const char *upstream_port_name
;
1702 const char *downstream_port_name
;
1704 BT_ASSERT(downstream_port
);
1706 /* Skip port if it's already connected. */
1707 if (bt_port_is_connected(downstream_port
)) {
1708 BT_LOGI("Skipping downstream port: already connected: "
1709 "port-addr=%p, port-name=\"%s\"",
1711 bt_port_get_name(downstream_port
));
1715 downstream_port_name
= bt_port_get_name(downstream_port
);
1716 BT_ASSERT(downstream_port_name
);
1717 upstream_port_name
= bt_port_get_name(upstream_port
);
1718 BT_ASSERT(upstream_port_name
);
1720 if (!bt_common_star_glob_match(
1721 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1722 downstream_port_name
, SIZE_MAX
)) {
1726 if (insert_trimmer
) {
1728 * In order to insert the trimmer between the
1729 * two components that were being connected, we
1730 * create a connection configuration entry which
1731 * describes a connection from the trimmer's
1732 * output to the original input that was being
1735 * Hence, the creation of the trimmer will cause
1736 * the graph "new port" listener to establish
1737 * all downstream connections as its output port
1738 * is connected. We will then establish the
1739 * connection between the original upstream
1740 * source and the trimmer.
1742 char *trimmer_name
= NULL
;
1743 bt_graph_add_component_status add_comp_status
;
1745 ret
= asprintf(&trimmer_name
,
1746 "stream-intersection-trimmer-%s",
1747 upstream_port_name
);
1753 ctx
->connect_ports
= false;
1754 add_comp_status
= bt_graph_add_filter_component(
1755 ctx
->graph
, trimmer_class
, trimmer_name
,
1756 trimmer_params
, ctx
->cfg
->log_level
,
1759 if (add_comp_status
!=
1760 BT_GRAPH_ADD_COMPONENT_STATUS_OK
) {
1766 bt_component_filter_borrow_input_port_by_index_const(
1768 if (!trimmer_input
) {
1772 bt_component_filter_borrow_output_port_by_index_const(
1774 if (!trimmer_output
) {
1779 * Replace the current downstream port by the trimmer's
1782 in_downstream_port
= trimmer_input
;
1784 bt_port_input_as_port_const(in_downstream_port
);
1785 downstream_port_name
= bt_port_get_name(
1787 BT_ASSERT(downstream_port_name
);
1790 /* We have a winner! */
1791 connect_ports_status
= bt_graph_connect_ports(ctx
->graph
,
1792 out_upstream_port
, in_downstream_port
, NULL
);
1793 downstream_port
= NULL
;
1794 switch (connect_ports_status
) {
1795 case BT_GRAPH_CONNECT_PORTS_STATUS_OK
:
1797 case BT_GRAPH_CONNECT_PORTS_STATUS_CANCELED
:
1798 BT_CLI_LOGW_APPEND_CAUSE("Graph was canceled by user.");
1801 BT_CLI_LOGE_APPEND_CAUSE(
1802 "Cannot create connection: graph refuses to connect ports: "
1803 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1804 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1805 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1806 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1808 upstream_comp
, bt_component_get_name(upstream_comp
),
1809 upstream_port
, bt_port_get_name(upstream_port
),
1810 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1811 downstream_port
, downstream_port_name
,
1812 cfg_conn
->arg
->str
);
1816 BT_LOGI("Connected component ports: "
1817 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1818 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1819 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1820 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1822 upstream_comp
, bt_component_get_name(upstream_comp
),
1823 upstream_port
, bt_port_get_name(upstream_port
),
1824 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1825 downstream_port
, downstream_port_name
,
1826 cfg_conn
->arg
->str
);
1828 if (insert_trimmer
) {
1830 * The first connection, from the source to the trimmer,
1831 * has been done. We now connect the trimmer to the
1832 * original downstream port.
1834 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1836 bt_component_filter_as_component_const(trimmer
),
1837 trimmer_output
, cfg_conn
);
1841 ctx
->connect_ports
= true;
1845 * We found a matching downstream port: the search is
1851 /* No downstream port found */
1852 BT_CLI_LOGE_APPEND_CAUSE(
1853 "Cannot create connection: cannot find a matching downstream port for upstream port: "
1854 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1855 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1856 upstream_port
, bt_port_get_name(upstream_port
),
1857 cfg_conn
->downstream_comp_name
->str
,
1858 cfg_conn
->arg
->str
);
1864 free(intersection_begin
);
1865 free(intersection_end
);
1866 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1867 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1868 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1873 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1874 const bt_port_output
*upstream_port
)
1877 const char *upstream_port_name
;
1878 const char *upstream_comp_name
;
1879 const bt_component
*upstream_comp
= NULL
;
1883 BT_ASSERT(upstream_port
);
1884 upstream_port_name
= bt_port_get_name(
1885 bt_port_output_as_port_const(upstream_port
));
1886 BT_ASSERT(upstream_port_name
);
1887 upstream_comp
= bt_port_borrow_component_const(
1888 bt_port_output_as_port_const(upstream_port
));
1889 BT_ASSERT(upstream_comp
);
1890 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1891 BT_ASSERT(upstream_comp_name
);
1892 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1893 "port-addr=%p, port-name=\"%s\"",
1894 upstream_comp
, upstream_comp_name
,
1895 upstream_port
, upstream_port_name
);
1897 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1898 struct bt_config_connection
*cfg_conn
=
1900 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1902 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1903 upstream_comp_name
)) {
1907 if (!bt_common_star_glob_match(
1908 cfg_conn
->upstream_port_glob
->str
,
1909 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1913 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1914 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1916 BT_CLI_LOGE_APPEND_CAUSE(
1917 "Cannot connect upstream port: "
1918 "port-addr=%p, port-name=\"%s\"",
1920 upstream_port_name
);
1926 BT_CLI_LOGE_APPEND_CAUSE(
1927 "Cannot connect upstream port: port does not match any connection argument: "
1928 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1929 upstream_port_name
);
1939 bt_graph_listener_func_status
1940 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1941 const bt_port_output
*out_port
)
1943 const bt_component
*comp
;
1944 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1945 bt_graph_listener_func_status ret
=
1946 BT_GRAPH_LISTENER_FUNC_STATUS_OK
;
1948 comp
= bt_port_borrow_component_const(port
);
1949 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1950 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1951 comp
, comp
? bt_component_get_name(comp
) : "",
1952 port
, bt_port_get_name(port
));
1954 if (!ctx
->connect_ports
) {
1960 if (bt_port_is_connected(port
)) {
1961 BT_LOGW_STR("Port is already connected.");
1965 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
1966 BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port.");
1967 ret
= BT_GRAPH_LISTENER_FUNC_STATUS_ERROR
;
1976 bt_graph_listener_func_status
graph_source_output_port_added_listener(
1977 const bt_component_source
*component
,
1978 const bt_port_output
*port
, void *data
)
1980 return graph_output_port_added_listener(data
, port
);
1984 bt_graph_listener_func_status
graph_filter_output_port_added_listener(
1985 const bt_component_filter
*component
,
1986 const bt_port_output
*port
, void *data
)
1988 return graph_output_port_added_listener(data
, port
);
1992 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1998 if (ctx
->src_components
) {
1999 g_hash_table_destroy(ctx
->src_components
);
2000 ctx
->src_components
= NULL
;
2003 if (ctx
->flt_components
) {
2004 g_hash_table_destroy(ctx
->flt_components
);
2005 ctx
->flt_components
= NULL
;
2008 if (ctx
->sink_components
) {
2009 g_hash_table_destroy(ctx
->sink_components
);
2010 ctx
->sink_components
= NULL
;
2013 if (ctx
->intersections
) {
2014 g_hash_table_destroy(ctx
->intersections
);
2015 ctx
->intersections
= NULL
;
2018 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
2024 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2027 bt_graph_add_listener_status add_listener_status
;
2030 ctx
->connect_ports
= false;
2031 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
2032 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2033 if (!ctx
->src_components
) {
2037 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
2038 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2039 if (!ctx
->flt_components
) {
2043 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
2044 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2045 if (!ctx
->sink_components
) {
2049 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2050 ctx
->stream_intersection_mode
= true;
2051 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2052 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2053 if (!ctx
->intersections
) {
2058 ctx
->graph
= bt_graph_create();
2063 the_graph
= ctx
->graph
;
2064 add_listener_status
= bt_graph_add_source_component_output_port_added_listener(
2065 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
2067 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
2068 BT_CLI_LOGE_APPEND_CAUSE(
2069 "Cannot add \"port added\" listener to graph.");
2073 add_listener_status
= bt_graph_add_filter_component_output_port_added_listener(
2074 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
2076 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
2077 BT_CLI_LOGE_APPEND_CAUSE(
2078 "Cannot add \"port added\" listener to graph.");
2085 cmd_run_ctx_destroy(ctx
);
2093 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2094 struct bt_config_component
*cfg_comp
,
2095 const bt_component_class_source
*src_comp_cls
)
2099 int64_t trace_count
;
2100 const char *path
= NULL
;
2101 const bt_value
*query_result
= NULL
;
2102 const bt_value
*trace_info
= NULL
;
2103 const bt_value
*intersection_range
= NULL
;
2104 const bt_value
*intersection_begin
= NULL
;
2105 const bt_value
*intersection_end
= NULL
;
2106 const bt_value
*stream_infos
= NULL
;
2107 const bt_value
*stream_info
= NULL
;
2108 struct port_id
*port_id
= NULL
;
2109 struct trace_range
*trace_range
= NULL
;
2110 const char *fail_reason
= NULL
;
2111 const bt_component_class
*comp_cls
=
2112 bt_component_class_source_as_component_class_const(src_comp_cls
);
2114 ret
= query(ctx
->cfg
, comp_cls
, "trace-info",
2115 cfg_comp
->params
, &query_result
,
2118 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2119 "comp-class-name=\"%s\"", fail_reason
,
2120 bt_component_class_get_name(comp_cls
));
2125 BT_ASSERT(query_result
);
2127 if (!bt_value_is_array(query_result
)) {
2128 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2129 "component-class-name=%s",
2130 bt_component_class_get_name(comp_cls
));
2135 trace_count
= bt_value_array_get_size(query_result
);
2136 if (trace_count
< 0) {
2141 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2143 uint64_t stream_idx
;
2144 int64_t stream_count
;
2146 trace_info
= bt_value_array_borrow_element_by_index_const(
2147 query_result
, trace_idx
);
2148 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2150 BT_LOGD_STR("Cannot retrieve trace from query result.");
2154 intersection_range
= bt_value_map_borrow_entry_value_const(
2155 trace_info
, "intersection-range-ns");
2156 if (!intersection_range
) {
2158 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2162 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
2164 if (!intersection_begin
) {
2166 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2170 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
2172 if (!intersection_end
) {
2174 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2178 begin
= bt_value_signed_integer_get(intersection_begin
);
2179 end
= bt_value_signed_integer_get(intersection_end
);
2181 if (begin
< 0 || end
< 0 || end
< begin
) {
2182 BT_CLI_LOGE_APPEND_CAUSE(
2183 "Invalid trace stream intersection values: "
2184 "intersection-range-ns:begin=%" PRId64
2185 ", intersection-range-ns:end=%" PRId64
,
2191 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
2193 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2195 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2199 stream_count
= bt_value_array_get_size(stream_infos
);
2200 if (stream_count
< 0) {
2205 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2206 const bt_value
*port_name
;
2208 port_id
= g_new0(struct port_id
, 1);
2211 BT_CLI_LOGE_APPEND_CAUSE(
2212 "Cannot allocate memory for port_id structure.");
2215 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2216 if (!port_id
->instance_name
) {
2218 BT_CLI_LOGE_APPEND_CAUSE(
2219 "Cannot allocate memory for port_id component instance name.");
2223 trace_range
= g_new0(struct trace_range
, 1);
2226 BT_CLI_LOGE_APPEND_CAUSE(
2227 "Cannot allocate memory for trace_range structure.");
2230 trace_range
->intersection_range_begin_ns
= begin
;
2231 trace_range
->intersection_range_end_ns
= end
;
2233 stream_info
= bt_value_array_borrow_element_by_index_const(
2234 stream_infos
, stream_idx
);
2235 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2237 BT_CLI_LOGE_APPEND_CAUSE(
2238 "Cannot retrieve stream informations from trace in query result.");
2242 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2243 if (!port_name
|| !bt_value_is_string(port_name
)) {
2245 BT_CLI_LOGE_APPEND_CAUSE(
2246 "Cannot retrieve port name in query result.");
2250 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2251 if (!port_id
->port_name
) {
2253 BT_CLI_LOGE_APPEND_CAUSE(
2254 "Cannot allocate memory for port_id port_name.");
2258 BT_LOGD("Inserting stream intersection ");
2260 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2270 BT_CLI_LOGE_APPEND_CAUSE(
2271 "Cannot determine stream intersection of trace: path=\"%s\"",
2272 path
? path
: "(unknown)");
2275 bt_value_put_ref(query_result
);
2277 g_free(trace_range
);
2282 int cmd_run_ctx_create_components_from_config_components(
2283 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2286 const void *comp_cls
= NULL
;
2287 const void *comp
= NULL
;
2290 for (i
= 0; i
< cfg_components
->len
; i
++) {
2291 struct bt_config_component
*cfg_comp
=
2292 g_ptr_array_index(cfg_components
, i
);
2295 switch (cfg_comp
->type
) {
2296 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2297 comp_cls
= find_source_component_class(
2298 cfg_comp
->plugin_name
->str
,
2299 cfg_comp
->comp_cls_name
->str
);
2301 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2302 comp_cls
= find_filter_component_class(
2303 cfg_comp
->plugin_name
->str
,
2304 cfg_comp
->comp_cls_name
->str
);
2306 case BT_COMPONENT_CLASS_TYPE_SINK
:
2307 comp_cls
= find_sink_component_class(
2308 cfg_comp
->plugin_name
->str
,
2309 cfg_comp
->comp_cls_name
->str
);
2316 BT_CLI_LOGE_APPEND_CAUSE(
2317 "Cannot find component class: plugin-name=\"%s\", "
2318 "comp-cls-name=\"%s\", comp-cls-type=%d",
2319 cfg_comp
->plugin_name
->str
,
2320 cfg_comp
->comp_cls_name
->str
,
2325 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_TRACE
);
2327 switch (cfg_comp
->type
) {
2328 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2329 ret
= bt_graph_add_source_component(ctx
->graph
,
2330 comp_cls
, cfg_comp
->instance_name
->str
,
2331 cfg_comp
->params
, cfg_comp
->log_level
,
2334 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2335 ret
= bt_graph_add_filter_component(ctx
->graph
,
2336 comp_cls
, cfg_comp
->instance_name
->str
,
2337 cfg_comp
->params
, cfg_comp
->log_level
,
2340 case BT_COMPONENT_CLASS_TYPE_SINK
:
2341 ret
= bt_graph_add_sink_component(ctx
->graph
,
2342 comp_cls
, cfg_comp
->instance_name
->str
,
2343 cfg_comp
->params
, cfg_comp
->log_level
,
2351 BT_CLI_LOGE_APPEND_CAUSE(
2352 "Cannot create component: plugin-name=\"%s\", "
2353 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2355 cfg_comp
->plugin_name
->str
,
2356 cfg_comp
->comp_cls_name
->str
,
2357 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2361 if (ctx
->stream_intersection_mode
&&
2362 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2363 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2369 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2370 comp
, cfg_comp
->instance_name
->str
);
2371 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2372 BT_ASSERT(quark
> 0);
2374 switch (cfg_comp
->type
) {
2375 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2376 g_hash_table_insert(ctx
->src_components
,
2377 GUINT_TO_POINTER(quark
), (void *) comp
);
2379 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2380 g_hash_table_insert(ctx
->flt_components
,
2381 GUINT_TO_POINTER(quark
), (void *) comp
);
2383 case BT_COMPONENT_CLASS_TYPE_SINK
:
2384 g_hash_table_insert(ctx
->sink_components
,
2385 GUINT_TO_POINTER(quark
), (void *) comp
);
2392 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2401 bt_object_put_ref(comp
);
2402 bt_object_put_ref(comp_cls
);
2407 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2412 * Make sure that, during this phase, our graph's "port added"
2413 * listener does not connect ports while we are creating the
2414 * components because we have a special, initial phase for
2417 ctx
->connect_ports
= false;
2419 ret
= cmd_run_ctx_create_components_from_config_components(
2420 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2426 ret
= cmd_run_ctx_create_components_from_config_components(
2427 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2433 ret
= cmd_run_ctx_create_components_from_config_components(
2434 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2444 typedef uint64_t (*output_port_count_func_t
)(const void *);
2445 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2446 const void *, uint64_t);
2449 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2450 void *comp
, output_port_count_func_t port_count_fn
,
2451 borrow_output_port_by_index_func_t port_by_index_fn
)
2457 count
= port_count_fn(comp
);
2459 for (i
= 0; i
< count
; i
++) {
2460 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2462 BT_ASSERT(upstream_port
);
2463 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2474 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2477 GHashTableIter iter
;
2478 gpointer g_name_quark
, g_comp
;
2480 ctx
->connect_ports
= true;
2481 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2483 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2484 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2485 (output_port_count_func_t
)
2486 bt_component_source_get_output_port_count
,
2487 (borrow_output_port_by_index_func_t
)
2488 bt_component_source_borrow_output_port_by_index_const
);
2494 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2496 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2497 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2498 (output_port_count_func_t
)
2499 bt_component_filter_get_output_port_count
,
2500 (borrow_output_port_by_index_func_t
)
2501 bt_component_filter_borrow_output_port_by_index_const
);
2512 int cmd_run(struct bt_config
*cfg
)
2515 struct cmd_run_ctx ctx
= { 0 };
2517 /* Initialize the command's context and the graph object */
2518 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2519 BT_CLI_LOGE_APPEND_CAUSE(
2520 "Cannot initialize the command's context.");
2525 BT_CLI_LOGW_APPEND_CAUSE(
2526 "Canceled by user before creating components.");
2530 BT_LOGI_STR("Creating components.");
2532 /* Create the requested component instances */
2533 if (cmd_run_ctx_create_components(&ctx
)) {
2534 BT_CLI_LOGE_APPEND_CAUSE("Cannot create components.");
2539 BT_CLI_LOGW_APPEND_CAUSE(
2540 "Canceled by user before connecting components.");
2544 BT_LOGI_STR("Connecting components.");
2546 /* Connect the initially visible component ports */
2547 if (cmd_run_ctx_connect_ports(&ctx
)) {
2548 BT_CLI_LOGE_APPEND_CAUSE(
2549 "Cannot connect initial component ports.");
2554 BT_CLI_LOGW_APPEND_CAUSE(
2555 "Canceled by user before running the graph.");
2559 BT_LOGI_STR("Running the graph.");
2563 bt_graph_run_status run_status
= bt_graph_run(ctx
.graph
);
2566 * Reset console in case something messed with console
2567 * codes during the graph's execution.
2569 printf("%s", bt_common_color_reset());
2571 fprintf(stderr
, "%s", bt_common_color_reset());
2572 BT_LOGT("bt_graph_run() returned: status=%s",
2573 bt_common_func_status_string(run_status
));
2575 switch (run_status
) {
2576 case BT_GRAPH_RUN_STATUS_OK
:
2578 case BT_GRAPH_RUN_STATUS_CANCELED
:
2579 BT_CLI_LOGW_APPEND_CAUSE("Graph was canceled by user.");
2581 case BT_GRAPH_RUN_STATUS_AGAIN
:
2582 if (bt_graph_is_canceled(ctx
.graph
)) {
2583 BT_CLI_LOGW_APPEND_CAUSE(
2584 "Graph was canceled by user.");
2588 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2589 BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: "
2591 cfg
->cmd_data
.run
.retry_duration_us
);
2593 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2594 if (bt_graph_is_canceled(ctx
.graph
)) {
2595 BT_CLI_LOGW_APPEND_CAUSE(
2596 "Graph was canceled by user.");
2602 case BT_GRAPH_RUN_STATUS_END
:
2605 BT_CLI_LOGE_APPEND_CAUSE(
2606 "Graph failed to complete successfully");
2619 cmd_run_ctx_destroy(&ctx
);
2624 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2626 const char *env_clash
;
2628 if (!cfg
->command_name
) {
2632 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2633 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2637 if (g_file_test(cfg
->command_name
,
2638 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2639 _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION
, __FILE__
, __LINE__
,
2640 BT_LOG_WARNING
, BT_LOG_TAG
,
2641 "The `%s` command was executed. "
2642 "If you meant to convert a trace located in "
2643 "the local `%s` directory, please use:\n\n"
2644 " babeltrace2 convert %s [OPTIONS]",
2645 cfg
->command_name
, cfg
->command_name
,
2651 void init_log_level(void)
2653 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2657 void set_auto_log_levels(struct bt_config
*cfg
)
2659 const char **env_var_name
;
2662 * Override the configuration's default log level if
2663 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2664 * are found for backward compatibility with legacy Babetrace 1.
2666 if (getenv("BABELTRACE_DEBUG") &&
2667 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2668 cfg
->log_level
= BT_LOG_TRACE
;
2669 } else if (getenv("BABELTRACE_VERBOSE") &&
2670 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2671 cfg
->log_level
= BT_LOG_INFO
;
2675 * Set log levels according to --debug or --verbose. For
2676 * backward compatibility, --debug is more verbose than
2679 * --verbose: INFO log level
2680 * --debug: TRACE log level (includes DEBUG, which is
2681 * is less verbose than TRACE in the internal
2682 * logging framework)
2684 if (!getenv("LIBBABELTRACE2_INIT_LOG_LEVEL")) {
2686 bt_logging_set_global_level(BT_LOG_INFO
);
2687 } else if (cfg
->debug
) {
2688 bt_logging_set_global_level(BT_LOG_TRACE
);
2691 * Set library's default log level if not
2692 * explicitly specified.
2694 bt_logging_set_global_level(cfg
->log_level
);
2698 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2700 bt_cli_log_level
= BT_LOG_INFO
;
2701 } else if (cfg
->debug
) {
2702 bt_cli_log_level
= BT_LOG_TRACE
;
2705 * Set CLI's default log level if not explicitly
2708 bt_cli_log_level
= cfg
->log_level
;
2712 env_var_name
= log_level_env_var_names
;
2714 while (*env_var_name
) {
2715 if (!getenv(*env_var_name
)) {
2717 g_setenv(*env_var_name
, "INFO", 1);
2718 } else if (cfg
->debug
) {
2719 g_setenv(*env_var_name
, "TRACE", 1);
2721 char val
[2] = { 0 };
2724 * Set module's default log level if not
2725 * explicitly specified.
2727 val
[0] = bt_log_get_letter_from_level(
2729 g_setenv(*env_var_name
, val
, 1);
2738 void print_error_causes(void)
2740 const bt_error
*error
= bt_current_thread_take_error();
2742 GString
*folded
= NULL
;
2743 unsigned int columns
;
2745 if (!error
|| bt_error_get_cause_count(error
) == 0) {
2746 fprintf(stderr
, "%s%sUnknown command-line error.%s\n",
2747 bt_common_color_bold(), bt_common_color_fg_red(),
2748 bt_common_color_reset());
2752 /* Try to get terminal width to fold the error cause messages */
2753 if (bt_common_get_term_size(&columns
, NULL
) < 0) {
2754 /* Width not found: default to 80 */
2759 * This helps visually separate the error causes from the last
2760 * logging statement.
2762 fprintf(stderr
, "\n");
2764 /* Reverse order: deepest (root) cause printed at the end */
2765 for (i
= bt_error_get_cause_count(error
) - 1; i
>= 0; i
--) {
2766 const bt_error_cause
*cause
=
2767 bt_error_borrow_cause_by_index(error
, (uint64_t) i
);
2768 const char *prefix_fmt
=
2769 i
== bt_error_get_cause_count(error
) - 1 ?
2770 "%s%sERROR%s: " : "%s%sCAUSED BY%s ";
2773 fprintf(stderr
, prefix_fmt
,
2774 bt_common_color_bold(), bt_common_color_fg_red(),
2775 bt_common_color_reset());
2777 /* Print actor name */
2778 fprintf(stderr
, "[");
2779 switch (bt_error_cause_get_actor_type(cause
)) {
2780 case BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN
:
2781 fprintf(stderr
, "%s%s%s",
2782 bt_common_color_bold(),
2783 bt_error_cause_get_module_name(cause
),
2784 bt_common_color_reset());
2786 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
:
2787 fprintf(stderr
, "%s%s%s: ",
2788 bt_common_color_bold(),
2789 bt_error_cause_component_actor_get_component_name(cause
),
2790 bt_common_color_reset());
2791 print_plugin_comp_cls_opt(stderr
,
2792 bt_error_cause_component_actor_get_plugin_name(cause
),
2793 bt_error_cause_component_actor_get_component_class_name(cause
),
2794 bt_error_cause_component_actor_get_component_class_type(cause
));
2796 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
:
2797 print_plugin_comp_cls_opt(stderr
,
2798 bt_error_cause_component_class_actor_get_plugin_name(cause
),
2799 bt_error_cause_component_class_actor_get_component_class_name(cause
),
2800 bt_error_cause_component_class_actor_get_component_class_type(cause
));
2802 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
:
2803 fprintf(stderr
, "%s%s%s (%s%s%s): ",
2804 bt_common_color_bold(),
2805 bt_error_cause_message_iterator_actor_get_component_name(cause
),
2806 bt_common_color_reset(),
2807 bt_common_color_bold(),
2808 bt_error_cause_message_iterator_actor_get_component_output_port_name(cause
),
2809 bt_common_color_reset());
2810 print_plugin_comp_cls_opt(stderr
,
2811 bt_error_cause_message_iterator_actor_get_plugin_name(cause
),
2812 bt_error_cause_message_iterator_actor_get_component_class_name(cause
),
2813 bt_error_cause_message_iterator_actor_get_component_class_type(cause
));
2819 /* Print file name and line number */
2820 fprintf(stderr
, "] (%s%s%s%s:%s%" PRIu64
"%s)\n",
2821 bt_common_color_bold(),
2822 bt_common_color_fg_magenta(),
2823 bt_error_cause_get_file_name(cause
),
2824 bt_common_color_reset(),
2825 bt_common_color_fg_green(),
2826 bt_error_cause_get_line_number(cause
),
2827 bt_common_color_reset());
2830 folded
= bt_common_fold(bt_error_cause_get_message(cause
),
2833 BT_LOGE_STR("Could not fold string.");
2834 fprintf(stderr
, "%s\n",
2835 bt_error_cause_get_message(cause
));
2839 fprintf(stderr
, "%s\n", folded
->str
);
2840 g_string_free(folded
, TRUE
);
2846 g_string_free(folded
, TRUE
);
2850 bt_error_release(error
);
2854 int main(int argc
, const char **argv
)
2858 struct bt_config
*cfg
;
2861 set_signal_handler();
2863 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2866 /* Quit without errors; typically usage/version */
2868 BT_LOGI_STR("Quitting without errors.");
2873 BT_CLI_LOGE_APPEND_CAUSE(
2874 "Command-line error: retcode=%d", retcode
);
2879 BT_CLI_LOGE_APPEND_CAUSE(
2880 "Failed to create a valid Babeltrace configuration.");
2885 set_auto_log_levels(cfg
);
2888 if (cfg
->command_needs_plugins
) {
2889 ret
= load_all_plugins(cfg
->plugin_paths
);
2891 BT_CLI_LOGE_APPEND_CAUSE(
2892 "Failed to load plugins: ret=%d", ret
);
2898 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2899 cfg
->command
, cfg
->command_name
);
2901 switch (cfg
->command
) {
2902 case BT_CONFIG_COMMAND_RUN
:
2905 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2906 ret
= cmd_list_plugins(cfg
);
2908 case BT_CONFIG_COMMAND_HELP
:
2909 ret
= cmd_help(cfg
);
2911 case BT_CONFIG_COMMAND_QUERY
:
2912 ret
= cmd_query(cfg
);
2914 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2915 ret
= cmd_print_ctf_metadata(cfg
);
2917 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2918 ret
= cmd_print_lttng_live_sessions(cfg
);
2921 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2925 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2926 cfg
->command
, cfg
->command_name
, ret
);
2927 warn_command_name_and_directory_clash(cfg
);
2928 retcode
= ret
? 1 : 0;
2931 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2935 print_error_causes();
2939 * Clear current thread's error in case there is one to avoid a
2942 bt_current_thread_clear_error();