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";
181 BT_ASSERT(fail_reason
);
182 BT_ASSERT(user_result
);
183 ret
= create_the_query_executor();
185 /* create_the_query_executor() logs errors */
190 BT_LOGI("Canceled by user before executing the query: "
191 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
192 "query-obj=\"%s\"", comp_cls
,
193 bt_component_class_get_name(comp_cls
), obj
);
194 *fail_reason
= "canceled by user";
199 status
= bt_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
);
411 if (bt_value_is_array(object
) &&
412 bt_value_array_is_empty(object
)) {
413 fprintf(print_map_value_data
->fp
, "[ ]\n");
417 if (bt_value_is_map(object
) &&
418 bt_value_map_is_empty(object
)) {
419 fprintf(print_map_value_data
->fp
, "{ }\n");
423 if (bt_value_is_array(object
) ||
424 bt_value_is_map(object
)) {
425 fprintf(print_map_value_data
->fp
, "\n");
428 print_value_rec(print_map_value_data
->fp
, object
,
429 print_map_value_data
->indent
+ 2);
434 void print_value_rec(FILE *fp
, struct bt_value
*value
, size_t indent
)
442 enum bt_value_status status
;
448 switch (bt_value_get_type(value
)) {
449 case BT_VALUE_TYPE_NULL
:
450 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
451 bt_common_color_reset());
453 case BT_VALUE_TYPE_BOOL
:
454 status
= bt_value_bool_get(value
, &bool_val
);
455 if (status
!= BT_VALUE_STATUS_OK
) {
458 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
459 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
460 bt_common_color_reset());
462 case BT_VALUE_TYPE_INTEGER
:
463 status
= bt_value_integer_get(value
, &int_val
);
464 if (status
!= BT_VALUE_STATUS_OK
) {
467 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
468 bt_common_color_fg_red(), int_val
,
469 bt_common_color_reset());
471 case BT_VALUE_TYPE_FLOAT
:
472 status
= bt_value_float_get(value
, &dbl_val
);
473 if (status
!= BT_VALUE_STATUS_OK
) {
476 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
477 bt_common_color_fg_red(), dbl_val
,
478 bt_common_color_reset());
480 case BT_VALUE_TYPE_STRING
:
481 status
= bt_value_string_get(value
, &str_val
);
482 if (status
!= BT_VALUE_STATUS_OK
) {
485 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
486 bt_common_color_fg_green(), str_val
,
487 bt_common_color_reset());
489 case BT_VALUE_TYPE_ARRAY
:
490 size
= bt_value_array_size(value
);
496 print_indent(fp
, indent
);
497 fprintf(fp
, "[ ]\n");
501 for (i
= 0; i
< size
; i
++) {
502 struct bt_value
*element
=
503 bt_value_array_get(value
, i
);
508 print_indent(fp
, indent
);
511 if (bt_value_is_array(element
) &&
512 bt_value_array_is_empty(element
)) {
513 fprintf(fp
, "[ ]\n");
517 if (bt_value_is_map(element
) &&
518 bt_value_map_is_empty(element
)) {
519 fprintf(fp
, "{ }\n");
523 if (bt_value_is_array(element
) ||
524 bt_value_is_map(element
)) {
528 print_value_rec(fp
, element
, indent
+ 2);
532 case BT_VALUE_TYPE_MAP
:
534 struct print_map_value_data data
= {
539 if (bt_value_map_is_empty(value
)) {
540 print_indent(fp
, indent
);
541 fprintf(fp
, "{ }\n");
545 bt_value_map_foreach(value
, print_map_value
, &data
);
554 BT_LOGE("Error printing value of type %s.",
555 bt_value_type_string(bt_value_get_type(value
)));
559 void print_value(FILE *fp
, struct bt_value
*value
, size_t indent
)
561 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
562 print_indent(fp
, indent
);
565 print_value_rec(fp
, value
, indent
);
569 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
571 fprintf(stderr
, " ");
572 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
573 bt_config_component
->comp_cls_name
->str
,
574 bt_config_component
->type
);
575 fprintf(stderr
, ":\n");
577 if (bt_config_component
->instance_name
->len
> 0) {
578 fprintf(stderr
, " Name: %s\n",
579 bt_config_component
->instance_name
->str
);
582 fprintf(stderr
, " Parameters:\n");
583 print_value(stderr
, bt_config_component
->params
, 8);
587 void print_bt_config_components(GPtrArray
*array
)
591 for (i
= 0; i
< array
->len
; i
++) {
592 struct bt_config_component
*cfg_component
=
593 bt_config_get_component(array
, i
);
594 print_bt_config_component(cfg_component
);
595 BT_PUT(cfg_component
);
600 void print_plugin_paths(struct bt_value
*plugin_paths
)
602 fprintf(stderr
, " Plugin paths:\n");
603 print_value(stderr
, plugin_paths
, 4);
607 void print_cfg_run(struct bt_config
*cfg
)
611 print_plugin_paths(cfg
->plugin_paths
);
612 fprintf(stderr
, " Source component instances:\n");
613 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
615 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
616 fprintf(stderr
, " Filter component instances:\n");
617 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
620 fprintf(stderr
, " Sink component instances:\n");
621 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
622 fprintf(stderr
, " Connections:\n");
624 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
625 struct bt_config_connection
*cfg_connection
=
626 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
629 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
630 cfg_connection
->upstream_comp_name
->str
,
631 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
632 cfg_connection
->upstream_port_glob
->str
,
633 cfg_connection
->downstream_comp_name
->str
,
634 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
635 cfg_connection
->downstream_port_glob
->str
);
640 void print_cfg_list_plugins(struct bt_config
*cfg
)
642 print_plugin_paths(cfg
->plugin_paths
);
646 void print_cfg_help(struct bt_config
*cfg
)
648 print_plugin_paths(cfg
->plugin_paths
);
652 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
654 print_plugin_paths(cfg
->plugin_paths
);
655 fprintf(stderr
, " Path: %s\n",
656 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
660 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
662 print_plugin_paths(cfg
->plugin_paths
);
663 fprintf(stderr
, " URL: %s\n",
664 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
668 void print_cfg_query(struct bt_config
*cfg
)
670 print_plugin_paths(cfg
->plugin_paths
);
671 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
672 fprintf(stderr
, " Component class:\n");
673 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
677 void print_cfg(struct bt_config
*cfg
)
679 if (!BT_LOG_ON_INFO
) {
683 BT_LOGI_STR("Configuration:");
684 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
685 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
687 switch (cfg
->command
) {
688 case BT_CONFIG_COMMAND_RUN
:
691 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
692 print_cfg_list_plugins(cfg
);
694 case BT_CONFIG_COMMAND_HELP
:
697 case BT_CONFIG_COMMAND_QUERY
:
698 print_cfg_query(cfg
);
700 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
701 print_cfg_print_ctf_metadata(cfg
);
703 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
704 print_cfg_print_lttng_live_sessions(cfg
);
712 void add_to_loaded_plugins(struct bt_plugin_set
*plugin_set
)
717 count
= bt_plugin_set_get_plugin_count(plugin_set
);
718 BT_ASSERT(count
>= 0);
720 for (i
= 0; i
< count
; i
++) {
721 struct bt_plugin
*plugin
=
722 bt_plugin_set_get_plugin(plugin_set
, i
);
723 struct bt_plugin
*loaded_plugin
=
724 find_plugin(bt_plugin_get_name(plugin
));
729 BT_LOGI("Not using plugin: another one already exists with the same name: "
730 "plugin-name=\"%s\", plugin-path=\"%s\", "
731 "existing-plugin-path=\"%s\"",
732 bt_plugin_get_name(plugin
),
733 bt_plugin_get_path(plugin
),
734 bt_plugin_get_path(loaded_plugin
));
735 bt_put(loaded_plugin
);
737 /* Add to global array. */
738 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
739 bt_plugin_get_name(plugin
));
740 g_ptr_array_add(loaded_plugins
, bt_get(plugin
));
748 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
750 int nr_paths
, i
, ret
= 0;
752 nr_paths
= bt_value_array_size(plugin_paths
);
754 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
759 BT_LOGI("Loading dynamic plugins.");
761 for (i
= 0; i
< nr_paths
; i
++) {
762 struct bt_value
*plugin_path_value
= NULL
;
763 const char *plugin_path
;
764 struct bt_plugin_set
*plugin_set
;
765 enum bt_value_status status
;
767 plugin_path_value
= bt_value_array_get(plugin_paths
, i
);
768 status
= bt_value_string_get(plugin_path_value
, &plugin_path
);
769 if (status
!= BT_VALUE_STATUS_OK
) {
770 BT_LOGD_STR("Cannot get plugin path string.");
771 BT_PUT(plugin_path_value
);
776 * Skip this if the directory does not exist because
777 * bt_plugin_create_all_from_dir() expects an existing
780 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
781 BT_LOGV("Skipping nonexistent directory path: "
782 "path=\"%s\"", plugin_path
);
783 BT_PUT(plugin_path_value
);
787 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
789 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
791 BT_PUT(plugin_path_value
);
795 add_to_loaded_plugins(plugin_set
);
797 BT_PUT(plugin_path_value
);
804 int load_static_plugins(void)
807 struct bt_plugin_set
*plugin_set
;
809 BT_LOGI("Loading static plugins.");
810 plugin_set
= bt_plugin_create_all_from_static();
812 BT_LOGE("Unable to load static plugins.");
817 add_to_loaded_plugins(plugin_set
);
824 int load_all_plugins(struct bt_value
*plugin_paths
)
828 if (load_dynamic_plugins(plugin_paths
)) {
833 if (load_static_plugins()) {
838 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
845 void print_plugin_info(struct bt_plugin
*plugin
)
847 unsigned int major
, minor
, patch
;
849 enum bt_plugin_status version_status
;
850 const char *plugin_name
;
854 const char *plugin_description
;
856 plugin_name
= bt_plugin_get_name(plugin
);
857 path
= bt_plugin_get_path(plugin
);
858 author
= bt_plugin_get_author(plugin
);
859 license
= bt_plugin_get_license(plugin
);
860 plugin_description
= bt_plugin_get_description(plugin
);
861 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
863 printf("%s%s%s%s:\n", bt_common_color_bold(),
864 bt_common_color_fg_blue(), plugin_name
,
865 bt_common_color_reset());
867 printf(" %sPath%s: %s\n", bt_common_color_bold(),
868 bt_common_color_reset(), path
);
873 if (version_status
== BT_PLUGIN_STATUS_OK
) {
874 printf(" %sVersion%s: %u.%u.%u",
875 bt_common_color_bold(), bt_common_color_reset(),
876 major
, minor
, patch
);
885 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
886 bt_common_color_reset(),
887 plugin_description
? plugin_description
: "(None)");
888 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
889 bt_common_color_reset(), author
? author
: "(Unknown)");
890 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
891 bt_common_color_reset(),
892 license
? license
: "(Unknown)");
896 int cmd_query(struct bt_config
*cfg
)
899 struct bt_component_class
*comp_cls
= NULL
;
900 struct bt_value
*results
= NULL
;
901 const char *fail_reason
= NULL
;
903 comp_cls
= find_component_class(cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
904 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
905 cfg
->cmd_data
.query
.cfg_component
->type
);
907 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
908 "comp-cls-name=\"%s\", comp-cls-type=%d",
909 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
910 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
911 cfg
->cmd_data
.query
.cfg_component
->type
);
912 fprintf(stderr
, "%s%sCannot find component class %s",
913 bt_common_color_bold(),
914 bt_common_color_fg_red(),
915 bt_common_color_reset());
916 print_plugin_comp_cls_opt(stderr
,
917 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
918 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
919 cfg
->cmd_data
.query
.cfg_component
->type
);
920 fprintf(stderr
, "\n");
925 ret
= query(comp_cls
, cfg
->cmd_data
.query
.object
->str
,
926 cfg
->cmd_data
.query
.cfg_component
->params
, &results
,
932 print_value(stdout
, results
, 0);
936 BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", "
937 "comp-cls-name=\"%s\", comp-cls-type=%d "
938 "object=\"%s\"", fail_reason
,
939 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
940 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
941 cfg
->cmd_data
.query
.cfg_component
->type
,
942 cfg
->cmd_data
.query
.object
->str
);
943 fprintf(stderr
, "%s%sFailed to query info to %s",
944 bt_common_color_bold(),
945 bt_common_color_fg_red(),
946 bt_common_color_reset());
947 print_plugin_comp_cls_opt(stderr
,
948 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
949 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
950 cfg
->cmd_data
.query
.cfg_component
->type
);
951 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
952 bt_common_color_bold(),
953 bt_common_color_fg_red(),
954 cfg
->cmd_data
.query
.object
->str
,
956 bt_common_color_reset());
966 int cmd_help(struct bt_config
*cfg
)
969 struct bt_plugin
*plugin
= NULL
;
972 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
974 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
975 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
976 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
977 bt_common_color_bold(), bt_common_color_fg_red(),
978 bt_common_color_fg_blue(),
979 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
980 bt_common_color_reset());
985 print_plugin_info(plugin
);
986 printf(" %sComponent classes%s: %d\n",
987 bt_common_color_bold(),
988 bt_common_color_reset(),
989 (int) bt_plugin_get_component_class_count(plugin
));
992 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
993 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
994 struct bt_component_class
*needed_comp_cls
=
995 find_component_class(
996 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
997 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
998 cfg
->cmd_data
.help
.cfg_component
->type
);
1000 if (!needed_comp_cls
) {
1001 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1002 "comp-cls-name=\"%s\", comp-cls-type=%d",
1003 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1004 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1005 cfg
->cmd_data
.help
.cfg_component
->type
);
1006 fprintf(stderr
, "\n%s%sCannot find component class %s",
1007 bt_common_color_bold(),
1008 bt_common_color_fg_red(),
1009 bt_common_color_reset());
1010 print_plugin_comp_cls_opt(stderr
,
1011 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1012 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1013 cfg
->cmd_data
.help
.cfg_component
->type
);
1014 fprintf(stderr
, "\n");
1019 bt_put(needed_comp_cls
);
1022 for (i
= 0; i
< bt_plugin_get_component_class_count(plugin
); i
++) {
1023 struct bt_component_class
*comp_cls
=
1024 bt_plugin_get_component_class_by_index(plugin
, i
);
1025 const char *comp_class_name
=
1026 bt_component_class_get_name(comp_cls
);
1027 const char *comp_class_description
=
1028 bt_component_class_get_description(comp_cls
);
1029 const char *comp_class_help
=
1030 bt_component_class_get_help(comp_cls
);
1031 enum bt_component_class_type type
=
1032 bt_component_class_get_type(comp_cls
);
1034 BT_ASSERT(comp_cls
);
1036 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
1037 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
1038 if (strcmp(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1039 comp_class_name
) != 0 ||
1040 type
!= cfg
->cmd_data
.help
.cfg_component
->type
) {
1047 print_plugin_comp_cls_opt(stdout
,
1048 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1052 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1053 bt_common_color_reset(),
1054 comp_class_description
? comp_class_description
: "(None)");
1056 if (comp_class_help
) {
1057 printf("\n%s\n", comp_class_help
);
1069 int cmd_list_plugins(struct bt_config
*cfg
)
1072 int plugins_count
, component_classes_count
= 0, i
;
1074 printf("From the following plugin paths:\n\n");
1075 print_value(stdout
, cfg
->plugin_paths
, 2);
1077 plugins_count
= loaded_plugins
->len
;
1078 if (plugins_count
== 0) {
1079 printf("No plugins found.\n");
1083 for (i
= 0; i
< plugins_count
; i
++) {
1084 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1086 component_classes_count
+= bt_plugin_get_component_class_count(plugin
);
1089 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1090 bt_common_color_bold(),
1091 component_classes_count
,
1092 bt_common_color_reset(),
1093 bt_common_color_bold(),
1095 bt_common_color_reset());
1097 for (i
= 0; i
< plugins_count
; i
++) {
1099 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1101 component_classes_count
=
1102 bt_plugin_get_component_class_count(plugin
);
1104 print_plugin_info(plugin
);
1106 if (component_classes_count
== 0) {
1107 printf(" %sComponent classes%s: (none)\n",
1108 bt_common_color_bold(),
1109 bt_common_color_reset());
1111 printf(" %sComponent classes%s:\n",
1112 bt_common_color_bold(),
1113 bt_common_color_reset());
1116 for (j
= 0; j
< component_classes_count
; j
++) {
1117 struct bt_component_class
*comp_class
=
1118 bt_plugin_get_component_class_by_index(
1120 const char *comp_class_name
=
1121 bt_component_class_get_name(comp_class
);
1122 const char *comp_class_description
=
1123 bt_component_class_get_description(comp_class
);
1124 enum bt_component_class_type type
=
1125 bt_component_class_get_type(comp_class
);
1128 print_plugin_comp_cls_opt(stdout
,
1129 bt_plugin_get_name(plugin
), comp_class_name
,
1132 if (comp_class_description
) {
1133 printf(": %s", comp_class_description
);
1146 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1149 struct bt_component_class
*comp_cls
= NULL
;
1150 struct bt_value
*results
= NULL
;
1151 struct bt_value
*params
= NULL
;
1152 struct bt_value
*map
= NULL
;
1153 struct bt_value
*v
= NULL
;
1154 static const char * const plugin_name
= "ctf";
1155 static const char * const comp_cls_name
= "lttng-live";
1156 static const enum bt_component_class_type comp_cls_type
=
1157 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1158 int64_t array_size
, i
;
1159 const char *fail_reason
= NULL
;
1160 FILE *out_stream
= stdout
;
1162 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1163 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1166 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1167 "comp-cls-name=\"%s\", comp-cls-type=%d",
1168 plugin_name
, comp_cls_name
,
1169 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1170 fprintf(stderr
, "%s%sCannot find component class %s",
1171 bt_common_color_bold(),
1172 bt_common_color_fg_red(),
1173 bt_common_color_reset());
1174 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1175 comp_cls_name
, comp_cls_type
);
1176 fprintf(stderr
, "\n");
1180 params
= bt_value_map_create();
1185 ret
= bt_value_map_insert_string(params
, "url",
1186 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1191 ret
= query(comp_cls
, "sessions", params
, &results
, &fail_reason
);
1198 if (!bt_value_is_array(results
)) {
1199 BT_LOGE_STR("Expecting an array for sessions query.");
1200 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1201 bt_common_color_bold(),
1202 bt_common_color_fg_red(),
1203 bt_common_color_reset());
1207 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1209 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1213 BT_LOGE_ERRNO("Cannot open file for writing",
1215 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1220 array_size
= bt_value_array_size(results
);
1221 for (i
= 0; i
< array_size
; i
++) {
1222 const char *url_text
;
1223 int64_t timer_us
, streams
, clients
;
1225 map
= bt_value_array_get(results
, i
);
1227 BT_LOGE_STR("Unexpected empty array entry.");
1230 if (!bt_value_is_map(map
)) {
1231 BT_LOGE_STR("Unexpected entry type.");
1235 v
= bt_value_map_get(map
, "url");
1237 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1240 ret
= bt_value_string_get(v
, &url_text
);
1241 BT_ASSERT(ret
== 0);
1242 fprintf(out_stream
, "%s", url_text
);
1245 v
= bt_value_map_get(map
, "timer-us");
1247 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1250 ret
= bt_value_integer_get(v
, &timer_us
);
1251 BT_ASSERT(ret
== 0);
1252 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1255 v
= bt_value_map_get(map
, "stream-count");
1257 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1260 ret
= bt_value_integer_get(v
, &streams
);
1261 BT_ASSERT(ret
== 0);
1262 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1265 v
= bt_value_map_get(map
, "client-count");
1267 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1270 ret
= bt_value_integer_get(v
, &clients
);
1271 BT_ASSERT(ret
== 0);
1272 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1281 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1282 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1283 bt_common_color_bold(),
1284 bt_common_color_fg_red(),
1286 bt_common_color_reset());
1298 if (out_stream
&& out_stream
!= stdout
) {
1299 int fclose_ret
= fclose(out_stream
);
1302 BT_LOGE_ERRNO("Cannot close file stream",
1304 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1312 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1315 struct bt_component_class
*comp_cls
= NULL
;
1316 struct bt_value
*results
= NULL
;
1317 struct bt_value
*params
= NULL
;
1318 struct bt_value
*metadata_text_value
= NULL
;
1319 const char *metadata_text
= NULL
;
1320 static const char * const plugin_name
= "ctf";
1321 static const char * const comp_cls_name
= "fs";
1322 static const enum bt_component_class_type comp_cls_type
=
1323 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1324 const char *fail_reason
= NULL
;
1325 FILE *out_stream
= stdout
;
1327 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1328 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1331 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1332 "comp-cls-name=\"%s\", comp-cls-type=%d",
1333 plugin_name
, comp_cls_name
,
1334 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1335 fprintf(stderr
, "%s%sCannot find component class %s",
1336 bt_common_color_bold(),
1337 bt_common_color_fg_red(),
1338 bt_common_color_reset());
1339 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1340 comp_cls_name
, comp_cls_type
);
1341 fprintf(stderr
, "\n");
1346 params
= bt_value_map_create();
1352 ret
= bt_value_map_insert_string(params
, "path",
1353 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1359 ret
= query(comp_cls
, "metadata-info", params
, &results
, &fail_reason
);
1364 metadata_text_value
= bt_value_map_get(results
, "text");
1365 if (!metadata_text_value
) {
1366 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1371 ret
= bt_value_string_get(metadata_text_value
, &metadata_text
);
1372 BT_ASSERT(ret
== 0);
1374 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1376 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1380 BT_LOGE_ERRNO("Cannot open file for writing",
1382 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1387 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1389 BT_LOGE("Cannot write whole metadata text to output stream: "
1397 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1398 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1399 bt_common_color_bold(),
1400 bt_common_color_fg_red(),
1402 bt_common_color_reset());
1405 destroy_the_query_executor();
1408 bt_put(metadata_text_value
);
1411 if (out_stream
&& out_stream
!= stdout
) {
1412 int fclose_ret
= fclose(out_stream
);
1415 BT_LOGE_ERRNO("Cannot close file stream",
1417 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1425 char *instance_name
;
1429 struct trace_range
{
1430 uint64_t intersection_range_begin_ns
;
1431 uint64_t intersection_range_end_ns
;
1435 guint
port_id_hash(gconstpointer v
)
1437 const struct port_id
*id
= v
;
1439 BT_ASSERT(id
->instance_name
);
1440 BT_ASSERT(id
->port_name
);
1442 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1446 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1448 const struct port_id
*id1
= v1
;
1449 const struct port_id
*id2
= v2
;
1451 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1452 !strcmp(id1
->port_name
, id2
->port_name
);
1456 void port_id_destroy(gpointer data
)
1458 struct port_id
*id
= data
;
1460 free(id
->instance_name
);
1461 free(id
->port_name
);
1466 void trace_range_destroy(gpointer data
)
1471 struct cmd_run_ctx
{
1473 GHashTable
*components
;
1476 struct bt_graph
*graph
;
1479 struct bt_config
*cfg
;
1483 bool stream_intersection_mode
;
1486 * Association of struct port_id -> struct trace_range.
1488 GHashTable
*intersections
;
1491 /* Returns a timestamp of the form "(-)s.ns" */
1493 char *s_from_ns(int64_t ns
)
1498 int64_t ts_sec_abs
, ts_nsec_abs
;
1499 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1500 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1502 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1503 is_negative
= false;
1504 ts_sec_abs
= ts_sec
;
1505 ts_nsec_abs
= ts_nsec
;
1506 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1507 is_negative
= false;
1508 ts_sec_abs
= ts_sec
- 1;
1509 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1510 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1512 ts_sec_abs
= ts_sec
;
1513 ts_nsec_abs
= -ts_nsec
;
1514 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1516 ts_sec_abs
= -(ts_sec
+ 1);
1517 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1518 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1520 ts_sec_abs
= -ts_sec
;
1521 ts_nsec_abs
= ts_nsec
;
1522 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1524 ts_sec_abs
= -ts_sec
;
1525 ts_nsec_abs
= -ts_nsec
;
1528 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1529 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1537 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1538 struct cmd_run_ctx
*ctx
, struct bt_component
*upstream_comp
,
1539 struct bt_port
*upstream_port
,
1540 struct bt_config_connection
*cfg_conn
)
1543 GQuark downstreamp_comp_name_quark
;
1544 struct bt_component
*downstream_comp
;
1545 int64_t downstream_port_count
;
1547 int64_t (*port_count_fn
)(struct bt_component
*);
1548 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
1549 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1550 bool insert_trimmer
= false;
1551 struct bt_value
*trimmer_params
= NULL
;
1552 char *intersection_begin
= NULL
;
1553 char *intersection_end
= NULL
;
1554 struct bt_component
*trimmer
= NULL
;
1555 struct bt_component_class
*trimmer_class
= NULL
;
1556 struct bt_port
*trimmer_input
= NULL
;
1557 struct bt_port
*trimmer_output
= NULL
;
1559 if (ctx
->intersections
&&
1560 bt_component_get_class_type(upstream_comp
) ==
1561 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1562 struct trace_range
*range
;
1563 struct port_id port_id
= {
1564 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1565 .port_name
= (char *) bt_port_get_name(upstream_port
)
1568 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1572 range
= (struct trace_range
*) g_hash_table_lookup(
1573 ctx
->intersections
, &port_id
);
1575 enum bt_value_status status
;
1577 intersection_begin
= s_from_ns(
1578 range
->intersection_range_begin_ns
);
1579 intersection_end
= s_from_ns(
1580 range
->intersection_range_end_ns
);
1581 if (!intersection_begin
|| !intersection_end
) {
1582 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1586 insert_trimmer
= true;
1587 trimmer_params
= bt_value_map_create();
1588 if (!trimmer_params
) {
1592 status
= bt_value_map_insert_string(trimmer_params
,
1593 "begin", intersection_begin
);
1594 if (status
!= BT_VALUE_STATUS_OK
) {
1597 status
= bt_value_map_insert_string(trimmer_params
,
1598 "end", intersection_end
);
1599 if (status
!= BT_VALUE_STATUS_OK
) {
1604 trimmer_class
= find_component_class("utils", "trimmer",
1605 BT_COMPONENT_CLASS_TYPE_FILTER
);
1606 if (!trimmer_class
) {
1611 BT_LOGI("Connecting upstream port to the next available downstream port: "
1612 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1613 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1614 upstream_port
, bt_port_get_name(upstream_port
),
1615 cfg_conn
->downstream_comp_name
->str
,
1616 cfg_conn
->arg
->str
);
1617 downstreamp_comp_name_quark
= g_quark_from_string(
1618 cfg_conn
->downstream_comp_name
->str
);
1619 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1620 downstream_comp
= g_hash_table_lookup(ctx
->components
,
1621 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1622 if (!downstream_comp
) {
1623 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1624 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1625 cfg_conn
->arg
->str
);
1626 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1627 cfg_conn
->arg
->str
);
1631 if (bt_component_is_filter(downstream_comp
)) {
1632 port_count_fn
= bt_component_filter_get_input_port_count
;
1633 port_by_index_fn
= bt_component_filter_get_input_port_by_index
;
1634 } else if (bt_component_is_sink(downstream_comp
)) {
1635 port_count_fn
= bt_component_sink_get_input_port_count
;
1636 port_by_index_fn
= bt_component_sink_get_input_port_by_index
;
1639 * Should never happen because the connections are
1640 * validated before we get here.
1642 BT_LOGF("Invalid connection: downstream component is a source: "
1643 "conn-arg=\"%s\"", cfg_conn
->arg
->str
);
1647 downstream_port_count
= port_count_fn(downstream_comp
);
1648 BT_ASSERT(downstream_port_count
>= 0);
1650 for (i
= 0; i
< downstream_port_count
; i
++) {
1651 struct bt_port
*downstream_port
=
1652 port_by_index_fn(downstream_comp
, i
);
1653 const char *upstream_port_name
;
1654 const char *downstream_port_name
;
1656 BT_ASSERT(downstream_port
);
1658 /* Skip port if it's already connected. */
1659 if (bt_port_is_connected(downstream_port
)) {
1660 bt_put(downstream_port
);
1661 BT_LOGD("Skipping downstream port: already connected: "
1662 "port-addr=%p, port-name=\"%s\"",
1664 bt_port_get_name(downstream_port
));
1668 downstream_port_name
= bt_port_get_name(downstream_port
);
1669 BT_ASSERT(downstream_port_name
);
1670 upstream_port_name
= bt_port_get_name(upstream_port
);
1671 BT_ASSERT(upstream_port_name
);
1673 if (!bt_common_star_glob_match(
1674 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1675 downstream_port_name
, SIZE_MAX
)) {
1676 bt_put(downstream_port
);
1680 if (insert_trimmer
) {
1682 * In order to insert the trimmer between the two
1683 * components that were being connected, we create
1684 * a connection configuration entry which describes
1685 * a connection from the trimmer's output to the
1686 * original input that was being connected.
1688 * Hence, the creation of the trimmer will cause the
1689 * graph "new port" listener to establish all downstream
1690 * connections as its output port is connected. We will
1691 * then establish the connection between the original
1692 * upstream source and the trimmer.
1694 char *trimmer_name
= NULL
;
1695 enum bt_graph_status graph_status
;
1697 ret
= asprintf(&trimmer_name
, "%s-%s",
1698 "stream-intersection-trimmer",
1699 upstream_port_name
);
1705 ctx
->connect_ports
= false;
1706 graph_status
= bt_graph_add_component(ctx
->graph
,
1707 trimmer_class
, trimmer_name
, trimmer_params
,
1710 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1716 bt_component_filter_get_input_port_by_index(
1718 if (!trimmer_input
) {
1722 bt_component_filter_get_output_port_by_index(
1724 if (!trimmer_output
) {
1729 * Replace the current downstream port by the trimmer's
1732 BT_MOVE(downstream_port
, trimmer_input
);
1733 downstream_port_name
= bt_port_get_name(
1735 if (!downstream_port_name
) {
1740 /* We have a winner! */
1741 status
= bt_graph_connect_ports(ctx
->graph
,
1742 upstream_port
, downstream_port
, NULL
);
1743 BT_PUT(downstream_port
);
1745 case BT_GRAPH_STATUS_OK
:
1747 case BT_GRAPH_STATUS_CANCELED
:
1748 BT_LOGI_STR("Graph was canceled by user.");
1749 status
= BT_GRAPH_STATUS_OK
;
1751 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1752 BT_LOGE("A component refused a connection to one of its ports: "
1753 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1754 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1755 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1756 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1758 upstream_comp
, bt_component_get_name(upstream_comp
),
1759 upstream_port
, bt_port_get_name(upstream_port
),
1760 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1761 downstream_port
, downstream_port_name
,
1762 cfg_conn
->arg
->str
);
1764 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1765 bt_port_get_name(upstream_port
),
1766 downstream_port_name
,
1767 cfg_conn
->arg
->str
);
1770 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1771 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1772 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1773 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1774 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1776 upstream_comp
, bt_component_get_name(upstream_comp
),
1777 upstream_port
, bt_port_get_name(upstream_port
),
1778 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1779 downstream_port
, downstream_port_name
,
1780 cfg_conn
->arg
->str
);
1782 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1783 bt_port_get_name(upstream_port
),
1784 downstream_port_name
,
1785 cfg_conn
->arg
->str
);
1789 BT_LOGI("Connected component ports: "
1790 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1791 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1792 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1793 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1795 upstream_comp
, bt_component_get_name(upstream_comp
),
1796 upstream_port
, bt_port_get_name(upstream_port
),
1797 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1798 downstream_port
, downstream_port_name
,
1799 cfg_conn
->arg
->str
);
1801 if (insert_trimmer
) {
1803 * The first connection, from the source to the trimmer,
1804 * has been done. We now connect the trimmer to the
1805 * original downstream port.
1807 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1808 ctx
, trimmer
, trimmer_output
, cfg_conn
);
1812 ctx
->connect_ports
= true;
1816 * We found a matching downstream port: the search is
1822 /* No downstream port found */
1823 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1824 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1825 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1826 upstream_port
, bt_port_get_name(upstream_port
),
1827 cfg_conn
->downstream_comp_name
->str
,
1828 cfg_conn
->arg
->str
);
1830 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1831 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1837 free(intersection_begin
);
1838 free(intersection_end
);
1839 BT_PUT(trimmer_params
);
1840 BT_PUT(trimmer_class
);
1842 BT_PUT(trimmer_input
);
1843 BT_PUT(trimmer_output
);
1848 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1849 struct bt_port
*upstream_port
)
1852 const char *upstream_port_name
;
1853 const char *upstream_comp_name
;
1854 struct bt_component
*upstream_comp
= NULL
;
1858 BT_ASSERT(upstream_port
);
1859 upstream_port_name
= bt_port_get_name(upstream_port
);
1860 BT_ASSERT(upstream_port_name
);
1861 upstream_comp
= bt_port_get_component(upstream_port
);
1862 if (!upstream_comp
) {
1863 BT_LOGW("Upstream port to connect is not part of a component: "
1864 "port-addr=%p, port-name=\"%s\"",
1865 upstream_port
, upstream_port_name
);
1870 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1871 BT_ASSERT(upstream_comp_name
);
1872 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1873 "port-addr=%p, port-name=\"%s\"",
1874 upstream_comp
, upstream_comp_name
,
1875 upstream_port
, upstream_port_name
);
1877 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1878 struct bt_config_connection
*cfg_conn
=
1880 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1882 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1883 upstream_comp_name
)) {
1887 if (!bt_common_star_glob_match(
1888 cfg_conn
->upstream_port_glob
->str
,
1889 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1893 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1894 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1896 BT_LOGE("Cannot connect upstream port: "
1897 "port-addr=%p, port-name=\"%s\"",
1899 upstream_port_name
);
1901 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1904 cfg_conn
->arg
->str
);
1910 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1911 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1912 upstream_port_name
);
1914 "Cannot create connection: upstream port `%s` does not match any connection\n",
1915 upstream_port_name
);
1921 bt_put(upstream_comp
);
1926 void graph_port_added_listener(struct bt_port
*port
, void *data
)
1928 struct bt_component
*comp
= NULL
;
1929 struct cmd_run_ctx
*ctx
= data
;
1931 comp
= bt_port_get_component(port
);
1932 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1933 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1934 comp
, comp
? bt_component_get_name(comp
) : "",
1935 port
, bt_port_get_name(port
));
1937 if (!ctx
->connect_ports
) {
1942 BT_LOGW_STR("Port has no component.");
1946 if (bt_port_is_connected(port
)) {
1947 BT_LOGW_STR("Port is already connected.");
1951 if (!bt_port_is_output(port
)) {
1952 BT_LOGI_STR("Skipping input port.");
1956 if (cmd_run_ctx_connect_upstream_port(ctx
, port
)) {
1957 BT_LOGF_STR("Cannot connect upstream port.");
1958 fprintf(stderr
, "Added port could not be connected: aborting\n");
1968 void graph_port_removed_listener(struct bt_component
*component
,
1969 struct bt_port
*port
, void *data
)
1971 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1972 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1973 component
, bt_component_get_name(component
),
1974 port
, bt_port_get_name(port
));
1978 void graph_ports_connected_listener(struct bt_port
*upstream_port
,
1979 struct bt_port
*downstream_port
, void *data
)
1981 struct bt_component
*upstream_comp
= bt_port_get_component(upstream_port
);
1982 struct bt_component
*downstream_comp
= bt_port_get_component(downstream_port
);
1984 BT_ASSERT(upstream_comp
);
1985 BT_ASSERT(downstream_comp
);
1986 BT_LOGI("Graph's component ports connected: "
1987 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1988 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1989 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1990 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1991 upstream_comp
, bt_component_get_name(upstream_comp
),
1992 upstream_port
, bt_port_get_name(upstream_port
),
1993 downstream_comp
, bt_component_get_name(downstream_comp
),
1994 downstream_port
, bt_port_get_name(downstream_port
));
1995 bt_put(upstream_comp
);
1996 bt_put(downstream_comp
);
2000 void graph_ports_disconnected_listener(
2001 struct bt_component
*upstream_component
,
2002 struct bt_component
*downstream_component
,
2003 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
2006 BT_LOGI("Graph's component ports disconnected: "
2007 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
2008 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
2009 upstream_port
, bt_port_get_name(upstream_port
),
2010 downstream_port
, bt_port_get_name(downstream_port
));
2014 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
2020 if (ctx
->components
) {
2021 g_hash_table_destroy(ctx
->components
);
2022 ctx
->components
= NULL
;
2025 if (ctx
->intersections
) {
2026 g_hash_table_destroy(ctx
->intersections
);
2027 ctx
->intersections
= NULL
;
2036 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2041 ctx
->connect_ports
= false;
2042 ctx
->components
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
2044 if (!ctx
->components
) {
2048 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2049 ctx
->stream_intersection_mode
= true;
2050 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2051 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2052 if (!ctx
->intersections
) {
2057 ctx
->graph
= bt_graph_create();
2062 the_graph
= ctx
->graph
;
2063 ret
= bt_graph_add_port_added_listener(ctx
->graph
,
2064 graph_port_added_listener
, NULL
, ctx
);
2066 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2070 ret
= bt_graph_add_port_removed_listener(ctx
->graph
,
2071 graph_port_removed_listener
, NULL
, ctx
);
2073 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
2077 ret
= bt_graph_add_ports_connected_listener(ctx
->graph
,
2078 graph_ports_connected_listener
, NULL
, ctx
);
2080 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
2084 ret
= bt_graph_add_ports_disconnected_listener(ctx
->graph
,
2085 graph_ports_disconnected_listener
, NULL
, ctx
);
2087 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
2094 cmd_run_ctx_destroy(ctx
);
2102 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2103 struct bt_config_component
*cfg_comp
,
2104 struct bt_component_class
*comp_cls
)
2108 int64_t trace_count
;
2109 enum bt_value_status value_status
;
2110 const char *path
= NULL
;
2111 struct bt_value
*component_path_value
= NULL
;
2112 struct bt_value
*query_params
= NULL
;
2113 struct bt_value
*query_result
= NULL
;
2114 struct bt_value
*trace_info
= NULL
;
2115 struct bt_value
*intersection_range
= NULL
;
2116 struct bt_value
*intersection_begin
= NULL
;
2117 struct bt_value
*intersection_end
= NULL
;
2118 struct bt_value
*stream_path_value
= NULL
;
2119 struct bt_value
*stream_paths
= NULL
;
2120 struct bt_value
*stream_infos
= NULL
;
2121 struct bt_value
*stream_info
= NULL
;
2122 struct port_id
*port_id
= NULL
;
2123 struct trace_range
*trace_range
= NULL
;
2124 const char *fail_reason
= NULL
;
2126 component_path_value
= bt_value_map_get(cfg_comp
->params
, "path");
2127 if (component_path_value
&& !bt_value_is_string(component_path_value
)) {
2128 BT_LOGD("Cannot get path parameter: component-name=%s",
2129 cfg_comp
->instance_name
->str
);
2134 value_status
= bt_value_string_get(component_path_value
, &path
);
2135 if (value_status
!= BT_VALUE_STATUS_OK
) {
2136 BT_LOGD("Cannot get path string value: component-name=%s",
2137 cfg_comp
->instance_name
->str
);
2142 query_params
= bt_value_map_create();
2143 if (!query_params
) {
2144 BT_LOGE_STR("Cannot create query parameters.");
2149 value_status
= bt_value_map_insert(query_params
, "path", component_path_value
);
2150 if (value_status
!= BT_VALUE_STATUS_OK
) {
2151 BT_LOGE_STR("Cannot insert path parameter in query parameter map.");
2156 ret
= query(comp_cls
, "trace-info", query_params
, &query_result
,
2159 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2160 "comp-class-name=\"%s\"", fail_reason
,
2161 bt_component_class_get_name(comp_cls
));
2166 BT_ASSERT(query_result
);
2168 if (!bt_value_is_array(query_result
)) {
2169 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2170 "component-class-name=%s",
2171 bt_component_class_get_name(comp_cls
));
2176 trace_count
= bt_value_array_size(query_result
);
2177 if (trace_count
< 0) {
2182 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2184 uint64_t stream_idx
;
2185 int64_t stream_count
;
2187 trace_info
= bt_value_array_get(query_result
, trace_idx
);
2188 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2190 BT_LOGD_STR("Cannot retrieve trace from query result.");
2194 intersection_range
= bt_value_map_get(trace_info
,
2195 "intersection-range-ns");
2196 if (!intersection_range
) {
2198 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2202 intersection_begin
= bt_value_map_get(intersection_range
,
2204 if (!intersection_begin
) {
2206 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2210 intersection_end
= bt_value_map_get(intersection_range
,
2212 if (!intersection_end
) {
2214 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2218 value_status
= bt_value_integer_get(intersection_begin
, &begin
);
2219 if (value_status
!= BT_VALUE_STATUS_OK
) {
2221 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'begin\' field from query result.");
2225 value_status
= bt_value_integer_get(intersection_end
, &end
);
2226 if (value_status
!= BT_VALUE_STATUS_OK
) {
2228 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'end\' field from query result.");
2232 if (begin
< 0 || end
< 0 || end
< begin
) {
2233 BT_LOGW("Invalid trace stream intersection values: "
2234 "intersection-range-ns:begin=%" PRId64
2235 ", intersection-range-ns:end=%" PRId64
,
2241 stream_infos
= bt_value_map_get(trace_info
, "streams");
2242 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2244 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2248 stream_count
= bt_value_array_size(stream_infos
);
2249 if (stream_count
< 0) {
2257 * The first path of a stream's "paths" is currently used to
2258 * associate streams/ports to a given trace intersection.
2260 * This is a fragile hack as it relies on the port names
2261 * being set to the various streams path.
2263 * A stream name should be introduced as part of the trace-info
2266 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2267 const char *stream_path
;
2269 port_id
= g_new0(struct port_id
, 1);
2272 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2275 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2276 if (!port_id
->instance_name
) {
2278 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2282 trace_range
= g_new0(struct trace_range
, 1);
2285 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2288 trace_range
->intersection_range_begin_ns
= begin
;
2289 trace_range
->intersection_range_end_ns
= end
;
2291 stream_info
= bt_value_array_get(stream_infos
,
2293 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2295 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2299 stream_paths
= bt_value_map_get(stream_info
, "paths");
2300 if (!stream_paths
|| !bt_value_is_array(stream_paths
)) {
2302 BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
2306 stream_path_value
= bt_value_array_get(stream_paths
, 0);
2307 if (!stream_path_value
||
2308 !bt_value_is_string(stream_path_value
)) {
2310 BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
2314 value_status
= bt_value_string_get(stream_path_value
,
2316 if (value_status
!= BT_VALUE_STATUS_OK
) {
2321 port_id
->port_name
= strdup(stream_path
);
2322 if (!port_id
->port_name
) {
2324 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2328 BT_LOGD("Inserting stream intersection ");
2330 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2334 BT_PUT(stream_info
);
2335 BT_PUT(stream_paths
);
2336 BT_PUT(stream_path_value
);
2340 BT_PUT(stream_paths
);
2341 BT_PUT(stream_path_value
);
2342 BT_PUT(intersection_range
);
2343 BT_PUT(intersection_begin
);
2344 BT_PUT(intersection_end
);
2345 BT_PUT(stream_paths
);
2346 BT_PUT(stream_path_value
);
2352 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2353 bt_common_color_bold(),
2354 bt_common_color_fg_yellow(),
2355 path
? path
: "(unknown)",
2356 bt_common_color_reset());
2358 bt_put(component_path_value
);
2359 bt_put(query_params
);
2360 bt_put(query_result
);
2362 bt_put(intersection_range
);
2363 bt_put(intersection_begin
);
2364 bt_put(intersection_end
);
2365 bt_put(stream_infos
);
2366 bt_put(stream_info
);
2367 bt_put(stream_paths
);
2368 bt_put(stream_path_value
);
2370 g_free(trace_range
);
2375 int cmd_run_ctx_create_components_from_config_components(
2376 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2379 struct bt_component_class
*comp_cls
= NULL
;
2380 struct bt_component
*comp
= NULL
;
2383 for (i
= 0; i
< cfg_components
->len
; i
++) {
2384 struct bt_config_component
*cfg_comp
=
2385 g_ptr_array_index(cfg_components
, i
);
2388 comp_cls
= find_component_class(cfg_comp
->plugin_name
->str
,
2389 cfg_comp
->comp_cls_name
->str
, cfg_comp
->type
);
2391 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2392 "comp-cls-name=\"%s\", comp-cls-type=%d",
2393 cfg_comp
->plugin_name
->str
,
2394 cfg_comp
->comp_cls_name
->str
,
2396 fprintf(stderr
, "%s%sCannot find component class %s",
2397 bt_common_color_bold(),
2398 bt_common_color_fg_red(),
2399 bt_common_color_reset());
2400 print_plugin_comp_cls_opt(stderr
,
2401 cfg_comp
->plugin_name
->str
,
2402 cfg_comp
->comp_cls_name
->str
,
2404 fprintf(stderr
, "\n");
2408 ret
= bt_graph_add_component(ctx
->graph
, comp_cls
,
2409 cfg_comp
->instance_name
->str
, cfg_comp
->params
, &comp
);
2411 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2412 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2414 cfg_comp
->plugin_name
->str
,
2415 cfg_comp
->comp_cls_name
->str
,
2416 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2417 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2418 bt_common_color_bold(),
2419 bt_common_color_fg_red(),
2420 cfg_comp
->instance_name
->str
,
2421 bt_common_color_reset());
2425 if (ctx
->stream_intersection_mode
&&
2426 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2427 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2433 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2434 comp
, cfg_comp
->instance_name
->str
);
2435 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2436 BT_ASSERT(quark
> 0);
2437 g_hash_table_insert(ctx
->components
,
2438 GUINT_TO_POINTER(quark
), comp
);
2455 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2460 * Make sure that, during this phase, our graph's "port added"
2461 * listener does not connect ports while we are creating the
2462 * components because we have a special, initial phase for
2465 ctx
->connect_ports
= false;
2467 ret
= cmd_run_ctx_create_components_from_config_components(
2468 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2474 ret
= cmd_run_ctx_create_components_from_config_components(
2475 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2481 ret
= cmd_run_ctx_create_components_from_config_components(
2482 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2493 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2494 struct bt_component
*comp
,
2495 int64_t (*port_count_fn
)(struct bt_component
*),
2496 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t))
2502 count
= port_count_fn(comp
);
2503 BT_ASSERT(count
>= 0);
2505 for (i
= 0; i
< count
; i
++) {
2506 struct bt_port
*upstream_port
= port_by_index_fn(comp
, i
);
2508 BT_ASSERT(upstream_port
);
2509 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2510 bt_put(upstream_port
);
2521 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2524 GHashTableIter iter
;
2525 gpointer g_name_quark
, g_comp
;
2527 ctx
->connect_ports
= true;
2528 g_hash_table_iter_init(&iter
, ctx
->components
);
2530 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2531 int64_t (*port_count_fn
)(struct bt_component
*);
2532 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
2534 if (bt_component_is_source(g_comp
)) {
2536 bt_component_source_get_output_port_count
;
2538 bt_component_source_get_output_port_by_index
;
2539 } else if (bt_component_is_filter(g_comp
)) {
2541 bt_component_filter_get_output_port_count
;
2543 bt_component_filter_get_output_port_by_index
;
2548 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
2549 g_comp
, port_count_fn
, port_by_index_fn
);
2560 const char *bt_graph_status_str(enum bt_graph_status status
)
2563 case BT_GRAPH_STATUS_CANCELED
:
2564 return "BT_GRAPH_STATUS_CANCELED";
2565 case BT_GRAPH_STATUS_AGAIN
:
2566 return "BT_GRAPH_STATUS_AGAIN";
2567 case BT_GRAPH_STATUS_END
:
2568 return "BT_GRAPH_STATUS_END";
2569 case BT_GRAPH_STATUS_OK
:
2570 return "BT_GRAPH_STATUS_OK";
2571 case BT_GRAPH_STATUS_INVALID
:
2572 return "BT_GRAPH_STATUS_INVALID";
2573 case BT_GRAPH_STATUS_NO_SINK
:
2574 return "BT_GRAPH_STATUS_NO_SINK";
2575 case BT_GRAPH_STATUS_ERROR
:
2576 return "BT_GRAPH_STATUS_ERROR";
2583 int cmd_run(struct bt_config
*cfg
)
2586 struct cmd_run_ctx ctx
= { 0 };
2588 /* Initialize the command's context and the graph object */
2589 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2590 BT_LOGE_STR("Cannot initialize the command's context.");
2591 fprintf(stderr
, "Cannot initialize the command's context\n");
2596 BT_LOGI_STR("Canceled by user before creating components.");
2600 BT_LOGI_STR("Creating components.");
2602 /* Create the requested component instances */
2603 if (cmd_run_ctx_create_components(&ctx
)) {
2604 BT_LOGE_STR("Cannot create components.");
2605 fprintf(stderr
, "Cannot create components\n");
2610 BT_LOGI_STR("Canceled by user before connecting components.");
2614 BT_LOGI_STR("Connecting components.");
2616 /* Connect the initially visible component ports */
2617 if (cmd_run_ctx_connect_ports(&ctx
)) {
2618 BT_LOGE_STR("Cannot connect initial component ports.");
2619 fprintf(stderr
, "Cannot connect initial component ports\n");
2624 BT_LOGI_STR("Canceled by user before running the graph.");
2628 BT_LOGI_STR("Running the graph.");
2632 enum bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2635 * Reset console in case something messed with console
2636 * codes during the graph's execution.
2638 printf("%s", bt_common_color_reset());
2640 fprintf(stderr
, "%s", bt_common_color_reset());
2641 BT_LOGV("bt_graph_run() returned: status=%s",
2642 bt_graph_status_str(graph_status
));
2644 switch (graph_status
) {
2645 case BT_GRAPH_STATUS_OK
:
2647 case BT_GRAPH_STATUS_CANCELED
:
2648 BT_LOGI_STR("Graph was canceled by user.");
2650 case BT_GRAPH_STATUS_AGAIN
:
2651 if (bt_graph_is_canceled(ctx
.graph
)) {
2652 BT_LOGI_STR("Graph was canceled by user.");
2656 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2657 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2659 cfg
->cmd_data
.run
.retry_duration_us
);
2661 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2662 if (bt_graph_is_canceled(ctx
.graph
)) {
2663 BT_LOGI_STR("Graph was canceled by user.");
2669 case BT_COMPONENT_STATUS_END
:
2672 BT_LOGE_STR("Graph failed to complete successfully");
2673 fprintf(stderr
, "Graph failed to complete successfully\n");
2686 cmd_run_ctx_destroy(&ctx
);
2691 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2693 const char *env_clash
;
2695 if (!cfg
->command_name
) {
2699 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2700 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2704 if (g_file_test(cfg
->command_name
,
2705 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2706 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2708 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2710 fprintf(stderr
, "\n");
2711 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
2717 void init_log_level(void)
2719 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2723 void set_auto_log_levels(struct bt_config
*cfg
)
2725 const char **env_var_name
;
2728 * Override the configuration's default log level if
2729 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2730 * are found for backward compatibility with legacy Babetrace 1.
2732 if (getenv("BABELTRACE_DEBUG") &&
2733 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2734 cfg
->log_level
= 'V';
2735 } else if (getenv("BABELTRACE_VERBOSE") &&
2736 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2737 cfg
->log_level
= 'I';
2741 * Set log levels according to --debug or --verbose. For
2742 * backward compatibility, --debug is more verbose than
2745 * --verbose: INFO log level
2746 * --debug: VERBOSE log level (includes DEBUG, which is
2747 * is less verbose than VERBOSE in the internal
2748 * logging framework)
2750 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2752 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2753 } else if (cfg
->debug
) {
2754 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2757 * Set library's default log level if not
2758 * explicitly specified.
2760 switch (cfg
->log_level
) {
2762 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE
);
2765 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2768 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
2771 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2774 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN
);
2777 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR
);
2780 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL
);
2788 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2790 bt_cli_log_level
= BT_LOG_INFO
;
2791 } else if (cfg
->debug
) {
2792 bt_cli_log_level
= BT_LOG_VERBOSE
;
2795 * Set CLI's default log level if not explicitly
2798 switch (cfg
->log_level
) {
2800 bt_cli_log_level
= BT_LOG_NONE
;
2803 bt_cli_log_level
= BT_LOG_VERBOSE
;
2806 bt_cli_log_level
= BT_LOG_DEBUG
;
2809 bt_cli_log_level
= BT_LOG_INFO
;
2812 bt_cli_log_level
= BT_LOG_WARN
;
2815 bt_cli_log_level
= BT_LOG_ERROR
;
2818 bt_cli_log_level
= BT_LOG_FATAL
;
2826 env_var_name
= log_level_env_var_names
;
2828 while (*env_var_name
) {
2829 if (!getenv(*env_var_name
)) {
2831 g_setenv(*env_var_name
, "I", 1);
2832 } else if (cfg
->debug
) {
2833 g_setenv(*env_var_name
, "V", 1);
2835 char val
[2] = { 0 };
2838 * Set module's default log level if not
2839 * explicitly specified.
2841 val
[0] = cfg
->log_level
;
2842 g_setenv(*env_var_name
, val
, 1);
2850 int main(int argc
, const char **argv
)
2854 struct bt_config
*cfg
;
2857 set_signal_handler();
2859 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2862 /* Quit without errors; typically usage/version */
2864 BT_LOGI_STR("Quitting without errors.");
2869 BT_LOGE("Command-line error: retcode=%d", retcode
);
2874 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2875 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2880 set_auto_log_levels(cfg
);
2883 if (cfg
->command_needs_plugins
) {
2884 ret
= load_all_plugins(cfg
->plugin_paths
);
2886 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2892 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2893 cfg
->command
, cfg
->command_name
);
2895 switch (cfg
->command
) {
2896 case BT_CONFIG_COMMAND_RUN
:
2899 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2900 ret
= cmd_list_plugins(cfg
);
2902 case BT_CONFIG_COMMAND_HELP
:
2903 ret
= cmd_help(cfg
);
2905 case BT_CONFIG_COMMAND_QUERY
:
2906 ret
= cmd_query(cfg
);
2908 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2909 ret
= cmd_print_ctf_metadata(cfg
);
2911 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2912 ret
= cmd_print_lttng_live_sessions(cfg
);
2915 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2919 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2920 cfg
->command
, cfg
->command_name
, ret
);
2921 warn_command_name_and_directory_clash(cfg
);
2922 retcode
= ret
? 1 : 0;