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_PLUGIN_CTF_BFCR_LOG_LEVEL",
53 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
54 "BABELTRACE_PLUGIN_CTF_MSG_ITER_LOG_LEVEL",
55 "BABELTRACE_PLUGIN_CTF_UTILS_LOG_LEVEL",
56 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
57 "BABELTRACE_SINK_CTF_FS_LOG_LEVEL",
58 "BABELTRACE_SINK_TEXT_DETAILS_LOG_LEVEL",
59 "BABELTRACE_SRC_CTF_FS_LOG_LEVEL",
60 "BABELTRACE_SRC_CTF_LTTNG_LIVE_LOG_LEVEL",
64 /* Application's processing graph (weak) */
65 static bt_graph
*the_graph
;
66 static bt_query_executor
*the_query_executor
;
67 static bool canceled
= false;
69 GPtrArray
*loaded_plugins
;
76 BOOL WINAPI
signal_handler(DWORD signal
) {
78 bt_graph_cancel(the_graph
);
87 void set_signal_handler(void)
89 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
90 BT_LOGE("Failed to set the Ctrl+C handler.");
94 #else /* __MINGW32__ */
97 void signal_handler(int signum
)
99 if (signum
!= SIGINT
) {
104 bt_graph_cancel(the_graph
);
107 if (the_query_executor
) {
108 bt_query_executor_cancel(the_query_executor
);
115 void set_signal_handler(void)
117 struct sigaction new_action
, old_action
;
119 new_action
.sa_handler
= signal_handler
;
120 sigemptyset(&new_action
.sa_mask
);
121 new_action
.sa_flags
= 0;
122 sigaction(SIGINT
, NULL
, &old_action
);
124 if (old_action
.sa_handler
!= SIG_IGN
) {
125 sigaction(SIGINT
, &new_action
, NULL
);
129 #endif /* __MINGW32__ */
132 void init_static_data(void)
134 loaded_plugins
= g_ptr_array_new_with_free_func(
135 (GDestroyNotify
) bt_object_put_ref
);
139 void fini_static_data(void)
141 g_ptr_array_free(loaded_plugins
, TRUE
);
145 int create_the_query_executor(void)
149 the_query_executor
= bt_query_executor_create();
150 if (!the_query_executor
) {
151 BT_LOGE_STR("Cannot create a query executor.");
159 void destroy_the_query_executor(void)
161 BT_QUERY_EXECUTOR_PUT_REF_AND_RESET(the_query_executor
);
165 int query(struct bt_config
*cfg
, const bt_component_class
*comp_cls
,
166 const char *obj
, const bt_value
*params
,
167 const bt_value
**user_result
, const char **fail_reason
)
169 const bt_value
*result
= NULL
;
170 bt_query_executor_status status
;
171 *fail_reason
= "unknown error";
174 BT_ASSERT(fail_reason
);
175 BT_ASSERT(user_result
);
176 ret
= create_the_query_executor();
178 /* create_the_query_executor() logs errors */
183 BT_LOGI("Canceled by user before executing the query: "
184 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
185 "query-obj=\"%s\"", comp_cls
,
186 bt_component_class_get_name(comp_cls
), obj
);
187 *fail_reason
= "canceled by user";
192 status
= bt_query_executor_query(the_query_executor
,
193 comp_cls
, obj
, params
, cfg
->log_level
, &result
);
195 case BT_QUERY_EXECUTOR_STATUS_OK
:
197 case BT_QUERY_EXECUTOR_STATUS_AGAIN
:
199 const uint64_t sleep_time_us
= 100000;
201 /* Wait 100 ms and retry */
202 BT_LOGD("Got BT_QUERY_EXECUTOR_STATUS_AGAIN: sleeping: "
203 "time-us=%" PRIu64
, sleep_time_us
);
205 if (usleep(sleep_time_us
)) {
206 if (bt_query_executor_is_canceled(the_query_executor
)) {
207 BT_LOGI("Query was canceled by user: "
208 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
209 "query-obj=\"%s\"", comp_cls
,
210 bt_component_class_get_name(comp_cls
),
212 *fail_reason
= "canceled by user";
219 case BT_QUERY_EXECUTOR_STATUS_CANCELED
:
220 *fail_reason
= "canceled by user";
222 case BT_QUERY_EXECUTOR_STATUS_ERROR
:
224 case BT_QUERY_EXECUTOR_STATUS_INVALID_OBJECT
:
225 *fail_reason
= "invalid or unknown query object";
227 case BT_QUERY_EXECUTOR_STATUS_INVALID_PARAMS
:
228 *fail_reason
= "invalid query parameters";
230 case BT_QUERY_EXECUTOR_STATUS_UNSUPPORTED
:
231 *fail_reason
= "unsupported action";
233 case BT_QUERY_EXECUTOR_STATUS_NOMEM
:
234 *fail_reason
= "not enough memory";
237 BT_LOGF("Unknown query status: status=%d", status
);
243 *user_result
= result
;
251 destroy_the_query_executor();
252 bt_value_put_ref(result
);
257 const bt_plugin
*find_plugin(const char *name
)
260 const bt_plugin
*plugin
= NULL
;
263 BT_LOGI("Finding plugin: name=\"%s\"", name
);
265 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
266 plugin
= g_ptr_array_index(loaded_plugins
, i
);
268 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
276 BT_LOGI("Found plugin: name=\"%s\", plugin-addr=%p",
279 BT_LOGI("Cannot find plugin: name=\"%s\"", name
);
282 bt_plugin_get_ref(plugin
);
286 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
287 const bt_plugin
*, const char *);
290 const void *find_component_class_from_plugin(const char *plugin_name
,
291 const char *comp_class_name
,
292 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
294 const void *comp_class
= NULL
;
295 const bt_plugin
*plugin
;
297 BT_LOGI("Finding component class: plugin-name=\"%s\", "
298 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
300 plugin
= find_plugin(plugin_name
);
305 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
306 bt_object_get_ref(comp_class
);
307 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
311 BT_LOGI("Found component class: plugin-name=\"%s\", "
312 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
314 BT_LOGI("Cannot find source component class: "
315 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
316 plugin_name
, comp_class_name
);
323 const bt_component_class_source
*find_source_component_class(
324 const char *plugin_name
, const char *comp_class_name
)
326 return (const void *) find_component_class_from_plugin(
327 plugin_name
, comp_class_name
,
328 (plugin_borrow_comp_cls_func_t
)
329 bt_plugin_borrow_source_component_class_by_name_const
);
333 const bt_component_class_filter
*find_filter_component_class(
334 const char *plugin_name
, const char *comp_class_name
)
336 return (const void *) find_component_class_from_plugin(
337 plugin_name
, comp_class_name
,
338 (plugin_borrow_comp_cls_func_t
)
339 bt_plugin_borrow_filter_component_class_by_name_const
);
343 const bt_component_class_sink
*find_sink_component_class(
344 const char *plugin_name
, const char *comp_class_name
)
346 return (const void *) find_component_class_from_plugin(plugin_name
,
348 (plugin_borrow_comp_cls_func_t
)
349 bt_plugin_borrow_sink_component_class_by_name_const
);
353 const bt_component_class
*find_component_class(const char *plugin_name
,
354 const char *comp_class_name
,
355 bt_component_class_type comp_class_type
)
357 const bt_component_class
*comp_cls
= NULL
;
359 switch (comp_class_type
) {
360 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
361 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
363 case BT_COMPONENT_CLASS_TYPE_FILTER
:
364 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
366 case BT_COMPONENT_CLASS_TYPE_SINK
:
367 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
377 void print_indent(FILE *fp
, size_t indent
)
381 for (i
= 0; i
< indent
; i
++) {
387 const char *component_type_str(bt_component_class_type type
)
390 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
392 case BT_COMPONENT_CLASS_TYPE_SINK
:
394 case BT_COMPONENT_CLASS_TYPE_FILTER
:
402 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
403 const char *comp_cls_name
, bt_component_class_type type
)
405 GString
*shell_plugin_name
= NULL
;
406 GString
*shell_comp_cls_name
= NULL
;
408 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
409 if (!shell_plugin_name
) {
413 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
414 if (!shell_comp_cls_name
) {
418 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
419 bt_common_color_bold(),
420 bt_common_color_fg_cyan(),
421 component_type_str(type
),
422 bt_common_color_fg_default(),
423 bt_common_color_fg_blue(),
424 shell_plugin_name
->str
,
425 bt_common_color_fg_default(),
426 bt_common_color_fg_yellow(),
427 shell_comp_cls_name
->str
,
428 bt_common_color_reset());
431 if (shell_plugin_name
) {
432 g_string_free(shell_plugin_name
, TRUE
);
435 if (shell_comp_cls_name
) {
436 g_string_free(shell_comp_cls_name
, TRUE
);
441 void print_value(FILE *, const bt_value
*, size_t);
444 void print_value_rec(FILE *, const bt_value
*, size_t);
446 struct print_map_value_data
{
452 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
455 struct print_map_value_data
*print_map_value_data
= data
;
457 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
458 fprintf(print_map_value_data
->fp
, "%s: ", key
);
461 if (bt_value_is_array(object
) &&
462 bt_value_array_is_empty(object
)) {
463 fprintf(print_map_value_data
->fp
, "[ ]\n");
467 if (bt_value_is_map(object
) &&
468 bt_value_map_is_empty(object
)) {
469 fprintf(print_map_value_data
->fp
, "{ }\n");
473 if (bt_value_is_array(object
) ||
474 bt_value_is_map(object
)) {
475 fprintf(print_map_value_data
->fp
, "\n");
478 print_value_rec(print_map_value_data
->fp
, object
,
479 print_map_value_data
->indent
+ 2);
484 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
498 switch (bt_value_get_type(value
)) {
499 case BT_VALUE_TYPE_NULL
:
500 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
501 bt_common_color_reset());
503 case BT_VALUE_TYPE_BOOL
:
504 bool_val
= bt_value_bool_get(value
);
505 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
506 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
507 bt_common_color_reset());
509 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
510 uint_val
= bt_value_unsigned_integer_get(value
);
511 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
512 bt_common_color_fg_red(), uint_val
,
513 bt_common_color_reset());
515 case BT_VALUE_TYPE_SIGNED_INTEGER
:
516 int_val
= bt_value_signed_integer_get(value
);
517 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
518 bt_common_color_fg_red(), int_val
,
519 bt_common_color_reset());
521 case BT_VALUE_TYPE_REAL
:
522 dbl_val
= bt_value_real_get(value
);
523 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
524 bt_common_color_fg_red(), dbl_val
,
525 bt_common_color_reset());
527 case BT_VALUE_TYPE_STRING
:
528 str_val
= bt_value_string_get(value
);
529 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
530 bt_common_color_fg_green(), str_val
,
531 bt_common_color_reset());
533 case BT_VALUE_TYPE_ARRAY
:
534 size
= bt_value_array_get_size(value
);
540 print_indent(fp
, indent
);
541 fprintf(fp
, "[ ]\n");
545 for (i
= 0; i
< size
; i
++) {
546 const bt_value
*element
=
547 bt_value_array_borrow_element_by_index_const(
553 print_indent(fp
, indent
);
556 if (bt_value_is_array(element
) &&
557 bt_value_array_is_empty(element
)) {
558 fprintf(fp
, "[ ]\n");
562 if (bt_value_is_map(element
) &&
563 bt_value_map_is_empty(element
)) {
564 fprintf(fp
, "{ }\n");
568 if (bt_value_is_array(element
) ||
569 bt_value_is_map(element
)) {
573 print_value_rec(fp
, element
, indent
+ 2);
576 case BT_VALUE_TYPE_MAP
:
578 struct print_map_value_data data
= {
583 if (bt_value_map_is_empty(value
)) {
584 print_indent(fp
, indent
);
585 fprintf(fp
, "{ }\n");
589 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
598 BT_LOGE("Error printing value of type %s.",
599 bt_common_value_type_string(bt_value_get_type(value
)));
603 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
605 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
606 print_indent(fp
, indent
);
609 print_value_rec(fp
, value
, indent
);
613 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
615 fprintf(stderr
, " ");
616 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
617 bt_config_component
->comp_cls_name
->str
,
618 bt_config_component
->type
);
619 fprintf(stderr
, ":\n");
621 if (bt_config_component
->instance_name
->len
> 0) {
622 fprintf(stderr
, " Name: %s\n",
623 bt_config_component
->instance_name
->str
);
626 fprintf(stderr
, " Parameters:\n");
627 print_value(stderr
, bt_config_component
->params
, 8);
631 void print_bt_config_components(GPtrArray
*array
)
635 for (i
= 0; i
< array
->len
; i
++) {
636 struct bt_config_component
*cfg_component
=
637 bt_config_get_component(array
, i
);
638 print_bt_config_component(cfg_component
);
639 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
644 void print_plugin_paths(const bt_value
*plugin_paths
)
646 fprintf(stderr
, " Plugin paths:\n");
647 print_value(stderr
, plugin_paths
, 4);
651 void print_cfg_run(struct bt_config
*cfg
)
655 print_plugin_paths(cfg
->plugin_paths
);
656 fprintf(stderr
, " Source component instances:\n");
657 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
659 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
660 fprintf(stderr
, " Filter component instances:\n");
661 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
664 fprintf(stderr
, " Sink component instances:\n");
665 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
666 fprintf(stderr
, " Connections:\n");
668 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
669 struct bt_config_connection
*cfg_connection
=
670 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
673 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
674 cfg_connection
->upstream_comp_name
->str
,
675 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
676 cfg_connection
->upstream_port_glob
->str
,
677 cfg_connection
->downstream_comp_name
->str
,
678 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
679 cfg_connection
->downstream_port_glob
->str
);
684 void print_cfg_list_plugins(struct bt_config
*cfg
)
686 print_plugin_paths(cfg
->plugin_paths
);
690 void print_cfg_help(struct bt_config
*cfg
)
692 print_plugin_paths(cfg
->plugin_paths
);
696 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
698 print_plugin_paths(cfg
->plugin_paths
);
699 fprintf(stderr
, " Path: %s\n",
700 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
704 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
706 print_plugin_paths(cfg
->plugin_paths
);
707 fprintf(stderr
, " URL: %s\n",
708 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
712 void print_cfg_query(struct bt_config
*cfg
)
714 print_plugin_paths(cfg
->plugin_paths
);
715 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
716 fprintf(stderr
, " Component class:\n");
717 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
721 void print_cfg(struct bt_config
*cfg
)
723 if (!BT_LOG_ON_INFO
) {
727 BT_LOGI_STR("CLI configuration:");
728 BT_LOGI(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
729 BT_LOGI(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
731 switch (cfg
->command
) {
732 case BT_CONFIG_COMMAND_RUN
:
735 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
736 print_cfg_list_plugins(cfg
);
738 case BT_CONFIG_COMMAND_HELP
:
741 case BT_CONFIG_COMMAND_QUERY
:
742 print_cfg_query(cfg
);
744 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
745 print_cfg_print_ctf_metadata(cfg
);
747 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
748 print_cfg_print_lttng_live_sessions(cfg
);
756 void add_to_loaded_plugins(const bt_plugin_set
*plugin_set
)
761 count
= bt_plugin_set_get_plugin_count(plugin_set
);
762 BT_ASSERT(count
>= 0);
764 for (i
= 0; i
< count
; i
++) {
765 const bt_plugin
*plugin
=
766 bt_plugin_set_borrow_plugin_by_index_const(plugin_set
, i
);
767 const bt_plugin
*loaded_plugin
=
768 find_plugin(bt_plugin_get_name(plugin
));
773 BT_LOGI("Not using plugin: another one already exists with the same name: "
774 "plugin-name=\"%s\", plugin-path=\"%s\", "
775 "existing-plugin-path=\"%s\"",
776 bt_plugin_get_name(plugin
),
777 bt_plugin_get_path(plugin
),
778 bt_plugin_get_path(loaded_plugin
));
779 bt_plugin_put_ref(loaded_plugin
);
781 /* Add to global array. */
782 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
783 bt_plugin_get_name(plugin
));
784 bt_plugin_get_ref(plugin
);
785 g_ptr_array_add(loaded_plugins
, (void *) plugin
);
791 int load_dynamic_plugins(const bt_value
*plugin_paths
)
793 int nr_paths
, i
, ret
= 0;
795 nr_paths
= bt_value_array_get_size(plugin_paths
);
797 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
802 BT_LOGI_STR("Loading dynamic plugins.");
804 for (i
= 0; i
< nr_paths
; i
++) {
805 const bt_value
*plugin_path_value
= NULL
;
806 const char *plugin_path
;
807 const bt_plugin_set
*plugin_set
;
810 bt_value_array_borrow_element_by_index_const(
812 plugin_path
= bt_value_string_get(plugin_path_value
);
815 * Skip this if the directory does not exist because
816 * bt_plugin_find_all_from_dir() expects an existing
819 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
820 BT_LOGI("Skipping nonexistent directory path: "
821 "path=\"%s\"", plugin_path
);
825 plugin_set
= bt_plugin_find_all_from_dir(plugin_path
, false);
827 BT_LOGI("Unable to load dynamic plugins from directory: "
828 "path=\"%s\"", plugin_path
);
832 add_to_loaded_plugins(plugin_set
);
833 bt_plugin_set_put_ref(plugin_set
);
840 int load_static_plugins(void)
843 const bt_plugin_set
*plugin_set
;
845 BT_LOGI("Loading static plugins.");
846 plugin_set
= bt_plugin_find_all_from_static();
848 BT_LOGE("Unable to load static plugins.");
853 add_to_loaded_plugins(plugin_set
);
854 bt_plugin_set_put_ref(plugin_set
);
860 int load_all_plugins(const bt_value
*plugin_paths
)
864 if (load_dynamic_plugins(plugin_paths
)) {
869 if (load_static_plugins()) {
874 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
881 void print_plugin_info(const bt_plugin
*plugin
)
883 unsigned int major
, minor
, patch
;
885 bt_property_availability version_avail
;
886 const char *plugin_name
;
890 const char *plugin_description
;
892 plugin_name
= bt_plugin_get_name(plugin
);
893 path
= bt_plugin_get_path(plugin
);
894 author
= bt_plugin_get_author(plugin
);
895 license
= bt_plugin_get_license(plugin
);
896 plugin_description
= bt_plugin_get_description(plugin
);
897 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
899 printf("%s%s%s%s:\n", bt_common_color_bold(),
900 bt_common_color_fg_blue(), plugin_name
,
901 bt_common_color_reset());
903 printf(" %sPath%s: %s\n", bt_common_color_bold(),
904 bt_common_color_reset(), path
);
909 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
910 printf(" %sVersion%s: %u.%u.%u",
911 bt_common_color_bold(), bt_common_color_reset(),
912 major
, minor
, patch
);
921 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
922 bt_common_color_reset(),
923 plugin_description
? plugin_description
: "(None)");
924 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
925 bt_common_color_reset(), author
? author
: "(Unknown)");
926 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
927 bt_common_color_reset(),
928 license
? license
: "(Unknown)");
932 int cmd_query(struct bt_config
*cfg
)
935 const bt_component_class
*comp_cls
= NULL
;
936 const bt_value
*results
= NULL
;
937 const char *fail_reason
= NULL
;
939 comp_cls
= find_component_class(
940 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
941 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
942 cfg
->cmd_data
.query
.cfg_component
->type
);
944 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
945 "comp-cls-name=\"%s\", comp-cls-type=%d",
946 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
947 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
948 cfg
->cmd_data
.query
.cfg_component
->type
);
949 fprintf(stderr
, "%s%sCannot find component class %s",
950 bt_common_color_bold(),
951 bt_common_color_fg_red(),
952 bt_common_color_reset());
953 print_plugin_comp_cls_opt(stderr
,
954 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
955 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
956 cfg
->cmd_data
.query
.cfg_component
->type
);
957 fprintf(stderr
, "\n");
962 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
963 cfg
->cmd_data
.query
.cfg_component
->params
,
964 &results
, &fail_reason
);
969 print_value(stdout
, results
, 0);
973 BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", "
974 "comp-cls-name=\"%s\", comp-cls-type=%d "
975 "object=\"%s\"", fail_reason
,
976 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
977 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
978 cfg
->cmd_data
.query
.cfg_component
->type
,
979 cfg
->cmd_data
.query
.object
->str
);
980 fprintf(stderr
, "%s%sFailed to query info to %s",
981 bt_common_color_bold(),
982 bt_common_color_fg_red(),
983 bt_common_color_reset());
984 print_plugin_comp_cls_opt(stderr
,
985 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
986 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
987 cfg
->cmd_data
.query
.cfg_component
->type
);
988 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
989 bt_common_color_bold(),
990 bt_common_color_fg_red(),
991 cfg
->cmd_data
.query
.object
->str
,
993 bt_common_color_reset());
997 bt_component_class_put_ref(comp_cls
);
998 bt_value_put_ref(results
);
1003 void print_component_class_help(const char *plugin_name
,
1004 const bt_component_class
*comp_cls
)
1006 const char *comp_class_name
=
1007 bt_component_class_get_name(comp_cls
);
1008 const char *comp_class_description
=
1009 bt_component_class_get_description(comp_cls
);
1010 const char *comp_class_help
=
1011 bt_component_class_get_help(comp_cls
);
1012 bt_component_class_type type
=
1013 bt_component_class_get_type(comp_cls
);
1015 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
1017 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1018 bt_common_color_reset(),
1019 comp_class_description
? comp_class_description
: "(None)");
1021 if (comp_class_help
) {
1022 printf("\n%s\n", comp_class_help
);
1027 int cmd_help(struct bt_config
*cfg
)
1030 const bt_plugin
*plugin
= NULL
;
1031 const bt_component_class
*needed_comp_cls
= NULL
;
1033 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1035 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
1036 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1037 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
1038 bt_common_color_bold(), bt_common_color_fg_red(),
1039 bt_common_color_fg_blue(),
1040 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1041 bt_common_color_reset());
1046 print_plugin_info(plugin
);
1047 printf(" %sSource component classes%s: %d\n",
1048 bt_common_color_bold(),
1049 bt_common_color_reset(),
1050 (int) bt_plugin_get_source_component_class_count(plugin
));
1051 printf(" %sFilter component classes%s: %d\n",
1052 bt_common_color_bold(),
1053 bt_common_color_reset(),
1054 (int) bt_plugin_get_filter_component_class_count(plugin
));
1055 printf(" %sSink component classes%s: %d\n",
1056 bt_common_color_bold(),
1057 bt_common_color_reset(),
1058 (int) bt_plugin_get_sink_component_class_count(plugin
));
1060 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
1061 /* Plugin help only */
1065 needed_comp_cls
= find_component_class(
1066 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1067 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1068 cfg
->cmd_data
.help
.cfg_component
->type
);
1069 if (!needed_comp_cls
) {
1070 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1071 "comp-cls-name=\"%s\", comp-cls-type=%d",
1072 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1073 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1074 cfg
->cmd_data
.help
.cfg_component
->type
);
1075 fprintf(stderr
, "\n%s%sCannot find component class %s",
1076 bt_common_color_bold(),
1077 bt_common_color_fg_red(),
1078 bt_common_color_reset());
1079 print_plugin_comp_cls_opt(stderr
,
1080 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1081 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1082 cfg
->cmd_data
.help
.cfg_component
->type
);
1083 fprintf(stderr
, "\n");
1089 print_component_class_help(
1090 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1094 bt_component_class_put_ref(needed_comp_cls
);
1095 bt_plugin_put_ref(plugin
);
1099 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
1101 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
1104 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
1105 const char *cc_type_name
, uint64_t count
,
1106 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
1107 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
1112 printf(" %s%s component classes%s: (none)\n",
1113 bt_common_color_bold(),
1115 bt_common_color_reset());
1118 printf(" %s%s component classes%s:\n",
1119 bt_common_color_bold(),
1121 bt_common_color_reset());
1124 for (i
= 0; i
< count
; i
++) {
1125 const bt_component_class
*comp_class
=
1126 spec_comp_cls_borrow_comp_cls_func(
1127 borrow_comp_cls_by_index_func(plugin
, i
));
1128 const char *comp_class_name
=
1129 bt_component_class_get_name(comp_class
);
1130 const char *comp_class_description
=
1131 bt_component_class_get_description(comp_class
);
1132 bt_component_class_type type
=
1133 bt_component_class_get_type(comp_class
);
1136 print_plugin_comp_cls_opt(stdout
,
1137 bt_plugin_get_name(plugin
), comp_class_name
,
1140 if (comp_class_description
) {
1141 printf(": %s", comp_class_description
);
1152 int cmd_list_plugins(struct bt_config
*cfg
)
1155 int plugins_count
, component_classes_count
= 0, i
;
1157 printf("From the following plugin paths:\n\n");
1158 print_value(stdout
, cfg
->plugin_paths
, 2);
1160 plugins_count
= loaded_plugins
->len
;
1161 if (plugins_count
== 0) {
1162 printf("No plugins found.\n");
1166 for (i
= 0; i
< plugins_count
; i
++) {
1167 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1169 component_classes_count
+=
1170 bt_plugin_get_source_component_class_count(plugin
) +
1171 bt_plugin_get_filter_component_class_count(plugin
) +
1172 bt_plugin_get_sink_component_class_count(plugin
);
1175 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1176 bt_common_color_bold(),
1177 component_classes_count
,
1178 bt_common_color_reset(),
1179 bt_common_color_bold(),
1181 bt_common_color_reset());
1183 for (i
= 0; i
< plugins_count
; i
++) {
1184 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1187 print_plugin_info(plugin
);
1188 cmd_list_plugins_print_component_classes(plugin
, "Source",
1189 bt_plugin_get_source_component_class_count(plugin
),
1190 (plugin_borrow_comp_cls_by_index_func_t
)
1191 bt_plugin_borrow_source_component_class_by_index_const
,
1192 (spec_comp_cls_borrow_comp_cls_func_t
)
1193 bt_component_class_source_as_component_class
);
1194 cmd_list_plugins_print_component_classes(plugin
, "Filter",
1195 bt_plugin_get_filter_component_class_count(plugin
),
1196 (plugin_borrow_comp_cls_by_index_func_t
)
1197 bt_plugin_borrow_filter_component_class_by_index_const
,
1198 (spec_comp_cls_borrow_comp_cls_func_t
)
1199 bt_component_class_filter_as_component_class
);
1200 cmd_list_plugins_print_component_classes(plugin
, "Sink",
1201 bt_plugin_get_sink_component_class_count(plugin
),
1202 (plugin_borrow_comp_cls_by_index_func_t
)
1203 bt_plugin_borrow_sink_component_class_by_index_const
,
1204 (spec_comp_cls_borrow_comp_cls_func_t
)
1205 bt_component_class_sink_as_component_class
);
1213 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1216 const bt_component_class
*comp_cls
= NULL
;
1217 const bt_value
*results
= NULL
;
1218 bt_value
*params
= NULL
;
1219 const bt_value
*map
= NULL
;
1220 const bt_value
*v
= NULL
;
1221 static const char * const plugin_name
= "ctf";
1222 static const char * const comp_cls_name
= "lttng-live";
1223 static const bt_component_class_type comp_cls_type
=
1224 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1225 int64_t array_size
, i
;
1226 const char *fail_reason
= NULL
;
1227 FILE *out_stream
= stdout
;
1229 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1230 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1233 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1234 "comp-cls-name=\"%s\", comp-cls-type=%d",
1235 plugin_name
, comp_cls_name
,
1236 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1237 fprintf(stderr
, "%s%sCannot find component class %s",
1238 bt_common_color_bold(),
1239 bt_common_color_fg_red(),
1240 bt_common_color_reset());
1241 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1242 comp_cls_name
, comp_cls_type
);
1243 fprintf(stderr
, "\n");
1247 params
= bt_value_map_create();
1252 ret
= bt_value_map_insert_string_entry(params
, "url",
1253 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1258 ret
= query(cfg
, comp_cls
, "sessions", params
,
1259 &results
, &fail_reason
);
1266 if (!bt_value_is_array(results
)) {
1267 BT_LOGE_STR("Expecting an array for sessions query.");
1268 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1269 bt_common_color_bold(),
1270 bt_common_color_fg_red(),
1271 bt_common_color_reset());
1275 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1277 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1281 BT_LOGE_ERRNO("Cannot open file for writing",
1283 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1288 array_size
= bt_value_array_get_size(results
);
1289 for (i
= 0; i
< array_size
; i
++) {
1290 const char *url_text
;
1291 int64_t timer_us
, streams
, clients
;
1293 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1295 BT_LOGE_STR("Unexpected empty array entry.");
1298 if (!bt_value_is_map(map
)) {
1299 BT_LOGE_STR("Unexpected entry type.");
1303 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1305 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1308 url_text
= bt_value_string_get(v
);
1309 fprintf(out_stream
, "%s", url_text
);
1310 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1312 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1315 timer_us
= bt_value_signed_integer_get(v
);
1316 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1317 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1319 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1322 streams
= bt_value_signed_integer_get(v
);
1323 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1324 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1326 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1329 clients
= bt_value_signed_integer_get(v
);
1330 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1336 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1337 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1338 bt_common_color_bold(),
1339 bt_common_color_fg_red(),
1341 bt_common_color_reset());
1347 bt_value_put_ref(results
);
1348 bt_value_put_ref(params
);
1349 bt_component_class_put_ref(comp_cls
);
1351 if (out_stream
&& out_stream
!= stdout
) {
1352 int fclose_ret
= fclose(out_stream
);
1355 BT_LOGE_ERRNO("Cannot close file stream",
1357 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1365 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1368 const bt_component_class
*comp_cls
= NULL
;
1369 const bt_value
*results
= NULL
;
1370 bt_value
*params
= NULL
;
1371 const bt_value
*metadata_text_value
= NULL
;
1372 const char *metadata_text
= NULL
;
1373 static const char * const plugin_name
= "ctf";
1374 static const char * const comp_cls_name
= "fs";
1375 static const bt_component_class_type comp_cls_type
=
1376 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1377 const char *fail_reason
= NULL
;
1378 FILE *out_stream
= stdout
;
1380 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1381 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1384 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1385 "comp-cls-name=\"%s\", comp-cls-type=%d",
1386 plugin_name
, comp_cls_name
,
1387 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1388 fprintf(stderr
, "%s%sCannot find component class %s",
1389 bt_common_color_bold(),
1390 bt_common_color_fg_red(),
1391 bt_common_color_reset());
1392 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1393 comp_cls_name
, comp_cls_type
);
1394 fprintf(stderr
, "\n");
1399 params
= bt_value_map_create();
1405 ret
= bt_value_map_insert_string_entry(params
, "path",
1406 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1412 ret
= query(cfg
, comp_cls
, "metadata-info",
1413 params
, &results
, &fail_reason
);
1418 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1420 if (!metadata_text_value
) {
1421 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1426 metadata_text
= bt_value_string_get(metadata_text_value
);
1428 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1430 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1434 BT_LOGE_ERRNO("Cannot open file for writing",
1436 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1441 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1443 BT_LOGE("Cannot write whole metadata text to output stream: "
1454 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1455 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1456 bt_common_color_bold(),
1457 bt_common_color_fg_red(),
1459 bt_common_color_reset());
1462 bt_value_put_ref(results
);
1463 bt_value_put_ref(params
);
1464 bt_component_class_put_ref(comp_cls
);
1466 if (out_stream
&& out_stream
!= stdout
) {
1467 int fclose_ret
= fclose(out_stream
);
1470 BT_LOGE_ERRNO("Cannot close file stream",
1472 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1480 char *instance_name
;
1484 struct trace_range
{
1485 uint64_t intersection_range_begin_ns
;
1486 uint64_t intersection_range_end_ns
;
1490 guint
port_id_hash(gconstpointer v
)
1492 const struct port_id
*id
= v
;
1494 BT_ASSERT(id
->instance_name
);
1495 BT_ASSERT(id
->port_name
);
1497 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1501 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1503 const struct port_id
*id1
= v1
;
1504 const struct port_id
*id2
= v2
;
1506 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1507 !strcmp(id1
->port_name
, id2
->port_name
);
1511 void port_id_destroy(gpointer data
)
1513 struct port_id
*id
= data
;
1515 free(id
->instance_name
);
1516 free(id
->port_name
);
1521 void trace_range_destroy(gpointer data
)
1526 struct cmd_run_ctx
{
1528 GHashTable
*src_components
;
1531 GHashTable
*flt_components
;
1534 GHashTable
*sink_components
;
1540 struct bt_config
*cfg
;
1544 bool stream_intersection_mode
;
1547 * Association of struct port_id -> struct trace_range.
1549 GHashTable
*intersections
;
1552 /* Returns a timestamp of the form "(-)s.ns" */
1554 char *s_from_ns(int64_t ns
)
1559 int64_t ts_sec_abs
, ts_nsec_abs
;
1560 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1561 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1563 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1564 is_negative
= false;
1565 ts_sec_abs
= ts_sec
;
1566 ts_nsec_abs
= ts_nsec
;
1567 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1568 is_negative
= false;
1569 ts_sec_abs
= ts_sec
- 1;
1570 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1571 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1573 ts_sec_abs
= ts_sec
;
1574 ts_nsec_abs
= -ts_nsec
;
1575 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1577 ts_sec_abs
= -(ts_sec
+ 1);
1578 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1579 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1581 ts_sec_abs
= -ts_sec
;
1582 ts_nsec_abs
= ts_nsec
;
1583 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1585 ts_sec_abs
= -ts_sec
;
1586 ts_nsec_abs
= -ts_nsec
;
1589 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1590 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1598 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1599 struct cmd_run_ctx
*ctx
,
1600 const bt_component
*upstream_comp
,
1601 const bt_port_output
*out_upstream_port
,
1602 struct bt_config_connection
*cfg_conn
)
1604 typedef uint64_t (*input_port_count_func_t
)(void *);
1605 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1606 const void *, uint64_t);
1607 const bt_port
*upstream_port
=
1608 bt_port_output_as_port_const(out_upstream_port
);
1611 GQuark downstreamp_comp_name_quark
;
1612 void *downstream_comp
;
1613 uint64_t downstream_port_count
;
1615 input_port_count_func_t port_count_fn
;
1616 borrow_input_port_by_index_func_t port_by_index_fn
;
1617 bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1618 bool insert_trimmer
= false;
1619 bt_value
*trimmer_params
= NULL
;
1620 char *intersection_begin
= NULL
;
1621 char *intersection_end
= NULL
;
1622 const bt_component_filter
*trimmer
= NULL
;
1623 const bt_component_class_filter
*trimmer_class
= NULL
;
1624 const bt_port_input
*trimmer_input
= NULL
;
1625 const bt_port_output
*trimmer_output
= NULL
;
1627 if (ctx
->intersections
&&
1628 bt_component_get_class_type(upstream_comp
) ==
1629 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1630 struct trace_range
*range
;
1631 struct port_id port_id
= {
1632 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1633 .port_name
= (char *) bt_port_get_name(upstream_port
)
1636 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1640 range
= (struct trace_range
*) g_hash_table_lookup(
1641 ctx
->intersections
, &port_id
);
1643 bt_value_status status
;
1645 intersection_begin
= s_from_ns(
1646 range
->intersection_range_begin_ns
);
1647 intersection_end
= s_from_ns(
1648 range
->intersection_range_end_ns
);
1649 if (!intersection_begin
|| !intersection_end
) {
1650 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1654 insert_trimmer
= true;
1655 trimmer_params
= bt_value_map_create();
1656 if (!trimmer_params
) {
1660 status
= bt_value_map_insert_string_entry(
1661 trimmer_params
, "begin", intersection_begin
);
1662 if (status
!= BT_VALUE_STATUS_OK
) {
1665 status
= bt_value_map_insert_string_entry(
1667 "end", intersection_end
);
1668 if (status
!= BT_VALUE_STATUS_OK
) {
1673 trimmer_class
= find_filter_component_class("utils", "trimmer");
1674 if (!trimmer_class
) {
1679 BT_LOGI("Connecting upstream port to the next available downstream port: "
1680 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1681 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1682 upstream_port
, bt_port_get_name(upstream_port
),
1683 cfg_conn
->downstream_comp_name
->str
,
1684 cfg_conn
->arg
->str
);
1685 downstreamp_comp_name_quark
= g_quark_from_string(
1686 cfg_conn
->downstream_comp_name
->str
);
1687 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1688 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1689 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1690 port_count_fn
= (input_port_count_func_t
)
1691 bt_component_filter_get_input_port_count
;
1692 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1693 bt_component_filter_borrow_input_port_by_index_const
;
1695 if (!downstream_comp
) {
1696 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1697 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1698 port_count_fn
= (input_port_count_func_t
)
1699 bt_component_sink_get_input_port_count
;
1700 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1701 bt_component_sink_borrow_input_port_by_index_const
;
1704 if (!downstream_comp
) {
1705 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1706 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1707 cfg_conn
->arg
->str
);
1708 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1709 cfg_conn
->arg
->str
);
1713 downstream_port_count
= port_count_fn(downstream_comp
);
1715 for (i
= 0; i
< downstream_port_count
; i
++) {
1716 const bt_port_input
*in_downstream_port
=
1717 port_by_index_fn(downstream_comp
, i
);
1718 const bt_port
*downstream_port
=
1719 bt_port_input_as_port_const(in_downstream_port
);
1720 const char *upstream_port_name
;
1721 const char *downstream_port_name
;
1723 BT_ASSERT(downstream_port
);
1725 /* Skip port if it's already connected. */
1726 if (bt_port_is_connected(downstream_port
)) {
1727 BT_LOGI("Skipping downstream port: already connected: "
1728 "port-addr=%p, port-name=\"%s\"",
1730 bt_port_get_name(downstream_port
));
1734 downstream_port_name
= bt_port_get_name(downstream_port
);
1735 BT_ASSERT(downstream_port_name
);
1736 upstream_port_name
= bt_port_get_name(upstream_port
);
1737 BT_ASSERT(upstream_port_name
);
1739 if (!bt_common_star_glob_match(
1740 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1741 downstream_port_name
, SIZE_MAX
)) {
1745 if (insert_trimmer
) {
1747 * In order to insert the trimmer between the
1748 * two components that were being connected, we
1749 * create a connection configuration entry which
1750 * describes a connection from the trimmer's
1751 * output to the original input that was being
1754 * Hence, the creation of the trimmer will cause
1755 * the graph "new port" listener to establish
1756 * all downstream connections as its output port
1757 * is connected. We will then establish the
1758 * connection between the original upstream
1759 * source and the trimmer.
1761 char *trimmer_name
= NULL
;
1762 bt_graph_status graph_status
;
1764 ret
= asprintf(&trimmer_name
,
1765 "stream-intersection-trimmer-%s",
1766 upstream_port_name
);
1772 ctx
->connect_ports
= false;
1773 graph_status
= bt_graph_add_filter_component(
1774 ctx
->graph
, trimmer_class
, trimmer_name
,
1775 trimmer_params
, ctx
->cfg
->log_level
,
1778 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1784 bt_component_filter_borrow_input_port_by_index_const(
1786 if (!trimmer_input
) {
1790 bt_component_filter_borrow_output_port_by_index_const(
1792 if (!trimmer_output
) {
1797 * Replace the current downstream port by the trimmer's
1800 in_downstream_port
= trimmer_input
;
1802 bt_port_input_as_port_const(in_downstream_port
);
1803 downstream_port_name
= bt_port_get_name(
1805 BT_ASSERT(downstream_port_name
);
1808 /* We have a winner! */
1809 status
= bt_graph_connect_ports(ctx
->graph
,
1810 out_upstream_port
, in_downstream_port
, NULL
);
1811 downstream_port
= NULL
;
1813 case BT_GRAPH_STATUS_OK
:
1815 case BT_GRAPH_STATUS_CANCELED
:
1816 BT_LOGI_STR("Graph was canceled by user.");
1817 status
= BT_GRAPH_STATUS_OK
;
1819 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1820 BT_LOGE("A component refused a connection to one of its ports: "
1821 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1822 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1823 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1824 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1826 upstream_comp
, bt_component_get_name(upstream_comp
),
1827 upstream_port
, bt_port_get_name(upstream_port
),
1828 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1829 downstream_port
, downstream_port_name
,
1830 cfg_conn
->arg
->str
);
1832 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1833 bt_port_get_name(upstream_port
),
1834 downstream_port_name
,
1835 cfg_conn
->arg
->str
);
1838 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1839 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1840 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1841 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1842 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1844 upstream_comp
, bt_component_get_name(upstream_comp
),
1845 upstream_port
, bt_port_get_name(upstream_port
),
1846 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1847 downstream_port
, downstream_port_name
,
1848 cfg_conn
->arg
->str
);
1850 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1851 bt_port_get_name(upstream_port
),
1852 downstream_port_name
,
1853 cfg_conn
->arg
->str
);
1857 BT_LOGI("Connected component ports: "
1858 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1859 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1860 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1861 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1863 upstream_comp
, bt_component_get_name(upstream_comp
),
1864 upstream_port
, bt_port_get_name(upstream_port
),
1865 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1866 downstream_port
, downstream_port_name
,
1867 cfg_conn
->arg
->str
);
1869 if (insert_trimmer
) {
1871 * The first connection, from the source to the trimmer,
1872 * has been done. We now connect the trimmer to the
1873 * original downstream port.
1875 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1877 bt_component_filter_as_component_const(trimmer
),
1878 trimmer_output
, cfg_conn
);
1882 ctx
->connect_ports
= true;
1886 * We found a matching downstream port: the search is
1892 /* No downstream port found */
1893 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1894 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1895 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1896 upstream_port
, bt_port_get_name(upstream_port
),
1897 cfg_conn
->downstream_comp_name
->str
,
1898 cfg_conn
->arg
->str
);
1900 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1901 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1907 free(intersection_begin
);
1908 free(intersection_end
);
1909 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1910 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1911 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1916 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1917 const bt_port_output
*upstream_port
)
1920 const char *upstream_port_name
;
1921 const char *upstream_comp_name
;
1922 const bt_component
*upstream_comp
= NULL
;
1926 BT_ASSERT(upstream_port
);
1927 upstream_port_name
= bt_port_get_name(
1928 bt_port_output_as_port_const(upstream_port
));
1929 BT_ASSERT(upstream_port_name
);
1930 upstream_comp
= bt_port_borrow_component_const(
1931 bt_port_output_as_port_const(upstream_port
));
1932 if (!upstream_comp
) {
1933 BT_LOGW("Upstream port to connect is not part of a component: "
1934 "port-addr=%p, port-name=\"%s\"",
1935 upstream_port
, upstream_port_name
);
1940 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1941 BT_ASSERT(upstream_comp_name
);
1942 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1943 "port-addr=%p, port-name=\"%s\"",
1944 upstream_comp
, upstream_comp_name
,
1945 upstream_port
, upstream_port_name
);
1947 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1948 struct bt_config_connection
*cfg_conn
=
1950 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1952 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1953 upstream_comp_name
)) {
1957 if (!bt_common_star_glob_match(
1958 cfg_conn
->upstream_port_glob
->str
,
1959 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1963 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1964 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1966 BT_LOGE("Cannot connect upstream port: "
1967 "port-addr=%p, port-name=\"%s\"",
1969 upstream_port_name
);
1971 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1974 cfg_conn
->arg
->str
);
1980 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1981 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1982 upstream_port_name
);
1984 "Cannot create connection: upstream port `%s` does not match any connection\n",
1985 upstream_port_name
);
1995 bt_graph_listener_status
1996 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1997 const bt_port_output
*out_port
)
1999 const bt_component
*comp
;
2000 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
2001 bt_graph_listener_status ret
= BT_GRAPH_LISTENER_STATUS_OK
;
2003 comp
= bt_port_borrow_component_const(port
);
2004 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
2005 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
2006 comp
, comp
? bt_component_get_name(comp
) : "",
2007 port
, bt_port_get_name(port
));
2009 if (!ctx
->connect_ports
) {
2014 BT_LOGW_STR("Port has no component.");
2018 if (bt_port_is_connected(port
)) {
2019 BT_LOGW_STR("Port is already connected.");
2023 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
2024 BT_LOGF_STR("Cannot connect upstream port.");
2025 fprintf(stderr
, "Added port could not be connected: aborting\n");
2026 ret
= BT_GRAPH_LISTENER_STATUS_ERROR
;
2035 bt_graph_listener_status
graph_source_output_port_added_listener(
2036 const bt_component_source
*component
,
2037 const bt_port_output
*port
, void *data
)
2039 return graph_output_port_added_listener(data
, port
);
2043 bt_graph_listener_status
graph_filter_output_port_added_listener(
2044 const bt_component_filter
*component
,
2045 const bt_port_output
*port
, void *data
)
2047 return graph_output_port_added_listener(data
, port
);
2051 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
2057 if (ctx
->src_components
) {
2058 g_hash_table_destroy(ctx
->src_components
);
2059 ctx
->src_components
= NULL
;
2062 if (ctx
->flt_components
) {
2063 g_hash_table_destroy(ctx
->flt_components
);
2064 ctx
->flt_components
= NULL
;
2067 if (ctx
->sink_components
) {
2068 g_hash_table_destroy(ctx
->sink_components
);
2069 ctx
->sink_components
= NULL
;
2072 if (ctx
->intersections
) {
2073 g_hash_table_destroy(ctx
->intersections
);
2074 ctx
->intersections
= NULL
;
2077 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
2083 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2086 bt_graph_status status
;
2089 ctx
->connect_ports
= false;
2090 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
2091 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2092 if (!ctx
->src_components
) {
2096 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
2097 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2098 if (!ctx
->flt_components
) {
2102 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
2103 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2104 if (!ctx
->sink_components
) {
2108 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2109 ctx
->stream_intersection_mode
= true;
2110 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2111 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2112 if (!ctx
->intersections
) {
2117 ctx
->graph
= bt_graph_create();
2122 the_graph
= ctx
->graph
;
2123 status
= bt_graph_add_source_component_output_port_added_listener(
2124 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
2126 if (status
!= BT_GRAPH_STATUS_OK
) {
2127 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2131 status
= bt_graph_add_filter_component_output_port_added_listener(
2132 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
2134 if (status
!= BT_GRAPH_STATUS_OK
) {
2135 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2142 cmd_run_ctx_destroy(ctx
);
2150 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2151 struct bt_config_component
*cfg_comp
,
2152 const bt_component_class_source
*src_comp_cls
)
2156 int64_t trace_count
;
2157 const char *path
= NULL
;
2158 const bt_value
*query_result
= NULL
;
2159 const bt_value
*trace_info
= NULL
;
2160 const bt_value
*intersection_range
= NULL
;
2161 const bt_value
*intersection_begin
= NULL
;
2162 const bt_value
*intersection_end
= NULL
;
2163 const bt_value
*stream_infos
= NULL
;
2164 const bt_value
*stream_info
= NULL
;
2165 struct port_id
*port_id
= NULL
;
2166 struct trace_range
*trace_range
= NULL
;
2167 const char *fail_reason
= NULL
;
2168 const bt_component_class
*comp_cls
=
2169 bt_component_class_source_as_component_class_const(src_comp_cls
);
2171 ret
= query(ctx
->cfg
, comp_cls
, "trace-info",
2172 cfg_comp
->params
, &query_result
,
2175 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2176 "comp-class-name=\"%s\"", fail_reason
,
2177 bt_component_class_get_name(comp_cls
));
2182 BT_ASSERT(query_result
);
2184 if (!bt_value_is_array(query_result
)) {
2185 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2186 "component-class-name=%s",
2187 bt_component_class_get_name(comp_cls
));
2192 trace_count
= bt_value_array_get_size(query_result
);
2193 if (trace_count
< 0) {
2198 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2200 uint64_t stream_idx
;
2201 int64_t stream_count
;
2203 trace_info
= bt_value_array_borrow_element_by_index_const(
2204 query_result
, trace_idx
);
2205 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2207 BT_LOGD_STR("Cannot retrieve trace from query result.");
2211 intersection_range
= bt_value_map_borrow_entry_value_const(
2212 trace_info
, "intersection-range-ns");
2213 if (!intersection_range
) {
2215 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2219 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
2221 if (!intersection_begin
) {
2223 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2227 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
2229 if (!intersection_end
) {
2231 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2235 begin
= bt_value_signed_integer_get(intersection_begin
);
2236 end
= bt_value_signed_integer_get(intersection_end
);
2238 if (begin
< 0 || end
< 0 || end
< begin
) {
2239 BT_LOGW("Invalid trace stream intersection values: "
2240 "intersection-range-ns:begin=%" PRId64
2241 ", intersection-range-ns:end=%" PRId64
,
2247 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
2249 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2251 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2255 stream_count
= bt_value_array_get_size(stream_infos
);
2256 if (stream_count
< 0) {
2261 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2262 const bt_value
*port_name
;
2264 port_id
= g_new0(struct port_id
, 1);
2267 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2270 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2271 if (!port_id
->instance_name
) {
2273 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2277 trace_range
= g_new0(struct trace_range
, 1);
2280 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2283 trace_range
->intersection_range_begin_ns
= begin
;
2284 trace_range
->intersection_range_end_ns
= end
;
2286 stream_info
= bt_value_array_borrow_element_by_index_const(
2287 stream_infos
, stream_idx
);
2288 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2290 BT_LOGE_STR("Cannot retrieve stream informations from trace in query result.");
2294 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2295 if (!port_name
|| !bt_value_is_string(port_name
)) {
2297 BT_LOGE_STR("Cannot retrieve port name in query result.");
2301 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2302 if (!port_id
->port_name
) {
2304 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2308 BT_LOGD("Inserting stream intersection ");
2310 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2320 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2321 bt_common_color_bold(),
2322 bt_common_color_fg_yellow(),
2323 path
? path
: "(unknown)",
2324 bt_common_color_reset());
2326 bt_value_put_ref(query_result
);
2328 g_free(trace_range
);
2333 int cmd_run_ctx_create_components_from_config_components(
2334 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2337 const void *comp_cls
= NULL
;
2338 const void *comp
= NULL
;
2341 for (i
= 0; i
< cfg_components
->len
; i
++) {
2342 struct bt_config_component
*cfg_comp
=
2343 g_ptr_array_index(cfg_components
, i
);
2346 switch (cfg_comp
->type
) {
2347 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2348 comp_cls
= find_source_component_class(
2349 cfg_comp
->plugin_name
->str
,
2350 cfg_comp
->comp_cls_name
->str
);
2352 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2353 comp_cls
= find_filter_component_class(
2354 cfg_comp
->plugin_name
->str
,
2355 cfg_comp
->comp_cls_name
->str
);
2357 case BT_COMPONENT_CLASS_TYPE_SINK
:
2358 comp_cls
= find_sink_component_class(
2359 cfg_comp
->plugin_name
->str
,
2360 cfg_comp
->comp_cls_name
->str
);
2367 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2368 "comp-cls-name=\"%s\", comp-cls-type=%d",
2369 cfg_comp
->plugin_name
->str
,
2370 cfg_comp
->comp_cls_name
->str
,
2372 fprintf(stderr
, "%s%sCannot find component class %s",
2373 bt_common_color_bold(),
2374 bt_common_color_fg_red(),
2375 bt_common_color_reset());
2376 print_plugin_comp_cls_opt(stderr
,
2377 cfg_comp
->plugin_name
->str
,
2378 cfg_comp
->comp_cls_name
->str
,
2380 fprintf(stderr
, "\n");
2384 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_VERBOSE
);
2386 switch (cfg_comp
->type
) {
2387 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2388 ret
= bt_graph_add_source_component(ctx
->graph
,
2389 comp_cls
, cfg_comp
->instance_name
->str
,
2390 cfg_comp
->params
, cfg_comp
->log_level
,
2393 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2394 ret
= bt_graph_add_filter_component(ctx
->graph
,
2395 comp_cls
, cfg_comp
->instance_name
->str
,
2396 cfg_comp
->params
, cfg_comp
->log_level
,
2399 case BT_COMPONENT_CLASS_TYPE_SINK
:
2400 ret
= bt_graph_add_sink_component(ctx
->graph
,
2401 comp_cls
, cfg_comp
->instance_name
->str
,
2402 cfg_comp
->params
, cfg_comp
->log_level
,
2410 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2411 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2413 cfg_comp
->plugin_name
->str
,
2414 cfg_comp
->comp_cls_name
->str
,
2415 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2416 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2417 bt_common_color_bold(),
2418 bt_common_color_fg_red(),
2419 cfg_comp
->instance_name
->str
,
2420 bt_common_color_reset());
2424 if (ctx
->stream_intersection_mode
&&
2425 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2426 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2432 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2433 comp
, cfg_comp
->instance_name
->str
);
2434 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2435 BT_ASSERT(quark
> 0);
2437 switch (cfg_comp
->type
) {
2438 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2439 g_hash_table_insert(ctx
->src_components
,
2440 GUINT_TO_POINTER(quark
), (void *) comp
);
2442 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2443 g_hash_table_insert(ctx
->flt_components
,
2444 GUINT_TO_POINTER(quark
), (void *) comp
);
2446 case BT_COMPONENT_CLASS_TYPE_SINK
:
2447 g_hash_table_insert(ctx
->sink_components
,
2448 GUINT_TO_POINTER(quark
), (void *) comp
);
2455 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2464 bt_object_put_ref(comp
);
2465 bt_object_put_ref(comp_cls
);
2470 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2475 * Make sure that, during this phase, our graph's "port added"
2476 * listener does not connect ports while we are creating the
2477 * components because we have a special, initial phase for
2480 ctx
->connect_ports
= false;
2482 ret
= cmd_run_ctx_create_components_from_config_components(
2483 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2489 ret
= cmd_run_ctx_create_components_from_config_components(
2490 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2496 ret
= cmd_run_ctx_create_components_from_config_components(
2497 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2507 typedef uint64_t (*output_port_count_func_t
)(const void *);
2508 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2509 const void *, uint64_t);
2512 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2513 void *comp
, output_port_count_func_t port_count_fn
,
2514 borrow_output_port_by_index_func_t port_by_index_fn
)
2520 count
= port_count_fn(comp
);
2522 for (i
= 0; i
< count
; i
++) {
2523 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2525 BT_ASSERT(upstream_port
);
2526 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2537 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2540 GHashTableIter iter
;
2541 gpointer g_name_quark
, g_comp
;
2543 ctx
->connect_ports
= true;
2544 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2546 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2547 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2548 (output_port_count_func_t
)
2549 bt_component_source_get_output_port_count
,
2550 (borrow_output_port_by_index_func_t
)
2551 bt_component_source_borrow_output_port_by_index_const
);
2557 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2559 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2560 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2561 (output_port_count_func_t
)
2562 bt_component_filter_get_output_port_count
,
2563 (borrow_output_port_by_index_func_t
)
2564 bt_component_filter_borrow_output_port_by_index_const
);
2575 const char *bt_graph_status_str(bt_graph_status status
)
2578 case BT_GRAPH_STATUS_OK
:
2579 return "BT_GRAPH_STATUS_OK";
2580 case BT_GRAPH_STATUS_END
:
2581 return "BT_GRAPH_STATUS_END";
2582 case BT_GRAPH_STATUS_AGAIN
:
2583 return "BT_GRAPH_STATUS_AGAIN";
2584 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
2585 return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION";
2586 case BT_GRAPH_STATUS_CANCELED
:
2587 return "BT_GRAPH_STATUS_CANCELED";
2588 case BT_GRAPH_STATUS_ERROR
:
2589 return "BT_GRAPH_STATUS_ERROR";
2590 case BT_GRAPH_STATUS_NOMEM
:
2591 return "BT_GRAPH_STATUS_NOMEM";
2598 int cmd_run(struct bt_config
*cfg
)
2601 struct cmd_run_ctx ctx
= { 0 };
2603 /* Initialize the command's context and the graph object */
2604 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2605 BT_LOGE_STR("Cannot initialize the command's context.");
2606 fprintf(stderr
, "Cannot initialize the command's context\n");
2611 BT_LOGI_STR("Canceled by user before creating components.");
2615 BT_LOGI_STR("Creating components.");
2617 /* Create the requested component instances */
2618 if (cmd_run_ctx_create_components(&ctx
)) {
2619 BT_LOGE_STR("Cannot create components.");
2620 fprintf(stderr
, "Cannot create components\n");
2625 BT_LOGI_STR("Canceled by user before connecting components.");
2629 BT_LOGI_STR("Connecting components.");
2631 /* Connect the initially visible component ports */
2632 if (cmd_run_ctx_connect_ports(&ctx
)) {
2633 BT_LOGE_STR("Cannot connect initial component ports.");
2634 fprintf(stderr
, "Cannot connect initial component ports\n");
2639 BT_LOGI_STR("Canceled by user before running the graph.");
2643 BT_LOGI_STR("Running the graph.");
2647 bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2650 * Reset console in case something messed with console
2651 * codes during the graph's execution.
2653 printf("%s", bt_common_color_reset());
2655 fprintf(stderr
, "%s", bt_common_color_reset());
2656 BT_LOGV("bt_graph_run() returned: status=%s",
2657 bt_graph_status_str(graph_status
));
2659 switch (graph_status
) {
2660 case BT_GRAPH_STATUS_OK
:
2662 case BT_GRAPH_STATUS_CANCELED
:
2663 BT_LOGI_STR("Graph was canceled by user.");
2665 case BT_GRAPH_STATUS_AGAIN
:
2666 if (bt_graph_is_canceled(ctx
.graph
)) {
2667 BT_LOGI_STR("Graph was canceled by user.");
2671 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2672 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2674 cfg
->cmd_data
.run
.retry_duration_us
);
2676 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2677 if (bt_graph_is_canceled(ctx
.graph
)) {
2678 BT_LOGI_STR("Graph was canceled by user.");
2684 case BT_GRAPH_STATUS_END
:
2687 BT_LOGE_STR("Graph failed to complete successfully");
2688 fprintf(stderr
, "Graph failed to complete successfully\n");
2701 cmd_run_ctx_destroy(&ctx
);
2706 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2708 const char *env_clash
;
2710 if (!cfg
->command_name
) {
2714 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2715 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2719 if (g_file_test(cfg
->command_name
,
2720 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2721 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2723 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2725 fprintf(stderr
, "\n");
2726 fprintf(stderr
, " babeltrace2 convert %s [OPTIONS]\n",
2732 void init_log_level(void)
2734 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2738 void set_auto_log_levels(struct bt_config
*cfg
)
2740 const char **env_var_name
;
2743 * Override the configuration's default log level if
2744 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2745 * are found for backward compatibility with legacy Babetrace 1.
2747 if (getenv("BABELTRACE_DEBUG") &&
2748 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2749 cfg
->log_level
= BT_LOG_VERBOSE
;
2750 } else if (getenv("BABELTRACE_VERBOSE") &&
2751 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2752 cfg
->log_level
= BT_LOG_INFO
;
2756 * Set log levels according to --debug or --verbose. For
2757 * backward compatibility, --debug is more verbose than
2760 * --verbose: INFO log level
2761 * --debug: VERBOSE log level (includes DEBUG, which is
2762 * is less verbose than VERBOSE in the internal
2763 * logging framework)
2765 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2767 bt_logging_set_global_level(BT_LOG_INFO
);
2768 } else if (cfg
->debug
) {
2769 bt_logging_set_global_level(BT_LOG_VERBOSE
);
2772 * Set library's default log level if not
2773 * explicitly specified.
2775 bt_logging_set_global_level(cfg
->log_level
);
2779 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2781 bt_cli_log_level
= BT_LOG_INFO
;
2782 } else if (cfg
->debug
) {
2783 bt_cli_log_level
= BT_LOG_VERBOSE
;
2786 * Set CLI's default log level if not explicitly
2789 bt_cli_log_level
= cfg
->log_level
;
2793 env_var_name
= log_level_env_var_names
;
2795 while (*env_var_name
) {
2796 if (!getenv(*env_var_name
)) {
2798 g_setenv(*env_var_name
, "I", 1);
2799 } else if (cfg
->debug
) {
2800 g_setenv(*env_var_name
, "V", 1);
2802 char val
[2] = { 0 };
2805 * Set module's default log level if not
2806 * explicitly specified.
2808 val
[0] = bt_log_get_letter_from_level(
2810 g_setenv(*env_var_name
, val
, 1);
2818 int main(int argc
, const char **argv
)
2822 struct bt_config
*cfg
;
2825 set_signal_handler();
2827 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2830 /* Quit without errors; typically usage/version */
2832 BT_LOGI_STR("Quitting without errors.");
2837 BT_LOGE("Command-line error: retcode=%d", retcode
);
2842 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2843 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2848 set_auto_log_levels(cfg
);
2851 if (cfg
->command_needs_plugins
) {
2852 ret
= load_all_plugins(cfg
->plugin_paths
);
2854 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2860 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2861 cfg
->command
, cfg
->command_name
);
2863 switch (cfg
->command
) {
2864 case BT_CONFIG_COMMAND_RUN
:
2867 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2868 ret
= cmd_list_plugins(cfg
);
2870 case BT_CONFIG_COMMAND_HELP
:
2871 ret
= cmd_help(cfg
);
2873 case BT_CONFIG_COMMAND_QUERY
:
2874 ret
= cmd_query(cfg
);
2876 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2877 ret
= cmd_print_ctf_metadata(cfg
);
2879 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2880 ret
= cmd_print_lttng_live_sessions(cfg
);
2883 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2887 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2888 cfg
->command
, cfg
->command_name
, ret
);
2889 warn_command_name_and_directory_clash(cfg
);
2890 retcode
= ret
? 1 : 0;
2893 BT_OBJECT_PUT_REF_AND_RESET(cfg
);