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_PLUGIN_CTF_BTR_LOG_LEVEL",
59 "BABELTRACE_PLUGIN_CTF_FS_SRC_LOG_LEVEL",
60 "BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_SRC_LOG_LEVEL",
61 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
62 "BABELTRACE_PLUGIN_CTF_NOTIF_ITER_LOG_LEVEL",
63 "BABELTRACE_PLUGIN_LTTNG_UTILS_DEBUG_INFO_FLT_LOG_LEVEL",
64 "BABELTRACE_PLUGIN_TEXT_DMESG_SRC_LOG_LEVEL",
65 "BABELTRACE_PLUGIN_UTILS_MUXER_FLT_LOG_LEVEL",
66 "BABELTRACE_PLUGIN_UTILS_TRIMMER_FLT_LOG_LEVEL",
67 "BABELTRACE_PLUGIN_CTFCOPYTRACE_LIB_LOG_LEVEL",
68 "BABELTRACE_PLUGIN_CTF_FS_SINK_LOG_LEVEL",
69 "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
73 /* Application's processing graph (weak) */
74 static struct bt_graph
*the_graph
;
75 static struct bt_query_executor
*the_query_executor
;
76 static bool canceled
= false;
78 GPtrArray
*loaded_plugins
;
84 BOOL WINAPI
signal_handler(DWORD signal
) {
86 bt_graph_cancel(the_graph
);
95 void set_signal_handler(void)
97 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
98 BT_LOGE("Failed to set the ctrl+c handler.");
101 #else /* __MINGW32__ */
103 void signal_handler(int signum
)
105 if (signum
!= SIGINT
) {
110 bt_graph_cancel(the_graph
);
113 if (the_query_executor
) {
114 bt_query_executor_cancel(the_query_executor
);
121 void set_signal_handler(void)
123 struct sigaction new_action
, old_action
;
125 new_action
.sa_handler
= signal_handler
;
126 sigemptyset(&new_action
.sa_mask
);
127 new_action
.sa_flags
= 0;
128 sigaction(SIGINT
, NULL
, &old_action
);
130 if (old_action
.sa_handler
!= SIG_IGN
) {
131 sigaction(SIGINT
, &new_action
, NULL
);
134 #endif /* __MINGW32__ */
137 void init_static_data(void)
139 loaded_plugins
= g_ptr_array_new_with_free_func(bt_put
);
143 void fini_static_data(void)
145 g_ptr_array_free(loaded_plugins
, TRUE
);
149 int create_the_query_executor(void)
153 the_query_executor
= bt_query_executor_create();
154 if (!the_query_executor
) {
155 BT_LOGE_STR("Cannot create a query executor.");
163 void destroy_the_query_executor(void)
165 BT_PUT(the_query_executor
);
169 int query(struct bt_component_class
*comp_cls
, const char *obj
,
170 struct bt_value
*params
, struct bt_value
**user_result
,
171 const char **fail_reason
)
173 struct bt_value
*result
= NULL
;
174 enum bt_query_status status
;
175 *fail_reason
= "unknown error";
180 ret
= create_the_query_executor();
182 /* create_the_query_executor() logs errors */
187 BT_LOGI("Canceled by user before executing the query: "
188 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
189 "query-obj=\"%s\"", comp_cls
,
190 bt_component_class_get_name(comp_cls
), obj
);
191 *fail_reason
= "canceled by user";
196 status
= bt_query_executor_query(the_query_executor
, comp_cls
,
197 obj
, params
, &result
);
199 case BT_QUERY_STATUS_OK
:
201 case BT_QUERY_STATUS_AGAIN
:
203 const uint64_t sleep_time_us
= 100000;
205 /* Wait 100 ms and retry */
206 BT_LOGV("Got BT_QUERY_STATUS_AGAIN: sleeping: "
207 "time-us=%" PRIu64
, sleep_time_us
);
209 if (usleep(sleep_time_us
)) {
210 if (bt_query_executor_is_canceled(the_query_executor
)) {
211 BT_LOGI("Query was canceled by user: "
212 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
213 "query-obj=\"%s\"", comp_cls
,
214 bt_component_class_get_name(comp_cls
),
216 *fail_reason
= "canceled by user";
223 case BT_QUERY_STATUS_EXECUTOR_CANCELED
:
224 *fail_reason
= "canceled by user";
226 case BT_QUERY_STATUS_ERROR
:
227 case BT_QUERY_STATUS_INVALID
:
229 case BT_QUERY_STATUS_INVALID_OBJECT
:
230 *fail_reason
= "invalid or unknown query object";
232 case BT_QUERY_STATUS_INVALID_PARAMS
:
233 *fail_reason
= "invalid query parameters";
235 case BT_QUERY_STATUS_NOMEM
:
236 *fail_reason
= "not enough memory";
239 BT_LOGF("Unknown query status: status=%d", status
);
245 *user_result
= result
;
253 destroy_the_query_executor();
259 struct bt_plugin
*find_plugin(const char *name
)
262 struct bt_plugin
*plugin
= NULL
;
265 BT_LOGD("Finding plugin: name=\"%s\"", name
);
267 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
268 plugin
= g_ptr_array_index(loaded_plugins
, i
);
270 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
277 if (BT_LOG_ON_DEBUG
) {
279 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
281 BT_LOGD("Cannot find plugin.");
285 return bt_get(plugin
);
289 struct bt_component_class
*find_component_class(const char *plugin_name
,
290 const char *comp_class_name
,
291 enum bt_component_class_type comp_class_type
)
293 struct bt_component_class
*comp_class
= NULL
;
294 struct bt_plugin
*plugin
;
296 BT_LOGD("Finding component class: plugin-name=\"%s\", "
297 "comp-cls-name=\"%s\", comp-cls-type=%d",
298 plugin_name
, comp_class_name
, comp_class_type
);
300 plugin
= find_plugin(plugin_name
);
306 comp_class
= bt_plugin_get_component_class_by_name_and_type(plugin
,
307 comp_class_name
, comp_class_type
);
311 if (BT_LOG_ON_DEBUG
) {
313 BT_LOGD("Found component class: comp-cls-addr=%p",
316 BT_LOGD("Cannot find component class.");
324 void print_indent(FILE *fp
, size_t indent
)
328 for (i
= 0; i
< indent
; i
++) {
334 const char *component_type_str(enum bt_component_class_type type
)
337 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
339 case BT_COMPONENT_CLASS_TYPE_SINK
:
341 case BT_COMPONENT_CLASS_TYPE_FILTER
:
343 case BT_COMPONENT_CLASS_TYPE_UNKNOWN
:
350 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
351 const char *comp_cls_name
, enum bt_component_class_type type
)
353 GString
*shell_plugin_name
= NULL
;
354 GString
*shell_comp_cls_name
= NULL
;
356 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
357 if (!shell_plugin_name
) {
361 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
362 if (!shell_comp_cls_name
) {
366 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
367 bt_common_color_bold(),
368 bt_common_color_fg_cyan(),
369 component_type_str(type
),
370 bt_common_color_fg_default(),
371 bt_common_color_fg_blue(),
372 shell_plugin_name
->str
,
373 bt_common_color_fg_default(),
374 bt_common_color_fg_yellow(),
375 shell_comp_cls_name
->str
,
376 bt_common_color_reset());
379 if (shell_plugin_name
) {
380 g_string_free(shell_plugin_name
, TRUE
);
383 if (shell_comp_cls_name
) {
384 g_string_free(shell_comp_cls_name
, TRUE
);
389 void print_value(FILE *, struct bt_value
*, size_t);
392 void print_value_rec(FILE *, struct bt_value
*, size_t);
394 struct print_map_value_data
{
400 bt_bool
print_map_value(const char *key
, struct bt_value
*object
, void *data
)
402 struct print_map_value_data
*print_map_value_data
= data
;
404 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
405 fprintf(print_map_value_data
->fp
, "%s: ", key
);
407 if (bt_value_is_array(object
) &&
408 bt_value_array_is_empty(object
)) {
409 fprintf(print_map_value_data
->fp
, "[ ]\n");
413 if (bt_value_is_map(object
) &&
414 bt_value_map_is_empty(object
)) {
415 fprintf(print_map_value_data
->fp
, "{ }\n");
419 if (bt_value_is_array(object
) ||
420 bt_value_is_map(object
)) {
421 fprintf(print_map_value_data
->fp
, "\n");
424 print_value_rec(print_map_value_data
->fp
, object
,
425 print_map_value_data
->indent
+ 2);
430 void print_value_rec(FILE *fp
, struct bt_value
*value
, size_t indent
)
438 enum bt_value_status status
;
444 switch (bt_value_get_type(value
)) {
445 case BT_VALUE_TYPE_NULL
:
446 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
447 bt_common_color_reset());
449 case BT_VALUE_TYPE_BOOL
:
450 status
= bt_value_bool_get(value
, &bool_val
);
451 if (status
!= BT_VALUE_STATUS_OK
) {
454 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
455 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
456 bt_common_color_reset());
458 case BT_VALUE_TYPE_INTEGER
:
459 status
= bt_value_integer_get(value
, &int_val
);
460 if (status
!= BT_VALUE_STATUS_OK
) {
463 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
464 bt_common_color_fg_red(), int_val
,
465 bt_common_color_reset());
467 case BT_VALUE_TYPE_FLOAT
:
468 status
= bt_value_float_get(value
, &dbl_val
);
469 if (status
!= BT_VALUE_STATUS_OK
) {
472 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
473 bt_common_color_fg_red(), dbl_val
,
474 bt_common_color_reset());
476 case BT_VALUE_TYPE_STRING
:
477 status
= bt_value_string_get(value
, &str_val
);
478 if (status
!= BT_VALUE_STATUS_OK
) {
481 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
482 bt_common_color_fg_green(), str_val
,
483 bt_common_color_reset());
485 case BT_VALUE_TYPE_ARRAY
:
486 size
= bt_value_array_size(value
);
492 print_indent(fp
, indent
);
493 fprintf(fp
, "[ ]\n");
497 for (i
= 0; i
< size
; i
++) {
498 struct bt_value
*element
=
499 bt_value_array_get(value
, i
);
504 print_indent(fp
, indent
);
507 if (bt_value_is_array(element
) &&
508 bt_value_array_is_empty(element
)) {
509 fprintf(fp
, "[ ]\n");
513 if (bt_value_is_map(element
) &&
514 bt_value_map_is_empty(element
)) {
515 fprintf(fp
, "{ }\n");
519 if (bt_value_is_array(element
) ||
520 bt_value_is_map(element
)) {
524 print_value_rec(fp
, element
, indent
+ 2);
528 case BT_VALUE_TYPE_MAP
:
530 struct print_map_value_data data
= {
535 if (bt_value_map_is_empty(value
)) {
536 print_indent(fp
, indent
);
537 fprintf(fp
, "{ }\n");
541 bt_value_map_foreach(value
, print_map_value
, &data
);
550 BT_LOGE("Error printing value of type %s.",
551 bt_value_type_string(bt_value_get_type(value
)));
555 void print_value(FILE *fp
, struct bt_value
*value
, size_t indent
)
557 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
558 print_indent(fp
, indent
);
561 print_value_rec(fp
, value
, indent
);
565 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
567 fprintf(stderr
, " ");
568 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
569 bt_config_component
->comp_cls_name
->str
,
570 bt_config_component
->type
);
571 fprintf(stderr
, ":\n");
573 if (bt_config_component
->instance_name
->len
> 0) {
574 fprintf(stderr
, " Name: %s\n",
575 bt_config_component
->instance_name
->str
);
578 fprintf(stderr
, " Parameters:\n");
579 print_value(stderr
, bt_config_component
->params
, 8);
583 void print_bt_config_components(GPtrArray
*array
)
587 for (i
= 0; i
< array
->len
; i
++) {
588 struct bt_config_component
*cfg_component
=
589 bt_config_get_component(array
, i
);
590 print_bt_config_component(cfg_component
);
591 BT_PUT(cfg_component
);
596 void print_plugin_paths(struct bt_value
*plugin_paths
)
598 fprintf(stderr
, " Plugin paths:\n");
599 print_value(stderr
, plugin_paths
, 4);
603 void print_cfg_run(struct bt_config
*cfg
)
607 print_plugin_paths(cfg
->plugin_paths
);
608 fprintf(stderr
, " Source component instances:\n");
609 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
611 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
612 fprintf(stderr
, " Filter component instances:\n");
613 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
616 fprintf(stderr
, " Sink component instances:\n");
617 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
618 fprintf(stderr
, " Connections:\n");
620 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
621 struct bt_config_connection
*cfg_connection
=
622 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
625 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
626 cfg_connection
->upstream_comp_name
->str
,
627 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
628 cfg_connection
->upstream_port_glob
->str
,
629 cfg_connection
->downstream_comp_name
->str
,
630 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
631 cfg_connection
->downstream_port_glob
->str
);
636 void print_cfg_list_plugins(struct bt_config
*cfg
)
638 print_plugin_paths(cfg
->plugin_paths
);
642 void print_cfg_help(struct bt_config
*cfg
)
644 print_plugin_paths(cfg
->plugin_paths
);
648 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
650 print_plugin_paths(cfg
->plugin_paths
);
651 fprintf(stderr
, " Path: %s\n",
652 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
656 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
658 print_plugin_paths(cfg
->plugin_paths
);
659 fprintf(stderr
, " URL: %s\n",
660 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
664 void print_cfg_query(struct bt_config
*cfg
)
666 print_plugin_paths(cfg
->plugin_paths
);
667 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
668 fprintf(stderr
, " Component class:\n");
669 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
673 void print_cfg(struct bt_config
*cfg
)
675 if (!BT_LOG_ON_INFO
) {
679 BT_LOGI_STR("Configuration:");
680 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
681 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
683 switch (cfg
->command
) {
684 case BT_CONFIG_COMMAND_RUN
:
687 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
688 print_cfg_list_plugins(cfg
);
690 case BT_CONFIG_COMMAND_HELP
:
693 case BT_CONFIG_COMMAND_QUERY
:
694 print_cfg_query(cfg
);
696 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
697 print_cfg_print_ctf_metadata(cfg
);
699 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
700 print_cfg_print_lttng_live_sessions(cfg
);
708 void add_to_loaded_plugins(struct bt_plugin_set
*plugin_set
)
713 count
= bt_plugin_set_get_plugin_count(plugin_set
);
716 for (i
= 0; i
< count
; i
++) {
717 struct bt_plugin
*plugin
=
718 bt_plugin_set_get_plugin(plugin_set
, i
);
719 struct bt_plugin
*loaded_plugin
=
720 find_plugin(bt_plugin_get_name(plugin
));
725 BT_LOGI("Not using plugin: another one already exists with the same name: "
726 "plugin-name=\"%s\", plugin-path=\"%s\", "
727 "existing-plugin-path=\"%s\"",
728 bt_plugin_get_name(plugin
),
729 bt_plugin_get_path(plugin
),
730 bt_plugin_get_path(loaded_plugin
));
731 bt_put(loaded_plugin
);
733 /* Add to global array. */
734 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
735 bt_plugin_get_name(plugin
));
736 g_ptr_array_add(loaded_plugins
, bt_get(plugin
));
744 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
746 int nr_paths
, i
, ret
= 0;
748 nr_paths
= bt_value_array_size(plugin_paths
);
750 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
755 BT_LOGI("Loading dynamic plugins.");
757 for (i
= 0; i
< nr_paths
; i
++) {
758 struct bt_value
*plugin_path_value
= NULL
;
759 const char *plugin_path
;
760 struct bt_plugin_set
*plugin_set
;
761 enum bt_value_status status
;
763 plugin_path_value
= bt_value_array_get(plugin_paths
, i
);
764 status
= bt_value_string_get(plugin_path_value
, &plugin_path
);
765 if (status
!= BT_VALUE_STATUS_OK
) {
766 BT_LOGD_STR("Cannot get plugin path string.");
767 BT_PUT(plugin_path_value
);
772 * Skip this if the directory does not exist because
773 * bt_plugin_create_all_from_dir() expects an existing
776 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
777 BT_LOGV("Skipping nonexistent directory path: "
778 "path=\"%s\"", plugin_path
);
779 BT_PUT(plugin_path_value
);
783 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
785 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
787 BT_PUT(plugin_path_value
);
791 add_to_loaded_plugins(plugin_set
);
793 BT_PUT(plugin_path_value
);
800 int load_static_plugins(void)
803 struct bt_plugin_set
*plugin_set
;
805 BT_LOGI("Loading static plugins.");
806 plugin_set
= bt_plugin_create_all_from_static();
808 BT_LOGE("Unable to load static plugins.");
813 add_to_loaded_plugins(plugin_set
);
820 int load_all_plugins(struct bt_value
*plugin_paths
)
824 if (load_dynamic_plugins(plugin_paths
)) {
829 if (load_static_plugins()) {
834 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
841 void print_plugin_info(struct bt_plugin
*plugin
)
843 unsigned int major
, minor
, patch
;
845 enum bt_plugin_status version_status
;
846 const char *plugin_name
;
850 const char *plugin_description
;
852 plugin_name
= bt_plugin_get_name(plugin
);
853 path
= bt_plugin_get_path(plugin
);
854 author
= bt_plugin_get_author(plugin
);
855 license
= bt_plugin_get_license(plugin
);
856 plugin_description
= bt_plugin_get_description(plugin
);
857 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
859 printf("%s%s%s%s:\n", bt_common_color_bold(),
860 bt_common_color_fg_blue(), plugin_name
,
861 bt_common_color_reset());
863 printf(" %sPath%s: %s\n", bt_common_color_bold(),
864 bt_common_color_reset(), path
);
869 if (version_status
== BT_PLUGIN_STATUS_OK
) {
870 printf(" %sVersion%s: %u.%u.%u",
871 bt_common_color_bold(), bt_common_color_reset(),
872 major
, minor
, patch
);
881 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
882 bt_common_color_reset(),
883 plugin_description
? plugin_description
: "(None)");
884 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
885 bt_common_color_reset(), author
? author
: "(Unknown)");
886 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
887 bt_common_color_reset(),
888 license
? license
: "(Unknown)");
892 int cmd_query(struct bt_config
*cfg
)
895 struct bt_component_class
*comp_cls
= NULL
;
896 struct bt_value
*results
= NULL
;
897 const char *fail_reason
= NULL
;
899 comp_cls
= find_component_class(cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
900 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
901 cfg
->cmd_data
.query
.cfg_component
->type
);
903 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
904 "comp-cls-name=\"%s\", comp-cls-type=%d",
905 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
906 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
907 cfg
->cmd_data
.query
.cfg_component
->type
);
908 fprintf(stderr
, "%s%sCannot find component class %s",
909 bt_common_color_bold(),
910 bt_common_color_fg_red(),
911 bt_common_color_reset());
912 print_plugin_comp_cls_opt(stderr
,
913 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
914 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
915 cfg
->cmd_data
.query
.cfg_component
->type
);
916 fprintf(stderr
, "\n");
921 ret
= query(comp_cls
, cfg
->cmd_data
.query
.object
->str
,
922 cfg
->cmd_data
.query
.cfg_component
->params
, &results
,
928 print_value(stdout
, results
, 0);
932 BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", "
933 "comp-cls-name=\"%s\", comp-cls-type=%d "
934 "object=\"%s\"", fail_reason
,
935 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
936 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
937 cfg
->cmd_data
.query
.cfg_component
->type
,
938 cfg
->cmd_data
.query
.object
->str
);
939 fprintf(stderr
, "%s%sFailed to query info to %s",
940 bt_common_color_bold(),
941 bt_common_color_fg_red(),
942 bt_common_color_reset());
943 print_plugin_comp_cls_opt(stderr
,
944 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
945 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
946 cfg
->cmd_data
.query
.cfg_component
->type
);
947 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
948 bt_common_color_bold(),
949 bt_common_color_fg_red(),
950 cfg
->cmd_data
.query
.object
->str
,
952 bt_common_color_reset());
962 int cmd_help(struct bt_config
*cfg
)
965 struct bt_plugin
*plugin
= NULL
;
968 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
970 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
971 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
972 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
973 bt_common_color_bold(), bt_common_color_fg_red(),
974 bt_common_color_fg_blue(),
975 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
976 bt_common_color_reset());
981 print_plugin_info(plugin
);
982 printf(" %sComponent classes%s: %d\n",
983 bt_common_color_bold(),
984 bt_common_color_reset(),
985 (int) bt_plugin_get_component_class_count(plugin
));
988 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
989 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
990 struct bt_component_class
*needed_comp_cls
=
991 find_component_class(
992 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
993 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
994 cfg
->cmd_data
.help
.cfg_component
->type
);
996 if (!needed_comp_cls
) {
997 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
998 "comp-cls-name=\"%s\", comp-cls-type=%d",
999 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1000 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1001 cfg
->cmd_data
.help
.cfg_component
->type
);
1002 fprintf(stderr
, "\n%s%sCannot find component class %s",
1003 bt_common_color_bold(),
1004 bt_common_color_fg_red(),
1005 bt_common_color_reset());
1006 print_plugin_comp_cls_opt(stderr
,
1007 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1008 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1009 cfg
->cmd_data
.help
.cfg_component
->type
);
1010 fprintf(stderr
, "\n");
1015 bt_put(needed_comp_cls
);
1018 for (i
= 0; i
< bt_plugin_get_component_class_count(plugin
); i
++) {
1019 struct bt_component_class
*comp_cls
=
1020 bt_plugin_get_component_class_by_index(plugin
, i
);
1021 const char *comp_class_name
=
1022 bt_component_class_get_name(comp_cls
);
1023 const char *comp_class_description
=
1024 bt_component_class_get_description(comp_cls
);
1025 const char *comp_class_help
=
1026 bt_component_class_get_help(comp_cls
);
1027 enum bt_component_class_type type
=
1028 bt_component_class_get_type(comp_cls
);
1032 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
1033 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
1034 if (strcmp(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1035 comp_class_name
) != 0 &&
1037 cfg
->cmd_data
.help
.cfg_component
->type
) {
1044 print_plugin_comp_cls_opt(stdout
,
1045 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1049 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1050 bt_common_color_reset(),
1051 comp_class_description
? comp_class_description
: "(None)");
1053 if (comp_class_help
) {
1054 printf("\n%s\n", comp_class_help
);
1066 int cmd_list_plugins(struct bt_config
*cfg
)
1069 int plugins_count
, component_classes_count
= 0, i
;
1071 printf("From the following plugin paths:\n\n");
1072 print_value(stdout
, cfg
->plugin_paths
, 2);
1074 plugins_count
= loaded_plugins
->len
;
1075 if (plugins_count
== 0) {
1076 printf("No plugins found.\n");
1080 for (i
= 0; i
< plugins_count
; i
++) {
1081 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1083 component_classes_count
+= bt_plugin_get_component_class_count(plugin
);
1086 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1087 bt_common_color_bold(),
1088 component_classes_count
,
1089 bt_common_color_reset(),
1090 bt_common_color_bold(),
1092 bt_common_color_reset());
1094 for (i
= 0; i
< plugins_count
; i
++) {
1096 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1098 component_classes_count
=
1099 bt_plugin_get_component_class_count(plugin
);
1101 print_plugin_info(plugin
);
1103 if (component_classes_count
== 0) {
1104 printf(" %sComponent classes%s: (none)\n",
1105 bt_common_color_bold(),
1106 bt_common_color_reset());
1108 printf(" %sComponent classes%s:\n",
1109 bt_common_color_bold(),
1110 bt_common_color_reset());
1113 for (j
= 0; j
< component_classes_count
; j
++) {
1114 struct bt_component_class
*comp_class
=
1115 bt_plugin_get_component_class_by_index(
1117 const char *comp_class_name
=
1118 bt_component_class_get_name(comp_class
);
1119 const char *comp_class_description
=
1120 bt_component_class_get_description(comp_class
);
1121 enum bt_component_class_type type
=
1122 bt_component_class_get_type(comp_class
);
1125 print_plugin_comp_cls_opt(stdout
,
1126 bt_plugin_get_name(plugin
), comp_class_name
,
1129 if (comp_class_description
) {
1130 printf(": %s", comp_class_description
);
1143 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1146 struct bt_component_class
*comp_cls
= NULL
;
1147 struct bt_value
*results
= NULL
;
1148 struct bt_value
*params
= NULL
;
1149 struct bt_value
*map
= NULL
;
1150 struct bt_value
*v
= NULL
;
1151 static const char * const plugin_name
= "ctf";
1152 static const char * const comp_cls_name
= "lttng-live";
1153 static const enum bt_component_class_type comp_cls_type
=
1154 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1155 int64_t array_size
, i
;
1156 const char *fail_reason
= NULL
;
1158 assert(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1159 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1162 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1163 "comp-cls-name=\"%s\", comp-cls-type=%d",
1164 plugin_name
, comp_cls_name
,
1165 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1166 fprintf(stderr
, "%s%sCannot find component class %s",
1167 bt_common_color_bold(),
1168 bt_common_color_fg_red(),
1169 bt_common_color_reset());
1170 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1171 comp_cls_name
, comp_cls_type
);
1172 fprintf(stderr
, "\n");
1176 params
= bt_value_map_create();
1181 ret
= bt_value_map_insert_string(params
, "url",
1182 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1187 ret
= query(comp_cls
, "sessions", params
, &results
, &fail_reason
);
1192 if (!bt_value_is_array(results
)) {
1193 BT_LOGE_STR("Expecting an array for sessions query.");
1194 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1195 bt_common_color_bold(),
1196 bt_common_color_fg_red(),
1197 bt_common_color_reset());
1201 array_size
= bt_value_array_size(results
);
1202 for (i
= 0; i
< array_size
; i
++) {
1203 const char *url_text
;
1204 int64_t timer_us
, streams
, clients
;
1206 map
= bt_value_array_get(results
, i
);
1208 BT_LOGE_STR("Unexpected empty array entry.");
1211 if (!bt_value_is_map(map
)) {
1212 BT_LOGE_STR("Unexpected entry type.");
1216 v
= bt_value_map_get(map
, "url");
1218 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1221 ret
= bt_value_string_get(v
, &url_text
);
1223 printf("%s", url_text
);
1226 v
= bt_value_map_get(map
, "timer-us");
1228 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1231 ret
= bt_value_integer_get(v
, &timer_us
);
1233 printf(" (timer = %" PRIu64
", ", timer_us
);
1236 v
= bt_value_map_get(map
, "stream-count");
1238 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1241 ret
= bt_value_integer_get(v
, &streams
);
1243 printf("%" PRIu64
" stream(s), ", streams
);
1246 v
= bt_value_map_get(map
, "client-count");
1248 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1251 ret
= bt_value_integer_get(v
, &clients
);
1253 printf("%" PRIu64
" client(s) connected)\n", clients
);
1262 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1263 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1264 bt_common_color_bold(),
1265 bt_common_color_fg_red(),
1267 bt_common_color_reset());
1282 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1285 struct bt_component_class
*comp_cls
= NULL
;
1286 struct bt_value
*results
= NULL
;
1287 struct bt_value
*params
= NULL
;
1288 struct bt_value
*metadata_text_value
= NULL
;
1289 const char *metadata_text
= NULL
;
1290 static const char * const plugin_name
= "ctf";
1291 static const char * const comp_cls_name
= "fs";
1292 static const enum bt_component_class_type comp_cls_type
=
1293 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1294 const char *fail_reason
= NULL
;
1296 assert(cfg
->cmd_data
.print_ctf_metadata
.path
);
1297 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1300 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1301 "comp-cls-name=\"%s\", comp-cls-type=%d",
1302 plugin_name
, comp_cls_name
,
1303 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1304 fprintf(stderr
, "%s%sCannot find component class %s",
1305 bt_common_color_bold(),
1306 bt_common_color_fg_red(),
1307 bt_common_color_reset());
1308 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1309 comp_cls_name
, comp_cls_type
);
1310 fprintf(stderr
, "\n");
1315 params
= bt_value_map_create();
1321 ret
= bt_value_map_insert_string(params
, "path",
1322 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1328 ret
= query(comp_cls
, "metadata-info", params
, &results
, &fail_reason
);
1333 metadata_text_value
= bt_value_map_get(results
, "text");
1334 if (!metadata_text_value
) {
1335 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1340 ret
= bt_value_string_get(metadata_text_value
, &metadata_text
);
1342 printf("%s\n", metadata_text
);
1347 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1348 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1349 bt_common_color_bold(),
1350 bt_common_color_fg_red(),
1352 bt_common_color_reset());
1355 destroy_the_query_executor();
1358 bt_put(metadata_text_value
);
1364 char *instance_name
;
1368 struct trace_range
{
1369 uint64_t intersection_range_begin_ns
;
1370 uint64_t intersection_range_end_ns
;
1374 guint
port_id_hash(gconstpointer v
)
1376 const struct port_id
*id
= v
;
1378 assert(id
->instance_name
);
1379 assert(id
->port_name
);
1381 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1385 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1387 const struct port_id
*id1
= v1
;
1388 const struct port_id
*id2
= v2
;
1390 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1391 !strcmp(id1
->port_name
, id2
->port_name
);
1395 void port_id_destroy(gpointer data
)
1397 struct port_id
*id
= data
;
1399 free(id
->instance_name
);
1400 free(id
->port_name
);
1405 void trace_range_destroy(gpointer data
)
1410 struct cmd_run_ctx
{
1412 GHashTable
*components
;
1415 struct bt_graph
*graph
;
1418 struct bt_config
*cfg
;
1422 bool stream_intersection_mode
;
1425 * Association of struct port_id -> struct trace_range.
1427 GHashTable
*intersections
;
1430 /* Returns a timestamp of the form "(-)s.ns" */
1432 char *s_from_ns(int64_t ns
)
1437 int64_t ts_sec_abs
, ts_nsec_abs
;
1438 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1439 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1441 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1442 is_negative
= false;
1443 ts_sec_abs
= ts_sec
;
1444 ts_nsec_abs
= ts_nsec
;
1445 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1446 is_negative
= false;
1447 ts_sec_abs
= ts_sec
- 1;
1448 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1449 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1451 ts_sec_abs
= ts_sec
;
1452 ts_nsec_abs
= -ts_nsec
;
1453 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1455 ts_sec_abs
= -(ts_sec
+ 1);
1456 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1457 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1459 ts_sec_abs
= -ts_sec
;
1460 ts_nsec_abs
= ts_nsec
;
1461 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1463 ts_sec_abs
= -ts_sec
;
1464 ts_nsec_abs
= -ts_nsec
;
1467 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1468 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1476 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1477 struct cmd_run_ctx
*ctx
, struct bt_component
*upstream_comp
,
1478 struct bt_port
*upstream_port
,
1479 struct bt_config_connection
*cfg_conn
)
1482 GQuark downstreamp_comp_name_quark
;
1483 struct bt_component
*downstream_comp
;
1484 int64_t downstream_port_count
;
1486 int64_t (*port_count_fn
)(struct bt_component
*);
1487 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
1488 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1489 bool insert_trimmer
= false;
1490 struct bt_value
*trimmer_params
= NULL
;
1491 char *intersection_begin
= NULL
;
1492 char *intersection_end
= NULL
;
1493 struct bt_component
*trimmer
= NULL
;
1494 struct bt_component_class
*trimmer_class
= NULL
;
1495 struct bt_port
*trimmer_input
= NULL
;
1496 struct bt_port
*trimmer_output
= NULL
;
1498 if (ctx
->intersections
&&
1499 bt_component_get_class_type(upstream_comp
) ==
1500 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1501 struct trace_range
*range
;
1502 struct port_id port_id
= {
1503 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1504 .port_name
= (char *) bt_port_get_name(upstream_port
)
1507 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1511 range
= (struct trace_range
*) g_hash_table_lookup(
1512 ctx
->intersections
, &port_id
);
1514 enum bt_value_status status
;
1516 intersection_begin
= s_from_ns(
1517 range
->intersection_range_begin_ns
);
1518 intersection_end
= s_from_ns(
1519 range
->intersection_range_end_ns
);
1520 if (!intersection_begin
|| !intersection_end
) {
1521 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1525 insert_trimmer
= true;
1526 trimmer_params
= bt_value_map_create();
1527 if (!trimmer_params
) {
1531 status
= bt_value_map_insert_string(trimmer_params
,
1532 "begin", intersection_begin
);
1533 if (status
!= BT_VALUE_STATUS_OK
) {
1536 status
= bt_value_map_insert_string(trimmer_params
,
1537 "end", intersection_end
);
1538 if (status
!= BT_VALUE_STATUS_OK
) {
1543 trimmer_class
= find_component_class("utils", "trimmer",
1544 BT_COMPONENT_CLASS_TYPE_FILTER
);
1545 if (!trimmer_class
) {
1550 BT_LOGI("Connecting upstream port to the next available downstream port: "
1551 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1552 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1553 upstream_port
, bt_port_get_name(upstream_port
),
1554 cfg_conn
->downstream_comp_name
->str
,
1555 cfg_conn
->arg
->str
);
1556 downstreamp_comp_name_quark
= g_quark_from_string(
1557 cfg_conn
->downstream_comp_name
->str
);
1558 assert(downstreamp_comp_name_quark
> 0);
1559 downstream_comp
= g_hash_table_lookup(ctx
->components
,
1560 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1561 if (!downstream_comp
) {
1562 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1563 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1564 cfg_conn
->arg
->str
);
1565 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1566 cfg_conn
->arg
->str
);
1570 if (bt_component_is_filter(downstream_comp
)) {
1571 port_count_fn
= bt_component_filter_get_input_port_count
;
1572 port_by_index_fn
= bt_component_filter_get_input_port_by_index
;
1573 } else if (bt_component_is_sink(downstream_comp
)) {
1574 port_count_fn
= bt_component_sink_get_input_port_count
;
1575 port_by_index_fn
= bt_component_sink_get_input_port_by_index
;
1578 * Should never happen because the connections are
1579 * validated before we get here.
1581 BT_LOGF("Invalid connection: downstream component is a source: "
1582 "conn-arg=\"%s\"", cfg_conn
->arg
->str
);
1586 downstream_port_count
= port_count_fn(downstream_comp
);
1587 assert(downstream_port_count
>= 0);
1589 for (i
= 0; i
< downstream_port_count
; i
++) {
1590 struct bt_port
*downstream_port
=
1591 port_by_index_fn(downstream_comp
, i
);
1592 const char *upstream_port_name
;
1593 const char *downstream_port_name
;
1595 assert(downstream_port
);
1597 /* Skip port if it's already connected. */
1598 if (bt_port_is_connected(downstream_port
)) {
1599 bt_put(downstream_port
);
1600 BT_LOGD("Skipping downstream port: already connected: "
1601 "port-addr=%p, port-name=\"%s\"",
1603 bt_port_get_name(downstream_port
));
1607 downstream_port_name
= bt_port_get_name(downstream_port
);
1608 assert(downstream_port_name
);
1609 upstream_port_name
= bt_port_get_name(upstream_port
);
1610 assert(upstream_port_name
);
1612 if (!bt_common_star_glob_match(
1613 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1614 downstream_port_name
, SIZE_MAX
)) {
1615 bt_put(downstream_port
);
1619 if (insert_trimmer
) {
1621 * In order to insert the trimmer between the two
1622 * components that were being connected, we create
1623 * a connection configuration entry which describes
1624 * a connection from the trimmer's output to the
1625 * original input that was being connected.
1627 * Hence, the creation of the trimmer will cause the
1628 * graph "new port" listener to establish all downstream
1629 * connections as its output port is connected. We will
1630 * then establish the connection between the original
1631 * upstream source and the trimmer.
1633 char *trimmer_name
= NULL
;
1634 enum bt_graph_status graph_status
;
1636 ret
= asprintf(&trimmer_name
, "%s-%s",
1637 "stream-intersection-trimmer",
1638 upstream_port_name
);
1644 ctx
->connect_ports
= false;
1645 graph_status
= bt_graph_add_component(ctx
->graph
,
1646 trimmer_class
, trimmer_name
, trimmer_params
,
1649 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1655 bt_component_filter_get_input_port_by_index(
1657 if (!trimmer_input
) {
1661 bt_component_filter_get_output_port_by_index(
1663 if (!trimmer_output
) {
1668 * Replace the current downstream port by the trimmer's
1671 BT_MOVE(downstream_port
, trimmer_input
);
1672 downstream_port_name
= bt_port_get_name(
1674 if (!downstream_port_name
) {
1679 /* We have a winner! */
1680 status
= bt_graph_connect_ports(ctx
->graph
,
1681 upstream_port
, downstream_port
, NULL
);
1682 BT_PUT(downstream_port
);
1684 case BT_GRAPH_STATUS_OK
:
1686 case BT_GRAPH_STATUS_CANCELED
:
1687 BT_LOGI_STR("Graph was canceled by user.");
1688 status
= BT_GRAPH_STATUS_OK
;
1690 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1691 BT_LOGE("A component refused a connection to one of its ports: "
1692 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1693 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1694 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1695 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1697 upstream_comp
, bt_component_get_name(upstream_comp
),
1698 upstream_port
, bt_port_get_name(upstream_port
),
1699 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1700 downstream_port
, downstream_port_name
,
1701 cfg_conn
->arg
->str
);
1703 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1704 bt_port_get_name(upstream_port
),
1705 downstream_port_name
,
1706 cfg_conn
->arg
->str
);
1709 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1710 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1711 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1712 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1713 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1715 upstream_comp
, bt_component_get_name(upstream_comp
),
1716 upstream_port
, bt_port_get_name(upstream_port
),
1717 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1718 downstream_port
, downstream_port_name
,
1719 cfg_conn
->arg
->str
);
1721 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1722 bt_port_get_name(upstream_port
),
1723 downstream_port_name
,
1724 cfg_conn
->arg
->str
);
1728 BT_LOGI("Connected component ports: "
1729 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1730 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1731 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1732 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1734 upstream_comp
, bt_component_get_name(upstream_comp
),
1735 upstream_port
, bt_port_get_name(upstream_port
),
1736 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1737 downstream_port
, downstream_port_name
,
1738 cfg_conn
->arg
->str
);
1740 if (insert_trimmer
) {
1742 * The first connection, from the source to the trimmer,
1743 * has been done. We now connect the trimmer to the
1744 * original downstream port.
1746 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1747 ctx
, trimmer
, trimmer_output
, cfg_conn
);
1751 ctx
->connect_ports
= true;
1755 * We found a matching downstream port: the search is
1761 /* No downstream port found */
1762 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1763 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1764 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1765 upstream_port
, bt_port_get_name(upstream_port
),
1766 cfg_conn
->downstream_comp_name
->str
,
1767 cfg_conn
->arg
->str
);
1769 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1770 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1776 free(intersection_begin
);
1777 free(intersection_end
);
1778 BT_PUT(trimmer_params
);
1779 BT_PUT(trimmer_class
);
1781 BT_PUT(trimmer_input
);
1782 BT_PUT(trimmer_output
);
1787 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1788 struct bt_port
*upstream_port
)
1791 const char *upstream_port_name
;
1792 const char *upstream_comp_name
;
1793 struct bt_component
*upstream_comp
= NULL
;
1797 assert(upstream_port
);
1798 upstream_port_name
= bt_port_get_name(upstream_port
);
1799 assert(upstream_port_name
);
1800 upstream_comp
= bt_port_get_component(upstream_port
);
1801 if (!upstream_comp
) {
1802 BT_LOGW("Upstream port to connect is not part of a component: "
1803 "port-addr=%p, port-name=\"%s\"",
1804 upstream_port
, upstream_port_name
);
1809 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1810 assert(upstream_comp_name
);
1811 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1812 "port-addr=%p, port-name=\"%s\"",
1813 upstream_comp
, upstream_comp_name
,
1814 upstream_port
, upstream_port_name
);
1816 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1817 struct bt_config_connection
*cfg_conn
=
1819 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1821 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1822 upstream_comp_name
)) {
1826 if (!bt_common_star_glob_match(
1827 cfg_conn
->upstream_port_glob
->str
,
1828 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1832 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1833 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1835 BT_LOGE("Cannot connect upstream port: "
1836 "port-addr=%p, port-name=\"%s\"",
1838 upstream_port_name
);
1840 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1843 cfg_conn
->arg
->str
);
1849 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1850 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1851 upstream_port_name
);
1853 "Cannot create connection: upstream port `%s` does not match any connection\n",
1854 upstream_port_name
);
1860 bt_put(upstream_comp
);
1865 void graph_port_added_listener(struct bt_port
*port
, void *data
)
1867 struct bt_component
*comp
= NULL
;
1868 struct cmd_run_ctx
*ctx
= data
;
1870 comp
= bt_port_get_component(port
);
1871 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1872 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1873 comp
, comp
? bt_component_get_name(comp
) : "",
1874 port
, bt_port_get_name(port
));
1876 if (!ctx
->connect_ports
) {
1881 BT_LOGW_STR("Port has no component.");
1885 if (bt_port_is_connected(port
)) {
1886 BT_LOGW_STR("Port is already connected.");
1890 if (!bt_port_is_output(port
)) {
1891 BT_LOGI_STR("Skipping input port.");
1895 if (cmd_run_ctx_connect_upstream_port(ctx
, port
)) {
1896 BT_LOGF_STR("Cannot connect upstream port.");
1897 fprintf(stderr
, "Added port could not be connected: aborting\n");
1907 void graph_port_removed_listener(struct bt_component
*component
,
1908 struct bt_port
*port
, void *data
)
1910 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1911 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1912 component
, bt_component_get_name(component
),
1913 port
, bt_port_get_name(port
));
1917 void graph_ports_connected_listener(struct bt_port
*upstream_port
,
1918 struct bt_port
*downstream_port
, void *data
)
1920 struct bt_component
*upstream_comp
= bt_port_get_component(upstream_port
);
1921 struct bt_component
*downstream_comp
= bt_port_get_component(downstream_port
);
1923 assert(upstream_comp
);
1924 assert(downstream_comp
);
1925 BT_LOGI("Graph's component ports connected: "
1926 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1927 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1928 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1929 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1930 upstream_comp
, bt_component_get_name(upstream_comp
),
1931 upstream_port
, bt_port_get_name(upstream_port
),
1932 downstream_comp
, bt_component_get_name(downstream_comp
),
1933 downstream_port
, bt_port_get_name(downstream_port
));
1934 bt_put(upstream_comp
);
1935 bt_put(downstream_comp
);
1939 void graph_ports_disconnected_listener(
1940 struct bt_component
*upstream_component
,
1941 struct bt_component
*downstream_component
,
1942 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
1945 BT_LOGI("Graph's component ports disconnected: "
1946 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1947 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1948 upstream_port
, bt_port_get_name(upstream_port
),
1949 downstream_port
, bt_port_get_name(downstream_port
));
1953 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1959 if (ctx
->components
) {
1960 g_hash_table_destroy(ctx
->components
);
1961 ctx
->components
= NULL
;
1964 if (ctx
->intersections
) {
1965 g_hash_table_destroy(ctx
->intersections
);
1966 ctx
->intersections
= NULL
;
1975 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1980 ctx
->connect_ports
= false;
1981 ctx
->components
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
1983 if (!ctx
->components
) {
1987 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1988 ctx
->stream_intersection_mode
= true;
1989 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1990 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1991 if (!ctx
->intersections
) {
1996 ctx
->graph
= bt_graph_create();
2001 the_graph
= ctx
->graph
;
2002 ret
= bt_graph_add_port_added_listener(ctx
->graph
,
2003 graph_port_added_listener
, NULL
, ctx
);
2005 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2009 ret
= bt_graph_add_port_removed_listener(ctx
->graph
,
2010 graph_port_removed_listener
, NULL
, ctx
);
2012 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
2016 ret
= bt_graph_add_ports_connected_listener(ctx
->graph
,
2017 graph_ports_connected_listener
, NULL
, ctx
);
2019 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
2023 ret
= bt_graph_add_ports_disconnected_listener(ctx
->graph
,
2024 graph_ports_disconnected_listener
, NULL
, ctx
);
2026 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
2033 cmd_run_ctx_destroy(ctx
);
2041 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2042 struct bt_config_component
*cfg_comp
,
2043 struct bt_component_class
*comp_cls
)
2047 int64_t trace_count
;
2048 enum bt_value_status value_status
;
2049 const char *path
= NULL
;
2050 struct bt_value
*component_path_value
= NULL
;
2051 struct bt_value
*query_params
= NULL
;
2052 struct bt_value
*query_result
= NULL
;
2053 struct bt_value
*trace_info
= NULL
;
2054 struct bt_value
*intersection_range
= NULL
;
2055 struct bt_value
*intersection_begin
= NULL
;
2056 struct bt_value
*intersection_end
= NULL
;
2057 struct bt_value
*stream_path_value
= NULL
;
2058 struct bt_value
*stream_paths
= NULL
;
2059 struct bt_value
*stream_infos
= NULL
;
2060 struct bt_value
*stream_info
= NULL
;
2061 struct port_id
*port_id
= NULL
;
2062 struct trace_range
*trace_range
= NULL
;
2063 const char *fail_reason
= NULL
;
2065 component_path_value
= bt_value_map_get(cfg_comp
->params
, "path");
2066 if (!bt_value_is_string(component_path_value
)) {
2067 BT_LOGD("Cannot get path parameter: component-name=%s",
2068 cfg_comp
->instance_name
->str
);
2073 value_status
= bt_value_string_get(component_path_value
, &path
);
2074 if (value_status
!= BT_VALUE_STATUS_OK
) {
2075 BT_LOGD("Cannot get path string value: component-name=%s",
2076 cfg_comp
->instance_name
->str
);
2081 query_params
= bt_value_map_create();
2082 if (!query_params
) {
2083 BT_LOGE_STR("Cannot create query parameters.");
2088 value_status
= bt_value_map_insert(query_params
, "path", component_path_value
);
2089 if (value_status
!= BT_VALUE_STATUS_OK
) {
2090 BT_LOGE_STR("Cannot insert path parameter in query paramater map.");
2095 ret
= query(comp_cls
, "trace-info", query_params
, &query_result
,
2098 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2099 "comp-class-name=\"%s\"", fail_reason
,
2100 bt_component_class_get_name(comp_cls
));
2105 if (!bt_value_is_array(query_result
)) {
2106 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2107 "component-class-name=%s",
2108 bt_component_class_get_name(comp_cls
));
2113 trace_count
= bt_value_array_size(query_result
);
2114 if (trace_count
< 0) {
2119 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2121 uint64_t stream_idx
;
2122 int64_t stream_count
;
2124 trace_info
= bt_value_array_get(query_result
, trace_idx
);
2125 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2127 BT_LOGD_STR("Cannot retrieve trace from query result.");
2131 intersection_range
= bt_value_map_get(trace_info
,
2132 "intersection-range-ns");
2133 if (!intersection_range
) {
2135 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2139 intersection_begin
= bt_value_map_get(intersection_range
,
2141 if (!intersection_begin
) {
2143 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2147 intersection_end
= bt_value_map_get(intersection_range
,
2149 if (!intersection_end
) {
2151 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2155 value_status
= bt_value_integer_get(intersection_begin
, &begin
);
2156 if (value_status
!= BT_VALUE_STATUS_OK
) {
2158 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'begin\' field from query result.");
2162 value_status
= bt_value_integer_get(intersection_end
, &end
);
2163 if (value_status
!= BT_VALUE_STATUS_OK
) {
2165 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'end\' field from query result.");
2169 if (begin
< 0 || end
< 0 || end
< begin
) {
2170 BT_LOGW("Invalid trace stream intersection values: "
2171 "intersection-range-ns:begin=%" PRId64
2172 ", intersection-range-ns:end=%" PRId64
,
2178 stream_infos
= bt_value_map_get(trace_info
, "streams");
2179 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2181 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2185 stream_count
= bt_value_array_size(stream_infos
);
2186 if (stream_count
< 0) {
2194 * The first path of a stream's "paths" is currently used to
2195 * associate streams/ports to a given trace intersection.
2197 * This is a fragile hack as it relies on the port names
2198 * being set to the various streams path.
2200 * A stream name should be introduced as part of the trace-info
2203 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2204 const char *stream_path
;
2206 port_id
= g_new0(struct port_id
, 1);
2209 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2212 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2213 if (!port_id
->instance_name
) {
2215 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2219 trace_range
= g_new0(struct trace_range
, 1);
2222 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2225 trace_range
->intersection_range_begin_ns
= begin
;
2226 trace_range
->intersection_range_end_ns
= end
;
2228 stream_info
= bt_value_array_get(stream_infos
,
2230 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2232 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2236 stream_paths
= bt_value_map_get(stream_info
, "paths");
2237 if (!stream_paths
|| !bt_value_is_array(stream_paths
)) {
2239 BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
2243 stream_path_value
= bt_value_array_get(stream_paths
, 0);
2244 if (!stream_path_value
||
2245 !bt_value_is_string(stream_path_value
)) {
2247 BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
2251 value_status
= bt_value_string_get(stream_path_value
,
2253 if (value_status
!= BT_VALUE_STATUS_OK
) {
2258 port_id
->port_name
= strdup(stream_path
);
2259 if (!port_id
->port_name
) {
2261 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2265 BT_LOGD("Inserting stream intersection ");
2267 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2271 BT_PUT(stream_info
);
2272 BT_PUT(stream_paths
);
2273 BT_PUT(stream_path_value
);
2277 BT_PUT(stream_paths
);
2278 BT_PUT(stream_path_value
);
2279 BT_PUT(intersection_range
);
2280 BT_PUT(intersection_begin
);
2281 BT_PUT(intersection_end
);
2282 BT_PUT(stream_paths
);
2283 BT_PUT(stream_path_value
);
2289 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2290 bt_common_color_bold(),
2291 bt_common_color_fg_yellow(),
2292 path
? path
: "(unknown)",
2293 bt_common_color_reset());
2295 bt_put(component_path_value
);
2296 bt_put(query_params
);
2297 bt_put(query_result
);
2299 bt_put(intersection_range
);
2300 bt_put(intersection_begin
);
2301 bt_put(intersection_end
);
2302 bt_put(stream_infos
);
2303 bt_put(stream_info
);
2304 bt_put(stream_paths
);
2305 bt_put(stream_path_value
);
2307 g_free(trace_range
);
2312 int cmd_run_ctx_create_components_from_config_components(
2313 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2316 struct bt_component_class
*comp_cls
= NULL
;
2317 struct bt_component
*comp
= NULL
;
2320 for (i
= 0; i
< cfg_components
->len
; i
++) {
2321 struct bt_config_component
*cfg_comp
=
2322 g_ptr_array_index(cfg_components
, i
);
2325 comp_cls
= find_component_class(cfg_comp
->plugin_name
->str
,
2326 cfg_comp
->comp_cls_name
->str
, cfg_comp
->type
);
2328 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2329 "comp-cls-name=\"%s\", comp-cls-type=%d",
2330 cfg_comp
->plugin_name
->str
,
2331 cfg_comp
->comp_cls_name
->str
,
2333 fprintf(stderr
, "%s%sCannot find component class %s",
2334 bt_common_color_bold(),
2335 bt_common_color_fg_red(),
2336 bt_common_color_reset());
2337 print_plugin_comp_cls_opt(stderr
,
2338 cfg_comp
->plugin_name
->str
,
2339 cfg_comp
->comp_cls_name
->str
,
2341 fprintf(stderr
, "\n");
2345 ret
= bt_graph_add_component(ctx
->graph
, comp_cls
,
2346 cfg_comp
->instance_name
->str
, cfg_comp
->params
, &comp
);
2348 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2349 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2351 cfg_comp
->plugin_name
->str
,
2352 cfg_comp
->comp_cls_name
->str
,
2353 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2354 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2355 bt_common_color_bold(),
2356 bt_common_color_fg_red(),
2357 cfg_comp
->instance_name
->str
,
2358 bt_common_color_reset());
2362 if (ctx
->stream_intersection_mode
&&
2363 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2364 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2370 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2371 comp
, cfg_comp
->instance_name
->str
);
2372 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2374 g_hash_table_insert(ctx
->components
,
2375 GUINT_TO_POINTER(quark
), comp
);
2392 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2397 * Make sure that, during this phase, our graph's "port added"
2398 * listener does not connect ports while we are creating the
2399 * components because we have a special, initial phase for
2402 ctx
->connect_ports
= false;
2404 ret
= cmd_run_ctx_create_components_from_config_components(
2405 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2411 ret
= cmd_run_ctx_create_components_from_config_components(
2412 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2418 ret
= cmd_run_ctx_create_components_from_config_components(
2419 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2430 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2431 struct bt_component
*comp
,
2432 int64_t (*port_count_fn
)(struct bt_component
*),
2433 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t))
2439 count
= port_count_fn(comp
);
2442 for (i
= 0; i
< count
; i
++) {
2443 struct bt_port
*upstream_port
= port_by_index_fn(comp
, i
);
2445 assert(upstream_port
);
2446 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2447 bt_put(upstream_port
);
2458 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2461 GHashTableIter iter
;
2462 gpointer g_name_quark
, g_comp
;
2464 ctx
->connect_ports
= true;
2465 g_hash_table_iter_init(&iter
, ctx
->components
);
2467 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2468 int64_t (*port_count_fn
)(struct bt_component
*);
2469 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
2471 if (bt_component_is_source(g_comp
)) {
2473 bt_component_source_get_output_port_count
;
2475 bt_component_source_get_output_port_by_index
;
2476 } else if (bt_component_is_filter(g_comp
)) {
2478 bt_component_filter_get_output_port_count
;
2480 bt_component_filter_get_output_port_by_index
;
2485 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
2486 g_comp
, port_count_fn
, port_by_index_fn
);
2497 const char *bt_graph_status_str(enum bt_graph_status status
)
2500 case BT_GRAPH_STATUS_CANCELED
:
2501 return "BT_GRAPH_STATUS_CANCELED";
2502 case BT_GRAPH_STATUS_AGAIN
:
2503 return "BT_GRAPH_STATUS_AGAIN";
2504 case BT_GRAPH_STATUS_END
:
2505 return "BT_GRAPH_STATUS_END";
2506 case BT_GRAPH_STATUS_OK
:
2507 return "BT_GRAPH_STATUS_OK";
2508 case BT_GRAPH_STATUS_INVALID
:
2509 return "BT_GRAPH_STATUS_INVALID";
2510 case BT_GRAPH_STATUS_NO_SINK
:
2511 return "BT_GRAPH_STATUS_NO_SINK";
2512 case BT_GRAPH_STATUS_ERROR
:
2513 return "BT_GRAPH_STATUS_ERROR";
2520 int cmd_run(struct bt_config
*cfg
)
2523 struct cmd_run_ctx ctx
= { 0 };
2525 /* Initialize the command's context and the graph object */
2526 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2527 BT_LOGE_STR("Cannot initialize the command's context.");
2528 fprintf(stderr
, "Cannot initialize the command's context\n");
2533 BT_LOGI_STR("Canceled by user before creating components.");
2537 BT_LOGI_STR("Creating components.");
2539 /* Create the requested component instances */
2540 if (cmd_run_ctx_create_components(&ctx
)) {
2541 BT_LOGE_STR("Cannot create components.");
2542 fprintf(stderr
, "Cannot create components\n");
2547 BT_LOGI_STR("Canceled by user before connecting components.");
2551 BT_LOGI_STR("Connecting components.");
2553 /* Connect the initially visible component ports */
2554 if (cmd_run_ctx_connect_ports(&ctx
)) {
2555 BT_LOGE_STR("Cannot connect initial component ports.");
2556 fprintf(stderr
, "Cannot connect initial component ports\n");
2561 BT_LOGI_STR("Canceled by user before running the graph.");
2565 BT_LOGI_STR("Running the graph.");
2569 enum bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2572 * Reset console in case something messed with console
2573 * codes during the graph's execution.
2575 printf("%s", bt_common_color_reset());
2577 fprintf(stderr
, "%s", bt_common_color_reset());
2578 BT_LOGV("bt_graph_run() returned: status=%s",
2579 bt_graph_status_str(graph_status
));
2581 switch (graph_status
) {
2582 case BT_GRAPH_STATUS_OK
:
2584 case BT_GRAPH_STATUS_CANCELED
:
2585 BT_LOGI_STR("Graph was canceled by user.");
2587 case BT_GRAPH_STATUS_AGAIN
:
2588 if (bt_graph_is_canceled(ctx
.graph
)) {
2589 BT_LOGI_STR("Graph was canceled by user.");
2593 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2594 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2596 cfg
->cmd_data
.run
.retry_duration_us
);
2598 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2599 if (bt_graph_is_canceled(ctx
.graph
)) {
2600 BT_LOGI_STR("Graph was canceled by user.");
2606 case BT_COMPONENT_STATUS_END
:
2609 BT_LOGE_STR("Graph failed to complete successfully");
2610 fprintf(stderr
, "Graph failed to complete successfully\n");
2623 cmd_run_ctx_destroy(&ctx
);
2628 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2630 const char *env_clash
;
2632 if (!cfg
->command_name
) {
2636 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2637 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2641 if (g_file_test(cfg
->command_name
,
2642 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2643 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2645 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2647 fprintf(stderr
, "\n");
2648 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
2654 void init_log_level(void)
2656 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2660 void set_auto_log_levels(struct bt_config
*cfg
)
2662 const char **env_var_name
;
2665 * Override the configuration's default log level if
2666 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2667 * are found for backward compatibility with legacy Babetrace 1.
2669 if (getenv("BABELTRACE_DEBUG") &&
2670 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2671 cfg
->log_level
= 'V';
2672 } else if (getenv("BABELTRACE_VERBOSE") &&
2673 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2674 cfg
->log_level
= 'I';
2678 * Set log levels according to --debug or --verbose. For
2679 * backward compatibility, --debug is more verbose than
2682 * --verbose: INFO log level
2683 * --debug: VERBOSE log level (includes DEBUG, which is
2684 * is less verbose than VERBOSE in the internal
2685 * logging framework)
2687 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2689 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2690 } else if (cfg
->debug
) {
2691 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2694 * Set library's default log level if not
2695 * explicitly specified.
2697 switch (cfg
->log_level
) {
2699 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE
);
2702 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2705 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
2708 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2711 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN
);
2714 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR
);
2717 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL
);
2725 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2727 bt_cli_log_level
= BT_LOG_INFO
;
2728 } else if (cfg
->debug
) {
2729 bt_cli_log_level
= BT_LOG_VERBOSE
;
2732 * Set CLI's default log level if not explicitly
2735 switch (cfg
->log_level
) {
2737 bt_cli_log_level
= BT_LOG_NONE
;
2740 bt_cli_log_level
= BT_LOG_VERBOSE
;
2743 bt_cli_log_level
= BT_LOG_DEBUG
;
2746 bt_cli_log_level
= BT_LOG_INFO
;
2749 bt_cli_log_level
= BT_LOG_WARN
;
2752 bt_cli_log_level
= BT_LOG_ERROR
;
2755 bt_cli_log_level
= BT_LOG_FATAL
;
2763 env_var_name
= log_level_env_var_names
;
2765 while (*env_var_name
) {
2766 if (!getenv(*env_var_name
)) {
2768 g_setenv(*env_var_name
, "I", 1);
2769 } else if (cfg
->debug
) {
2770 g_setenv(*env_var_name
, "V", 1);
2772 char val
[2] = { 0 };
2775 * Set module's default log level if not
2776 * explicitly specified.
2778 val
[0] = cfg
->log_level
;
2779 g_setenv(*env_var_name
, val
, 1);
2787 int main(int argc
, const char **argv
)
2791 struct bt_config
*cfg
;
2794 set_signal_handler();
2796 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2799 /* Quit without errors; typically usage/version */
2801 BT_LOGI_STR("Quitting without errors.");
2806 BT_LOGE("Command-line error: retcode=%d", retcode
);
2811 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2812 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2817 set_auto_log_levels(cfg
);
2820 if (cfg
->command_needs_plugins
) {
2821 ret
= load_all_plugins(cfg
->plugin_paths
);
2823 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2829 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2830 cfg
->command
, cfg
->command_name
);
2832 switch (cfg
->command
) {
2833 case BT_CONFIG_COMMAND_RUN
:
2836 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2837 ret
= cmd_list_plugins(cfg
);
2839 case BT_CONFIG_COMMAND_HELP
:
2840 ret
= cmd_help(cfg
);
2842 case BT_CONFIG_COMMAND_QUERY
:
2843 ret
= cmd_query(cfg
);
2845 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2846 ret
= cmd_print_ctf_metadata(cfg
);
2848 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2849 ret
= cmd_print_lttng_live_sessions(cfg
);
2852 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2856 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2857 cfg
->command
, cfg
->command_name
, ret
);
2858 warn_command_name_and_directory_clash(cfg
);
2859 retcode
= ret
? 1 : 0;