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(bt_object_put_ref
);
145 void fini_static_data(void)
147 g_ptr_array_free(loaded_plugins
, TRUE
);
151 int create_the_query_executor(void)
155 the_query_executor
= bt_private_query_executor_create();
156 if (!the_query_executor
) {
157 BT_LOGE_STR("Cannot create a query executor.");
165 void destroy_the_query_executor(void)
167 BT_OBJECT_PUT_REF_AND_RESET(the_query_executor
);
171 int query(struct bt_component_class
*comp_cls
, const char *obj
,
172 struct bt_value
*params
, struct bt_value
**user_result
,
173 const char **fail_reason
)
175 struct bt_value
*result
= NULL
;
176 enum bt_query_executor_status status
;
177 *fail_reason
= "unknown error";
180 BT_ASSERT(fail_reason
);
181 BT_ASSERT(user_result
);
182 ret
= create_the_query_executor();
184 /* create_the_query_executor() logs errors */
189 BT_LOGI("Canceled by user before executing the query: "
190 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
191 "query-obj=\"%s\"", comp_cls
,
192 bt_component_class_get_name(comp_cls
), obj
);
193 *fail_reason
= "canceled by user";
198 status
= bt_private_query_executor_query(the_query_executor
,
199 comp_cls
, obj
, params
, &result
);
201 case BT_QUERY_EXECUTOR_STATUS_OK
:
203 case BT_QUERY_EXECUTOR_STATUS_AGAIN
:
205 const uint64_t sleep_time_us
= 100000;
207 /* Wait 100 ms and retry */
208 BT_LOGV("Got BT_QUERY_EXECUTOR_STATUS_AGAIN: sleeping: "
209 "time-us=%" PRIu64
, sleep_time_us
);
211 if (usleep(sleep_time_us
)) {
212 if (bt_query_executor_is_canceled(
213 bt_private_query_executor_as_query_executor(the_query_executor
))) {
214 BT_LOGI("Query was canceled by user: "
215 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
216 "query-obj=\"%s\"", comp_cls
,
217 bt_component_class_get_name(comp_cls
),
219 *fail_reason
= "canceled by user";
226 case BT_QUERY_EXECUTOR_STATUS_CANCELED
:
227 *fail_reason
= "canceled by user";
229 case BT_QUERY_EXECUTOR_STATUS_ERROR
:
231 case BT_QUERY_EXECUTOR_STATUS_INVALID_OBJECT
:
232 *fail_reason
= "invalid or unknown query object";
234 case BT_QUERY_EXECUTOR_STATUS_INVALID_PARAMS
:
235 *fail_reason
= "invalid query parameters";
237 case BT_QUERY_EXECUTOR_STATUS_UNSUPPORTED
:
238 *fail_reason
= "unsupported action";
240 case BT_QUERY_EXECUTOR_STATUS_NOMEM
:
241 *fail_reason
= "not enough memory";
244 BT_LOGF("Unknown query status: status=%d", status
);
250 *user_result
= result
;
258 destroy_the_query_executor();
259 bt_object_put_ref(result
);
264 struct bt_plugin
*find_plugin(const char *name
)
267 struct bt_plugin
*plugin
= NULL
;
270 BT_LOGD("Finding plugin: name=\"%s\"", name
);
272 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
273 plugin
= g_ptr_array_index(loaded_plugins
, i
);
275 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
282 if (BT_LOG_ON_DEBUG
) {
284 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
286 BT_LOGD("Cannot find plugin.");
290 return bt_object_get_ref(plugin
);
293 typedef void *(*plugin_borrow_comp_cls_func_t
)(struct bt_plugin
*,
297 void *find_component_class_from_plugin(const char *plugin_name
,
298 const char *comp_class_name
,
299 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
301 void *comp_class
= NULL
;
302 struct bt_plugin
*plugin
;
304 BT_LOGD("Finding component class: plugin-name=\"%s\", "
305 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
307 plugin
= find_plugin(plugin_name
);
312 comp_class
= bt_object_get_ref(
313 plugin_borrow_comp_cls_func(plugin
, comp_class_name
));
314 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
317 if (BT_LOG_ON_DEBUG
) {
319 BT_LOGD("Found component class: comp-cls-addr=%p",
322 BT_LOGD("Cannot find source component class.");
330 struct bt_component_class_source
*find_source_component_class(
331 const char *plugin_name
, const char *comp_class_name
)
333 return (void *) find_component_class_from_plugin(plugin_name
,
335 (plugin_borrow_comp_cls_func_t
)
336 bt_plugin_borrow_source_component_class_by_name
);
340 struct bt_component_class_filter
*find_filter_component_class(
341 const char *plugin_name
, const char *comp_class_name
)
343 return (void *) find_component_class_from_plugin(plugin_name
,
345 (plugin_borrow_comp_cls_func_t
)
346 bt_plugin_borrow_filter_component_class_by_name
);
350 struct bt_component_class_sink
*find_sink_component_class(
351 const char *plugin_name
, const char *comp_class_name
)
353 return (void *) find_component_class_from_plugin(plugin_name
,
355 (plugin_borrow_comp_cls_func_t
)
356 bt_plugin_borrow_sink_component_class_by_name
);
360 struct bt_component_class
*find_component_class(const char *plugin_name
,
361 const char *comp_class_name
,
362 enum bt_component_class_type comp_class_type
)
364 struct bt_component_class
*comp_cls
= NULL
;
366 switch (comp_class_type
) {
367 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
368 comp_cls
= bt_component_class_source_as_component_class(
369 find_source_component_class(plugin_name
,
372 case BT_COMPONENT_CLASS_TYPE_FILTER
:
373 comp_cls
= bt_component_class_filter_as_component_class(
374 find_filter_component_class(plugin_name
,
377 case BT_COMPONENT_CLASS_TYPE_SINK
:
378 comp_cls
= bt_component_class_sink_as_component_class(
379 find_sink_component_class(plugin_name
,
390 void print_indent(FILE *fp
, size_t indent
)
394 for (i
= 0; i
< indent
; i
++) {
400 const char *component_type_str(enum bt_component_class_type type
)
403 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
405 case BT_COMPONENT_CLASS_TYPE_SINK
:
407 case BT_COMPONENT_CLASS_TYPE_FILTER
:
415 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
416 const char *comp_cls_name
, enum bt_component_class_type type
)
418 GString
*shell_plugin_name
= NULL
;
419 GString
*shell_comp_cls_name
= NULL
;
421 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
422 if (!shell_plugin_name
) {
426 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
427 if (!shell_comp_cls_name
) {
431 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
432 bt_common_color_bold(),
433 bt_common_color_fg_cyan(),
434 component_type_str(type
),
435 bt_common_color_fg_default(),
436 bt_common_color_fg_blue(),
437 shell_plugin_name
->str
,
438 bt_common_color_fg_default(),
439 bt_common_color_fg_yellow(),
440 shell_comp_cls_name
->str
,
441 bt_common_color_reset());
444 if (shell_plugin_name
) {
445 g_string_free(shell_plugin_name
, TRUE
);
448 if (shell_comp_cls_name
) {
449 g_string_free(shell_comp_cls_name
, TRUE
);
454 void print_value(FILE *, struct bt_value
*, size_t);
457 void print_value_rec(FILE *, struct bt_value
*, size_t);
459 struct print_map_value_data
{
465 bt_bool
print_map_value(const char *key
, struct bt_value
*object
, void *data
)
467 struct print_map_value_data
*print_map_value_data
= data
;
469 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
470 fprintf(print_map_value_data
->fp
, "%s: ", key
);
473 if (bt_value_is_array(object
) &&
474 bt_value_array_is_empty(object
)) {
475 fprintf(print_map_value_data
->fp
, "[ ]\n");
479 if (bt_value_is_map(object
) &&
480 bt_value_map_is_empty(object
)) {
481 fprintf(print_map_value_data
->fp
, "{ }\n");
485 if (bt_value_is_array(object
) ||
486 bt_value_is_map(object
)) {
487 fprintf(print_map_value_data
->fp
, "\n");
490 print_value_rec(print_map_value_data
->fp
, object
,
491 print_map_value_data
->indent
+ 2);
496 void print_value_rec(FILE *fp
, struct bt_value
*value
, size_t indent
)
509 switch (bt_value_get_type(value
)) {
510 case BT_VALUE_TYPE_NULL
:
511 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
512 bt_common_color_reset());
514 case BT_VALUE_TYPE_BOOL
:
515 bool_val
= bt_value_bool_get(value
);
516 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
517 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
518 bt_common_color_reset());
520 case BT_VALUE_TYPE_INTEGER
:
521 int_val
= bt_value_integer_get(value
);
522 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
523 bt_common_color_fg_red(), int_val
,
524 bt_common_color_reset());
526 case BT_VALUE_TYPE_REAL
:
527 dbl_val
= bt_value_real_get(value
);
528 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
529 bt_common_color_fg_red(), dbl_val
,
530 bt_common_color_reset());
532 case BT_VALUE_TYPE_STRING
:
533 str_val
= bt_value_string_get(value
);
534 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
535 bt_common_color_fg_green(), str_val
,
536 bt_common_color_reset());
538 case BT_VALUE_TYPE_ARRAY
:
539 size
= bt_value_array_get_size(value
);
545 print_indent(fp
, indent
);
546 fprintf(fp
, "[ ]\n");
550 for (i
= 0; i
< size
; i
++) {
551 struct bt_value
*element
=
552 bt_value_array_borrow_element_by_index(
558 print_indent(fp
, indent
);
561 if (bt_value_is_array(element
) &&
562 bt_value_array_is_empty(element
)) {
563 fprintf(fp
, "[ ]\n");
567 if (bt_value_is_map(element
) &&
568 bt_value_map_is_empty(element
)) {
569 fprintf(fp
, "{ }\n");
573 if (bt_value_is_array(element
) ||
574 bt_value_is_map(element
)) {
578 print_value_rec(fp
, element
, indent
+ 2);
581 case BT_VALUE_TYPE_MAP
:
583 struct print_map_value_data data
= {
588 if (bt_value_map_is_empty(value
)) {
589 print_indent(fp
, indent
);
590 fprintf(fp
, "{ }\n");
594 bt_value_map_foreach_entry(value
, print_map_value
, &data
);
603 BT_LOGE("Error printing value of type %s.",
604 bt_common_value_type_string(bt_value_get_type(value
)));
608 void print_value(FILE *fp
, struct bt_value
*value
, size_t indent
)
610 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
611 print_indent(fp
, indent
);
614 print_value_rec(fp
, value
, indent
);
618 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
620 fprintf(stderr
, " ");
621 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
622 bt_config_component
->comp_cls_name
->str
,
623 bt_config_component
->type
);
624 fprintf(stderr
, ":\n");
626 if (bt_config_component
->instance_name
->len
> 0) {
627 fprintf(stderr
, " Name: %s\n",
628 bt_config_component
->instance_name
->str
);
631 fprintf(stderr
, " Parameters:\n");
633 bt_private_value_as_value(bt_config_component
->params
), 8);
637 void print_bt_config_components(GPtrArray
*array
)
641 for (i
= 0; i
< array
->len
; i
++) {
642 struct bt_config_component
*cfg_component
=
643 bt_config_get_component(array
, i
);
644 print_bt_config_component(cfg_component
);
645 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
650 void print_plugin_paths(struct bt_value
*plugin_paths
)
652 fprintf(stderr
, " Plugin paths:\n");
653 print_value(stderr
, plugin_paths
, 4);
657 void print_cfg_run(struct bt_config
*cfg
)
661 print_plugin_paths(bt_private_value_as_value(cfg
->plugin_paths
));
662 fprintf(stderr
, " Source component instances:\n");
663 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
665 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
666 fprintf(stderr
, " Filter component instances:\n");
667 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
670 fprintf(stderr
, " Sink component instances:\n");
671 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
672 fprintf(stderr
, " Connections:\n");
674 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
675 struct bt_config_connection
*cfg_connection
=
676 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
679 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
680 cfg_connection
->upstream_comp_name
->str
,
681 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
682 cfg_connection
->upstream_port_glob
->str
,
683 cfg_connection
->downstream_comp_name
->str
,
684 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
685 cfg_connection
->downstream_port_glob
->str
);
690 void print_cfg_list_plugins(struct bt_config
*cfg
)
692 print_plugin_paths(bt_private_value_as_value(cfg
->plugin_paths
));
696 void print_cfg_help(struct bt_config
*cfg
)
698 print_plugin_paths(bt_private_value_as_value(cfg
->plugin_paths
));
702 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
704 print_plugin_paths(bt_private_value_as_value(cfg
->plugin_paths
));
705 fprintf(stderr
, " Path: %s\n",
706 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
710 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
712 print_plugin_paths(bt_private_value_as_value(cfg
->plugin_paths
));
713 fprintf(stderr
, " URL: %s\n",
714 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
718 void print_cfg_query(struct bt_config
*cfg
)
720 print_plugin_paths(bt_private_value_as_value(cfg
->plugin_paths
));
721 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
722 fprintf(stderr
, " Component class:\n");
723 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
727 void print_cfg(struct bt_config
*cfg
)
729 if (!BT_LOG_ON_INFO
) {
733 BT_LOGI_STR("Configuration:");
734 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
735 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
737 switch (cfg
->command
) {
738 case BT_CONFIG_COMMAND_RUN
:
741 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
742 print_cfg_list_plugins(cfg
);
744 case BT_CONFIG_COMMAND_HELP
:
747 case BT_CONFIG_COMMAND_QUERY
:
748 print_cfg_query(cfg
);
750 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
751 print_cfg_print_ctf_metadata(cfg
);
753 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
754 print_cfg_print_lttng_live_sessions(cfg
);
762 void add_to_loaded_plugins(struct bt_plugin_set
*plugin_set
)
767 count
= bt_plugin_set_get_plugin_count(plugin_set
);
768 BT_ASSERT(count
>= 0);
770 for (i
= 0; i
< count
; i
++) {
771 struct bt_plugin
*plugin
=
772 bt_plugin_set_borrow_plugin_by_index(plugin_set
, i
);
773 struct bt_plugin
*loaded_plugin
=
774 find_plugin(bt_plugin_get_name(plugin
));
779 BT_LOGI("Not using plugin: another one already exists with the same name: "
780 "plugin-name=\"%s\", plugin-path=\"%s\", "
781 "existing-plugin-path=\"%s\"",
782 bt_plugin_get_name(plugin
),
783 bt_plugin_get_path(plugin
),
784 bt_plugin_get_path(loaded_plugin
));
785 bt_object_put_ref(loaded_plugin
);
787 /* Add to global array. */
788 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
789 bt_plugin_get_name(plugin
));
790 g_ptr_array_add(loaded_plugins
, bt_object_get_ref(plugin
));
796 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
798 int nr_paths
, i
, ret
= 0;
800 nr_paths
= bt_value_array_get_size(plugin_paths
);
802 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
807 BT_LOGI("Loading dynamic plugins.");
809 for (i
= 0; i
< nr_paths
; i
++) {
810 struct bt_value
*plugin_path_value
= NULL
;
811 const char *plugin_path
;
812 struct bt_plugin_set
*plugin_set
;
814 plugin_path_value
= bt_value_array_borrow_element_by_index(
816 plugin_path
= bt_value_string_get(plugin_path_value
);
819 * Skip this if the directory does not exist because
820 * bt_plugin_create_all_from_dir() expects an existing
823 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
824 BT_LOGV("Skipping nonexistent directory path: "
825 "path=\"%s\"", plugin_path
);
829 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
831 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
836 add_to_loaded_plugins(plugin_set
);
837 bt_object_put_ref(plugin_set
);
844 int load_static_plugins(void)
847 struct bt_plugin_set
*plugin_set
;
849 BT_LOGI("Loading static plugins.");
850 plugin_set
= bt_plugin_create_all_from_static();
852 BT_LOGE("Unable to load static plugins.");
857 add_to_loaded_plugins(plugin_set
);
858 bt_object_put_ref(plugin_set
);
864 int load_all_plugins(struct bt_value
*plugin_paths
)
868 if (load_dynamic_plugins(plugin_paths
)) {
873 if (load_static_plugins()) {
878 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
885 void print_plugin_info(struct bt_plugin
*plugin
)
887 unsigned int major
, minor
, patch
;
889 enum bt_plugin_status version_status
;
890 const char *plugin_name
;
894 const char *plugin_description
;
896 plugin_name
= bt_plugin_get_name(plugin
);
897 path
= bt_plugin_get_path(plugin
);
898 author
= bt_plugin_get_author(plugin
);
899 license
= bt_plugin_get_license(plugin
);
900 plugin_description
= bt_plugin_get_description(plugin
);
901 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
903 printf("%s%s%s%s:\n", bt_common_color_bold(),
904 bt_common_color_fg_blue(), plugin_name
,
905 bt_common_color_reset());
907 printf(" %sPath%s: %s\n", bt_common_color_bold(),
908 bt_common_color_reset(), path
);
913 if (version_status
== BT_PLUGIN_STATUS_OK
) {
914 printf(" %sVersion%s: %u.%u.%u",
915 bt_common_color_bold(), bt_common_color_reset(),
916 major
, minor
, patch
);
925 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
926 bt_common_color_reset(),
927 plugin_description
? plugin_description
: "(None)");
928 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
929 bt_common_color_reset(), author
? author
: "(Unknown)");
930 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
931 bt_common_color_reset(),
932 license
? license
: "(Unknown)");
936 int cmd_query(struct bt_config
*cfg
)
939 struct bt_component_class
*comp_cls
= NULL
;
940 struct bt_value
*results
= NULL
;
941 const char *fail_reason
= NULL
;
943 comp_cls
= find_component_class(
944 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
945 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
946 cfg
->cmd_data
.query
.cfg_component
->type
);
948 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
949 "comp-cls-name=\"%s\", comp-cls-type=%d",
950 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
951 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
952 cfg
->cmd_data
.query
.cfg_component
->type
);
953 fprintf(stderr
, "%s%sCannot find component class %s",
954 bt_common_color_bold(),
955 bt_common_color_fg_red(),
956 bt_common_color_reset());
957 print_plugin_comp_cls_opt(stderr
,
958 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
959 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
960 cfg
->cmd_data
.query
.cfg_component
->type
);
961 fprintf(stderr
, "\n");
966 ret
= query(comp_cls
, cfg
->cmd_data
.query
.object
->str
,
967 bt_private_value_as_value(
968 cfg
->cmd_data
.query
.cfg_component
->params
),
969 &results
, &fail_reason
);
974 print_value(stdout
, results
, 0);
978 BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", "
979 "comp-cls-name=\"%s\", comp-cls-type=%d "
980 "object=\"%s\"", fail_reason
,
981 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
982 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
983 cfg
->cmd_data
.query
.cfg_component
->type
,
984 cfg
->cmd_data
.query
.object
->str
);
985 fprintf(stderr
, "%s%sFailed to query info to %s",
986 bt_common_color_bold(),
987 bt_common_color_fg_red(),
988 bt_common_color_reset());
989 print_plugin_comp_cls_opt(stderr
,
990 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
991 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
992 cfg
->cmd_data
.query
.cfg_component
->type
);
993 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
994 bt_common_color_bold(),
995 bt_common_color_fg_red(),
996 cfg
->cmd_data
.query
.object
->str
,
998 bt_common_color_reset());
1002 bt_object_put_ref(comp_cls
);
1003 bt_object_put_ref(results
);
1008 void print_component_class_help(const char *plugin_name
,
1009 struct bt_component_class
*comp_cls
)
1011 const char *comp_class_name
=
1012 bt_component_class_get_name(comp_cls
);
1013 const char *comp_class_description
=
1014 bt_component_class_get_description(comp_cls
);
1015 const char *comp_class_help
=
1016 bt_component_class_get_help(comp_cls
);
1017 enum bt_component_class_type type
=
1018 bt_component_class_get_type(comp_cls
);
1020 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
1022 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1023 bt_common_color_reset(),
1024 comp_class_description
? comp_class_description
: "(None)");
1026 if (comp_class_help
) {
1027 printf("\n%s\n", comp_class_help
);
1032 int cmd_help(struct bt_config
*cfg
)
1035 struct bt_plugin
*plugin
= NULL
;
1036 struct bt_component_class
*needed_comp_cls
= NULL
;
1038 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1040 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
1041 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1042 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
1043 bt_common_color_bold(), bt_common_color_fg_red(),
1044 bt_common_color_fg_blue(),
1045 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1046 bt_common_color_reset());
1051 print_plugin_info(plugin
);
1052 printf(" %sSource component classes%s: %d\n",
1053 bt_common_color_bold(),
1054 bt_common_color_reset(),
1055 (int) bt_plugin_get_source_component_class_count(plugin
));
1056 printf(" %sFilter component classes%s: %d\n",
1057 bt_common_color_bold(),
1058 bt_common_color_reset(),
1059 (int) bt_plugin_get_filter_component_class_count(plugin
));
1060 printf(" %sSink component classes%s: %d\n",
1061 bt_common_color_bold(),
1062 bt_common_color_reset(),
1063 (int) bt_plugin_get_sink_component_class_count(plugin
));
1065 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
1066 /* Plugin help only */
1070 needed_comp_cls
= find_component_class(
1071 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1072 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1073 cfg
->cmd_data
.help
.cfg_component
->type
);
1074 if (!needed_comp_cls
) {
1075 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1076 "comp-cls-name=\"%s\", comp-cls-type=%d",
1077 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1078 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1079 cfg
->cmd_data
.help
.cfg_component
->type
);
1080 fprintf(stderr
, "\n%s%sCannot find component class %s",
1081 bt_common_color_bold(),
1082 bt_common_color_fg_red(),
1083 bt_common_color_reset());
1084 print_plugin_comp_cls_opt(stderr
,
1085 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1086 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1087 cfg
->cmd_data
.help
.cfg_component
->type
);
1088 fprintf(stderr
, "\n");
1094 print_component_class_help(
1095 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1099 bt_object_put_ref(needed_comp_cls
);
1100 bt_object_put_ref(plugin
);
1104 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(struct bt_plugin
*,
1106 typedef struct bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
1109 void cmd_list_plugins_print_component_classes(struct bt_plugin
*plugin
,
1110 const char *cc_type_name
, uint64_t count
,
1111 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
1112 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
1117 printf(" %s%s component classes%s: (none)\n", cc_type_name
,
1118 bt_common_color_bold(),
1119 bt_common_color_reset());
1122 printf(" %s%s component classes%s:\n", cc_type_name
,
1123 bt_common_color_bold(),
1124 bt_common_color_reset());
1127 for (i
= 0; i
< count
; i
++) {
1128 struct bt_component_class
*comp_class
=
1129 spec_comp_cls_borrow_comp_cls_func(
1130 borrow_comp_cls_by_index_func(plugin
, i
));
1131 const char *comp_class_name
=
1132 bt_component_class_get_name(comp_class
);
1133 const char *comp_class_description
=
1134 bt_component_class_get_description(comp_class
);
1135 enum bt_component_class_type type
=
1136 bt_component_class_get_type(comp_class
);
1139 print_plugin_comp_cls_opt(stdout
,
1140 bt_plugin_get_name(plugin
), comp_class_name
,
1143 if (comp_class_description
) {
1144 printf(": %s", comp_class_description
);
1155 int cmd_list_plugins(struct bt_config
*cfg
)
1158 int plugins_count
, component_classes_count
= 0, i
;
1160 printf("From the following plugin paths:\n\n");
1161 print_value(stdout
, bt_private_value_as_value(cfg
->plugin_paths
), 2);
1163 plugins_count
= loaded_plugins
->len
;
1164 if (plugins_count
== 0) {
1165 printf("No plugins found.\n");
1169 for (i
= 0; i
< plugins_count
; i
++) {
1170 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1172 component_classes_count
+=
1173 bt_plugin_get_source_component_class_count(plugin
) +
1174 bt_plugin_get_filter_component_class_count(plugin
) +
1175 bt_plugin_get_sink_component_class_count(plugin
);
1178 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1179 bt_common_color_bold(),
1180 component_classes_count
,
1181 bt_common_color_reset(),
1182 bt_common_color_bold(),
1184 bt_common_color_reset());
1186 for (i
= 0; i
< plugins_count
; i
++) {
1187 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1190 print_plugin_info(plugin
);
1191 cmd_list_plugins_print_component_classes(plugin
, "Source",
1192 bt_plugin_get_source_component_class_count(plugin
),
1193 (plugin_borrow_comp_cls_by_index_func_t
)
1194 bt_plugin_borrow_source_component_class_by_name
,
1195 (spec_comp_cls_borrow_comp_cls_func_t
)
1196 bt_component_class_source_as_component_class
);
1197 cmd_list_plugins_print_component_classes(plugin
, "Filter",
1198 bt_plugin_get_filter_component_class_count(plugin
),
1199 (plugin_borrow_comp_cls_by_index_func_t
)
1200 bt_plugin_borrow_filter_component_class_by_name
,
1201 (spec_comp_cls_borrow_comp_cls_func_t
)
1202 bt_component_class_filter_as_component_class
);
1203 cmd_list_plugins_print_component_classes(plugin
, "Sink",
1204 bt_plugin_get_sink_component_class_count(plugin
),
1205 (plugin_borrow_comp_cls_by_index_func_t
)
1206 bt_plugin_borrow_sink_component_class_by_name
,
1207 (spec_comp_cls_borrow_comp_cls_func_t
)
1208 bt_component_class_sink_as_component_class
);
1216 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1219 struct bt_component_class
*comp_cls
= NULL
;
1220 struct bt_value
*results
= NULL
;
1221 struct bt_private_value
*params
= NULL
;
1222 struct bt_value
*map
= NULL
;
1223 struct bt_value
*v
= NULL
;
1224 static const char * const plugin_name
= "ctf";
1225 static const char * const comp_cls_name
= "lttng-live";
1226 static const enum bt_component_class_type comp_cls_type
=
1227 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1228 int64_t array_size
, i
;
1229 const char *fail_reason
= NULL
;
1230 FILE *out_stream
= stdout
;
1232 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1233 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1236 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1237 "comp-cls-name=\"%s\", comp-cls-type=%d",
1238 plugin_name
, comp_cls_name
,
1239 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1240 fprintf(stderr
, "%s%sCannot find component class %s",
1241 bt_common_color_bold(),
1242 bt_common_color_fg_red(),
1243 bt_common_color_reset());
1244 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1245 comp_cls_name
, comp_cls_type
);
1246 fprintf(stderr
, "\n");
1250 params
= bt_private_value_map_create();
1255 ret
= bt_private_value_map_insert_string_entry(params
, "url",
1256 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1261 ret
= query(comp_cls
, "sessions", bt_private_value_as_value(params
),
1262 &results
, &fail_reason
);
1269 if (!bt_value_is_array(results
)) {
1270 BT_LOGE_STR("Expecting an array for sessions query.");
1271 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1272 bt_common_color_bold(),
1273 bt_common_color_fg_red(),
1274 bt_common_color_reset());
1278 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1280 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1284 BT_LOGE_ERRNO("Cannot open file for writing",
1286 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1291 array_size
= bt_value_array_get_size(results
);
1292 for (i
= 0; i
< array_size
; i
++) {
1293 const char *url_text
;
1294 int64_t timer_us
, streams
, clients
;
1296 map
= bt_value_array_borrow_element_by_index(results
, i
);
1298 BT_LOGE_STR("Unexpected empty array entry.");
1301 if (!bt_value_is_map(map
)) {
1302 BT_LOGE_STR("Unexpected entry type.");
1306 v
= bt_value_map_borrow_entry_value(map
, "url");
1308 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1311 url_text
= bt_value_string_get(v
);
1312 fprintf(out_stream
, "%s", url_text
);
1313 v
= bt_value_map_borrow_entry_value(map
, "timer-us");
1315 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1318 timer_us
= bt_value_integer_get(v
);
1319 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1320 v
= bt_value_map_borrow_entry_value(map
, "stream-count");
1322 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1325 streams
= bt_value_integer_get(v
);
1326 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1327 v
= bt_value_map_borrow_entry_value(map
, "client-count");
1329 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1332 clients
= bt_value_integer_get(v
);
1333 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1339 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1340 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1341 bt_common_color_bold(),
1342 bt_common_color_fg_red(),
1344 bt_common_color_reset());
1350 bt_object_put_ref(results
);
1351 bt_object_put_ref(params
);
1352 bt_object_put_ref(comp_cls
);
1354 if (out_stream
&& out_stream
!= stdout
) {
1355 int fclose_ret
= fclose(out_stream
);
1358 BT_LOGE_ERRNO("Cannot close file stream",
1360 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1368 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1371 struct bt_component_class
*comp_cls
= NULL
;
1372 struct bt_value
*results
= NULL
;
1373 struct bt_private_value
*params
= NULL
;
1374 struct bt_value
*metadata_text_value
= NULL
;
1375 const char *metadata_text
= NULL
;
1376 static const char * const plugin_name
= "ctf";
1377 static const char * const comp_cls_name
= "fs";
1378 static const enum bt_component_class_type comp_cls_type
=
1379 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1380 const char *fail_reason
= NULL
;
1381 FILE *out_stream
= stdout
;
1383 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1384 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1387 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1388 "comp-cls-name=\"%s\", comp-cls-type=%d",
1389 plugin_name
, comp_cls_name
,
1390 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1391 fprintf(stderr
, "%s%sCannot find component class %s",
1392 bt_common_color_bold(),
1393 bt_common_color_fg_red(),
1394 bt_common_color_reset());
1395 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1396 comp_cls_name
, comp_cls_type
);
1397 fprintf(stderr
, "\n");
1402 params
= bt_private_value_map_create();
1408 ret
= bt_private_value_map_insert_string_entry(params
, "path",
1409 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1415 ret
= query(comp_cls
, "metadata-info",
1416 bt_private_value_as_value(params
), &results
, &fail_reason
);
1421 metadata_text_value
= bt_value_map_borrow_entry_value(results
, "text");
1422 if (!metadata_text_value
) {
1423 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1428 metadata_text
= bt_value_string_get(metadata_text_value
);
1430 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1432 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1436 BT_LOGE_ERRNO("Cannot open file for writing",
1438 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1443 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1445 BT_LOGE("Cannot write whole metadata text to output stream: "
1453 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1454 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1455 bt_common_color_bold(),
1456 bt_common_color_fg_red(),
1458 bt_common_color_reset());
1461 bt_object_put_ref(results
);
1462 bt_object_put_ref(params
);
1463 bt_object_put_ref(comp_cls
);
1465 if (out_stream
&& out_stream
!= stdout
) {
1466 int fclose_ret
= fclose(out_stream
);
1469 BT_LOGE_ERRNO("Cannot close file stream",
1471 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1479 char *instance_name
;
1483 struct trace_range
{
1484 uint64_t intersection_range_begin_ns
;
1485 uint64_t intersection_range_end_ns
;
1489 guint
port_id_hash(gconstpointer v
)
1491 const struct port_id
*id
= v
;
1493 BT_ASSERT(id
->instance_name
);
1494 BT_ASSERT(id
->port_name
);
1496 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1500 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1502 const struct port_id
*id1
= v1
;
1503 const struct port_id
*id2
= v2
;
1505 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1506 !strcmp(id1
->port_name
, id2
->port_name
);
1510 void port_id_destroy(gpointer data
)
1512 struct port_id
*id
= data
;
1514 free(id
->instance_name
);
1515 free(id
->port_name
);
1520 void trace_range_destroy(gpointer data
)
1525 struct cmd_run_ctx
{
1527 GHashTable
*src_components
;
1530 GHashTable
*flt_components
;
1533 GHashTable
*sink_components
;
1536 struct bt_private_graph
*graph
;
1539 struct bt_config
*cfg
;
1543 bool stream_intersection_mode
;
1546 * Association of struct port_id -> struct trace_range.
1548 GHashTable
*intersections
;
1551 /* Returns a timestamp of the form "(-)s.ns" */
1553 char *s_from_ns(int64_t ns
)
1558 int64_t ts_sec_abs
, ts_nsec_abs
;
1559 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1560 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1562 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1563 is_negative
= false;
1564 ts_sec_abs
= ts_sec
;
1565 ts_nsec_abs
= ts_nsec
;
1566 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1567 is_negative
= false;
1568 ts_sec_abs
= ts_sec
- 1;
1569 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1570 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1572 ts_sec_abs
= ts_sec
;
1573 ts_nsec_abs
= -ts_nsec
;
1574 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1576 ts_sec_abs
= -(ts_sec
+ 1);
1577 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1578 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1580 ts_sec_abs
= -ts_sec
;
1581 ts_nsec_abs
= ts_nsec
;
1582 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1584 ts_sec_abs
= -ts_sec
;
1585 ts_nsec_abs
= -ts_nsec
;
1588 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1589 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1597 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1598 struct cmd_run_ctx
*ctx
,
1599 struct bt_component
*upstream_comp
,
1600 struct bt_port_output
*out_upstream_port
,
1601 struct bt_config_connection
*cfg_conn
)
1603 typedef uint64_t (*input_port_count_func_t
)(void *);
1604 typedef struct bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1606 struct bt_port
*upstream_port
=
1607 bt_port_output_as_port(out_upstream_port
);
1610 GQuark downstreamp_comp_name_quark
;
1611 void *downstream_comp
;
1612 uint64_t downstream_port_count
;
1614 input_port_count_func_t port_count_fn
;
1615 borrow_input_port_by_index_func_t port_by_index_fn
;
1616 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1617 bool insert_trimmer
= false;
1618 struct bt_private_value
*trimmer_params
= NULL
;
1619 char *intersection_begin
= NULL
;
1620 char *intersection_end
= NULL
;
1621 struct bt_component_filter
*trimmer
= NULL
;
1622 struct bt_component_class_filter
*trimmer_class
= NULL
;
1623 struct bt_port_input
*trimmer_input
= NULL
;
1624 struct bt_port_output
*trimmer_output
= NULL
;
1626 if (ctx
->intersections
&&
1627 bt_component_get_class_type(upstream_comp
) ==
1628 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1629 struct trace_range
*range
;
1630 struct port_id port_id
= {
1631 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1632 .port_name
= (char *) bt_port_get_name(upstream_port
)
1635 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1639 range
= (struct trace_range
*) g_hash_table_lookup(
1640 ctx
->intersections
, &port_id
);
1642 enum bt_value_status status
;
1644 intersection_begin
= s_from_ns(
1645 range
->intersection_range_begin_ns
);
1646 intersection_end
= s_from_ns(
1647 range
->intersection_range_end_ns
);
1648 if (!intersection_begin
|| !intersection_end
) {
1649 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1653 insert_trimmer
= true;
1654 trimmer_params
= bt_private_value_map_create();
1655 if (!trimmer_params
) {
1659 status
= bt_private_value_map_insert_string_entry(
1660 trimmer_params
, "begin", intersection_begin
);
1661 if (status
!= BT_VALUE_STATUS_OK
) {
1664 status
= bt_private_value_map_insert_string_entry(
1666 "end", intersection_end
);
1667 if (status
!= BT_VALUE_STATUS_OK
) {
1672 trimmer_class
= find_filter_component_class("utils", "trimmer");
1673 if (!trimmer_class
) {
1678 BT_LOGI("Connecting upstream port to the next available downstream port: "
1679 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1680 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1681 upstream_port
, bt_port_get_name(upstream_port
),
1682 cfg_conn
->downstream_comp_name
->str
,
1683 cfg_conn
->arg
->str
);
1684 downstreamp_comp_name_quark
= g_quark_from_string(
1685 cfg_conn
->downstream_comp_name
->str
);
1686 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1687 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1688 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1689 port_count_fn
= (input_port_count_func_t
)
1690 bt_component_filter_get_input_port_count
;
1691 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1692 bt_component_filter_borrow_input_port_by_index
;
1694 if (!downstream_comp
) {
1695 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1696 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1697 port_count_fn
= (input_port_count_func_t
)
1698 bt_component_sink_get_input_port_count
;
1699 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1700 bt_component_sink_borrow_input_port_by_index
;
1703 if (!downstream_comp
) {
1704 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1705 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1706 cfg_conn
->arg
->str
);
1707 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1708 cfg_conn
->arg
->str
);
1712 downstream_port_count
= port_count_fn(downstream_comp
);
1713 BT_ASSERT(downstream_port_count
>= 0);
1715 for (i
= 0; i
< downstream_port_count
; i
++) {
1716 struct bt_port_input
*in_downstream_port
=
1717 port_by_index_fn(downstream_comp
, i
);
1718 struct bt_port
*downstream_port
=
1719 bt_port_input_as_port(in_downstream_port
);
1720 const char *upstream_port_name
;
1721 const char *downstream_port_name
;
1723 BT_ASSERT(downstream_port
);
1725 /* Skip port if it's already connected. */
1726 if (bt_port_is_connected(downstream_port
)) {
1727 BT_LOGD("Skipping downstream port: already connected: "
1728 "port-addr=%p, port-name=\"%s\"",
1730 bt_port_get_name(downstream_port
));
1734 downstream_port_name
= bt_port_get_name(downstream_port
);
1735 BT_ASSERT(downstream_port_name
);
1736 upstream_port_name
= bt_port_get_name(upstream_port
);
1737 BT_ASSERT(upstream_port_name
);
1739 if (!bt_common_star_glob_match(
1740 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1741 downstream_port_name
, SIZE_MAX
)) {
1745 if (insert_trimmer
) {
1747 * In order to insert the trimmer between the
1748 * two components that were being connected, we
1749 * create a connection configuration entry which
1750 * describes a connection from the trimmer's
1751 * output to the original input that was being
1754 * Hence, the creation of the trimmer will cause
1755 * the graph "new port" listener to establish
1756 * all downstream connections as its output port
1757 * is connected. We will then establish the
1758 * connection between the original upstream
1759 * source and the trimmer.
1761 char *trimmer_name
= NULL
;
1762 enum bt_graph_status graph_status
;
1764 ret
= asprintf(&trimmer_name
,
1765 "stream-intersection-trimmer-%s",
1766 upstream_port_name
);
1772 ctx
->connect_ports
= false;
1773 graph_status
= bt_private_graph_add_filter_component(
1774 ctx
->graph
, trimmer_class
, trimmer_name
,
1775 bt_private_value_as_value(trimmer_params
),
1778 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1784 bt_component_filter_borrow_input_port_by_index(
1786 if (!trimmer_input
) {
1790 bt_component_filter_borrow_output_port_by_index(
1792 if (!trimmer_output
) {
1797 * Replace the current downstream port by the trimmer's
1800 in_downstream_port
= trimmer_input
;
1802 bt_port_input_as_port(in_downstream_port
);
1803 downstream_port_name
= bt_port_get_name(
1805 BT_ASSERT(downstream_port_name
);
1808 /* We have a winner! */
1809 status
= bt_private_graph_connect_ports(ctx
->graph
,
1810 out_upstream_port
, in_downstream_port
, NULL
);
1811 downstream_port
= NULL
;
1813 case BT_GRAPH_STATUS_OK
:
1815 case BT_GRAPH_STATUS_CANCELED
:
1816 BT_LOGI_STR("Graph was canceled by user.");
1817 status
= BT_GRAPH_STATUS_OK
;
1819 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1820 BT_LOGE("A component refused a connection to one of its ports: "
1821 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1822 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1823 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1824 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1826 upstream_comp
, bt_component_get_name(upstream_comp
),
1827 upstream_port
, bt_port_get_name(upstream_port
),
1828 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1829 downstream_port
, downstream_port_name
,
1830 cfg_conn
->arg
->str
);
1832 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1833 bt_port_get_name(upstream_port
),
1834 downstream_port_name
,
1835 cfg_conn
->arg
->str
);
1838 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1839 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1840 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1841 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1842 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1844 upstream_comp
, bt_component_get_name(upstream_comp
),
1845 upstream_port
, bt_port_get_name(upstream_port
),
1846 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1847 downstream_port
, downstream_port_name
,
1848 cfg_conn
->arg
->str
);
1850 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1851 bt_port_get_name(upstream_port
),
1852 downstream_port_name
,
1853 cfg_conn
->arg
->str
);
1857 BT_LOGI("Connected component ports: "
1858 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1859 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1860 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1861 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1863 upstream_comp
, bt_component_get_name(upstream_comp
),
1864 upstream_port
, bt_port_get_name(upstream_port
),
1865 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1866 downstream_port
, downstream_port_name
,
1867 cfg_conn
->arg
->str
);
1869 if (insert_trimmer
) {
1871 * The first connection, from the source to the trimmer,
1872 * has been done. We now connect the trimmer to the
1873 * original downstream port.
1875 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1877 bt_component_filter_as_component(trimmer
),
1878 trimmer_output
, cfg_conn
);
1882 ctx
->connect_ports
= true;
1886 * We found a matching downstream port: the search is
1892 /* No downstream port found */
1893 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1894 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1895 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1896 upstream_port
, bt_port_get_name(upstream_port
),
1897 cfg_conn
->downstream_comp_name
->str
,
1898 cfg_conn
->arg
->str
);
1900 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1901 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1907 free(intersection_begin
);
1908 free(intersection_end
);
1909 BT_OBJECT_PUT_REF_AND_RESET(trimmer_params
);
1910 BT_OBJECT_PUT_REF_AND_RESET(trimmer_class
);
1911 BT_OBJECT_PUT_REF_AND_RESET(trimmer
);
1916 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1917 struct bt_port_output
*upstream_port
)
1920 const char *upstream_port_name
;
1921 const char *upstream_comp_name
;
1922 struct bt_component
*upstream_comp
= NULL
;
1926 BT_ASSERT(upstream_port
);
1927 upstream_port_name
= bt_port_get_name(
1928 bt_port_output_as_port(upstream_port
));
1929 BT_ASSERT(upstream_port_name
);
1930 upstream_comp
= bt_port_borrow_component(
1931 bt_port_output_as_port(upstream_port
));
1932 if (!upstream_comp
) {
1933 BT_LOGW("Upstream port to connect is not part of a component: "
1934 "port-addr=%p, port-name=\"%s\"",
1935 upstream_port
, upstream_port_name
);
1940 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1941 BT_ASSERT(upstream_comp_name
);
1942 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1943 "port-addr=%p, port-name=\"%s\"",
1944 upstream_comp
, upstream_comp_name
,
1945 upstream_port
, upstream_port_name
);
1947 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1948 struct bt_config_connection
*cfg_conn
=
1950 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1952 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1953 upstream_comp_name
)) {
1957 if (!bt_common_star_glob_match(
1958 cfg_conn
->upstream_port_glob
->str
,
1959 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1963 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1964 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1966 BT_LOGE("Cannot connect upstream port: "
1967 "port-addr=%p, port-name=\"%s\"",
1969 upstream_port_name
);
1971 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1974 cfg_conn
->arg
->str
);
1980 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1981 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1982 upstream_port_name
);
1984 "Cannot create connection: upstream port `%s` does not match any connection\n",
1985 upstream_port_name
);
1995 void graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1996 struct bt_port_output
*out_port
)
1998 struct bt_component
*comp
;
1999 struct bt_port
*port
= bt_port_output_as_port(out_port
);
2001 comp
= bt_port_borrow_component(port
);
2002 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
2003 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
2004 comp
, comp
? bt_component_get_name(comp
) : "",
2005 port
, bt_port_get_name(port
));
2007 if (!ctx
->connect_ports
) {
2012 BT_LOGW_STR("Port has no component.");
2016 if (bt_port_is_connected(port
)) {
2017 BT_LOGW_STR("Port is already connected.");
2021 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
2022 BT_LOGF_STR("Cannot connect upstream port.");
2023 fprintf(stderr
, "Added port could not be connected: aborting\n");
2032 void graph_source_output_port_added_listener(
2033 struct bt_component_source
*component
,
2034 struct bt_port_output
*port
, void *data
)
2036 graph_output_port_added_listener(data
, port
);
2040 void graph_filter_output_port_added_listener(
2041 struct bt_component_filter
*component
,
2042 struct bt_port_output
*port
, void *data
)
2044 graph_output_port_added_listener(data
, port
);
2048 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
2054 if (ctx
->src_components
) {
2055 g_hash_table_destroy(ctx
->src_components
);
2056 ctx
->src_components
= NULL
;
2059 if (ctx
->flt_components
) {
2060 g_hash_table_destroy(ctx
->flt_components
);
2061 ctx
->flt_components
= NULL
;
2064 if (ctx
->sink_components
) {
2065 g_hash_table_destroy(ctx
->sink_components
);
2066 ctx
->sink_components
= NULL
;
2069 if (ctx
->intersections
) {
2070 g_hash_table_destroy(ctx
->intersections
);
2071 ctx
->intersections
= NULL
;
2074 BT_OBJECT_PUT_REF_AND_RESET(ctx
->graph
);
2080 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2083 enum bt_graph_status status
;
2086 ctx
->connect_ports
= false;
2087 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
2088 g_direct_equal
, NULL
, bt_object_put_ref
);
2089 if (!ctx
->src_components
) {
2093 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
2094 g_direct_equal
, NULL
, bt_object_put_ref
);
2095 if (!ctx
->flt_components
) {
2099 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
2100 g_direct_equal
, NULL
, bt_object_put_ref
);
2101 if (!ctx
->sink_components
) {
2105 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2106 ctx
->stream_intersection_mode
= true;
2107 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2108 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2109 if (!ctx
->intersections
) {
2114 ctx
->graph
= bt_private_graph_create();
2119 the_graph
= ctx
->graph
;
2120 status
= bt_private_graph_add_source_component_output_port_added_listener(
2121 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
2123 if (status
!= BT_GRAPH_STATUS_OK
) {
2124 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2128 status
= bt_private_graph_add_filter_component_output_port_added_listener(
2129 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
2131 if (status
!= BT_GRAPH_STATUS_OK
) {
2132 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2139 cmd_run_ctx_destroy(ctx
);
2147 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2148 struct bt_config_component
*cfg_comp
,
2149 struct bt_component_class_source
*src_comp_cls
)
2153 int64_t trace_count
;
2154 enum bt_value_status value_status
;
2155 const char *path
= NULL
;
2156 struct bt_value
*component_path_value
= NULL
;
2157 struct bt_private_value
*query_params
= NULL
;
2158 struct bt_value
*query_result
= NULL
;
2159 struct bt_value
*trace_info
= NULL
;
2160 struct bt_value
*intersection_range
= NULL
;
2161 struct bt_value
*intersection_begin
= NULL
;
2162 struct bt_value
*intersection_end
= NULL
;
2163 struct bt_value
*stream_path_value
= NULL
;
2164 struct bt_value
*stream_paths
= NULL
;
2165 struct bt_value
*stream_infos
= NULL
;
2166 struct bt_value
*stream_info
= NULL
;
2167 struct port_id
*port_id
= NULL
;
2168 struct trace_range
*trace_range
= NULL
;
2169 const char *fail_reason
= NULL
;
2170 struct bt_component_class
*comp_cls
=
2171 bt_component_class_source_as_component_class(src_comp_cls
);
2173 component_path_value
= bt_value_map_borrow_entry_value(
2174 bt_private_value_as_value(cfg_comp
->params
),
2176 if (component_path_value
&& !bt_value_is_string(component_path_value
)) {
2177 BT_LOGD("Cannot get path parameter: component-name=%s",
2178 cfg_comp
->instance_name
->str
);
2183 path
= bt_value_string_get(component_path_value
);
2184 query_params
= bt_private_value_map_create();
2185 if (!query_params
) {
2186 BT_LOGE_STR("Cannot create query parameters.");
2191 value_status
= bt_private_value_map_insert_entry(query_params
, "path",
2192 component_path_value
);
2193 if (value_status
!= BT_VALUE_STATUS_OK
) {
2194 BT_LOGE_STR("Cannot insert path parameter in query parameter map.");
2199 ret
= query(comp_cls
, "trace-info",
2200 bt_private_value_as_value(query_params
), &query_result
,
2203 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2204 "comp-class-name=\"%s\"", fail_reason
,
2205 bt_component_class_get_name(comp_cls
));
2210 BT_ASSERT(query_result
);
2212 if (!bt_value_is_array(query_result
)) {
2213 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2214 "component-class-name=%s",
2215 bt_component_class_get_name(comp_cls
));
2220 trace_count
= bt_value_array_get_size(query_result
);
2221 if (trace_count
< 0) {
2226 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2228 uint64_t stream_idx
;
2229 int64_t stream_count
;
2231 trace_info
= bt_value_array_borrow_element_by_index(
2232 query_result
, trace_idx
);
2233 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2235 BT_LOGD_STR("Cannot retrieve trace from query result.");
2239 intersection_range
= bt_value_map_borrow_entry_value(trace_info
,
2240 "intersection-range-ns");
2241 if (!intersection_range
) {
2243 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2247 intersection_begin
= bt_value_map_borrow_entry_value(
2248 intersection_range
, "begin");
2249 if (!intersection_begin
) {
2251 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2255 intersection_end
= bt_value_map_borrow_entry_value(
2256 intersection_range
, "end");
2257 if (!intersection_end
) {
2259 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2263 begin
= bt_value_integer_get(intersection_begin
);
2264 end
= bt_value_integer_get(intersection_end
);
2266 if (begin
< 0 || end
< 0 || end
< begin
) {
2267 BT_LOGW("Invalid trace stream intersection values: "
2268 "intersection-range-ns:begin=%" PRId64
2269 ", intersection-range-ns:end=%" PRId64
,
2275 stream_infos
= bt_value_map_borrow_entry_value(trace_info
,
2277 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2279 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2283 stream_count
= bt_value_array_get_size(stream_infos
);
2284 if (stream_count
< 0) {
2292 * The first path of a stream's "paths" is currently used to
2293 * associate streams/ports to a given trace intersection.
2295 * This is a fragile hack as it relies on the port names
2296 * being set to the various streams path.
2298 * A stream name should be introduced as part of the trace-info
2301 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2302 const char *stream_path
;
2304 port_id
= g_new0(struct port_id
, 1);
2307 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2310 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2311 if (!port_id
->instance_name
) {
2313 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2317 trace_range
= g_new0(struct trace_range
, 1);
2320 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2323 trace_range
->intersection_range_begin_ns
= begin
;
2324 trace_range
->intersection_range_end_ns
= end
;
2326 stream_info
= bt_value_array_borrow_element_by_index(
2327 stream_infos
, stream_idx
);
2328 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2330 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2334 stream_paths
= bt_value_map_borrow_entry_value(
2335 stream_info
, "paths");
2336 if (!stream_paths
|| !bt_value_is_array(stream_paths
)) {
2338 BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
2343 bt_value_array_borrow_element_by_index(
2345 if (!stream_path_value
||
2346 !bt_value_is_string(stream_path_value
)) {
2348 BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
2352 stream_path
= bt_value_string_get(stream_path_value
);
2353 port_id
->port_name
= strdup(stream_path
);
2354 if (!port_id
->port_name
) {
2356 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2360 BT_LOGD("Inserting stream intersection ");
2362 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2372 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2373 bt_common_color_bold(),
2374 bt_common_color_fg_yellow(),
2375 path
? path
: "(unknown)",
2376 bt_common_color_reset());
2378 bt_object_put_ref(query_params
);
2379 bt_object_put_ref(query_result
);
2381 g_free(trace_range
);
2386 int cmd_run_ctx_create_components_from_config_components(
2387 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2390 void *comp_cls
= NULL
;
2394 for (i
= 0; i
< cfg_components
->len
; i
++) {
2395 struct bt_config_component
*cfg_comp
=
2396 g_ptr_array_index(cfg_components
, i
);
2399 switch (cfg_comp
->type
) {
2400 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2401 comp_cls
= find_source_component_class(
2402 cfg_comp
->plugin_name
->str
,
2403 cfg_comp
->comp_cls_name
->str
);
2405 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2406 comp_cls
= find_filter_component_class(
2407 cfg_comp
->plugin_name
->str
,
2408 cfg_comp
->comp_cls_name
->str
);
2410 case BT_COMPONENT_CLASS_TYPE_SINK
:
2411 comp_cls
= find_sink_component_class(
2412 cfg_comp
->plugin_name
->str
,
2413 cfg_comp
->comp_cls_name
->str
);
2420 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2421 "comp-cls-name=\"%s\", comp-cls-type=%d",
2422 cfg_comp
->plugin_name
->str
,
2423 cfg_comp
->comp_cls_name
->str
,
2425 fprintf(stderr
, "%s%sCannot find component class %s",
2426 bt_common_color_bold(),
2427 bt_common_color_fg_red(),
2428 bt_common_color_reset());
2429 print_plugin_comp_cls_opt(stderr
,
2430 cfg_comp
->plugin_name
->str
,
2431 cfg_comp
->comp_cls_name
->str
,
2433 fprintf(stderr
, "\n");
2437 switch (cfg_comp
->type
) {
2438 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2439 ret
= bt_private_graph_add_source_component(ctx
->graph
,
2440 comp_cls
, cfg_comp
->instance_name
->str
,
2441 bt_private_value_as_value(cfg_comp
->params
),
2444 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2445 ret
= bt_private_graph_add_filter_component(ctx
->graph
,
2446 comp_cls
, cfg_comp
->instance_name
->str
,
2447 bt_private_value_as_value(cfg_comp
->params
),
2450 case BT_COMPONENT_CLASS_TYPE_SINK
:
2451 ret
= bt_private_graph_add_sink_component(ctx
->graph
,
2452 comp_cls
, cfg_comp
->instance_name
->str
,
2453 bt_private_value_as_value(cfg_comp
->params
),
2461 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2462 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2464 cfg_comp
->plugin_name
->str
,
2465 cfg_comp
->comp_cls_name
->str
,
2466 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2467 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2468 bt_common_color_bold(),
2469 bt_common_color_fg_red(),
2470 cfg_comp
->instance_name
->str
,
2471 bt_common_color_reset());
2475 if (ctx
->stream_intersection_mode
&&
2476 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2477 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2483 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2484 comp
, cfg_comp
->instance_name
->str
);
2485 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2486 BT_ASSERT(quark
> 0);
2488 switch (cfg_comp
->type
) {
2489 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2490 g_hash_table_insert(ctx
->src_components
,
2491 GUINT_TO_POINTER(quark
), comp
);
2493 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2494 g_hash_table_insert(ctx
->flt_components
,
2495 GUINT_TO_POINTER(quark
), comp
);
2497 case BT_COMPONENT_CLASS_TYPE_SINK
:
2498 g_hash_table_insert(ctx
->sink_components
,
2499 GUINT_TO_POINTER(quark
), comp
);
2506 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2515 bt_object_put_ref(comp
);
2516 bt_object_put_ref(comp_cls
);
2521 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2526 * Make sure that, during this phase, our graph's "port added"
2527 * listener does not connect ports while we are creating the
2528 * components because we have a special, initial phase for
2531 ctx
->connect_ports
= false;
2533 ret
= cmd_run_ctx_create_components_from_config_components(
2534 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2540 ret
= cmd_run_ctx_create_components_from_config_components(
2541 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2547 ret
= cmd_run_ctx_create_components_from_config_components(
2548 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2558 typedef uint64_t (*output_port_count_func_t
)(void *);
2559 typedef struct bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2563 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2564 void *comp
, output_port_count_func_t port_count_fn
,
2565 borrow_output_port_by_index_func_t port_by_index_fn
)
2571 count
= port_count_fn(comp
);
2572 BT_ASSERT(count
>= 0);
2574 for (i
= 0; i
< count
; i
++) {
2575 struct bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2577 BT_ASSERT(upstream_port
);
2578 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2589 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2592 GHashTableIter iter
;
2593 gpointer g_name_quark
, g_comp
;
2595 ctx
->connect_ports
= true;
2596 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2598 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2599 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2600 (output_port_count_func_t
)
2601 bt_component_source_get_output_port_count
,
2602 (borrow_output_port_by_index_func_t
)
2603 bt_component_source_borrow_output_port_by_index
);
2609 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2611 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2612 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2613 (output_port_count_func_t
)
2614 bt_component_filter_get_output_port_count
,
2615 (borrow_output_port_by_index_func_t
)
2616 bt_component_filter_borrow_output_port_by_index
);
2627 const char *bt_graph_status_str(enum bt_graph_status status
)
2630 case BT_GRAPH_STATUS_OK
:
2631 return "BT_GRAPH_STATUS_OK";
2632 case BT_GRAPH_STATUS_END
:
2633 return "BT_GRAPH_STATUS_END";
2634 case BT_GRAPH_STATUS_AGAIN
:
2635 return "BT_GRAPH_STATUS_AGAIN";
2636 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
2637 return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION";
2638 case BT_GRAPH_STATUS_CANCELED
:
2639 return "BT_GRAPH_STATUS_CANCELED";
2640 case BT_GRAPH_STATUS_ERROR
:
2641 return "BT_GRAPH_STATUS_ERROR";
2642 case BT_GRAPH_STATUS_NO_SINK
:
2643 return "BT_GRAPH_STATUS_NO_SINK";
2644 case BT_GRAPH_STATUS_NOMEM
:
2645 return "BT_GRAPH_STATUS_NOMEM";
2652 int cmd_run(struct bt_config
*cfg
)
2655 struct cmd_run_ctx ctx
= { 0 };
2657 /* Initialize the command's context and the graph object */
2658 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2659 BT_LOGE_STR("Cannot initialize the command's context.");
2660 fprintf(stderr
, "Cannot initialize the command's context\n");
2665 BT_LOGI_STR("Canceled by user before creating components.");
2669 BT_LOGI_STR("Creating components.");
2671 /* Create the requested component instances */
2672 if (cmd_run_ctx_create_components(&ctx
)) {
2673 BT_LOGE_STR("Cannot create components.");
2674 fprintf(stderr
, "Cannot create components\n");
2679 BT_LOGI_STR("Canceled by user before connecting components.");
2683 BT_LOGI_STR("Connecting components.");
2685 /* Connect the initially visible component ports */
2686 if (cmd_run_ctx_connect_ports(&ctx
)) {
2687 BT_LOGE_STR("Cannot connect initial component ports.");
2688 fprintf(stderr
, "Cannot connect initial component ports\n");
2693 BT_LOGI_STR("Canceled by user before running the graph.");
2697 BT_LOGI_STR("Running the graph.");
2701 enum bt_graph_status graph_status
= bt_private_graph_run(ctx
.graph
);
2704 * Reset console in case something messed with console
2705 * codes during the graph's execution.
2707 printf("%s", bt_common_color_reset());
2709 fprintf(stderr
, "%s", bt_common_color_reset());
2710 BT_LOGV("bt_private_graph_run() returned: status=%s",
2711 bt_graph_status_str(graph_status
));
2713 switch (graph_status
) {
2714 case BT_GRAPH_STATUS_OK
:
2716 case BT_GRAPH_STATUS_CANCELED
:
2717 BT_LOGI_STR("Graph was canceled by user.");
2719 case BT_GRAPH_STATUS_AGAIN
:
2720 if (bt_graph_is_canceled(
2721 bt_private_graph_as_graph(ctx
.graph
))) {
2722 BT_LOGI_STR("Graph was canceled by user.");
2726 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2727 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2729 cfg
->cmd_data
.run
.retry_duration_us
);
2731 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2732 if (bt_graph_is_canceled(
2733 bt_private_graph_as_graph(ctx
.graph
))) {
2734 BT_LOGI_STR("Graph was canceled by user.");
2740 case BT_GRAPH_STATUS_END
:
2743 BT_LOGE_STR("Graph failed to complete successfully");
2744 fprintf(stderr
, "Graph failed to complete successfully\n");
2757 cmd_run_ctx_destroy(&ctx
);
2762 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2764 const char *env_clash
;
2766 if (!cfg
->command_name
) {
2770 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2771 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2775 if (g_file_test(cfg
->command_name
,
2776 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2777 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2779 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2781 fprintf(stderr
, "\n");
2782 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
2788 void init_log_level(void)
2790 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2794 void set_auto_log_levels(struct bt_config
*cfg
)
2796 const char **env_var_name
;
2799 * Override the configuration's default log level if
2800 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2801 * are found for backward compatibility with legacy Babetrace 1.
2803 if (getenv("BABELTRACE_DEBUG") &&
2804 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2805 cfg
->log_level
= 'V';
2806 } else if (getenv("BABELTRACE_VERBOSE") &&
2807 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2808 cfg
->log_level
= 'I';
2812 * Set log levels according to --debug or --verbose. For
2813 * backward compatibility, --debug is more verbose than
2816 * --verbose: INFO log level
2817 * --debug: VERBOSE log level (includes DEBUG, which is
2818 * is less verbose than VERBOSE in the internal
2819 * logging framework)
2821 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2823 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2824 } else if (cfg
->debug
) {
2825 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2828 * Set library's default log level if not
2829 * explicitly specified.
2831 switch (cfg
->log_level
) {
2833 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE
);
2836 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2839 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
2842 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2845 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN
);
2848 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR
);
2851 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL
);
2859 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2861 bt_cli_log_level
= BT_LOG_INFO
;
2862 } else if (cfg
->debug
) {
2863 bt_cli_log_level
= BT_LOG_VERBOSE
;
2866 * Set CLI's default log level if not explicitly
2869 switch (cfg
->log_level
) {
2871 bt_cli_log_level
= BT_LOG_NONE
;
2874 bt_cli_log_level
= BT_LOG_VERBOSE
;
2877 bt_cli_log_level
= BT_LOG_DEBUG
;
2880 bt_cli_log_level
= BT_LOG_INFO
;
2883 bt_cli_log_level
= BT_LOG_WARN
;
2886 bt_cli_log_level
= BT_LOG_ERROR
;
2889 bt_cli_log_level
= BT_LOG_FATAL
;
2897 env_var_name
= log_level_env_var_names
;
2899 while (*env_var_name
) {
2900 if (!getenv(*env_var_name
)) {
2902 g_setenv(*env_var_name
, "I", 1);
2903 } else if (cfg
->debug
) {
2904 g_setenv(*env_var_name
, "V", 1);
2906 char val
[2] = { 0 };
2909 * Set module's default log level if not
2910 * explicitly specified.
2912 val
[0] = cfg
->log_level
;
2913 g_setenv(*env_var_name
, val
, 1);
2921 int main(int argc
, const char **argv
)
2925 struct bt_config
*cfg
;
2928 set_signal_handler();
2930 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2933 /* Quit without errors; typically usage/version */
2935 BT_LOGI_STR("Quitting without errors.");
2940 BT_LOGE("Command-line error: retcode=%d", retcode
);
2945 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2946 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2951 set_auto_log_levels(cfg
);
2954 if (cfg
->command_needs_plugins
) {
2955 ret
= load_all_plugins(
2956 bt_private_value_as_value(cfg
->plugin_paths
));
2958 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2964 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2965 cfg
->command
, cfg
->command_name
);
2967 switch (cfg
->command
) {
2968 case BT_CONFIG_COMMAND_RUN
:
2971 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2972 ret
= cmd_list_plugins(cfg
);
2974 case BT_CONFIG_COMMAND_HELP
:
2975 ret
= cmd_help(cfg
);
2977 case BT_CONFIG_COMMAND_QUERY
:
2978 ret
= cmd_query(cfg
);
2980 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2981 ret
= cmd_print_ctf_metadata(cfg
);
2983 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2984 ret
= cmd_print_lttng_live_sessions(cfg
);
2987 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2991 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2992 cfg
->command
, cfg
->command_name
, ret
);
2993 warn_command_name_and_directory_clash(cfg
);
2994 retcode
= ret
? 1 : 0;
2997 BT_OBJECT_PUT_REF_AND_RESET(cfg
);