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_LOGI("Canceled by user before executing the query: "
177 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
178 "query-obj=\"%s\"", comp_cls
,
179 bt_component_class_get_name(comp_cls
), obj
);
180 *fail_reason
= "canceled by user";
185 query_status
= bt_query_executor_query(
186 the_query_executor
, comp_cls
, obj
, params
,
187 cfg
->log_level
, &result
);
188 switch (query_status
) {
189 case BT_QUERY_EXECUTOR_QUERY_STATUS_OK
:
191 case BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN
:
193 const uint64_t sleep_time_us
= 100000;
195 /* Wait 100 ms and retry */
196 BT_LOGD("Got BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN: sleeping: "
197 "time-us=%" PRIu64
, sleep_time_us
);
199 if (usleep(sleep_time_us
)) {
200 if (bt_query_executor_is_canceled(the_query_executor
)) {
201 BT_LOGI("Query was canceled by user: "
202 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
203 "query-obj=\"%s\"", comp_cls
,
204 bt_component_class_get_name(comp_cls
),
206 *fail_reason
= "canceled by user";
213 case BT_QUERY_EXECUTOR_QUERY_STATUS_CANCELED
:
214 *fail_reason
= "canceled by user";
216 case BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR
:
218 case BT_QUERY_EXECUTOR_QUERY_STATUS_INVALID_OBJECT
:
219 *fail_reason
= "invalid or unknown query object";
221 case BT_QUERY_EXECUTOR_QUERY_STATUS_INVALID_PARAMS
:
222 *fail_reason
= "invalid query parameters";
224 case BT_QUERY_EXECUTOR_QUERY_STATUS_UNSUPPORTED
:
225 *fail_reason
= "unsupported action";
227 case BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR
:
228 *fail_reason
= "not enough memory";
231 BT_LOGF("Unknown query status: status=%s",
232 bt_common_func_status_string(query_status
));
238 *user_result
= result
;
246 destroy_the_query_executor();
247 bt_value_put_ref(result
);
252 const bt_plugin
*find_plugin(const char *name
)
255 const bt_plugin
*plugin
= NULL
;
258 BT_LOGI("Finding plugin: name=\"%s\"", name
);
260 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
261 plugin
= g_ptr_array_index(loaded_plugins
, i
);
263 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
271 BT_LOGI("Found plugin: name=\"%s\", plugin-addr=%p",
274 BT_LOGI("Cannot find plugin: name=\"%s\"", name
);
277 bt_plugin_get_ref(plugin
);
281 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
282 const bt_plugin
*, const char *);
285 const void *find_component_class_from_plugin(const char *plugin_name
,
286 const char *comp_class_name
,
287 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
289 const void *comp_class
= NULL
;
290 const bt_plugin
*plugin
;
292 BT_LOGI("Finding component class: plugin-name=\"%s\", "
293 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
295 plugin
= find_plugin(plugin_name
);
300 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
301 bt_object_get_ref(comp_class
);
302 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
306 BT_LOGI("Found component class: plugin-name=\"%s\", "
307 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
309 BT_LOGI("Cannot find source component class: "
310 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
311 plugin_name
, comp_class_name
);
318 const bt_component_class_source
*find_source_component_class(
319 const char *plugin_name
, const char *comp_class_name
)
321 return (const void *) find_component_class_from_plugin(
322 plugin_name
, comp_class_name
,
323 (plugin_borrow_comp_cls_func_t
)
324 bt_plugin_borrow_source_component_class_by_name_const
);
328 const bt_component_class_filter
*find_filter_component_class(
329 const char *plugin_name
, const char *comp_class_name
)
331 return (const void *) find_component_class_from_plugin(
332 plugin_name
, comp_class_name
,
333 (plugin_borrow_comp_cls_func_t
)
334 bt_plugin_borrow_filter_component_class_by_name_const
);
338 const bt_component_class_sink
*find_sink_component_class(
339 const char *plugin_name
, const char *comp_class_name
)
341 return (const void *) find_component_class_from_plugin(plugin_name
,
343 (plugin_borrow_comp_cls_func_t
)
344 bt_plugin_borrow_sink_component_class_by_name_const
);
348 const bt_component_class
*find_component_class(const char *plugin_name
,
349 const char *comp_class_name
,
350 bt_component_class_type comp_class_type
)
352 const bt_component_class
*comp_cls
= NULL
;
354 switch (comp_class_type
) {
355 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
356 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
358 case BT_COMPONENT_CLASS_TYPE_FILTER
:
359 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
361 case BT_COMPONENT_CLASS_TYPE_SINK
:
362 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
372 void print_indent(FILE *fp
, size_t indent
)
376 for (i
= 0; i
< indent
; i
++) {
382 const char *component_type_str(bt_component_class_type type
)
385 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
387 case BT_COMPONENT_CLASS_TYPE_SINK
:
389 case BT_COMPONENT_CLASS_TYPE_FILTER
:
397 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
398 const char *comp_cls_name
, bt_component_class_type type
)
400 GString
*shell_plugin_name
= NULL
;
401 GString
*shell_comp_cls_name
= NULL
;
403 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
404 if (!shell_plugin_name
) {
408 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
409 if (!shell_comp_cls_name
) {
413 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
414 bt_common_color_bold(),
415 bt_common_color_fg_cyan(),
416 component_type_str(type
),
417 bt_common_color_fg_default(),
418 bt_common_color_fg_blue(),
419 shell_plugin_name
->str
,
420 bt_common_color_fg_default(),
421 bt_common_color_fg_yellow(),
422 shell_comp_cls_name
->str
,
423 bt_common_color_reset());
426 if (shell_plugin_name
) {
427 g_string_free(shell_plugin_name
, TRUE
);
430 if (shell_comp_cls_name
) {
431 g_string_free(shell_comp_cls_name
, TRUE
);
436 void print_value(FILE *, const bt_value
*, size_t);
439 void print_value_rec(FILE *, const bt_value
*, size_t);
441 struct print_map_value_data
{
447 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
450 struct print_map_value_data
*print_map_value_data
= data
;
452 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
453 fprintf(print_map_value_data
->fp
, "%s: ", key
);
456 if (bt_value_is_array(object
) &&
457 bt_value_array_is_empty(object
)) {
458 fprintf(print_map_value_data
->fp
, "[ ]\n");
462 if (bt_value_is_map(object
) &&
463 bt_value_map_is_empty(object
)) {
464 fprintf(print_map_value_data
->fp
, "{ }\n");
468 if (bt_value_is_array(object
) ||
469 bt_value_is_map(object
)) {
470 fprintf(print_map_value_data
->fp
, "\n");
473 print_value_rec(print_map_value_data
->fp
, object
,
474 print_map_value_data
->indent
+ 2);
479 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
493 switch (bt_value_get_type(value
)) {
494 case BT_VALUE_TYPE_NULL
:
495 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
496 bt_common_color_reset());
498 case BT_VALUE_TYPE_BOOL
:
499 bool_val
= bt_value_bool_get(value
);
500 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
501 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
502 bt_common_color_reset());
504 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
505 uint_val
= bt_value_unsigned_integer_get(value
);
506 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
507 bt_common_color_fg_red(), uint_val
,
508 bt_common_color_reset());
510 case BT_VALUE_TYPE_SIGNED_INTEGER
:
511 int_val
= bt_value_signed_integer_get(value
);
512 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
513 bt_common_color_fg_red(), int_val
,
514 bt_common_color_reset());
516 case BT_VALUE_TYPE_REAL
:
517 dbl_val
= bt_value_real_get(value
);
518 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
519 bt_common_color_fg_red(), dbl_val
,
520 bt_common_color_reset());
522 case BT_VALUE_TYPE_STRING
:
523 str_val
= bt_value_string_get(value
);
524 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
525 bt_common_color_fg_green(), str_val
,
526 bt_common_color_reset());
528 case BT_VALUE_TYPE_ARRAY
:
529 size
= bt_value_array_get_size(value
);
535 print_indent(fp
, indent
);
536 fprintf(fp
, "[ ]\n");
540 for (i
= 0; i
< size
; i
++) {
541 const bt_value
*element
=
542 bt_value_array_borrow_element_by_index_const(
548 print_indent(fp
, indent
);
551 if (bt_value_is_array(element
) &&
552 bt_value_array_is_empty(element
)) {
553 fprintf(fp
, "[ ]\n");
557 if (bt_value_is_map(element
) &&
558 bt_value_map_is_empty(element
)) {
559 fprintf(fp
, "{ }\n");
563 if (bt_value_is_array(element
) ||
564 bt_value_is_map(element
)) {
568 print_value_rec(fp
, element
, indent
+ 2);
571 case BT_VALUE_TYPE_MAP
:
573 struct print_map_value_data data
= {
578 if (bt_value_map_is_empty(value
)) {
579 print_indent(fp
, indent
);
580 fprintf(fp
, "{ }\n");
584 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
593 BT_LOGE("Error printing value of type %s.",
594 bt_common_value_type_string(bt_value_get_type(value
)));
598 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
600 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
601 print_indent(fp
, indent
);
604 print_value_rec(fp
, value
, indent
);
608 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
610 fprintf(stderr
, " ");
611 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
612 bt_config_component
->comp_cls_name
->str
,
613 bt_config_component
->type
);
614 fprintf(stderr
, ":\n");
616 if (bt_config_component
->instance_name
->len
> 0) {
617 fprintf(stderr
, " Name: %s\n",
618 bt_config_component
->instance_name
->str
);
621 fprintf(stderr
, " Parameters:\n");
622 print_value(stderr
, bt_config_component
->params
, 8);
626 void print_bt_config_components(GPtrArray
*array
)
630 for (i
= 0; i
< array
->len
; i
++) {
631 struct bt_config_component
*cfg_component
=
632 bt_config_get_component(array
, i
);
633 print_bt_config_component(cfg_component
);
634 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
639 void print_plugin_paths(const bt_value
*plugin_paths
)
641 fprintf(stderr
, " Plugin paths:\n");
642 print_value(stderr
, plugin_paths
, 4);
646 void print_cfg_run(struct bt_config
*cfg
)
650 print_plugin_paths(cfg
->plugin_paths
);
651 fprintf(stderr
, " Source component instances:\n");
652 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
654 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
655 fprintf(stderr
, " Filter component instances:\n");
656 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
659 fprintf(stderr
, " Sink component instances:\n");
660 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
661 fprintf(stderr
, " Connections:\n");
663 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
664 struct bt_config_connection
*cfg_connection
=
665 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
668 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
669 cfg_connection
->upstream_comp_name
->str
,
670 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
671 cfg_connection
->upstream_port_glob
->str
,
672 cfg_connection
->downstream_comp_name
->str
,
673 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
674 cfg_connection
->downstream_port_glob
->str
);
679 void print_cfg_list_plugins(struct bt_config
*cfg
)
681 print_plugin_paths(cfg
->plugin_paths
);
685 void print_cfg_help(struct bt_config
*cfg
)
687 print_plugin_paths(cfg
->plugin_paths
);
691 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
693 print_plugin_paths(cfg
->plugin_paths
);
694 fprintf(stderr
, " Path: %s\n",
695 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
699 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
701 print_plugin_paths(cfg
->plugin_paths
);
702 fprintf(stderr
, " URL: %s\n",
703 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
707 void print_cfg_query(struct bt_config
*cfg
)
709 print_plugin_paths(cfg
->plugin_paths
);
710 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
711 fprintf(stderr
, " Component class:\n");
712 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
716 void print_cfg(struct bt_config
*cfg
)
718 if (!BT_LOG_ON_INFO
) {
722 BT_LOGI_STR("CLI configuration:");
723 BT_LOGI(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
724 BT_LOGI(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
726 switch (cfg
->command
) {
727 case BT_CONFIG_COMMAND_RUN
:
730 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
731 print_cfg_list_plugins(cfg
);
733 case BT_CONFIG_COMMAND_HELP
:
736 case BT_CONFIG_COMMAND_QUERY
:
737 print_cfg_query(cfg
);
739 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
740 print_cfg_print_ctf_metadata(cfg
);
742 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
743 print_cfg_print_lttng_live_sessions(cfg
);
751 void add_to_loaded_plugins(const bt_plugin_set
*plugin_set
)
756 count
= bt_plugin_set_get_plugin_count(plugin_set
);
757 BT_ASSERT(count
>= 0);
759 for (i
= 0; i
< count
; i
++) {
760 const bt_plugin
*plugin
=
761 bt_plugin_set_borrow_plugin_by_index_const(plugin_set
, i
);
762 const bt_plugin
*loaded_plugin
=
763 find_plugin(bt_plugin_get_name(plugin
));
768 BT_LOGI("Not using plugin: another one already exists with the same name: "
769 "plugin-name=\"%s\", plugin-path=\"%s\", "
770 "existing-plugin-path=\"%s\"",
771 bt_plugin_get_name(plugin
),
772 bt_plugin_get_path(plugin
),
773 bt_plugin_get_path(loaded_plugin
));
774 bt_plugin_put_ref(loaded_plugin
);
776 /* Add to global array. */
777 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
778 bt_plugin_get_name(plugin
));
779 bt_plugin_get_ref(plugin
);
780 g_ptr_array_add(loaded_plugins
, (void *) plugin
);
786 int load_dynamic_plugins(const bt_value
*plugin_paths
)
788 int nr_paths
, i
, ret
= 0;
790 nr_paths
= bt_value_array_get_size(plugin_paths
);
792 BT_CLI_LOGE_APPEND_CAUSE(
793 "Cannot load dynamic plugins: no plugin path.");
798 BT_LOGI_STR("Loading dynamic plugins.");
800 for (i
= 0; i
< nr_paths
; i
++) {
801 const bt_value
*plugin_path_value
= NULL
;
802 const char *plugin_path
;
803 const bt_plugin_set
*plugin_set
= NULL
;
804 bt_plugin_find_all_from_dir_status status
;
807 bt_value_array_borrow_element_by_index_const(
809 plugin_path
= bt_value_string_get(plugin_path_value
);
812 * Skip this if the directory does not exist because
813 * bt_plugin_find_all_from_dir() expects an existing
816 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
817 BT_LOGI("Skipping nonexistent directory path: "
818 "path=\"%s\"", plugin_path
);
822 status
= bt_plugin_find_all_from_dir(plugin_path
, BT_FALSE
,
823 BT_FALSE
, &plugin_set
);
825 BT_CLI_LOGE_APPEND_CAUSE(
826 "Unable to load dynamic plugins from directory: "
827 "path=\"%s\"", plugin_path
);
831 BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_NOT_FOUND
) {
832 BT_LOGI("No plugins found in directory: path=\"%s\"",
837 BT_ASSERT(status
== BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_OK
);
838 BT_ASSERT(plugin_set
);
839 add_to_loaded_plugins(plugin_set
);
840 bt_plugin_set_put_ref(plugin_set
);
847 int load_static_plugins(void)
850 const bt_plugin_set
*plugin_set
;
851 bt_plugin_find_all_from_static_status status
;
853 BT_LOGI("Loading static plugins.");
854 status
= bt_plugin_find_all_from_static(BT_FALSE
, &plugin_set
);
856 BT_CLI_LOGE_APPEND_CAUSE("Unable to load static plugins.");
860 BT_PLUGIN_FIND_ALL_FROM_STATIC_STATUS_NOT_FOUND
) {
861 BT_LOGI("No static plugins found.");
865 BT_ASSERT(status
== BT_PLUGIN_FIND_ALL_FROM_STATIC_STATUS_OK
);
866 BT_ASSERT(plugin_set
);
867 add_to_loaded_plugins(plugin_set
);
868 bt_plugin_set_put_ref(plugin_set
);
875 int load_all_plugins(const bt_value
*plugin_paths
)
879 if (load_dynamic_plugins(plugin_paths
)) {
884 if (load_static_plugins()) {
889 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
896 void print_plugin_info(const bt_plugin
*plugin
)
898 unsigned int major
, minor
, patch
;
900 bt_property_availability version_avail
;
901 const char *plugin_name
;
905 const char *plugin_description
;
907 plugin_name
= bt_plugin_get_name(plugin
);
908 path
= bt_plugin_get_path(plugin
);
909 author
= bt_plugin_get_author(plugin
);
910 license
= bt_plugin_get_license(plugin
);
911 plugin_description
= bt_plugin_get_description(plugin
);
912 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
914 printf("%s%s%s%s:\n", bt_common_color_bold(),
915 bt_common_color_fg_blue(), plugin_name
,
916 bt_common_color_reset());
918 printf(" %sPath%s: %s\n", bt_common_color_bold(),
919 bt_common_color_reset(), path
);
924 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
925 printf(" %sVersion%s: %u.%u.%u",
926 bt_common_color_bold(), bt_common_color_reset(),
927 major
, minor
, patch
);
936 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
937 bt_common_color_reset(),
938 plugin_description
? plugin_description
: "(None)");
939 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
940 bt_common_color_reset(), author
? author
: "(Unknown)");
941 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
942 bt_common_color_reset(),
943 license
? license
: "(Unknown)");
947 int cmd_query(struct bt_config
*cfg
)
950 const bt_component_class
*comp_cls
= NULL
;
951 const bt_value
*results
= NULL
;
952 const char *fail_reason
= NULL
;
954 comp_cls
= find_component_class(
955 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
956 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
957 cfg
->cmd_data
.query
.cfg_component
->type
);
959 BT_CLI_LOGE_APPEND_CAUSE(
960 "Cannot find component class: plugin-name=\"%s\", "
961 "comp-cls-name=\"%s\", comp-cls-type=%d",
962 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
963 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
964 cfg
->cmd_data
.query
.cfg_component
->type
);
969 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
970 cfg
->cmd_data
.query
.cfg_component
->params
,
971 &results
, &fail_reason
);
976 print_value(stdout
, results
, 0);
980 BT_CLI_LOGE_APPEND_CAUSE(
981 "Failed to query component class: %s: plugin-name=\"%s\", "
982 "comp-cls-name=\"%s\", comp-cls-type=%d "
983 "object=\"%s\"", fail_reason
,
984 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
985 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
986 cfg
->cmd_data
.query
.cfg_component
->type
,
987 cfg
->cmd_data
.query
.object
->str
);
991 bt_component_class_put_ref(comp_cls
);
992 bt_value_put_ref(results
);
997 void print_component_class_help(const char *plugin_name
,
998 const bt_component_class
*comp_cls
)
1000 const char *comp_class_name
=
1001 bt_component_class_get_name(comp_cls
);
1002 const char *comp_class_description
=
1003 bt_component_class_get_description(comp_cls
);
1004 const char *comp_class_help
=
1005 bt_component_class_get_help(comp_cls
);
1006 bt_component_class_type type
=
1007 bt_component_class_get_type(comp_cls
);
1009 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
1011 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1012 bt_common_color_reset(),
1013 comp_class_description
? comp_class_description
: "(None)");
1015 if (comp_class_help
) {
1016 printf("\n%s\n", comp_class_help
);
1021 int cmd_help(struct bt_config
*cfg
)
1024 const bt_plugin
*plugin
= NULL
;
1025 const bt_component_class
*needed_comp_cls
= NULL
;
1027 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1029 BT_CLI_LOGE_APPEND_CAUSE(
1030 "Cannot find plugin: plugin-name=\"%s\"",
1031 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1036 print_plugin_info(plugin
);
1037 printf(" %sSource component classes%s: %d\n",
1038 bt_common_color_bold(),
1039 bt_common_color_reset(),
1040 (int) bt_plugin_get_source_component_class_count(plugin
));
1041 printf(" %sFilter component classes%s: %d\n",
1042 bt_common_color_bold(),
1043 bt_common_color_reset(),
1044 (int) bt_plugin_get_filter_component_class_count(plugin
));
1045 printf(" %sSink component classes%s: %d\n",
1046 bt_common_color_bold(),
1047 bt_common_color_reset(),
1048 (int) bt_plugin_get_sink_component_class_count(plugin
));
1050 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
1051 /* Plugin help only */
1055 needed_comp_cls
= find_component_class(
1056 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1057 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1058 cfg
->cmd_data
.help
.cfg_component
->type
);
1059 if (!needed_comp_cls
) {
1060 BT_CLI_LOGE_APPEND_CAUSE(
1061 "Cannot find component class: plugin-name=\"%s\", "
1062 "comp-cls-name=\"%s\", comp-cls-type=%d",
1063 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1064 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1065 cfg
->cmd_data
.help
.cfg_component
->type
);
1071 print_component_class_help(
1072 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1076 bt_component_class_put_ref(needed_comp_cls
);
1077 bt_plugin_put_ref(plugin
);
1081 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
1083 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
1086 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
1087 const char *cc_type_name
, uint64_t count
,
1088 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
1089 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
1094 printf(" %s%s component classes%s: (none)\n",
1095 bt_common_color_bold(),
1097 bt_common_color_reset());
1100 printf(" %s%s component classes%s:\n",
1101 bt_common_color_bold(),
1103 bt_common_color_reset());
1106 for (i
= 0; i
< count
; i
++) {
1107 const bt_component_class
*comp_class
=
1108 spec_comp_cls_borrow_comp_cls_func(
1109 borrow_comp_cls_by_index_func(plugin
, i
));
1110 const char *comp_class_name
=
1111 bt_component_class_get_name(comp_class
);
1112 const char *comp_class_description
=
1113 bt_component_class_get_description(comp_class
);
1114 bt_component_class_type type
=
1115 bt_component_class_get_type(comp_class
);
1118 print_plugin_comp_cls_opt(stdout
,
1119 bt_plugin_get_name(plugin
), comp_class_name
,
1122 if (comp_class_description
) {
1123 printf(": %s", comp_class_description
);
1134 int cmd_list_plugins(struct bt_config
*cfg
)
1137 int plugins_count
, component_classes_count
= 0, i
;
1139 printf("From the following plugin paths:\n\n");
1140 print_value(stdout
, cfg
->plugin_paths
, 2);
1142 plugins_count
= loaded_plugins
->len
;
1143 if (plugins_count
== 0) {
1144 printf("No plugins found.\n");
1148 for (i
= 0; i
< plugins_count
; i
++) {
1149 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1151 component_classes_count
+=
1152 bt_plugin_get_source_component_class_count(plugin
) +
1153 bt_plugin_get_filter_component_class_count(plugin
) +
1154 bt_plugin_get_sink_component_class_count(plugin
);
1157 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1158 bt_common_color_bold(),
1159 component_classes_count
,
1160 bt_common_color_reset(),
1161 bt_common_color_bold(),
1163 bt_common_color_reset());
1165 for (i
= 0; i
< plugins_count
; i
++) {
1166 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1169 print_plugin_info(plugin
);
1170 cmd_list_plugins_print_component_classes(plugin
, "Source",
1171 bt_plugin_get_source_component_class_count(plugin
),
1172 (plugin_borrow_comp_cls_by_index_func_t
)
1173 bt_plugin_borrow_source_component_class_by_index_const
,
1174 (spec_comp_cls_borrow_comp_cls_func_t
)
1175 bt_component_class_source_as_component_class
);
1176 cmd_list_plugins_print_component_classes(plugin
, "Filter",
1177 bt_plugin_get_filter_component_class_count(plugin
),
1178 (plugin_borrow_comp_cls_by_index_func_t
)
1179 bt_plugin_borrow_filter_component_class_by_index_const
,
1180 (spec_comp_cls_borrow_comp_cls_func_t
)
1181 bt_component_class_filter_as_component_class
);
1182 cmd_list_plugins_print_component_classes(plugin
, "Sink",
1183 bt_plugin_get_sink_component_class_count(plugin
),
1184 (plugin_borrow_comp_cls_by_index_func_t
)
1185 bt_plugin_borrow_sink_component_class_by_index_const
,
1186 (spec_comp_cls_borrow_comp_cls_func_t
)
1187 bt_component_class_sink_as_component_class
);
1195 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1198 const bt_component_class
*comp_cls
= NULL
;
1199 const bt_value
*results
= NULL
;
1200 bt_value
*params
= NULL
;
1201 const bt_value
*map
= NULL
;
1202 const bt_value
*v
= NULL
;
1203 static const char * const plugin_name
= "ctf";
1204 static const char * const comp_cls_name
= "lttng-live";
1205 static const bt_component_class_type comp_cls_type
=
1206 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1207 int64_t array_size
, i
;
1208 const char *fail_reason
= NULL
;
1209 FILE *out_stream
= stdout
;
1211 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1212 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1215 BT_CLI_LOGE_APPEND_CAUSE(
1216 "Cannot find component class: plugin-name=\"%s\", "
1217 "comp-cls-name=\"%s\", comp-cls-type=%d",
1218 plugin_name
, comp_cls_name
,
1219 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1223 params
= bt_value_map_create();
1228 ret
= bt_value_map_insert_string_entry(params
, "url",
1229 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1234 ret
= query(cfg
, comp_cls
, "sessions", params
,
1235 &results
, &fail_reason
);
1242 if (!bt_value_is_array(results
)) {
1243 BT_CLI_LOGE_APPEND_CAUSE(
1244 "Expecting an array for LTTng live `sessions` query.");
1248 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1250 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1254 BT_LOGE_ERRNO("Cannot open file for writing",
1256 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1257 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1259 "Cannot open file for writing: path=\"%s\"",
1260 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1265 array_size
= bt_value_array_get_size(results
);
1266 for (i
= 0; i
< array_size
; i
++) {
1267 const char *url_text
;
1268 int64_t timer_us
, streams
, clients
;
1270 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1272 BT_CLI_LOGE_APPEND_CAUSE("Unexpected empty array entry.");
1275 if (!bt_value_is_map(map
)) {
1276 BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type.");
1280 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1282 BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry.");
1285 url_text
= bt_value_string_get(v
);
1286 fprintf(out_stream
, "%s", url_text
);
1287 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1289 BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry.");
1292 timer_us
= bt_value_signed_integer_get(v
);
1293 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1294 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1296 BT_CLI_LOGE_APPEND_CAUSE(
1297 "Missing `stream-count` entry.");
1300 streams
= bt_value_signed_integer_get(v
);
1301 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1302 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1304 BT_CLI_LOGE_APPEND_CAUSE(
1305 "Missing `client-count` entry.");
1308 clients
= bt_value_signed_integer_get(v
);
1309 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1315 BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s",
1322 bt_value_put_ref(results
);
1323 bt_value_put_ref(params
);
1324 bt_component_class_put_ref(comp_cls
);
1326 if (out_stream
&& out_stream
!= stdout
) {
1327 int fclose_ret
= fclose(out_stream
);
1330 BT_LOGE_ERRNO("Cannot close file stream",
1332 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1340 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1343 const bt_component_class
*comp_cls
= NULL
;
1344 const bt_value
*results
= NULL
;
1345 bt_value
*params
= NULL
;
1346 const bt_value
*metadata_text_value
= NULL
;
1347 const char *metadata_text
= NULL
;
1348 static const char * const plugin_name
= "ctf";
1349 static const char * const comp_cls_name
= "fs";
1350 static const bt_component_class_type comp_cls_type
=
1351 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1352 const char *fail_reason
= NULL
;
1353 FILE *out_stream
= stdout
;
1355 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1356 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1359 BT_CLI_LOGE_APPEND_CAUSE(
1360 "Cannot find component class: plugin-name=\"%s\", "
1361 "comp-cls-name=\"%s\", comp-cls-type=%d",
1362 plugin_name
, comp_cls_name
,
1363 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1368 params
= bt_value_map_create();
1374 ret
= bt_value_map_insert_string_entry(params
, "path",
1375 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1381 ret
= query(cfg
, comp_cls
, "metadata-info",
1382 params
, &results
, &fail_reason
);
1387 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1389 if (!metadata_text_value
) {
1390 BT_CLI_LOGE_APPEND_CAUSE(
1391 "Cannot find `text` string value in the resulting metadata info object.");
1396 metadata_text
= bt_value_string_get(metadata_text_value
);
1398 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1400 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1403 BT_LOGE_ERRNO("Cannot open file for writing",
1405 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1406 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1408 "Cannot open file for writing: path=\"%s\"",
1409 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1415 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1417 BT_CLI_LOGE_APPEND_CAUSE(
1418 "Cannot write whole metadata text to output stream: "
1428 BT_CLI_LOGE_APPEND_CAUSE(
1429 "Failed to query `metadata-info` object: %s", fail_reason
);
1432 bt_value_put_ref(results
);
1433 bt_value_put_ref(params
);
1434 bt_component_class_put_ref(comp_cls
);
1436 if (out_stream
&& out_stream
!= stdout
) {
1437 int fclose_ret
= fclose(out_stream
);
1440 BT_LOGE_ERRNO("Cannot close file stream",
1442 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1450 char *instance_name
;
1454 struct trace_range
{
1455 uint64_t intersection_range_begin_ns
;
1456 uint64_t intersection_range_end_ns
;
1460 guint
port_id_hash(gconstpointer v
)
1462 const struct port_id
*id
= v
;
1464 BT_ASSERT(id
->instance_name
);
1465 BT_ASSERT(id
->port_name
);
1467 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1471 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1473 const struct port_id
*id1
= v1
;
1474 const struct port_id
*id2
= v2
;
1476 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1477 !strcmp(id1
->port_name
, id2
->port_name
);
1481 void port_id_destroy(gpointer data
)
1483 struct port_id
*id
= data
;
1485 free(id
->instance_name
);
1486 free(id
->port_name
);
1491 void trace_range_destroy(gpointer data
)
1496 struct cmd_run_ctx
{
1498 GHashTable
*src_components
;
1501 GHashTable
*flt_components
;
1504 GHashTable
*sink_components
;
1510 struct bt_config
*cfg
;
1514 bool stream_intersection_mode
;
1517 * Association of struct port_id -> struct trace_range.
1519 GHashTable
*intersections
;
1522 /* Returns a timestamp of the form "(-)s.ns" */
1524 char *s_from_ns(int64_t ns
)
1529 int64_t ts_sec_abs
, ts_nsec_abs
;
1530 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1531 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1533 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1534 is_negative
= false;
1535 ts_sec_abs
= ts_sec
;
1536 ts_nsec_abs
= ts_nsec
;
1537 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1538 is_negative
= false;
1539 ts_sec_abs
= ts_sec
- 1;
1540 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1541 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1543 ts_sec_abs
= ts_sec
;
1544 ts_nsec_abs
= -ts_nsec
;
1545 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1547 ts_sec_abs
= -(ts_sec
+ 1);
1548 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1549 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1551 ts_sec_abs
= -ts_sec
;
1552 ts_nsec_abs
= ts_nsec
;
1553 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1555 ts_sec_abs
= -ts_sec
;
1556 ts_nsec_abs
= -ts_nsec
;
1559 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1560 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1568 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1569 struct cmd_run_ctx
*ctx
,
1570 const bt_component
*upstream_comp
,
1571 const bt_port_output
*out_upstream_port
,
1572 struct bt_config_connection
*cfg_conn
)
1574 typedef uint64_t (*input_port_count_func_t
)(void *);
1575 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1576 const void *, uint64_t);
1577 const bt_port
*upstream_port
=
1578 bt_port_output_as_port_const(out_upstream_port
);
1581 GQuark downstreamp_comp_name_quark
;
1582 void *downstream_comp
;
1583 uint64_t downstream_port_count
;
1585 input_port_count_func_t port_count_fn
;
1586 borrow_input_port_by_index_func_t port_by_index_fn
;
1587 bt_graph_connect_ports_status connect_ports_status
=
1588 BT_GRAPH_CONNECT_PORTS_STATUS_OK
;
1589 bool insert_trimmer
= false;
1590 bt_value
*trimmer_params
= NULL
;
1591 char *intersection_begin
= NULL
;
1592 char *intersection_end
= NULL
;
1593 const bt_component_filter
*trimmer
= NULL
;
1594 const bt_component_class_filter
*trimmer_class
= NULL
;
1595 const bt_port_input
*trimmer_input
= NULL
;
1596 const bt_port_output
*trimmer_output
= NULL
;
1598 if (ctx
->intersections
&&
1599 bt_component_get_class_type(upstream_comp
) ==
1600 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1601 struct trace_range
*range
;
1602 struct port_id port_id
= {
1603 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1604 .port_name
= (char *) bt_port_get_name(upstream_port
)
1607 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1611 range
= (struct trace_range
*) g_hash_table_lookup(
1612 ctx
->intersections
, &port_id
);
1614 bt_value_map_insert_entry_status insert_status
;
1616 intersection_begin
= s_from_ns(
1617 range
->intersection_range_begin_ns
);
1618 intersection_end
= s_from_ns(
1619 range
->intersection_range_end_ns
);
1620 if (!intersection_begin
|| !intersection_end
) {
1621 BT_CLI_LOGE_APPEND_CAUSE(
1622 "Cannot create trimmer argument timestamp string.");
1626 insert_trimmer
= true;
1627 trimmer_params
= bt_value_map_create();
1628 if (!trimmer_params
) {
1632 insert_status
= bt_value_map_insert_string_entry(
1633 trimmer_params
, "begin", intersection_begin
);
1634 if (insert_status
< 0) {
1637 insert_status
= bt_value_map_insert_string_entry(
1639 "end", intersection_end
);
1640 if (insert_status
< 0) {
1645 trimmer_class
= find_filter_component_class("utils", "trimmer");
1646 if (!trimmer_class
) {
1651 BT_LOGI("Connecting upstream port to the next available downstream port: "
1652 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1653 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1654 upstream_port
, bt_port_get_name(upstream_port
),
1655 cfg_conn
->downstream_comp_name
->str
,
1656 cfg_conn
->arg
->str
);
1657 downstreamp_comp_name_quark
= g_quark_from_string(
1658 cfg_conn
->downstream_comp_name
->str
);
1659 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1660 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1661 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1662 port_count_fn
= (input_port_count_func_t
)
1663 bt_component_filter_get_input_port_count
;
1664 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1665 bt_component_filter_borrow_input_port_by_index_const
;
1667 if (!downstream_comp
) {
1668 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1669 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1670 port_count_fn
= (input_port_count_func_t
)
1671 bt_component_sink_get_input_port_count
;
1672 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1673 bt_component_sink_borrow_input_port_by_index_const
;
1676 if (!downstream_comp
) {
1677 BT_CLI_LOGE_APPEND_CAUSE("Cannot find downstream component: "
1678 "comp-name=\"%s\", conn-arg=\"%s\"",
1679 cfg_conn
->downstream_comp_name
->str
,
1680 cfg_conn
->arg
->str
);
1684 downstream_port_count
= port_count_fn(downstream_comp
);
1686 for (i
= 0; i
< downstream_port_count
; i
++) {
1687 const bt_port_input
*in_downstream_port
=
1688 port_by_index_fn(downstream_comp
, i
);
1689 const bt_port
*downstream_port
=
1690 bt_port_input_as_port_const(in_downstream_port
);
1691 const char *upstream_port_name
;
1692 const char *downstream_port_name
;
1694 BT_ASSERT(downstream_port
);
1696 /* Skip port if it's already connected. */
1697 if (bt_port_is_connected(downstream_port
)) {
1698 BT_LOGI("Skipping downstream port: already connected: "
1699 "port-addr=%p, port-name=\"%s\"",
1701 bt_port_get_name(downstream_port
));
1705 downstream_port_name
= bt_port_get_name(downstream_port
);
1706 BT_ASSERT(downstream_port_name
);
1707 upstream_port_name
= bt_port_get_name(upstream_port
);
1708 BT_ASSERT(upstream_port_name
);
1710 if (!bt_common_star_glob_match(
1711 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1712 downstream_port_name
, SIZE_MAX
)) {
1716 if (insert_trimmer
) {
1718 * In order to insert the trimmer between the
1719 * two components that were being connected, we
1720 * create a connection configuration entry which
1721 * describes a connection from the trimmer's
1722 * output to the original input that was being
1725 * Hence, the creation of the trimmer will cause
1726 * the graph "new port" listener to establish
1727 * all downstream connections as its output port
1728 * is connected. We will then establish the
1729 * connection between the original upstream
1730 * source and the trimmer.
1732 char *trimmer_name
= NULL
;
1733 bt_graph_add_component_status add_comp_status
;
1735 ret
= asprintf(&trimmer_name
,
1736 "stream-intersection-trimmer-%s",
1737 upstream_port_name
);
1743 ctx
->connect_ports
= false;
1744 add_comp_status
= bt_graph_add_filter_component(
1745 ctx
->graph
, trimmer_class
, trimmer_name
,
1746 trimmer_params
, ctx
->cfg
->log_level
,
1749 if (add_comp_status
!=
1750 BT_GRAPH_ADD_COMPONENT_STATUS_OK
) {
1756 bt_component_filter_borrow_input_port_by_index_const(
1758 if (!trimmer_input
) {
1762 bt_component_filter_borrow_output_port_by_index_const(
1764 if (!trimmer_output
) {
1769 * Replace the current downstream port by the trimmer's
1772 in_downstream_port
= trimmer_input
;
1774 bt_port_input_as_port_const(in_downstream_port
);
1775 downstream_port_name
= bt_port_get_name(
1777 BT_ASSERT(downstream_port_name
);
1780 /* We have a winner! */
1781 connect_ports_status
= bt_graph_connect_ports(ctx
->graph
,
1782 out_upstream_port
, in_downstream_port
, NULL
);
1783 downstream_port
= NULL
;
1784 switch (connect_ports_status
) {
1785 case BT_GRAPH_CONNECT_PORTS_STATUS_OK
:
1787 case BT_GRAPH_CONNECT_PORTS_STATUS_CANCELED
:
1788 BT_LOGI_STR("Graph was canceled by user.");
1791 BT_CLI_LOGE_APPEND_CAUSE(
1792 "Cannot create connection: graph refuses to connect ports: "
1793 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1794 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1795 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1796 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1798 upstream_comp
, bt_component_get_name(upstream_comp
),
1799 upstream_port
, bt_port_get_name(upstream_port
),
1800 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1801 downstream_port
, downstream_port_name
,
1802 cfg_conn
->arg
->str
);
1806 BT_LOGI("Connected component ports: "
1807 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1808 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1809 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1810 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1812 upstream_comp
, bt_component_get_name(upstream_comp
),
1813 upstream_port
, bt_port_get_name(upstream_port
),
1814 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1815 downstream_port
, downstream_port_name
,
1816 cfg_conn
->arg
->str
);
1818 if (insert_trimmer
) {
1820 * The first connection, from the source to the trimmer,
1821 * has been done. We now connect the trimmer to the
1822 * original downstream port.
1824 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1826 bt_component_filter_as_component_const(trimmer
),
1827 trimmer_output
, cfg_conn
);
1831 ctx
->connect_ports
= true;
1835 * We found a matching downstream port: the search is
1841 /* No downstream port found */
1842 BT_CLI_LOGE_APPEND_CAUSE(
1843 "Cannot create connection: cannot find a matching downstream port for upstream port: "
1844 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1845 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1846 upstream_port
, bt_port_get_name(upstream_port
),
1847 cfg_conn
->downstream_comp_name
->str
,
1848 cfg_conn
->arg
->str
);
1854 free(intersection_begin
);
1855 free(intersection_end
);
1856 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1857 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1858 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1863 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1864 const bt_port_output
*upstream_port
)
1867 const char *upstream_port_name
;
1868 const char *upstream_comp_name
;
1869 const bt_component
*upstream_comp
= NULL
;
1873 BT_ASSERT(upstream_port
);
1874 upstream_port_name
= bt_port_get_name(
1875 bt_port_output_as_port_const(upstream_port
));
1876 BT_ASSERT(upstream_port_name
);
1877 upstream_comp
= bt_port_borrow_component_const(
1878 bt_port_output_as_port_const(upstream_port
));
1879 BT_ASSERT(upstream_comp
);
1880 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1881 BT_ASSERT(upstream_comp_name
);
1882 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1883 "port-addr=%p, port-name=\"%s\"",
1884 upstream_comp
, upstream_comp_name
,
1885 upstream_port
, upstream_port_name
);
1887 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1888 struct bt_config_connection
*cfg_conn
=
1890 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1892 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1893 upstream_comp_name
)) {
1897 if (!bt_common_star_glob_match(
1898 cfg_conn
->upstream_port_glob
->str
,
1899 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1903 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1904 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1906 BT_CLI_LOGE_APPEND_CAUSE(
1907 "Cannot connect upstream port: "
1908 "port-addr=%p, port-name=\"%s\"",
1910 upstream_port_name
);
1916 BT_CLI_LOGE_APPEND_CAUSE(
1917 "Cannot connect upstream port: port does not match any connection argument: "
1918 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1919 upstream_port_name
);
1929 bt_graph_listener_func_status
1930 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1931 const bt_port_output
*out_port
)
1933 const bt_component
*comp
;
1934 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1935 bt_graph_listener_func_status ret
=
1936 BT_GRAPH_LISTENER_FUNC_STATUS_OK
;
1938 comp
= bt_port_borrow_component_const(port
);
1939 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1940 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1941 comp
, comp
? bt_component_get_name(comp
) : "",
1942 port
, bt_port_get_name(port
));
1944 if (!ctx
->connect_ports
) {
1950 if (bt_port_is_connected(port
)) {
1951 BT_LOGW_STR("Port is already connected.");
1955 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
1956 BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port.");
1957 ret
= BT_GRAPH_LISTENER_FUNC_STATUS_ERROR
;
1966 bt_graph_listener_func_status
graph_source_output_port_added_listener(
1967 const bt_component_source
*component
,
1968 const bt_port_output
*port
, void *data
)
1970 return graph_output_port_added_listener(data
, port
);
1974 bt_graph_listener_func_status
graph_filter_output_port_added_listener(
1975 const bt_component_filter
*component
,
1976 const bt_port_output
*port
, void *data
)
1978 return graph_output_port_added_listener(data
, port
);
1982 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1988 if (ctx
->src_components
) {
1989 g_hash_table_destroy(ctx
->src_components
);
1990 ctx
->src_components
= NULL
;
1993 if (ctx
->flt_components
) {
1994 g_hash_table_destroy(ctx
->flt_components
);
1995 ctx
->flt_components
= NULL
;
1998 if (ctx
->sink_components
) {
1999 g_hash_table_destroy(ctx
->sink_components
);
2000 ctx
->sink_components
= NULL
;
2003 if (ctx
->intersections
) {
2004 g_hash_table_destroy(ctx
->intersections
);
2005 ctx
->intersections
= NULL
;
2008 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
2014 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2017 bt_graph_add_listener_status add_listener_status
;
2020 ctx
->connect_ports
= false;
2021 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
2022 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2023 if (!ctx
->src_components
) {
2027 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
2028 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2029 if (!ctx
->flt_components
) {
2033 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
2034 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2035 if (!ctx
->sink_components
) {
2039 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2040 ctx
->stream_intersection_mode
= true;
2041 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2042 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2043 if (!ctx
->intersections
) {
2048 ctx
->graph
= bt_graph_create();
2053 the_graph
= ctx
->graph
;
2054 add_listener_status
= bt_graph_add_source_component_output_port_added_listener(
2055 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
2057 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
2058 BT_CLI_LOGE_APPEND_CAUSE(
2059 "Cannot add \"port added\" listener to graph.");
2063 add_listener_status
= bt_graph_add_filter_component_output_port_added_listener(
2064 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
2066 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
2067 BT_CLI_LOGE_APPEND_CAUSE(
2068 "Cannot add \"port added\" listener to graph.");
2075 cmd_run_ctx_destroy(ctx
);
2083 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2084 struct bt_config_component
*cfg_comp
,
2085 const bt_component_class_source
*src_comp_cls
)
2089 int64_t trace_count
;
2090 const char *path
= NULL
;
2091 const bt_value
*query_result
= NULL
;
2092 const bt_value
*trace_info
= NULL
;
2093 const bt_value
*intersection_range
= NULL
;
2094 const bt_value
*intersection_begin
= NULL
;
2095 const bt_value
*intersection_end
= NULL
;
2096 const bt_value
*stream_infos
= NULL
;
2097 const bt_value
*stream_info
= NULL
;
2098 struct port_id
*port_id
= NULL
;
2099 struct trace_range
*trace_range
= NULL
;
2100 const char *fail_reason
= NULL
;
2101 const bt_component_class
*comp_cls
=
2102 bt_component_class_source_as_component_class_const(src_comp_cls
);
2104 ret
= query(ctx
->cfg
, comp_cls
, "trace-info",
2105 cfg_comp
->params
, &query_result
,
2108 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2109 "comp-class-name=\"%s\"", fail_reason
,
2110 bt_component_class_get_name(comp_cls
));
2115 BT_ASSERT(query_result
);
2117 if (!bt_value_is_array(query_result
)) {
2118 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2119 "component-class-name=%s",
2120 bt_component_class_get_name(comp_cls
));
2125 trace_count
= bt_value_array_get_size(query_result
);
2126 if (trace_count
< 0) {
2131 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2133 uint64_t stream_idx
;
2134 int64_t stream_count
;
2136 trace_info
= bt_value_array_borrow_element_by_index_const(
2137 query_result
, trace_idx
);
2138 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2140 BT_LOGD_STR("Cannot retrieve trace from query result.");
2144 intersection_range
= bt_value_map_borrow_entry_value_const(
2145 trace_info
, "intersection-range-ns");
2146 if (!intersection_range
) {
2148 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2152 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
2154 if (!intersection_begin
) {
2156 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2160 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
2162 if (!intersection_end
) {
2164 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2168 begin
= bt_value_signed_integer_get(intersection_begin
);
2169 end
= bt_value_signed_integer_get(intersection_end
);
2171 if (begin
< 0 || end
< 0 || end
< begin
) {
2172 BT_CLI_LOGE_APPEND_CAUSE(
2173 "Invalid trace stream intersection values: "
2174 "intersection-range-ns:begin=%" PRId64
2175 ", intersection-range-ns:end=%" PRId64
,
2181 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
2183 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2185 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2189 stream_count
= bt_value_array_get_size(stream_infos
);
2190 if (stream_count
< 0) {
2195 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2196 const bt_value
*port_name
;
2198 port_id
= g_new0(struct port_id
, 1);
2201 BT_CLI_LOGE_APPEND_CAUSE(
2202 "Cannot allocate memory for port_id structure.");
2205 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2206 if (!port_id
->instance_name
) {
2208 BT_CLI_LOGE_APPEND_CAUSE(
2209 "Cannot allocate memory for port_id component instance name.");
2213 trace_range
= g_new0(struct trace_range
, 1);
2216 BT_CLI_LOGE_APPEND_CAUSE(
2217 "Cannot allocate memory for trace_range structure.");
2220 trace_range
->intersection_range_begin_ns
= begin
;
2221 trace_range
->intersection_range_end_ns
= end
;
2223 stream_info
= bt_value_array_borrow_element_by_index_const(
2224 stream_infos
, stream_idx
);
2225 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2227 BT_CLI_LOGE_APPEND_CAUSE(
2228 "Cannot retrieve stream informations from trace in query result.");
2232 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2233 if (!port_name
|| !bt_value_is_string(port_name
)) {
2235 BT_CLI_LOGE_APPEND_CAUSE(
2236 "Cannot retrieve port name in query result.");
2240 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2241 if (!port_id
->port_name
) {
2243 BT_CLI_LOGE_APPEND_CAUSE(
2244 "Cannot allocate memory for port_id port_name.");
2248 BT_LOGD("Inserting stream intersection ");
2250 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2260 BT_CLI_LOGE_APPEND_CAUSE(
2261 "Cannot determine stream intersection of trace: path=\"%s\"",
2262 path
? path
: "(unknown)");
2265 bt_value_put_ref(query_result
);
2267 g_free(trace_range
);
2272 int cmd_run_ctx_create_components_from_config_components(
2273 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2276 const void *comp_cls
= NULL
;
2277 const void *comp
= NULL
;
2280 for (i
= 0; i
< cfg_components
->len
; i
++) {
2281 struct bt_config_component
*cfg_comp
=
2282 g_ptr_array_index(cfg_components
, i
);
2285 switch (cfg_comp
->type
) {
2286 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2287 comp_cls
= find_source_component_class(
2288 cfg_comp
->plugin_name
->str
,
2289 cfg_comp
->comp_cls_name
->str
);
2291 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2292 comp_cls
= find_filter_component_class(
2293 cfg_comp
->plugin_name
->str
,
2294 cfg_comp
->comp_cls_name
->str
);
2296 case BT_COMPONENT_CLASS_TYPE_SINK
:
2297 comp_cls
= find_sink_component_class(
2298 cfg_comp
->plugin_name
->str
,
2299 cfg_comp
->comp_cls_name
->str
);
2306 BT_CLI_LOGE_APPEND_CAUSE(
2307 "Cannot find component class: plugin-name=\"%s\", "
2308 "comp-cls-name=\"%s\", comp-cls-type=%d",
2309 cfg_comp
->plugin_name
->str
,
2310 cfg_comp
->comp_cls_name
->str
,
2315 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_TRACE
);
2317 switch (cfg_comp
->type
) {
2318 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2319 ret
= bt_graph_add_source_component(ctx
->graph
,
2320 comp_cls
, cfg_comp
->instance_name
->str
,
2321 cfg_comp
->params
, cfg_comp
->log_level
,
2324 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2325 ret
= bt_graph_add_filter_component(ctx
->graph
,
2326 comp_cls
, cfg_comp
->instance_name
->str
,
2327 cfg_comp
->params
, cfg_comp
->log_level
,
2330 case BT_COMPONENT_CLASS_TYPE_SINK
:
2331 ret
= bt_graph_add_sink_component(ctx
->graph
,
2332 comp_cls
, cfg_comp
->instance_name
->str
,
2333 cfg_comp
->params
, cfg_comp
->log_level
,
2341 BT_CLI_LOGE_APPEND_CAUSE(
2342 "Cannot create component: plugin-name=\"%s\", "
2343 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2345 cfg_comp
->plugin_name
->str
,
2346 cfg_comp
->comp_cls_name
->str
,
2347 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2351 if (ctx
->stream_intersection_mode
&&
2352 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2353 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2359 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2360 comp
, cfg_comp
->instance_name
->str
);
2361 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2362 BT_ASSERT(quark
> 0);
2364 switch (cfg_comp
->type
) {
2365 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2366 g_hash_table_insert(ctx
->src_components
,
2367 GUINT_TO_POINTER(quark
), (void *) comp
);
2369 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2370 g_hash_table_insert(ctx
->flt_components
,
2371 GUINT_TO_POINTER(quark
), (void *) comp
);
2373 case BT_COMPONENT_CLASS_TYPE_SINK
:
2374 g_hash_table_insert(ctx
->sink_components
,
2375 GUINT_TO_POINTER(quark
), (void *) comp
);
2382 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2391 bt_object_put_ref(comp
);
2392 bt_object_put_ref(comp_cls
);
2397 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2402 * Make sure that, during this phase, our graph's "port added"
2403 * listener does not connect ports while we are creating the
2404 * components because we have a special, initial phase for
2407 ctx
->connect_ports
= false;
2409 ret
= cmd_run_ctx_create_components_from_config_components(
2410 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2416 ret
= cmd_run_ctx_create_components_from_config_components(
2417 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2423 ret
= cmd_run_ctx_create_components_from_config_components(
2424 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2434 typedef uint64_t (*output_port_count_func_t
)(const void *);
2435 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2436 const void *, uint64_t);
2439 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2440 void *comp
, output_port_count_func_t port_count_fn
,
2441 borrow_output_port_by_index_func_t port_by_index_fn
)
2447 count
= port_count_fn(comp
);
2449 for (i
= 0; i
< count
; i
++) {
2450 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2452 BT_ASSERT(upstream_port
);
2453 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2464 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2467 GHashTableIter iter
;
2468 gpointer g_name_quark
, g_comp
;
2470 ctx
->connect_ports
= true;
2471 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2473 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2474 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2475 (output_port_count_func_t
)
2476 bt_component_source_get_output_port_count
,
2477 (borrow_output_port_by_index_func_t
)
2478 bt_component_source_borrow_output_port_by_index_const
);
2484 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2486 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2487 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2488 (output_port_count_func_t
)
2489 bt_component_filter_get_output_port_count
,
2490 (borrow_output_port_by_index_func_t
)
2491 bt_component_filter_borrow_output_port_by_index_const
);
2502 int cmd_run(struct bt_config
*cfg
)
2505 struct cmd_run_ctx ctx
= { 0 };
2507 /* Initialize the command's context and the graph object */
2508 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2509 BT_CLI_LOGE_APPEND_CAUSE(
2510 "Cannot initialize the command's context.");
2515 BT_LOGI_STR("Canceled by user before creating components.");
2519 BT_LOGI_STR("Creating components.");
2521 /* Create the requested component instances */
2522 if (cmd_run_ctx_create_components(&ctx
)) {
2523 BT_CLI_LOGE_APPEND_CAUSE("Cannot create components.");
2528 BT_LOGI_STR("Canceled by user before connecting components.");
2532 BT_LOGI_STR("Connecting components.");
2534 /* Connect the initially visible component ports */
2535 if (cmd_run_ctx_connect_ports(&ctx
)) {
2536 BT_CLI_LOGE_APPEND_CAUSE(
2537 "Cannot connect initial component ports.");
2542 BT_LOGI_STR("Canceled by user before running the graph.");
2546 BT_LOGI_STR("Running the graph.");
2550 bt_graph_run_status run_status
= bt_graph_run(ctx
.graph
);
2553 * Reset console in case something messed with console
2554 * codes during the graph's execution.
2556 printf("%s", bt_common_color_reset());
2558 fprintf(stderr
, "%s", bt_common_color_reset());
2559 BT_LOGT("bt_graph_run() returned: status=%s",
2560 bt_common_func_status_string(run_status
));
2562 switch (run_status
) {
2563 case BT_GRAPH_RUN_STATUS_OK
:
2565 case BT_GRAPH_RUN_STATUS_CANCELED
:
2566 BT_LOGI_STR("Graph was canceled by user.");
2568 case BT_GRAPH_RUN_STATUS_AGAIN
:
2569 if (bt_graph_is_canceled(ctx
.graph
)) {
2570 BT_LOGI_STR("Graph was canceled by user.");
2574 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2575 BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: "
2577 cfg
->cmd_data
.run
.retry_duration_us
);
2579 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2580 if (bt_graph_is_canceled(ctx
.graph
)) {
2581 BT_LOGI_STR("Graph was canceled by user.");
2587 case BT_GRAPH_RUN_STATUS_END
:
2590 BT_CLI_LOGE_APPEND_CAUSE(
2591 "Graph failed to complete successfully");
2604 cmd_run_ctx_destroy(&ctx
);
2609 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2611 const char *env_clash
;
2613 if (!cfg
->command_name
) {
2617 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2618 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2622 if (g_file_test(cfg
->command_name
,
2623 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2624 _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION
, __FILE__
, __LINE__
,
2625 BT_LOG_WARN
, BT_LOG_TAG
,
2626 "The `%s` command was executed. "
2627 "If you meant to convert a trace located in "
2628 "the local `%s` directory, please use:\n\n"
2629 " babeltrace2 convert %s [OPTIONS]",
2630 cfg
->command_name
, cfg
->command_name
,
2636 void init_log_level(void)
2638 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2642 void set_auto_log_levels(struct bt_config
*cfg
)
2644 const char **env_var_name
;
2647 * Override the configuration's default log level if
2648 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2649 * are found for backward compatibility with legacy Babetrace 1.
2651 if (getenv("BABELTRACE_DEBUG") &&
2652 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2653 cfg
->log_level
= BT_LOG_TRACE
;
2654 } else if (getenv("BABELTRACE_VERBOSE") &&
2655 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2656 cfg
->log_level
= BT_LOG_INFO
;
2660 * Set log levels according to --debug or --verbose. For
2661 * backward compatibility, --debug is more verbose than
2664 * --verbose: INFO log level
2665 * --debug: TRACE log level (includes DEBUG, which is
2666 * is less verbose than TRACE in the internal
2667 * logging framework)
2669 if (!getenv("LIBBABELTRACE2_INIT_LOG_LEVEL")) {
2671 bt_logging_set_global_level(BT_LOG_INFO
);
2672 } else if (cfg
->debug
) {
2673 bt_logging_set_global_level(BT_LOG_TRACE
);
2676 * Set library's default log level if not
2677 * explicitly specified.
2679 bt_logging_set_global_level(cfg
->log_level
);
2683 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2685 bt_cli_log_level
= BT_LOG_INFO
;
2686 } else if (cfg
->debug
) {
2687 bt_cli_log_level
= BT_LOG_TRACE
;
2690 * Set CLI's default log level if not explicitly
2693 bt_cli_log_level
= cfg
->log_level
;
2697 env_var_name
= log_level_env_var_names
;
2699 while (*env_var_name
) {
2700 if (!getenv(*env_var_name
)) {
2702 g_setenv(*env_var_name
, "INFO", 1);
2703 } else if (cfg
->debug
) {
2704 g_setenv(*env_var_name
, "TRACE", 1);
2706 char val
[2] = { 0 };
2709 * Set module's default log level if not
2710 * explicitly specified.
2712 val
[0] = bt_log_get_letter_from_level(
2714 g_setenv(*env_var_name
, val
, 1);
2722 int main(int argc
, const char **argv
)
2726 struct bt_config
*cfg
;
2729 set_signal_handler();
2731 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2734 /* Quit without errors; typically usage/version */
2736 BT_LOGI_STR("Quitting without errors.");
2741 BT_CLI_LOGE_APPEND_CAUSE(
2742 "Command-line error: retcode=%d", retcode
);
2747 BT_CLI_LOGE_APPEND_CAUSE(
2748 "Failed to create a valid Babeltrace configuration.");
2753 set_auto_log_levels(cfg
);
2756 if (cfg
->command_needs_plugins
) {
2757 ret
= load_all_plugins(cfg
->plugin_paths
);
2759 BT_CLI_LOGE_APPEND_CAUSE(
2760 "Failed to load plugins: ret=%d", ret
);
2766 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2767 cfg
->command
, cfg
->command_name
);
2769 switch (cfg
->command
) {
2770 case BT_CONFIG_COMMAND_RUN
:
2773 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2774 ret
= cmd_list_plugins(cfg
);
2776 case BT_CONFIG_COMMAND_HELP
:
2777 ret
= cmd_help(cfg
);
2779 case BT_CONFIG_COMMAND_QUERY
:
2780 ret
= cmd_query(cfg
);
2782 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2783 ret
= cmd_print_ctf_metadata(cfg
);
2785 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2786 ret
= cmd_print_lttng_live_sessions(cfg
);
2789 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2793 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2794 cfg
->command
, cfg
->command_name
, ret
);
2795 warn_command_name_and_directory_clash(cfg
);
2796 retcode
= ret
? 1 : 0;
2799 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2803 * Clear current thread's error in case there is one to avoid a
2806 bt_current_thread_clear_error();