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_TRIMMER_LOG_LEVEL",
54 "BABELTRACE_PLUGIN_CTF_BFCR_LOG_LEVEL",
55 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
56 "BABELTRACE_PLUGIN_CTF_MSG_ITER_LOG_LEVEL",
57 "BABELTRACE_PLUGIN_CTF_UTILS_LOG_LEVEL",
58 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
59 "BABELTRACE_SINK_CTF_FS_LOG_LEVEL",
60 "BABELTRACE_SINK_TEXT_DETAILS_LOG_LEVEL",
61 "BABELTRACE_SRC_CTF_FS_LOG_LEVEL",
62 "BABELTRACE_SRC_CTF_LTTNG_LIVE_LOG_LEVEL",
66 /* Application's processing graph (weak) */
67 static bt_graph
*the_graph
;
68 static bt_query_executor
*the_query_executor
;
69 static bool canceled
= false;
71 GPtrArray
*loaded_plugins
;
78 BOOL WINAPI
signal_handler(DWORD signal
) {
80 bt_graph_cancel(the_graph
);
89 void set_signal_handler(void)
91 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
92 BT_LOGE("Failed to set the Ctrl+C handler.");
96 #else /* __MINGW32__ */
99 void signal_handler(int signum
)
101 if (signum
!= SIGINT
) {
106 bt_graph_cancel(the_graph
);
109 if (the_query_executor
) {
110 bt_query_executor_cancel(the_query_executor
);
117 void set_signal_handler(void)
119 struct sigaction new_action
, old_action
;
121 new_action
.sa_handler
= signal_handler
;
122 sigemptyset(&new_action
.sa_mask
);
123 new_action
.sa_flags
= 0;
124 sigaction(SIGINT
, NULL
, &old_action
);
126 if (old_action
.sa_handler
!= SIG_IGN
) {
127 sigaction(SIGINT
, &new_action
, NULL
);
131 #endif /* __MINGW32__ */
134 void init_static_data(void)
136 loaded_plugins
= g_ptr_array_new_with_free_func(
137 (GDestroyNotify
) bt_object_put_ref
);
141 void fini_static_data(void)
143 g_ptr_array_free(loaded_plugins
, TRUE
);
147 int create_the_query_executor(void)
151 the_query_executor
= bt_query_executor_create();
152 if (!the_query_executor
) {
153 BT_LOGE_STR("Cannot create a query executor.");
161 void destroy_the_query_executor(void)
163 BT_QUERY_EXECUTOR_PUT_REF_AND_RESET(the_query_executor
);
167 int query(struct bt_config
*cfg
, const bt_component_class
*comp_cls
,
168 const char *obj
, const bt_value
*params
,
169 const bt_value
**user_result
, const char **fail_reason
)
171 const bt_value
*result
= NULL
;
172 bt_query_executor_status status
;
173 *fail_reason
= "unknown error";
176 BT_ASSERT(fail_reason
);
177 BT_ASSERT(user_result
);
178 ret
= create_the_query_executor();
180 /* create_the_query_executor() logs errors */
185 BT_LOGI("Canceled by user before executing the query: "
186 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
187 "query-obj=\"%s\"", comp_cls
,
188 bt_component_class_get_name(comp_cls
), obj
);
189 *fail_reason
= "canceled by user";
194 status
= bt_query_executor_query(the_query_executor
,
195 comp_cls
, obj
, params
, cfg
->log_level
, &result
);
197 case BT_QUERY_EXECUTOR_STATUS_OK
:
199 case BT_QUERY_EXECUTOR_STATUS_AGAIN
:
201 const uint64_t sleep_time_us
= 100000;
203 /* Wait 100 ms and retry */
204 BT_LOGD("Got BT_QUERY_EXECUTOR_STATUS_AGAIN: sleeping: "
205 "time-us=%" PRIu64
, sleep_time_us
);
207 if (usleep(sleep_time_us
)) {
208 if (bt_query_executor_is_canceled(the_query_executor
)) {
209 BT_LOGI("Query was canceled by user: "
210 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
211 "query-obj=\"%s\"", comp_cls
,
212 bt_component_class_get_name(comp_cls
),
214 *fail_reason
= "canceled by user";
221 case BT_QUERY_EXECUTOR_STATUS_CANCELED
:
222 *fail_reason
= "canceled by user";
224 case BT_QUERY_EXECUTOR_STATUS_ERROR
:
226 case BT_QUERY_EXECUTOR_STATUS_INVALID_OBJECT
:
227 *fail_reason
= "invalid or unknown query object";
229 case BT_QUERY_EXECUTOR_STATUS_INVALID_PARAMS
:
230 *fail_reason
= "invalid query parameters";
232 case BT_QUERY_EXECUTOR_STATUS_UNSUPPORTED
:
233 *fail_reason
= "unsupported action";
235 case BT_QUERY_EXECUTOR_STATUS_NOMEM
:
236 *fail_reason
= "not enough memory";
239 BT_LOGF("Unknown query status: status=%d", status
);
245 *user_result
= result
;
253 destroy_the_query_executor();
254 bt_value_put_ref(result
);
259 const bt_plugin
*find_plugin(const char *name
)
262 const bt_plugin
*plugin
= NULL
;
265 BT_LOGI("Finding plugin: name=\"%s\"", name
);
267 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
268 plugin
= g_ptr_array_index(loaded_plugins
, i
);
270 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
278 BT_LOGI("Found plugin: name=\"%s\", plugin-addr=%p",
281 BT_LOGI("Cannot find plugin: name=\"%s\"", name
);
284 bt_plugin_get_ref(plugin
);
288 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
289 const bt_plugin
*, const char *);
292 const void *find_component_class_from_plugin(const char *plugin_name
,
293 const char *comp_class_name
,
294 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
296 const void *comp_class
= NULL
;
297 const bt_plugin
*plugin
;
299 BT_LOGI("Finding component class: plugin-name=\"%s\", "
300 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
302 plugin
= find_plugin(plugin_name
);
307 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
308 bt_object_get_ref(comp_class
);
309 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
313 BT_LOGI("Found component class: plugin-name=\"%s\", "
314 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
316 BT_LOGI("Cannot find source component class: "
317 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
318 plugin_name
, comp_class_name
);
325 const bt_component_class_source
*find_source_component_class(
326 const char *plugin_name
, const char *comp_class_name
)
328 return (const void *) find_component_class_from_plugin(
329 plugin_name
, comp_class_name
,
330 (plugin_borrow_comp_cls_func_t
)
331 bt_plugin_borrow_source_component_class_by_name_const
);
335 const bt_component_class_filter
*find_filter_component_class(
336 const char *plugin_name
, const char *comp_class_name
)
338 return (const void *) find_component_class_from_plugin(
339 plugin_name
, comp_class_name
,
340 (plugin_borrow_comp_cls_func_t
)
341 bt_plugin_borrow_filter_component_class_by_name_const
);
345 const bt_component_class_sink
*find_sink_component_class(
346 const char *plugin_name
, const char *comp_class_name
)
348 return (const void *) find_component_class_from_plugin(plugin_name
,
350 (plugin_borrow_comp_cls_func_t
)
351 bt_plugin_borrow_sink_component_class_by_name_const
);
355 const bt_component_class
*find_component_class(const char *plugin_name
,
356 const char *comp_class_name
,
357 bt_component_class_type comp_class_type
)
359 const bt_component_class
*comp_cls
= NULL
;
361 switch (comp_class_type
) {
362 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
363 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
365 case BT_COMPONENT_CLASS_TYPE_FILTER
:
366 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
368 case BT_COMPONENT_CLASS_TYPE_SINK
:
369 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
379 void print_indent(FILE *fp
, size_t indent
)
383 for (i
= 0; i
< indent
; i
++) {
389 const char *component_type_str(bt_component_class_type type
)
392 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
394 case BT_COMPONENT_CLASS_TYPE_SINK
:
396 case BT_COMPONENT_CLASS_TYPE_FILTER
:
404 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
405 const char *comp_cls_name
, bt_component_class_type type
)
407 GString
*shell_plugin_name
= NULL
;
408 GString
*shell_comp_cls_name
= NULL
;
410 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
411 if (!shell_plugin_name
) {
415 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
416 if (!shell_comp_cls_name
) {
420 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
421 bt_common_color_bold(),
422 bt_common_color_fg_cyan(),
423 component_type_str(type
),
424 bt_common_color_fg_default(),
425 bt_common_color_fg_blue(),
426 shell_plugin_name
->str
,
427 bt_common_color_fg_default(),
428 bt_common_color_fg_yellow(),
429 shell_comp_cls_name
->str
,
430 bt_common_color_reset());
433 if (shell_plugin_name
) {
434 g_string_free(shell_plugin_name
, TRUE
);
437 if (shell_comp_cls_name
) {
438 g_string_free(shell_comp_cls_name
, TRUE
);
443 void print_value(FILE *, const bt_value
*, size_t);
446 void print_value_rec(FILE *, const bt_value
*, size_t);
448 struct print_map_value_data
{
454 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
457 struct print_map_value_data
*print_map_value_data
= data
;
459 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
460 fprintf(print_map_value_data
->fp
, "%s: ", key
);
463 if (bt_value_is_array(object
) &&
464 bt_value_array_is_empty(object
)) {
465 fprintf(print_map_value_data
->fp
, "[ ]\n");
469 if (bt_value_is_map(object
) &&
470 bt_value_map_is_empty(object
)) {
471 fprintf(print_map_value_data
->fp
, "{ }\n");
475 if (bt_value_is_array(object
) ||
476 bt_value_is_map(object
)) {
477 fprintf(print_map_value_data
->fp
, "\n");
480 print_value_rec(print_map_value_data
->fp
, object
,
481 print_map_value_data
->indent
+ 2);
486 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
500 switch (bt_value_get_type(value
)) {
501 case BT_VALUE_TYPE_NULL
:
502 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
503 bt_common_color_reset());
505 case BT_VALUE_TYPE_BOOL
:
506 bool_val
= bt_value_bool_get(value
);
507 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
508 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
509 bt_common_color_reset());
511 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
512 uint_val
= bt_value_unsigned_integer_get(value
);
513 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
514 bt_common_color_fg_red(), uint_val
,
515 bt_common_color_reset());
517 case BT_VALUE_TYPE_SIGNED_INTEGER
:
518 int_val
= bt_value_signed_integer_get(value
);
519 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
520 bt_common_color_fg_red(), int_val
,
521 bt_common_color_reset());
523 case BT_VALUE_TYPE_REAL
:
524 dbl_val
= bt_value_real_get(value
);
525 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
526 bt_common_color_fg_red(), dbl_val
,
527 bt_common_color_reset());
529 case BT_VALUE_TYPE_STRING
:
530 str_val
= bt_value_string_get(value
);
531 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
532 bt_common_color_fg_green(), str_val
,
533 bt_common_color_reset());
535 case BT_VALUE_TYPE_ARRAY
:
536 size
= bt_value_array_get_size(value
);
542 print_indent(fp
, indent
);
543 fprintf(fp
, "[ ]\n");
547 for (i
= 0; i
< size
; i
++) {
548 const bt_value
*element
=
549 bt_value_array_borrow_element_by_index_const(
555 print_indent(fp
, indent
);
558 if (bt_value_is_array(element
) &&
559 bt_value_array_is_empty(element
)) {
560 fprintf(fp
, "[ ]\n");
564 if (bt_value_is_map(element
) &&
565 bt_value_map_is_empty(element
)) {
566 fprintf(fp
, "{ }\n");
570 if (bt_value_is_array(element
) ||
571 bt_value_is_map(element
)) {
575 print_value_rec(fp
, element
, indent
+ 2);
578 case BT_VALUE_TYPE_MAP
:
580 struct print_map_value_data data
= {
585 if (bt_value_map_is_empty(value
)) {
586 print_indent(fp
, indent
);
587 fprintf(fp
, "{ }\n");
591 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
600 BT_LOGE("Error printing value of type %s.",
601 bt_common_value_type_string(bt_value_get_type(value
)));
605 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
607 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
608 print_indent(fp
, indent
);
611 print_value_rec(fp
, value
, indent
);
615 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
617 fprintf(stderr
, " ");
618 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
619 bt_config_component
->comp_cls_name
->str
,
620 bt_config_component
->type
);
621 fprintf(stderr
, ":\n");
623 if (bt_config_component
->instance_name
->len
> 0) {
624 fprintf(stderr
, " Name: %s\n",
625 bt_config_component
->instance_name
->str
);
628 fprintf(stderr
, " Parameters:\n");
629 print_value(stderr
, bt_config_component
->params
, 8);
633 void print_bt_config_components(GPtrArray
*array
)
637 for (i
= 0; i
< array
->len
; i
++) {
638 struct bt_config_component
*cfg_component
=
639 bt_config_get_component(array
, i
);
640 print_bt_config_component(cfg_component
);
641 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
646 void print_plugin_paths(const bt_value
*plugin_paths
)
648 fprintf(stderr
, " Plugin paths:\n");
649 print_value(stderr
, plugin_paths
, 4);
653 void print_cfg_run(struct bt_config
*cfg
)
657 print_plugin_paths(cfg
->plugin_paths
);
658 fprintf(stderr
, " Source component instances:\n");
659 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
661 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
662 fprintf(stderr
, " Filter component instances:\n");
663 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
666 fprintf(stderr
, " Sink component instances:\n");
667 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
668 fprintf(stderr
, " Connections:\n");
670 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
671 struct bt_config_connection
*cfg_connection
=
672 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
675 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
676 cfg_connection
->upstream_comp_name
->str
,
677 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
678 cfg_connection
->upstream_port_glob
->str
,
679 cfg_connection
->downstream_comp_name
->str
,
680 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
681 cfg_connection
->downstream_port_glob
->str
);
686 void print_cfg_list_plugins(struct bt_config
*cfg
)
688 print_plugin_paths(cfg
->plugin_paths
);
692 void print_cfg_help(struct bt_config
*cfg
)
694 print_plugin_paths(cfg
->plugin_paths
);
698 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
700 print_plugin_paths(cfg
->plugin_paths
);
701 fprintf(stderr
, " Path: %s\n",
702 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
706 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
708 print_plugin_paths(cfg
->plugin_paths
);
709 fprintf(stderr
, " URL: %s\n",
710 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
714 void print_cfg_query(struct bt_config
*cfg
)
716 print_plugin_paths(cfg
->plugin_paths
);
717 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
718 fprintf(stderr
, " Component class:\n");
719 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
723 void print_cfg(struct bt_config
*cfg
)
725 if (!BT_LOG_ON_INFO
) {
729 BT_LOGI_STR("CLI configuration:");
730 BT_LOGI(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
731 BT_LOGI(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
733 switch (cfg
->command
) {
734 case BT_CONFIG_COMMAND_RUN
:
737 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
738 print_cfg_list_plugins(cfg
);
740 case BT_CONFIG_COMMAND_HELP
:
743 case BT_CONFIG_COMMAND_QUERY
:
744 print_cfg_query(cfg
);
746 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
747 print_cfg_print_ctf_metadata(cfg
);
749 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
750 print_cfg_print_lttng_live_sessions(cfg
);
758 void add_to_loaded_plugins(const bt_plugin_set
*plugin_set
)
763 count
= bt_plugin_set_get_plugin_count(plugin_set
);
764 BT_ASSERT(count
>= 0);
766 for (i
= 0; i
< count
; i
++) {
767 const bt_plugin
*plugin
=
768 bt_plugin_set_borrow_plugin_by_index_const(plugin_set
, i
);
769 const bt_plugin
*loaded_plugin
=
770 find_plugin(bt_plugin_get_name(plugin
));
775 BT_LOGI("Not using plugin: another one already exists with the same name: "
776 "plugin-name=\"%s\", plugin-path=\"%s\", "
777 "existing-plugin-path=\"%s\"",
778 bt_plugin_get_name(plugin
),
779 bt_plugin_get_path(plugin
),
780 bt_plugin_get_path(loaded_plugin
));
781 bt_plugin_put_ref(loaded_plugin
);
783 /* Add to global array. */
784 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
785 bt_plugin_get_name(plugin
));
786 bt_plugin_get_ref(plugin
);
787 g_ptr_array_add(loaded_plugins
, (void *) plugin
);
793 int load_dynamic_plugins(const bt_value
*plugin_paths
)
795 int nr_paths
, i
, ret
= 0;
797 nr_paths
= bt_value_array_get_size(plugin_paths
);
799 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
804 BT_LOGI_STR("Loading dynamic plugins.");
806 for (i
= 0; i
< nr_paths
; i
++) {
807 const bt_value
*plugin_path_value
= NULL
;
808 const char *plugin_path
;
809 const bt_plugin_set
*plugin_set
;
812 bt_value_array_borrow_element_by_index_const(
814 plugin_path
= bt_value_string_get(plugin_path_value
);
817 * Skip this if the directory does not exist because
818 * bt_plugin_find_all_from_dir() expects an existing
821 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
822 BT_LOGI("Skipping nonexistent directory path: "
823 "path=\"%s\"", plugin_path
);
827 plugin_set
= bt_plugin_find_all_from_dir(plugin_path
, false);
829 BT_LOGI("Unable to load dynamic plugins from directory: "
830 "path=\"%s\"", plugin_path
);
834 add_to_loaded_plugins(plugin_set
);
835 bt_plugin_set_put_ref(plugin_set
);
842 int load_static_plugins(void)
845 const bt_plugin_set
*plugin_set
;
847 BT_LOGI("Loading static plugins.");
848 plugin_set
= bt_plugin_find_all_from_static();
850 BT_LOGE("Unable to load static plugins.");
855 add_to_loaded_plugins(plugin_set
);
856 bt_plugin_set_put_ref(plugin_set
);
862 int load_all_plugins(const bt_value
*plugin_paths
)
866 if (load_dynamic_plugins(plugin_paths
)) {
871 if (load_static_plugins()) {
876 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
883 void print_plugin_info(const bt_plugin
*plugin
)
885 unsigned int major
, minor
, patch
;
887 bt_property_availability version_avail
;
888 const char *plugin_name
;
892 const char *plugin_description
;
894 plugin_name
= bt_plugin_get_name(plugin
);
895 path
= bt_plugin_get_path(plugin
);
896 author
= bt_plugin_get_author(plugin
);
897 license
= bt_plugin_get_license(plugin
);
898 plugin_description
= bt_plugin_get_description(plugin
);
899 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
901 printf("%s%s%s%s:\n", bt_common_color_bold(),
902 bt_common_color_fg_blue(), plugin_name
,
903 bt_common_color_reset());
905 printf(" %sPath%s: %s\n", bt_common_color_bold(),
906 bt_common_color_reset(), path
);
911 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
912 printf(" %sVersion%s: %u.%u.%u",
913 bt_common_color_bold(), bt_common_color_reset(),
914 major
, minor
, patch
);
923 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
924 bt_common_color_reset(),
925 plugin_description
? plugin_description
: "(None)");
926 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
927 bt_common_color_reset(), author
? author
: "(Unknown)");
928 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
929 bt_common_color_reset(),
930 license
? license
: "(Unknown)");
934 int cmd_query(struct bt_config
*cfg
)
937 const bt_component_class
*comp_cls
= NULL
;
938 const bt_value
*results
= NULL
;
939 const char *fail_reason
= NULL
;
941 comp_cls
= find_component_class(
942 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
943 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
944 cfg
->cmd_data
.query
.cfg_component
->type
);
946 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
947 "comp-cls-name=\"%s\", comp-cls-type=%d",
948 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
949 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
950 cfg
->cmd_data
.query
.cfg_component
->type
);
951 fprintf(stderr
, "%s%sCannot find component class %s",
952 bt_common_color_bold(),
953 bt_common_color_fg_red(),
954 bt_common_color_reset());
955 print_plugin_comp_cls_opt(stderr
,
956 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
957 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
958 cfg
->cmd_data
.query
.cfg_component
->type
);
959 fprintf(stderr
, "\n");
964 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
965 cfg
->cmd_data
.query
.cfg_component
->params
,
966 &results
, &fail_reason
);
971 print_value(stdout
, results
, 0);
975 BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", "
976 "comp-cls-name=\"%s\", comp-cls-type=%d "
977 "object=\"%s\"", fail_reason
,
978 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
979 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
980 cfg
->cmd_data
.query
.cfg_component
->type
,
981 cfg
->cmd_data
.query
.object
->str
);
982 fprintf(stderr
, "%s%sFailed to query info to %s",
983 bt_common_color_bold(),
984 bt_common_color_fg_red(),
985 bt_common_color_reset());
986 print_plugin_comp_cls_opt(stderr
,
987 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
988 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
989 cfg
->cmd_data
.query
.cfg_component
->type
);
990 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
991 bt_common_color_bold(),
992 bt_common_color_fg_red(),
993 cfg
->cmd_data
.query
.object
->str
,
995 bt_common_color_reset());
999 bt_component_class_put_ref(comp_cls
);
1000 bt_value_put_ref(results
);
1005 void print_component_class_help(const char *plugin_name
,
1006 const bt_component_class
*comp_cls
)
1008 const char *comp_class_name
=
1009 bt_component_class_get_name(comp_cls
);
1010 const char *comp_class_description
=
1011 bt_component_class_get_description(comp_cls
);
1012 const char *comp_class_help
=
1013 bt_component_class_get_help(comp_cls
);
1014 bt_component_class_type type
=
1015 bt_component_class_get_type(comp_cls
);
1017 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
1019 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1020 bt_common_color_reset(),
1021 comp_class_description
? comp_class_description
: "(None)");
1023 if (comp_class_help
) {
1024 printf("\n%s\n", comp_class_help
);
1029 int cmd_help(struct bt_config
*cfg
)
1032 const bt_plugin
*plugin
= NULL
;
1033 const bt_component_class
*needed_comp_cls
= NULL
;
1035 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1037 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
1038 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1039 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
1040 bt_common_color_bold(), bt_common_color_fg_red(),
1041 bt_common_color_fg_blue(),
1042 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1043 bt_common_color_reset());
1048 print_plugin_info(plugin
);
1049 printf(" %sSource component classes%s: %d\n",
1050 bt_common_color_bold(),
1051 bt_common_color_reset(),
1052 (int) bt_plugin_get_source_component_class_count(plugin
));
1053 printf(" %sFilter component classes%s: %d\n",
1054 bt_common_color_bold(),
1055 bt_common_color_reset(),
1056 (int) bt_plugin_get_filter_component_class_count(plugin
));
1057 printf(" %sSink component classes%s: %d\n",
1058 bt_common_color_bold(),
1059 bt_common_color_reset(),
1060 (int) bt_plugin_get_sink_component_class_count(plugin
));
1062 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
1063 /* Plugin help only */
1067 needed_comp_cls
= find_component_class(
1068 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1069 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1070 cfg
->cmd_data
.help
.cfg_component
->type
);
1071 if (!needed_comp_cls
) {
1072 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1073 "comp-cls-name=\"%s\", comp-cls-type=%d",
1074 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1075 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1076 cfg
->cmd_data
.help
.cfg_component
->type
);
1077 fprintf(stderr
, "\n%s%sCannot find component class %s",
1078 bt_common_color_bold(),
1079 bt_common_color_fg_red(),
1080 bt_common_color_reset());
1081 print_plugin_comp_cls_opt(stderr
,
1082 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1083 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1084 cfg
->cmd_data
.help
.cfg_component
->type
);
1085 fprintf(stderr
, "\n");
1091 print_component_class_help(
1092 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1096 bt_component_class_put_ref(needed_comp_cls
);
1097 bt_plugin_put_ref(plugin
);
1101 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
1103 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
1106 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
1107 const char *cc_type_name
, uint64_t count
,
1108 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
1109 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
1114 printf(" %s%s component classes%s: (none)\n",
1115 bt_common_color_bold(),
1117 bt_common_color_reset());
1120 printf(" %s%s component classes%s:\n",
1121 bt_common_color_bold(),
1123 bt_common_color_reset());
1126 for (i
= 0; i
< count
; i
++) {
1127 const bt_component_class
*comp_class
=
1128 spec_comp_cls_borrow_comp_cls_func(
1129 borrow_comp_cls_by_index_func(plugin
, i
));
1130 const char *comp_class_name
=
1131 bt_component_class_get_name(comp_class
);
1132 const char *comp_class_description
=
1133 bt_component_class_get_description(comp_class
);
1134 bt_component_class_type type
=
1135 bt_component_class_get_type(comp_class
);
1138 print_plugin_comp_cls_opt(stdout
,
1139 bt_plugin_get_name(plugin
), comp_class_name
,
1142 if (comp_class_description
) {
1143 printf(": %s", comp_class_description
);
1154 int cmd_list_plugins(struct bt_config
*cfg
)
1157 int plugins_count
, component_classes_count
= 0, i
;
1159 printf("From the following plugin paths:\n\n");
1160 print_value(stdout
, cfg
->plugin_paths
, 2);
1162 plugins_count
= loaded_plugins
->len
;
1163 if (plugins_count
== 0) {
1164 printf("No plugins found.\n");
1168 for (i
= 0; i
< plugins_count
; i
++) {
1169 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1171 component_classes_count
+=
1172 bt_plugin_get_source_component_class_count(plugin
) +
1173 bt_plugin_get_filter_component_class_count(plugin
) +
1174 bt_plugin_get_sink_component_class_count(plugin
);
1177 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1178 bt_common_color_bold(),
1179 component_classes_count
,
1180 bt_common_color_reset(),
1181 bt_common_color_bold(),
1183 bt_common_color_reset());
1185 for (i
= 0; i
< plugins_count
; i
++) {
1186 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1189 print_plugin_info(plugin
);
1190 cmd_list_plugins_print_component_classes(plugin
, "Source",
1191 bt_plugin_get_source_component_class_count(plugin
),
1192 (plugin_borrow_comp_cls_by_index_func_t
)
1193 bt_plugin_borrow_source_component_class_by_index_const
,
1194 (spec_comp_cls_borrow_comp_cls_func_t
)
1195 bt_component_class_source_as_component_class
);
1196 cmd_list_plugins_print_component_classes(plugin
, "Filter",
1197 bt_plugin_get_filter_component_class_count(plugin
),
1198 (plugin_borrow_comp_cls_by_index_func_t
)
1199 bt_plugin_borrow_filter_component_class_by_index_const
,
1200 (spec_comp_cls_borrow_comp_cls_func_t
)
1201 bt_component_class_filter_as_component_class
);
1202 cmd_list_plugins_print_component_classes(plugin
, "Sink",
1203 bt_plugin_get_sink_component_class_count(plugin
),
1204 (plugin_borrow_comp_cls_by_index_func_t
)
1205 bt_plugin_borrow_sink_component_class_by_index_const
,
1206 (spec_comp_cls_borrow_comp_cls_func_t
)
1207 bt_component_class_sink_as_component_class
);
1215 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1218 const bt_component_class
*comp_cls
= NULL
;
1219 const bt_value
*results
= NULL
;
1220 bt_value
*params
= NULL
;
1221 const bt_value
*map
= NULL
;
1222 const bt_value
*v
= NULL
;
1223 static const char * const plugin_name
= "ctf";
1224 static const char * const comp_cls_name
= "lttng-live";
1225 static const bt_component_class_type comp_cls_type
=
1226 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1227 int64_t array_size
, i
;
1228 const char *fail_reason
= NULL
;
1229 FILE *out_stream
= stdout
;
1231 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1232 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1235 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1236 "comp-cls-name=\"%s\", comp-cls-type=%d",
1237 plugin_name
, comp_cls_name
,
1238 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1239 fprintf(stderr
, "%s%sCannot find component class %s",
1240 bt_common_color_bold(),
1241 bt_common_color_fg_red(),
1242 bt_common_color_reset());
1243 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1244 comp_cls_name
, comp_cls_type
);
1245 fprintf(stderr
, "\n");
1249 params
= bt_value_map_create();
1254 ret
= bt_value_map_insert_string_entry(params
, "url",
1255 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1260 ret
= query(cfg
, comp_cls
, "sessions", params
,
1261 &results
, &fail_reason
);
1268 if (!bt_value_is_array(results
)) {
1269 BT_LOGE_STR("Expecting an array for sessions query.");
1270 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1271 bt_common_color_bold(),
1272 bt_common_color_fg_red(),
1273 bt_common_color_reset());
1277 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1279 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1283 BT_LOGE_ERRNO("Cannot open file for writing",
1285 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1290 array_size
= bt_value_array_get_size(results
);
1291 for (i
= 0; i
< array_size
; i
++) {
1292 const char *url_text
;
1293 int64_t timer_us
, streams
, clients
;
1295 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1297 BT_LOGE_STR("Unexpected empty array entry.");
1300 if (!bt_value_is_map(map
)) {
1301 BT_LOGE_STR("Unexpected entry type.");
1305 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1307 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1310 url_text
= bt_value_string_get(v
);
1311 fprintf(out_stream
, "%s", url_text
);
1312 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1314 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1317 timer_us
= bt_value_signed_integer_get(v
);
1318 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1319 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1321 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1324 streams
= bt_value_signed_integer_get(v
);
1325 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1326 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1328 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1331 clients
= bt_value_signed_integer_get(v
);
1332 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1338 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1339 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1340 bt_common_color_bold(),
1341 bt_common_color_fg_red(),
1343 bt_common_color_reset());
1349 bt_value_put_ref(results
);
1350 bt_value_put_ref(params
);
1351 bt_component_class_put_ref(comp_cls
);
1353 if (out_stream
&& out_stream
!= stdout
) {
1354 int fclose_ret
= fclose(out_stream
);
1357 BT_LOGE_ERRNO("Cannot close file stream",
1359 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1367 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1370 const bt_component_class
*comp_cls
= NULL
;
1371 const bt_value
*results
= NULL
;
1372 bt_value
*params
= NULL
;
1373 const bt_value
*metadata_text_value
= NULL
;
1374 const char *metadata_text
= NULL
;
1375 static const char * const plugin_name
= "ctf";
1376 static const char * const comp_cls_name
= "fs";
1377 static const bt_component_class_type comp_cls_type
=
1378 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1379 const char *fail_reason
= NULL
;
1380 FILE *out_stream
= stdout
;
1382 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1383 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1386 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1387 "comp-cls-name=\"%s\", comp-cls-type=%d",
1388 plugin_name
, comp_cls_name
,
1389 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1390 fprintf(stderr
, "%s%sCannot find component class %s",
1391 bt_common_color_bold(),
1392 bt_common_color_fg_red(),
1393 bt_common_color_reset());
1394 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1395 comp_cls_name
, comp_cls_type
);
1396 fprintf(stderr
, "\n");
1401 params
= bt_value_map_create();
1407 ret
= bt_value_map_insert_string_entry(params
, "path",
1408 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1414 ret
= query(cfg
, comp_cls
, "metadata-info",
1415 params
, &results
, &fail_reason
);
1420 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1422 if (!metadata_text_value
) {
1423 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1428 metadata_text
= bt_value_string_get(metadata_text_value
);
1430 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1432 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1436 BT_LOGE_ERRNO("Cannot open file for writing",
1438 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1443 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1445 BT_LOGE("Cannot write whole metadata text to output stream: "
1456 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1457 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1458 bt_common_color_bold(),
1459 bt_common_color_fg_red(),
1461 bt_common_color_reset());
1464 bt_value_put_ref(results
);
1465 bt_value_put_ref(params
);
1466 bt_component_class_put_ref(comp_cls
);
1468 if (out_stream
&& out_stream
!= stdout
) {
1469 int fclose_ret
= fclose(out_stream
);
1472 BT_LOGE_ERRNO("Cannot close file stream",
1474 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1482 char *instance_name
;
1486 struct trace_range
{
1487 uint64_t intersection_range_begin_ns
;
1488 uint64_t intersection_range_end_ns
;
1492 guint
port_id_hash(gconstpointer v
)
1494 const struct port_id
*id
= v
;
1496 BT_ASSERT(id
->instance_name
);
1497 BT_ASSERT(id
->port_name
);
1499 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1503 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1505 const struct port_id
*id1
= v1
;
1506 const struct port_id
*id2
= v2
;
1508 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1509 !strcmp(id1
->port_name
, id2
->port_name
);
1513 void port_id_destroy(gpointer data
)
1515 struct port_id
*id
= data
;
1517 free(id
->instance_name
);
1518 free(id
->port_name
);
1523 void trace_range_destroy(gpointer data
)
1528 struct cmd_run_ctx
{
1530 GHashTable
*src_components
;
1533 GHashTable
*flt_components
;
1536 GHashTable
*sink_components
;
1542 struct bt_config
*cfg
;
1546 bool stream_intersection_mode
;
1549 * Association of struct port_id -> struct trace_range.
1551 GHashTable
*intersections
;
1554 /* Returns a timestamp of the form "(-)s.ns" */
1556 char *s_from_ns(int64_t ns
)
1561 int64_t ts_sec_abs
, ts_nsec_abs
;
1562 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1563 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1565 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1566 is_negative
= false;
1567 ts_sec_abs
= ts_sec
;
1568 ts_nsec_abs
= ts_nsec
;
1569 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1570 is_negative
= false;
1571 ts_sec_abs
= ts_sec
- 1;
1572 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1573 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1575 ts_sec_abs
= ts_sec
;
1576 ts_nsec_abs
= -ts_nsec
;
1577 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1579 ts_sec_abs
= -(ts_sec
+ 1);
1580 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1581 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1583 ts_sec_abs
= -ts_sec
;
1584 ts_nsec_abs
= ts_nsec
;
1585 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1587 ts_sec_abs
= -ts_sec
;
1588 ts_nsec_abs
= -ts_nsec
;
1591 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1592 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1600 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1601 struct cmd_run_ctx
*ctx
,
1602 const bt_component
*upstream_comp
,
1603 const bt_port_output
*out_upstream_port
,
1604 struct bt_config_connection
*cfg_conn
)
1606 typedef uint64_t (*input_port_count_func_t
)(void *);
1607 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1608 const void *, uint64_t);
1609 const bt_port
*upstream_port
=
1610 bt_port_output_as_port_const(out_upstream_port
);
1613 GQuark downstreamp_comp_name_quark
;
1614 void *downstream_comp
;
1615 uint64_t downstream_port_count
;
1617 input_port_count_func_t port_count_fn
;
1618 borrow_input_port_by_index_func_t port_by_index_fn
;
1619 bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1620 bool insert_trimmer
= false;
1621 bt_value
*trimmer_params
= NULL
;
1622 char *intersection_begin
= NULL
;
1623 char *intersection_end
= NULL
;
1624 const bt_component_filter
*trimmer
= NULL
;
1625 const bt_component_class_filter
*trimmer_class
= NULL
;
1626 const bt_port_input
*trimmer_input
= NULL
;
1627 const bt_port_output
*trimmer_output
= NULL
;
1629 if (ctx
->intersections
&&
1630 bt_component_get_class_type(upstream_comp
) ==
1631 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1632 struct trace_range
*range
;
1633 struct port_id port_id
= {
1634 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1635 .port_name
= (char *) bt_port_get_name(upstream_port
)
1638 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1642 range
= (struct trace_range
*) g_hash_table_lookup(
1643 ctx
->intersections
, &port_id
);
1645 bt_value_status status
;
1647 intersection_begin
= s_from_ns(
1648 range
->intersection_range_begin_ns
);
1649 intersection_end
= s_from_ns(
1650 range
->intersection_range_end_ns
);
1651 if (!intersection_begin
|| !intersection_end
) {
1652 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1656 insert_trimmer
= true;
1657 trimmer_params
= bt_value_map_create();
1658 if (!trimmer_params
) {
1662 status
= bt_value_map_insert_string_entry(
1663 trimmer_params
, "begin", intersection_begin
);
1664 if (status
!= BT_VALUE_STATUS_OK
) {
1667 status
= bt_value_map_insert_string_entry(
1669 "end", intersection_end
);
1670 if (status
!= BT_VALUE_STATUS_OK
) {
1675 trimmer_class
= find_filter_component_class("utils", "trimmer");
1676 if (!trimmer_class
) {
1681 BT_LOGI("Connecting upstream port to the next available downstream port: "
1682 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1683 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1684 upstream_port
, bt_port_get_name(upstream_port
),
1685 cfg_conn
->downstream_comp_name
->str
,
1686 cfg_conn
->arg
->str
);
1687 downstreamp_comp_name_quark
= g_quark_from_string(
1688 cfg_conn
->downstream_comp_name
->str
);
1689 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1690 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1691 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1692 port_count_fn
= (input_port_count_func_t
)
1693 bt_component_filter_get_input_port_count
;
1694 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1695 bt_component_filter_borrow_input_port_by_index_const
;
1697 if (!downstream_comp
) {
1698 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1699 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1700 port_count_fn
= (input_port_count_func_t
)
1701 bt_component_sink_get_input_port_count
;
1702 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1703 bt_component_sink_borrow_input_port_by_index_const
;
1706 if (!downstream_comp
) {
1707 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1708 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1709 cfg_conn
->arg
->str
);
1710 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1711 cfg_conn
->arg
->str
);
1715 downstream_port_count
= port_count_fn(downstream_comp
);
1717 for (i
= 0; i
< downstream_port_count
; i
++) {
1718 const bt_port_input
*in_downstream_port
=
1719 port_by_index_fn(downstream_comp
, i
);
1720 const bt_port
*downstream_port
=
1721 bt_port_input_as_port_const(in_downstream_port
);
1722 const char *upstream_port_name
;
1723 const char *downstream_port_name
;
1725 BT_ASSERT(downstream_port
);
1727 /* Skip port if it's already connected. */
1728 if (bt_port_is_connected(downstream_port
)) {
1729 BT_LOGI("Skipping downstream port: already connected: "
1730 "port-addr=%p, port-name=\"%s\"",
1732 bt_port_get_name(downstream_port
));
1736 downstream_port_name
= bt_port_get_name(downstream_port
);
1737 BT_ASSERT(downstream_port_name
);
1738 upstream_port_name
= bt_port_get_name(upstream_port
);
1739 BT_ASSERT(upstream_port_name
);
1741 if (!bt_common_star_glob_match(
1742 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1743 downstream_port_name
, SIZE_MAX
)) {
1747 if (insert_trimmer
) {
1749 * In order to insert the trimmer between the
1750 * two components that were being connected, we
1751 * create a connection configuration entry which
1752 * describes a connection from the trimmer's
1753 * output to the original input that was being
1756 * Hence, the creation of the trimmer will cause
1757 * the graph "new port" listener to establish
1758 * all downstream connections as its output port
1759 * is connected. We will then establish the
1760 * connection between the original upstream
1761 * source and the trimmer.
1763 char *trimmer_name
= NULL
;
1764 bt_graph_status graph_status
;
1766 ret
= asprintf(&trimmer_name
,
1767 "stream-intersection-trimmer-%s",
1768 upstream_port_name
);
1774 ctx
->connect_ports
= false;
1775 graph_status
= bt_graph_add_filter_component(
1776 ctx
->graph
, trimmer_class
, trimmer_name
,
1777 trimmer_params
, ctx
->cfg
->log_level
,
1780 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1786 bt_component_filter_borrow_input_port_by_index_const(
1788 if (!trimmer_input
) {
1792 bt_component_filter_borrow_output_port_by_index_const(
1794 if (!trimmer_output
) {
1799 * Replace the current downstream port by the trimmer's
1802 in_downstream_port
= trimmer_input
;
1804 bt_port_input_as_port_const(in_downstream_port
);
1805 downstream_port_name
= bt_port_get_name(
1807 BT_ASSERT(downstream_port_name
);
1810 /* We have a winner! */
1811 status
= bt_graph_connect_ports(ctx
->graph
,
1812 out_upstream_port
, in_downstream_port
, NULL
);
1813 downstream_port
= NULL
;
1815 case BT_GRAPH_STATUS_OK
:
1817 case BT_GRAPH_STATUS_CANCELED
:
1818 BT_LOGI_STR("Graph was canceled by user.");
1819 status
= BT_GRAPH_STATUS_OK
;
1821 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1822 BT_LOGE("A component refused a connection to one of its ports: "
1823 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1824 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1825 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1826 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1828 upstream_comp
, bt_component_get_name(upstream_comp
),
1829 upstream_port
, bt_port_get_name(upstream_port
),
1830 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1831 downstream_port
, downstream_port_name
,
1832 cfg_conn
->arg
->str
);
1834 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1835 bt_port_get_name(upstream_port
),
1836 downstream_port_name
,
1837 cfg_conn
->arg
->str
);
1840 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1841 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1842 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1843 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1844 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1846 upstream_comp
, bt_component_get_name(upstream_comp
),
1847 upstream_port
, bt_port_get_name(upstream_port
),
1848 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1849 downstream_port
, downstream_port_name
,
1850 cfg_conn
->arg
->str
);
1852 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1853 bt_port_get_name(upstream_port
),
1854 downstream_port_name
,
1855 cfg_conn
->arg
->str
);
1859 BT_LOGI("Connected component ports: "
1860 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1861 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1862 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1863 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1865 upstream_comp
, bt_component_get_name(upstream_comp
),
1866 upstream_port
, bt_port_get_name(upstream_port
),
1867 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1868 downstream_port
, downstream_port_name
,
1869 cfg_conn
->arg
->str
);
1871 if (insert_trimmer
) {
1873 * The first connection, from the source to the trimmer,
1874 * has been done. We now connect the trimmer to the
1875 * original downstream port.
1877 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1879 bt_component_filter_as_component_const(trimmer
),
1880 trimmer_output
, cfg_conn
);
1884 ctx
->connect_ports
= true;
1888 * We found a matching downstream port: the search is
1894 /* No downstream port found */
1895 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1896 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1897 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1898 upstream_port
, bt_port_get_name(upstream_port
),
1899 cfg_conn
->downstream_comp_name
->str
,
1900 cfg_conn
->arg
->str
);
1902 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1903 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1909 free(intersection_begin
);
1910 free(intersection_end
);
1911 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1912 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1913 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1918 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1919 const bt_port_output
*upstream_port
)
1922 const char *upstream_port_name
;
1923 const char *upstream_comp_name
;
1924 const bt_component
*upstream_comp
= NULL
;
1928 BT_ASSERT(upstream_port
);
1929 upstream_port_name
= bt_port_get_name(
1930 bt_port_output_as_port_const(upstream_port
));
1931 BT_ASSERT(upstream_port_name
);
1932 upstream_comp
= bt_port_borrow_component_const(
1933 bt_port_output_as_port_const(upstream_port
));
1934 if (!upstream_comp
) {
1935 BT_LOGW("Upstream port to connect is not part of a component: "
1936 "port-addr=%p, port-name=\"%s\"",
1937 upstream_port
, upstream_port_name
);
1942 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1943 BT_ASSERT(upstream_comp_name
);
1944 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1945 "port-addr=%p, port-name=\"%s\"",
1946 upstream_comp
, upstream_comp_name
,
1947 upstream_port
, upstream_port_name
);
1949 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1950 struct bt_config_connection
*cfg_conn
=
1952 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1954 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1955 upstream_comp_name
)) {
1959 if (!bt_common_star_glob_match(
1960 cfg_conn
->upstream_port_glob
->str
,
1961 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1965 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1966 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1968 BT_LOGE("Cannot connect upstream port: "
1969 "port-addr=%p, port-name=\"%s\"",
1971 upstream_port_name
);
1973 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1976 cfg_conn
->arg
->str
);
1982 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1983 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1984 upstream_port_name
);
1986 "Cannot create connection: upstream port `%s` does not match any connection\n",
1987 upstream_port_name
);
1997 bt_graph_listener_status
1998 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1999 const bt_port_output
*out_port
)
2001 const bt_component
*comp
;
2002 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
2003 bt_graph_listener_status ret
= BT_GRAPH_LISTENER_STATUS_OK
;
2005 comp
= bt_port_borrow_component_const(port
);
2006 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
2007 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
2008 comp
, comp
? bt_component_get_name(comp
) : "",
2009 port
, bt_port_get_name(port
));
2011 if (!ctx
->connect_ports
) {
2016 BT_LOGW_STR("Port has no component.");
2020 if (bt_port_is_connected(port
)) {
2021 BT_LOGW_STR("Port is already connected.");
2025 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
2026 BT_LOGF_STR("Cannot connect upstream port.");
2027 fprintf(stderr
, "Added port could not be connected: aborting\n");
2028 ret
= BT_GRAPH_LISTENER_STATUS_ERROR
;
2037 bt_graph_listener_status
graph_source_output_port_added_listener(
2038 const bt_component_source
*component
,
2039 const bt_port_output
*port
, void *data
)
2041 return graph_output_port_added_listener(data
, port
);
2045 bt_graph_listener_status
graph_filter_output_port_added_listener(
2046 const bt_component_filter
*component
,
2047 const bt_port_output
*port
, void *data
)
2049 return graph_output_port_added_listener(data
, port
);
2053 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
2059 if (ctx
->src_components
) {
2060 g_hash_table_destroy(ctx
->src_components
);
2061 ctx
->src_components
= NULL
;
2064 if (ctx
->flt_components
) {
2065 g_hash_table_destroy(ctx
->flt_components
);
2066 ctx
->flt_components
= NULL
;
2069 if (ctx
->sink_components
) {
2070 g_hash_table_destroy(ctx
->sink_components
);
2071 ctx
->sink_components
= NULL
;
2074 if (ctx
->intersections
) {
2075 g_hash_table_destroy(ctx
->intersections
);
2076 ctx
->intersections
= NULL
;
2079 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
2085 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2088 bt_graph_status status
;
2091 ctx
->connect_ports
= false;
2092 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
2093 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2094 if (!ctx
->src_components
) {
2098 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
2099 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2100 if (!ctx
->flt_components
) {
2104 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
2105 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2106 if (!ctx
->sink_components
) {
2110 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2111 ctx
->stream_intersection_mode
= true;
2112 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2113 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2114 if (!ctx
->intersections
) {
2119 ctx
->graph
= bt_graph_create();
2124 the_graph
= ctx
->graph
;
2125 status
= bt_graph_add_source_component_output_port_added_listener(
2126 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
2128 if (status
!= BT_GRAPH_STATUS_OK
) {
2129 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2133 status
= bt_graph_add_filter_component_output_port_added_listener(
2134 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
2136 if (status
!= BT_GRAPH_STATUS_OK
) {
2137 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2144 cmd_run_ctx_destroy(ctx
);
2152 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2153 struct bt_config_component
*cfg_comp
,
2154 const bt_component_class_source
*src_comp_cls
)
2158 int64_t trace_count
;
2159 const char *path
= NULL
;
2160 const bt_value
*query_result
= NULL
;
2161 const bt_value
*trace_info
= NULL
;
2162 const bt_value
*intersection_range
= NULL
;
2163 const bt_value
*intersection_begin
= NULL
;
2164 const bt_value
*intersection_end
= NULL
;
2165 const bt_value
*stream_infos
= NULL
;
2166 const bt_value
*stream_info
= NULL
;
2167 struct port_id
*port_id
= NULL
;
2168 struct trace_range
*trace_range
= NULL
;
2169 const char *fail_reason
= NULL
;
2170 const bt_component_class
*comp_cls
=
2171 bt_component_class_source_as_component_class_const(src_comp_cls
);
2173 ret
= query(ctx
->cfg
, comp_cls
, "trace-info",
2174 cfg_comp
->params
, &query_result
,
2177 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2178 "comp-class-name=\"%s\"", fail_reason
,
2179 bt_component_class_get_name(comp_cls
));
2184 BT_ASSERT(query_result
);
2186 if (!bt_value_is_array(query_result
)) {
2187 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2188 "component-class-name=%s",
2189 bt_component_class_get_name(comp_cls
));
2194 trace_count
= bt_value_array_get_size(query_result
);
2195 if (trace_count
< 0) {
2200 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2202 uint64_t stream_idx
;
2203 int64_t stream_count
;
2205 trace_info
= bt_value_array_borrow_element_by_index_const(
2206 query_result
, trace_idx
);
2207 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2209 BT_LOGD_STR("Cannot retrieve trace from query result.");
2213 intersection_range
= bt_value_map_borrow_entry_value_const(
2214 trace_info
, "intersection-range-ns");
2215 if (!intersection_range
) {
2217 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2221 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
2223 if (!intersection_begin
) {
2225 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2229 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
2231 if (!intersection_end
) {
2233 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2237 begin
= bt_value_signed_integer_get(intersection_begin
);
2238 end
= bt_value_signed_integer_get(intersection_end
);
2240 if (begin
< 0 || end
< 0 || end
< begin
) {
2241 BT_LOGW("Invalid trace stream intersection values: "
2242 "intersection-range-ns:begin=%" PRId64
2243 ", intersection-range-ns:end=%" PRId64
,
2249 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
2251 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2253 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2257 stream_count
= bt_value_array_get_size(stream_infos
);
2258 if (stream_count
< 0) {
2263 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2264 const bt_value
*port_name
;
2266 port_id
= g_new0(struct port_id
, 1);
2269 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2272 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2273 if (!port_id
->instance_name
) {
2275 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2279 trace_range
= g_new0(struct trace_range
, 1);
2282 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2285 trace_range
->intersection_range_begin_ns
= begin
;
2286 trace_range
->intersection_range_end_ns
= end
;
2288 stream_info
= bt_value_array_borrow_element_by_index_const(
2289 stream_infos
, stream_idx
);
2290 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2292 BT_LOGE_STR("Cannot retrieve stream informations from trace in query result.");
2296 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2297 if (!port_name
|| !bt_value_is_string(port_name
)) {
2299 BT_LOGE_STR("Cannot retrieve port name in query result.");
2303 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2304 if (!port_id
->port_name
) {
2306 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2310 BT_LOGD("Inserting stream intersection ");
2312 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2322 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2323 bt_common_color_bold(),
2324 bt_common_color_fg_yellow(),
2325 path
? path
: "(unknown)",
2326 bt_common_color_reset());
2328 bt_value_put_ref(query_result
);
2330 g_free(trace_range
);
2335 int cmd_run_ctx_create_components_from_config_components(
2336 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2339 const void *comp_cls
= NULL
;
2340 const void *comp
= NULL
;
2343 for (i
= 0; i
< cfg_components
->len
; i
++) {
2344 struct bt_config_component
*cfg_comp
=
2345 g_ptr_array_index(cfg_components
, i
);
2348 switch (cfg_comp
->type
) {
2349 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2350 comp_cls
= find_source_component_class(
2351 cfg_comp
->plugin_name
->str
,
2352 cfg_comp
->comp_cls_name
->str
);
2354 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2355 comp_cls
= find_filter_component_class(
2356 cfg_comp
->plugin_name
->str
,
2357 cfg_comp
->comp_cls_name
->str
);
2359 case BT_COMPONENT_CLASS_TYPE_SINK
:
2360 comp_cls
= find_sink_component_class(
2361 cfg_comp
->plugin_name
->str
,
2362 cfg_comp
->comp_cls_name
->str
);
2369 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2370 "comp-cls-name=\"%s\", comp-cls-type=%d",
2371 cfg_comp
->plugin_name
->str
,
2372 cfg_comp
->comp_cls_name
->str
,
2374 fprintf(stderr
, "%s%sCannot find component class %s",
2375 bt_common_color_bold(),
2376 bt_common_color_fg_red(),
2377 bt_common_color_reset());
2378 print_plugin_comp_cls_opt(stderr
,
2379 cfg_comp
->plugin_name
->str
,
2380 cfg_comp
->comp_cls_name
->str
,
2382 fprintf(stderr
, "\n");
2386 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_VERBOSE
);
2388 switch (cfg_comp
->type
) {
2389 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2390 ret
= bt_graph_add_source_component(ctx
->graph
,
2391 comp_cls
, cfg_comp
->instance_name
->str
,
2392 cfg_comp
->params
, cfg_comp
->log_level
,
2395 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2396 ret
= bt_graph_add_filter_component(ctx
->graph
,
2397 comp_cls
, cfg_comp
->instance_name
->str
,
2398 cfg_comp
->params
, cfg_comp
->log_level
,
2401 case BT_COMPONENT_CLASS_TYPE_SINK
:
2402 ret
= bt_graph_add_sink_component(ctx
->graph
,
2403 comp_cls
, cfg_comp
->instance_name
->str
,
2404 cfg_comp
->params
, cfg_comp
->log_level
,
2412 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2413 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2415 cfg_comp
->plugin_name
->str
,
2416 cfg_comp
->comp_cls_name
->str
,
2417 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2418 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2419 bt_common_color_bold(),
2420 bt_common_color_fg_red(),
2421 cfg_comp
->instance_name
->str
,
2422 bt_common_color_reset());
2426 if (ctx
->stream_intersection_mode
&&
2427 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2428 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2434 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2435 comp
, cfg_comp
->instance_name
->str
);
2436 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2437 BT_ASSERT(quark
> 0);
2439 switch (cfg_comp
->type
) {
2440 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2441 g_hash_table_insert(ctx
->src_components
,
2442 GUINT_TO_POINTER(quark
), (void *) comp
);
2444 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2445 g_hash_table_insert(ctx
->flt_components
,
2446 GUINT_TO_POINTER(quark
), (void *) comp
);
2448 case BT_COMPONENT_CLASS_TYPE_SINK
:
2449 g_hash_table_insert(ctx
->sink_components
,
2450 GUINT_TO_POINTER(quark
), (void *) comp
);
2457 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2466 bt_object_put_ref(comp
);
2467 bt_object_put_ref(comp_cls
);
2472 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2477 * Make sure that, during this phase, our graph's "port added"
2478 * listener does not connect ports while we are creating the
2479 * components because we have a special, initial phase for
2482 ctx
->connect_ports
= false;
2484 ret
= cmd_run_ctx_create_components_from_config_components(
2485 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2491 ret
= cmd_run_ctx_create_components_from_config_components(
2492 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2498 ret
= cmd_run_ctx_create_components_from_config_components(
2499 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2509 typedef uint64_t (*output_port_count_func_t
)(const void *);
2510 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2511 const void *, uint64_t);
2514 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2515 void *comp
, output_port_count_func_t port_count_fn
,
2516 borrow_output_port_by_index_func_t port_by_index_fn
)
2522 count
= port_count_fn(comp
);
2524 for (i
= 0; i
< count
; i
++) {
2525 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2527 BT_ASSERT(upstream_port
);
2528 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2539 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2542 GHashTableIter iter
;
2543 gpointer g_name_quark
, g_comp
;
2545 ctx
->connect_ports
= true;
2546 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2548 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2549 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2550 (output_port_count_func_t
)
2551 bt_component_source_get_output_port_count
,
2552 (borrow_output_port_by_index_func_t
)
2553 bt_component_source_borrow_output_port_by_index_const
);
2559 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2561 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2562 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2563 (output_port_count_func_t
)
2564 bt_component_filter_get_output_port_count
,
2565 (borrow_output_port_by_index_func_t
)
2566 bt_component_filter_borrow_output_port_by_index_const
);
2577 const char *bt_graph_status_str(bt_graph_status status
)
2580 case BT_GRAPH_STATUS_OK
:
2581 return "BT_GRAPH_STATUS_OK";
2582 case BT_GRAPH_STATUS_END
:
2583 return "BT_GRAPH_STATUS_END";
2584 case BT_GRAPH_STATUS_AGAIN
:
2585 return "BT_GRAPH_STATUS_AGAIN";
2586 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
2587 return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION";
2588 case BT_GRAPH_STATUS_CANCELED
:
2589 return "BT_GRAPH_STATUS_CANCELED";
2590 case BT_GRAPH_STATUS_ERROR
:
2591 return "BT_GRAPH_STATUS_ERROR";
2592 case BT_GRAPH_STATUS_NOMEM
:
2593 return "BT_GRAPH_STATUS_NOMEM";
2600 int cmd_run(struct bt_config
*cfg
)
2603 struct cmd_run_ctx ctx
= { 0 };
2605 /* Initialize the command's context and the graph object */
2606 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2607 BT_LOGE_STR("Cannot initialize the command's context.");
2608 fprintf(stderr
, "Cannot initialize the command's context\n");
2613 BT_LOGI_STR("Canceled by user before creating components.");
2617 BT_LOGI_STR("Creating components.");
2619 /* Create the requested component instances */
2620 if (cmd_run_ctx_create_components(&ctx
)) {
2621 BT_LOGE_STR("Cannot create components.");
2622 fprintf(stderr
, "Cannot create components\n");
2627 BT_LOGI_STR("Canceled by user before connecting components.");
2631 BT_LOGI_STR("Connecting components.");
2633 /* Connect the initially visible component ports */
2634 if (cmd_run_ctx_connect_ports(&ctx
)) {
2635 BT_LOGE_STR("Cannot connect initial component ports.");
2636 fprintf(stderr
, "Cannot connect initial component ports\n");
2641 BT_LOGI_STR("Canceled by user before running the graph.");
2645 BT_LOGI_STR("Running the graph.");
2649 bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2652 * Reset console in case something messed with console
2653 * codes during the graph's execution.
2655 printf("%s", bt_common_color_reset());
2657 fprintf(stderr
, "%s", bt_common_color_reset());
2658 BT_LOGV("bt_graph_run() returned: status=%s",
2659 bt_graph_status_str(graph_status
));
2661 switch (graph_status
) {
2662 case BT_GRAPH_STATUS_OK
:
2664 case BT_GRAPH_STATUS_CANCELED
:
2665 BT_LOGI_STR("Graph was canceled by user.");
2667 case BT_GRAPH_STATUS_AGAIN
:
2668 if (bt_graph_is_canceled(ctx
.graph
)) {
2669 BT_LOGI_STR("Graph was canceled by user.");
2673 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2674 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2676 cfg
->cmd_data
.run
.retry_duration_us
);
2678 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2679 if (bt_graph_is_canceled(ctx
.graph
)) {
2680 BT_LOGI_STR("Graph was canceled by user.");
2686 case BT_GRAPH_STATUS_END
:
2689 BT_LOGE_STR("Graph failed to complete successfully");
2690 fprintf(stderr
, "Graph failed to complete successfully\n");
2703 cmd_run_ctx_destroy(&ctx
);
2708 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2710 const char *env_clash
;
2712 if (!cfg
->command_name
) {
2716 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2717 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2721 if (g_file_test(cfg
->command_name
,
2722 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2723 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2725 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2727 fprintf(stderr
, "\n");
2728 fprintf(stderr
, " babeltrace2 convert %s [OPTIONS]\n",
2734 void init_log_level(void)
2736 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2740 void set_auto_log_levels(struct bt_config
*cfg
)
2742 const char **env_var_name
;
2745 * Override the configuration's default log level if
2746 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2747 * are found for backward compatibility with legacy Babetrace 1.
2749 if (getenv("BABELTRACE_DEBUG") &&
2750 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2751 cfg
->log_level
= BT_LOG_VERBOSE
;
2752 } else if (getenv("BABELTRACE_VERBOSE") &&
2753 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2754 cfg
->log_level
= BT_LOG_INFO
;
2758 * Set log levels according to --debug or --verbose. For
2759 * backward compatibility, --debug is more verbose than
2762 * --verbose: INFO log level
2763 * --debug: VERBOSE log level (includes DEBUG, which is
2764 * is less verbose than VERBOSE in the internal
2765 * logging framework)
2767 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2769 bt_logging_set_global_level(BT_LOG_INFO
);
2770 } else if (cfg
->debug
) {
2771 bt_logging_set_global_level(BT_LOG_VERBOSE
);
2774 * Set library's default log level if not
2775 * explicitly specified.
2777 bt_logging_set_global_level(cfg
->log_level
);
2781 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2783 bt_cli_log_level
= BT_LOG_INFO
;
2784 } else if (cfg
->debug
) {
2785 bt_cli_log_level
= BT_LOG_VERBOSE
;
2788 * Set CLI's default log level if not explicitly
2791 bt_cli_log_level
= cfg
->log_level
;
2795 env_var_name
= log_level_env_var_names
;
2797 while (*env_var_name
) {
2798 if (!getenv(*env_var_name
)) {
2800 g_setenv(*env_var_name
, "I", 1);
2801 } else if (cfg
->debug
) {
2802 g_setenv(*env_var_name
, "V", 1);
2804 char val
[2] = { 0 };
2807 * Set module's default log level if not
2808 * explicitly specified.
2810 val
[0] = bt_log_get_letter_from_level(
2812 g_setenv(*env_var_name
, val
, 1);
2820 int main(int argc
, const char **argv
)
2824 struct bt_config
*cfg
;
2827 set_signal_handler();
2829 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2832 /* Quit without errors; typically usage/version */
2834 BT_LOGI_STR("Quitting without errors.");
2839 BT_LOGE("Command-line error: retcode=%d", retcode
);
2844 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2845 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2850 set_auto_log_levels(cfg
);
2853 if (cfg
->command_needs_plugins
) {
2854 ret
= load_all_plugins(cfg
->plugin_paths
);
2856 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2862 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2863 cfg
->command
, cfg
->command_name
);
2865 switch (cfg
->command
) {
2866 case BT_CONFIG_COMMAND_RUN
:
2869 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2870 ret
= cmd_list_plugins(cfg
);
2872 case BT_CONFIG_COMMAND_HELP
:
2873 ret
= cmd_help(cfg
);
2875 case BT_CONFIG_COMMAND_QUERY
:
2876 ret
= cmd_query(cfg
);
2878 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2879 ret
= cmd_print_ctf_metadata(cfg
);
2881 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2882 ret
= cmd_print_lttng_live_sessions(cfg
);
2885 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2889 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2890 cfg
->command
, cfg
->command_name
, ret
);
2891 warn_command_name_and_directory_clash(cfg
);
2892 retcode
= ret
? 1 : 0;
2895 BT_OBJECT_PUT_REF_AND_RESET(cfg
);