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>
43 #include "babeltrace-cfg.h"
44 #include "babeltrace-cfg-cli-args.h"
45 #include "babeltrace-cfg-cli-args-default.h"
47 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
48 #define ENV_BABELTRACE_CLI_LOG_LEVEL "BABELTRACE_CLI_LOG_LEVEL"
49 #define NSEC_PER_SEC 1000000000LL
52 * Known environment variable names for the log levels of the project's
55 static const char* log_level_env_var_names
[] = {
56 "BABELTRACE_COMMON_LOG_LEVEL",
57 "BABELTRACE_COMPAT_LOG_LEVEL",
58 "BABELTRACE_PLUGIN_CTF_BTR_LOG_LEVEL",
59 "BABELTRACE_SINK_CTF_FS_LOG_LEVEL",
60 "BABELTRACE_SRC_CTF_FS_LOG_LEVEL",
61 "BABELTRACE_SRC_CTF_LTTNG_LIVE_LOG_LEVEL",
62 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
63 "BABELTRACE_PLUGIN_CTF_NOTIF_ITER_LOG_LEVEL",
64 "BABELTRACE_PLUGIN_CTFCOPYTRACE_LIB_LOG_LEVEL",
65 "BABELTRACE_FLT_LTTNG_UTILS_DEBUG_INFO_LOG_LEVEL",
66 "BABELTRACE_SRC_TEXT_DMESG_LOG_LEVEL",
67 "BABELTRACE_SINK_TEXT_PRETTY_LOG_LEVEL",
68 "BABELTRACE_FLT_UTILS_MUXER_LOG_LEVEL",
69 "BABELTRACE_FLT_UTILS_TRIMMER_LOG_LEVEL",
70 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
71 "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
75 /* Application's processing graph (weak) */
76 static struct bt_graph
*the_graph
;
77 static struct bt_query_executor
*the_query_executor
;
78 static bool canceled
= false;
80 GPtrArray
*loaded_plugins
;
86 BOOL WINAPI
signal_handler(DWORD signal
) {
88 bt_graph_cancel(the_graph
);
97 void set_signal_handler(void)
99 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
100 BT_LOGE("Failed to set the ctrl+c handler.");
103 #else /* __MINGW32__ */
105 void signal_handler(int signum
)
107 if (signum
!= SIGINT
) {
112 bt_graph_cancel(the_graph
);
115 if (the_query_executor
) {
116 bt_query_executor_cancel(the_query_executor
);
123 void set_signal_handler(void)
125 struct sigaction new_action
, old_action
;
127 new_action
.sa_handler
= signal_handler
;
128 sigemptyset(&new_action
.sa_mask
);
129 new_action
.sa_flags
= 0;
130 sigaction(SIGINT
, NULL
, &old_action
);
132 if (old_action
.sa_handler
!= SIG_IGN
) {
133 sigaction(SIGINT
, &new_action
, NULL
);
136 #endif /* __MINGW32__ */
139 void init_static_data(void)
141 loaded_plugins
= g_ptr_array_new_with_free_func(bt_object_put_ref
);
145 void fini_static_data(void)
147 g_ptr_array_free(loaded_plugins
, TRUE
);
151 int create_the_query_executor(void)
155 the_query_executor
= bt_query_executor_create();
156 if (!the_query_executor
) {
157 BT_LOGE_STR("Cannot create a query executor.");
165 void destroy_the_query_executor(void)
167 BT_OBJECT_PUT_REF_AND_RESET(the_query_executor
);
171 int query(struct bt_component_class
*comp_cls
, const char *obj
,
172 struct bt_value
*params
, struct bt_value
**user_result
,
173 const char **fail_reason
)
175 struct bt_value
*result
= NULL
;
176 enum bt_query_status status
;
177 *fail_reason
= "unknown error";
180 BT_ASSERT(fail_reason
);
181 BT_ASSERT(user_result
);
182 ret
= create_the_query_executor();
184 /* create_the_query_executor() logs errors */
189 BT_LOGI("Canceled by user before executing the query: "
190 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
191 "query-obj=\"%s\"", comp_cls
,
192 bt_component_class_get_name(comp_cls
), obj
);
193 *fail_reason
= "canceled by user";
198 status
= bt_query_executor_query(the_query_executor
, comp_cls
,
199 obj
, params
, &result
);
201 case BT_QUERY_STATUS_OK
:
203 case BT_QUERY_STATUS_AGAIN
:
205 const uint64_t sleep_time_us
= 100000;
207 /* Wait 100 ms and retry */
208 BT_LOGV("Got BT_QUERY_STATUS_AGAIN: sleeping: "
209 "time-us=%" PRIu64
, sleep_time_us
);
211 if (usleep(sleep_time_us
)) {
212 if (bt_query_executor_is_canceled(the_query_executor
)) {
213 BT_LOGI("Query was canceled by user: "
214 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
215 "query-obj=\"%s\"", comp_cls
,
216 bt_component_class_get_name(comp_cls
),
218 *fail_reason
= "canceled by user";
225 case BT_QUERY_STATUS_EXECUTOR_CANCELED
:
226 *fail_reason
= "canceled by user";
228 case BT_QUERY_STATUS_ERROR
:
229 case BT_QUERY_STATUS_INVALID
:
231 case BT_QUERY_STATUS_INVALID_OBJECT
:
232 *fail_reason
= "invalid or unknown query object";
234 case BT_QUERY_STATUS_INVALID_PARAMS
:
235 *fail_reason
= "invalid query parameters";
237 case BT_QUERY_STATUS_NOMEM
:
238 *fail_reason
= "not enough memory";
241 BT_LOGF("Unknown query status: status=%d", status
);
247 *user_result
= result
;
255 destroy_the_query_executor();
256 bt_object_put_ref(result
);
261 struct bt_plugin
*find_plugin(const char *name
)
264 struct bt_plugin
*plugin
= NULL
;
267 BT_LOGD("Finding plugin: name=\"%s\"", name
);
269 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
270 plugin
= g_ptr_array_index(loaded_plugins
, i
);
272 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
279 if (BT_LOG_ON_DEBUG
) {
281 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
283 BT_LOGD("Cannot find plugin.");
287 return bt_object_get_ref(plugin
);
291 struct bt_component_class
*find_component_class(const char *plugin_name
,
292 const char *comp_class_name
,
293 enum bt_component_class_type comp_class_type
)
295 struct bt_component_class
*comp_class
= NULL
;
296 struct bt_plugin
*plugin
;
298 BT_LOGD("Finding component class: plugin-name=\"%s\", "
299 "comp-cls-name=\"%s\", comp-cls-type=%d",
300 plugin_name
, comp_class_name
, comp_class_type
);
302 plugin
= find_plugin(plugin_name
);
308 comp_class
= bt_plugin_get_component_class_by_name_and_type(plugin
,
309 comp_class_name
, comp_class_type
);
310 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
313 if (BT_LOG_ON_DEBUG
) {
315 BT_LOGD("Found component class: comp-cls-addr=%p",
318 BT_LOGD("Cannot find component class.");
326 void print_indent(FILE *fp
, size_t indent
)
330 for (i
= 0; i
< indent
; i
++) {
336 const char *component_type_str(enum bt_component_class_type type
)
339 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
341 case BT_COMPONENT_CLASS_TYPE_SINK
:
343 case BT_COMPONENT_CLASS_TYPE_FILTER
:
345 case BT_COMPONENT_CLASS_TYPE_UNKNOWN
:
352 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
353 const char *comp_cls_name
, enum bt_component_class_type type
)
355 GString
*shell_plugin_name
= NULL
;
356 GString
*shell_comp_cls_name
= NULL
;
358 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
359 if (!shell_plugin_name
) {
363 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
364 if (!shell_comp_cls_name
) {
368 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
369 bt_common_color_bold(),
370 bt_common_color_fg_cyan(),
371 component_type_str(type
),
372 bt_common_color_fg_default(),
373 bt_common_color_fg_blue(),
374 shell_plugin_name
->str
,
375 bt_common_color_fg_default(),
376 bt_common_color_fg_yellow(),
377 shell_comp_cls_name
->str
,
378 bt_common_color_reset());
381 if (shell_plugin_name
) {
382 g_string_free(shell_plugin_name
, TRUE
);
385 if (shell_comp_cls_name
) {
386 g_string_free(shell_comp_cls_name
, TRUE
);
391 void print_value(FILE *, struct bt_value
*, size_t);
394 void print_value_rec(FILE *, struct bt_value
*, size_t);
396 struct print_map_value_data
{
402 bt_bool
print_map_value(const char *key
, struct bt_value
*object
, void *data
)
404 struct print_map_value_data
*print_map_value_data
= data
;
406 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
407 fprintf(print_map_value_data
->fp
, "%s: ", key
);
410 if (bt_value_is_array(object
) &&
411 bt_value_array_is_empty(object
)) {
412 fprintf(print_map_value_data
->fp
, "[ ]\n");
416 if (bt_value_is_map(object
) &&
417 bt_value_map_is_empty(object
)) {
418 fprintf(print_map_value_data
->fp
, "{ }\n");
422 if (bt_value_is_array(object
) ||
423 bt_value_is_map(object
)) {
424 fprintf(print_map_value_data
->fp
, "\n");
427 print_value_rec(print_map_value_data
->fp
, object
,
428 print_map_value_data
->indent
+ 2);
433 void print_value_rec(FILE *fp
, struct bt_value
*value
, size_t indent
)
441 enum bt_value_status status
;
447 switch (bt_value_get_type(value
)) {
448 case BT_VALUE_TYPE_NULL
:
449 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
450 bt_common_color_reset());
452 case BT_VALUE_TYPE_BOOL
:
453 status
= bt_value_bool_get(value
, &bool_val
);
454 if (status
!= BT_VALUE_STATUS_OK
) {
457 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
458 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
459 bt_common_color_reset());
461 case BT_VALUE_TYPE_INTEGER
:
462 status
= bt_value_integer_get(value
, &int_val
);
463 if (status
!= BT_VALUE_STATUS_OK
) {
466 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
467 bt_common_color_fg_red(), int_val
,
468 bt_common_color_reset());
470 case BT_VALUE_TYPE_REAL
:
471 status
= bt_value_real_get(value
, &dbl_val
);
472 if (status
!= BT_VALUE_STATUS_OK
) {
475 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
476 bt_common_color_fg_red(), dbl_val
,
477 bt_common_color_reset());
479 case BT_VALUE_TYPE_STRING
:
480 status
= bt_value_string_get(value
, &str_val
);
481 if (status
!= BT_VALUE_STATUS_OK
) {
484 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
485 bt_common_color_fg_green(), str_val
,
486 bt_common_color_reset());
488 case BT_VALUE_TYPE_ARRAY
:
489 size
= bt_value_array_get_size(value
);
495 print_indent(fp
, indent
);
496 fprintf(fp
, "[ ]\n");
500 for (i
= 0; i
< size
; i
++) {
501 struct bt_value
*element
=
502 bt_value_array_borrow_element_by_index(
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);
531 case BT_VALUE_TYPE_MAP
:
533 struct print_map_value_data data
= {
538 if (bt_value_map_is_empty(value
)) {
539 print_indent(fp
, indent
);
540 fprintf(fp
, "{ }\n");
544 bt_value_map_foreach_entry(value
, print_map_value
, &data
);
553 BT_LOGE("Error printing value of type %s.",
554 bt_common_value_type_string(bt_value_get_type(value
)));
558 void print_value(FILE *fp
, struct bt_value
*value
, size_t indent
)
560 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
561 print_indent(fp
, indent
);
564 print_value_rec(fp
, value
, indent
);
568 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
570 fprintf(stderr
, " ");
571 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
572 bt_config_component
->comp_cls_name
->str
,
573 bt_config_component
->type
);
574 fprintf(stderr
, ":\n");
576 if (bt_config_component
->instance_name
->len
> 0) {
577 fprintf(stderr
, " Name: %s\n",
578 bt_config_component
->instance_name
->str
);
581 fprintf(stderr
, " Parameters:\n");
583 bt_value_borrow_from_private(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(bt_value_borrow_from_private(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(bt_value_borrow_from_private(cfg
->plugin_paths
));
646 void print_cfg_help(struct bt_config
*cfg
)
648 print_plugin_paths(bt_value_borrow_from_private(cfg
->plugin_paths
));
652 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
654 print_plugin_paths(bt_value_borrow_from_private(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(bt_value_borrow_from_private(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(bt_value_borrow_from_private(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 bt_value_borrow_from_private(cfg
->cmd_data
.query
.cfg_component
->params
),
924 &results
, &fail_reason
);
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
, bt_value_borrow_from_private(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_private_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_private_value_map_create();
1182 ret
= bt_private_value_map_insert_string_entry(params
, "url",
1183 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1188 ret
= query(comp_cls
, "sessions", bt_value_borrow_from_private(params
),
1189 &results
, &fail_reason
);
1196 if (!bt_value_is_array(results
)) {
1197 BT_LOGE_STR("Expecting an array for sessions query.");
1198 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1199 bt_common_color_bold(),
1200 bt_common_color_fg_red(),
1201 bt_common_color_reset());
1205 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1207 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1211 BT_LOGE_ERRNO("Cannot open file for writing",
1213 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1218 array_size
= bt_value_array_get_size(results
);
1219 for (i
= 0; i
< array_size
; i
++) {
1220 const char *url_text
;
1221 int64_t timer_us
, streams
, clients
;
1223 map
= bt_value_array_borrow_element_by_index(results
, i
);
1225 BT_LOGE_STR("Unexpected empty array entry.");
1228 if (!bt_value_is_map(map
)) {
1229 BT_LOGE_STR("Unexpected entry type.");
1233 v
= bt_value_map_borrow_entry_value(map
, "url");
1235 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1238 ret
= bt_value_string_get(v
, &url_text
);
1239 BT_ASSERT(ret
== 0);
1240 fprintf(out_stream
, "%s", url_text
);
1241 v
= bt_value_map_borrow_entry_value(map
, "timer-us");
1243 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1246 ret
= bt_value_integer_get(v
, &timer_us
);
1247 BT_ASSERT(ret
== 0);
1248 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1249 v
= bt_value_map_borrow_entry_value(map
, "stream-count");
1251 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1254 ret
= bt_value_integer_get(v
, &streams
);
1255 BT_ASSERT(ret
== 0);
1256 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1257 v
= bt_value_map_borrow_entry_value(map
, "client-count");
1259 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1262 ret
= bt_value_integer_get(v
, &clients
);
1263 BT_ASSERT(ret
== 0);
1264 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1270 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1271 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1272 bt_common_color_bold(),
1273 bt_common_color_fg_red(),
1275 bt_common_color_reset());
1281 bt_object_put_ref(results
);
1282 bt_object_put_ref(params
);
1283 bt_object_put_ref(comp_cls
);
1285 if (out_stream
&& out_stream
!= stdout
) {
1286 int fclose_ret
= fclose(out_stream
);
1289 BT_LOGE_ERRNO("Cannot close file stream",
1291 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1299 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1302 struct bt_component_class
*comp_cls
= NULL
;
1303 struct bt_value
*results
= NULL
;
1304 struct bt_private_value
*params
= NULL
;
1305 struct bt_value
*metadata_text_value
= NULL
;
1306 const char *metadata_text
= NULL
;
1307 static const char * const plugin_name
= "ctf";
1308 static const char * const comp_cls_name
= "fs";
1309 static const enum bt_component_class_type comp_cls_type
=
1310 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1311 const char *fail_reason
= NULL
;
1312 FILE *out_stream
= stdout
;
1314 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1315 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1318 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1319 "comp-cls-name=\"%s\", comp-cls-type=%d",
1320 plugin_name
, comp_cls_name
,
1321 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1322 fprintf(stderr
, "%s%sCannot find component class %s",
1323 bt_common_color_bold(),
1324 bt_common_color_fg_red(),
1325 bt_common_color_reset());
1326 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1327 comp_cls_name
, comp_cls_type
);
1328 fprintf(stderr
, "\n");
1333 params
= bt_private_value_map_create();
1339 ret
= bt_private_value_map_insert_string_entry(params
, "path",
1340 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1346 ret
= query(comp_cls
, "metadata-info",
1347 bt_value_borrow_from_private(params
), &results
, &fail_reason
);
1352 metadata_text_value
= bt_value_map_borrow_entry_value(results
, "text");
1353 if (!metadata_text_value
) {
1354 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1359 ret
= bt_value_string_get(metadata_text_value
, &metadata_text
);
1360 BT_ASSERT(ret
== 0);
1362 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1364 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1368 BT_LOGE_ERRNO("Cannot open file for writing",
1370 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1375 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1377 BT_LOGE("Cannot write whole metadata text to output stream: "
1385 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1386 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1387 bt_common_color_bold(),
1388 bt_common_color_fg_red(),
1390 bt_common_color_reset());
1393 destroy_the_query_executor();
1394 bt_object_put_ref(results
);
1395 bt_object_put_ref(params
);
1396 bt_object_put_ref(comp_cls
);
1398 if (out_stream
&& out_stream
!= stdout
) {
1399 int fclose_ret
= fclose(out_stream
);
1402 BT_LOGE_ERRNO("Cannot close file stream",
1404 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1412 char *instance_name
;
1416 struct trace_range
{
1417 uint64_t intersection_range_begin_ns
;
1418 uint64_t intersection_range_end_ns
;
1422 guint
port_id_hash(gconstpointer v
)
1424 const struct port_id
*id
= v
;
1426 BT_ASSERT(id
->instance_name
);
1427 BT_ASSERT(id
->port_name
);
1429 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1433 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1435 const struct port_id
*id1
= v1
;
1436 const struct port_id
*id2
= v2
;
1438 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1439 !strcmp(id1
->port_name
, id2
->port_name
);
1443 void port_id_destroy(gpointer data
)
1445 struct port_id
*id
= data
;
1447 free(id
->instance_name
);
1448 free(id
->port_name
);
1453 void trace_range_destroy(gpointer data
)
1458 struct cmd_run_ctx
{
1460 GHashTable
*components
;
1463 struct bt_graph
*graph
;
1466 struct bt_config
*cfg
;
1470 bool stream_intersection_mode
;
1473 * Association of struct port_id -> struct trace_range.
1475 GHashTable
*intersections
;
1478 /* Returns a timestamp of the form "(-)s.ns" */
1480 char *s_from_ns(int64_t ns
)
1485 int64_t ts_sec_abs
, ts_nsec_abs
;
1486 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1487 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1489 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1490 is_negative
= false;
1491 ts_sec_abs
= ts_sec
;
1492 ts_nsec_abs
= ts_nsec
;
1493 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1494 is_negative
= false;
1495 ts_sec_abs
= ts_sec
- 1;
1496 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1497 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1499 ts_sec_abs
= ts_sec
;
1500 ts_nsec_abs
= -ts_nsec
;
1501 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1503 ts_sec_abs
= -(ts_sec
+ 1);
1504 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1505 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1507 ts_sec_abs
= -ts_sec
;
1508 ts_nsec_abs
= ts_nsec
;
1509 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1511 ts_sec_abs
= -ts_sec
;
1512 ts_nsec_abs
= -ts_nsec
;
1515 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1516 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1524 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1525 struct cmd_run_ctx
*ctx
, struct bt_component
*upstream_comp
,
1526 struct bt_port
*upstream_port
,
1527 struct bt_config_connection
*cfg_conn
)
1530 GQuark downstreamp_comp_name_quark
;
1531 struct bt_component
*downstream_comp
;
1532 int64_t downstream_port_count
;
1534 int64_t (*port_count_fn
)(struct bt_component
*);
1535 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
1536 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1537 bool insert_trimmer
= false;
1538 struct bt_private_value
*trimmer_params
= NULL
;
1539 char *intersection_begin
= NULL
;
1540 char *intersection_end
= NULL
;
1541 struct bt_component
*trimmer
= NULL
;
1542 struct bt_component_class
*trimmer_class
= NULL
;
1543 struct bt_port
*trimmer_input
= NULL
;
1544 struct bt_port
*trimmer_output
= NULL
;
1546 if (ctx
->intersections
&&
1547 bt_component_get_class_type(upstream_comp
) ==
1548 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1549 struct trace_range
*range
;
1550 struct port_id port_id
= {
1551 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1552 .port_name
= (char *) bt_port_get_name(upstream_port
)
1555 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1559 range
= (struct trace_range
*) g_hash_table_lookup(
1560 ctx
->intersections
, &port_id
);
1562 enum bt_value_status status
;
1564 intersection_begin
= s_from_ns(
1565 range
->intersection_range_begin_ns
);
1566 intersection_end
= s_from_ns(
1567 range
->intersection_range_end_ns
);
1568 if (!intersection_begin
|| !intersection_end
) {
1569 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1573 insert_trimmer
= true;
1574 trimmer_params
= bt_private_value_map_create();
1575 if (!trimmer_params
) {
1579 status
= bt_private_value_map_insert_string_entry(
1580 trimmer_params
, "begin", intersection_begin
);
1581 if (status
!= BT_VALUE_STATUS_OK
) {
1584 status
= bt_private_value_map_insert_string_entry(
1586 "end", intersection_end
);
1587 if (status
!= BT_VALUE_STATUS_OK
) {
1592 trimmer_class
= find_component_class("utils", "trimmer",
1593 BT_COMPONENT_CLASS_TYPE_FILTER
);
1594 if (!trimmer_class
) {
1599 BT_LOGI("Connecting upstream port to the next available downstream port: "
1600 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1601 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1602 upstream_port
, bt_port_get_name(upstream_port
),
1603 cfg_conn
->downstream_comp_name
->str
,
1604 cfg_conn
->arg
->str
);
1605 downstreamp_comp_name_quark
= g_quark_from_string(
1606 cfg_conn
->downstream_comp_name
->str
);
1607 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1608 downstream_comp
= g_hash_table_lookup(ctx
->components
,
1609 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1610 if (!downstream_comp
) {
1611 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1612 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1613 cfg_conn
->arg
->str
);
1614 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1615 cfg_conn
->arg
->str
);
1619 if (bt_component_is_filter(downstream_comp
)) {
1620 port_count_fn
= bt_component_filter_get_input_port_count
;
1621 port_by_index_fn
= bt_component_filter_get_input_port_by_index
;
1622 } else if (bt_component_is_sink(downstream_comp
)) {
1623 port_count_fn
= bt_component_sink_get_input_port_count
;
1624 port_by_index_fn
= bt_component_sink_get_input_port_by_index
;
1627 * Should never happen because the connections are
1628 * validated before we get here.
1630 BT_LOGF("Invalid connection: downstream component is a source: "
1631 "conn-arg=\"%s\"", cfg_conn
->arg
->str
);
1635 downstream_port_count
= port_count_fn(downstream_comp
);
1636 BT_ASSERT(downstream_port_count
>= 0);
1638 for (i
= 0; i
< downstream_port_count
; i
++) {
1639 struct bt_port
*downstream_port
=
1640 port_by_index_fn(downstream_comp
, i
);
1641 const char *upstream_port_name
;
1642 const char *downstream_port_name
;
1644 BT_ASSERT(downstream_port
);
1646 /* Skip port if it's already connected. */
1647 if (bt_port_is_connected(downstream_port
)) {
1648 bt_object_put_ref(downstream_port
);
1649 BT_LOGD("Skipping downstream port: already connected: "
1650 "port-addr=%p, port-name=\"%s\"",
1652 bt_port_get_name(downstream_port
));
1656 downstream_port_name
= bt_port_get_name(downstream_port
);
1657 BT_ASSERT(downstream_port_name
);
1658 upstream_port_name
= bt_port_get_name(upstream_port
);
1659 BT_ASSERT(upstream_port_name
);
1661 if (!bt_common_star_glob_match(
1662 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1663 downstream_port_name
, SIZE_MAX
)) {
1664 bt_object_put_ref(downstream_port
);
1668 if (insert_trimmer
) {
1670 * In order to insert the trimmer between the two
1671 * components that were being connected, we create
1672 * a connection configuration entry which describes
1673 * a connection from the trimmer's output to the
1674 * original input that was being connected.
1676 * Hence, the creation of the trimmer will cause the
1677 * graph "new port" listener to establish all downstream
1678 * connections as its output port is connected. We will
1679 * then establish the connection between the original
1680 * upstream source and the trimmer.
1682 char *trimmer_name
= NULL
;
1683 enum bt_graph_status graph_status
;
1685 ret
= asprintf(&trimmer_name
, "%s-%s",
1686 "stream-intersection-trimmer",
1687 upstream_port_name
);
1693 ctx
->connect_ports
= false;
1694 graph_status
= bt_graph_add_component(ctx
->graph
,
1695 trimmer_class
, trimmer_name
,
1696 bt_value_borrow_from_private(trimmer_params
),
1699 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1705 bt_component_filter_get_input_port_by_index(
1707 if (!trimmer_input
) {
1711 bt_component_filter_get_output_port_by_index(
1713 if (!trimmer_output
) {
1718 * Replace the current downstream port by the trimmer's
1721 BT_OBJECT_MOVE_REF(downstream_port
, trimmer_input
);
1722 downstream_port_name
= bt_port_get_name(
1724 if (!downstream_port_name
) {
1729 /* We have a winner! */
1730 status
= bt_graph_connect_ports(ctx
->graph
,
1731 upstream_port
, downstream_port
, NULL
);
1732 BT_OBJECT_PUT_REF_AND_RESET(downstream_port
);
1734 case BT_GRAPH_STATUS_OK
:
1736 case BT_GRAPH_STATUS_CANCELED
:
1737 BT_LOGI_STR("Graph was canceled by user.");
1738 status
= BT_GRAPH_STATUS_OK
;
1740 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1741 BT_LOGE("A component refused a connection to one of its ports: "
1742 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1743 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1744 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1745 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1747 upstream_comp
, bt_component_get_name(upstream_comp
),
1748 upstream_port
, bt_port_get_name(upstream_port
),
1749 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1750 downstream_port
, downstream_port_name
,
1751 cfg_conn
->arg
->str
);
1753 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1754 bt_port_get_name(upstream_port
),
1755 downstream_port_name
,
1756 cfg_conn
->arg
->str
);
1759 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1760 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1761 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1762 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1763 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1765 upstream_comp
, bt_component_get_name(upstream_comp
),
1766 upstream_port
, bt_port_get_name(upstream_port
),
1767 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1768 downstream_port
, downstream_port_name
,
1769 cfg_conn
->arg
->str
);
1771 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1772 bt_port_get_name(upstream_port
),
1773 downstream_port_name
,
1774 cfg_conn
->arg
->str
);
1778 BT_LOGI("Connected component ports: "
1779 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1780 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1781 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1782 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1784 upstream_comp
, bt_component_get_name(upstream_comp
),
1785 upstream_port
, bt_port_get_name(upstream_port
),
1786 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1787 downstream_port
, downstream_port_name
,
1788 cfg_conn
->arg
->str
);
1790 if (insert_trimmer
) {
1792 * The first connection, from the source to the trimmer,
1793 * has been done. We now connect the trimmer to the
1794 * original downstream port.
1796 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1797 ctx
, trimmer
, trimmer_output
, cfg_conn
);
1801 ctx
->connect_ports
= true;
1805 * We found a matching downstream port: the search is
1811 /* No downstream port found */
1812 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1813 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1814 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1815 upstream_port
, bt_port_get_name(upstream_port
),
1816 cfg_conn
->downstream_comp_name
->str
,
1817 cfg_conn
->arg
->str
);
1819 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1820 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1826 free(intersection_begin
);
1827 free(intersection_end
);
1828 BT_OBJECT_PUT_REF_AND_RESET(trimmer_params
);
1829 BT_OBJECT_PUT_REF_AND_RESET(trimmer_class
);
1830 BT_OBJECT_PUT_REF_AND_RESET(trimmer
);
1831 BT_OBJECT_PUT_REF_AND_RESET(trimmer_input
);
1832 BT_OBJECT_PUT_REF_AND_RESET(trimmer_output
);
1837 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1838 struct bt_port
*upstream_port
)
1841 const char *upstream_port_name
;
1842 const char *upstream_comp_name
;
1843 struct bt_component
*upstream_comp
= NULL
;
1847 BT_ASSERT(upstream_port
);
1848 upstream_port_name
= bt_port_get_name(upstream_port
);
1849 BT_ASSERT(upstream_port_name
);
1850 upstream_comp
= bt_port_get_component(upstream_port
);
1851 if (!upstream_comp
) {
1852 BT_LOGW("Upstream port to connect is not part of a component: "
1853 "port-addr=%p, port-name=\"%s\"",
1854 upstream_port
, upstream_port_name
);
1859 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1860 BT_ASSERT(upstream_comp_name
);
1861 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1862 "port-addr=%p, port-name=\"%s\"",
1863 upstream_comp
, upstream_comp_name
,
1864 upstream_port
, upstream_port_name
);
1866 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1867 struct bt_config_connection
*cfg_conn
=
1869 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1871 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1872 upstream_comp_name
)) {
1876 if (!bt_common_star_glob_match(
1877 cfg_conn
->upstream_port_glob
->str
,
1878 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1882 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1883 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1885 BT_LOGE("Cannot connect upstream port: "
1886 "port-addr=%p, port-name=\"%s\"",
1888 upstream_port_name
);
1890 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1893 cfg_conn
->arg
->str
);
1899 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1900 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1901 upstream_port_name
);
1903 "Cannot create connection: upstream port `%s` does not match any connection\n",
1904 upstream_port_name
);
1910 bt_object_put_ref(upstream_comp
);
1915 void graph_port_added_listener(struct bt_port
*port
, void *data
)
1917 struct bt_component
*comp
= NULL
;
1918 struct cmd_run_ctx
*ctx
= data
;
1920 comp
= bt_port_get_component(port
);
1921 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1922 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1923 comp
, comp
? bt_component_get_name(comp
) : "",
1924 port
, bt_port_get_name(port
));
1926 if (!ctx
->connect_ports
) {
1931 BT_LOGW_STR("Port has no component.");
1935 if (bt_port_is_connected(port
)) {
1936 BT_LOGW_STR("Port is already connected.");
1940 if (!bt_port_is_output(port
)) {
1941 BT_LOGI_STR("Skipping input port.");
1945 if (cmd_run_ctx_connect_upstream_port(ctx
, port
)) {
1946 BT_LOGF_STR("Cannot connect upstream port.");
1947 fprintf(stderr
, "Added port could not be connected: aborting\n");
1952 bt_object_put_ref(comp
);
1957 void graph_port_removed_listener(struct bt_component
*component
,
1958 struct bt_port
*port
, void *data
)
1960 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1961 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1962 component
, bt_component_get_name(component
),
1963 port
, bt_port_get_name(port
));
1967 void graph_ports_connected_listener(struct bt_port
*upstream_port
,
1968 struct bt_port
*downstream_port
, void *data
)
1970 struct bt_component
*upstream_comp
= bt_port_get_component(upstream_port
);
1971 struct bt_component
*downstream_comp
= bt_port_get_component(downstream_port
);
1973 BT_ASSERT(upstream_comp
);
1974 BT_ASSERT(downstream_comp
);
1975 BT_LOGI("Graph's component ports connected: "
1976 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1977 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1978 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1979 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1980 upstream_comp
, bt_component_get_name(upstream_comp
),
1981 upstream_port
, bt_port_get_name(upstream_port
),
1982 downstream_comp
, bt_component_get_name(downstream_comp
),
1983 downstream_port
, bt_port_get_name(downstream_port
));
1984 bt_object_put_ref(upstream_comp
);
1985 bt_object_put_ref(downstream_comp
);
1989 void graph_ports_disconnected_listener(
1990 struct bt_component
*upstream_component
,
1991 struct bt_component
*downstream_component
,
1992 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
1995 BT_LOGI("Graph's component ports disconnected: "
1996 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1997 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1998 upstream_port
, bt_port_get_name(upstream_port
),
1999 downstream_port
, bt_port_get_name(downstream_port
));
2003 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
2009 if (ctx
->components
) {
2010 g_hash_table_destroy(ctx
->components
);
2011 ctx
->components
= NULL
;
2014 if (ctx
->intersections
) {
2015 g_hash_table_destroy(ctx
->intersections
);
2016 ctx
->intersections
= NULL
;
2019 BT_OBJECT_PUT_REF_AND_RESET(ctx
->graph
);
2025 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2030 ctx
->connect_ports
= false;
2031 ctx
->components
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
2032 NULL
, bt_object_put_ref
);
2033 if (!ctx
->components
) {
2037 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2038 ctx
->stream_intersection_mode
= true;
2039 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2040 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2041 if (!ctx
->intersections
) {
2046 ctx
->graph
= bt_graph_create();
2051 the_graph
= ctx
->graph
;
2052 ret
= bt_graph_add_port_added_listener(ctx
->graph
,
2053 graph_port_added_listener
, NULL
, ctx
);
2055 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2059 ret
= bt_graph_add_port_removed_listener(ctx
->graph
,
2060 graph_port_removed_listener
, NULL
, ctx
);
2062 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
2066 ret
= bt_graph_add_ports_connected_listener(ctx
->graph
,
2067 graph_ports_connected_listener
, NULL
, ctx
);
2069 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
2073 ret
= bt_graph_add_ports_disconnected_listener(ctx
->graph
,
2074 graph_ports_disconnected_listener
, NULL
, ctx
);
2076 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
2083 cmd_run_ctx_destroy(ctx
);
2091 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2092 struct bt_config_component
*cfg_comp
,
2093 struct bt_component_class
*comp_cls
)
2097 int64_t trace_count
;
2098 enum bt_value_status value_status
;
2099 const char *path
= NULL
;
2100 struct bt_value
*component_path_value
= NULL
;
2101 struct bt_private_value
*query_params
= NULL
;
2102 struct bt_value
*query_result
= NULL
;
2103 struct bt_value
*trace_info
= NULL
;
2104 struct bt_value
*intersection_range
= NULL
;
2105 struct bt_value
*intersection_begin
= NULL
;
2106 struct bt_value
*intersection_end
= NULL
;
2107 struct bt_value
*stream_path_value
= NULL
;
2108 struct bt_value
*stream_paths
= NULL
;
2109 struct bt_value
*stream_infos
= NULL
;
2110 struct bt_value
*stream_info
= NULL
;
2111 struct port_id
*port_id
= NULL
;
2112 struct trace_range
*trace_range
= NULL
;
2113 const char *fail_reason
= NULL
;
2115 component_path_value
= bt_value_map_borrow_entry_value(
2116 bt_value_borrow_from_private(cfg_comp
->params
),
2118 if (component_path_value
&& !bt_value_is_string(component_path_value
)) {
2119 BT_LOGD("Cannot get path parameter: component-name=%s",
2120 cfg_comp
->instance_name
->str
);
2125 value_status
= bt_value_string_get(component_path_value
, &path
);
2126 if (value_status
!= BT_VALUE_STATUS_OK
) {
2127 BT_LOGD("Cannot get path string value: component-name=%s",
2128 cfg_comp
->instance_name
->str
);
2133 query_params
= bt_private_value_map_create();
2134 if (!query_params
) {
2135 BT_LOGE_STR("Cannot create query parameters.");
2140 value_status
= bt_private_value_map_insert_entry(query_params
, "path",
2141 component_path_value
);
2142 if (value_status
!= BT_VALUE_STATUS_OK
) {
2143 BT_LOGE_STR("Cannot insert path parameter in query parameter map.");
2148 ret
= query(comp_cls
, "trace-info",
2149 bt_value_borrow_from_private(query_params
), &query_result
,
2152 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2153 "comp-class-name=\"%s\"", fail_reason
,
2154 bt_component_class_get_name(comp_cls
));
2159 BT_ASSERT(query_result
);
2161 if (!bt_value_is_array(query_result
)) {
2162 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2163 "component-class-name=%s",
2164 bt_component_class_get_name(comp_cls
));
2169 trace_count
= bt_value_array_get_size(query_result
);
2170 if (trace_count
< 0) {
2175 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2177 uint64_t stream_idx
;
2178 int64_t stream_count
;
2180 trace_info
= bt_value_array_borrow_element_by_index(
2181 query_result
, trace_idx
);
2182 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2184 BT_LOGD_STR("Cannot retrieve trace from query result.");
2188 intersection_range
= bt_value_map_borrow_entry_value(trace_info
,
2189 "intersection-range-ns");
2190 if (!intersection_range
) {
2192 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2196 intersection_begin
= bt_value_map_borrow_entry_value(
2197 intersection_range
, "begin");
2198 if (!intersection_begin
) {
2200 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2204 intersection_end
= bt_value_map_borrow_entry_value(
2205 intersection_range
, "end");
2206 if (!intersection_end
) {
2208 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2212 value_status
= bt_value_integer_get(intersection_begin
, &begin
);
2213 if (value_status
!= BT_VALUE_STATUS_OK
) {
2215 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'begin\' field from query result.");
2219 value_status
= bt_value_integer_get(intersection_end
, &end
);
2220 if (value_status
!= BT_VALUE_STATUS_OK
) {
2222 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'end\' field from query result.");
2226 if (begin
< 0 || end
< 0 || end
< begin
) {
2227 BT_LOGW("Invalid trace stream intersection values: "
2228 "intersection-range-ns:begin=%" PRId64
2229 ", intersection-range-ns:end=%" PRId64
,
2235 stream_infos
= bt_value_map_borrow_entry_value(trace_info
,
2237 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2239 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2243 stream_count
= bt_value_array_get_size(stream_infos
);
2244 if (stream_count
< 0) {
2252 * The first path of a stream's "paths" is currently used to
2253 * associate streams/ports to a given trace intersection.
2255 * This is a fragile hack as it relies on the port names
2256 * being set to the various streams path.
2258 * A stream name should be introduced as part of the trace-info
2261 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2262 const char *stream_path
;
2264 port_id
= g_new0(struct port_id
, 1);
2267 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2270 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2271 if (!port_id
->instance_name
) {
2273 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2277 trace_range
= g_new0(struct trace_range
, 1);
2280 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2283 trace_range
->intersection_range_begin_ns
= begin
;
2284 trace_range
->intersection_range_end_ns
= end
;
2286 stream_info
= bt_value_array_borrow_element_by_index(
2287 stream_infos
, stream_idx
);
2288 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2290 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2294 stream_paths
= bt_value_map_borrow_entry_value(
2295 stream_info
, "paths");
2296 if (!stream_paths
|| !bt_value_is_array(stream_paths
)) {
2298 BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
2303 bt_value_array_borrow_element_by_index(
2305 if (!stream_path_value
||
2306 !bt_value_is_string(stream_path_value
)) {
2308 BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
2312 value_status
= bt_value_string_get(stream_path_value
,
2314 if (value_status
!= BT_VALUE_STATUS_OK
) {
2319 port_id
->port_name
= strdup(stream_path
);
2320 if (!port_id
->port_name
) {
2322 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2326 BT_LOGD("Inserting stream intersection ");
2328 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2338 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2339 bt_common_color_bold(),
2340 bt_common_color_fg_yellow(),
2341 path
? path
: "(unknown)",
2342 bt_common_color_reset());
2344 bt_object_put_ref(query_params
);
2345 bt_object_put_ref(query_result
);
2347 g_free(trace_range
);
2352 int cmd_run_ctx_create_components_from_config_components(
2353 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2356 struct bt_component_class
*comp_cls
= NULL
;
2357 struct bt_component
*comp
= NULL
;
2360 for (i
= 0; i
< cfg_components
->len
; i
++) {
2361 struct bt_config_component
*cfg_comp
=
2362 g_ptr_array_index(cfg_components
, i
);
2365 comp_cls
= find_component_class(cfg_comp
->plugin_name
->str
,
2366 cfg_comp
->comp_cls_name
->str
, cfg_comp
->type
);
2368 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2369 "comp-cls-name=\"%s\", comp-cls-type=%d",
2370 cfg_comp
->plugin_name
->str
,
2371 cfg_comp
->comp_cls_name
->str
,
2373 fprintf(stderr
, "%s%sCannot find component class %s",
2374 bt_common_color_bold(),
2375 bt_common_color_fg_red(),
2376 bt_common_color_reset());
2377 print_plugin_comp_cls_opt(stderr
,
2378 cfg_comp
->plugin_name
->str
,
2379 cfg_comp
->comp_cls_name
->str
,
2381 fprintf(stderr
, "\n");
2385 ret
= bt_graph_add_component(ctx
->graph
, comp_cls
,
2386 cfg_comp
->instance_name
->str
,
2387 bt_value_borrow_from_private(cfg_comp
->params
), &comp
);
2389 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2390 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2392 cfg_comp
->plugin_name
->str
,
2393 cfg_comp
->comp_cls_name
->str
,
2394 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2395 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2396 bt_common_color_bold(),
2397 bt_common_color_fg_red(),
2398 cfg_comp
->instance_name
->str
,
2399 bt_common_color_reset());
2403 if (ctx
->stream_intersection_mode
&&
2404 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2405 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2411 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2412 comp
, cfg_comp
->instance_name
->str
);
2413 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2414 BT_ASSERT(quark
> 0);
2415 g_hash_table_insert(ctx
->components
,
2416 GUINT_TO_POINTER(quark
), comp
);
2418 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2427 bt_object_put_ref(comp
);
2428 bt_object_put_ref(comp_cls
);
2433 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2438 * Make sure that, during this phase, our graph's "port added"
2439 * listener does not connect ports while we are creating the
2440 * components because we have a special, initial phase for
2443 ctx
->connect_ports
= false;
2445 ret
= cmd_run_ctx_create_components_from_config_components(
2446 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2452 ret
= cmd_run_ctx_create_components_from_config_components(
2453 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2459 ret
= cmd_run_ctx_create_components_from_config_components(
2460 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2471 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2472 struct bt_component
*comp
,
2473 int64_t (*port_count_fn
)(struct bt_component
*),
2474 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t))
2480 count
= port_count_fn(comp
);
2481 BT_ASSERT(count
>= 0);
2483 for (i
= 0; i
< count
; i
++) {
2484 struct bt_port
*upstream_port
= port_by_index_fn(comp
, i
);
2486 BT_ASSERT(upstream_port
);
2487 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2488 bt_object_put_ref(upstream_port
);
2499 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2502 GHashTableIter iter
;
2503 gpointer g_name_quark
, g_comp
;
2505 ctx
->connect_ports
= true;
2506 g_hash_table_iter_init(&iter
, ctx
->components
);
2508 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2509 int64_t (*port_count_fn
)(struct bt_component
*);
2510 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
2512 if (bt_component_is_source(g_comp
)) {
2514 bt_component_source_get_output_port_count
;
2516 bt_component_source_get_output_port_by_index
;
2517 } else if (bt_component_is_filter(g_comp
)) {
2519 bt_component_filter_get_output_port_count
;
2521 bt_component_filter_get_output_port_by_index
;
2526 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
2527 g_comp
, port_count_fn
, port_by_index_fn
);
2538 const char *bt_graph_status_str(enum bt_graph_status status
)
2541 case BT_GRAPH_STATUS_CANCELED
:
2542 return "BT_GRAPH_STATUS_CANCELED";
2543 case BT_GRAPH_STATUS_AGAIN
:
2544 return "BT_GRAPH_STATUS_AGAIN";
2545 case BT_GRAPH_STATUS_END
:
2546 return "BT_GRAPH_STATUS_END";
2547 case BT_GRAPH_STATUS_OK
:
2548 return "BT_GRAPH_STATUS_OK";
2549 case BT_GRAPH_STATUS_INVALID
:
2550 return "BT_GRAPH_STATUS_INVALID";
2551 case BT_GRAPH_STATUS_NO_SINK
:
2552 return "BT_GRAPH_STATUS_NO_SINK";
2553 case BT_GRAPH_STATUS_ERROR
:
2554 return "BT_GRAPH_STATUS_ERROR";
2561 int cmd_run(struct bt_config
*cfg
)
2564 struct cmd_run_ctx ctx
= { 0 };
2566 /* Initialize the command's context and the graph object */
2567 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2568 BT_LOGE_STR("Cannot initialize the command's context.");
2569 fprintf(stderr
, "Cannot initialize the command's context\n");
2574 BT_LOGI_STR("Canceled by user before creating components.");
2578 BT_LOGI_STR("Creating components.");
2580 /* Create the requested component instances */
2581 if (cmd_run_ctx_create_components(&ctx
)) {
2582 BT_LOGE_STR("Cannot create components.");
2583 fprintf(stderr
, "Cannot create components\n");
2588 BT_LOGI_STR("Canceled by user before connecting components.");
2592 BT_LOGI_STR("Connecting components.");
2594 /* Connect the initially visible component ports */
2595 if (cmd_run_ctx_connect_ports(&ctx
)) {
2596 BT_LOGE_STR("Cannot connect initial component ports.");
2597 fprintf(stderr
, "Cannot connect initial component ports\n");
2602 BT_LOGI_STR("Canceled by user before running the graph.");
2606 BT_LOGI_STR("Running the graph.");
2610 enum bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2613 * Reset console in case something messed with console
2614 * codes during the graph's execution.
2616 printf("%s", bt_common_color_reset());
2618 fprintf(stderr
, "%s", bt_common_color_reset());
2619 BT_LOGV("bt_graph_run() returned: status=%s",
2620 bt_graph_status_str(graph_status
));
2622 switch (graph_status
) {
2623 case BT_GRAPH_STATUS_OK
:
2625 case BT_GRAPH_STATUS_CANCELED
:
2626 BT_LOGI_STR("Graph was canceled by user.");
2628 case BT_GRAPH_STATUS_AGAIN
:
2629 if (bt_graph_is_canceled(ctx
.graph
)) {
2630 BT_LOGI_STR("Graph was canceled by user.");
2634 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2635 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2637 cfg
->cmd_data
.run
.retry_duration_us
);
2639 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2640 if (bt_graph_is_canceled(ctx
.graph
)) {
2641 BT_LOGI_STR("Graph was canceled by user.");
2647 case BT_COMPONENT_STATUS_END
:
2650 BT_LOGE_STR("Graph failed to complete successfully");
2651 fprintf(stderr
, "Graph failed to complete successfully\n");
2664 cmd_run_ctx_destroy(&ctx
);
2669 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2671 const char *env_clash
;
2673 if (!cfg
->command_name
) {
2677 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2678 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2682 if (g_file_test(cfg
->command_name
,
2683 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2684 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2686 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2688 fprintf(stderr
, "\n");
2689 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
2695 void init_log_level(void)
2697 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2701 void set_auto_log_levels(struct bt_config
*cfg
)
2703 const char **env_var_name
;
2706 * Override the configuration's default log level if
2707 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2708 * are found for backward compatibility with legacy Babetrace 1.
2710 if (getenv("BABELTRACE_DEBUG") &&
2711 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2712 cfg
->log_level
= 'V';
2713 } else if (getenv("BABELTRACE_VERBOSE") &&
2714 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2715 cfg
->log_level
= 'I';
2719 * Set log levels according to --debug or --verbose. For
2720 * backward compatibility, --debug is more verbose than
2723 * --verbose: INFO log level
2724 * --debug: VERBOSE log level (includes DEBUG, which is
2725 * is less verbose than VERBOSE in the internal
2726 * logging framework)
2728 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2730 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2731 } else if (cfg
->debug
) {
2732 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2735 * Set library's default log level if not
2736 * explicitly specified.
2738 switch (cfg
->log_level
) {
2740 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE
);
2743 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2746 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
2749 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2752 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN
);
2755 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR
);
2758 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL
);
2766 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2768 bt_cli_log_level
= BT_LOG_INFO
;
2769 } else if (cfg
->debug
) {
2770 bt_cli_log_level
= BT_LOG_VERBOSE
;
2773 * Set CLI's default log level if not explicitly
2776 switch (cfg
->log_level
) {
2778 bt_cli_log_level
= BT_LOG_NONE
;
2781 bt_cli_log_level
= BT_LOG_VERBOSE
;
2784 bt_cli_log_level
= BT_LOG_DEBUG
;
2787 bt_cli_log_level
= BT_LOG_INFO
;
2790 bt_cli_log_level
= BT_LOG_WARN
;
2793 bt_cli_log_level
= BT_LOG_ERROR
;
2796 bt_cli_log_level
= BT_LOG_FATAL
;
2804 env_var_name
= log_level_env_var_names
;
2806 while (*env_var_name
) {
2807 if (!getenv(*env_var_name
)) {
2809 g_setenv(*env_var_name
, "I", 1);
2810 } else if (cfg
->debug
) {
2811 g_setenv(*env_var_name
, "V", 1);
2813 char val
[2] = { 0 };
2816 * Set module's default log level if not
2817 * explicitly specified.
2819 val
[0] = cfg
->log_level
;
2820 g_setenv(*env_var_name
, val
, 1);
2828 int main(int argc
, const char **argv
)
2832 struct bt_config
*cfg
;
2835 set_signal_handler();
2837 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2840 /* Quit without errors; typically usage/version */
2842 BT_LOGI_STR("Quitting without errors.");
2847 BT_LOGE("Command-line error: retcode=%d", retcode
);
2852 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2853 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2858 set_auto_log_levels(cfg
);
2861 if (cfg
->command_needs_plugins
) {
2862 ret
= load_all_plugins(
2863 bt_value_borrow_from_private(cfg
->plugin_paths
));
2865 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2871 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2872 cfg
->command
, cfg
->command_name
);
2874 switch (cfg
->command
) {
2875 case BT_CONFIG_COMMAND_RUN
:
2878 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2879 ret
= cmd_list_plugins(cfg
);
2881 case BT_CONFIG_COMMAND_HELP
:
2882 ret
= cmd_help(cfg
);
2884 case BT_CONFIG_COMMAND_QUERY
:
2885 ret
= cmd_query(cfg
);
2887 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2888 ret
= cmd_print_ctf_metadata(cfg
);
2890 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2891 ret
= cmd_print_lttng_live_sessions(cfg
);
2894 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2898 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2899 cfg
->command
, cfg
->command_name
, ret
);
2900 warn_command_name_and_directory_clash(cfg
);
2901 retcode
= ret
? 1 : 0;
2904 BT_OBJECT_PUT_REF_AND_RESET(cfg
);