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_object_put_ref
);
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_OBJECT_PUT_REF_AND_RESET(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();
257 bt_object_put_ref(result
);
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_object_get_ref(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
);
311 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
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_REAL
:
472 status
= bt_value_real_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_get_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_borrow_element_by_index(
509 print_indent(fp
, indent
);
512 if (bt_value_is_array(element
) &&
513 bt_value_array_is_empty(element
)) {
514 fprintf(fp
, "[ ]\n");
518 if (bt_value_is_map(element
) &&
519 bt_value_map_is_empty(element
)) {
520 fprintf(fp
, "{ }\n");
524 if (bt_value_is_array(element
) ||
525 bt_value_is_map(element
)) {
529 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_entry(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_OBJECT_PUT_REF_AND_RESET(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_object_put_ref(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_object_get_ref(plugin
));
743 bt_object_put_ref(plugin
);
748 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
750 int nr_paths
, i
, ret
= 0;
752 nr_paths
= bt_value_array_get_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_borrow_element_by_index(
769 status
= bt_value_string_get(plugin_path_value
, &plugin_path
);
770 if (status
!= BT_VALUE_STATUS_OK
) {
771 BT_LOGD_STR("Cannot get plugin path string.");
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
);
786 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
788 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
793 add_to_loaded_plugins(plugin_set
);
794 bt_object_put_ref(plugin_set
);
801 int load_static_plugins(void)
804 struct bt_plugin_set
*plugin_set
;
806 BT_LOGI("Loading static plugins.");
807 plugin_set
= bt_plugin_create_all_from_static();
809 BT_LOGE("Unable to load static plugins.");
814 add_to_loaded_plugins(plugin_set
);
815 bt_object_put_ref(plugin_set
);
821 int load_all_plugins(struct bt_value
*plugin_paths
)
825 if (load_dynamic_plugins(plugin_paths
)) {
830 if (load_static_plugins()) {
835 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
842 void print_plugin_info(struct bt_plugin
*plugin
)
844 unsigned int major
, minor
, patch
;
846 enum bt_plugin_status version_status
;
847 const char *plugin_name
;
851 const char *plugin_description
;
853 plugin_name
= bt_plugin_get_name(plugin
);
854 path
= bt_plugin_get_path(plugin
);
855 author
= bt_plugin_get_author(plugin
);
856 license
= bt_plugin_get_license(plugin
);
857 plugin_description
= bt_plugin_get_description(plugin
);
858 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
860 printf("%s%s%s%s:\n", bt_common_color_bold(),
861 bt_common_color_fg_blue(), plugin_name
,
862 bt_common_color_reset());
864 printf(" %sPath%s: %s\n", bt_common_color_bold(),
865 bt_common_color_reset(), path
);
870 if (version_status
== BT_PLUGIN_STATUS_OK
) {
871 printf(" %sVersion%s: %u.%u.%u",
872 bt_common_color_bold(), bt_common_color_reset(),
873 major
, minor
, patch
);
882 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
883 bt_common_color_reset(),
884 plugin_description
? plugin_description
: "(None)");
885 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
886 bt_common_color_reset(), author
? author
: "(Unknown)");
887 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
888 bt_common_color_reset(),
889 license
? license
: "(Unknown)");
893 int cmd_query(struct bt_config
*cfg
)
896 struct bt_component_class
*comp_cls
= NULL
;
897 struct bt_value
*results
= NULL
;
898 const char *fail_reason
= NULL
;
900 comp_cls
= find_component_class(cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
901 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
902 cfg
->cmd_data
.query
.cfg_component
->type
);
904 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
905 "comp-cls-name=\"%s\", comp-cls-type=%d",
906 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
907 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
908 cfg
->cmd_data
.query
.cfg_component
->type
);
909 fprintf(stderr
, "%s%sCannot find component class %s",
910 bt_common_color_bold(),
911 bt_common_color_fg_red(),
912 bt_common_color_reset());
913 print_plugin_comp_cls_opt(stderr
,
914 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
915 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
916 cfg
->cmd_data
.query
.cfg_component
->type
);
917 fprintf(stderr
, "\n");
922 ret
= query(comp_cls
, cfg
->cmd_data
.query
.object
->str
,
923 cfg
->cmd_data
.query
.cfg_component
->params
, &results
,
929 print_value(stdout
, results
, 0);
933 BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", "
934 "comp-cls-name=\"%s\", comp-cls-type=%d "
935 "object=\"%s\"", fail_reason
,
936 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
937 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
938 cfg
->cmd_data
.query
.cfg_component
->type
,
939 cfg
->cmd_data
.query
.object
->str
);
940 fprintf(stderr
, "%s%sFailed to query info to %s",
941 bt_common_color_bold(),
942 bt_common_color_fg_red(),
943 bt_common_color_reset());
944 print_plugin_comp_cls_opt(stderr
,
945 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
946 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
947 cfg
->cmd_data
.query
.cfg_component
->type
);
948 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
949 bt_common_color_bold(),
950 bt_common_color_fg_red(),
951 cfg
->cmd_data
.query
.object
->str
,
953 bt_common_color_reset());
957 bt_object_put_ref(comp_cls
);
958 bt_object_put_ref(results
);
963 int cmd_help(struct bt_config
*cfg
)
966 struct bt_plugin
*plugin
= NULL
;
969 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
971 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
972 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
973 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
974 bt_common_color_bold(), bt_common_color_fg_red(),
975 bt_common_color_fg_blue(),
976 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
977 bt_common_color_reset());
982 print_plugin_info(plugin
);
983 printf(" %sComponent classes%s: %d\n",
984 bt_common_color_bold(),
985 bt_common_color_reset(),
986 (int) bt_plugin_get_component_class_count(plugin
));
989 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
990 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
991 struct bt_component_class
*needed_comp_cls
=
992 find_component_class(
993 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
994 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
995 cfg
->cmd_data
.help
.cfg_component
->type
);
997 if (!needed_comp_cls
) {
998 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
999 "comp-cls-name=\"%s\", comp-cls-type=%d",
1000 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1001 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1002 cfg
->cmd_data
.help
.cfg_component
->type
);
1003 fprintf(stderr
, "\n%s%sCannot find component class %s",
1004 bt_common_color_bold(),
1005 bt_common_color_fg_red(),
1006 bt_common_color_reset());
1007 print_plugin_comp_cls_opt(stderr
,
1008 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1009 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1010 cfg
->cmd_data
.help
.cfg_component
->type
);
1011 fprintf(stderr
, "\n");
1016 bt_object_put_ref(needed_comp_cls
);
1019 for (i
= 0; i
< bt_plugin_get_component_class_count(plugin
); i
++) {
1020 struct bt_component_class
*comp_cls
=
1021 bt_plugin_get_component_class_by_index(plugin
, i
);
1022 const char *comp_class_name
=
1023 bt_component_class_get_name(comp_cls
);
1024 const char *comp_class_description
=
1025 bt_component_class_get_description(comp_cls
);
1026 const char *comp_class_help
=
1027 bt_component_class_get_help(comp_cls
);
1028 enum bt_component_class_type type
=
1029 bt_component_class_get_type(comp_cls
);
1031 BT_ASSERT(comp_cls
);
1033 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
1034 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
1035 if (strcmp(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1036 comp_class_name
) != 0 ||
1037 type
!= cfg
->cmd_data
.help
.cfg_component
->type
) {
1038 bt_object_put_ref(comp_cls
);
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
);
1057 bt_object_put_ref(comp_cls
);
1061 bt_object_put_ref(plugin
);
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
);
1134 bt_object_put_ref(comp_class
);
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
;
1157 FILE *out_stream
= stdout
;
1159 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1160 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1163 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1164 "comp-cls-name=\"%s\", comp-cls-type=%d",
1165 plugin_name
, comp_cls_name
,
1166 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1167 fprintf(stderr
, "%s%sCannot find component class %s",
1168 bt_common_color_bold(),
1169 bt_common_color_fg_red(),
1170 bt_common_color_reset());
1171 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1172 comp_cls_name
, comp_cls_type
);
1173 fprintf(stderr
, "\n");
1177 params
= bt_value_map_create();
1182 ret
= bt_value_map_insert_string_entry(params
, "url",
1183 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1188 ret
= query(comp_cls
, "sessions", params
, &results
, &fail_reason
);
1195 if (!bt_value_is_array(results
)) {
1196 BT_LOGE_STR("Expecting an array for sessions query.");
1197 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1198 bt_common_color_bold(),
1199 bt_common_color_fg_red(),
1200 bt_common_color_reset());
1204 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1206 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1210 BT_LOGE_ERRNO("Cannot open file for writing",
1212 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1217 array_size
= bt_value_array_get_size(results
);
1218 for (i
= 0; i
< array_size
; i
++) {
1219 const char *url_text
;
1220 int64_t timer_us
, streams
, clients
;
1222 map
= bt_value_array_borrow_element_by_index(results
, i
);
1224 BT_LOGE_STR("Unexpected empty array entry.");
1227 if (!bt_value_is_map(map
)) {
1228 BT_LOGE_STR("Unexpected entry type.");
1232 v
= bt_value_map_borrow_entry_value(map
, "url");
1234 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1237 ret
= bt_value_string_get(v
, &url_text
);
1238 BT_ASSERT(ret
== 0);
1239 fprintf(out_stream
, "%s", url_text
);
1240 v
= bt_value_map_borrow_entry_value(map
, "timer-us");
1242 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1245 ret
= bt_value_integer_get(v
, &timer_us
);
1246 BT_ASSERT(ret
== 0);
1247 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1248 v
= bt_value_map_borrow_entry_value(map
, "stream-count");
1250 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1253 ret
= bt_value_integer_get(v
, &streams
);
1254 BT_ASSERT(ret
== 0);
1255 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1256 v
= bt_value_map_borrow_entry_value(map
, "client-count");
1258 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1261 ret
= bt_value_integer_get(v
, &clients
);
1262 BT_ASSERT(ret
== 0);
1263 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1269 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1270 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1271 bt_common_color_bold(),
1272 bt_common_color_fg_red(),
1274 bt_common_color_reset());
1280 bt_object_put_ref(results
);
1281 bt_object_put_ref(params
);
1282 bt_object_put_ref(comp_cls
);
1284 if (out_stream
&& out_stream
!= stdout
) {
1285 int fclose_ret
= fclose(out_stream
);
1288 BT_LOGE_ERRNO("Cannot close file stream",
1290 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1298 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1301 struct bt_component_class
*comp_cls
= NULL
;
1302 struct bt_value
*results
= NULL
;
1303 struct bt_value
*params
= NULL
;
1304 struct bt_value
*metadata_text_value
= NULL
;
1305 const char *metadata_text
= NULL
;
1306 static const char * const plugin_name
= "ctf";
1307 static const char * const comp_cls_name
= "fs";
1308 static const enum bt_component_class_type comp_cls_type
=
1309 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1310 const char *fail_reason
= NULL
;
1311 FILE *out_stream
= stdout
;
1313 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1314 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1317 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1318 "comp-cls-name=\"%s\", comp-cls-type=%d",
1319 plugin_name
, comp_cls_name
,
1320 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1321 fprintf(stderr
, "%s%sCannot find component class %s",
1322 bt_common_color_bold(),
1323 bt_common_color_fg_red(),
1324 bt_common_color_reset());
1325 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1326 comp_cls_name
, comp_cls_type
);
1327 fprintf(stderr
, "\n");
1332 params
= bt_value_map_create();
1338 ret
= bt_value_map_insert_string_entry(params
, "path",
1339 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1345 ret
= query(comp_cls
, "metadata-info", params
, &results
, &fail_reason
);
1350 metadata_text_value
= bt_value_map_borrow_entry_value(results
, "text");
1351 if (!metadata_text_value
) {
1352 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1357 ret
= bt_value_string_get(metadata_text_value
, &metadata_text
);
1358 BT_ASSERT(ret
== 0);
1360 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1362 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1366 BT_LOGE_ERRNO("Cannot open file for writing",
1368 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1373 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1375 BT_LOGE("Cannot write whole metadata text to output stream: "
1383 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1384 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1385 bt_common_color_bold(),
1386 bt_common_color_fg_red(),
1388 bt_common_color_reset());
1391 destroy_the_query_executor();
1392 bt_object_put_ref(results
);
1393 bt_object_put_ref(params
);
1394 bt_object_put_ref(comp_cls
);
1396 if (out_stream
&& out_stream
!= stdout
) {
1397 int fclose_ret
= fclose(out_stream
);
1400 BT_LOGE_ERRNO("Cannot close file stream",
1402 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1410 char *instance_name
;
1414 struct trace_range
{
1415 uint64_t intersection_range_begin_ns
;
1416 uint64_t intersection_range_end_ns
;
1420 guint
port_id_hash(gconstpointer v
)
1422 const struct port_id
*id
= v
;
1424 BT_ASSERT(id
->instance_name
);
1425 BT_ASSERT(id
->port_name
);
1427 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1431 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1433 const struct port_id
*id1
= v1
;
1434 const struct port_id
*id2
= v2
;
1436 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1437 !strcmp(id1
->port_name
, id2
->port_name
);
1441 void port_id_destroy(gpointer data
)
1443 struct port_id
*id
= data
;
1445 free(id
->instance_name
);
1446 free(id
->port_name
);
1451 void trace_range_destroy(gpointer data
)
1456 struct cmd_run_ctx
{
1458 GHashTable
*components
;
1461 struct bt_graph
*graph
;
1464 struct bt_config
*cfg
;
1468 bool stream_intersection_mode
;
1471 * Association of struct port_id -> struct trace_range.
1473 GHashTable
*intersections
;
1476 /* Returns a timestamp of the form "(-)s.ns" */
1478 char *s_from_ns(int64_t ns
)
1483 int64_t ts_sec_abs
, ts_nsec_abs
;
1484 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1485 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1487 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1488 is_negative
= false;
1489 ts_sec_abs
= ts_sec
;
1490 ts_nsec_abs
= ts_nsec
;
1491 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1492 is_negative
= false;
1493 ts_sec_abs
= ts_sec
- 1;
1494 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1495 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1497 ts_sec_abs
= ts_sec
;
1498 ts_nsec_abs
= -ts_nsec
;
1499 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1501 ts_sec_abs
= -(ts_sec
+ 1);
1502 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1503 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1505 ts_sec_abs
= -ts_sec
;
1506 ts_nsec_abs
= ts_nsec
;
1507 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1509 ts_sec_abs
= -ts_sec
;
1510 ts_nsec_abs
= -ts_nsec
;
1513 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1514 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1522 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1523 struct cmd_run_ctx
*ctx
, struct bt_component
*upstream_comp
,
1524 struct bt_port
*upstream_port
,
1525 struct bt_config_connection
*cfg_conn
)
1528 GQuark downstreamp_comp_name_quark
;
1529 struct bt_component
*downstream_comp
;
1530 int64_t downstream_port_count
;
1532 int64_t (*port_count_fn
)(struct bt_component
*);
1533 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
1534 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1535 bool insert_trimmer
= false;
1536 struct bt_value
*trimmer_params
= NULL
;
1537 char *intersection_begin
= NULL
;
1538 char *intersection_end
= NULL
;
1539 struct bt_component
*trimmer
= NULL
;
1540 struct bt_component_class
*trimmer_class
= NULL
;
1541 struct bt_port
*trimmer_input
= NULL
;
1542 struct bt_port
*trimmer_output
= NULL
;
1544 if (ctx
->intersections
&&
1545 bt_component_get_class_type(upstream_comp
) ==
1546 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1547 struct trace_range
*range
;
1548 struct port_id port_id
= {
1549 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1550 .port_name
= (char *) bt_port_get_name(upstream_port
)
1553 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1557 range
= (struct trace_range
*) g_hash_table_lookup(
1558 ctx
->intersections
, &port_id
);
1560 enum bt_value_status status
;
1562 intersection_begin
= s_from_ns(
1563 range
->intersection_range_begin_ns
);
1564 intersection_end
= s_from_ns(
1565 range
->intersection_range_end_ns
);
1566 if (!intersection_begin
|| !intersection_end
) {
1567 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1571 insert_trimmer
= true;
1572 trimmer_params
= bt_value_map_create();
1573 if (!trimmer_params
) {
1577 status
= bt_value_map_insert_string_entry(
1578 trimmer_params
, "begin", intersection_begin
);
1579 if (status
!= BT_VALUE_STATUS_OK
) {
1582 status
= bt_value_map_insert_string_entry(
1584 "end", intersection_end
);
1585 if (status
!= BT_VALUE_STATUS_OK
) {
1590 trimmer_class
= find_component_class("utils", "trimmer",
1591 BT_COMPONENT_CLASS_TYPE_FILTER
);
1592 if (!trimmer_class
) {
1597 BT_LOGI("Connecting upstream port to the next available downstream port: "
1598 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1599 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1600 upstream_port
, bt_port_get_name(upstream_port
),
1601 cfg_conn
->downstream_comp_name
->str
,
1602 cfg_conn
->arg
->str
);
1603 downstreamp_comp_name_quark
= g_quark_from_string(
1604 cfg_conn
->downstream_comp_name
->str
);
1605 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1606 downstream_comp
= g_hash_table_lookup(ctx
->components
,
1607 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1608 if (!downstream_comp
) {
1609 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1610 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1611 cfg_conn
->arg
->str
);
1612 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1613 cfg_conn
->arg
->str
);
1617 if (bt_component_is_filter(downstream_comp
)) {
1618 port_count_fn
= bt_component_filter_get_input_port_count
;
1619 port_by_index_fn
= bt_component_filter_get_input_port_by_index
;
1620 } else if (bt_component_is_sink(downstream_comp
)) {
1621 port_count_fn
= bt_component_sink_get_input_port_count
;
1622 port_by_index_fn
= bt_component_sink_get_input_port_by_index
;
1625 * Should never happen because the connections are
1626 * validated before we get here.
1628 BT_LOGF("Invalid connection: downstream component is a source: "
1629 "conn-arg=\"%s\"", cfg_conn
->arg
->str
);
1633 downstream_port_count
= port_count_fn(downstream_comp
);
1634 BT_ASSERT(downstream_port_count
>= 0);
1636 for (i
= 0; i
< downstream_port_count
; i
++) {
1637 struct bt_port
*downstream_port
=
1638 port_by_index_fn(downstream_comp
, i
);
1639 const char *upstream_port_name
;
1640 const char *downstream_port_name
;
1642 BT_ASSERT(downstream_port
);
1644 /* Skip port if it's already connected. */
1645 if (bt_port_is_connected(downstream_port
)) {
1646 bt_object_put_ref(downstream_port
);
1647 BT_LOGD("Skipping downstream port: already connected: "
1648 "port-addr=%p, port-name=\"%s\"",
1650 bt_port_get_name(downstream_port
));
1654 downstream_port_name
= bt_port_get_name(downstream_port
);
1655 BT_ASSERT(downstream_port_name
);
1656 upstream_port_name
= bt_port_get_name(upstream_port
);
1657 BT_ASSERT(upstream_port_name
);
1659 if (!bt_common_star_glob_match(
1660 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1661 downstream_port_name
, SIZE_MAX
)) {
1662 bt_object_put_ref(downstream_port
);
1666 if (insert_trimmer
) {
1668 * In order to insert the trimmer between the two
1669 * components that were being connected, we create
1670 * a connection configuration entry which describes
1671 * a connection from the trimmer's output to the
1672 * original input that was being connected.
1674 * Hence, the creation of the trimmer will cause the
1675 * graph "new port" listener to establish all downstream
1676 * connections as its output port is connected. We will
1677 * then establish the connection between the original
1678 * upstream source and the trimmer.
1680 char *trimmer_name
= NULL
;
1681 enum bt_graph_status graph_status
;
1683 ret
= asprintf(&trimmer_name
, "%s-%s",
1684 "stream-intersection-trimmer",
1685 upstream_port_name
);
1691 ctx
->connect_ports
= false;
1692 graph_status
= bt_graph_add_component(ctx
->graph
,
1693 trimmer_class
, trimmer_name
, trimmer_params
,
1696 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1702 bt_component_filter_get_input_port_by_index(
1704 if (!trimmer_input
) {
1708 bt_component_filter_get_output_port_by_index(
1710 if (!trimmer_output
) {
1715 * Replace the current downstream port by the trimmer's
1718 BT_OBJECT_MOVE_REF(downstream_port
, trimmer_input
);
1719 downstream_port_name
= bt_port_get_name(
1721 if (!downstream_port_name
) {
1726 /* We have a winner! */
1727 status
= bt_graph_connect_ports(ctx
->graph
,
1728 upstream_port
, downstream_port
, NULL
);
1729 BT_OBJECT_PUT_REF_AND_RESET(downstream_port
);
1731 case BT_GRAPH_STATUS_OK
:
1733 case BT_GRAPH_STATUS_CANCELED
:
1734 BT_LOGI_STR("Graph was canceled by user.");
1735 status
= BT_GRAPH_STATUS_OK
;
1737 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1738 BT_LOGE("A component refused a connection to one of its ports: "
1739 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1740 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1741 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1742 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1744 upstream_comp
, bt_component_get_name(upstream_comp
),
1745 upstream_port
, bt_port_get_name(upstream_port
),
1746 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1747 downstream_port
, downstream_port_name
,
1748 cfg_conn
->arg
->str
);
1750 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1751 bt_port_get_name(upstream_port
),
1752 downstream_port_name
,
1753 cfg_conn
->arg
->str
);
1756 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1757 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1758 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1759 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1760 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1762 upstream_comp
, bt_component_get_name(upstream_comp
),
1763 upstream_port
, bt_port_get_name(upstream_port
),
1764 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1765 downstream_port
, downstream_port_name
,
1766 cfg_conn
->arg
->str
);
1768 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1769 bt_port_get_name(upstream_port
),
1770 downstream_port_name
,
1771 cfg_conn
->arg
->str
);
1775 BT_LOGI("Connected component ports: "
1776 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1777 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1778 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1779 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1781 upstream_comp
, bt_component_get_name(upstream_comp
),
1782 upstream_port
, bt_port_get_name(upstream_port
),
1783 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1784 downstream_port
, downstream_port_name
,
1785 cfg_conn
->arg
->str
);
1787 if (insert_trimmer
) {
1789 * The first connection, from the source to the trimmer,
1790 * has been done. We now connect the trimmer to the
1791 * original downstream port.
1793 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1794 ctx
, trimmer
, trimmer_output
, cfg_conn
);
1798 ctx
->connect_ports
= true;
1802 * We found a matching downstream port: the search is
1808 /* No downstream port found */
1809 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1810 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1811 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1812 upstream_port
, bt_port_get_name(upstream_port
),
1813 cfg_conn
->downstream_comp_name
->str
,
1814 cfg_conn
->arg
->str
);
1816 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1817 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1823 free(intersection_begin
);
1824 free(intersection_end
);
1825 BT_OBJECT_PUT_REF_AND_RESET(trimmer_params
);
1826 BT_OBJECT_PUT_REF_AND_RESET(trimmer_class
);
1827 BT_OBJECT_PUT_REF_AND_RESET(trimmer
);
1828 BT_OBJECT_PUT_REF_AND_RESET(trimmer_input
);
1829 BT_OBJECT_PUT_REF_AND_RESET(trimmer_output
);
1834 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1835 struct bt_port
*upstream_port
)
1838 const char *upstream_port_name
;
1839 const char *upstream_comp_name
;
1840 struct bt_component
*upstream_comp
= NULL
;
1844 BT_ASSERT(upstream_port
);
1845 upstream_port_name
= bt_port_get_name(upstream_port
);
1846 BT_ASSERT(upstream_port_name
);
1847 upstream_comp
= bt_port_get_component(upstream_port
);
1848 if (!upstream_comp
) {
1849 BT_LOGW("Upstream port to connect is not part of a component: "
1850 "port-addr=%p, port-name=\"%s\"",
1851 upstream_port
, upstream_port_name
);
1856 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1857 BT_ASSERT(upstream_comp_name
);
1858 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1859 "port-addr=%p, port-name=\"%s\"",
1860 upstream_comp
, upstream_comp_name
,
1861 upstream_port
, upstream_port_name
);
1863 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1864 struct bt_config_connection
*cfg_conn
=
1866 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1868 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1869 upstream_comp_name
)) {
1873 if (!bt_common_star_glob_match(
1874 cfg_conn
->upstream_port_glob
->str
,
1875 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1879 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1880 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1882 BT_LOGE("Cannot connect upstream port: "
1883 "port-addr=%p, port-name=\"%s\"",
1885 upstream_port_name
);
1887 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1890 cfg_conn
->arg
->str
);
1896 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1897 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1898 upstream_port_name
);
1900 "Cannot create connection: upstream port `%s` does not match any connection\n",
1901 upstream_port_name
);
1907 bt_object_put_ref(upstream_comp
);
1912 void graph_port_added_listener(struct bt_port
*port
, void *data
)
1914 struct bt_component
*comp
= NULL
;
1915 struct cmd_run_ctx
*ctx
= data
;
1917 comp
= bt_port_get_component(port
);
1918 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1919 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1920 comp
, comp
? bt_component_get_name(comp
) : "",
1921 port
, bt_port_get_name(port
));
1923 if (!ctx
->connect_ports
) {
1928 BT_LOGW_STR("Port has no component.");
1932 if (bt_port_is_connected(port
)) {
1933 BT_LOGW_STR("Port is already connected.");
1937 if (!bt_port_is_output(port
)) {
1938 BT_LOGI_STR("Skipping input port.");
1942 if (cmd_run_ctx_connect_upstream_port(ctx
, port
)) {
1943 BT_LOGF_STR("Cannot connect upstream port.");
1944 fprintf(stderr
, "Added port could not be connected: aborting\n");
1949 bt_object_put_ref(comp
);
1954 void graph_port_removed_listener(struct bt_component
*component
,
1955 struct bt_port
*port
, void *data
)
1957 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1958 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1959 component
, bt_component_get_name(component
),
1960 port
, bt_port_get_name(port
));
1964 void graph_ports_connected_listener(struct bt_port
*upstream_port
,
1965 struct bt_port
*downstream_port
, void *data
)
1967 struct bt_component
*upstream_comp
= bt_port_get_component(upstream_port
);
1968 struct bt_component
*downstream_comp
= bt_port_get_component(downstream_port
);
1970 BT_ASSERT(upstream_comp
);
1971 BT_ASSERT(downstream_comp
);
1972 BT_LOGI("Graph's component ports connected: "
1973 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1974 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1975 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1976 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1977 upstream_comp
, bt_component_get_name(upstream_comp
),
1978 upstream_port
, bt_port_get_name(upstream_port
),
1979 downstream_comp
, bt_component_get_name(downstream_comp
),
1980 downstream_port
, bt_port_get_name(downstream_port
));
1981 bt_object_put_ref(upstream_comp
);
1982 bt_object_put_ref(downstream_comp
);
1986 void graph_ports_disconnected_listener(
1987 struct bt_component
*upstream_component
,
1988 struct bt_component
*downstream_component
,
1989 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
1992 BT_LOGI("Graph's component ports disconnected: "
1993 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1994 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1995 upstream_port
, bt_port_get_name(upstream_port
),
1996 downstream_port
, bt_port_get_name(downstream_port
));
2000 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
2006 if (ctx
->components
) {
2007 g_hash_table_destroy(ctx
->components
);
2008 ctx
->components
= NULL
;
2011 if (ctx
->intersections
) {
2012 g_hash_table_destroy(ctx
->intersections
);
2013 ctx
->intersections
= NULL
;
2016 BT_OBJECT_PUT_REF_AND_RESET(ctx
->graph
);
2022 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2027 ctx
->connect_ports
= false;
2028 ctx
->components
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
2029 NULL
, bt_object_put_ref
);
2030 if (!ctx
->components
) {
2034 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2035 ctx
->stream_intersection_mode
= true;
2036 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2037 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2038 if (!ctx
->intersections
) {
2043 ctx
->graph
= bt_graph_create();
2048 the_graph
= ctx
->graph
;
2049 ret
= bt_graph_add_port_added_listener(ctx
->graph
,
2050 graph_port_added_listener
, NULL
, ctx
);
2052 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2056 ret
= bt_graph_add_port_removed_listener(ctx
->graph
,
2057 graph_port_removed_listener
, NULL
, ctx
);
2059 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
2063 ret
= bt_graph_add_ports_connected_listener(ctx
->graph
,
2064 graph_ports_connected_listener
, NULL
, ctx
);
2066 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
2070 ret
= bt_graph_add_ports_disconnected_listener(ctx
->graph
,
2071 graph_ports_disconnected_listener
, NULL
, ctx
);
2073 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
2080 cmd_run_ctx_destroy(ctx
);
2088 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2089 struct bt_config_component
*cfg_comp
,
2090 struct bt_component_class
*comp_cls
)
2094 int64_t trace_count
;
2095 enum bt_value_status value_status
;
2096 const char *path
= NULL
;
2097 struct bt_value
*component_path_value
= NULL
;
2098 struct bt_value
*query_params
= NULL
;
2099 struct bt_value
*query_result
= NULL
;
2100 struct bt_value
*trace_info
= NULL
;
2101 struct bt_value
*intersection_range
= NULL
;
2102 struct bt_value
*intersection_begin
= NULL
;
2103 struct bt_value
*intersection_end
= NULL
;
2104 struct bt_value
*stream_path_value
= NULL
;
2105 struct bt_value
*stream_paths
= NULL
;
2106 struct bt_value
*stream_infos
= NULL
;
2107 struct bt_value
*stream_info
= NULL
;
2108 struct port_id
*port_id
= NULL
;
2109 struct trace_range
*trace_range
= NULL
;
2110 const char *fail_reason
= NULL
;
2112 component_path_value
= bt_value_map_borrow_entry_value(cfg_comp
->params
,
2114 if (component_path_value
&& !bt_value_is_string(component_path_value
)) {
2115 BT_LOGD("Cannot get path parameter: component-name=%s",
2116 cfg_comp
->instance_name
->str
);
2121 value_status
= bt_value_string_get(component_path_value
, &path
);
2122 if (value_status
!= BT_VALUE_STATUS_OK
) {
2123 BT_LOGD("Cannot get path string value: component-name=%s",
2124 cfg_comp
->instance_name
->str
);
2129 query_params
= bt_value_map_create();
2130 if (!query_params
) {
2131 BT_LOGE_STR("Cannot create query parameters.");
2136 value_status
= bt_value_map_insert_entry(query_params
, "path",
2137 component_path_value
);
2138 if (value_status
!= BT_VALUE_STATUS_OK
) {
2139 BT_LOGE_STR("Cannot insert path parameter in query parameter map.");
2144 ret
= query(comp_cls
, "trace-info", query_params
, &query_result
,
2147 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2148 "comp-class-name=\"%s\"", fail_reason
,
2149 bt_component_class_get_name(comp_cls
));
2154 BT_ASSERT(query_result
);
2156 if (!bt_value_is_array(query_result
)) {
2157 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2158 "component-class-name=%s",
2159 bt_component_class_get_name(comp_cls
));
2164 trace_count
= bt_value_array_get_size(query_result
);
2165 if (trace_count
< 0) {
2170 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2172 uint64_t stream_idx
;
2173 int64_t stream_count
;
2175 trace_info
= bt_value_array_borrow_element_by_index(
2176 query_result
, trace_idx
);
2177 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2179 BT_LOGD_STR("Cannot retrieve trace from query result.");
2183 intersection_range
= bt_value_map_borrow_entry_value(trace_info
,
2184 "intersection-range-ns");
2185 if (!intersection_range
) {
2187 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2191 intersection_begin
= bt_value_map_borrow_entry_value(
2192 intersection_range
, "begin");
2193 if (!intersection_begin
) {
2195 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2199 intersection_end
= bt_value_map_borrow_entry_value(
2200 intersection_range
, "end");
2201 if (!intersection_end
) {
2203 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2207 value_status
= bt_value_integer_get(intersection_begin
, &begin
);
2208 if (value_status
!= BT_VALUE_STATUS_OK
) {
2210 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'begin\' field from query result.");
2214 value_status
= bt_value_integer_get(intersection_end
, &end
);
2215 if (value_status
!= BT_VALUE_STATUS_OK
) {
2217 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'end\' field from query result.");
2221 if (begin
< 0 || end
< 0 || end
< begin
) {
2222 BT_LOGW("Invalid trace stream intersection values: "
2223 "intersection-range-ns:begin=%" PRId64
2224 ", intersection-range-ns:end=%" PRId64
,
2230 stream_infos
= bt_value_map_borrow_entry_value(trace_info
,
2232 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2234 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2238 stream_count
= bt_value_array_get_size(stream_infos
);
2239 if (stream_count
< 0) {
2247 * The first path of a stream's "paths" is currently used to
2248 * associate streams/ports to a given trace intersection.
2250 * This is a fragile hack as it relies on the port names
2251 * being set to the various streams path.
2253 * A stream name should be introduced as part of the trace-info
2256 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2257 const char *stream_path
;
2259 port_id
= g_new0(struct port_id
, 1);
2262 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2265 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2266 if (!port_id
->instance_name
) {
2268 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2272 trace_range
= g_new0(struct trace_range
, 1);
2275 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2278 trace_range
->intersection_range_begin_ns
= begin
;
2279 trace_range
->intersection_range_end_ns
= end
;
2281 stream_info
= bt_value_array_borrow_element_by_index(
2282 stream_infos
, stream_idx
);
2283 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2285 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2289 stream_paths
= bt_value_map_borrow_entry_value(
2290 stream_info
, "paths");
2291 if (!stream_paths
|| !bt_value_is_array(stream_paths
)) {
2293 BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
2298 bt_value_array_borrow_element_by_index(
2300 if (!stream_path_value
||
2301 !bt_value_is_string(stream_path_value
)) {
2303 BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
2307 value_status
= bt_value_string_get(stream_path_value
,
2309 if (value_status
!= BT_VALUE_STATUS_OK
) {
2314 port_id
->port_name
= strdup(stream_path
);
2315 if (!port_id
->port_name
) {
2317 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2321 BT_LOGD("Inserting stream intersection ");
2323 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2333 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2334 bt_common_color_bold(),
2335 bt_common_color_fg_yellow(),
2336 path
? path
: "(unknown)",
2337 bt_common_color_reset());
2339 bt_object_put_ref(query_params
);
2340 bt_object_put_ref(query_result
);
2342 g_free(trace_range
);
2347 int cmd_run_ctx_create_components_from_config_components(
2348 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2351 struct bt_component_class
*comp_cls
= NULL
;
2352 struct bt_component
*comp
= NULL
;
2355 for (i
= 0; i
< cfg_components
->len
; i
++) {
2356 struct bt_config_component
*cfg_comp
=
2357 g_ptr_array_index(cfg_components
, i
);
2360 comp_cls
= find_component_class(cfg_comp
->plugin_name
->str
,
2361 cfg_comp
->comp_cls_name
->str
, cfg_comp
->type
);
2363 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2364 "comp-cls-name=\"%s\", comp-cls-type=%d",
2365 cfg_comp
->plugin_name
->str
,
2366 cfg_comp
->comp_cls_name
->str
,
2368 fprintf(stderr
, "%s%sCannot find component class %s",
2369 bt_common_color_bold(),
2370 bt_common_color_fg_red(),
2371 bt_common_color_reset());
2372 print_plugin_comp_cls_opt(stderr
,
2373 cfg_comp
->plugin_name
->str
,
2374 cfg_comp
->comp_cls_name
->str
,
2376 fprintf(stderr
, "\n");
2380 ret
= bt_graph_add_component(ctx
->graph
, comp_cls
,
2381 cfg_comp
->instance_name
->str
, cfg_comp
->params
, &comp
);
2383 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2384 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2386 cfg_comp
->plugin_name
->str
,
2387 cfg_comp
->comp_cls_name
->str
,
2388 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2389 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2390 bt_common_color_bold(),
2391 bt_common_color_fg_red(),
2392 cfg_comp
->instance_name
->str
,
2393 bt_common_color_reset());
2397 if (ctx
->stream_intersection_mode
&&
2398 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2399 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2405 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2406 comp
, cfg_comp
->instance_name
->str
);
2407 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2408 BT_ASSERT(quark
> 0);
2409 g_hash_table_insert(ctx
->components
,
2410 GUINT_TO_POINTER(quark
), comp
);
2412 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2421 bt_object_put_ref(comp
);
2422 bt_object_put_ref(comp_cls
);
2427 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2432 * Make sure that, during this phase, our graph's "port added"
2433 * listener does not connect ports while we are creating the
2434 * components because we have a special, initial phase for
2437 ctx
->connect_ports
= false;
2439 ret
= cmd_run_ctx_create_components_from_config_components(
2440 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2446 ret
= cmd_run_ctx_create_components_from_config_components(
2447 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2453 ret
= cmd_run_ctx_create_components_from_config_components(
2454 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2465 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2466 struct bt_component
*comp
,
2467 int64_t (*port_count_fn
)(struct bt_component
*),
2468 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t))
2474 count
= port_count_fn(comp
);
2475 BT_ASSERT(count
>= 0);
2477 for (i
= 0; i
< count
; i
++) {
2478 struct bt_port
*upstream_port
= port_by_index_fn(comp
, i
);
2480 BT_ASSERT(upstream_port
);
2481 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2482 bt_object_put_ref(upstream_port
);
2493 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2496 GHashTableIter iter
;
2497 gpointer g_name_quark
, g_comp
;
2499 ctx
->connect_ports
= true;
2500 g_hash_table_iter_init(&iter
, ctx
->components
);
2502 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2503 int64_t (*port_count_fn
)(struct bt_component
*);
2504 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
2506 if (bt_component_is_source(g_comp
)) {
2508 bt_component_source_get_output_port_count
;
2510 bt_component_source_get_output_port_by_index
;
2511 } else if (bt_component_is_filter(g_comp
)) {
2513 bt_component_filter_get_output_port_count
;
2515 bt_component_filter_get_output_port_by_index
;
2520 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
2521 g_comp
, port_count_fn
, port_by_index_fn
);
2532 const char *bt_graph_status_str(enum bt_graph_status status
)
2535 case BT_GRAPH_STATUS_CANCELED
:
2536 return "BT_GRAPH_STATUS_CANCELED";
2537 case BT_GRAPH_STATUS_AGAIN
:
2538 return "BT_GRAPH_STATUS_AGAIN";
2539 case BT_GRAPH_STATUS_END
:
2540 return "BT_GRAPH_STATUS_END";
2541 case BT_GRAPH_STATUS_OK
:
2542 return "BT_GRAPH_STATUS_OK";
2543 case BT_GRAPH_STATUS_INVALID
:
2544 return "BT_GRAPH_STATUS_INVALID";
2545 case BT_GRAPH_STATUS_NO_SINK
:
2546 return "BT_GRAPH_STATUS_NO_SINK";
2547 case BT_GRAPH_STATUS_ERROR
:
2548 return "BT_GRAPH_STATUS_ERROR";
2555 int cmd_run(struct bt_config
*cfg
)
2558 struct cmd_run_ctx ctx
= { 0 };
2560 /* Initialize the command's context and the graph object */
2561 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2562 BT_LOGE_STR("Cannot initialize the command's context.");
2563 fprintf(stderr
, "Cannot initialize the command's context\n");
2568 BT_LOGI_STR("Canceled by user before creating components.");
2572 BT_LOGI_STR("Creating components.");
2574 /* Create the requested component instances */
2575 if (cmd_run_ctx_create_components(&ctx
)) {
2576 BT_LOGE_STR("Cannot create components.");
2577 fprintf(stderr
, "Cannot create components\n");
2582 BT_LOGI_STR("Canceled by user before connecting components.");
2586 BT_LOGI_STR("Connecting components.");
2588 /* Connect the initially visible component ports */
2589 if (cmd_run_ctx_connect_ports(&ctx
)) {
2590 BT_LOGE_STR("Cannot connect initial component ports.");
2591 fprintf(stderr
, "Cannot connect initial component ports\n");
2596 BT_LOGI_STR("Canceled by user before running the graph.");
2600 BT_LOGI_STR("Running the graph.");
2604 enum bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2607 * Reset console in case something messed with console
2608 * codes during the graph's execution.
2610 printf("%s", bt_common_color_reset());
2612 fprintf(stderr
, "%s", bt_common_color_reset());
2613 BT_LOGV("bt_graph_run() returned: status=%s",
2614 bt_graph_status_str(graph_status
));
2616 switch (graph_status
) {
2617 case BT_GRAPH_STATUS_OK
:
2619 case BT_GRAPH_STATUS_CANCELED
:
2620 BT_LOGI_STR("Graph was canceled by user.");
2622 case BT_GRAPH_STATUS_AGAIN
:
2623 if (bt_graph_is_canceled(ctx
.graph
)) {
2624 BT_LOGI_STR("Graph was canceled by user.");
2628 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2629 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2631 cfg
->cmd_data
.run
.retry_duration_us
);
2633 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2634 if (bt_graph_is_canceled(ctx
.graph
)) {
2635 BT_LOGI_STR("Graph was canceled by user.");
2641 case BT_COMPONENT_STATUS_END
:
2644 BT_LOGE_STR("Graph failed to complete successfully");
2645 fprintf(stderr
, "Graph failed to complete successfully\n");
2658 cmd_run_ctx_destroy(&ctx
);
2663 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2665 const char *env_clash
;
2667 if (!cfg
->command_name
) {
2671 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2672 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2676 if (g_file_test(cfg
->command_name
,
2677 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2678 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2680 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2682 fprintf(stderr
, "\n");
2683 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
2689 void init_log_level(void)
2691 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2695 void set_auto_log_levels(struct bt_config
*cfg
)
2697 const char **env_var_name
;
2700 * Override the configuration's default log level if
2701 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2702 * are found for backward compatibility with legacy Babetrace 1.
2704 if (getenv("BABELTRACE_DEBUG") &&
2705 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2706 cfg
->log_level
= 'V';
2707 } else if (getenv("BABELTRACE_VERBOSE") &&
2708 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2709 cfg
->log_level
= 'I';
2713 * Set log levels according to --debug or --verbose. For
2714 * backward compatibility, --debug is more verbose than
2717 * --verbose: INFO log level
2718 * --debug: VERBOSE log level (includes DEBUG, which is
2719 * is less verbose than VERBOSE in the internal
2720 * logging framework)
2722 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2724 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2725 } else if (cfg
->debug
) {
2726 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2729 * Set library's default log level if not
2730 * explicitly specified.
2732 switch (cfg
->log_level
) {
2734 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE
);
2737 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2740 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
2743 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2746 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN
);
2749 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR
);
2752 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL
);
2760 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2762 bt_cli_log_level
= BT_LOG_INFO
;
2763 } else if (cfg
->debug
) {
2764 bt_cli_log_level
= BT_LOG_VERBOSE
;
2767 * Set CLI's default log level if not explicitly
2770 switch (cfg
->log_level
) {
2772 bt_cli_log_level
= BT_LOG_NONE
;
2775 bt_cli_log_level
= BT_LOG_VERBOSE
;
2778 bt_cli_log_level
= BT_LOG_DEBUG
;
2781 bt_cli_log_level
= BT_LOG_INFO
;
2784 bt_cli_log_level
= BT_LOG_WARN
;
2787 bt_cli_log_level
= BT_LOG_ERROR
;
2790 bt_cli_log_level
= BT_LOG_FATAL
;
2798 env_var_name
= log_level_env_var_names
;
2800 while (*env_var_name
) {
2801 if (!getenv(*env_var_name
)) {
2803 g_setenv(*env_var_name
, "I", 1);
2804 } else if (cfg
->debug
) {
2805 g_setenv(*env_var_name
, "V", 1);
2807 char val
[2] = { 0 };
2810 * Set module's default log level if not
2811 * explicitly specified.
2813 val
[0] = cfg
->log_level
;
2814 g_setenv(*env_var_name
, val
, 1);
2822 int main(int argc
, const char **argv
)
2826 struct bt_config
*cfg
;
2829 set_signal_handler();
2831 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2834 /* Quit without errors; typically usage/version */
2836 BT_LOGI_STR("Quitting without errors.");
2841 BT_LOGE("Command-line error: retcode=%d", retcode
);
2846 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2847 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2852 set_auto_log_levels(cfg
);
2855 if (cfg
->command_needs_plugins
) {
2856 ret
= load_all_plugins(cfg
->plugin_paths
);
2858 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2864 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2865 cfg
->command
, cfg
->command_name
);
2867 switch (cfg
->command
) {
2868 case BT_CONFIG_COMMAND_RUN
:
2871 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2872 ret
= cmd_list_plugins(cfg
);
2874 case BT_CONFIG_COMMAND_HELP
:
2875 ret
= cmd_help(cfg
);
2877 case BT_CONFIG_COMMAND_QUERY
:
2878 ret
= cmd_query(cfg
);
2880 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2881 ret
= cmd_print_ctf_metadata(cfg
);
2883 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2884 ret
= cmd_print_lttng_live_sessions(cfg
);
2887 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2891 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2892 cfg
->command
, cfg
->command_name
, ret
);
2893 warn_command_name_and_directory_clash(cfg
);
2894 retcode
= ret
? 1 : 0;
2897 BT_OBJECT_PUT_REF_AND_RESET(cfg
);