2 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
4 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "CLI"
28 #include <babeltrace2/babeltrace.h>
29 #include "common/common.h"
39 #include "babeltrace2-cfg.h"
40 #include "babeltrace2-cfg-cli-args.h"
41 #include "babeltrace2-cfg-cli-args-default.h"
43 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
44 #define ENV_BABELTRACE_CLI_LOG_LEVEL "BABELTRACE_CLI_LOG_LEVEL"
45 #define NSEC_PER_SEC 1000000000LL
48 * Known environment variable names for the log levels of the project's
51 static const char* log_level_env_var_names
[] = {
52 "BABELTRACE_FLT_LTTNG_UTILS_DEBUG_INFO_LOG_LEVEL",
53 "BABELTRACE_FLT_UTILS_COUNTER_LOG_LEVEL",
54 "BABELTRACE_FLT_UTILS_MUXER_LOG_LEVEL",
55 "BABELTRACE_FLT_UTILS_TRIMMER_LOG_LEVEL",
56 "BABELTRACE_PLUGIN_CTF_BFCR_LOG_LEVEL",
57 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
58 "BABELTRACE_PLUGIN_CTF_MSG_ITER_LOG_LEVEL",
59 "BABELTRACE_PLUGIN_CTF_UTILS_LOG_LEVEL",
60 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
61 "BABELTRACE_SINK_CTF_FS_LOG_LEVEL",
62 "BABELTRACE_SINK_TEXT_DETAILS_LOG_LEVEL",
63 "BABELTRACE_SRC_CTF_FS_LOG_LEVEL",
64 "BABELTRACE_SRC_CTF_LTTNG_LIVE_LOG_LEVEL",
68 /* Application's processing graph (weak) */
69 static bt_graph
*the_graph
;
70 static bt_query_executor
*the_query_executor
;
71 static bool canceled
= false;
73 GPtrArray
*loaded_plugins
;
80 BOOL WINAPI
signal_handler(DWORD signal
) {
82 bt_graph_cancel(the_graph
);
91 void set_signal_handler(void)
93 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
94 BT_LOGE("Failed to set the Ctrl+C handler.");
98 #else /* __MINGW32__ */
101 void signal_handler(int signum
)
103 if (signum
!= SIGINT
) {
108 bt_graph_cancel(the_graph
);
111 if (the_query_executor
) {
112 bt_query_executor_cancel(the_query_executor
);
119 void set_signal_handler(void)
121 struct sigaction new_action
, old_action
;
123 new_action
.sa_handler
= signal_handler
;
124 sigemptyset(&new_action
.sa_mask
);
125 new_action
.sa_flags
= 0;
126 sigaction(SIGINT
, NULL
, &old_action
);
128 if (old_action
.sa_handler
!= SIG_IGN
) {
129 sigaction(SIGINT
, &new_action
, NULL
);
133 #endif /* __MINGW32__ */
136 void init_static_data(void)
138 loaded_plugins
= g_ptr_array_new_with_free_func(
139 (GDestroyNotify
) bt_object_put_ref
);
143 void fini_static_data(void)
145 g_ptr_array_free(loaded_plugins
, TRUE
);
149 int create_the_query_executor(void)
153 the_query_executor
= bt_query_executor_create();
154 if (!the_query_executor
) {
155 BT_LOGE_STR("Cannot create a query executor.");
163 void destroy_the_query_executor(void)
165 BT_QUERY_EXECUTOR_PUT_REF_AND_RESET(the_query_executor
);
169 int query(struct bt_config
*cfg
, const bt_component_class
*comp_cls
,
170 const char *obj
, const bt_value
*params
,
171 const bt_value
**user_result
, const char **fail_reason
)
173 const bt_value
*result
= NULL
;
174 bt_query_executor_status status
;
175 *fail_reason
= "unknown error";
178 BT_ASSERT(fail_reason
);
179 BT_ASSERT(user_result
);
180 ret
= create_the_query_executor();
182 /* create_the_query_executor() logs errors */
187 BT_LOGI("Canceled by user before executing the query: "
188 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
189 "query-obj=\"%s\"", comp_cls
,
190 bt_component_class_get_name(comp_cls
), obj
);
191 *fail_reason
= "canceled by user";
196 status
= bt_query_executor_query(the_query_executor
,
197 comp_cls
, obj
, params
, cfg
->log_level
, &result
);
199 case BT_QUERY_EXECUTOR_STATUS_OK
:
201 case BT_QUERY_EXECUTOR_STATUS_AGAIN
:
203 const uint64_t sleep_time_us
= 100000;
205 /* Wait 100 ms and retry */
206 BT_LOGD("Got BT_QUERY_EXECUTOR_STATUS_AGAIN: sleeping: "
207 "time-us=%" PRIu64
, sleep_time_us
);
209 if (usleep(sleep_time_us
)) {
210 if (bt_query_executor_is_canceled(the_query_executor
)) {
211 BT_LOGI("Query was canceled by user: "
212 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
213 "query-obj=\"%s\"", comp_cls
,
214 bt_component_class_get_name(comp_cls
),
216 *fail_reason
= "canceled by user";
223 case BT_QUERY_EXECUTOR_STATUS_CANCELED
:
224 *fail_reason
= "canceled by user";
226 case BT_QUERY_EXECUTOR_STATUS_ERROR
:
228 case BT_QUERY_EXECUTOR_STATUS_INVALID_OBJECT
:
229 *fail_reason
= "invalid or unknown query object";
231 case BT_QUERY_EXECUTOR_STATUS_INVALID_PARAMS
:
232 *fail_reason
= "invalid query parameters";
234 case BT_QUERY_EXECUTOR_STATUS_UNSUPPORTED
:
235 *fail_reason
= "unsupported action";
237 case BT_QUERY_EXECUTOR_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_value_put_ref(result
);
261 const bt_plugin
*find_plugin(const char *name
)
264 const bt_plugin
*plugin
= NULL
;
267 BT_LOGI("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) {
280 BT_LOGI("Found plugin: name=\"%s\", plugin-addr=%p",
283 BT_LOGI("Cannot find plugin: name=\"%s\"", name
);
286 bt_plugin_get_ref(plugin
);
290 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
291 const bt_plugin
*, const char *);
294 const void *find_component_class_from_plugin(const char *plugin_name
,
295 const char *comp_class_name
,
296 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
298 const void *comp_class
= NULL
;
299 const bt_plugin
*plugin
;
301 BT_LOGI("Finding component class: plugin-name=\"%s\", "
302 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
304 plugin
= find_plugin(plugin_name
);
309 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
310 bt_object_get_ref(comp_class
);
311 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
315 BT_LOGI("Found component class: plugin-name=\"%s\", "
316 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
318 BT_LOGI("Cannot find source component class: "
319 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
320 plugin_name
, comp_class_name
);
327 const bt_component_class_source
*find_source_component_class(
328 const char *plugin_name
, const char *comp_class_name
)
330 return (const void *) find_component_class_from_plugin(
331 plugin_name
, comp_class_name
,
332 (plugin_borrow_comp_cls_func_t
)
333 bt_plugin_borrow_source_component_class_by_name_const
);
337 const bt_component_class_filter
*find_filter_component_class(
338 const char *plugin_name
, const char *comp_class_name
)
340 return (const void *) find_component_class_from_plugin(
341 plugin_name
, comp_class_name
,
342 (plugin_borrow_comp_cls_func_t
)
343 bt_plugin_borrow_filter_component_class_by_name_const
);
347 const bt_component_class_sink
*find_sink_component_class(
348 const char *plugin_name
, const char *comp_class_name
)
350 return (const void *) find_component_class_from_plugin(plugin_name
,
352 (plugin_borrow_comp_cls_func_t
)
353 bt_plugin_borrow_sink_component_class_by_name_const
);
357 const bt_component_class
*find_component_class(const char *plugin_name
,
358 const char *comp_class_name
,
359 bt_component_class_type comp_class_type
)
361 const bt_component_class
*comp_cls
= NULL
;
363 switch (comp_class_type
) {
364 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
365 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
367 case BT_COMPONENT_CLASS_TYPE_FILTER
:
368 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
370 case BT_COMPONENT_CLASS_TYPE_SINK
:
371 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
381 void print_indent(FILE *fp
, size_t indent
)
385 for (i
= 0; i
< indent
; i
++) {
391 const char *component_type_str(bt_component_class_type type
)
394 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
396 case BT_COMPONENT_CLASS_TYPE_SINK
:
398 case BT_COMPONENT_CLASS_TYPE_FILTER
:
406 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
407 const char *comp_cls_name
, bt_component_class_type type
)
409 GString
*shell_plugin_name
= NULL
;
410 GString
*shell_comp_cls_name
= NULL
;
412 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
413 if (!shell_plugin_name
) {
417 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
418 if (!shell_comp_cls_name
) {
422 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
423 bt_common_color_bold(),
424 bt_common_color_fg_cyan(),
425 component_type_str(type
),
426 bt_common_color_fg_default(),
427 bt_common_color_fg_blue(),
428 shell_plugin_name
->str
,
429 bt_common_color_fg_default(),
430 bt_common_color_fg_yellow(),
431 shell_comp_cls_name
->str
,
432 bt_common_color_reset());
435 if (shell_plugin_name
) {
436 g_string_free(shell_plugin_name
, TRUE
);
439 if (shell_comp_cls_name
) {
440 g_string_free(shell_comp_cls_name
, TRUE
);
445 void print_value(FILE *, const bt_value
*, size_t);
448 void print_value_rec(FILE *, const bt_value
*, size_t);
450 struct print_map_value_data
{
456 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
459 struct print_map_value_data
*print_map_value_data
= data
;
461 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
462 fprintf(print_map_value_data
->fp
, "%s: ", key
);
465 if (bt_value_is_array(object
) &&
466 bt_value_array_is_empty(object
)) {
467 fprintf(print_map_value_data
->fp
, "[ ]\n");
471 if (bt_value_is_map(object
) &&
472 bt_value_map_is_empty(object
)) {
473 fprintf(print_map_value_data
->fp
, "{ }\n");
477 if (bt_value_is_array(object
) ||
478 bt_value_is_map(object
)) {
479 fprintf(print_map_value_data
->fp
, "\n");
482 print_value_rec(print_map_value_data
->fp
, object
,
483 print_map_value_data
->indent
+ 2);
488 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
502 switch (bt_value_get_type(value
)) {
503 case BT_VALUE_TYPE_NULL
:
504 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
505 bt_common_color_reset());
507 case BT_VALUE_TYPE_BOOL
:
508 bool_val
= bt_value_bool_get(value
);
509 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
510 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
511 bt_common_color_reset());
513 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
514 uint_val
= bt_value_unsigned_integer_get(value
);
515 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
516 bt_common_color_fg_red(), uint_val
,
517 bt_common_color_reset());
519 case BT_VALUE_TYPE_SIGNED_INTEGER
:
520 int_val
= bt_value_signed_integer_get(value
);
521 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
522 bt_common_color_fg_red(), int_val
,
523 bt_common_color_reset());
525 case BT_VALUE_TYPE_REAL
:
526 dbl_val
= bt_value_real_get(value
);
527 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
528 bt_common_color_fg_red(), dbl_val
,
529 bt_common_color_reset());
531 case BT_VALUE_TYPE_STRING
:
532 str_val
= bt_value_string_get(value
);
533 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
534 bt_common_color_fg_green(), str_val
,
535 bt_common_color_reset());
537 case BT_VALUE_TYPE_ARRAY
:
538 size
= bt_value_array_get_size(value
);
544 print_indent(fp
, indent
);
545 fprintf(fp
, "[ ]\n");
549 for (i
= 0; i
< size
; i
++) {
550 const bt_value
*element
=
551 bt_value_array_borrow_element_by_index_const(
557 print_indent(fp
, indent
);
560 if (bt_value_is_array(element
) &&
561 bt_value_array_is_empty(element
)) {
562 fprintf(fp
, "[ ]\n");
566 if (bt_value_is_map(element
) &&
567 bt_value_map_is_empty(element
)) {
568 fprintf(fp
, "{ }\n");
572 if (bt_value_is_array(element
) ||
573 bt_value_is_map(element
)) {
577 print_value_rec(fp
, element
, indent
+ 2);
580 case BT_VALUE_TYPE_MAP
:
582 struct print_map_value_data data
= {
587 if (bt_value_map_is_empty(value
)) {
588 print_indent(fp
, indent
);
589 fprintf(fp
, "{ }\n");
593 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
602 BT_LOGE("Error printing value of type %s.",
603 bt_common_value_type_string(bt_value_get_type(value
)));
607 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
609 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
610 print_indent(fp
, indent
);
613 print_value_rec(fp
, value
, indent
);
617 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
619 fprintf(stderr
, " ");
620 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
621 bt_config_component
->comp_cls_name
->str
,
622 bt_config_component
->type
);
623 fprintf(stderr
, ":\n");
625 if (bt_config_component
->instance_name
->len
> 0) {
626 fprintf(stderr
, " Name: %s\n",
627 bt_config_component
->instance_name
->str
);
630 fprintf(stderr
, " Parameters:\n");
631 print_value(stderr
, bt_config_component
->params
, 8);
635 void print_bt_config_components(GPtrArray
*array
)
639 for (i
= 0; i
< array
->len
; i
++) {
640 struct bt_config_component
*cfg_component
=
641 bt_config_get_component(array
, i
);
642 print_bt_config_component(cfg_component
);
643 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
648 void print_plugin_paths(const bt_value
*plugin_paths
)
650 fprintf(stderr
, " Plugin paths:\n");
651 print_value(stderr
, plugin_paths
, 4);
655 void print_cfg_run(struct bt_config
*cfg
)
659 print_plugin_paths(cfg
->plugin_paths
);
660 fprintf(stderr
, " Source component instances:\n");
661 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
663 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
664 fprintf(stderr
, " Filter component instances:\n");
665 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
668 fprintf(stderr
, " Sink component instances:\n");
669 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
670 fprintf(stderr
, " Connections:\n");
672 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
673 struct bt_config_connection
*cfg_connection
=
674 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
677 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
678 cfg_connection
->upstream_comp_name
->str
,
679 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
680 cfg_connection
->upstream_port_glob
->str
,
681 cfg_connection
->downstream_comp_name
->str
,
682 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
683 cfg_connection
->downstream_port_glob
->str
);
688 void print_cfg_list_plugins(struct bt_config
*cfg
)
690 print_plugin_paths(cfg
->plugin_paths
);
694 void print_cfg_help(struct bt_config
*cfg
)
696 print_plugin_paths(cfg
->plugin_paths
);
700 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
702 print_plugin_paths(cfg
->plugin_paths
);
703 fprintf(stderr
, " Path: %s\n",
704 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
708 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
710 print_plugin_paths(cfg
->plugin_paths
);
711 fprintf(stderr
, " URL: %s\n",
712 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
716 void print_cfg_query(struct bt_config
*cfg
)
718 print_plugin_paths(cfg
->plugin_paths
);
719 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
720 fprintf(stderr
, " Component class:\n");
721 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
725 void print_cfg(struct bt_config
*cfg
)
727 if (!BT_LOG_ON_INFO
) {
731 BT_LOGI_STR("CLI configuration:");
732 BT_LOGI(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
733 BT_LOGI(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
735 switch (cfg
->command
) {
736 case BT_CONFIG_COMMAND_RUN
:
739 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
740 print_cfg_list_plugins(cfg
);
742 case BT_CONFIG_COMMAND_HELP
:
745 case BT_CONFIG_COMMAND_QUERY
:
746 print_cfg_query(cfg
);
748 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
749 print_cfg_print_ctf_metadata(cfg
);
751 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
752 print_cfg_print_lttng_live_sessions(cfg
);
760 void add_to_loaded_plugins(const bt_plugin_set
*plugin_set
)
765 count
= bt_plugin_set_get_plugin_count(plugin_set
);
766 BT_ASSERT(count
>= 0);
768 for (i
= 0; i
< count
; i
++) {
769 const bt_plugin
*plugin
=
770 bt_plugin_set_borrow_plugin_by_index_const(plugin_set
, i
);
771 const bt_plugin
*loaded_plugin
=
772 find_plugin(bt_plugin_get_name(plugin
));
777 BT_LOGI("Not using plugin: another one already exists with the same name: "
778 "plugin-name=\"%s\", plugin-path=\"%s\", "
779 "existing-plugin-path=\"%s\"",
780 bt_plugin_get_name(plugin
),
781 bt_plugin_get_path(plugin
),
782 bt_plugin_get_path(loaded_plugin
));
783 bt_plugin_put_ref(loaded_plugin
);
785 /* Add to global array. */
786 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
787 bt_plugin_get_name(plugin
));
788 bt_plugin_get_ref(plugin
);
789 g_ptr_array_add(loaded_plugins
, (void *) plugin
);
795 int load_dynamic_plugins(const bt_value
*plugin_paths
)
797 int nr_paths
, i
, ret
= 0;
799 nr_paths
= bt_value_array_get_size(plugin_paths
);
801 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
806 BT_LOGI_STR("Loading dynamic plugins.");
808 for (i
= 0; i
< nr_paths
; i
++) {
809 const bt_value
*plugin_path_value
= NULL
;
810 const char *plugin_path
;
811 const bt_plugin_set
*plugin_set
;
814 bt_value_array_borrow_element_by_index_const(
816 plugin_path
= bt_value_string_get(plugin_path_value
);
819 * Skip this if the directory does not exist because
820 * bt_plugin_find_all_from_dir() expects an existing
823 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
824 BT_LOGI("Skipping nonexistent directory path: "
825 "path=\"%s\"", plugin_path
);
829 plugin_set
= bt_plugin_find_all_from_dir(plugin_path
, false);
831 BT_LOGI("Unable to load dynamic plugins from directory: "
832 "path=\"%s\"", plugin_path
);
836 add_to_loaded_plugins(plugin_set
);
837 bt_plugin_set_put_ref(plugin_set
);
844 int load_static_plugins(void)
847 const bt_plugin_set
*plugin_set
;
849 BT_LOGI("Loading static plugins.");
850 plugin_set
= bt_plugin_find_all_from_static();
852 BT_LOGE("Unable to load static plugins.");
857 add_to_loaded_plugins(plugin_set
);
858 bt_plugin_set_put_ref(plugin_set
);
864 int load_all_plugins(const bt_value
*plugin_paths
)
868 if (load_dynamic_plugins(plugin_paths
)) {
873 if (load_static_plugins()) {
878 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
885 void print_plugin_info(const bt_plugin
*plugin
)
887 unsigned int major
, minor
, patch
;
889 bt_property_availability version_avail
;
890 const char *plugin_name
;
894 const char *plugin_description
;
896 plugin_name
= bt_plugin_get_name(plugin
);
897 path
= bt_plugin_get_path(plugin
);
898 author
= bt_plugin_get_author(plugin
);
899 license
= bt_plugin_get_license(plugin
);
900 plugin_description
= bt_plugin_get_description(plugin
);
901 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
903 printf("%s%s%s%s:\n", bt_common_color_bold(),
904 bt_common_color_fg_blue(), plugin_name
,
905 bt_common_color_reset());
907 printf(" %sPath%s: %s\n", bt_common_color_bold(),
908 bt_common_color_reset(), path
);
913 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
914 printf(" %sVersion%s: %u.%u.%u",
915 bt_common_color_bold(), bt_common_color_reset(),
916 major
, minor
, patch
);
925 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
926 bt_common_color_reset(),
927 plugin_description
? plugin_description
: "(None)");
928 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
929 bt_common_color_reset(), author
? author
: "(Unknown)");
930 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
931 bt_common_color_reset(),
932 license
? license
: "(Unknown)");
936 int cmd_query(struct bt_config
*cfg
)
939 const bt_component_class
*comp_cls
= NULL
;
940 const bt_value
*results
= NULL
;
941 const char *fail_reason
= NULL
;
943 comp_cls
= find_component_class(
944 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
945 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
946 cfg
->cmd_data
.query
.cfg_component
->type
);
948 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
949 "comp-cls-name=\"%s\", comp-cls-type=%d",
950 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
951 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
952 cfg
->cmd_data
.query
.cfg_component
->type
);
953 fprintf(stderr
, "%s%sCannot find component class %s",
954 bt_common_color_bold(),
955 bt_common_color_fg_red(),
956 bt_common_color_reset());
957 print_plugin_comp_cls_opt(stderr
,
958 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
959 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
960 cfg
->cmd_data
.query
.cfg_component
->type
);
961 fprintf(stderr
, "\n");
966 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
967 cfg
->cmd_data
.query
.cfg_component
->params
,
968 &results
, &fail_reason
);
973 print_value(stdout
, results
, 0);
977 BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", "
978 "comp-cls-name=\"%s\", comp-cls-type=%d "
979 "object=\"%s\"", fail_reason
,
980 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
981 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
982 cfg
->cmd_data
.query
.cfg_component
->type
,
983 cfg
->cmd_data
.query
.object
->str
);
984 fprintf(stderr
, "%s%sFailed to query info to %s",
985 bt_common_color_bold(),
986 bt_common_color_fg_red(),
987 bt_common_color_reset());
988 print_plugin_comp_cls_opt(stderr
,
989 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
990 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
991 cfg
->cmd_data
.query
.cfg_component
->type
);
992 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
993 bt_common_color_bold(),
994 bt_common_color_fg_red(),
995 cfg
->cmd_data
.query
.object
->str
,
997 bt_common_color_reset());
1001 bt_component_class_put_ref(comp_cls
);
1002 bt_value_put_ref(results
);
1007 void print_component_class_help(const char *plugin_name
,
1008 const bt_component_class
*comp_cls
)
1010 const char *comp_class_name
=
1011 bt_component_class_get_name(comp_cls
);
1012 const char *comp_class_description
=
1013 bt_component_class_get_description(comp_cls
);
1014 const char *comp_class_help
=
1015 bt_component_class_get_help(comp_cls
);
1016 bt_component_class_type type
=
1017 bt_component_class_get_type(comp_cls
);
1019 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
1021 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1022 bt_common_color_reset(),
1023 comp_class_description
? comp_class_description
: "(None)");
1025 if (comp_class_help
) {
1026 printf("\n%s\n", comp_class_help
);
1031 int cmd_help(struct bt_config
*cfg
)
1034 const bt_plugin
*plugin
= NULL
;
1035 const bt_component_class
*needed_comp_cls
= NULL
;
1037 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1039 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
1040 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1041 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
1042 bt_common_color_bold(), bt_common_color_fg_red(),
1043 bt_common_color_fg_blue(),
1044 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1045 bt_common_color_reset());
1050 print_plugin_info(plugin
);
1051 printf(" %sSource component classes%s: %d\n",
1052 bt_common_color_bold(),
1053 bt_common_color_reset(),
1054 (int) bt_plugin_get_source_component_class_count(plugin
));
1055 printf(" %sFilter component classes%s: %d\n",
1056 bt_common_color_bold(),
1057 bt_common_color_reset(),
1058 (int) bt_plugin_get_filter_component_class_count(plugin
));
1059 printf(" %sSink component classes%s: %d\n",
1060 bt_common_color_bold(),
1061 bt_common_color_reset(),
1062 (int) bt_plugin_get_sink_component_class_count(plugin
));
1064 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
1065 /* Plugin help only */
1069 needed_comp_cls
= find_component_class(
1070 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1071 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1072 cfg
->cmd_data
.help
.cfg_component
->type
);
1073 if (!needed_comp_cls
) {
1074 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1075 "comp-cls-name=\"%s\", comp-cls-type=%d",
1076 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1077 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1078 cfg
->cmd_data
.help
.cfg_component
->type
);
1079 fprintf(stderr
, "\n%s%sCannot find component class %s",
1080 bt_common_color_bold(),
1081 bt_common_color_fg_red(),
1082 bt_common_color_reset());
1083 print_plugin_comp_cls_opt(stderr
,
1084 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1085 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1086 cfg
->cmd_data
.help
.cfg_component
->type
);
1087 fprintf(stderr
, "\n");
1093 print_component_class_help(
1094 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1098 bt_component_class_put_ref(needed_comp_cls
);
1099 bt_plugin_put_ref(plugin
);
1103 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
1105 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
1108 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
1109 const char *cc_type_name
, uint64_t count
,
1110 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
1111 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
1116 printf(" %s%s component classes%s: (none)\n",
1117 bt_common_color_bold(),
1119 bt_common_color_reset());
1122 printf(" %s%s component classes%s:\n",
1123 bt_common_color_bold(),
1125 bt_common_color_reset());
1128 for (i
= 0; i
< count
; i
++) {
1129 const bt_component_class
*comp_class
=
1130 spec_comp_cls_borrow_comp_cls_func(
1131 borrow_comp_cls_by_index_func(plugin
, i
));
1132 const char *comp_class_name
=
1133 bt_component_class_get_name(comp_class
);
1134 const char *comp_class_description
=
1135 bt_component_class_get_description(comp_class
);
1136 bt_component_class_type type
=
1137 bt_component_class_get_type(comp_class
);
1140 print_plugin_comp_cls_opt(stdout
,
1141 bt_plugin_get_name(plugin
), comp_class_name
,
1144 if (comp_class_description
) {
1145 printf(": %s", comp_class_description
);
1156 int cmd_list_plugins(struct bt_config
*cfg
)
1159 int plugins_count
, component_classes_count
= 0, i
;
1161 printf("From the following plugin paths:\n\n");
1162 print_value(stdout
, cfg
->plugin_paths
, 2);
1164 plugins_count
= loaded_plugins
->len
;
1165 if (plugins_count
== 0) {
1166 printf("No plugins found.\n");
1170 for (i
= 0; i
< plugins_count
; i
++) {
1171 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1173 component_classes_count
+=
1174 bt_plugin_get_source_component_class_count(plugin
) +
1175 bt_plugin_get_filter_component_class_count(plugin
) +
1176 bt_plugin_get_sink_component_class_count(plugin
);
1179 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1180 bt_common_color_bold(),
1181 component_classes_count
,
1182 bt_common_color_reset(),
1183 bt_common_color_bold(),
1185 bt_common_color_reset());
1187 for (i
= 0; i
< plugins_count
; i
++) {
1188 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1191 print_plugin_info(plugin
);
1192 cmd_list_plugins_print_component_classes(plugin
, "Source",
1193 bt_plugin_get_source_component_class_count(plugin
),
1194 (plugin_borrow_comp_cls_by_index_func_t
)
1195 bt_plugin_borrow_source_component_class_by_index_const
,
1196 (spec_comp_cls_borrow_comp_cls_func_t
)
1197 bt_component_class_source_as_component_class
);
1198 cmd_list_plugins_print_component_classes(plugin
, "Filter",
1199 bt_plugin_get_filter_component_class_count(plugin
),
1200 (plugin_borrow_comp_cls_by_index_func_t
)
1201 bt_plugin_borrow_filter_component_class_by_index_const
,
1202 (spec_comp_cls_borrow_comp_cls_func_t
)
1203 bt_component_class_filter_as_component_class
);
1204 cmd_list_plugins_print_component_classes(plugin
, "Sink",
1205 bt_plugin_get_sink_component_class_count(plugin
),
1206 (plugin_borrow_comp_cls_by_index_func_t
)
1207 bt_plugin_borrow_sink_component_class_by_index_const
,
1208 (spec_comp_cls_borrow_comp_cls_func_t
)
1209 bt_component_class_sink_as_component_class
);
1217 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1220 const bt_component_class
*comp_cls
= NULL
;
1221 const bt_value
*results
= NULL
;
1222 bt_value
*params
= NULL
;
1223 const bt_value
*map
= NULL
;
1224 const bt_value
*v
= NULL
;
1225 static const char * const plugin_name
= "ctf";
1226 static const char * const comp_cls_name
= "lttng-live";
1227 static const bt_component_class_type comp_cls_type
=
1228 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1229 int64_t array_size
, i
;
1230 const char *fail_reason
= NULL
;
1231 FILE *out_stream
= stdout
;
1233 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1234 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1237 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1238 "comp-cls-name=\"%s\", comp-cls-type=%d",
1239 plugin_name
, comp_cls_name
,
1240 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1241 fprintf(stderr
, "%s%sCannot find component class %s",
1242 bt_common_color_bold(),
1243 bt_common_color_fg_red(),
1244 bt_common_color_reset());
1245 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1246 comp_cls_name
, comp_cls_type
);
1247 fprintf(stderr
, "\n");
1251 params
= bt_value_map_create();
1256 ret
= bt_value_map_insert_string_entry(params
, "url",
1257 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1262 ret
= query(cfg
, comp_cls
, "sessions", params
,
1263 &results
, &fail_reason
);
1270 if (!bt_value_is_array(results
)) {
1271 BT_LOGE_STR("Expecting an array for sessions query.");
1272 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1273 bt_common_color_bold(),
1274 bt_common_color_fg_red(),
1275 bt_common_color_reset());
1279 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1281 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1285 BT_LOGE_ERRNO("Cannot open file for writing",
1287 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1292 array_size
= bt_value_array_get_size(results
);
1293 for (i
= 0; i
< array_size
; i
++) {
1294 const char *url_text
;
1295 int64_t timer_us
, streams
, clients
;
1297 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1299 BT_LOGE_STR("Unexpected empty array entry.");
1302 if (!bt_value_is_map(map
)) {
1303 BT_LOGE_STR("Unexpected entry type.");
1307 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1309 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1312 url_text
= bt_value_string_get(v
);
1313 fprintf(out_stream
, "%s", url_text
);
1314 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1316 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1319 timer_us
= bt_value_signed_integer_get(v
);
1320 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1321 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1323 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1326 streams
= bt_value_signed_integer_get(v
);
1327 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1328 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1330 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1333 clients
= bt_value_signed_integer_get(v
);
1334 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1340 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1341 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1342 bt_common_color_bold(),
1343 bt_common_color_fg_red(),
1345 bt_common_color_reset());
1351 bt_value_put_ref(results
);
1352 bt_value_put_ref(params
);
1353 bt_component_class_put_ref(comp_cls
);
1355 if (out_stream
&& out_stream
!= stdout
) {
1356 int fclose_ret
= fclose(out_stream
);
1359 BT_LOGE_ERRNO("Cannot close file stream",
1361 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1369 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1372 const bt_component_class
*comp_cls
= NULL
;
1373 const bt_value
*results
= NULL
;
1374 bt_value
*params
= NULL
;
1375 const bt_value
*metadata_text_value
= NULL
;
1376 const char *metadata_text
= NULL
;
1377 static const char * const plugin_name
= "ctf";
1378 static const char * const comp_cls_name
= "fs";
1379 static const bt_component_class_type comp_cls_type
=
1380 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1381 const char *fail_reason
= NULL
;
1382 FILE *out_stream
= stdout
;
1384 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1385 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1388 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1389 "comp-cls-name=\"%s\", comp-cls-type=%d",
1390 plugin_name
, comp_cls_name
,
1391 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1392 fprintf(stderr
, "%s%sCannot find component class %s",
1393 bt_common_color_bold(),
1394 bt_common_color_fg_red(),
1395 bt_common_color_reset());
1396 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1397 comp_cls_name
, comp_cls_type
);
1398 fprintf(stderr
, "\n");
1403 params
= bt_value_map_create();
1409 ret
= bt_value_map_insert_string_entry(params
, "path",
1410 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1416 ret
= query(cfg
, comp_cls
, "metadata-info",
1417 params
, &results
, &fail_reason
);
1422 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1424 if (!metadata_text_value
) {
1425 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1430 metadata_text
= bt_value_string_get(metadata_text_value
);
1432 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1434 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1438 BT_LOGE_ERRNO("Cannot open file for writing",
1440 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1445 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1447 BT_LOGE("Cannot write whole metadata text to output stream: "
1458 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1459 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1460 bt_common_color_bold(),
1461 bt_common_color_fg_red(),
1463 bt_common_color_reset());
1466 bt_value_put_ref(results
);
1467 bt_value_put_ref(params
);
1468 bt_component_class_put_ref(comp_cls
);
1470 if (out_stream
&& out_stream
!= stdout
) {
1471 int fclose_ret
= fclose(out_stream
);
1474 BT_LOGE_ERRNO("Cannot close file stream",
1476 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1484 char *instance_name
;
1488 struct trace_range
{
1489 uint64_t intersection_range_begin_ns
;
1490 uint64_t intersection_range_end_ns
;
1494 guint
port_id_hash(gconstpointer v
)
1496 const struct port_id
*id
= v
;
1498 BT_ASSERT(id
->instance_name
);
1499 BT_ASSERT(id
->port_name
);
1501 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1505 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1507 const struct port_id
*id1
= v1
;
1508 const struct port_id
*id2
= v2
;
1510 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1511 !strcmp(id1
->port_name
, id2
->port_name
);
1515 void port_id_destroy(gpointer data
)
1517 struct port_id
*id
= data
;
1519 free(id
->instance_name
);
1520 free(id
->port_name
);
1525 void trace_range_destroy(gpointer data
)
1530 struct cmd_run_ctx
{
1532 GHashTable
*src_components
;
1535 GHashTable
*flt_components
;
1538 GHashTable
*sink_components
;
1544 struct bt_config
*cfg
;
1548 bool stream_intersection_mode
;
1551 * Association of struct port_id -> struct trace_range.
1553 GHashTable
*intersections
;
1556 /* Returns a timestamp of the form "(-)s.ns" */
1558 char *s_from_ns(int64_t ns
)
1563 int64_t ts_sec_abs
, ts_nsec_abs
;
1564 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1565 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1567 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1568 is_negative
= false;
1569 ts_sec_abs
= ts_sec
;
1570 ts_nsec_abs
= ts_nsec
;
1571 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1572 is_negative
= false;
1573 ts_sec_abs
= ts_sec
- 1;
1574 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1575 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1577 ts_sec_abs
= ts_sec
;
1578 ts_nsec_abs
= -ts_nsec
;
1579 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1581 ts_sec_abs
= -(ts_sec
+ 1);
1582 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1583 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1585 ts_sec_abs
= -ts_sec
;
1586 ts_nsec_abs
= ts_nsec
;
1587 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1589 ts_sec_abs
= -ts_sec
;
1590 ts_nsec_abs
= -ts_nsec
;
1593 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1594 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1602 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1603 struct cmd_run_ctx
*ctx
,
1604 const bt_component
*upstream_comp
,
1605 const bt_port_output
*out_upstream_port
,
1606 struct bt_config_connection
*cfg_conn
)
1608 typedef uint64_t (*input_port_count_func_t
)(void *);
1609 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1610 const void *, uint64_t);
1611 const bt_port
*upstream_port
=
1612 bt_port_output_as_port_const(out_upstream_port
);
1615 GQuark downstreamp_comp_name_quark
;
1616 void *downstream_comp
;
1617 uint64_t downstream_port_count
;
1619 input_port_count_func_t port_count_fn
;
1620 borrow_input_port_by_index_func_t port_by_index_fn
;
1621 bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1622 bool insert_trimmer
= false;
1623 bt_value
*trimmer_params
= NULL
;
1624 char *intersection_begin
= NULL
;
1625 char *intersection_end
= NULL
;
1626 const bt_component_filter
*trimmer
= NULL
;
1627 const bt_component_class_filter
*trimmer_class
= NULL
;
1628 const bt_port_input
*trimmer_input
= NULL
;
1629 const bt_port_output
*trimmer_output
= NULL
;
1631 if (ctx
->intersections
&&
1632 bt_component_get_class_type(upstream_comp
) ==
1633 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1634 struct trace_range
*range
;
1635 struct port_id port_id
= {
1636 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1637 .port_name
= (char *) bt_port_get_name(upstream_port
)
1640 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1644 range
= (struct trace_range
*) g_hash_table_lookup(
1645 ctx
->intersections
, &port_id
);
1647 bt_value_status status
;
1649 intersection_begin
= s_from_ns(
1650 range
->intersection_range_begin_ns
);
1651 intersection_end
= s_from_ns(
1652 range
->intersection_range_end_ns
);
1653 if (!intersection_begin
|| !intersection_end
) {
1654 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1658 insert_trimmer
= true;
1659 trimmer_params
= bt_value_map_create();
1660 if (!trimmer_params
) {
1664 status
= bt_value_map_insert_string_entry(
1665 trimmer_params
, "begin", intersection_begin
);
1666 if (status
!= BT_VALUE_STATUS_OK
) {
1669 status
= bt_value_map_insert_string_entry(
1671 "end", intersection_end
);
1672 if (status
!= BT_VALUE_STATUS_OK
) {
1677 trimmer_class
= find_filter_component_class("utils", "trimmer");
1678 if (!trimmer_class
) {
1683 BT_LOGI("Connecting upstream port to the next available downstream port: "
1684 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1685 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1686 upstream_port
, bt_port_get_name(upstream_port
),
1687 cfg_conn
->downstream_comp_name
->str
,
1688 cfg_conn
->arg
->str
);
1689 downstreamp_comp_name_quark
= g_quark_from_string(
1690 cfg_conn
->downstream_comp_name
->str
);
1691 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1692 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1693 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1694 port_count_fn
= (input_port_count_func_t
)
1695 bt_component_filter_get_input_port_count
;
1696 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1697 bt_component_filter_borrow_input_port_by_index_const
;
1699 if (!downstream_comp
) {
1700 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1701 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1702 port_count_fn
= (input_port_count_func_t
)
1703 bt_component_sink_get_input_port_count
;
1704 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1705 bt_component_sink_borrow_input_port_by_index_const
;
1708 if (!downstream_comp
) {
1709 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1710 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1711 cfg_conn
->arg
->str
);
1712 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1713 cfg_conn
->arg
->str
);
1717 downstream_port_count
= port_count_fn(downstream_comp
);
1719 for (i
= 0; i
< downstream_port_count
; i
++) {
1720 const bt_port_input
*in_downstream_port
=
1721 port_by_index_fn(downstream_comp
, i
);
1722 const bt_port
*downstream_port
=
1723 bt_port_input_as_port_const(in_downstream_port
);
1724 const char *upstream_port_name
;
1725 const char *downstream_port_name
;
1727 BT_ASSERT(downstream_port
);
1729 /* Skip port if it's already connected. */
1730 if (bt_port_is_connected(downstream_port
)) {
1731 BT_LOGI("Skipping downstream port: already connected: "
1732 "port-addr=%p, port-name=\"%s\"",
1734 bt_port_get_name(downstream_port
));
1738 downstream_port_name
= bt_port_get_name(downstream_port
);
1739 BT_ASSERT(downstream_port_name
);
1740 upstream_port_name
= bt_port_get_name(upstream_port
);
1741 BT_ASSERT(upstream_port_name
);
1743 if (!bt_common_star_glob_match(
1744 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1745 downstream_port_name
, SIZE_MAX
)) {
1749 if (insert_trimmer
) {
1751 * In order to insert the trimmer between the
1752 * two components that were being connected, we
1753 * create a connection configuration entry which
1754 * describes a connection from the trimmer's
1755 * output to the original input that was being
1758 * Hence, the creation of the trimmer will cause
1759 * the graph "new port" listener to establish
1760 * all downstream connections as its output port
1761 * is connected. We will then establish the
1762 * connection between the original upstream
1763 * source and the trimmer.
1765 char *trimmer_name
= NULL
;
1766 bt_graph_status graph_status
;
1768 ret
= asprintf(&trimmer_name
,
1769 "stream-intersection-trimmer-%s",
1770 upstream_port_name
);
1776 ctx
->connect_ports
= false;
1777 graph_status
= bt_graph_add_filter_component(
1778 ctx
->graph
, trimmer_class
, trimmer_name
,
1779 trimmer_params
, ctx
->cfg
->log_level
,
1782 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1788 bt_component_filter_borrow_input_port_by_index_const(
1790 if (!trimmer_input
) {
1794 bt_component_filter_borrow_output_port_by_index_const(
1796 if (!trimmer_output
) {
1801 * Replace the current downstream port by the trimmer's
1804 in_downstream_port
= trimmer_input
;
1806 bt_port_input_as_port_const(in_downstream_port
);
1807 downstream_port_name
= bt_port_get_name(
1809 BT_ASSERT(downstream_port_name
);
1812 /* We have a winner! */
1813 status
= bt_graph_connect_ports(ctx
->graph
,
1814 out_upstream_port
, in_downstream_port
, NULL
);
1815 downstream_port
= NULL
;
1817 case BT_GRAPH_STATUS_OK
:
1819 case BT_GRAPH_STATUS_CANCELED
:
1820 BT_LOGI_STR("Graph was canceled by user.");
1821 status
= BT_GRAPH_STATUS_OK
;
1823 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1824 BT_LOGE("A component refused a connection to one of its ports: "
1825 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1826 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1827 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1828 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1830 upstream_comp
, bt_component_get_name(upstream_comp
),
1831 upstream_port
, bt_port_get_name(upstream_port
),
1832 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1833 downstream_port
, downstream_port_name
,
1834 cfg_conn
->arg
->str
);
1836 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1837 bt_port_get_name(upstream_port
),
1838 downstream_port_name
,
1839 cfg_conn
->arg
->str
);
1842 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1843 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1844 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1845 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1846 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1848 upstream_comp
, bt_component_get_name(upstream_comp
),
1849 upstream_port
, bt_port_get_name(upstream_port
),
1850 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1851 downstream_port
, downstream_port_name
,
1852 cfg_conn
->arg
->str
);
1854 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1855 bt_port_get_name(upstream_port
),
1856 downstream_port_name
,
1857 cfg_conn
->arg
->str
);
1861 BT_LOGI("Connected component ports: "
1862 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1863 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1864 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1865 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1867 upstream_comp
, bt_component_get_name(upstream_comp
),
1868 upstream_port
, bt_port_get_name(upstream_port
),
1869 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1870 downstream_port
, downstream_port_name
,
1871 cfg_conn
->arg
->str
);
1873 if (insert_trimmer
) {
1875 * The first connection, from the source to the trimmer,
1876 * has been done. We now connect the trimmer to the
1877 * original downstream port.
1879 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1881 bt_component_filter_as_component_const(trimmer
),
1882 trimmer_output
, cfg_conn
);
1886 ctx
->connect_ports
= true;
1890 * We found a matching downstream port: the search is
1896 /* No downstream port found */
1897 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1898 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1899 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1900 upstream_port
, bt_port_get_name(upstream_port
),
1901 cfg_conn
->downstream_comp_name
->str
,
1902 cfg_conn
->arg
->str
);
1904 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1905 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1911 free(intersection_begin
);
1912 free(intersection_end
);
1913 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1914 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1915 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1920 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1921 const bt_port_output
*upstream_port
)
1924 const char *upstream_port_name
;
1925 const char *upstream_comp_name
;
1926 const bt_component
*upstream_comp
= NULL
;
1930 BT_ASSERT(upstream_port
);
1931 upstream_port_name
= bt_port_get_name(
1932 bt_port_output_as_port_const(upstream_port
));
1933 BT_ASSERT(upstream_port_name
);
1934 upstream_comp
= bt_port_borrow_component_const(
1935 bt_port_output_as_port_const(upstream_port
));
1936 if (!upstream_comp
) {
1937 BT_LOGW("Upstream port to connect is not part of a component: "
1938 "port-addr=%p, port-name=\"%s\"",
1939 upstream_port
, upstream_port_name
);
1944 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1945 BT_ASSERT(upstream_comp_name
);
1946 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1947 "port-addr=%p, port-name=\"%s\"",
1948 upstream_comp
, upstream_comp_name
,
1949 upstream_port
, upstream_port_name
);
1951 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1952 struct bt_config_connection
*cfg_conn
=
1954 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1956 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1957 upstream_comp_name
)) {
1961 if (!bt_common_star_glob_match(
1962 cfg_conn
->upstream_port_glob
->str
,
1963 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1967 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1968 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1970 BT_LOGE("Cannot connect upstream port: "
1971 "port-addr=%p, port-name=\"%s\"",
1973 upstream_port_name
);
1975 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1978 cfg_conn
->arg
->str
);
1984 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1985 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1986 upstream_port_name
);
1988 "Cannot create connection: upstream port `%s` does not match any connection\n",
1989 upstream_port_name
);
1999 bt_graph_listener_status
2000 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
2001 const bt_port_output
*out_port
)
2003 const bt_component
*comp
;
2004 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
2005 bt_graph_listener_status ret
= BT_GRAPH_LISTENER_STATUS_OK
;
2007 comp
= bt_port_borrow_component_const(port
);
2008 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
2009 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
2010 comp
, comp
? bt_component_get_name(comp
) : "",
2011 port
, bt_port_get_name(port
));
2013 if (!ctx
->connect_ports
) {
2018 BT_LOGW_STR("Port has no component.");
2022 if (bt_port_is_connected(port
)) {
2023 BT_LOGW_STR("Port is already connected.");
2027 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
2028 BT_LOGF_STR("Cannot connect upstream port.");
2029 fprintf(stderr
, "Added port could not be connected: aborting\n");
2030 ret
= BT_GRAPH_LISTENER_STATUS_ERROR
;
2039 bt_graph_listener_status
graph_source_output_port_added_listener(
2040 const bt_component_source
*component
,
2041 const bt_port_output
*port
, void *data
)
2043 return graph_output_port_added_listener(data
, port
);
2047 bt_graph_listener_status
graph_filter_output_port_added_listener(
2048 const bt_component_filter
*component
,
2049 const bt_port_output
*port
, void *data
)
2051 return graph_output_port_added_listener(data
, port
);
2055 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
2061 if (ctx
->src_components
) {
2062 g_hash_table_destroy(ctx
->src_components
);
2063 ctx
->src_components
= NULL
;
2066 if (ctx
->flt_components
) {
2067 g_hash_table_destroy(ctx
->flt_components
);
2068 ctx
->flt_components
= NULL
;
2071 if (ctx
->sink_components
) {
2072 g_hash_table_destroy(ctx
->sink_components
);
2073 ctx
->sink_components
= NULL
;
2076 if (ctx
->intersections
) {
2077 g_hash_table_destroy(ctx
->intersections
);
2078 ctx
->intersections
= NULL
;
2081 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
2087 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2090 bt_graph_status status
;
2093 ctx
->connect_ports
= false;
2094 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
2095 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2096 if (!ctx
->src_components
) {
2100 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
2101 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2102 if (!ctx
->flt_components
) {
2106 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
2107 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2108 if (!ctx
->sink_components
) {
2112 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2113 ctx
->stream_intersection_mode
= true;
2114 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2115 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2116 if (!ctx
->intersections
) {
2121 ctx
->graph
= bt_graph_create();
2126 the_graph
= ctx
->graph
;
2127 status
= bt_graph_add_source_component_output_port_added_listener(
2128 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
2130 if (status
!= BT_GRAPH_STATUS_OK
) {
2131 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2135 status
= bt_graph_add_filter_component_output_port_added_listener(
2136 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
2138 if (status
!= BT_GRAPH_STATUS_OK
) {
2139 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2146 cmd_run_ctx_destroy(ctx
);
2154 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2155 struct bt_config_component
*cfg_comp
,
2156 const bt_component_class_source
*src_comp_cls
)
2160 int64_t trace_count
;
2161 const char *path
= NULL
;
2162 const bt_value
*query_result
= NULL
;
2163 const bt_value
*trace_info
= NULL
;
2164 const bt_value
*intersection_range
= NULL
;
2165 const bt_value
*intersection_begin
= NULL
;
2166 const bt_value
*intersection_end
= NULL
;
2167 const bt_value
*stream_infos
= NULL
;
2168 const bt_value
*stream_info
= NULL
;
2169 struct port_id
*port_id
= NULL
;
2170 struct trace_range
*trace_range
= NULL
;
2171 const char *fail_reason
= NULL
;
2172 const bt_component_class
*comp_cls
=
2173 bt_component_class_source_as_component_class_const(src_comp_cls
);
2175 ret
= query(ctx
->cfg
, comp_cls
, "trace-info",
2176 cfg_comp
->params
, &query_result
,
2179 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2180 "comp-class-name=\"%s\"", fail_reason
,
2181 bt_component_class_get_name(comp_cls
));
2186 BT_ASSERT(query_result
);
2188 if (!bt_value_is_array(query_result
)) {
2189 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2190 "component-class-name=%s",
2191 bt_component_class_get_name(comp_cls
));
2196 trace_count
= bt_value_array_get_size(query_result
);
2197 if (trace_count
< 0) {
2202 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2204 uint64_t stream_idx
;
2205 int64_t stream_count
;
2207 trace_info
= bt_value_array_borrow_element_by_index_const(
2208 query_result
, trace_idx
);
2209 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2211 BT_LOGD_STR("Cannot retrieve trace from query result.");
2215 intersection_range
= bt_value_map_borrow_entry_value_const(
2216 trace_info
, "intersection-range-ns");
2217 if (!intersection_range
) {
2219 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2223 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
2225 if (!intersection_begin
) {
2227 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2231 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
2233 if (!intersection_end
) {
2235 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2239 begin
= bt_value_signed_integer_get(intersection_begin
);
2240 end
= bt_value_signed_integer_get(intersection_end
);
2242 if (begin
< 0 || end
< 0 || end
< begin
) {
2243 BT_LOGW("Invalid trace stream intersection values: "
2244 "intersection-range-ns:begin=%" PRId64
2245 ", intersection-range-ns:end=%" PRId64
,
2251 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
2253 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2255 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2259 stream_count
= bt_value_array_get_size(stream_infos
);
2260 if (stream_count
< 0) {
2265 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2266 const bt_value
*port_name
;
2268 port_id
= g_new0(struct port_id
, 1);
2271 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2274 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2275 if (!port_id
->instance_name
) {
2277 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2281 trace_range
= g_new0(struct trace_range
, 1);
2284 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2287 trace_range
->intersection_range_begin_ns
= begin
;
2288 trace_range
->intersection_range_end_ns
= end
;
2290 stream_info
= bt_value_array_borrow_element_by_index_const(
2291 stream_infos
, stream_idx
);
2292 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2294 BT_LOGE_STR("Cannot retrieve stream informations from trace in query result.");
2298 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2299 if (!port_name
|| !bt_value_is_string(port_name
)) {
2301 BT_LOGE_STR("Cannot retrieve port name in query result.");
2305 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2306 if (!port_id
->port_name
) {
2308 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2312 BT_LOGD("Inserting stream intersection ");
2314 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2324 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2325 bt_common_color_bold(),
2326 bt_common_color_fg_yellow(),
2327 path
? path
: "(unknown)",
2328 bt_common_color_reset());
2330 bt_value_put_ref(query_result
);
2332 g_free(trace_range
);
2337 int cmd_run_ctx_create_components_from_config_components(
2338 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2341 const void *comp_cls
= NULL
;
2342 const void *comp
= NULL
;
2345 for (i
= 0; i
< cfg_components
->len
; i
++) {
2346 struct bt_config_component
*cfg_comp
=
2347 g_ptr_array_index(cfg_components
, i
);
2350 switch (cfg_comp
->type
) {
2351 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2352 comp_cls
= find_source_component_class(
2353 cfg_comp
->plugin_name
->str
,
2354 cfg_comp
->comp_cls_name
->str
);
2356 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2357 comp_cls
= find_filter_component_class(
2358 cfg_comp
->plugin_name
->str
,
2359 cfg_comp
->comp_cls_name
->str
);
2361 case BT_COMPONENT_CLASS_TYPE_SINK
:
2362 comp_cls
= find_sink_component_class(
2363 cfg_comp
->plugin_name
->str
,
2364 cfg_comp
->comp_cls_name
->str
);
2371 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2372 "comp-cls-name=\"%s\", comp-cls-type=%d",
2373 cfg_comp
->plugin_name
->str
,
2374 cfg_comp
->comp_cls_name
->str
,
2376 fprintf(stderr
, "%s%sCannot find component class %s",
2377 bt_common_color_bold(),
2378 bt_common_color_fg_red(),
2379 bt_common_color_reset());
2380 print_plugin_comp_cls_opt(stderr
,
2381 cfg_comp
->plugin_name
->str
,
2382 cfg_comp
->comp_cls_name
->str
,
2384 fprintf(stderr
, "\n");
2388 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_VERBOSE
);
2390 switch (cfg_comp
->type
) {
2391 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2392 ret
= bt_graph_add_source_component(ctx
->graph
,
2393 comp_cls
, cfg_comp
->instance_name
->str
,
2394 cfg_comp
->params
, cfg_comp
->log_level
,
2397 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2398 ret
= bt_graph_add_filter_component(ctx
->graph
,
2399 comp_cls
, cfg_comp
->instance_name
->str
,
2400 cfg_comp
->params
, cfg_comp
->log_level
,
2403 case BT_COMPONENT_CLASS_TYPE_SINK
:
2404 ret
= bt_graph_add_sink_component(ctx
->graph
,
2405 comp_cls
, cfg_comp
->instance_name
->str
,
2406 cfg_comp
->params
, cfg_comp
->log_level
,
2414 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2415 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2417 cfg_comp
->plugin_name
->str
,
2418 cfg_comp
->comp_cls_name
->str
,
2419 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2420 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2421 bt_common_color_bold(),
2422 bt_common_color_fg_red(),
2423 cfg_comp
->instance_name
->str
,
2424 bt_common_color_reset());
2428 if (ctx
->stream_intersection_mode
&&
2429 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2430 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2436 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2437 comp
, cfg_comp
->instance_name
->str
);
2438 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2439 BT_ASSERT(quark
> 0);
2441 switch (cfg_comp
->type
) {
2442 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2443 g_hash_table_insert(ctx
->src_components
,
2444 GUINT_TO_POINTER(quark
), (void *) comp
);
2446 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2447 g_hash_table_insert(ctx
->flt_components
,
2448 GUINT_TO_POINTER(quark
), (void *) comp
);
2450 case BT_COMPONENT_CLASS_TYPE_SINK
:
2451 g_hash_table_insert(ctx
->sink_components
,
2452 GUINT_TO_POINTER(quark
), (void *) comp
);
2459 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2468 bt_object_put_ref(comp
);
2469 bt_object_put_ref(comp_cls
);
2474 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2479 * Make sure that, during this phase, our graph's "port added"
2480 * listener does not connect ports while we are creating the
2481 * components because we have a special, initial phase for
2484 ctx
->connect_ports
= false;
2486 ret
= cmd_run_ctx_create_components_from_config_components(
2487 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2493 ret
= cmd_run_ctx_create_components_from_config_components(
2494 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2500 ret
= cmd_run_ctx_create_components_from_config_components(
2501 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2511 typedef uint64_t (*output_port_count_func_t
)(const void *);
2512 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2513 const void *, uint64_t);
2516 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2517 void *comp
, output_port_count_func_t port_count_fn
,
2518 borrow_output_port_by_index_func_t port_by_index_fn
)
2524 count
= port_count_fn(comp
);
2526 for (i
= 0; i
< count
; i
++) {
2527 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2529 BT_ASSERT(upstream_port
);
2530 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2541 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2544 GHashTableIter iter
;
2545 gpointer g_name_quark
, g_comp
;
2547 ctx
->connect_ports
= true;
2548 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2550 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2551 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2552 (output_port_count_func_t
)
2553 bt_component_source_get_output_port_count
,
2554 (borrow_output_port_by_index_func_t
)
2555 bt_component_source_borrow_output_port_by_index_const
);
2561 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2563 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2564 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2565 (output_port_count_func_t
)
2566 bt_component_filter_get_output_port_count
,
2567 (borrow_output_port_by_index_func_t
)
2568 bt_component_filter_borrow_output_port_by_index_const
);
2579 const char *bt_graph_status_str(bt_graph_status status
)
2582 case BT_GRAPH_STATUS_OK
:
2583 return "BT_GRAPH_STATUS_OK";
2584 case BT_GRAPH_STATUS_END
:
2585 return "BT_GRAPH_STATUS_END";
2586 case BT_GRAPH_STATUS_AGAIN
:
2587 return "BT_GRAPH_STATUS_AGAIN";
2588 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
2589 return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION";
2590 case BT_GRAPH_STATUS_CANCELED
:
2591 return "BT_GRAPH_STATUS_CANCELED";
2592 case BT_GRAPH_STATUS_ERROR
:
2593 return "BT_GRAPH_STATUS_ERROR";
2594 case BT_GRAPH_STATUS_NOMEM
:
2595 return "BT_GRAPH_STATUS_NOMEM";
2602 int cmd_run(struct bt_config
*cfg
)
2605 struct cmd_run_ctx ctx
= { 0 };
2607 /* Initialize the command's context and the graph object */
2608 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2609 BT_LOGE_STR("Cannot initialize the command's context.");
2610 fprintf(stderr
, "Cannot initialize the command's context\n");
2615 BT_LOGI_STR("Canceled by user before creating components.");
2619 BT_LOGI_STR("Creating components.");
2621 /* Create the requested component instances */
2622 if (cmd_run_ctx_create_components(&ctx
)) {
2623 BT_LOGE_STR("Cannot create components.");
2624 fprintf(stderr
, "Cannot create components\n");
2629 BT_LOGI_STR("Canceled by user before connecting components.");
2633 BT_LOGI_STR("Connecting components.");
2635 /* Connect the initially visible component ports */
2636 if (cmd_run_ctx_connect_ports(&ctx
)) {
2637 BT_LOGE_STR("Cannot connect initial component ports.");
2638 fprintf(stderr
, "Cannot connect initial component ports\n");
2643 BT_LOGI_STR("Canceled by user before running the graph.");
2647 BT_LOGI_STR("Running the graph.");
2651 bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2654 * Reset console in case something messed with console
2655 * codes during the graph's execution.
2657 printf("%s", bt_common_color_reset());
2659 fprintf(stderr
, "%s", bt_common_color_reset());
2660 BT_LOGV("bt_graph_run() returned: status=%s",
2661 bt_graph_status_str(graph_status
));
2663 switch (graph_status
) {
2664 case BT_GRAPH_STATUS_OK
:
2666 case BT_GRAPH_STATUS_CANCELED
:
2667 BT_LOGI_STR("Graph was canceled by user.");
2669 case BT_GRAPH_STATUS_AGAIN
:
2670 if (bt_graph_is_canceled(ctx
.graph
)) {
2671 BT_LOGI_STR("Graph was canceled by user.");
2675 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2676 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2678 cfg
->cmd_data
.run
.retry_duration_us
);
2680 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2681 if (bt_graph_is_canceled(ctx
.graph
)) {
2682 BT_LOGI_STR("Graph was canceled by user.");
2688 case BT_GRAPH_STATUS_END
:
2691 BT_LOGE_STR("Graph failed to complete successfully");
2692 fprintf(stderr
, "Graph failed to complete successfully\n");
2705 cmd_run_ctx_destroy(&ctx
);
2710 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2712 const char *env_clash
;
2714 if (!cfg
->command_name
) {
2718 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2719 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2723 if (g_file_test(cfg
->command_name
,
2724 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2725 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2727 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2729 fprintf(stderr
, "\n");
2730 fprintf(stderr
, " babeltrace2 convert %s [OPTIONS]\n",
2736 void init_log_level(void)
2738 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2742 void set_auto_log_levels(struct bt_config
*cfg
)
2744 const char **env_var_name
;
2747 * Override the configuration's default log level if
2748 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2749 * are found for backward compatibility with legacy Babetrace 1.
2751 if (getenv("BABELTRACE_DEBUG") &&
2752 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2753 cfg
->log_level
= BT_LOG_VERBOSE
;
2754 } else if (getenv("BABELTRACE_VERBOSE") &&
2755 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2756 cfg
->log_level
= BT_LOG_INFO
;
2760 * Set log levels according to --debug or --verbose. For
2761 * backward compatibility, --debug is more verbose than
2764 * --verbose: INFO log level
2765 * --debug: VERBOSE log level (includes DEBUG, which is
2766 * is less verbose than VERBOSE in the internal
2767 * logging framework)
2769 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2771 bt_logging_set_global_level(BT_LOG_INFO
);
2772 } else if (cfg
->debug
) {
2773 bt_logging_set_global_level(BT_LOG_VERBOSE
);
2776 * Set library's default log level if not
2777 * explicitly specified.
2779 bt_logging_set_global_level(cfg
->log_level
);
2783 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2785 bt_cli_log_level
= BT_LOG_INFO
;
2786 } else if (cfg
->debug
) {
2787 bt_cli_log_level
= BT_LOG_VERBOSE
;
2790 * Set CLI's default log level if not explicitly
2793 bt_cli_log_level
= cfg
->log_level
;
2797 env_var_name
= log_level_env_var_names
;
2799 while (*env_var_name
) {
2800 if (!getenv(*env_var_name
)) {
2802 g_setenv(*env_var_name
, "I", 1);
2803 } else if (cfg
->debug
) {
2804 g_setenv(*env_var_name
, "V", 1);
2806 char val
[2] = { 0 };
2809 * Set module's default log level if not
2810 * explicitly specified.
2812 val
[0] = bt_log_get_letter_from_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
);