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 <babeltrace/babeltrace.h>
29 #include <babeltrace/common-internal.h>
39 #include "babeltrace-cfg.h"
40 #include "babeltrace-cfg-cli-args.h"
41 #include "babeltrace-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_COMMON_LOG_LEVEL",
53 "BABELTRACE_COMPAT_LOG_LEVEL",
54 "BABELTRACE_PLUGIN_CTF_BTR_LOG_LEVEL",
55 "BABELTRACE_SINK_CTF_FS_LOG_LEVEL",
56 "BABELTRACE_SRC_CTF_FS_LOG_LEVEL",
57 "BABELTRACE_SRC_CTF_LTTNG_LIVE_LOG_LEVEL",
58 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
59 "BABELTRACE_PLUGIN_CTF_NOTIF_ITER_LOG_LEVEL",
60 "BABELTRACE_PLUGIN_CTFCOPYTRACE_LIB_LOG_LEVEL",
61 "BABELTRACE_FLT_LTTNG_UTILS_DEBUG_INFO_LOG_LEVEL",
62 "BABELTRACE_SRC_TEXT_DMESG_LOG_LEVEL",
63 "BABELTRACE_SINK_TEXT_PRETTY_LOG_LEVEL",
64 "BABELTRACE_FLT_UTILS_MUXER_LOG_LEVEL",
65 "BABELTRACE_FLT_UTILS_TRIMMER_LOG_LEVEL",
66 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
67 "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
71 /* Application's processing graph (weak) */
72 static struct bt_private_graph
*the_graph
;
73 static struct bt_private_query_executor
*the_query_executor
;
74 static bool canceled
= false;
76 GPtrArray
*loaded_plugins
;
83 BOOL WINAPI
signal_handler(DWORD signal
) {
85 bt_private_graph_cancel(the_graph
);
94 void set_signal_handler(void)
96 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
97 BT_LOGE("Failed to set the ctrl+c handler.");
101 #else /* __MINGW32__ */
104 void signal_handler(int signum
)
106 if (signum
!= SIGINT
) {
111 bt_private_graph_cancel(the_graph
);
114 if (the_query_executor
) {
115 bt_private_query_executor_cancel(the_query_executor
);
122 void set_signal_handler(void)
124 struct sigaction new_action
, old_action
;
126 new_action
.sa_handler
= signal_handler
;
127 sigemptyset(&new_action
.sa_mask
);
128 new_action
.sa_flags
= 0;
129 sigaction(SIGINT
, NULL
, &old_action
);
131 if (old_action
.sa_handler
!= SIG_IGN
) {
132 sigaction(SIGINT
, &new_action
, NULL
);
136 #endif /* __MINGW32__ */
139 void init_static_data(void)
141 loaded_plugins
= g_ptr_array_new_with_free_func(
142 (GDestroyNotify
) bt_object_put_ref
);
146 void fini_static_data(void)
148 g_ptr_array_free(loaded_plugins
, TRUE
);
152 int create_the_query_executor(void)
156 the_query_executor
= bt_private_query_executor_create();
157 if (!the_query_executor
) {
158 BT_LOGE_STR("Cannot create a query executor.");
166 void destroy_the_query_executor(void)
168 BT_OBJECT_PUT_REF_AND_RESET(the_query_executor
);
172 int query(struct bt_component_class
*comp_cls
, const char *obj
,
173 const struct bt_value
*params
, const struct bt_value
**user_result
,
174 const char **fail_reason
)
176 const struct bt_value
*result
= NULL
;
177 enum bt_query_executor_status status
;
178 *fail_reason
= "unknown error";
181 BT_ASSERT(fail_reason
);
182 BT_ASSERT(user_result
);
183 ret
= create_the_query_executor();
185 /* create_the_query_executor() logs errors */
190 BT_LOGI("Canceled by user before executing the query: "
191 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
192 "query-obj=\"%s\"", comp_cls
,
193 bt_component_class_get_name(comp_cls
), obj
);
194 *fail_reason
= "canceled by user";
199 status
= bt_private_query_executor_query(the_query_executor
,
200 comp_cls
, obj
, params
, &result
);
202 case BT_QUERY_EXECUTOR_STATUS_OK
:
204 case BT_QUERY_EXECUTOR_STATUS_AGAIN
:
206 const uint64_t sleep_time_us
= 100000;
208 /* Wait 100 ms and retry */
209 BT_LOGV("Got BT_QUERY_EXECUTOR_STATUS_AGAIN: sleeping: "
210 "time-us=%" PRIu64
, sleep_time_us
);
212 if (usleep(sleep_time_us
)) {
213 if (bt_query_executor_is_canceled(
214 bt_private_query_executor_as_query_executor(the_query_executor
))) {
215 BT_LOGI("Query was canceled by user: "
216 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
217 "query-obj=\"%s\"", comp_cls
,
218 bt_component_class_get_name(comp_cls
),
220 *fail_reason
= "canceled by user";
227 case BT_QUERY_EXECUTOR_STATUS_CANCELED
:
228 *fail_reason
= "canceled by user";
230 case BT_QUERY_EXECUTOR_STATUS_ERROR
:
232 case BT_QUERY_EXECUTOR_STATUS_INVALID_OBJECT
:
233 *fail_reason
= "invalid or unknown query object";
235 case BT_QUERY_EXECUTOR_STATUS_INVALID_PARAMS
:
236 *fail_reason
= "invalid query parameters";
238 case BT_QUERY_EXECUTOR_STATUS_UNSUPPORTED
:
239 *fail_reason
= "unsupported action";
241 case BT_QUERY_EXECUTOR_STATUS_NOMEM
:
242 *fail_reason
= "not enough memory";
245 BT_LOGF("Unknown query status: status=%d", status
);
251 *user_result
= result
;
259 destroy_the_query_executor();
260 bt_object_put_ref(result
);
265 const struct bt_plugin
*find_plugin(const char *name
)
268 const struct bt_plugin
*plugin
= NULL
;
271 BT_LOGD("Finding plugin: name=\"%s\"", name
);
273 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
274 plugin
= g_ptr_array_index(loaded_plugins
, i
);
276 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
283 if (BT_LOG_ON_DEBUG
) {
285 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
287 BT_LOGD("Cannot find plugin.");
291 bt_object_get_ref(plugin
);
295 typedef void *(*plugin_borrow_comp_cls_func_t
)(const struct bt_plugin
*,
299 void *find_component_class_from_plugin(const char *plugin_name
,
300 const char *comp_class_name
,
301 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
303 void *comp_class
= NULL
;
304 const struct bt_plugin
*plugin
;
306 BT_LOGD("Finding component class: plugin-name=\"%s\", "
307 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
309 plugin
= find_plugin(plugin_name
);
314 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
315 bt_object_get_ref(comp_class
);
316 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
319 if (BT_LOG_ON_DEBUG
) {
321 BT_LOGD("Found component class: comp-cls-addr=%p",
324 BT_LOGD("Cannot find source component class.");
332 struct bt_component_class_source
*find_source_component_class(
333 const char *plugin_name
, const char *comp_class_name
)
335 return (void *) find_component_class_from_plugin(plugin_name
,
337 (plugin_borrow_comp_cls_func_t
)
338 bt_plugin_borrow_source_component_class_by_name_const
);
342 struct bt_component_class_filter
*find_filter_component_class(
343 const char *plugin_name
, const char *comp_class_name
)
345 return (void *) find_component_class_from_plugin(plugin_name
,
347 (plugin_borrow_comp_cls_func_t
)
348 bt_plugin_borrow_filter_component_class_by_name_const
);
352 struct bt_component_class_sink
*find_sink_component_class(
353 const char *plugin_name
, const char *comp_class_name
)
355 return (void *) find_component_class_from_plugin(plugin_name
,
357 (plugin_borrow_comp_cls_func_t
)
358 bt_plugin_borrow_sink_component_class_by_name_const
);
362 struct bt_component_class
*find_component_class(const char *plugin_name
,
363 const char *comp_class_name
,
364 enum bt_component_class_type comp_class_type
)
366 struct bt_component_class
*comp_cls
= NULL
;
368 switch (comp_class_type
) {
369 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
370 comp_cls
= bt_component_class_source_as_component_class(
371 find_source_component_class(plugin_name
,
374 case BT_COMPONENT_CLASS_TYPE_FILTER
:
375 comp_cls
= bt_component_class_filter_as_component_class(
376 find_filter_component_class(plugin_name
,
379 case BT_COMPONENT_CLASS_TYPE_SINK
:
380 comp_cls
= bt_component_class_sink_as_component_class(
381 find_sink_component_class(plugin_name
,
392 void print_indent(FILE *fp
, size_t indent
)
396 for (i
= 0; i
< indent
; i
++) {
402 const char *component_type_str(enum bt_component_class_type type
)
405 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
407 case BT_COMPONENT_CLASS_TYPE_SINK
:
409 case BT_COMPONENT_CLASS_TYPE_FILTER
:
417 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
418 const char *comp_cls_name
, enum bt_component_class_type type
)
420 GString
*shell_plugin_name
= NULL
;
421 GString
*shell_comp_cls_name
= NULL
;
423 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
424 if (!shell_plugin_name
) {
428 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
429 if (!shell_comp_cls_name
) {
433 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
434 bt_common_color_bold(),
435 bt_common_color_fg_cyan(),
436 component_type_str(type
),
437 bt_common_color_fg_default(),
438 bt_common_color_fg_blue(),
439 shell_plugin_name
->str
,
440 bt_common_color_fg_default(),
441 bt_common_color_fg_yellow(),
442 shell_comp_cls_name
->str
,
443 bt_common_color_reset());
446 if (shell_plugin_name
) {
447 g_string_free(shell_plugin_name
, TRUE
);
450 if (shell_comp_cls_name
) {
451 g_string_free(shell_comp_cls_name
, TRUE
);
456 void print_value(FILE *, const struct bt_value
*, size_t);
459 void print_value_rec(FILE *, const struct bt_value
*, size_t);
461 struct print_map_value_data
{
467 bt_bool
print_map_value(const char *key
, const struct bt_value
*object
,
470 struct print_map_value_data
*print_map_value_data
= data
;
472 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
473 fprintf(print_map_value_data
->fp
, "%s: ", key
);
476 if (bt_value_is_array(object
) &&
477 bt_value_array_is_empty(object
)) {
478 fprintf(print_map_value_data
->fp
, "[ ]\n");
482 if (bt_value_is_map(object
) &&
483 bt_value_map_is_empty(object
)) {
484 fprintf(print_map_value_data
->fp
, "{ }\n");
488 if (bt_value_is_array(object
) ||
489 bt_value_is_map(object
)) {
490 fprintf(print_map_value_data
->fp
, "\n");
493 print_value_rec(print_map_value_data
->fp
, object
,
494 print_map_value_data
->indent
+ 2);
499 void print_value_rec(FILE *fp
, const struct bt_value
*value
, size_t indent
)
512 switch (bt_value_get_type(value
)) {
513 case BT_VALUE_TYPE_NULL
:
514 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
515 bt_common_color_reset());
517 case BT_VALUE_TYPE_BOOL
:
518 bool_val
= bt_value_bool_get(value
);
519 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
520 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
521 bt_common_color_reset());
523 case BT_VALUE_TYPE_INTEGER
:
524 int_val
= bt_value_integer_get(value
);
525 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
526 bt_common_color_fg_red(), int_val
,
527 bt_common_color_reset());
529 case BT_VALUE_TYPE_REAL
:
530 dbl_val
= bt_value_real_get(value
);
531 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
532 bt_common_color_fg_red(), dbl_val
,
533 bt_common_color_reset());
535 case BT_VALUE_TYPE_STRING
:
536 str_val
= bt_value_string_get(value
);
537 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
538 bt_common_color_fg_green(), str_val
,
539 bt_common_color_reset());
541 case BT_VALUE_TYPE_ARRAY
:
542 size
= bt_value_array_get_size(value
);
548 print_indent(fp
, indent
);
549 fprintf(fp
, "[ ]\n");
553 for (i
= 0; i
< size
; i
++) {
554 const struct bt_value
*element
=
555 bt_value_array_borrow_element_by_index_const(
561 print_indent(fp
, indent
);
564 if (bt_value_is_array(element
) &&
565 bt_value_array_is_empty(element
)) {
566 fprintf(fp
, "[ ]\n");
570 if (bt_value_is_map(element
) &&
571 bt_value_map_is_empty(element
)) {
572 fprintf(fp
, "{ }\n");
576 if (bt_value_is_array(element
) ||
577 bt_value_is_map(element
)) {
581 print_value_rec(fp
, element
, indent
+ 2);
584 case BT_VALUE_TYPE_MAP
:
586 struct print_map_value_data data
= {
591 if (bt_value_map_is_empty(value
)) {
592 print_indent(fp
, indent
);
593 fprintf(fp
, "{ }\n");
597 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
606 BT_LOGE("Error printing value of type %s.",
607 bt_common_value_type_string(bt_value_get_type(value
)));
611 void print_value(FILE *fp
, const struct bt_value
*value
, size_t indent
)
613 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
614 print_indent(fp
, indent
);
617 print_value_rec(fp
, value
, indent
);
621 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
623 fprintf(stderr
, " ");
624 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
625 bt_config_component
->comp_cls_name
->str
,
626 bt_config_component
->type
);
627 fprintf(stderr
, ":\n");
629 if (bt_config_component
->instance_name
->len
> 0) {
630 fprintf(stderr
, " Name: %s\n",
631 bt_config_component
->instance_name
->str
);
634 fprintf(stderr
, " Parameters:\n");
635 print_value(stderr
, bt_config_component
->params
, 8);
639 void print_bt_config_components(GPtrArray
*array
)
643 for (i
= 0; i
< array
->len
; i
++) {
644 struct bt_config_component
*cfg_component
=
645 bt_config_get_component(array
, i
);
646 print_bt_config_component(cfg_component
);
647 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
652 void print_plugin_paths(const struct bt_value
*plugin_paths
)
654 fprintf(stderr
, " Plugin paths:\n");
655 print_value(stderr
, plugin_paths
, 4);
659 void print_cfg_run(struct bt_config
*cfg
)
663 print_plugin_paths(cfg
->plugin_paths
);
664 fprintf(stderr
, " Source component instances:\n");
665 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
667 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
668 fprintf(stderr
, " Filter component instances:\n");
669 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
672 fprintf(stderr
, " Sink component instances:\n");
673 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
674 fprintf(stderr
, " Connections:\n");
676 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
677 struct bt_config_connection
*cfg_connection
=
678 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
681 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
682 cfg_connection
->upstream_comp_name
->str
,
683 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
684 cfg_connection
->upstream_port_glob
->str
,
685 cfg_connection
->downstream_comp_name
->str
,
686 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
687 cfg_connection
->downstream_port_glob
->str
);
692 void print_cfg_list_plugins(struct bt_config
*cfg
)
694 print_plugin_paths(cfg
->plugin_paths
);
698 void print_cfg_help(struct bt_config
*cfg
)
700 print_plugin_paths(cfg
->plugin_paths
);
704 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
706 print_plugin_paths(cfg
->plugin_paths
);
707 fprintf(stderr
, " Path: %s\n",
708 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
712 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
714 print_plugin_paths(cfg
->plugin_paths
);
715 fprintf(stderr
, " URL: %s\n",
716 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
720 void print_cfg_query(struct bt_config
*cfg
)
722 print_plugin_paths(cfg
->plugin_paths
);
723 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
724 fprintf(stderr
, " Component class:\n");
725 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
729 void print_cfg(struct bt_config
*cfg
)
731 if (!BT_LOG_ON_INFO
) {
735 BT_LOGI_STR("Configuration:");
736 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
737 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
739 switch (cfg
->command
) {
740 case BT_CONFIG_COMMAND_RUN
:
743 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
744 print_cfg_list_plugins(cfg
);
746 case BT_CONFIG_COMMAND_HELP
:
749 case BT_CONFIG_COMMAND_QUERY
:
750 print_cfg_query(cfg
);
752 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
753 print_cfg_print_ctf_metadata(cfg
);
755 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
756 print_cfg_print_lttng_live_sessions(cfg
);
764 void add_to_loaded_plugins(const struct bt_plugin_set
*plugin_set
)
769 count
= bt_plugin_set_get_plugin_count(plugin_set
);
770 BT_ASSERT(count
>= 0);
772 for (i
= 0; i
< count
; i
++) {
773 const struct bt_plugin
*plugin
=
774 bt_plugin_set_borrow_plugin_by_index_const(plugin_set
, i
);
775 const struct bt_plugin
*loaded_plugin
=
776 find_plugin(bt_plugin_get_name(plugin
));
781 BT_LOGI("Not using plugin: another one already exists with the same name: "
782 "plugin-name=\"%s\", plugin-path=\"%s\", "
783 "existing-plugin-path=\"%s\"",
784 bt_plugin_get_name(plugin
),
785 bt_plugin_get_path(plugin
),
786 bt_plugin_get_path(loaded_plugin
));
787 bt_object_put_ref(loaded_plugin
);
789 /* Add to global array. */
790 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
791 bt_plugin_get_name(plugin
));
792 bt_object_get_ref(plugin
);
793 g_ptr_array_add(loaded_plugins
, (void *) plugin
);
799 int load_dynamic_plugins(const struct bt_value
*plugin_paths
)
801 int nr_paths
, i
, ret
= 0;
803 nr_paths
= bt_value_array_get_size(plugin_paths
);
805 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
810 BT_LOGI("Loading dynamic plugins.");
812 for (i
= 0; i
< nr_paths
; i
++) {
813 const struct bt_value
*plugin_path_value
= NULL
;
814 const char *plugin_path
;
815 const struct bt_plugin_set
*plugin_set
;
818 bt_value_array_borrow_element_by_index_const(
820 plugin_path
= bt_value_string_get(plugin_path_value
);
823 * Skip this if the directory does not exist because
824 * bt_plugin_create_all_from_dir() expects an existing
827 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
828 BT_LOGV("Skipping nonexistent directory path: "
829 "path=\"%s\"", plugin_path
);
833 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
835 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
840 add_to_loaded_plugins(plugin_set
);
841 bt_object_put_ref(plugin_set
);
848 int load_static_plugins(void)
851 const struct bt_plugin_set
*plugin_set
;
853 BT_LOGI("Loading static plugins.");
854 plugin_set
= bt_plugin_create_all_from_static();
856 BT_LOGE("Unable to load static plugins.");
861 add_to_loaded_plugins(plugin_set
);
862 bt_object_put_ref(plugin_set
);
868 int load_all_plugins(const struct bt_value
*plugin_paths
)
872 if (load_dynamic_plugins(plugin_paths
)) {
877 if (load_static_plugins()) {
882 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
889 void print_plugin_info(const struct bt_plugin
*plugin
)
891 unsigned int major
, minor
, patch
;
893 enum bt_plugin_status version_status
;
894 const char *plugin_name
;
898 const char *plugin_description
;
900 plugin_name
= bt_plugin_get_name(plugin
);
901 path
= bt_plugin_get_path(plugin
);
902 author
= bt_plugin_get_author(plugin
);
903 license
= bt_plugin_get_license(plugin
);
904 plugin_description
= bt_plugin_get_description(plugin
);
905 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
907 printf("%s%s%s%s:\n", bt_common_color_bold(),
908 bt_common_color_fg_blue(), plugin_name
,
909 bt_common_color_reset());
911 printf(" %sPath%s: %s\n", bt_common_color_bold(),
912 bt_common_color_reset(), path
);
917 if (version_status
== BT_PLUGIN_STATUS_OK
) {
918 printf(" %sVersion%s: %u.%u.%u",
919 bt_common_color_bold(), bt_common_color_reset(),
920 major
, minor
, patch
);
929 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
930 bt_common_color_reset(),
931 plugin_description
? plugin_description
: "(None)");
932 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
933 bt_common_color_reset(), author
? author
: "(Unknown)");
934 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
935 bt_common_color_reset(),
936 license
? license
: "(Unknown)");
940 int cmd_query(struct bt_config
*cfg
)
943 struct bt_component_class
*comp_cls
= NULL
;
944 const struct bt_value
*results
= NULL
;
945 const char *fail_reason
= NULL
;
947 comp_cls
= find_component_class(
948 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
949 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
950 cfg
->cmd_data
.query
.cfg_component
->type
);
952 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
953 "comp-cls-name=\"%s\", comp-cls-type=%d",
954 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
955 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
956 cfg
->cmd_data
.query
.cfg_component
->type
);
957 fprintf(stderr
, "%s%sCannot find component class %s",
958 bt_common_color_bold(),
959 bt_common_color_fg_red(),
960 bt_common_color_reset());
961 print_plugin_comp_cls_opt(stderr
,
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
);
965 fprintf(stderr
, "\n");
970 ret
= query(comp_cls
, cfg
->cmd_data
.query
.object
->str
,
971 cfg
->cmd_data
.query
.cfg_component
->params
,
972 &results
, &fail_reason
);
977 print_value(stdout
, results
, 0);
981 BT_LOGE("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
);
988 fprintf(stderr
, "%s%sFailed to query info to %s",
989 bt_common_color_bold(),
990 bt_common_color_fg_red(),
991 bt_common_color_reset());
992 print_plugin_comp_cls_opt(stderr
,
993 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
994 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
995 cfg
->cmd_data
.query
.cfg_component
->type
);
996 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
997 bt_common_color_bold(),
998 bt_common_color_fg_red(),
999 cfg
->cmd_data
.query
.object
->str
,
1001 bt_common_color_reset());
1005 bt_object_put_ref(comp_cls
);
1006 bt_object_put_ref(results
);
1011 void print_component_class_help(const char *plugin_name
,
1012 struct bt_component_class
*comp_cls
)
1014 const char *comp_class_name
=
1015 bt_component_class_get_name(comp_cls
);
1016 const char *comp_class_description
=
1017 bt_component_class_get_description(comp_cls
);
1018 const char *comp_class_help
=
1019 bt_component_class_get_help(comp_cls
);
1020 enum bt_component_class_type type
=
1021 bt_component_class_get_type(comp_cls
);
1023 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
1025 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1026 bt_common_color_reset(),
1027 comp_class_description
? comp_class_description
: "(None)");
1029 if (comp_class_help
) {
1030 printf("\n%s\n", comp_class_help
);
1035 int cmd_help(struct bt_config
*cfg
)
1038 const struct bt_plugin
*plugin
= NULL
;
1039 struct bt_component_class
*needed_comp_cls
= NULL
;
1041 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1043 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
1044 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1045 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
1046 bt_common_color_bold(), bt_common_color_fg_red(),
1047 bt_common_color_fg_blue(),
1048 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1049 bt_common_color_reset());
1054 print_plugin_info(plugin
);
1055 printf(" %sSource component classes%s: %d\n",
1056 bt_common_color_bold(),
1057 bt_common_color_reset(),
1058 (int) bt_plugin_get_source_component_class_count(plugin
));
1059 printf(" %sFilter component classes%s: %d\n",
1060 bt_common_color_bold(),
1061 bt_common_color_reset(),
1062 (int) bt_plugin_get_filter_component_class_count(plugin
));
1063 printf(" %sSink component classes%s: %d\n",
1064 bt_common_color_bold(),
1065 bt_common_color_reset(),
1066 (int) bt_plugin_get_sink_component_class_count(plugin
));
1068 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
1069 /* Plugin help only */
1073 needed_comp_cls
= find_component_class(
1074 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1075 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1076 cfg
->cmd_data
.help
.cfg_component
->type
);
1077 if (!needed_comp_cls
) {
1078 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1079 "comp-cls-name=\"%s\", comp-cls-type=%d",
1080 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1081 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1082 cfg
->cmd_data
.help
.cfg_component
->type
);
1083 fprintf(stderr
, "\n%s%sCannot find component class %s",
1084 bt_common_color_bold(),
1085 bt_common_color_fg_red(),
1086 bt_common_color_reset());
1087 print_plugin_comp_cls_opt(stderr
,
1088 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1089 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1090 cfg
->cmd_data
.help
.cfg_component
->type
);
1091 fprintf(stderr
, "\n");
1097 print_component_class_help(
1098 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1102 bt_object_put_ref(needed_comp_cls
);
1103 bt_object_put_ref(plugin
);
1107 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const struct bt_plugin
*,
1109 typedef struct bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
1112 void cmd_list_plugins_print_component_classes(const struct bt_plugin
*plugin
,
1113 const char *cc_type_name
, uint64_t count
,
1114 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
1115 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
1120 printf(" %s%s component classes%s: (none)\n", cc_type_name
,
1121 bt_common_color_bold(),
1122 bt_common_color_reset());
1125 printf(" %s%s component classes%s:\n", cc_type_name
,
1126 bt_common_color_bold(),
1127 bt_common_color_reset());
1130 for (i
= 0; i
< count
; i
++) {
1131 struct bt_component_class
*comp_class
=
1132 spec_comp_cls_borrow_comp_cls_func(
1133 borrow_comp_cls_by_index_func(plugin
, i
));
1134 const char *comp_class_name
=
1135 bt_component_class_get_name(comp_class
);
1136 const char *comp_class_description
=
1137 bt_component_class_get_description(comp_class
);
1138 enum bt_component_class_type type
=
1139 bt_component_class_get_type(comp_class
);
1142 print_plugin_comp_cls_opt(stdout
,
1143 bt_plugin_get_name(plugin
), comp_class_name
,
1146 if (comp_class_description
) {
1147 printf(": %s", comp_class_description
);
1158 int cmd_list_plugins(struct bt_config
*cfg
)
1161 int plugins_count
, component_classes_count
= 0, i
;
1163 printf("From the following plugin paths:\n\n");
1164 print_value(stdout
, cfg
->plugin_paths
, 2);
1166 plugins_count
= loaded_plugins
->len
;
1167 if (plugins_count
== 0) {
1168 printf("No plugins found.\n");
1172 for (i
= 0; i
< plugins_count
; i
++) {
1173 const struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1175 component_classes_count
+=
1176 bt_plugin_get_source_component_class_count(plugin
) +
1177 bt_plugin_get_filter_component_class_count(plugin
) +
1178 bt_plugin_get_sink_component_class_count(plugin
);
1181 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1182 bt_common_color_bold(),
1183 component_classes_count
,
1184 bt_common_color_reset(),
1185 bt_common_color_bold(),
1187 bt_common_color_reset());
1189 for (i
= 0; i
< plugins_count
; i
++) {
1190 const struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1193 print_plugin_info(plugin
);
1194 cmd_list_plugins_print_component_classes(plugin
, "Source",
1195 bt_plugin_get_source_component_class_count(plugin
),
1196 (plugin_borrow_comp_cls_by_index_func_t
)
1197 bt_plugin_borrow_source_component_class_by_name_const
,
1198 (spec_comp_cls_borrow_comp_cls_func_t
)
1199 bt_component_class_source_as_component_class
);
1200 cmd_list_plugins_print_component_classes(plugin
, "Filter",
1201 bt_plugin_get_filter_component_class_count(plugin
),
1202 (plugin_borrow_comp_cls_by_index_func_t
)
1203 bt_plugin_borrow_filter_component_class_by_name_const
,
1204 (spec_comp_cls_borrow_comp_cls_func_t
)
1205 bt_component_class_filter_as_component_class
);
1206 cmd_list_plugins_print_component_classes(plugin
, "Sink",
1207 bt_plugin_get_sink_component_class_count(plugin
),
1208 (plugin_borrow_comp_cls_by_index_func_t
)
1209 bt_plugin_borrow_sink_component_class_by_name_const
,
1210 (spec_comp_cls_borrow_comp_cls_func_t
)
1211 bt_component_class_sink_as_component_class
);
1219 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1222 struct bt_component_class
*comp_cls
= NULL
;
1223 const struct bt_value
*results
= NULL
;
1224 struct bt_value
*params
= NULL
;
1225 const struct bt_value
*map
= NULL
;
1226 const struct bt_value
*v
= NULL
;
1227 static const char * const plugin_name
= "ctf";
1228 static const char * const comp_cls_name
= "lttng-live";
1229 static const enum bt_component_class_type comp_cls_type
=
1230 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1231 int64_t array_size
, i
;
1232 const char *fail_reason
= NULL
;
1233 FILE *out_stream
= stdout
;
1235 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1236 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1239 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1240 "comp-cls-name=\"%s\", comp-cls-type=%d",
1241 plugin_name
, comp_cls_name
,
1242 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1243 fprintf(stderr
, "%s%sCannot find component class %s",
1244 bt_common_color_bold(),
1245 bt_common_color_fg_red(),
1246 bt_common_color_reset());
1247 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1248 comp_cls_name
, comp_cls_type
);
1249 fprintf(stderr
, "\n");
1253 params
= bt_value_map_create();
1258 ret
= bt_value_map_insert_string_entry(params
, "url",
1259 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1264 ret
= query(comp_cls
, "sessions", params
,
1265 &results
, &fail_reason
);
1272 if (!bt_value_is_array(results
)) {
1273 BT_LOGE_STR("Expecting an array for sessions query.");
1274 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1275 bt_common_color_bold(),
1276 bt_common_color_fg_red(),
1277 bt_common_color_reset());
1281 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1283 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1287 BT_LOGE_ERRNO("Cannot open file for writing",
1289 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1294 array_size
= bt_value_array_get_size(results
);
1295 for (i
= 0; i
< array_size
; i
++) {
1296 const char *url_text
;
1297 int64_t timer_us
, streams
, clients
;
1299 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1301 BT_LOGE_STR("Unexpected empty array entry.");
1304 if (!bt_value_is_map(map
)) {
1305 BT_LOGE_STR("Unexpected entry type.");
1309 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1311 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1314 url_text
= bt_value_string_get(v
);
1315 fprintf(out_stream
, "%s", url_text
);
1316 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1318 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1321 timer_us
= bt_value_integer_get(v
);
1322 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1323 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1325 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1328 streams
= bt_value_integer_get(v
);
1329 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1330 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1332 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1335 clients
= bt_value_integer_get(v
);
1336 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1342 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1343 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1344 bt_common_color_bold(),
1345 bt_common_color_fg_red(),
1347 bt_common_color_reset());
1353 bt_object_put_ref(results
);
1354 bt_object_put_ref(params
);
1355 bt_object_put_ref(comp_cls
);
1357 if (out_stream
&& out_stream
!= stdout
) {
1358 int fclose_ret
= fclose(out_stream
);
1361 BT_LOGE_ERRNO("Cannot close file stream",
1363 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1371 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1374 struct bt_component_class
*comp_cls
= NULL
;
1375 const struct bt_value
*results
= NULL
;
1376 struct bt_value
*params
= NULL
;
1377 const struct bt_value
*metadata_text_value
= NULL
;
1378 const char *metadata_text
= NULL
;
1379 static const char * const plugin_name
= "ctf";
1380 static const char * const comp_cls_name
= "fs";
1381 static const enum bt_component_class_type comp_cls_type
=
1382 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1383 const char *fail_reason
= NULL
;
1384 FILE *out_stream
= stdout
;
1386 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1387 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1390 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1391 "comp-cls-name=\"%s\", comp-cls-type=%d",
1392 plugin_name
, comp_cls_name
,
1393 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1394 fprintf(stderr
, "%s%sCannot find component class %s",
1395 bt_common_color_bold(),
1396 bt_common_color_fg_red(),
1397 bt_common_color_reset());
1398 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1399 comp_cls_name
, comp_cls_type
);
1400 fprintf(stderr
, "\n");
1405 params
= bt_value_map_create();
1411 ret
= bt_value_map_insert_string_entry(params
, "path",
1412 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1418 ret
= query(comp_cls
, "metadata-info",
1419 params
, &results
, &fail_reason
);
1424 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1426 if (!metadata_text_value
) {
1427 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1432 metadata_text
= bt_value_string_get(metadata_text_value
);
1434 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1436 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1440 BT_LOGE_ERRNO("Cannot open file for writing",
1442 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1447 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1449 BT_LOGE("Cannot write whole metadata text to output stream: "
1457 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1458 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1459 bt_common_color_bold(),
1460 bt_common_color_fg_red(),
1462 bt_common_color_reset());
1465 bt_object_put_ref(results
);
1466 bt_object_put_ref(params
);
1467 bt_object_put_ref(comp_cls
);
1469 if (out_stream
&& out_stream
!= stdout
) {
1470 int fclose_ret
= fclose(out_stream
);
1473 BT_LOGE_ERRNO("Cannot close file stream",
1475 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1483 char *instance_name
;
1487 struct trace_range
{
1488 uint64_t intersection_range_begin_ns
;
1489 uint64_t intersection_range_end_ns
;
1493 guint
port_id_hash(gconstpointer v
)
1495 const struct port_id
*id
= v
;
1497 BT_ASSERT(id
->instance_name
);
1498 BT_ASSERT(id
->port_name
);
1500 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1504 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1506 const struct port_id
*id1
= v1
;
1507 const struct port_id
*id2
= v2
;
1509 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1510 !strcmp(id1
->port_name
, id2
->port_name
);
1514 void port_id_destroy(gpointer data
)
1516 struct port_id
*id
= data
;
1518 free(id
->instance_name
);
1519 free(id
->port_name
);
1524 void trace_range_destroy(gpointer data
)
1529 struct cmd_run_ctx
{
1531 GHashTable
*src_components
;
1534 GHashTable
*flt_components
;
1537 GHashTable
*sink_components
;
1540 struct bt_private_graph
*graph
;
1543 struct bt_config
*cfg
;
1547 bool stream_intersection_mode
;
1550 * Association of struct port_id -> struct trace_range.
1552 GHashTable
*intersections
;
1555 /* Returns a timestamp of the form "(-)s.ns" */
1557 char *s_from_ns(int64_t ns
)
1562 int64_t ts_sec_abs
, ts_nsec_abs
;
1563 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1564 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1566 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1567 is_negative
= false;
1568 ts_sec_abs
= ts_sec
;
1569 ts_nsec_abs
= ts_nsec
;
1570 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1571 is_negative
= false;
1572 ts_sec_abs
= ts_sec
- 1;
1573 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1574 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1576 ts_sec_abs
= ts_sec
;
1577 ts_nsec_abs
= -ts_nsec
;
1578 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1580 ts_sec_abs
= -(ts_sec
+ 1);
1581 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1582 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1584 ts_sec_abs
= -ts_sec
;
1585 ts_nsec_abs
= ts_nsec
;
1586 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1588 ts_sec_abs
= -ts_sec
;
1589 ts_nsec_abs
= -ts_nsec
;
1592 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1593 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1601 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1602 struct cmd_run_ctx
*ctx
,
1603 struct bt_component
*upstream_comp
,
1604 struct bt_port_output
*out_upstream_port
,
1605 struct bt_config_connection
*cfg_conn
)
1607 typedef uint64_t (*input_port_count_func_t
)(void *);
1608 typedef struct bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1610 struct bt_port
*upstream_port
=
1611 bt_port_output_as_port(out_upstream_port
);
1614 GQuark downstreamp_comp_name_quark
;
1615 void *downstream_comp
;
1616 uint64_t downstream_port_count
;
1618 input_port_count_func_t port_count_fn
;
1619 borrow_input_port_by_index_func_t port_by_index_fn
;
1620 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1621 bool insert_trimmer
= false;
1622 struct bt_value
*trimmer_params
= NULL
;
1623 char *intersection_begin
= NULL
;
1624 char *intersection_end
= NULL
;
1625 struct bt_component_filter
*trimmer
= NULL
;
1626 struct bt_component_class_filter
*trimmer_class
= NULL
;
1627 struct bt_port_input
*trimmer_input
= NULL
;
1628 struct bt_port_output
*trimmer_output
= NULL
;
1630 if (ctx
->intersections
&&
1631 bt_component_get_class_type(upstream_comp
) ==
1632 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1633 struct trace_range
*range
;
1634 struct port_id port_id
= {
1635 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1636 .port_name
= (char *) bt_port_get_name(upstream_port
)
1639 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1643 range
= (struct trace_range
*) g_hash_table_lookup(
1644 ctx
->intersections
, &port_id
);
1646 enum bt_value_status status
;
1648 intersection_begin
= s_from_ns(
1649 range
->intersection_range_begin_ns
);
1650 intersection_end
= s_from_ns(
1651 range
->intersection_range_end_ns
);
1652 if (!intersection_begin
|| !intersection_end
) {
1653 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1657 insert_trimmer
= true;
1658 trimmer_params
= bt_value_map_create();
1659 if (!trimmer_params
) {
1663 status
= bt_value_map_insert_string_entry(
1664 trimmer_params
, "begin", intersection_begin
);
1665 if (status
!= BT_VALUE_STATUS_OK
) {
1668 status
= bt_value_map_insert_string_entry(
1670 "end", intersection_end
);
1671 if (status
!= BT_VALUE_STATUS_OK
) {
1676 trimmer_class
= find_filter_component_class("utils", "trimmer");
1677 if (!trimmer_class
) {
1682 BT_LOGI("Connecting upstream port to the next available downstream port: "
1683 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1684 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1685 upstream_port
, bt_port_get_name(upstream_port
),
1686 cfg_conn
->downstream_comp_name
->str
,
1687 cfg_conn
->arg
->str
);
1688 downstreamp_comp_name_quark
= g_quark_from_string(
1689 cfg_conn
->downstream_comp_name
->str
);
1690 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1691 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1692 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1693 port_count_fn
= (input_port_count_func_t
)
1694 bt_component_filter_get_input_port_count
;
1695 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1696 bt_component_filter_borrow_input_port_by_index
;
1698 if (!downstream_comp
) {
1699 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1700 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1701 port_count_fn
= (input_port_count_func_t
)
1702 bt_component_sink_get_input_port_count
;
1703 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1704 bt_component_sink_borrow_input_port_by_index
;
1707 if (!downstream_comp
) {
1708 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1709 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1710 cfg_conn
->arg
->str
);
1711 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1712 cfg_conn
->arg
->str
);
1716 downstream_port_count
= port_count_fn(downstream_comp
);
1717 BT_ASSERT(downstream_port_count
>= 0);
1719 for (i
= 0; i
< downstream_port_count
; i
++) {
1720 struct bt_port_input
*in_downstream_port
=
1721 port_by_index_fn(downstream_comp
, i
);
1722 struct bt_port
*downstream_port
=
1723 bt_port_input_as_port(in_downstream_port
);
1724 const char *upstream_port_name
;
1725 const char *downstream_port_name
;
1727 BT_ASSERT(downstream_port
);
1729 /* Skip port if it's already connected. */
1730 if (bt_port_is_connected(downstream_port
)) {
1731 BT_LOGD("Skipping downstream port: already connected: "
1732 "port-addr=%p, port-name=\"%s\"",
1734 bt_port_get_name(downstream_port
));
1738 downstream_port_name
= bt_port_get_name(downstream_port
);
1739 BT_ASSERT(downstream_port_name
);
1740 upstream_port_name
= bt_port_get_name(upstream_port
);
1741 BT_ASSERT(upstream_port_name
);
1743 if (!bt_common_star_glob_match(
1744 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1745 downstream_port_name
, SIZE_MAX
)) {
1749 if (insert_trimmer
) {
1751 * In order to insert the trimmer between the
1752 * two components that were being connected, we
1753 * create a connection configuration entry which
1754 * describes a connection from the trimmer's
1755 * output to the original input that was being
1758 * Hence, the creation of the trimmer will cause
1759 * the graph "new port" listener to establish
1760 * all downstream connections as its output port
1761 * is connected. We will then establish the
1762 * connection between the original upstream
1763 * source and the trimmer.
1765 char *trimmer_name
= NULL
;
1766 enum bt_graph_status graph_status
;
1768 ret
= asprintf(&trimmer_name
,
1769 "stream-intersection-trimmer-%s",
1770 upstream_port_name
);
1776 ctx
->connect_ports
= false;
1777 graph_status
= bt_private_graph_add_filter_component(
1778 ctx
->graph
, trimmer_class
, trimmer_name
,
1782 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1788 bt_component_filter_borrow_input_port_by_index(
1790 if (!trimmer_input
) {
1794 bt_component_filter_borrow_output_port_by_index(
1796 if (!trimmer_output
) {
1801 * Replace the current downstream port by the trimmer's
1804 in_downstream_port
= trimmer_input
;
1806 bt_port_input_as_port(in_downstream_port
);
1807 downstream_port_name
= bt_port_get_name(
1809 BT_ASSERT(downstream_port_name
);
1812 /* We have a winner! */
1813 status
= bt_private_graph_connect_ports(ctx
->graph
,
1814 out_upstream_port
, in_downstream_port
, NULL
);
1815 downstream_port
= NULL
;
1817 case BT_GRAPH_STATUS_OK
:
1819 case BT_GRAPH_STATUS_CANCELED
:
1820 BT_LOGI_STR("Graph was canceled by user.");
1821 status
= BT_GRAPH_STATUS_OK
;
1823 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1824 BT_LOGE("A component refused a connection to one of its ports: "
1825 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1826 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1827 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1828 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1830 upstream_comp
, bt_component_get_name(upstream_comp
),
1831 upstream_port
, bt_port_get_name(upstream_port
),
1832 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1833 downstream_port
, downstream_port_name
,
1834 cfg_conn
->arg
->str
);
1836 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1837 bt_port_get_name(upstream_port
),
1838 downstream_port_name
,
1839 cfg_conn
->arg
->str
);
1842 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1843 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1844 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1845 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1846 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1848 upstream_comp
, bt_component_get_name(upstream_comp
),
1849 upstream_port
, bt_port_get_name(upstream_port
),
1850 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1851 downstream_port
, downstream_port_name
,
1852 cfg_conn
->arg
->str
);
1854 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1855 bt_port_get_name(upstream_port
),
1856 downstream_port_name
,
1857 cfg_conn
->arg
->str
);
1861 BT_LOGI("Connected component ports: "
1862 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1863 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1864 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1865 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1867 upstream_comp
, bt_component_get_name(upstream_comp
),
1868 upstream_port
, bt_port_get_name(upstream_port
),
1869 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1870 downstream_port
, downstream_port_name
,
1871 cfg_conn
->arg
->str
);
1873 if (insert_trimmer
) {
1875 * The first connection, from the source to the trimmer,
1876 * has been done. We now connect the trimmer to the
1877 * original downstream port.
1879 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1881 bt_component_filter_as_component(trimmer
),
1882 trimmer_output
, cfg_conn
);
1886 ctx
->connect_ports
= true;
1890 * We found a matching downstream port: the search is
1896 /* No downstream port found */
1897 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1898 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1899 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1900 upstream_port
, bt_port_get_name(upstream_port
),
1901 cfg_conn
->downstream_comp_name
->str
,
1902 cfg_conn
->arg
->str
);
1904 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1905 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1911 free(intersection_begin
);
1912 free(intersection_end
);
1913 BT_OBJECT_PUT_REF_AND_RESET(trimmer_params
);
1914 BT_OBJECT_PUT_REF_AND_RESET(trimmer_class
);
1915 BT_OBJECT_PUT_REF_AND_RESET(trimmer
);
1920 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1921 struct bt_port_output
*upstream_port
)
1924 const char *upstream_port_name
;
1925 const char *upstream_comp_name
;
1926 struct bt_component
*upstream_comp
= NULL
;
1930 BT_ASSERT(upstream_port
);
1931 upstream_port_name
= bt_port_get_name(
1932 bt_port_output_as_port(upstream_port
));
1933 BT_ASSERT(upstream_port_name
);
1934 upstream_comp
= bt_port_borrow_component(
1935 bt_port_output_as_port(upstream_port
));
1936 if (!upstream_comp
) {
1937 BT_LOGW("Upstream port to connect is not part of a component: "
1938 "port-addr=%p, port-name=\"%s\"",
1939 upstream_port
, upstream_port_name
);
1944 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1945 BT_ASSERT(upstream_comp_name
);
1946 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1947 "port-addr=%p, port-name=\"%s\"",
1948 upstream_comp
, upstream_comp_name
,
1949 upstream_port
, upstream_port_name
);
1951 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1952 struct bt_config_connection
*cfg_conn
=
1954 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1956 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1957 upstream_comp_name
)) {
1961 if (!bt_common_star_glob_match(
1962 cfg_conn
->upstream_port_glob
->str
,
1963 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1967 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1968 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1970 BT_LOGE("Cannot connect upstream port: "
1971 "port-addr=%p, port-name=\"%s\"",
1973 upstream_port_name
);
1975 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1978 cfg_conn
->arg
->str
);
1984 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1985 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1986 upstream_port_name
);
1988 "Cannot create connection: upstream port `%s` does not match any connection\n",
1989 upstream_port_name
);
1999 void graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
2000 struct bt_port_output
*out_port
)
2002 struct bt_component
*comp
;
2003 struct bt_port
*port
= bt_port_output_as_port(out_port
);
2005 comp
= bt_port_borrow_component(port
);
2006 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
2007 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
2008 comp
, comp
? bt_component_get_name(comp
) : "",
2009 port
, bt_port_get_name(port
));
2011 if (!ctx
->connect_ports
) {
2016 BT_LOGW_STR("Port has no component.");
2020 if (bt_port_is_connected(port
)) {
2021 BT_LOGW_STR("Port is already connected.");
2025 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
2026 BT_LOGF_STR("Cannot connect upstream port.");
2027 fprintf(stderr
, "Added port could not be connected: aborting\n");
2036 void graph_source_output_port_added_listener(
2037 struct bt_component_source
*component
,
2038 struct bt_port_output
*port
, void *data
)
2040 graph_output_port_added_listener(data
, port
);
2044 void graph_filter_output_port_added_listener(
2045 struct bt_component_filter
*component
,
2046 struct bt_port_output
*port
, void *data
)
2048 graph_output_port_added_listener(data
, port
);
2052 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
2058 if (ctx
->src_components
) {
2059 g_hash_table_destroy(ctx
->src_components
);
2060 ctx
->src_components
= NULL
;
2063 if (ctx
->flt_components
) {
2064 g_hash_table_destroy(ctx
->flt_components
);
2065 ctx
->flt_components
= NULL
;
2068 if (ctx
->sink_components
) {
2069 g_hash_table_destroy(ctx
->sink_components
);
2070 ctx
->sink_components
= NULL
;
2073 if (ctx
->intersections
) {
2074 g_hash_table_destroy(ctx
->intersections
);
2075 ctx
->intersections
= NULL
;
2078 BT_OBJECT_PUT_REF_AND_RESET(ctx
->graph
);
2084 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2087 enum bt_graph_status status
;
2090 ctx
->connect_ports
= false;
2091 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
2092 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2093 if (!ctx
->src_components
) {
2097 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
2098 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2099 if (!ctx
->flt_components
) {
2103 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
2104 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2105 if (!ctx
->sink_components
) {
2109 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2110 ctx
->stream_intersection_mode
= true;
2111 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2112 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2113 if (!ctx
->intersections
) {
2118 ctx
->graph
= bt_private_graph_create();
2123 the_graph
= ctx
->graph
;
2124 status
= bt_private_graph_add_source_component_output_port_added_listener(
2125 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
2127 if (status
!= BT_GRAPH_STATUS_OK
) {
2128 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2132 status
= bt_private_graph_add_filter_component_output_port_added_listener(
2133 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
2135 if (status
!= BT_GRAPH_STATUS_OK
) {
2136 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2143 cmd_run_ctx_destroy(ctx
);
2151 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2152 struct bt_config_component
*cfg_comp
,
2153 struct bt_component_class_source
*src_comp_cls
)
2157 int64_t trace_count
;
2158 enum bt_value_status value_status
;
2159 const char *path
= NULL
;
2160 const struct bt_value
*component_path_value
= NULL
;
2161 struct bt_value
*query_params
= NULL
;
2162 const struct bt_value
*query_result
= NULL
;
2163 const struct bt_value
*trace_info
= NULL
;
2164 const struct bt_value
*intersection_range
= NULL
;
2165 const struct bt_value
*intersection_begin
= NULL
;
2166 const struct bt_value
*intersection_end
= NULL
;
2167 const struct bt_value
*stream_path_value
= NULL
;
2168 const struct bt_value
*stream_paths
= NULL
;
2169 const struct bt_value
*stream_infos
= NULL
;
2170 const struct bt_value
*stream_info
= NULL
;
2171 struct port_id
*port_id
= NULL
;
2172 struct trace_range
*trace_range
= NULL
;
2173 const char *fail_reason
= NULL
;
2174 struct bt_component_class
*comp_cls
=
2175 bt_component_class_source_as_component_class(src_comp_cls
);
2177 component_path_value
= bt_value_map_borrow_entry_value(cfg_comp
->params
,
2179 if (component_path_value
&& !bt_value_is_string(component_path_value
)) {
2180 BT_LOGD("Cannot get path parameter: component-name=%s",
2181 cfg_comp
->instance_name
->str
);
2186 path
= bt_value_string_get(component_path_value
);
2187 query_params
= bt_value_map_create();
2188 if (!query_params
) {
2189 BT_LOGE_STR("Cannot create query parameters.");
2194 value_status
= bt_value_map_insert_string_entry(query_params
, "path",
2196 if (value_status
!= BT_VALUE_STATUS_OK
) {
2197 BT_LOGE_STR("Cannot insert path parameter in query parameter map.");
2202 ret
= query(comp_cls
, "trace-info",
2203 query_params
, &query_result
,
2206 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2207 "comp-class-name=\"%s\"", fail_reason
,
2208 bt_component_class_get_name(comp_cls
));
2213 BT_ASSERT(query_result
);
2215 if (!bt_value_is_array(query_result
)) {
2216 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2217 "component-class-name=%s",
2218 bt_component_class_get_name(comp_cls
));
2223 trace_count
= bt_value_array_get_size(query_result
);
2224 if (trace_count
< 0) {
2229 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2231 uint64_t stream_idx
;
2232 int64_t stream_count
;
2234 trace_info
= bt_value_array_borrow_element_by_index_const(
2235 query_result
, trace_idx
);
2236 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2238 BT_LOGD_STR("Cannot retrieve trace from query result.");
2242 intersection_range
= bt_value_map_borrow_entry_value_const(
2243 trace_info
, "intersection-range-ns");
2244 if (!intersection_range
) {
2246 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2250 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
2252 if (!intersection_begin
) {
2254 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2258 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
2260 if (!intersection_end
) {
2262 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2266 begin
= bt_value_integer_get(intersection_begin
);
2267 end
= bt_value_integer_get(intersection_end
);
2269 if (begin
< 0 || end
< 0 || end
< begin
) {
2270 BT_LOGW("Invalid trace stream intersection values: "
2271 "intersection-range-ns:begin=%" PRId64
2272 ", intersection-range-ns:end=%" PRId64
,
2278 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
2280 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2282 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2286 stream_count
= bt_value_array_get_size(stream_infos
);
2287 if (stream_count
< 0) {
2295 * The first path of a stream's "paths" is currently used to
2296 * associate streams/ports to a given trace intersection.
2298 * This is a fragile hack as it relies on the port names
2299 * being set to the various streams path.
2301 * A stream name should be introduced as part of the trace-info
2304 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2305 const char *stream_path
;
2307 port_id
= g_new0(struct port_id
, 1);
2310 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2313 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2314 if (!port_id
->instance_name
) {
2316 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2320 trace_range
= g_new0(struct trace_range
, 1);
2323 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2326 trace_range
->intersection_range_begin_ns
= begin
;
2327 trace_range
->intersection_range_end_ns
= end
;
2329 stream_info
= bt_value_array_borrow_element_by_index_const(
2330 stream_infos
, stream_idx
);
2331 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2333 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2337 stream_paths
= bt_value_map_borrow_entry_value_const(stream_info
,
2339 if (!stream_paths
|| !bt_value_is_array(stream_paths
)) {
2341 BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
2346 bt_value_array_borrow_element_by_index_const(
2348 if (!stream_path_value
||
2349 !bt_value_is_string(stream_path_value
)) {
2351 BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
2355 stream_path
= bt_value_string_get(stream_path_value
);
2356 port_id
->port_name
= strdup(stream_path
);
2357 if (!port_id
->port_name
) {
2359 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2363 BT_LOGD("Inserting stream intersection ");
2365 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2375 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2376 bt_common_color_bold(),
2377 bt_common_color_fg_yellow(),
2378 path
? path
: "(unknown)",
2379 bt_common_color_reset());
2381 bt_object_put_ref(query_params
);
2382 bt_object_put_ref(query_result
);
2384 g_free(trace_range
);
2389 int cmd_run_ctx_create_components_from_config_components(
2390 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2393 void *comp_cls
= NULL
;
2397 for (i
= 0; i
< cfg_components
->len
; i
++) {
2398 struct bt_config_component
*cfg_comp
=
2399 g_ptr_array_index(cfg_components
, i
);
2402 switch (cfg_comp
->type
) {
2403 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2404 comp_cls
= find_source_component_class(
2405 cfg_comp
->plugin_name
->str
,
2406 cfg_comp
->comp_cls_name
->str
);
2408 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2409 comp_cls
= find_filter_component_class(
2410 cfg_comp
->plugin_name
->str
,
2411 cfg_comp
->comp_cls_name
->str
);
2413 case BT_COMPONENT_CLASS_TYPE_SINK
:
2414 comp_cls
= find_sink_component_class(
2415 cfg_comp
->plugin_name
->str
,
2416 cfg_comp
->comp_cls_name
->str
);
2423 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2424 "comp-cls-name=\"%s\", comp-cls-type=%d",
2425 cfg_comp
->plugin_name
->str
,
2426 cfg_comp
->comp_cls_name
->str
,
2428 fprintf(stderr
, "%s%sCannot find component class %s",
2429 bt_common_color_bold(),
2430 bt_common_color_fg_red(),
2431 bt_common_color_reset());
2432 print_plugin_comp_cls_opt(stderr
,
2433 cfg_comp
->plugin_name
->str
,
2434 cfg_comp
->comp_cls_name
->str
,
2436 fprintf(stderr
, "\n");
2440 switch (cfg_comp
->type
) {
2441 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2442 ret
= bt_private_graph_add_source_component(ctx
->graph
,
2443 comp_cls
, cfg_comp
->instance_name
->str
,
2447 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2448 ret
= bt_private_graph_add_filter_component(ctx
->graph
,
2449 comp_cls
, cfg_comp
->instance_name
->str
,
2453 case BT_COMPONENT_CLASS_TYPE_SINK
:
2454 ret
= bt_private_graph_add_sink_component(ctx
->graph
,
2455 comp_cls
, cfg_comp
->instance_name
->str
,
2464 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2465 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2467 cfg_comp
->plugin_name
->str
,
2468 cfg_comp
->comp_cls_name
->str
,
2469 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2470 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2471 bt_common_color_bold(),
2472 bt_common_color_fg_red(),
2473 cfg_comp
->instance_name
->str
,
2474 bt_common_color_reset());
2478 if (ctx
->stream_intersection_mode
&&
2479 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2480 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2486 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2487 comp
, cfg_comp
->instance_name
->str
);
2488 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2489 BT_ASSERT(quark
> 0);
2491 switch (cfg_comp
->type
) {
2492 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2493 g_hash_table_insert(ctx
->src_components
,
2494 GUINT_TO_POINTER(quark
), comp
);
2496 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2497 g_hash_table_insert(ctx
->flt_components
,
2498 GUINT_TO_POINTER(quark
), comp
);
2500 case BT_COMPONENT_CLASS_TYPE_SINK
:
2501 g_hash_table_insert(ctx
->sink_components
,
2502 GUINT_TO_POINTER(quark
), comp
);
2509 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2518 bt_object_put_ref(comp
);
2519 bt_object_put_ref(comp_cls
);
2524 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2529 * Make sure that, during this phase, our graph's "port added"
2530 * listener does not connect ports while we are creating the
2531 * components because we have a special, initial phase for
2534 ctx
->connect_ports
= false;
2536 ret
= cmd_run_ctx_create_components_from_config_components(
2537 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2543 ret
= cmd_run_ctx_create_components_from_config_components(
2544 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2550 ret
= cmd_run_ctx_create_components_from_config_components(
2551 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2561 typedef uint64_t (*output_port_count_func_t
)(void *);
2562 typedef struct bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2566 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2567 void *comp
, output_port_count_func_t port_count_fn
,
2568 borrow_output_port_by_index_func_t port_by_index_fn
)
2574 count
= port_count_fn(comp
);
2575 BT_ASSERT(count
>= 0);
2577 for (i
= 0; i
< count
; i
++) {
2578 struct bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2580 BT_ASSERT(upstream_port
);
2581 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2592 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2595 GHashTableIter iter
;
2596 gpointer g_name_quark
, g_comp
;
2598 ctx
->connect_ports
= true;
2599 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2601 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2602 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2603 (output_port_count_func_t
)
2604 bt_component_source_get_output_port_count
,
2605 (borrow_output_port_by_index_func_t
)
2606 bt_component_source_borrow_output_port_by_index
);
2612 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2614 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2615 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2616 (output_port_count_func_t
)
2617 bt_component_filter_get_output_port_count
,
2618 (borrow_output_port_by_index_func_t
)
2619 bt_component_filter_borrow_output_port_by_index
);
2630 const char *bt_graph_status_str(enum bt_graph_status status
)
2633 case BT_GRAPH_STATUS_OK
:
2634 return "BT_GRAPH_STATUS_OK";
2635 case BT_GRAPH_STATUS_END
:
2636 return "BT_GRAPH_STATUS_END";
2637 case BT_GRAPH_STATUS_AGAIN
:
2638 return "BT_GRAPH_STATUS_AGAIN";
2639 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
2640 return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION";
2641 case BT_GRAPH_STATUS_CANCELED
:
2642 return "BT_GRAPH_STATUS_CANCELED";
2643 case BT_GRAPH_STATUS_ERROR
:
2644 return "BT_GRAPH_STATUS_ERROR";
2645 case BT_GRAPH_STATUS_NO_SINK
:
2646 return "BT_GRAPH_STATUS_NO_SINK";
2647 case BT_GRAPH_STATUS_NOMEM
:
2648 return "BT_GRAPH_STATUS_NOMEM";
2655 int cmd_run(struct bt_config
*cfg
)
2658 struct cmd_run_ctx ctx
= { 0 };
2660 /* Initialize the command's context and the graph object */
2661 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2662 BT_LOGE_STR("Cannot initialize the command's context.");
2663 fprintf(stderr
, "Cannot initialize the command's context\n");
2668 BT_LOGI_STR("Canceled by user before creating components.");
2672 BT_LOGI_STR("Creating components.");
2674 /* Create the requested component instances */
2675 if (cmd_run_ctx_create_components(&ctx
)) {
2676 BT_LOGE_STR("Cannot create components.");
2677 fprintf(stderr
, "Cannot create components\n");
2682 BT_LOGI_STR("Canceled by user before connecting components.");
2686 BT_LOGI_STR("Connecting components.");
2688 /* Connect the initially visible component ports */
2689 if (cmd_run_ctx_connect_ports(&ctx
)) {
2690 BT_LOGE_STR("Cannot connect initial component ports.");
2691 fprintf(stderr
, "Cannot connect initial component ports\n");
2696 BT_LOGI_STR("Canceled by user before running the graph.");
2700 BT_LOGI_STR("Running the graph.");
2704 enum bt_graph_status graph_status
= bt_private_graph_run(ctx
.graph
);
2707 * Reset console in case something messed with console
2708 * codes during the graph's execution.
2710 printf("%s", bt_common_color_reset());
2712 fprintf(stderr
, "%s", bt_common_color_reset());
2713 BT_LOGV("bt_private_graph_run() returned: status=%s",
2714 bt_graph_status_str(graph_status
));
2716 switch (graph_status
) {
2717 case BT_GRAPH_STATUS_OK
:
2719 case BT_GRAPH_STATUS_CANCELED
:
2720 BT_LOGI_STR("Graph was canceled by user.");
2722 case BT_GRAPH_STATUS_AGAIN
:
2723 if (bt_graph_is_canceled(
2724 bt_private_graph_as_graph(ctx
.graph
))) {
2725 BT_LOGI_STR("Graph was canceled by user.");
2729 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2730 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2732 cfg
->cmd_data
.run
.retry_duration_us
);
2734 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2735 if (bt_graph_is_canceled(
2736 bt_private_graph_as_graph(ctx
.graph
))) {
2737 BT_LOGI_STR("Graph was canceled by user.");
2743 case BT_GRAPH_STATUS_END
:
2746 BT_LOGE_STR("Graph failed to complete successfully");
2747 fprintf(stderr
, "Graph failed to complete successfully\n");
2760 cmd_run_ctx_destroy(&ctx
);
2765 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2767 const char *env_clash
;
2769 if (!cfg
->command_name
) {
2773 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2774 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2778 if (g_file_test(cfg
->command_name
,
2779 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2780 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2782 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2784 fprintf(stderr
, "\n");
2785 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
2791 void init_log_level(void)
2793 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2797 void set_auto_log_levels(struct bt_config
*cfg
)
2799 const char **env_var_name
;
2802 * Override the configuration's default log level if
2803 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2804 * are found for backward compatibility with legacy Babetrace 1.
2806 if (getenv("BABELTRACE_DEBUG") &&
2807 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2808 cfg
->log_level
= 'V';
2809 } else if (getenv("BABELTRACE_VERBOSE") &&
2810 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2811 cfg
->log_level
= 'I';
2815 * Set log levels according to --debug or --verbose. For
2816 * backward compatibility, --debug is more verbose than
2819 * --verbose: INFO log level
2820 * --debug: VERBOSE log level (includes DEBUG, which is
2821 * is less verbose than VERBOSE in the internal
2822 * logging framework)
2824 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2826 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2827 } else if (cfg
->debug
) {
2828 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2831 * Set library's default log level if not
2832 * explicitly specified.
2834 switch (cfg
->log_level
) {
2836 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE
);
2839 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2842 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
2845 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2848 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN
);
2851 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR
);
2854 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL
);
2862 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2864 bt_cli_log_level
= BT_LOG_INFO
;
2865 } else if (cfg
->debug
) {
2866 bt_cli_log_level
= BT_LOG_VERBOSE
;
2869 * Set CLI's default log level if not explicitly
2872 switch (cfg
->log_level
) {
2874 bt_cli_log_level
= BT_LOG_NONE
;
2877 bt_cli_log_level
= BT_LOG_VERBOSE
;
2880 bt_cli_log_level
= BT_LOG_DEBUG
;
2883 bt_cli_log_level
= BT_LOG_INFO
;
2886 bt_cli_log_level
= BT_LOG_WARN
;
2889 bt_cli_log_level
= BT_LOG_ERROR
;
2892 bt_cli_log_level
= BT_LOG_FATAL
;
2900 env_var_name
= log_level_env_var_names
;
2902 while (*env_var_name
) {
2903 if (!getenv(*env_var_name
)) {
2905 g_setenv(*env_var_name
, "I", 1);
2906 } else if (cfg
->debug
) {
2907 g_setenv(*env_var_name
, "V", 1);
2909 char val
[2] = { 0 };
2912 * Set module's default log level if not
2913 * explicitly specified.
2915 val
[0] = cfg
->log_level
;
2916 g_setenv(*env_var_name
, val
, 1);
2924 int main(int argc
, const char **argv
)
2928 struct bt_config
*cfg
;
2931 set_signal_handler();
2933 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2936 /* Quit without errors; typically usage/version */
2938 BT_LOGI_STR("Quitting without errors.");
2943 BT_LOGE("Command-line error: retcode=%d", retcode
);
2948 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2949 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2954 set_auto_log_levels(cfg
);
2957 if (cfg
->command_needs_plugins
) {
2958 ret
= load_all_plugins(cfg
->plugin_paths
);
2960 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2966 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2967 cfg
->command
, cfg
->command_name
);
2969 switch (cfg
->command
) {
2970 case BT_CONFIG_COMMAND_RUN
:
2973 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2974 ret
= cmd_list_plugins(cfg
);
2976 case BT_CONFIG_COMMAND_HELP
:
2977 ret
= cmd_help(cfg
);
2979 case BT_CONFIG_COMMAND_QUERY
:
2980 ret
= cmd_query(cfg
);
2982 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2983 ret
= cmd_print_ctf_metadata(cfg
);
2985 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2986 ret
= cmd_print_lttng_live_sessions(cfg
);
2989 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2993 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2994 cfg
->command
, cfg
->command_name
, ret
);
2995 warn_command_name_and_directory_clash(cfg
);
2996 retcode
= ret
? 1 : 0;
2999 BT_OBJECT_PUT_REF_AND_RESET(cfg
);