4 * Babeltrace Trace Converter
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define BT_LOG_TAG "CLI"
32 #include <babeltrace/babeltrace.h>
33 #include <babeltrace/common-internal.h>
34 #include <babeltrace/values-internal.h>
44 #include "babeltrace-cfg.h"
45 #include "babeltrace-cfg-cli-args.h"
46 #include "babeltrace-cfg-cli-args-default.h"
48 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
49 #define ENV_BABELTRACE_CLI_LOG_LEVEL "BABELTRACE_CLI_LOG_LEVEL"
50 #define NSEC_PER_SEC 1000000000LL
53 * Known environment variable names for the log levels of the project's
56 static const char* log_level_env_var_names
[] = {
57 "BABELTRACE_COMMON_LOG_LEVEL",
58 "BABELTRACE_COMPAT_LOG_LEVEL",
59 "BABELTRACE_PLUGIN_CTF_BTR_LOG_LEVEL",
60 "BABELTRACE_SINK_CTF_FS_LOG_LEVEL",
61 "BABELTRACE_SRC_CTF_FS_LOG_LEVEL",
62 "BABELTRACE_SRC_CTF_LTTNG_LIVE_LOG_LEVEL",
63 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
64 "BABELTRACE_PLUGIN_CTF_NOTIF_ITER_LOG_LEVEL",
65 "BABELTRACE_PLUGIN_CTFCOPYTRACE_LIB_LOG_LEVEL",
66 "BABELTRACE_FLT_LTTNG_UTILS_DEBUG_INFO_LOG_LEVEL",
67 "BABELTRACE_SRC_TEXT_DMESG_LOG_LEVEL",
68 "BABELTRACE_SINK_TEXT_PRETTY_LOG_LEVEL",
69 "BABELTRACE_FLT_UTILS_MUXER_LOG_LEVEL",
70 "BABELTRACE_FLT_UTILS_TRIMMER_LOG_LEVEL",
71 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
72 "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
76 /* Application's processing graph (weak) */
77 static struct bt_graph
*the_graph
;
78 static struct bt_query_executor
*the_query_executor
;
79 static bool canceled
= false;
81 GPtrArray
*loaded_plugins
;
87 BOOL WINAPI
signal_handler(DWORD signal
) {
89 bt_graph_cancel(the_graph
);
98 void set_signal_handler(void)
100 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
101 BT_LOGE("Failed to set the ctrl+c handler.");
104 #else /* __MINGW32__ */
106 void signal_handler(int signum
)
108 if (signum
!= SIGINT
) {
113 bt_graph_cancel(the_graph
);
116 if (the_query_executor
) {
117 bt_query_executor_cancel(the_query_executor
);
124 void set_signal_handler(void)
126 struct sigaction new_action
, old_action
;
128 new_action
.sa_handler
= signal_handler
;
129 sigemptyset(&new_action
.sa_mask
);
130 new_action
.sa_flags
= 0;
131 sigaction(SIGINT
, NULL
, &old_action
);
133 if (old_action
.sa_handler
!= SIG_IGN
) {
134 sigaction(SIGINT
, &new_action
, NULL
);
137 #endif /* __MINGW32__ */
140 void init_static_data(void)
142 loaded_plugins
= g_ptr_array_new_with_free_func(bt_put
);
146 void fini_static_data(void)
148 g_ptr_array_free(loaded_plugins
, TRUE
);
152 int create_the_query_executor(void)
156 the_query_executor
= bt_query_executor_create();
157 if (!the_query_executor
) {
158 BT_LOGE_STR("Cannot create a query executor.");
166 void destroy_the_query_executor(void)
168 BT_PUT(the_query_executor
);
172 int query(struct bt_component_class
*comp_cls
, const char *obj
,
173 struct bt_value
*params
, struct bt_value
**user_result
,
174 const char **fail_reason
)
176 struct bt_value
*result
= NULL
;
177 enum bt_query_status status
;
178 *fail_reason
= "unknown error";
183 ret
= create_the_query_executor();
185 /* create_the_query_executor() logs errors */
190 BT_LOGI("Canceled by user before executing the query: "
191 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
192 "query-obj=\"%s\"", comp_cls
,
193 bt_component_class_get_name(comp_cls
), obj
);
194 *fail_reason
= "canceled by user";
199 status
= bt_query_executor_query(the_query_executor
, comp_cls
,
200 obj
, params
, &result
);
202 case BT_QUERY_STATUS_OK
:
204 case BT_QUERY_STATUS_AGAIN
:
206 const uint64_t sleep_time_us
= 100000;
208 /* Wait 100 ms and retry */
209 BT_LOGV("Got BT_QUERY_STATUS_AGAIN: sleeping: "
210 "time-us=%" PRIu64
, sleep_time_us
);
212 if (usleep(sleep_time_us
)) {
213 if (bt_query_executor_is_canceled(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_STATUS_EXECUTOR_CANCELED
:
227 *fail_reason
= "canceled by user";
229 case BT_QUERY_STATUS_ERROR
:
230 case BT_QUERY_STATUS_INVALID
:
232 case BT_QUERY_STATUS_INVALID_OBJECT
:
233 *fail_reason
= "invalid or unknown query object";
235 case BT_QUERY_STATUS_INVALID_PARAMS
:
236 *fail_reason
= "invalid query parameters";
238 case BT_QUERY_STATUS_NOMEM
:
239 *fail_reason
= "not enough memory";
242 BT_LOGF("Unknown query status: status=%d", status
);
248 *user_result
= result
;
256 destroy_the_query_executor();
262 struct bt_plugin
*find_plugin(const char *name
)
265 struct bt_plugin
*plugin
= NULL
;
268 BT_LOGD("Finding plugin: name=\"%s\"", name
);
270 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
271 plugin
= g_ptr_array_index(loaded_plugins
, i
);
273 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
280 if (BT_LOG_ON_DEBUG
) {
282 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
284 BT_LOGD("Cannot find plugin.");
288 return bt_get(plugin
);
292 struct bt_component_class
*find_component_class(const char *plugin_name
,
293 const char *comp_class_name
,
294 enum bt_component_class_type comp_class_type
)
296 struct bt_component_class
*comp_class
= NULL
;
297 struct bt_plugin
*plugin
;
299 BT_LOGD("Finding component class: plugin-name=\"%s\", "
300 "comp-cls-name=\"%s\", comp-cls-type=%d",
301 plugin_name
, comp_class_name
, comp_class_type
);
303 plugin
= find_plugin(plugin_name
);
309 comp_class
= bt_plugin_get_component_class_by_name_and_type(plugin
,
310 comp_class_name
, comp_class_type
);
314 if (BT_LOG_ON_DEBUG
) {
316 BT_LOGD("Found component class: comp-cls-addr=%p",
319 BT_LOGD("Cannot find component class.");
327 void print_indent(FILE *fp
, size_t indent
)
331 for (i
= 0; i
< indent
; i
++) {
337 const char *component_type_str(enum bt_component_class_type type
)
340 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
342 case BT_COMPONENT_CLASS_TYPE_SINK
:
344 case BT_COMPONENT_CLASS_TYPE_FILTER
:
346 case BT_COMPONENT_CLASS_TYPE_UNKNOWN
:
353 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
354 const char *comp_cls_name
, enum bt_component_class_type type
)
356 GString
*shell_plugin_name
= NULL
;
357 GString
*shell_comp_cls_name
= NULL
;
359 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
360 if (!shell_plugin_name
) {
364 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
365 if (!shell_comp_cls_name
) {
369 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
370 bt_common_color_bold(),
371 bt_common_color_fg_cyan(),
372 component_type_str(type
),
373 bt_common_color_fg_default(),
374 bt_common_color_fg_blue(),
375 shell_plugin_name
->str
,
376 bt_common_color_fg_default(),
377 bt_common_color_fg_yellow(),
378 shell_comp_cls_name
->str
,
379 bt_common_color_reset());
382 if (shell_plugin_name
) {
383 g_string_free(shell_plugin_name
, TRUE
);
386 if (shell_comp_cls_name
) {
387 g_string_free(shell_comp_cls_name
, TRUE
);
392 void print_value(FILE *, struct bt_value
*, size_t);
395 void print_value_rec(FILE *, struct bt_value
*, size_t);
397 struct print_map_value_data
{
403 bt_bool
print_map_value(const char *key
, struct bt_value
*object
, void *data
)
405 struct print_map_value_data
*print_map_value_data
= data
;
407 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
408 fprintf(print_map_value_data
->fp
, "%s: ", key
);
410 if (bt_value_is_array(object
) &&
411 bt_value_array_is_empty(object
)) {
412 fprintf(print_map_value_data
->fp
, "[ ]\n");
416 if (bt_value_is_map(object
) &&
417 bt_value_map_is_empty(object
)) {
418 fprintf(print_map_value_data
->fp
, "{ }\n");
422 if (bt_value_is_array(object
) ||
423 bt_value_is_map(object
)) {
424 fprintf(print_map_value_data
->fp
, "\n");
427 print_value_rec(print_map_value_data
->fp
, object
,
428 print_map_value_data
->indent
+ 2);
433 void print_value_rec(FILE *fp
, struct bt_value
*value
, size_t indent
)
441 enum bt_value_status status
;
447 switch (bt_value_get_type(value
)) {
448 case BT_VALUE_TYPE_NULL
:
449 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
450 bt_common_color_reset());
452 case BT_VALUE_TYPE_BOOL
:
453 status
= bt_value_bool_get(value
, &bool_val
);
454 if (status
!= BT_VALUE_STATUS_OK
) {
457 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
458 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
459 bt_common_color_reset());
461 case BT_VALUE_TYPE_INTEGER
:
462 status
= bt_value_integer_get(value
, &int_val
);
463 if (status
!= BT_VALUE_STATUS_OK
) {
466 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
467 bt_common_color_fg_red(), int_val
,
468 bt_common_color_reset());
470 case BT_VALUE_TYPE_FLOAT
:
471 status
= bt_value_float_get(value
, &dbl_val
);
472 if (status
!= BT_VALUE_STATUS_OK
) {
475 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
476 bt_common_color_fg_red(), dbl_val
,
477 bt_common_color_reset());
479 case BT_VALUE_TYPE_STRING
:
480 status
= bt_value_string_get(value
, &str_val
);
481 if (status
!= BT_VALUE_STATUS_OK
) {
484 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
485 bt_common_color_fg_green(), str_val
,
486 bt_common_color_reset());
488 case BT_VALUE_TYPE_ARRAY
:
489 size
= bt_value_array_size(value
);
495 print_indent(fp
, indent
);
496 fprintf(fp
, "[ ]\n");
500 for (i
= 0; i
< size
; i
++) {
501 struct bt_value
*element
=
502 bt_value_array_get(value
, i
);
507 print_indent(fp
, indent
);
510 if (bt_value_is_array(element
) &&
511 bt_value_array_is_empty(element
)) {
512 fprintf(fp
, "[ ]\n");
516 if (bt_value_is_map(element
) &&
517 bt_value_map_is_empty(element
)) {
518 fprintf(fp
, "{ }\n");
522 if (bt_value_is_array(element
) ||
523 bt_value_is_map(element
)) {
527 print_value_rec(fp
, element
, indent
+ 2);
531 case BT_VALUE_TYPE_MAP
:
533 struct print_map_value_data data
= {
538 if (bt_value_map_is_empty(value
)) {
539 print_indent(fp
, indent
);
540 fprintf(fp
, "{ }\n");
544 bt_value_map_foreach(value
, print_map_value
, &data
);
553 BT_LOGE("Error printing value of type %s.",
554 bt_value_type_string(bt_value_get_type(value
)));
558 void print_value(FILE *fp
, struct bt_value
*value
, size_t indent
)
560 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
561 print_indent(fp
, indent
);
564 print_value_rec(fp
, value
, indent
);
568 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
570 fprintf(stderr
, " ");
571 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
572 bt_config_component
->comp_cls_name
->str
,
573 bt_config_component
->type
);
574 fprintf(stderr
, ":\n");
576 if (bt_config_component
->instance_name
->len
> 0) {
577 fprintf(stderr
, " Name: %s\n",
578 bt_config_component
->instance_name
->str
);
581 fprintf(stderr
, " Parameters:\n");
582 print_value(stderr
, bt_config_component
->params
, 8);
586 void print_bt_config_components(GPtrArray
*array
)
590 for (i
= 0; i
< array
->len
; i
++) {
591 struct bt_config_component
*cfg_component
=
592 bt_config_get_component(array
, i
);
593 print_bt_config_component(cfg_component
);
594 BT_PUT(cfg_component
);
599 void print_plugin_paths(struct bt_value
*plugin_paths
)
601 fprintf(stderr
, " Plugin paths:\n");
602 print_value(stderr
, plugin_paths
, 4);
606 void print_cfg_run(struct bt_config
*cfg
)
610 print_plugin_paths(cfg
->plugin_paths
);
611 fprintf(stderr
, " Source component instances:\n");
612 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
614 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
615 fprintf(stderr
, " Filter component instances:\n");
616 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
619 fprintf(stderr
, " Sink component instances:\n");
620 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
621 fprintf(stderr
, " Connections:\n");
623 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
624 struct bt_config_connection
*cfg_connection
=
625 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
628 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
629 cfg_connection
->upstream_comp_name
->str
,
630 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
631 cfg_connection
->upstream_port_glob
->str
,
632 cfg_connection
->downstream_comp_name
->str
,
633 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
634 cfg_connection
->downstream_port_glob
->str
);
639 void print_cfg_list_plugins(struct bt_config
*cfg
)
641 print_plugin_paths(cfg
->plugin_paths
);
645 void print_cfg_help(struct bt_config
*cfg
)
647 print_plugin_paths(cfg
->plugin_paths
);
651 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
653 print_plugin_paths(cfg
->plugin_paths
);
654 fprintf(stderr
, " Path: %s\n",
655 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
659 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
661 print_plugin_paths(cfg
->plugin_paths
);
662 fprintf(stderr
, " URL: %s\n",
663 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
667 void print_cfg_query(struct bt_config
*cfg
)
669 print_plugin_paths(cfg
->plugin_paths
);
670 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
671 fprintf(stderr
, " Component class:\n");
672 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
676 void print_cfg(struct bt_config
*cfg
)
678 if (!BT_LOG_ON_INFO
) {
682 BT_LOGI_STR("Configuration:");
683 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
684 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
686 switch (cfg
->command
) {
687 case BT_CONFIG_COMMAND_RUN
:
690 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
691 print_cfg_list_plugins(cfg
);
693 case BT_CONFIG_COMMAND_HELP
:
696 case BT_CONFIG_COMMAND_QUERY
:
697 print_cfg_query(cfg
);
699 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
700 print_cfg_print_ctf_metadata(cfg
);
702 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
703 print_cfg_print_lttng_live_sessions(cfg
);
711 void add_to_loaded_plugins(struct bt_plugin_set
*plugin_set
)
716 count
= bt_plugin_set_get_plugin_count(plugin_set
);
719 for (i
= 0; i
< count
; i
++) {
720 struct bt_plugin
*plugin
=
721 bt_plugin_set_get_plugin(plugin_set
, i
);
722 struct bt_plugin
*loaded_plugin
=
723 find_plugin(bt_plugin_get_name(plugin
));
728 BT_LOGI("Not using plugin: another one already exists with the same name: "
729 "plugin-name=\"%s\", plugin-path=\"%s\", "
730 "existing-plugin-path=\"%s\"",
731 bt_plugin_get_name(plugin
),
732 bt_plugin_get_path(plugin
),
733 bt_plugin_get_path(loaded_plugin
));
734 bt_put(loaded_plugin
);
736 /* Add to global array. */
737 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
738 bt_plugin_get_name(plugin
));
739 g_ptr_array_add(loaded_plugins
, bt_get(plugin
));
747 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
749 int nr_paths
, i
, ret
= 0;
751 nr_paths
= bt_value_array_size(plugin_paths
);
753 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
758 BT_LOGI("Loading dynamic plugins.");
760 for (i
= 0; i
< nr_paths
; i
++) {
761 struct bt_value
*plugin_path_value
= NULL
;
762 const char *plugin_path
;
763 struct bt_plugin_set
*plugin_set
;
764 enum bt_value_status status
;
766 plugin_path_value
= bt_value_array_get(plugin_paths
, i
);
767 status
= bt_value_string_get(plugin_path_value
, &plugin_path
);
768 if (status
!= BT_VALUE_STATUS_OK
) {
769 BT_LOGD_STR("Cannot get plugin path string.");
770 BT_PUT(plugin_path_value
);
775 * Skip this if the directory does not exist because
776 * bt_plugin_create_all_from_dir() expects an existing
779 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
780 BT_LOGV("Skipping nonexistent directory path: "
781 "path=\"%s\"", plugin_path
);
782 BT_PUT(plugin_path_value
);
786 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
788 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
790 BT_PUT(plugin_path_value
);
794 add_to_loaded_plugins(plugin_set
);
796 BT_PUT(plugin_path_value
);
803 int load_static_plugins(void)
806 struct bt_plugin_set
*plugin_set
;
808 BT_LOGI("Loading static plugins.");
809 plugin_set
= bt_plugin_create_all_from_static();
811 BT_LOGE("Unable to load static plugins.");
816 add_to_loaded_plugins(plugin_set
);
823 int load_all_plugins(struct bt_value
*plugin_paths
)
827 if (load_dynamic_plugins(plugin_paths
)) {
832 if (load_static_plugins()) {
837 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
844 void print_plugin_info(struct bt_plugin
*plugin
)
846 unsigned int major
, minor
, patch
;
848 enum bt_plugin_status version_status
;
849 const char *plugin_name
;
853 const char *plugin_description
;
855 plugin_name
= bt_plugin_get_name(plugin
);
856 path
= bt_plugin_get_path(plugin
);
857 author
= bt_plugin_get_author(plugin
);
858 license
= bt_plugin_get_license(plugin
);
859 plugin_description
= bt_plugin_get_description(plugin
);
860 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
862 printf("%s%s%s%s:\n", bt_common_color_bold(),
863 bt_common_color_fg_blue(), plugin_name
,
864 bt_common_color_reset());
866 printf(" %sPath%s: %s\n", bt_common_color_bold(),
867 bt_common_color_reset(), path
);
872 if (version_status
== BT_PLUGIN_STATUS_OK
) {
873 printf(" %sVersion%s: %u.%u.%u",
874 bt_common_color_bold(), bt_common_color_reset(),
875 major
, minor
, patch
);
884 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
885 bt_common_color_reset(),
886 plugin_description
? plugin_description
: "(None)");
887 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
888 bt_common_color_reset(), author
? author
: "(Unknown)");
889 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
890 bt_common_color_reset(),
891 license
? license
: "(Unknown)");
895 int cmd_query(struct bt_config
*cfg
)
898 struct bt_component_class
*comp_cls
= NULL
;
899 struct bt_value
*results
= NULL
;
900 const char *fail_reason
= NULL
;
902 comp_cls
= find_component_class(cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
903 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
904 cfg
->cmd_data
.query
.cfg_component
->type
);
906 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
907 "comp-cls-name=\"%s\", comp-cls-type=%d",
908 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
909 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
910 cfg
->cmd_data
.query
.cfg_component
->type
);
911 fprintf(stderr
, "%s%sCannot find component class %s",
912 bt_common_color_bold(),
913 bt_common_color_fg_red(),
914 bt_common_color_reset());
915 print_plugin_comp_cls_opt(stderr
,
916 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
917 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
918 cfg
->cmd_data
.query
.cfg_component
->type
);
919 fprintf(stderr
, "\n");
924 ret
= query(comp_cls
, cfg
->cmd_data
.query
.object
->str
,
925 cfg
->cmd_data
.query
.cfg_component
->params
, &results
,
931 print_value(stdout
, results
, 0);
935 BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", "
936 "comp-cls-name=\"%s\", comp-cls-type=%d "
937 "object=\"%s\"", fail_reason
,
938 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
939 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
940 cfg
->cmd_data
.query
.cfg_component
->type
,
941 cfg
->cmd_data
.query
.object
->str
);
942 fprintf(stderr
, "%s%sFailed to query info to %s",
943 bt_common_color_bold(),
944 bt_common_color_fg_red(),
945 bt_common_color_reset());
946 print_plugin_comp_cls_opt(stderr
,
947 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
948 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
949 cfg
->cmd_data
.query
.cfg_component
->type
);
950 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
951 bt_common_color_bold(),
952 bt_common_color_fg_red(),
953 cfg
->cmd_data
.query
.object
->str
,
955 bt_common_color_reset());
965 int cmd_help(struct bt_config
*cfg
)
968 struct bt_plugin
*plugin
= NULL
;
971 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
973 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
974 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
975 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
976 bt_common_color_bold(), bt_common_color_fg_red(),
977 bt_common_color_fg_blue(),
978 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
979 bt_common_color_reset());
984 print_plugin_info(plugin
);
985 printf(" %sComponent classes%s: %d\n",
986 bt_common_color_bold(),
987 bt_common_color_reset(),
988 (int) bt_plugin_get_component_class_count(plugin
));
991 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
992 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
993 struct bt_component_class
*needed_comp_cls
=
994 find_component_class(
995 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
996 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
997 cfg
->cmd_data
.help
.cfg_component
->type
);
999 if (!needed_comp_cls
) {
1000 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1001 "comp-cls-name=\"%s\", comp-cls-type=%d",
1002 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1003 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1004 cfg
->cmd_data
.help
.cfg_component
->type
);
1005 fprintf(stderr
, "\n%s%sCannot find component class %s",
1006 bt_common_color_bold(),
1007 bt_common_color_fg_red(),
1008 bt_common_color_reset());
1009 print_plugin_comp_cls_opt(stderr
,
1010 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1011 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1012 cfg
->cmd_data
.help
.cfg_component
->type
);
1013 fprintf(stderr
, "\n");
1018 bt_put(needed_comp_cls
);
1021 for (i
= 0; i
< bt_plugin_get_component_class_count(plugin
); i
++) {
1022 struct bt_component_class
*comp_cls
=
1023 bt_plugin_get_component_class_by_index(plugin
, i
);
1024 const char *comp_class_name
=
1025 bt_component_class_get_name(comp_cls
);
1026 const char *comp_class_description
=
1027 bt_component_class_get_description(comp_cls
);
1028 const char *comp_class_help
=
1029 bt_component_class_get_help(comp_cls
);
1030 enum bt_component_class_type type
=
1031 bt_component_class_get_type(comp_cls
);
1035 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
1036 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
1037 if (strcmp(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1038 comp_class_name
) != 0 ||
1039 type
!= cfg
->cmd_data
.help
.cfg_component
->type
) {
1046 print_plugin_comp_cls_opt(stdout
,
1047 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1051 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1052 bt_common_color_reset(),
1053 comp_class_description
? comp_class_description
: "(None)");
1055 if (comp_class_help
) {
1056 printf("\n%s\n", comp_class_help
);
1068 int cmd_list_plugins(struct bt_config
*cfg
)
1071 int plugins_count
, component_classes_count
= 0, i
;
1073 printf("From the following plugin paths:\n\n");
1074 print_value(stdout
, cfg
->plugin_paths
, 2);
1076 plugins_count
= loaded_plugins
->len
;
1077 if (plugins_count
== 0) {
1078 printf("No plugins found.\n");
1082 for (i
= 0; i
< plugins_count
; i
++) {
1083 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1085 component_classes_count
+= bt_plugin_get_component_class_count(plugin
);
1088 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1089 bt_common_color_bold(),
1090 component_classes_count
,
1091 bt_common_color_reset(),
1092 bt_common_color_bold(),
1094 bt_common_color_reset());
1096 for (i
= 0; i
< plugins_count
; i
++) {
1098 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1100 component_classes_count
=
1101 bt_plugin_get_component_class_count(plugin
);
1103 print_plugin_info(plugin
);
1105 if (component_classes_count
== 0) {
1106 printf(" %sComponent classes%s: (none)\n",
1107 bt_common_color_bold(),
1108 bt_common_color_reset());
1110 printf(" %sComponent classes%s:\n",
1111 bt_common_color_bold(),
1112 bt_common_color_reset());
1115 for (j
= 0; j
< component_classes_count
; j
++) {
1116 struct bt_component_class
*comp_class
=
1117 bt_plugin_get_component_class_by_index(
1119 const char *comp_class_name
=
1120 bt_component_class_get_name(comp_class
);
1121 const char *comp_class_description
=
1122 bt_component_class_get_description(comp_class
);
1123 enum bt_component_class_type type
=
1124 bt_component_class_get_type(comp_class
);
1127 print_plugin_comp_cls_opt(stdout
,
1128 bt_plugin_get_name(plugin
), comp_class_name
,
1131 if (comp_class_description
) {
1132 printf(": %s", comp_class_description
);
1145 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1148 struct bt_component_class
*comp_cls
= NULL
;
1149 struct bt_value
*results
= NULL
;
1150 struct bt_value
*params
= NULL
;
1151 struct bt_value
*map
= NULL
;
1152 struct bt_value
*v
= NULL
;
1153 static const char * const plugin_name
= "ctf";
1154 static const char * const comp_cls_name
= "lttng-live";
1155 static const enum bt_component_class_type comp_cls_type
=
1156 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1157 int64_t array_size
, i
;
1158 const char *fail_reason
= NULL
;
1160 assert(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1161 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1164 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1165 "comp-cls-name=\"%s\", comp-cls-type=%d",
1166 plugin_name
, comp_cls_name
,
1167 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1168 fprintf(stderr
, "%s%sCannot find component class %s",
1169 bt_common_color_bold(),
1170 bt_common_color_fg_red(),
1171 bt_common_color_reset());
1172 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1173 comp_cls_name
, comp_cls_type
);
1174 fprintf(stderr
, "\n");
1178 params
= bt_value_map_create();
1183 ret
= bt_value_map_insert_string(params
, "url",
1184 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1189 ret
= query(comp_cls
, "sessions", params
, &results
, &fail_reason
);
1194 if (!bt_value_is_array(results
)) {
1195 BT_LOGE_STR("Expecting an array for sessions query.");
1196 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1197 bt_common_color_bold(),
1198 bt_common_color_fg_red(),
1199 bt_common_color_reset());
1203 array_size
= bt_value_array_size(results
);
1204 for (i
= 0; i
< array_size
; i
++) {
1205 const char *url_text
;
1206 int64_t timer_us
, streams
, clients
;
1208 map
= bt_value_array_get(results
, i
);
1210 BT_LOGE_STR("Unexpected empty array entry.");
1213 if (!bt_value_is_map(map
)) {
1214 BT_LOGE_STR("Unexpected entry type.");
1218 v
= bt_value_map_get(map
, "url");
1220 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1223 ret
= bt_value_string_get(v
, &url_text
);
1225 printf("%s", url_text
);
1228 v
= bt_value_map_get(map
, "timer-us");
1230 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1233 ret
= bt_value_integer_get(v
, &timer_us
);
1235 printf(" (timer = %" PRIu64
", ", timer_us
);
1238 v
= bt_value_map_get(map
, "stream-count");
1240 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1243 ret
= bt_value_integer_get(v
, &streams
);
1245 printf("%" PRIu64
" stream(s), ", streams
);
1248 v
= bt_value_map_get(map
, "client-count");
1250 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1253 ret
= bt_value_integer_get(v
, &clients
);
1255 printf("%" PRIu64
" client(s) connected)\n", clients
);
1264 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1265 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1266 bt_common_color_bold(),
1267 bt_common_color_fg_red(),
1269 bt_common_color_reset());
1284 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1287 struct bt_component_class
*comp_cls
= NULL
;
1288 struct bt_value
*results
= NULL
;
1289 struct bt_value
*params
= NULL
;
1290 struct bt_value
*metadata_text_value
= NULL
;
1291 const char *metadata_text
= NULL
;
1292 static const char * const plugin_name
= "ctf";
1293 static const char * const comp_cls_name
= "fs";
1294 static const enum bt_component_class_type comp_cls_type
=
1295 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1296 const char *fail_reason
= NULL
;
1298 assert(cfg
->cmd_data
.print_ctf_metadata
.path
);
1299 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1302 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1303 "comp-cls-name=\"%s\", comp-cls-type=%d",
1304 plugin_name
, comp_cls_name
,
1305 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1306 fprintf(stderr
, "%s%sCannot find component class %s",
1307 bt_common_color_bold(),
1308 bt_common_color_fg_red(),
1309 bt_common_color_reset());
1310 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1311 comp_cls_name
, comp_cls_type
);
1312 fprintf(stderr
, "\n");
1317 params
= bt_value_map_create();
1323 ret
= bt_value_map_insert_string(params
, "path",
1324 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1330 ret
= query(comp_cls
, "metadata-info", params
, &results
, &fail_reason
);
1335 metadata_text_value
= bt_value_map_get(results
, "text");
1336 if (!metadata_text_value
) {
1337 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1342 ret
= bt_value_string_get(metadata_text_value
, &metadata_text
);
1344 printf("%s\n", metadata_text
);
1349 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1350 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1351 bt_common_color_bold(),
1352 bt_common_color_fg_red(),
1354 bt_common_color_reset());
1357 destroy_the_query_executor();
1360 bt_put(metadata_text_value
);
1366 char *instance_name
;
1370 struct trace_range
{
1371 uint64_t intersection_range_begin_ns
;
1372 uint64_t intersection_range_end_ns
;
1376 guint
port_id_hash(gconstpointer v
)
1378 const struct port_id
*id
= v
;
1380 assert(id
->instance_name
);
1381 assert(id
->port_name
);
1383 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1387 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1389 const struct port_id
*id1
= v1
;
1390 const struct port_id
*id2
= v2
;
1392 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1393 !strcmp(id1
->port_name
, id2
->port_name
);
1397 void port_id_destroy(gpointer data
)
1399 struct port_id
*id
= data
;
1401 free(id
->instance_name
);
1402 free(id
->port_name
);
1407 void trace_range_destroy(gpointer data
)
1412 struct cmd_run_ctx
{
1414 GHashTable
*components
;
1417 struct bt_graph
*graph
;
1420 struct bt_config
*cfg
;
1424 bool stream_intersection_mode
;
1427 * Association of struct port_id -> struct trace_range.
1429 GHashTable
*intersections
;
1432 /* Returns a timestamp of the form "(-)s.ns" */
1434 char *s_from_ns(int64_t ns
)
1439 int64_t ts_sec_abs
, ts_nsec_abs
;
1440 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1441 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1443 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1444 is_negative
= false;
1445 ts_sec_abs
= ts_sec
;
1446 ts_nsec_abs
= ts_nsec
;
1447 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1448 is_negative
= false;
1449 ts_sec_abs
= ts_sec
- 1;
1450 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1451 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1453 ts_sec_abs
= ts_sec
;
1454 ts_nsec_abs
= -ts_nsec
;
1455 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1457 ts_sec_abs
= -(ts_sec
+ 1);
1458 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1459 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1461 ts_sec_abs
= -ts_sec
;
1462 ts_nsec_abs
= ts_nsec
;
1463 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1465 ts_sec_abs
= -ts_sec
;
1466 ts_nsec_abs
= -ts_nsec
;
1469 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1470 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1478 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1479 struct cmd_run_ctx
*ctx
, struct bt_component
*upstream_comp
,
1480 struct bt_port
*upstream_port
,
1481 struct bt_config_connection
*cfg_conn
)
1484 GQuark downstreamp_comp_name_quark
;
1485 struct bt_component
*downstream_comp
;
1486 int64_t downstream_port_count
;
1488 int64_t (*port_count_fn
)(struct bt_component
*);
1489 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
1490 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1491 bool insert_trimmer
= false;
1492 struct bt_value
*trimmer_params
= NULL
;
1493 char *intersection_begin
= NULL
;
1494 char *intersection_end
= NULL
;
1495 struct bt_component
*trimmer
= NULL
;
1496 struct bt_component_class
*trimmer_class
= NULL
;
1497 struct bt_port
*trimmer_input
= NULL
;
1498 struct bt_port
*trimmer_output
= NULL
;
1500 if (ctx
->intersections
&&
1501 bt_component_get_class_type(upstream_comp
) ==
1502 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1503 struct trace_range
*range
;
1504 struct port_id port_id
= {
1505 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1506 .port_name
= (char *) bt_port_get_name(upstream_port
)
1509 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1513 range
= (struct trace_range
*) g_hash_table_lookup(
1514 ctx
->intersections
, &port_id
);
1516 enum bt_value_status status
;
1518 intersection_begin
= s_from_ns(
1519 range
->intersection_range_begin_ns
);
1520 intersection_end
= s_from_ns(
1521 range
->intersection_range_end_ns
);
1522 if (!intersection_begin
|| !intersection_end
) {
1523 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1527 insert_trimmer
= true;
1528 trimmer_params
= bt_value_map_create();
1529 if (!trimmer_params
) {
1533 status
= bt_value_map_insert_string(trimmer_params
,
1534 "begin", intersection_begin
);
1535 if (status
!= BT_VALUE_STATUS_OK
) {
1538 status
= bt_value_map_insert_string(trimmer_params
,
1539 "end", intersection_end
);
1540 if (status
!= BT_VALUE_STATUS_OK
) {
1545 trimmer_class
= find_component_class("utils", "trimmer",
1546 BT_COMPONENT_CLASS_TYPE_FILTER
);
1547 if (!trimmer_class
) {
1552 BT_LOGI("Connecting upstream port to the next available downstream port: "
1553 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1554 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1555 upstream_port
, bt_port_get_name(upstream_port
),
1556 cfg_conn
->downstream_comp_name
->str
,
1557 cfg_conn
->arg
->str
);
1558 downstreamp_comp_name_quark
= g_quark_from_string(
1559 cfg_conn
->downstream_comp_name
->str
);
1560 assert(downstreamp_comp_name_quark
> 0);
1561 downstream_comp
= g_hash_table_lookup(ctx
->components
,
1562 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1563 if (!downstream_comp
) {
1564 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1565 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1566 cfg_conn
->arg
->str
);
1567 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1568 cfg_conn
->arg
->str
);
1572 if (bt_component_is_filter(downstream_comp
)) {
1573 port_count_fn
= bt_component_filter_get_input_port_count
;
1574 port_by_index_fn
= bt_component_filter_get_input_port_by_index
;
1575 } else if (bt_component_is_sink(downstream_comp
)) {
1576 port_count_fn
= bt_component_sink_get_input_port_count
;
1577 port_by_index_fn
= bt_component_sink_get_input_port_by_index
;
1580 * Should never happen because the connections are
1581 * validated before we get here.
1583 BT_LOGF("Invalid connection: downstream component is a source: "
1584 "conn-arg=\"%s\"", cfg_conn
->arg
->str
);
1588 downstream_port_count
= port_count_fn(downstream_comp
);
1589 assert(downstream_port_count
>= 0);
1591 for (i
= 0; i
< downstream_port_count
; i
++) {
1592 struct bt_port
*downstream_port
=
1593 port_by_index_fn(downstream_comp
, i
);
1594 const char *upstream_port_name
;
1595 const char *downstream_port_name
;
1597 assert(downstream_port
);
1599 /* Skip port if it's already connected. */
1600 if (bt_port_is_connected(downstream_port
)) {
1601 bt_put(downstream_port
);
1602 BT_LOGD("Skipping downstream port: already connected: "
1603 "port-addr=%p, port-name=\"%s\"",
1605 bt_port_get_name(downstream_port
));
1609 downstream_port_name
= bt_port_get_name(downstream_port
);
1610 assert(downstream_port_name
);
1611 upstream_port_name
= bt_port_get_name(upstream_port
);
1612 assert(upstream_port_name
);
1614 if (!bt_common_star_glob_match(
1615 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1616 downstream_port_name
, SIZE_MAX
)) {
1617 bt_put(downstream_port
);
1621 if (insert_trimmer
) {
1623 * In order to insert the trimmer between the two
1624 * components that were being connected, we create
1625 * a connection configuration entry which describes
1626 * a connection from the trimmer's output to the
1627 * original input that was being connected.
1629 * Hence, the creation of the trimmer will cause the
1630 * graph "new port" listener to establish all downstream
1631 * connections as its output port is connected. We will
1632 * then establish the connection between the original
1633 * upstream source and the trimmer.
1635 char *trimmer_name
= NULL
;
1636 enum bt_graph_status graph_status
;
1638 ret
= asprintf(&trimmer_name
, "%s-%s",
1639 "stream-intersection-trimmer",
1640 upstream_port_name
);
1646 ctx
->connect_ports
= false;
1647 graph_status
= bt_graph_add_component(ctx
->graph
,
1648 trimmer_class
, trimmer_name
, trimmer_params
,
1651 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1657 bt_component_filter_get_input_port_by_index(
1659 if (!trimmer_input
) {
1663 bt_component_filter_get_output_port_by_index(
1665 if (!trimmer_output
) {
1670 * Replace the current downstream port by the trimmer's
1673 BT_MOVE(downstream_port
, trimmer_input
);
1674 downstream_port_name
= bt_port_get_name(
1676 if (!downstream_port_name
) {
1681 /* We have a winner! */
1682 status
= bt_graph_connect_ports(ctx
->graph
,
1683 upstream_port
, downstream_port
, NULL
);
1684 BT_PUT(downstream_port
);
1686 case BT_GRAPH_STATUS_OK
:
1688 case BT_GRAPH_STATUS_CANCELED
:
1689 BT_LOGI_STR("Graph was canceled by user.");
1690 status
= BT_GRAPH_STATUS_OK
;
1692 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1693 BT_LOGE("A component refused a connection to one of its ports: "
1694 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1695 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1696 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1697 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1699 upstream_comp
, bt_component_get_name(upstream_comp
),
1700 upstream_port
, bt_port_get_name(upstream_port
),
1701 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1702 downstream_port
, downstream_port_name
,
1703 cfg_conn
->arg
->str
);
1705 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1706 bt_port_get_name(upstream_port
),
1707 downstream_port_name
,
1708 cfg_conn
->arg
->str
);
1711 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1712 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1713 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1714 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1715 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1717 upstream_comp
, bt_component_get_name(upstream_comp
),
1718 upstream_port
, bt_port_get_name(upstream_port
),
1719 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1720 downstream_port
, downstream_port_name
,
1721 cfg_conn
->arg
->str
);
1723 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1724 bt_port_get_name(upstream_port
),
1725 downstream_port_name
,
1726 cfg_conn
->arg
->str
);
1730 BT_LOGI("Connected component ports: "
1731 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1732 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1733 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1734 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1736 upstream_comp
, bt_component_get_name(upstream_comp
),
1737 upstream_port
, bt_port_get_name(upstream_port
),
1738 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1739 downstream_port
, downstream_port_name
,
1740 cfg_conn
->arg
->str
);
1742 if (insert_trimmer
) {
1744 * The first connection, from the source to the trimmer,
1745 * has been done. We now connect the trimmer to the
1746 * original downstream port.
1748 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1749 ctx
, trimmer
, trimmer_output
, cfg_conn
);
1753 ctx
->connect_ports
= true;
1757 * We found a matching downstream port: the search is
1763 /* No downstream port found */
1764 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1765 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1766 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1767 upstream_port
, bt_port_get_name(upstream_port
),
1768 cfg_conn
->downstream_comp_name
->str
,
1769 cfg_conn
->arg
->str
);
1771 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1772 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1778 free(intersection_begin
);
1779 free(intersection_end
);
1780 BT_PUT(trimmer_params
);
1781 BT_PUT(trimmer_class
);
1783 BT_PUT(trimmer_input
);
1784 BT_PUT(trimmer_output
);
1789 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1790 struct bt_port
*upstream_port
)
1793 const char *upstream_port_name
;
1794 const char *upstream_comp_name
;
1795 struct bt_component
*upstream_comp
= NULL
;
1799 assert(upstream_port
);
1800 upstream_port_name
= bt_port_get_name(upstream_port
);
1801 assert(upstream_port_name
);
1802 upstream_comp
= bt_port_get_component(upstream_port
);
1803 if (!upstream_comp
) {
1804 BT_LOGW("Upstream port to connect is not part of a component: "
1805 "port-addr=%p, port-name=\"%s\"",
1806 upstream_port
, upstream_port_name
);
1811 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1812 assert(upstream_comp_name
);
1813 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1814 "port-addr=%p, port-name=\"%s\"",
1815 upstream_comp
, upstream_comp_name
,
1816 upstream_port
, upstream_port_name
);
1818 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1819 struct bt_config_connection
*cfg_conn
=
1821 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1823 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1824 upstream_comp_name
)) {
1828 if (!bt_common_star_glob_match(
1829 cfg_conn
->upstream_port_glob
->str
,
1830 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1834 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1835 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1837 BT_LOGE("Cannot connect upstream port: "
1838 "port-addr=%p, port-name=\"%s\"",
1840 upstream_port_name
);
1842 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1845 cfg_conn
->arg
->str
);
1851 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1852 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1853 upstream_port_name
);
1855 "Cannot create connection: upstream port `%s` does not match any connection\n",
1856 upstream_port_name
);
1862 bt_put(upstream_comp
);
1867 void graph_port_added_listener(struct bt_port
*port
, void *data
)
1869 struct bt_component
*comp
= NULL
;
1870 struct cmd_run_ctx
*ctx
= data
;
1872 comp
= bt_port_get_component(port
);
1873 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1874 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1875 comp
, comp
? bt_component_get_name(comp
) : "",
1876 port
, bt_port_get_name(port
));
1878 if (!ctx
->connect_ports
) {
1883 BT_LOGW_STR("Port has no component.");
1887 if (bt_port_is_connected(port
)) {
1888 BT_LOGW_STR("Port is already connected.");
1892 if (!bt_port_is_output(port
)) {
1893 BT_LOGI_STR("Skipping input port.");
1897 if (cmd_run_ctx_connect_upstream_port(ctx
, port
)) {
1898 BT_LOGF_STR("Cannot connect upstream port.");
1899 fprintf(stderr
, "Added port could not be connected: aborting\n");
1909 void graph_port_removed_listener(struct bt_component
*component
,
1910 struct bt_port
*port
, void *data
)
1912 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1913 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1914 component
, bt_component_get_name(component
),
1915 port
, bt_port_get_name(port
));
1919 void graph_ports_connected_listener(struct bt_port
*upstream_port
,
1920 struct bt_port
*downstream_port
, void *data
)
1922 struct bt_component
*upstream_comp
= bt_port_get_component(upstream_port
);
1923 struct bt_component
*downstream_comp
= bt_port_get_component(downstream_port
);
1925 assert(upstream_comp
);
1926 assert(downstream_comp
);
1927 BT_LOGI("Graph's component ports connected: "
1928 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1929 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1930 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1931 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1932 upstream_comp
, bt_component_get_name(upstream_comp
),
1933 upstream_port
, bt_port_get_name(upstream_port
),
1934 downstream_comp
, bt_component_get_name(downstream_comp
),
1935 downstream_port
, bt_port_get_name(downstream_port
));
1936 bt_put(upstream_comp
);
1937 bt_put(downstream_comp
);
1941 void graph_ports_disconnected_listener(
1942 struct bt_component
*upstream_component
,
1943 struct bt_component
*downstream_component
,
1944 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
1947 BT_LOGI("Graph's component ports disconnected: "
1948 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1949 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1950 upstream_port
, bt_port_get_name(upstream_port
),
1951 downstream_port
, bt_port_get_name(downstream_port
));
1955 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1961 if (ctx
->components
) {
1962 g_hash_table_destroy(ctx
->components
);
1963 ctx
->components
= NULL
;
1966 if (ctx
->intersections
) {
1967 g_hash_table_destroy(ctx
->intersections
);
1968 ctx
->intersections
= NULL
;
1977 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1982 ctx
->connect_ports
= false;
1983 ctx
->components
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
1985 if (!ctx
->components
) {
1989 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1990 ctx
->stream_intersection_mode
= true;
1991 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1992 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1993 if (!ctx
->intersections
) {
1998 ctx
->graph
= bt_graph_create();
2003 the_graph
= ctx
->graph
;
2004 ret
= bt_graph_add_port_added_listener(ctx
->graph
,
2005 graph_port_added_listener
, NULL
, ctx
);
2007 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2011 ret
= bt_graph_add_port_removed_listener(ctx
->graph
,
2012 graph_port_removed_listener
, NULL
, ctx
);
2014 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
2018 ret
= bt_graph_add_ports_connected_listener(ctx
->graph
,
2019 graph_ports_connected_listener
, NULL
, ctx
);
2021 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
2025 ret
= bt_graph_add_ports_disconnected_listener(ctx
->graph
,
2026 graph_ports_disconnected_listener
, NULL
, ctx
);
2028 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
2035 cmd_run_ctx_destroy(ctx
);
2043 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2044 struct bt_config_component
*cfg_comp
,
2045 struct bt_component_class
*comp_cls
)
2049 int64_t trace_count
;
2050 enum bt_value_status value_status
;
2051 const char *path
= NULL
;
2052 struct bt_value
*component_path_value
= NULL
;
2053 struct bt_value
*query_params
= NULL
;
2054 struct bt_value
*query_result
= NULL
;
2055 struct bt_value
*trace_info
= NULL
;
2056 struct bt_value
*intersection_range
= NULL
;
2057 struct bt_value
*intersection_begin
= NULL
;
2058 struct bt_value
*intersection_end
= NULL
;
2059 struct bt_value
*stream_path_value
= NULL
;
2060 struct bt_value
*stream_paths
= NULL
;
2061 struct bt_value
*stream_infos
= NULL
;
2062 struct bt_value
*stream_info
= NULL
;
2063 struct port_id
*port_id
= NULL
;
2064 struct trace_range
*trace_range
= NULL
;
2065 const char *fail_reason
= NULL
;
2067 component_path_value
= bt_value_map_get(cfg_comp
->params
, "path");
2068 if (!bt_value_is_string(component_path_value
)) {
2069 BT_LOGD("Cannot get path parameter: component-name=%s",
2070 cfg_comp
->instance_name
->str
);
2075 value_status
= bt_value_string_get(component_path_value
, &path
);
2076 if (value_status
!= BT_VALUE_STATUS_OK
) {
2077 BT_LOGD("Cannot get path string value: component-name=%s",
2078 cfg_comp
->instance_name
->str
);
2083 query_params
= bt_value_map_create();
2084 if (!query_params
) {
2085 BT_LOGE_STR("Cannot create query parameters.");
2090 value_status
= bt_value_map_insert(query_params
, "path", component_path_value
);
2091 if (value_status
!= BT_VALUE_STATUS_OK
) {
2092 BT_LOGE_STR("Cannot insert path parameter in query paramater map.");
2097 ret
= query(comp_cls
, "trace-info", query_params
, &query_result
,
2100 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2101 "comp-class-name=\"%s\"", fail_reason
,
2102 bt_component_class_get_name(comp_cls
));
2107 if (!bt_value_is_array(query_result
)) {
2108 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2109 "component-class-name=%s",
2110 bt_component_class_get_name(comp_cls
));
2115 trace_count
= bt_value_array_size(query_result
);
2116 if (trace_count
< 0) {
2121 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2123 uint64_t stream_idx
;
2124 int64_t stream_count
;
2126 trace_info
= bt_value_array_get(query_result
, trace_idx
);
2127 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2129 BT_LOGD_STR("Cannot retrieve trace from query result.");
2133 intersection_range
= bt_value_map_get(trace_info
,
2134 "intersection-range-ns");
2135 if (!intersection_range
) {
2137 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2141 intersection_begin
= bt_value_map_get(intersection_range
,
2143 if (!intersection_begin
) {
2145 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2149 intersection_end
= bt_value_map_get(intersection_range
,
2151 if (!intersection_end
) {
2153 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2157 value_status
= bt_value_integer_get(intersection_begin
, &begin
);
2158 if (value_status
!= BT_VALUE_STATUS_OK
) {
2160 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'begin\' field from query result.");
2164 value_status
= bt_value_integer_get(intersection_end
, &end
);
2165 if (value_status
!= BT_VALUE_STATUS_OK
) {
2167 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'end\' field from query result.");
2171 if (begin
< 0 || end
< 0 || end
< begin
) {
2172 BT_LOGW("Invalid trace stream intersection values: "
2173 "intersection-range-ns:begin=%" PRId64
2174 ", intersection-range-ns:end=%" PRId64
,
2180 stream_infos
= bt_value_map_get(trace_info
, "streams");
2181 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2183 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2187 stream_count
= bt_value_array_size(stream_infos
);
2188 if (stream_count
< 0) {
2196 * The first path of a stream's "paths" is currently used to
2197 * associate streams/ports to a given trace intersection.
2199 * This is a fragile hack as it relies on the port names
2200 * being set to the various streams path.
2202 * A stream name should be introduced as part of the trace-info
2205 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2206 const char *stream_path
;
2208 port_id
= g_new0(struct port_id
, 1);
2211 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2214 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2215 if (!port_id
->instance_name
) {
2217 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2221 trace_range
= g_new0(struct trace_range
, 1);
2224 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2227 trace_range
->intersection_range_begin_ns
= begin
;
2228 trace_range
->intersection_range_end_ns
= end
;
2230 stream_info
= bt_value_array_get(stream_infos
,
2232 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2234 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2238 stream_paths
= bt_value_map_get(stream_info
, "paths");
2239 if (!stream_paths
|| !bt_value_is_array(stream_paths
)) {
2241 BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
2245 stream_path_value
= bt_value_array_get(stream_paths
, 0);
2246 if (!stream_path_value
||
2247 !bt_value_is_string(stream_path_value
)) {
2249 BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
2253 value_status
= bt_value_string_get(stream_path_value
,
2255 if (value_status
!= BT_VALUE_STATUS_OK
) {
2260 port_id
->port_name
= strdup(stream_path
);
2261 if (!port_id
->port_name
) {
2263 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2267 BT_LOGD("Inserting stream intersection ");
2269 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2273 BT_PUT(stream_info
);
2274 BT_PUT(stream_paths
);
2275 BT_PUT(stream_path_value
);
2279 BT_PUT(stream_paths
);
2280 BT_PUT(stream_path_value
);
2281 BT_PUT(intersection_range
);
2282 BT_PUT(intersection_begin
);
2283 BT_PUT(intersection_end
);
2284 BT_PUT(stream_paths
);
2285 BT_PUT(stream_path_value
);
2291 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2292 bt_common_color_bold(),
2293 bt_common_color_fg_yellow(),
2294 path
? path
: "(unknown)",
2295 bt_common_color_reset());
2297 bt_put(component_path_value
);
2298 bt_put(query_params
);
2299 bt_put(query_result
);
2301 bt_put(intersection_range
);
2302 bt_put(intersection_begin
);
2303 bt_put(intersection_end
);
2304 bt_put(stream_infos
);
2305 bt_put(stream_info
);
2306 bt_put(stream_paths
);
2307 bt_put(stream_path_value
);
2309 g_free(trace_range
);
2314 int cmd_run_ctx_create_components_from_config_components(
2315 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2318 struct bt_component_class
*comp_cls
= NULL
;
2319 struct bt_component
*comp
= NULL
;
2322 for (i
= 0; i
< cfg_components
->len
; i
++) {
2323 struct bt_config_component
*cfg_comp
=
2324 g_ptr_array_index(cfg_components
, i
);
2327 comp_cls
= find_component_class(cfg_comp
->plugin_name
->str
,
2328 cfg_comp
->comp_cls_name
->str
, cfg_comp
->type
);
2330 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2331 "comp-cls-name=\"%s\", comp-cls-type=%d",
2332 cfg_comp
->plugin_name
->str
,
2333 cfg_comp
->comp_cls_name
->str
,
2335 fprintf(stderr
, "%s%sCannot find component class %s",
2336 bt_common_color_bold(),
2337 bt_common_color_fg_red(),
2338 bt_common_color_reset());
2339 print_plugin_comp_cls_opt(stderr
,
2340 cfg_comp
->plugin_name
->str
,
2341 cfg_comp
->comp_cls_name
->str
,
2343 fprintf(stderr
, "\n");
2347 ret
= bt_graph_add_component(ctx
->graph
, comp_cls
,
2348 cfg_comp
->instance_name
->str
, cfg_comp
->params
, &comp
);
2350 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2351 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2353 cfg_comp
->plugin_name
->str
,
2354 cfg_comp
->comp_cls_name
->str
,
2355 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2356 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2357 bt_common_color_bold(),
2358 bt_common_color_fg_red(),
2359 cfg_comp
->instance_name
->str
,
2360 bt_common_color_reset());
2364 if (ctx
->stream_intersection_mode
&&
2365 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2366 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2372 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2373 comp
, cfg_comp
->instance_name
->str
);
2374 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2376 g_hash_table_insert(ctx
->components
,
2377 GUINT_TO_POINTER(quark
), comp
);
2394 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2399 * Make sure that, during this phase, our graph's "port added"
2400 * listener does not connect ports while we are creating the
2401 * components because we have a special, initial phase for
2404 ctx
->connect_ports
= false;
2406 ret
= cmd_run_ctx_create_components_from_config_components(
2407 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2413 ret
= cmd_run_ctx_create_components_from_config_components(
2414 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2420 ret
= cmd_run_ctx_create_components_from_config_components(
2421 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2432 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2433 struct bt_component
*comp
,
2434 int64_t (*port_count_fn
)(struct bt_component
*),
2435 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t))
2441 count
= port_count_fn(comp
);
2444 for (i
= 0; i
< count
; i
++) {
2445 struct bt_port
*upstream_port
= port_by_index_fn(comp
, i
);
2447 assert(upstream_port
);
2448 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2449 bt_put(upstream_port
);
2460 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2463 GHashTableIter iter
;
2464 gpointer g_name_quark
, g_comp
;
2466 ctx
->connect_ports
= true;
2467 g_hash_table_iter_init(&iter
, ctx
->components
);
2469 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2470 int64_t (*port_count_fn
)(struct bt_component
*);
2471 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
2473 if (bt_component_is_source(g_comp
)) {
2475 bt_component_source_get_output_port_count
;
2477 bt_component_source_get_output_port_by_index
;
2478 } else if (bt_component_is_filter(g_comp
)) {
2480 bt_component_filter_get_output_port_count
;
2482 bt_component_filter_get_output_port_by_index
;
2487 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
2488 g_comp
, port_count_fn
, port_by_index_fn
);
2499 const char *bt_graph_status_str(enum bt_graph_status status
)
2502 case BT_GRAPH_STATUS_CANCELED
:
2503 return "BT_GRAPH_STATUS_CANCELED";
2504 case BT_GRAPH_STATUS_AGAIN
:
2505 return "BT_GRAPH_STATUS_AGAIN";
2506 case BT_GRAPH_STATUS_END
:
2507 return "BT_GRAPH_STATUS_END";
2508 case BT_GRAPH_STATUS_OK
:
2509 return "BT_GRAPH_STATUS_OK";
2510 case BT_GRAPH_STATUS_INVALID
:
2511 return "BT_GRAPH_STATUS_INVALID";
2512 case BT_GRAPH_STATUS_NO_SINK
:
2513 return "BT_GRAPH_STATUS_NO_SINK";
2514 case BT_GRAPH_STATUS_ERROR
:
2515 return "BT_GRAPH_STATUS_ERROR";
2522 int cmd_run(struct bt_config
*cfg
)
2525 struct cmd_run_ctx ctx
= { 0 };
2527 /* Initialize the command's context and the graph object */
2528 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2529 BT_LOGE_STR("Cannot initialize the command's context.");
2530 fprintf(stderr
, "Cannot initialize the command's context\n");
2535 BT_LOGI_STR("Canceled by user before creating components.");
2539 BT_LOGI_STR("Creating components.");
2541 /* Create the requested component instances */
2542 if (cmd_run_ctx_create_components(&ctx
)) {
2543 BT_LOGE_STR("Cannot create components.");
2544 fprintf(stderr
, "Cannot create components\n");
2549 BT_LOGI_STR("Canceled by user before connecting components.");
2553 BT_LOGI_STR("Connecting components.");
2555 /* Connect the initially visible component ports */
2556 if (cmd_run_ctx_connect_ports(&ctx
)) {
2557 BT_LOGE_STR("Cannot connect initial component ports.");
2558 fprintf(stderr
, "Cannot connect initial component ports\n");
2563 BT_LOGI_STR("Canceled by user before running the graph.");
2567 BT_LOGI_STR("Running the graph.");
2571 enum bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2574 * Reset console in case something messed with console
2575 * codes during the graph's execution.
2577 printf("%s", bt_common_color_reset());
2579 fprintf(stderr
, "%s", bt_common_color_reset());
2580 BT_LOGV("bt_graph_run() returned: status=%s",
2581 bt_graph_status_str(graph_status
));
2583 switch (graph_status
) {
2584 case BT_GRAPH_STATUS_OK
:
2586 case BT_GRAPH_STATUS_CANCELED
:
2587 BT_LOGI_STR("Graph was canceled by user.");
2589 case BT_GRAPH_STATUS_AGAIN
:
2590 if (bt_graph_is_canceled(ctx
.graph
)) {
2591 BT_LOGI_STR("Graph was canceled by user.");
2595 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2596 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2598 cfg
->cmd_data
.run
.retry_duration_us
);
2600 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2601 if (bt_graph_is_canceled(ctx
.graph
)) {
2602 BT_LOGI_STR("Graph was canceled by user.");
2608 case BT_COMPONENT_STATUS_END
:
2611 BT_LOGE_STR("Graph failed to complete successfully");
2612 fprintf(stderr
, "Graph failed to complete successfully\n");
2625 cmd_run_ctx_destroy(&ctx
);
2630 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2632 const char *env_clash
;
2634 if (!cfg
->command_name
) {
2638 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2639 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2643 if (g_file_test(cfg
->command_name
,
2644 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2645 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2647 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2649 fprintf(stderr
, "\n");
2650 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
2656 void init_log_level(void)
2658 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2662 void set_auto_log_levels(struct bt_config
*cfg
)
2664 const char **env_var_name
;
2667 * Override the configuration's default log level if
2668 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2669 * are found for backward compatibility with legacy Babetrace 1.
2671 if (getenv("BABELTRACE_DEBUG") &&
2672 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2673 cfg
->log_level
= 'V';
2674 } else if (getenv("BABELTRACE_VERBOSE") &&
2675 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2676 cfg
->log_level
= 'I';
2680 * Set log levels according to --debug or --verbose. For
2681 * backward compatibility, --debug is more verbose than
2684 * --verbose: INFO log level
2685 * --debug: VERBOSE log level (includes DEBUG, which is
2686 * is less verbose than VERBOSE in the internal
2687 * logging framework)
2689 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2691 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2692 } else if (cfg
->debug
) {
2693 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2696 * Set library's default log level if not
2697 * explicitly specified.
2699 switch (cfg
->log_level
) {
2701 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE
);
2704 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2707 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
2710 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2713 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN
);
2716 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR
);
2719 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL
);
2727 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2729 bt_cli_log_level
= BT_LOG_INFO
;
2730 } else if (cfg
->debug
) {
2731 bt_cli_log_level
= BT_LOG_VERBOSE
;
2734 * Set CLI's default log level if not explicitly
2737 switch (cfg
->log_level
) {
2739 bt_cli_log_level
= BT_LOG_NONE
;
2742 bt_cli_log_level
= BT_LOG_VERBOSE
;
2745 bt_cli_log_level
= BT_LOG_DEBUG
;
2748 bt_cli_log_level
= BT_LOG_INFO
;
2751 bt_cli_log_level
= BT_LOG_WARN
;
2754 bt_cli_log_level
= BT_LOG_ERROR
;
2757 bt_cli_log_level
= BT_LOG_FATAL
;
2765 env_var_name
= log_level_env_var_names
;
2767 while (*env_var_name
) {
2768 if (!getenv(*env_var_name
)) {
2770 g_setenv(*env_var_name
, "I", 1);
2771 } else if (cfg
->debug
) {
2772 g_setenv(*env_var_name
, "V", 1);
2774 char val
[2] = { 0 };
2777 * Set module's default log level if not
2778 * explicitly specified.
2780 val
[0] = cfg
->log_level
;
2781 g_setenv(*env_var_name
, val
, 1);
2789 int main(int argc
, const char **argv
)
2793 struct bt_config
*cfg
;
2796 set_signal_handler();
2798 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2801 /* Quit without errors; typically usage/version */
2803 BT_LOGI_STR("Quitting without errors.");
2808 BT_LOGE("Command-line error: retcode=%d", retcode
);
2813 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2814 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2819 set_auto_log_levels(cfg
);
2822 if (cfg
->command_needs_plugins
) {
2823 ret
= load_all_plugins(cfg
->plugin_paths
);
2825 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2831 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2832 cfg
->command
, cfg
->command_name
);
2834 switch (cfg
->command
) {
2835 case BT_CONFIG_COMMAND_RUN
:
2838 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2839 ret
= cmd_list_plugins(cfg
);
2841 case BT_CONFIG_COMMAND_HELP
:
2842 ret
= cmd_help(cfg
);
2844 case BT_CONFIG_COMMAND_QUERY
:
2845 ret
= cmd_query(cfg
);
2847 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2848 ret
= cmd_print_ctf_metadata(cfg
);
2850 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2851 ret
= cmd_print_lttng_live_sessions(cfg
);
2854 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2858 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2859 cfg
->command
, cfg
->command_name
, ret
);
2860 warn_command_name_and_directory_clash(cfg
);
2861 retcode
= ret
? 1 : 0;