2 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
4 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "CLI"
28 #include <babeltrace2/babeltrace.h>
29 #include "common/common.h"
39 #include "babeltrace2-cfg.h"
40 #include "babeltrace2-cfg-cli-args.h"
41 #include "babeltrace2-cfg-cli-args-default.h"
43 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
44 #define ENV_BABELTRACE_CLI_LOG_LEVEL "BABELTRACE_CLI_LOG_LEVEL"
45 #define NSEC_PER_SEC 1000000000LL
48 * Known environment variable names for the log levels of the project's
51 static const char* log_level_env_var_names
[] = {
52 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
53 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
54 "BABELTRACE_SRC_CTF_FS_LOG_LEVEL",
58 /* Application's processing graph (weak) */
59 static bt_graph
*the_graph
;
60 static bt_query_executor
*the_query_executor
;
61 static bool canceled
= false;
63 GPtrArray
*loaded_plugins
;
70 BOOL WINAPI
signal_handler(DWORD signal
) {
72 bt_graph_cancel(the_graph
);
81 void set_signal_handler(void)
83 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
84 BT_LOGE("Failed to set the Ctrl+C handler.");
88 #else /* __MINGW32__ */
91 void signal_handler(int signum
)
93 if (signum
!= SIGINT
) {
98 bt_graph_cancel(the_graph
);
101 if (the_query_executor
) {
102 bt_query_executor_cancel(the_query_executor
);
109 void set_signal_handler(void)
111 struct sigaction new_action
, old_action
;
113 new_action
.sa_handler
= signal_handler
;
114 sigemptyset(&new_action
.sa_mask
);
115 new_action
.sa_flags
= 0;
116 sigaction(SIGINT
, NULL
, &old_action
);
118 if (old_action
.sa_handler
!= SIG_IGN
) {
119 sigaction(SIGINT
, &new_action
, NULL
);
123 #endif /* __MINGW32__ */
126 void init_static_data(void)
128 loaded_plugins
= g_ptr_array_new_with_free_func(
129 (GDestroyNotify
) bt_object_put_ref
);
133 void fini_static_data(void)
135 g_ptr_array_free(loaded_plugins
, TRUE
);
139 int create_the_query_executor(void)
143 the_query_executor
= bt_query_executor_create();
144 if (!the_query_executor
) {
145 BT_LOGE_STR("Cannot create a query executor.");
153 void destroy_the_query_executor(void)
155 BT_QUERY_EXECUTOR_PUT_REF_AND_RESET(the_query_executor
);
159 int query(struct bt_config
*cfg
, const bt_component_class
*comp_cls
,
160 const char *obj
, const bt_value
*params
,
161 const bt_value
**user_result
, const char **fail_reason
)
163 const bt_value
*result
= NULL
;
164 bt_query_executor_status status
;
165 *fail_reason
= "unknown error";
168 BT_ASSERT(fail_reason
);
169 BT_ASSERT(user_result
);
170 ret
= create_the_query_executor();
172 /* create_the_query_executor() logs errors */
177 BT_LOGI("Canceled by user before executing the query: "
178 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
179 "query-obj=\"%s\"", comp_cls
,
180 bt_component_class_get_name(comp_cls
), obj
);
181 *fail_reason
= "canceled by user";
186 status
= bt_query_executor_query(the_query_executor
,
187 comp_cls
, obj
, params
, cfg
->log_level
, &result
);
189 case BT_QUERY_EXECUTOR_STATUS_OK
:
191 case BT_QUERY_EXECUTOR_STATUS_AGAIN
:
193 const uint64_t sleep_time_us
= 100000;
195 /* Wait 100 ms and retry */
196 BT_LOGD("Got BT_QUERY_EXECUTOR_STATUS_AGAIN: sleeping: "
197 "time-us=%" PRIu64
, sleep_time_us
);
199 if (usleep(sleep_time_us
)) {
200 if (bt_query_executor_is_canceled(the_query_executor
)) {
201 BT_LOGI("Query was canceled by user: "
202 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
203 "query-obj=\"%s\"", comp_cls
,
204 bt_component_class_get_name(comp_cls
),
206 *fail_reason
= "canceled by user";
213 case BT_QUERY_EXECUTOR_STATUS_CANCELED
:
214 *fail_reason
= "canceled by user";
216 case BT_QUERY_EXECUTOR_STATUS_ERROR
:
218 case BT_QUERY_EXECUTOR_STATUS_INVALID_OBJECT
:
219 *fail_reason
= "invalid or unknown query object";
221 case BT_QUERY_EXECUTOR_STATUS_INVALID_PARAMS
:
222 *fail_reason
= "invalid query parameters";
224 case BT_QUERY_EXECUTOR_STATUS_UNSUPPORTED
:
225 *fail_reason
= "unsupported action";
227 case BT_QUERY_EXECUTOR_STATUS_NOMEM
:
228 *fail_reason
= "not enough memory";
231 BT_LOGF("Unknown query status: status=%d", status
);
237 *user_result
= result
;
245 destroy_the_query_executor();
246 bt_value_put_ref(result
);
251 const bt_plugin
*find_plugin(const char *name
)
254 const bt_plugin
*plugin
= NULL
;
257 BT_LOGI("Finding plugin: name=\"%s\"", name
);
259 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
260 plugin
= g_ptr_array_index(loaded_plugins
, i
);
262 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
270 BT_LOGI("Found plugin: name=\"%s\", plugin-addr=%p",
273 BT_LOGI("Cannot find plugin: name=\"%s\"", name
);
276 bt_plugin_get_ref(plugin
);
280 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
281 const bt_plugin
*, const char *);
284 const void *find_component_class_from_plugin(const char *plugin_name
,
285 const char *comp_class_name
,
286 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
288 const void *comp_class
= NULL
;
289 const bt_plugin
*plugin
;
291 BT_LOGI("Finding component class: plugin-name=\"%s\", "
292 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
294 plugin
= find_plugin(plugin_name
);
299 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
300 bt_object_get_ref(comp_class
);
301 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
305 BT_LOGI("Found component class: plugin-name=\"%s\", "
306 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
308 BT_LOGI("Cannot find source component class: "
309 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
310 plugin_name
, comp_class_name
);
317 const bt_component_class_source
*find_source_component_class(
318 const char *plugin_name
, const char *comp_class_name
)
320 return (const void *) find_component_class_from_plugin(
321 plugin_name
, comp_class_name
,
322 (plugin_borrow_comp_cls_func_t
)
323 bt_plugin_borrow_source_component_class_by_name_const
);
327 const bt_component_class_filter
*find_filter_component_class(
328 const char *plugin_name
, const char *comp_class_name
)
330 return (const void *) find_component_class_from_plugin(
331 plugin_name
, comp_class_name
,
332 (plugin_borrow_comp_cls_func_t
)
333 bt_plugin_borrow_filter_component_class_by_name_const
);
337 const bt_component_class_sink
*find_sink_component_class(
338 const char *plugin_name
, const char *comp_class_name
)
340 return (const void *) find_component_class_from_plugin(plugin_name
,
342 (plugin_borrow_comp_cls_func_t
)
343 bt_plugin_borrow_sink_component_class_by_name_const
);
347 const bt_component_class
*find_component_class(const char *plugin_name
,
348 const char *comp_class_name
,
349 bt_component_class_type comp_class_type
)
351 const bt_component_class
*comp_cls
= NULL
;
353 switch (comp_class_type
) {
354 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
355 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
357 case BT_COMPONENT_CLASS_TYPE_FILTER
:
358 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
360 case BT_COMPONENT_CLASS_TYPE_SINK
:
361 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
371 void print_indent(FILE *fp
, size_t indent
)
375 for (i
= 0; i
< indent
; i
++) {
381 const char *component_type_str(bt_component_class_type type
)
384 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
386 case BT_COMPONENT_CLASS_TYPE_SINK
:
388 case BT_COMPONENT_CLASS_TYPE_FILTER
:
396 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
397 const char *comp_cls_name
, bt_component_class_type type
)
399 GString
*shell_plugin_name
= NULL
;
400 GString
*shell_comp_cls_name
= NULL
;
402 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
403 if (!shell_plugin_name
) {
407 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
408 if (!shell_comp_cls_name
) {
412 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
413 bt_common_color_bold(),
414 bt_common_color_fg_cyan(),
415 component_type_str(type
),
416 bt_common_color_fg_default(),
417 bt_common_color_fg_blue(),
418 shell_plugin_name
->str
,
419 bt_common_color_fg_default(),
420 bt_common_color_fg_yellow(),
421 shell_comp_cls_name
->str
,
422 bt_common_color_reset());
425 if (shell_plugin_name
) {
426 g_string_free(shell_plugin_name
, TRUE
);
429 if (shell_comp_cls_name
) {
430 g_string_free(shell_comp_cls_name
, TRUE
);
435 void print_value(FILE *, const bt_value
*, size_t);
438 void print_value_rec(FILE *, const bt_value
*, size_t);
440 struct print_map_value_data
{
446 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
449 struct print_map_value_data
*print_map_value_data
= data
;
451 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
452 fprintf(print_map_value_data
->fp
, "%s: ", key
);
455 if (bt_value_is_array(object
) &&
456 bt_value_array_is_empty(object
)) {
457 fprintf(print_map_value_data
->fp
, "[ ]\n");
461 if (bt_value_is_map(object
) &&
462 bt_value_map_is_empty(object
)) {
463 fprintf(print_map_value_data
->fp
, "{ }\n");
467 if (bt_value_is_array(object
) ||
468 bt_value_is_map(object
)) {
469 fprintf(print_map_value_data
->fp
, "\n");
472 print_value_rec(print_map_value_data
->fp
, object
,
473 print_map_value_data
->indent
+ 2);
478 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
492 switch (bt_value_get_type(value
)) {
493 case BT_VALUE_TYPE_NULL
:
494 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
495 bt_common_color_reset());
497 case BT_VALUE_TYPE_BOOL
:
498 bool_val
= bt_value_bool_get(value
);
499 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
500 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
501 bt_common_color_reset());
503 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
504 uint_val
= bt_value_unsigned_integer_get(value
);
505 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
506 bt_common_color_fg_red(), uint_val
,
507 bt_common_color_reset());
509 case BT_VALUE_TYPE_SIGNED_INTEGER
:
510 int_val
= bt_value_signed_integer_get(value
);
511 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
512 bt_common_color_fg_red(), int_val
,
513 bt_common_color_reset());
515 case BT_VALUE_TYPE_REAL
:
516 dbl_val
= bt_value_real_get(value
);
517 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
518 bt_common_color_fg_red(), dbl_val
,
519 bt_common_color_reset());
521 case BT_VALUE_TYPE_STRING
:
522 str_val
= bt_value_string_get(value
);
523 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
524 bt_common_color_fg_green(), str_val
,
525 bt_common_color_reset());
527 case BT_VALUE_TYPE_ARRAY
:
528 size
= bt_value_array_get_size(value
);
534 print_indent(fp
, indent
);
535 fprintf(fp
, "[ ]\n");
539 for (i
= 0; i
< size
; i
++) {
540 const bt_value
*element
=
541 bt_value_array_borrow_element_by_index_const(
547 print_indent(fp
, indent
);
550 if (bt_value_is_array(element
) &&
551 bt_value_array_is_empty(element
)) {
552 fprintf(fp
, "[ ]\n");
556 if (bt_value_is_map(element
) &&
557 bt_value_map_is_empty(element
)) {
558 fprintf(fp
, "{ }\n");
562 if (bt_value_is_array(element
) ||
563 bt_value_is_map(element
)) {
567 print_value_rec(fp
, element
, indent
+ 2);
570 case BT_VALUE_TYPE_MAP
:
572 struct print_map_value_data data
= {
577 if (bt_value_map_is_empty(value
)) {
578 print_indent(fp
, indent
);
579 fprintf(fp
, "{ }\n");
583 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
592 BT_LOGE("Error printing value of type %s.",
593 bt_common_value_type_string(bt_value_get_type(value
)));
597 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
599 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
600 print_indent(fp
, indent
);
603 print_value_rec(fp
, value
, indent
);
607 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
609 fprintf(stderr
, " ");
610 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
611 bt_config_component
->comp_cls_name
->str
,
612 bt_config_component
->type
);
613 fprintf(stderr
, ":\n");
615 if (bt_config_component
->instance_name
->len
> 0) {
616 fprintf(stderr
, " Name: %s\n",
617 bt_config_component
->instance_name
->str
);
620 fprintf(stderr
, " Parameters:\n");
621 print_value(stderr
, bt_config_component
->params
, 8);
625 void print_bt_config_components(GPtrArray
*array
)
629 for (i
= 0; i
< array
->len
; i
++) {
630 struct bt_config_component
*cfg_component
=
631 bt_config_get_component(array
, i
);
632 print_bt_config_component(cfg_component
);
633 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
638 void print_plugin_paths(const bt_value
*plugin_paths
)
640 fprintf(stderr
, " Plugin paths:\n");
641 print_value(stderr
, plugin_paths
, 4);
645 void print_cfg_run(struct bt_config
*cfg
)
649 print_plugin_paths(cfg
->plugin_paths
);
650 fprintf(stderr
, " Source component instances:\n");
651 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
653 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
654 fprintf(stderr
, " Filter component instances:\n");
655 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
658 fprintf(stderr
, " Sink component instances:\n");
659 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
660 fprintf(stderr
, " Connections:\n");
662 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
663 struct bt_config_connection
*cfg_connection
=
664 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
667 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
668 cfg_connection
->upstream_comp_name
->str
,
669 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
670 cfg_connection
->upstream_port_glob
->str
,
671 cfg_connection
->downstream_comp_name
->str
,
672 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
673 cfg_connection
->downstream_port_glob
->str
);
678 void print_cfg_list_plugins(struct bt_config
*cfg
)
680 print_plugin_paths(cfg
->plugin_paths
);
684 void print_cfg_help(struct bt_config
*cfg
)
686 print_plugin_paths(cfg
->plugin_paths
);
690 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
692 print_plugin_paths(cfg
->plugin_paths
);
693 fprintf(stderr
, " Path: %s\n",
694 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
698 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
700 print_plugin_paths(cfg
->plugin_paths
);
701 fprintf(stderr
, " URL: %s\n",
702 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
706 void print_cfg_query(struct bt_config
*cfg
)
708 print_plugin_paths(cfg
->plugin_paths
);
709 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
710 fprintf(stderr
, " Component class:\n");
711 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
715 void print_cfg(struct bt_config
*cfg
)
717 if (!BT_LOG_ON_INFO
) {
721 BT_LOGI_STR("CLI configuration:");
722 BT_LOGI(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
723 BT_LOGI(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
725 switch (cfg
->command
) {
726 case BT_CONFIG_COMMAND_RUN
:
729 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
730 print_cfg_list_plugins(cfg
);
732 case BT_CONFIG_COMMAND_HELP
:
735 case BT_CONFIG_COMMAND_QUERY
:
736 print_cfg_query(cfg
);
738 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
739 print_cfg_print_ctf_metadata(cfg
);
741 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
742 print_cfg_print_lttng_live_sessions(cfg
);
750 void add_to_loaded_plugins(const bt_plugin_set
*plugin_set
)
755 count
= bt_plugin_set_get_plugin_count(plugin_set
);
756 BT_ASSERT(count
>= 0);
758 for (i
= 0; i
< count
; i
++) {
759 const bt_plugin
*plugin
=
760 bt_plugin_set_borrow_plugin_by_index_const(plugin_set
, i
);
761 const bt_plugin
*loaded_plugin
=
762 find_plugin(bt_plugin_get_name(plugin
));
767 BT_LOGI("Not using plugin: another one already exists with the same name: "
768 "plugin-name=\"%s\", plugin-path=\"%s\", "
769 "existing-plugin-path=\"%s\"",
770 bt_plugin_get_name(plugin
),
771 bt_plugin_get_path(plugin
),
772 bt_plugin_get_path(loaded_plugin
));
773 bt_plugin_put_ref(loaded_plugin
);
775 /* Add to global array. */
776 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
777 bt_plugin_get_name(plugin
));
778 bt_plugin_get_ref(plugin
);
779 g_ptr_array_add(loaded_plugins
, (void *) plugin
);
785 int load_dynamic_plugins(const bt_value
*plugin_paths
)
787 int nr_paths
, i
, ret
= 0;
789 nr_paths
= bt_value_array_get_size(plugin_paths
);
791 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
796 BT_LOGI_STR("Loading dynamic plugins.");
798 for (i
= 0; i
< nr_paths
; i
++) {
799 const bt_value
*plugin_path_value
= NULL
;
800 const char *plugin_path
;
801 const bt_plugin_set
*plugin_set
;
804 bt_value_array_borrow_element_by_index_const(
806 plugin_path
= bt_value_string_get(plugin_path_value
);
809 * Skip this if the directory does not exist because
810 * bt_plugin_find_all_from_dir() expects an existing
813 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
814 BT_LOGI("Skipping nonexistent directory path: "
815 "path=\"%s\"", plugin_path
);
819 plugin_set
= bt_plugin_find_all_from_dir(plugin_path
, false);
821 BT_LOGI("Unable to load dynamic plugins from directory: "
822 "path=\"%s\"", plugin_path
);
826 add_to_loaded_plugins(plugin_set
);
827 bt_plugin_set_put_ref(plugin_set
);
834 int load_static_plugins(void)
837 const bt_plugin_set
*plugin_set
;
839 BT_LOGI("Loading static plugins.");
840 plugin_set
= bt_plugin_find_all_from_static();
842 BT_LOGE("Unable to load static plugins.");
847 add_to_loaded_plugins(plugin_set
);
848 bt_plugin_set_put_ref(plugin_set
);
854 int load_all_plugins(const bt_value
*plugin_paths
)
858 if (load_dynamic_plugins(plugin_paths
)) {
863 if (load_static_plugins()) {
868 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
875 void print_plugin_info(const bt_plugin
*plugin
)
877 unsigned int major
, minor
, patch
;
879 bt_property_availability version_avail
;
880 const char *plugin_name
;
884 const char *plugin_description
;
886 plugin_name
= bt_plugin_get_name(plugin
);
887 path
= bt_plugin_get_path(plugin
);
888 author
= bt_plugin_get_author(plugin
);
889 license
= bt_plugin_get_license(plugin
);
890 plugin_description
= bt_plugin_get_description(plugin
);
891 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
893 printf("%s%s%s%s:\n", bt_common_color_bold(),
894 bt_common_color_fg_blue(), plugin_name
,
895 bt_common_color_reset());
897 printf(" %sPath%s: %s\n", bt_common_color_bold(),
898 bt_common_color_reset(), path
);
903 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
904 printf(" %sVersion%s: %u.%u.%u",
905 bt_common_color_bold(), bt_common_color_reset(),
906 major
, minor
, patch
);
915 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
916 bt_common_color_reset(),
917 plugin_description
? plugin_description
: "(None)");
918 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
919 bt_common_color_reset(), author
? author
: "(Unknown)");
920 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
921 bt_common_color_reset(),
922 license
? license
: "(Unknown)");
926 int cmd_query(struct bt_config
*cfg
)
929 const bt_component_class
*comp_cls
= NULL
;
930 const bt_value
*results
= NULL
;
931 const char *fail_reason
= NULL
;
933 comp_cls
= find_component_class(
934 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
935 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
936 cfg
->cmd_data
.query
.cfg_component
->type
);
938 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
939 "comp-cls-name=\"%s\", comp-cls-type=%d",
940 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
941 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
942 cfg
->cmd_data
.query
.cfg_component
->type
);
943 fprintf(stderr
, "%s%sCannot find component class %s",
944 bt_common_color_bold(),
945 bt_common_color_fg_red(),
946 bt_common_color_reset());
947 print_plugin_comp_cls_opt(stderr
,
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
);
951 fprintf(stderr
, "\n");
956 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
957 cfg
->cmd_data
.query
.cfg_component
->params
,
958 &results
, &fail_reason
);
963 print_value(stdout
, results
, 0);
967 BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", "
968 "comp-cls-name=\"%s\", comp-cls-type=%d "
969 "object=\"%s\"", fail_reason
,
970 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
971 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
972 cfg
->cmd_data
.query
.cfg_component
->type
,
973 cfg
->cmd_data
.query
.object
->str
);
974 fprintf(stderr
, "%s%sFailed to query info to %s",
975 bt_common_color_bold(),
976 bt_common_color_fg_red(),
977 bt_common_color_reset());
978 print_plugin_comp_cls_opt(stderr
,
979 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
980 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
981 cfg
->cmd_data
.query
.cfg_component
->type
);
982 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
983 bt_common_color_bold(),
984 bt_common_color_fg_red(),
985 cfg
->cmd_data
.query
.object
->str
,
987 bt_common_color_reset());
991 bt_component_class_put_ref(comp_cls
);
992 bt_value_put_ref(results
);
997 void print_component_class_help(const char *plugin_name
,
998 const bt_component_class
*comp_cls
)
1000 const char *comp_class_name
=
1001 bt_component_class_get_name(comp_cls
);
1002 const char *comp_class_description
=
1003 bt_component_class_get_description(comp_cls
);
1004 const char *comp_class_help
=
1005 bt_component_class_get_help(comp_cls
);
1006 bt_component_class_type type
=
1007 bt_component_class_get_type(comp_cls
);
1009 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
1011 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1012 bt_common_color_reset(),
1013 comp_class_description
? comp_class_description
: "(None)");
1015 if (comp_class_help
) {
1016 printf("\n%s\n", comp_class_help
);
1021 int cmd_help(struct bt_config
*cfg
)
1024 const bt_plugin
*plugin
= NULL
;
1025 const bt_component_class
*needed_comp_cls
= NULL
;
1027 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1029 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
1030 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1031 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
1032 bt_common_color_bold(), bt_common_color_fg_red(),
1033 bt_common_color_fg_blue(),
1034 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1035 bt_common_color_reset());
1040 print_plugin_info(plugin
);
1041 printf(" %sSource component classes%s: %d\n",
1042 bt_common_color_bold(),
1043 bt_common_color_reset(),
1044 (int) bt_plugin_get_source_component_class_count(plugin
));
1045 printf(" %sFilter component classes%s: %d\n",
1046 bt_common_color_bold(),
1047 bt_common_color_reset(),
1048 (int) bt_plugin_get_filter_component_class_count(plugin
));
1049 printf(" %sSink component classes%s: %d\n",
1050 bt_common_color_bold(),
1051 bt_common_color_reset(),
1052 (int) bt_plugin_get_sink_component_class_count(plugin
));
1054 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
1055 /* Plugin help only */
1059 needed_comp_cls
= find_component_class(
1060 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1061 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1062 cfg
->cmd_data
.help
.cfg_component
->type
);
1063 if (!needed_comp_cls
) {
1064 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1065 "comp-cls-name=\"%s\", comp-cls-type=%d",
1066 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1067 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1068 cfg
->cmd_data
.help
.cfg_component
->type
);
1069 fprintf(stderr
, "\n%s%sCannot find component class %s",
1070 bt_common_color_bold(),
1071 bt_common_color_fg_red(),
1072 bt_common_color_reset());
1073 print_plugin_comp_cls_opt(stderr
,
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 fprintf(stderr
, "\n");
1083 print_component_class_help(
1084 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1088 bt_component_class_put_ref(needed_comp_cls
);
1089 bt_plugin_put_ref(plugin
);
1093 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
1095 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
1098 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
1099 const char *cc_type_name
, uint64_t count
,
1100 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
1101 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
1106 printf(" %s%s component classes%s: (none)\n",
1107 bt_common_color_bold(),
1109 bt_common_color_reset());
1112 printf(" %s%s component classes%s:\n",
1113 bt_common_color_bold(),
1115 bt_common_color_reset());
1118 for (i
= 0; i
< count
; i
++) {
1119 const bt_component_class
*comp_class
=
1120 spec_comp_cls_borrow_comp_cls_func(
1121 borrow_comp_cls_by_index_func(plugin
, i
));
1122 const char *comp_class_name
=
1123 bt_component_class_get_name(comp_class
);
1124 const char *comp_class_description
=
1125 bt_component_class_get_description(comp_class
);
1126 bt_component_class_type type
=
1127 bt_component_class_get_type(comp_class
);
1130 print_plugin_comp_cls_opt(stdout
,
1131 bt_plugin_get_name(plugin
), comp_class_name
,
1134 if (comp_class_description
) {
1135 printf(": %s", comp_class_description
);
1146 int cmd_list_plugins(struct bt_config
*cfg
)
1149 int plugins_count
, component_classes_count
= 0, i
;
1151 printf("From the following plugin paths:\n\n");
1152 print_value(stdout
, cfg
->plugin_paths
, 2);
1154 plugins_count
= loaded_plugins
->len
;
1155 if (plugins_count
== 0) {
1156 printf("No plugins found.\n");
1160 for (i
= 0; i
< plugins_count
; i
++) {
1161 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1163 component_classes_count
+=
1164 bt_plugin_get_source_component_class_count(plugin
) +
1165 bt_plugin_get_filter_component_class_count(plugin
) +
1166 bt_plugin_get_sink_component_class_count(plugin
);
1169 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1170 bt_common_color_bold(),
1171 component_classes_count
,
1172 bt_common_color_reset(),
1173 bt_common_color_bold(),
1175 bt_common_color_reset());
1177 for (i
= 0; i
< plugins_count
; i
++) {
1178 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1181 print_plugin_info(plugin
);
1182 cmd_list_plugins_print_component_classes(plugin
, "Source",
1183 bt_plugin_get_source_component_class_count(plugin
),
1184 (plugin_borrow_comp_cls_by_index_func_t
)
1185 bt_plugin_borrow_source_component_class_by_index_const
,
1186 (spec_comp_cls_borrow_comp_cls_func_t
)
1187 bt_component_class_source_as_component_class
);
1188 cmd_list_plugins_print_component_classes(plugin
, "Filter",
1189 bt_plugin_get_filter_component_class_count(plugin
),
1190 (plugin_borrow_comp_cls_by_index_func_t
)
1191 bt_plugin_borrow_filter_component_class_by_index_const
,
1192 (spec_comp_cls_borrow_comp_cls_func_t
)
1193 bt_component_class_filter_as_component_class
);
1194 cmd_list_plugins_print_component_classes(plugin
, "Sink",
1195 bt_plugin_get_sink_component_class_count(plugin
),
1196 (plugin_borrow_comp_cls_by_index_func_t
)
1197 bt_plugin_borrow_sink_component_class_by_index_const
,
1198 (spec_comp_cls_borrow_comp_cls_func_t
)
1199 bt_component_class_sink_as_component_class
);
1207 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1210 const bt_component_class
*comp_cls
= NULL
;
1211 const bt_value
*results
= NULL
;
1212 bt_value
*params
= NULL
;
1213 const bt_value
*map
= NULL
;
1214 const bt_value
*v
= NULL
;
1215 static const char * const plugin_name
= "ctf";
1216 static const char * const comp_cls_name
= "lttng-live";
1217 static const bt_component_class_type comp_cls_type
=
1218 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1219 int64_t array_size
, i
;
1220 const char *fail_reason
= NULL
;
1221 FILE *out_stream
= stdout
;
1223 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1224 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1227 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1228 "comp-cls-name=\"%s\", comp-cls-type=%d",
1229 plugin_name
, comp_cls_name
,
1230 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1231 fprintf(stderr
, "%s%sCannot find component class %s",
1232 bt_common_color_bold(),
1233 bt_common_color_fg_red(),
1234 bt_common_color_reset());
1235 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1236 comp_cls_name
, comp_cls_type
);
1237 fprintf(stderr
, "\n");
1241 params
= bt_value_map_create();
1246 ret
= bt_value_map_insert_string_entry(params
, "url",
1247 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1252 ret
= query(cfg
, comp_cls
, "sessions", params
,
1253 &results
, &fail_reason
);
1260 if (!bt_value_is_array(results
)) {
1261 BT_LOGE_STR("Expecting an array for sessions query.");
1262 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1263 bt_common_color_bold(),
1264 bt_common_color_fg_red(),
1265 bt_common_color_reset());
1269 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1271 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1275 BT_LOGE_ERRNO("Cannot open file for writing",
1277 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1282 array_size
= bt_value_array_get_size(results
);
1283 for (i
= 0; i
< array_size
; i
++) {
1284 const char *url_text
;
1285 int64_t timer_us
, streams
, clients
;
1287 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1289 BT_LOGE_STR("Unexpected empty array entry.");
1292 if (!bt_value_is_map(map
)) {
1293 BT_LOGE_STR("Unexpected entry type.");
1297 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1299 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1302 url_text
= bt_value_string_get(v
);
1303 fprintf(out_stream
, "%s", url_text
);
1304 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1306 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1309 timer_us
= bt_value_signed_integer_get(v
);
1310 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1311 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1313 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1316 streams
= bt_value_signed_integer_get(v
);
1317 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1318 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1320 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1323 clients
= bt_value_signed_integer_get(v
);
1324 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1330 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1331 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1332 bt_common_color_bold(),
1333 bt_common_color_fg_red(),
1335 bt_common_color_reset());
1341 bt_value_put_ref(results
);
1342 bt_value_put_ref(params
);
1343 bt_component_class_put_ref(comp_cls
);
1345 if (out_stream
&& out_stream
!= stdout
) {
1346 int fclose_ret
= fclose(out_stream
);
1349 BT_LOGE_ERRNO("Cannot close file stream",
1351 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1359 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1362 const bt_component_class
*comp_cls
= NULL
;
1363 const bt_value
*results
= NULL
;
1364 bt_value
*params
= NULL
;
1365 const bt_value
*metadata_text_value
= NULL
;
1366 const char *metadata_text
= NULL
;
1367 static const char * const plugin_name
= "ctf";
1368 static const char * const comp_cls_name
= "fs";
1369 static const bt_component_class_type comp_cls_type
=
1370 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1371 const char *fail_reason
= NULL
;
1372 FILE *out_stream
= stdout
;
1374 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1375 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1378 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1379 "comp-cls-name=\"%s\", comp-cls-type=%d",
1380 plugin_name
, comp_cls_name
,
1381 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1382 fprintf(stderr
, "%s%sCannot find component class %s",
1383 bt_common_color_bold(),
1384 bt_common_color_fg_red(),
1385 bt_common_color_reset());
1386 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1387 comp_cls_name
, comp_cls_type
);
1388 fprintf(stderr
, "\n");
1393 params
= bt_value_map_create();
1399 ret
= bt_value_map_insert_string_entry(params
, "path",
1400 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1406 ret
= query(cfg
, comp_cls
, "metadata-info",
1407 params
, &results
, &fail_reason
);
1412 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1414 if (!metadata_text_value
) {
1415 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1420 metadata_text
= bt_value_string_get(metadata_text_value
);
1422 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1424 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1428 BT_LOGE_ERRNO("Cannot open file for writing",
1430 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1435 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1437 BT_LOGE("Cannot write whole metadata text to output stream: "
1448 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1449 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1450 bt_common_color_bold(),
1451 bt_common_color_fg_red(),
1453 bt_common_color_reset());
1456 bt_value_put_ref(results
);
1457 bt_value_put_ref(params
);
1458 bt_component_class_put_ref(comp_cls
);
1460 if (out_stream
&& out_stream
!= stdout
) {
1461 int fclose_ret
= fclose(out_stream
);
1464 BT_LOGE_ERRNO("Cannot close file stream",
1466 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1474 char *instance_name
;
1478 struct trace_range
{
1479 uint64_t intersection_range_begin_ns
;
1480 uint64_t intersection_range_end_ns
;
1484 guint
port_id_hash(gconstpointer v
)
1486 const struct port_id
*id
= v
;
1488 BT_ASSERT(id
->instance_name
);
1489 BT_ASSERT(id
->port_name
);
1491 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1495 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1497 const struct port_id
*id1
= v1
;
1498 const struct port_id
*id2
= v2
;
1500 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1501 !strcmp(id1
->port_name
, id2
->port_name
);
1505 void port_id_destroy(gpointer data
)
1507 struct port_id
*id
= data
;
1509 free(id
->instance_name
);
1510 free(id
->port_name
);
1515 void trace_range_destroy(gpointer data
)
1520 struct cmd_run_ctx
{
1522 GHashTable
*src_components
;
1525 GHashTable
*flt_components
;
1528 GHashTable
*sink_components
;
1534 struct bt_config
*cfg
;
1538 bool stream_intersection_mode
;
1541 * Association of struct port_id -> struct trace_range.
1543 GHashTable
*intersections
;
1546 /* Returns a timestamp of the form "(-)s.ns" */
1548 char *s_from_ns(int64_t ns
)
1553 int64_t ts_sec_abs
, ts_nsec_abs
;
1554 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1555 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1557 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1558 is_negative
= false;
1559 ts_sec_abs
= ts_sec
;
1560 ts_nsec_abs
= ts_nsec
;
1561 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1562 is_negative
= false;
1563 ts_sec_abs
= ts_sec
- 1;
1564 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1565 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1567 ts_sec_abs
= ts_sec
;
1568 ts_nsec_abs
= -ts_nsec
;
1569 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1571 ts_sec_abs
= -(ts_sec
+ 1);
1572 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1573 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1575 ts_sec_abs
= -ts_sec
;
1576 ts_nsec_abs
= ts_nsec
;
1577 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1579 ts_sec_abs
= -ts_sec
;
1580 ts_nsec_abs
= -ts_nsec
;
1583 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1584 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1592 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1593 struct cmd_run_ctx
*ctx
,
1594 const bt_component
*upstream_comp
,
1595 const bt_port_output
*out_upstream_port
,
1596 struct bt_config_connection
*cfg_conn
)
1598 typedef uint64_t (*input_port_count_func_t
)(void *);
1599 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1600 const void *, uint64_t);
1601 const bt_port
*upstream_port
=
1602 bt_port_output_as_port_const(out_upstream_port
);
1605 GQuark downstreamp_comp_name_quark
;
1606 void *downstream_comp
;
1607 uint64_t downstream_port_count
;
1609 input_port_count_func_t port_count_fn
;
1610 borrow_input_port_by_index_func_t port_by_index_fn
;
1611 bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1612 bool insert_trimmer
= false;
1613 bt_value
*trimmer_params
= NULL
;
1614 char *intersection_begin
= NULL
;
1615 char *intersection_end
= NULL
;
1616 const bt_component_filter
*trimmer
= NULL
;
1617 const bt_component_class_filter
*trimmer_class
= NULL
;
1618 const bt_port_input
*trimmer_input
= NULL
;
1619 const bt_port_output
*trimmer_output
= NULL
;
1621 if (ctx
->intersections
&&
1622 bt_component_get_class_type(upstream_comp
) ==
1623 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1624 struct trace_range
*range
;
1625 struct port_id port_id
= {
1626 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1627 .port_name
= (char *) bt_port_get_name(upstream_port
)
1630 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1634 range
= (struct trace_range
*) g_hash_table_lookup(
1635 ctx
->intersections
, &port_id
);
1637 bt_value_status status
;
1639 intersection_begin
= s_from_ns(
1640 range
->intersection_range_begin_ns
);
1641 intersection_end
= s_from_ns(
1642 range
->intersection_range_end_ns
);
1643 if (!intersection_begin
|| !intersection_end
) {
1644 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1648 insert_trimmer
= true;
1649 trimmer_params
= bt_value_map_create();
1650 if (!trimmer_params
) {
1654 status
= bt_value_map_insert_string_entry(
1655 trimmer_params
, "begin", intersection_begin
);
1656 if (status
!= BT_VALUE_STATUS_OK
) {
1659 status
= bt_value_map_insert_string_entry(
1661 "end", intersection_end
);
1662 if (status
!= BT_VALUE_STATUS_OK
) {
1667 trimmer_class
= find_filter_component_class("utils", "trimmer");
1668 if (!trimmer_class
) {
1673 BT_LOGI("Connecting upstream port to the next available downstream port: "
1674 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1675 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1676 upstream_port
, bt_port_get_name(upstream_port
),
1677 cfg_conn
->downstream_comp_name
->str
,
1678 cfg_conn
->arg
->str
);
1679 downstreamp_comp_name_quark
= g_quark_from_string(
1680 cfg_conn
->downstream_comp_name
->str
);
1681 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1682 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1683 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1684 port_count_fn
= (input_port_count_func_t
)
1685 bt_component_filter_get_input_port_count
;
1686 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1687 bt_component_filter_borrow_input_port_by_index_const
;
1689 if (!downstream_comp
) {
1690 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1691 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1692 port_count_fn
= (input_port_count_func_t
)
1693 bt_component_sink_get_input_port_count
;
1694 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1695 bt_component_sink_borrow_input_port_by_index_const
;
1698 if (!downstream_comp
) {
1699 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1700 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1701 cfg_conn
->arg
->str
);
1702 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1703 cfg_conn
->arg
->str
);
1707 downstream_port_count
= port_count_fn(downstream_comp
);
1709 for (i
= 0; i
< downstream_port_count
; i
++) {
1710 const bt_port_input
*in_downstream_port
=
1711 port_by_index_fn(downstream_comp
, i
);
1712 const bt_port
*downstream_port
=
1713 bt_port_input_as_port_const(in_downstream_port
);
1714 const char *upstream_port_name
;
1715 const char *downstream_port_name
;
1717 BT_ASSERT(downstream_port
);
1719 /* Skip port if it's already connected. */
1720 if (bt_port_is_connected(downstream_port
)) {
1721 BT_LOGI("Skipping downstream port: already connected: "
1722 "port-addr=%p, port-name=\"%s\"",
1724 bt_port_get_name(downstream_port
));
1728 downstream_port_name
= bt_port_get_name(downstream_port
);
1729 BT_ASSERT(downstream_port_name
);
1730 upstream_port_name
= bt_port_get_name(upstream_port
);
1731 BT_ASSERT(upstream_port_name
);
1733 if (!bt_common_star_glob_match(
1734 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1735 downstream_port_name
, SIZE_MAX
)) {
1739 if (insert_trimmer
) {
1741 * In order to insert the trimmer between the
1742 * two components that were being connected, we
1743 * create a connection configuration entry which
1744 * describes a connection from the trimmer's
1745 * output to the original input that was being
1748 * Hence, the creation of the trimmer will cause
1749 * the graph "new port" listener to establish
1750 * all downstream connections as its output port
1751 * is connected. We will then establish the
1752 * connection between the original upstream
1753 * source and the trimmer.
1755 char *trimmer_name
= NULL
;
1756 bt_graph_status graph_status
;
1758 ret
= asprintf(&trimmer_name
,
1759 "stream-intersection-trimmer-%s",
1760 upstream_port_name
);
1766 ctx
->connect_ports
= false;
1767 graph_status
= bt_graph_add_filter_component(
1768 ctx
->graph
, trimmer_class
, trimmer_name
,
1769 trimmer_params
, ctx
->cfg
->log_level
,
1772 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1778 bt_component_filter_borrow_input_port_by_index_const(
1780 if (!trimmer_input
) {
1784 bt_component_filter_borrow_output_port_by_index_const(
1786 if (!trimmer_output
) {
1791 * Replace the current downstream port by the trimmer's
1794 in_downstream_port
= trimmer_input
;
1796 bt_port_input_as_port_const(in_downstream_port
);
1797 downstream_port_name
= bt_port_get_name(
1799 BT_ASSERT(downstream_port_name
);
1802 /* We have a winner! */
1803 status
= bt_graph_connect_ports(ctx
->graph
,
1804 out_upstream_port
, in_downstream_port
, NULL
);
1805 downstream_port
= NULL
;
1807 case BT_GRAPH_STATUS_OK
:
1809 case BT_GRAPH_STATUS_CANCELED
:
1810 BT_LOGI_STR("Graph was canceled by user.");
1811 status
= BT_GRAPH_STATUS_OK
;
1813 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1814 BT_LOGE("A component refused a connection to one of its ports: "
1815 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1816 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1817 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1818 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1820 upstream_comp
, bt_component_get_name(upstream_comp
),
1821 upstream_port
, bt_port_get_name(upstream_port
),
1822 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1823 downstream_port
, downstream_port_name
,
1824 cfg_conn
->arg
->str
);
1826 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1827 bt_port_get_name(upstream_port
),
1828 downstream_port_name
,
1829 cfg_conn
->arg
->str
);
1832 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1833 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1834 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1835 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1836 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1838 upstream_comp
, bt_component_get_name(upstream_comp
),
1839 upstream_port
, bt_port_get_name(upstream_port
),
1840 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1841 downstream_port
, downstream_port_name
,
1842 cfg_conn
->arg
->str
);
1844 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1845 bt_port_get_name(upstream_port
),
1846 downstream_port_name
,
1847 cfg_conn
->arg
->str
);
1851 BT_LOGI("Connected component ports: "
1852 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1853 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1854 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1855 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1857 upstream_comp
, bt_component_get_name(upstream_comp
),
1858 upstream_port
, bt_port_get_name(upstream_port
),
1859 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1860 downstream_port
, downstream_port_name
,
1861 cfg_conn
->arg
->str
);
1863 if (insert_trimmer
) {
1865 * The first connection, from the source to the trimmer,
1866 * has been done. We now connect the trimmer to the
1867 * original downstream port.
1869 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1871 bt_component_filter_as_component_const(trimmer
),
1872 trimmer_output
, cfg_conn
);
1876 ctx
->connect_ports
= true;
1880 * We found a matching downstream port: the search is
1886 /* No downstream port found */
1887 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1888 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1889 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1890 upstream_port
, bt_port_get_name(upstream_port
),
1891 cfg_conn
->downstream_comp_name
->str
,
1892 cfg_conn
->arg
->str
);
1894 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1895 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1901 free(intersection_begin
);
1902 free(intersection_end
);
1903 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1904 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1905 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1910 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1911 const bt_port_output
*upstream_port
)
1914 const char *upstream_port_name
;
1915 const char *upstream_comp_name
;
1916 const bt_component
*upstream_comp
= NULL
;
1920 BT_ASSERT(upstream_port
);
1921 upstream_port_name
= bt_port_get_name(
1922 bt_port_output_as_port_const(upstream_port
));
1923 BT_ASSERT(upstream_port_name
);
1924 upstream_comp
= bt_port_borrow_component_const(
1925 bt_port_output_as_port_const(upstream_port
));
1926 if (!upstream_comp
) {
1927 BT_LOGW("Upstream port to connect is not part of a component: "
1928 "port-addr=%p, port-name=\"%s\"",
1929 upstream_port
, upstream_port_name
);
1934 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1935 BT_ASSERT(upstream_comp_name
);
1936 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1937 "port-addr=%p, port-name=\"%s\"",
1938 upstream_comp
, upstream_comp_name
,
1939 upstream_port
, upstream_port_name
);
1941 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1942 struct bt_config_connection
*cfg_conn
=
1944 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1946 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1947 upstream_comp_name
)) {
1951 if (!bt_common_star_glob_match(
1952 cfg_conn
->upstream_port_glob
->str
,
1953 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1957 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1958 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1960 BT_LOGE("Cannot connect upstream port: "
1961 "port-addr=%p, port-name=\"%s\"",
1963 upstream_port_name
);
1965 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1968 cfg_conn
->arg
->str
);
1974 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1975 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1976 upstream_port_name
);
1978 "Cannot create connection: upstream port `%s` does not match any connection\n",
1979 upstream_port_name
);
1989 bt_graph_listener_status
1990 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1991 const bt_port_output
*out_port
)
1993 const bt_component
*comp
;
1994 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1995 bt_graph_listener_status ret
= BT_GRAPH_LISTENER_STATUS_OK
;
1997 comp
= bt_port_borrow_component_const(port
);
1998 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1999 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
2000 comp
, comp
? bt_component_get_name(comp
) : "",
2001 port
, bt_port_get_name(port
));
2003 if (!ctx
->connect_ports
) {
2008 BT_LOGW_STR("Port has no component.");
2012 if (bt_port_is_connected(port
)) {
2013 BT_LOGW_STR("Port is already connected.");
2017 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
2018 BT_LOGF_STR("Cannot connect upstream port.");
2019 fprintf(stderr
, "Added port could not be connected: aborting\n");
2020 ret
= BT_GRAPH_LISTENER_STATUS_ERROR
;
2029 bt_graph_listener_status
graph_source_output_port_added_listener(
2030 const bt_component_source
*component
,
2031 const bt_port_output
*port
, void *data
)
2033 return graph_output_port_added_listener(data
, port
);
2037 bt_graph_listener_status
graph_filter_output_port_added_listener(
2038 const bt_component_filter
*component
,
2039 const bt_port_output
*port
, void *data
)
2041 return graph_output_port_added_listener(data
, port
);
2045 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
2051 if (ctx
->src_components
) {
2052 g_hash_table_destroy(ctx
->src_components
);
2053 ctx
->src_components
= NULL
;
2056 if (ctx
->flt_components
) {
2057 g_hash_table_destroy(ctx
->flt_components
);
2058 ctx
->flt_components
= NULL
;
2061 if (ctx
->sink_components
) {
2062 g_hash_table_destroy(ctx
->sink_components
);
2063 ctx
->sink_components
= NULL
;
2066 if (ctx
->intersections
) {
2067 g_hash_table_destroy(ctx
->intersections
);
2068 ctx
->intersections
= NULL
;
2071 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
2077 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2080 bt_graph_status status
;
2083 ctx
->connect_ports
= false;
2084 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
2085 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2086 if (!ctx
->src_components
) {
2090 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
2091 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2092 if (!ctx
->flt_components
) {
2096 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
2097 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2098 if (!ctx
->sink_components
) {
2102 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2103 ctx
->stream_intersection_mode
= true;
2104 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2105 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2106 if (!ctx
->intersections
) {
2111 ctx
->graph
= bt_graph_create();
2116 the_graph
= ctx
->graph
;
2117 status
= bt_graph_add_source_component_output_port_added_listener(
2118 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
2120 if (status
!= BT_GRAPH_STATUS_OK
) {
2121 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2125 status
= bt_graph_add_filter_component_output_port_added_listener(
2126 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
2128 if (status
!= BT_GRAPH_STATUS_OK
) {
2129 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2136 cmd_run_ctx_destroy(ctx
);
2144 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2145 struct bt_config_component
*cfg_comp
,
2146 const bt_component_class_source
*src_comp_cls
)
2150 int64_t trace_count
;
2151 const char *path
= NULL
;
2152 const bt_value
*query_result
= NULL
;
2153 const bt_value
*trace_info
= NULL
;
2154 const bt_value
*intersection_range
= NULL
;
2155 const bt_value
*intersection_begin
= NULL
;
2156 const bt_value
*intersection_end
= NULL
;
2157 const bt_value
*stream_infos
= NULL
;
2158 const bt_value
*stream_info
= NULL
;
2159 struct port_id
*port_id
= NULL
;
2160 struct trace_range
*trace_range
= NULL
;
2161 const char *fail_reason
= NULL
;
2162 const bt_component_class
*comp_cls
=
2163 bt_component_class_source_as_component_class_const(src_comp_cls
);
2165 ret
= query(ctx
->cfg
, comp_cls
, "trace-info",
2166 cfg_comp
->params
, &query_result
,
2169 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2170 "comp-class-name=\"%s\"", fail_reason
,
2171 bt_component_class_get_name(comp_cls
));
2176 BT_ASSERT(query_result
);
2178 if (!bt_value_is_array(query_result
)) {
2179 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2180 "component-class-name=%s",
2181 bt_component_class_get_name(comp_cls
));
2186 trace_count
= bt_value_array_get_size(query_result
);
2187 if (trace_count
< 0) {
2192 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2194 uint64_t stream_idx
;
2195 int64_t stream_count
;
2197 trace_info
= bt_value_array_borrow_element_by_index_const(
2198 query_result
, trace_idx
);
2199 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2201 BT_LOGD_STR("Cannot retrieve trace from query result.");
2205 intersection_range
= bt_value_map_borrow_entry_value_const(
2206 trace_info
, "intersection-range-ns");
2207 if (!intersection_range
) {
2209 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2213 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
2215 if (!intersection_begin
) {
2217 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2221 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
2223 if (!intersection_end
) {
2225 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2229 begin
= bt_value_signed_integer_get(intersection_begin
);
2230 end
= bt_value_signed_integer_get(intersection_end
);
2232 if (begin
< 0 || end
< 0 || end
< begin
) {
2233 BT_LOGW("Invalid trace stream intersection values: "
2234 "intersection-range-ns:begin=%" PRId64
2235 ", intersection-range-ns:end=%" PRId64
,
2241 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
2243 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2245 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2249 stream_count
= bt_value_array_get_size(stream_infos
);
2250 if (stream_count
< 0) {
2255 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2256 const bt_value
*port_name
;
2258 port_id
= g_new0(struct port_id
, 1);
2261 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2264 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2265 if (!port_id
->instance_name
) {
2267 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2271 trace_range
= g_new0(struct trace_range
, 1);
2274 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2277 trace_range
->intersection_range_begin_ns
= begin
;
2278 trace_range
->intersection_range_end_ns
= end
;
2280 stream_info
= bt_value_array_borrow_element_by_index_const(
2281 stream_infos
, stream_idx
);
2282 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2284 BT_LOGE_STR("Cannot retrieve stream informations from trace in query result.");
2288 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2289 if (!port_name
|| !bt_value_is_string(port_name
)) {
2291 BT_LOGE_STR("Cannot retrieve port name in query result.");
2295 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2296 if (!port_id
->port_name
) {
2298 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2302 BT_LOGD("Inserting stream intersection ");
2304 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2314 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2315 bt_common_color_bold(),
2316 bt_common_color_fg_yellow(),
2317 path
? path
: "(unknown)",
2318 bt_common_color_reset());
2320 bt_value_put_ref(query_result
);
2322 g_free(trace_range
);
2327 int cmd_run_ctx_create_components_from_config_components(
2328 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2331 const void *comp_cls
= NULL
;
2332 const void *comp
= NULL
;
2335 for (i
= 0; i
< cfg_components
->len
; i
++) {
2336 struct bt_config_component
*cfg_comp
=
2337 g_ptr_array_index(cfg_components
, i
);
2340 switch (cfg_comp
->type
) {
2341 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2342 comp_cls
= find_source_component_class(
2343 cfg_comp
->plugin_name
->str
,
2344 cfg_comp
->comp_cls_name
->str
);
2346 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2347 comp_cls
= find_filter_component_class(
2348 cfg_comp
->plugin_name
->str
,
2349 cfg_comp
->comp_cls_name
->str
);
2351 case BT_COMPONENT_CLASS_TYPE_SINK
:
2352 comp_cls
= find_sink_component_class(
2353 cfg_comp
->plugin_name
->str
,
2354 cfg_comp
->comp_cls_name
->str
);
2361 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2362 "comp-cls-name=\"%s\", comp-cls-type=%d",
2363 cfg_comp
->plugin_name
->str
,
2364 cfg_comp
->comp_cls_name
->str
,
2366 fprintf(stderr
, "%s%sCannot find component class %s",
2367 bt_common_color_bold(),
2368 bt_common_color_fg_red(),
2369 bt_common_color_reset());
2370 print_plugin_comp_cls_opt(stderr
,
2371 cfg_comp
->plugin_name
->str
,
2372 cfg_comp
->comp_cls_name
->str
,
2374 fprintf(stderr
, "\n");
2378 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_VERBOSE
);
2380 switch (cfg_comp
->type
) {
2381 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2382 ret
= bt_graph_add_source_component(ctx
->graph
,
2383 comp_cls
, cfg_comp
->instance_name
->str
,
2384 cfg_comp
->params
, cfg_comp
->log_level
,
2387 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2388 ret
= bt_graph_add_filter_component(ctx
->graph
,
2389 comp_cls
, cfg_comp
->instance_name
->str
,
2390 cfg_comp
->params
, cfg_comp
->log_level
,
2393 case BT_COMPONENT_CLASS_TYPE_SINK
:
2394 ret
= bt_graph_add_sink_component(ctx
->graph
,
2395 comp_cls
, cfg_comp
->instance_name
->str
,
2396 cfg_comp
->params
, cfg_comp
->log_level
,
2404 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2405 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2407 cfg_comp
->plugin_name
->str
,
2408 cfg_comp
->comp_cls_name
->str
,
2409 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2410 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2411 bt_common_color_bold(),
2412 bt_common_color_fg_red(),
2413 cfg_comp
->instance_name
->str
,
2414 bt_common_color_reset());
2418 if (ctx
->stream_intersection_mode
&&
2419 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2420 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2426 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2427 comp
, cfg_comp
->instance_name
->str
);
2428 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2429 BT_ASSERT(quark
> 0);
2431 switch (cfg_comp
->type
) {
2432 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2433 g_hash_table_insert(ctx
->src_components
,
2434 GUINT_TO_POINTER(quark
), (void *) comp
);
2436 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2437 g_hash_table_insert(ctx
->flt_components
,
2438 GUINT_TO_POINTER(quark
), (void *) comp
);
2440 case BT_COMPONENT_CLASS_TYPE_SINK
:
2441 g_hash_table_insert(ctx
->sink_components
,
2442 GUINT_TO_POINTER(quark
), (void *) comp
);
2449 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2458 bt_object_put_ref(comp
);
2459 bt_object_put_ref(comp_cls
);
2464 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2469 * Make sure that, during this phase, our graph's "port added"
2470 * listener does not connect ports while we are creating the
2471 * components because we have a special, initial phase for
2474 ctx
->connect_ports
= false;
2476 ret
= cmd_run_ctx_create_components_from_config_components(
2477 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2483 ret
= cmd_run_ctx_create_components_from_config_components(
2484 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2490 ret
= cmd_run_ctx_create_components_from_config_components(
2491 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2501 typedef uint64_t (*output_port_count_func_t
)(const void *);
2502 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2503 const void *, uint64_t);
2506 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2507 void *comp
, output_port_count_func_t port_count_fn
,
2508 borrow_output_port_by_index_func_t port_by_index_fn
)
2514 count
= port_count_fn(comp
);
2516 for (i
= 0; i
< count
; i
++) {
2517 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2519 BT_ASSERT(upstream_port
);
2520 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2531 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2534 GHashTableIter iter
;
2535 gpointer g_name_quark
, g_comp
;
2537 ctx
->connect_ports
= true;
2538 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2540 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2541 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2542 (output_port_count_func_t
)
2543 bt_component_source_get_output_port_count
,
2544 (borrow_output_port_by_index_func_t
)
2545 bt_component_source_borrow_output_port_by_index_const
);
2551 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2553 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2554 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2555 (output_port_count_func_t
)
2556 bt_component_filter_get_output_port_count
,
2557 (borrow_output_port_by_index_func_t
)
2558 bt_component_filter_borrow_output_port_by_index_const
);
2569 const char *bt_graph_status_str(bt_graph_status status
)
2572 case BT_GRAPH_STATUS_OK
:
2573 return "BT_GRAPH_STATUS_OK";
2574 case BT_GRAPH_STATUS_END
:
2575 return "BT_GRAPH_STATUS_END";
2576 case BT_GRAPH_STATUS_AGAIN
:
2577 return "BT_GRAPH_STATUS_AGAIN";
2578 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
2579 return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION";
2580 case BT_GRAPH_STATUS_CANCELED
:
2581 return "BT_GRAPH_STATUS_CANCELED";
2582 case BT_GRAPH_STATUS_ERROR
:
2583 return "BT_GRAPH_STATUS_ERROR";
2584 case BT_GRAPH_STATUS_NOMEM
:
2585 return "BT_GRAPH_STATUS_NOMEM";
2592 int cmd_run(struct bt_config
*cfg
)
2595 struct cmd_run_ctx ctx
= { 0 };
2597 /* Initialize the command's context and the graph object */
2598 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2599 BT_LOGE_STR("Cannot initialize the command's context.");
2600 fprintf(stderr
, "Cannot initialize the command's context\n");
2605 BT_LOGI_STR("Canceled by user before creating components.");
2609 BT_LOGI_STR("Creating components.");
2611 /* Create the requested component instances */
2612 if (cmd_run_ctx_create_components(&ctx
)) {
2613 BT_LOGE_STR("Cannot create components.");
2614 fprintf(stderr
, "Cannot create components\n");
2619 BT_LOGI_STR("Canceled by user before connecting components.");
2623 BT_LOGI_STR("Connecting components.");
2625 /* Connect the initially visible component ports */
2626 if (cmd_run_ctx_connect_ports(&ctx
)) {
2627 BT_LOGE_STR("Cannot connect initial component ports.");
2628 fprintf(stderr
, "Cannot connect initial component ports\n");
2633 BT_LOGI_STR("Canceled by user before running the graph.");
2637 BT_LOGI_STR("Running the graph.");
2641 bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2644 * Reset console in case something messed with console
2645 * codes during the graph's execution.
2647 printf("%s", bt_common_color_reset());
2649 fprintf(stderr
, "%s", bt_common_color_reset());
2650 BT_LOGV("bt_graph_run() returned: status=%s",
2651 bt_graph_status_str(graph_status
));
2653 switch (graph_status
) {
2654 case BT_GRAPH_STATUS_OK
:
2656 case BT_GRAPH_STATUS_CANCELED
:
2657 BT_LOGI_STR("Graph was canceled by user.");
2659 case BT_GRAPH_STATUS_AGAIN
:
2660 if (bt_graph_is_canceled(ctx
.graph
)) {
2661 BT_LOGI_STR("Graph was canceled by user.");
2665 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2666 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2668 cfg
->cmd_data
.run
.retry_duration_us
);
2670 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2671 if (bt_graph_is_canceled(ctx
.graph
)) {
2672 BT_LOGI_STR("Graph was canceled by user.");
2678 case BT_GRAPH_STATUS_END
:
2681 BT_LOGE_STR("Graph failed to complete successfully");
2682 fprintf(stderr
, "Graph failed to complete successfully\n");
2695 cmd_run_ctx_destroy(&ctx
);
2700 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2702 const char *env_clash
;
2704 if (!cfg
->command_name
) {
2708 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2709 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2713 if (g_file_test(cfg
->command_name
,
2714 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2715 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2717 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2719 fprintf(stderr
, "\n");
2720 fprintf(stderr
, " babeltrace2 convert %s [OPTIONS]\n",
2726 void init_log_level(void)
2728 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2732 void set_auto_log_levels(struct bt_config
*cfg
)
2734 const char **env_var_name
;
2737 * Override the configuration's default log level if
2738 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2739 * are found for backward compatibility with legacy Babetrace 1.
2741 if (getenv("BABELTRACE_DEBUG") &&
2742 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2743 cfg
->log_level
= BT_LOG_VERBOSE
;
2744 } else if (getenv("BABELTRACE_VERBOSE") &&
2745 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2746 cfg
->log_level
= BT_LOG_INFO
;
2750 * Set log levels according to --debug or --verbose. For
2751 * backward compatibility, --debug is more verbose than
2754 * --verbose: INFO log level
2755 * --debug: VERBOSE log level (includes DEBUG, which is
2756 * is less verbose than VERBOSE in the internal
2757 * logging framework)
2759 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2761 bt_logging_set_global_level(BT_LOG_INFO
);
2762 } else if (cfg
->debug
) {
2763 bt_logging_set_global_level(BT_LOG_VERBOSE
);
2766 * Set library's default log level if not
2767 * explicitly specified.
2769 bt_logging_set_global_level(cfg
->log_level
);
2773 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2775 bt_cli_log_level
= BT_LOG_INFO
;
2776 } else if (cfg
->debug
) {
2777 bt_cli_log_level
= BT_LOG_VERBOSE
;
2780 * Set CLI's default log level if not explicitly
2783 bt_cli_log_level
= cfg
->log_level
;
2787 env_var_name
= log_level_env_var_names
;
2789 while (*env_var_name
) {
2790 if (!getenv(*env_var_name
)) {
2792 g_setenv(*env_var_name
, "I", 1);
2793 } else if (cfg
->debug
) {
2794 g_setenv(*env_var_name
, "V", 1);
2796 char val
[2] = { 0 };
2799 * Set module's default log level if not
2800 * explicitly specified.
2802 val
[0] = bt_log_get_letter_from_level(
2804 g_setenv(*env_var_name
, val
, 1);
2812 int main(int argc
, const char **argv
)
2816 struct bt_config
*cfg
;
2819 set_signal_handler();
2821 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2824 /* Quit without errors; typically usage/version */
2826 BT_LOGI_STR("Quitting without errors.");
2831 BT_LOGE("Command-line error: retcode=%d", retcode
);
2836 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2837 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2842 set_auto_log_levels(cfg
);
2845 if (cfg
->command_needs_plugins
) {
2846 ret
= load_all_plugins(cfg
->plugin_paths
);
2848 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2854 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2855 cfg
->command
, cfg
->command_name
);
2857 switch (cfg
->command
) {
2858 case BT_CONFIG_COMMAND_RUN
:
2861 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2862 ret
= cmd_list_plugins(cfg
);
2864 case BT_CONFIG_COMMAND_HELP
:
2865 ret
= cmd_help(cfg
);
2867 case BT_CONFIG_COMMAND_QUERY
:
2868 ret
= cmd_query(cfg
);
2870 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2871 ret
= cmd_print_ctf_metadata(cfg
);
2873 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2874 ret
= cmd_print_lttng_live_sessions(cfg
);
2877 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2881 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2882 cfg
->command
, cfg
->command_name
, ret
);
2883 warn_command_name_and_directory_clash(cfg
);
2884 retcode
= ret
? 1 : 0;
2887 BT_OBJECT_PUT_REF_AND_RESET(cfg
);