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_COMMON_LOG_LEVEL",
53 "BABELTRACE_COMPAT_LOG_LEVEL",
54 "BABELTRACE_CTFSER_LOG_LEVEL",
55 "BABELTRACE_FD_CACHE_LOG_LEVEL",
56 "BABELTRACE_FLT_LTTNG_UTILS_DEBUG_INFO_LOG_LEVEL",
57 "BABELTRACE_FLT_UTILS_COUNTER_LOG_LEVEL",
58 "BABELTRACE_FLT_UTILS_MUXER_LOG_LEVEL",
59 "BABELTRACE_FLT_UTILS_TRIMMER_LOG_LEVEL",
60 "BABELTRACE_PLUGIN_CTF_BFCR_LOG_LEVEL",
61 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
62 "BABELTRACE_PLUGIN_CTF_MSG_ITER_LOG_LEVEL",
63 "BABELTRACE_PLUGIN_CTF_UTILS_LOG_LEVEL",
64 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
65 "BABELTRACE_SINK_CTF_FS_LOG_LEVEL",
66 "BABELTRACE_SINK_TEXT_DETAILS_LOG_LEVEL",
67 "BABELTRACE_SRC_CTF_FS_LOG_LEVEL",
68 "BABELTRACE_SRC_CTF_LTTNG_LIVE_LOG_LEVEL",
69 "BABELTRACE_SRC_TEXT_DMESG_LOG_LEVEL",
73 /* Application's processing graph (weak) */
74 static bt_graph
*the_graph
;
75 static bt_query_executor
*the_query_executor
;
76 static bool canceled
= false;
78 GPtrArray
*loaded_plugins
;
85 BOOL WINAPI
signal_handler(DWORD signal
) {
87 bt_graph_cancel(the_graph
);
96 void set_signal_handler(void)
98 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
99 BT_LOGE("Failed to set the Ctrl+C handler.");
103 #else /* __MINGW32__ */
106 void signal_handler(int signum
)
108 if (signum
!= SIGINT
) {
113 bt_graph_cancel(the_graph
);
116 if (the_query_executor
) {
117 bt_query_executor_cancel(the_query_executor
);
124 void set_signal_handler(void)
126 struct sigaction new_action
, old_action
;
128 new_action
.sa_handler
= signal_handler
;
129 sigemptyset(&new_action
.sa_mask
);
130 new_action
.sa_flags
= 0;
131 sigaction(SIGINT
, NULL
, &old_action
);
133 if (old_action
.sa_handler
!= SIG_IGN
) {
134 sigaction(SIGINT
, &new_action
, NULL
);
138 #endif /* __MINGW32__ */
141 void init_static_data(void)
143 loaded_plugins
= g_ptr_array_new_with_free_func(
144 (GDestroyNotify
) bt_object_put_ref
);
148 void fini_static_data(void)
150 g_ptr_array_free(loaded_plugins
, TRUE
);
154 int create_the_query_executor(void)
158 the_query_executor
= bt_query_executor_create();
159 if (!the_query_executor
) {
160 BT_LOGE_STR("Cannot create a query executor.");
168 void destroy_the_query_executor(void)
170 BT_QUERY_EXECUTOR_PUT_REF_AND_RESET(the_query_executor
);
174 int query(const bt_component_class
*comp_cls
, const char *obj
,
175 const bt_value
*params
, const bt_value
**user_result
,
176 const char **fail_reason
)
178 const bt_value
*result
= NULL
;
179 bt_query_executor_status status
;
180 *fail_reason
= "unknown error";
183 BT_ASSERT(fail_reason
);
184 BT_ASSERT(user_result
);
185 ret
= create_the_query_executor();
187 /* create_the_query_executor() logs errors */
192 BT_LOGI("Canceled by user before executing the query: "
193 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
194 "query-obj=\"%s\"", comp_cls
,
195 bt_component_class_get_name(comp_cls
), obj
);
196 *fail_reason
= "canceled by user";
201 status
= bt_query_executor_query(the_query_executor
,
202 comp_cls
, obj
, params
, &result
);
204 case BT_QUERY_EXECUTOR_STATUS_OK
:
206 case BT_QUERY_EXECUTOR_STATUS_AGAIN
:
208 const uint64_t sleep_time_us
= 100000;
210 /* Wait 100 ms and retry */
211 BT_LOGD("Got BT_QUERY_EXECUTOR_STATUS_AGAIN: sleeping: "
212 "time-us=%" PRIu64
, sleep_time_us
);
214 if (usleep(sleep_time_us
)) {
215 if (bt_query_executor_is_canceled(the_query_executor
)) {
216 BT_LOGI("Query was canceled by user: "
217 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
218 "query-obj=\"%s\"", comp_cls
,
219 bt_component_class_get_name(comp_cls
),
221 *fail_reason
= "canceled by user";
228 case BT_QUERY_EXECUTOR_STATUS_CANCELED
:
229 *fail_reason
= "canceled by user";
231 case BT_QUERY_EXECUTOR_STATUS_ERROR
:
233 case BT_QUERY_EXECUTOR_STATUS_INVALID_OBJECT
:
234 *fail_reason
= "invalid or unknown query object";
236 case BT_QUERY_EXECUTOR_STATUS_INVALID_PARAMS
:
237 *fail_reason
= "invalid query parameters";
239 case BT_QUERY_EXECUTOR_STATUS_UNSUPPORTED
:
240 *fail_reason
= "unsupported action";
242 case BT_QUERY_EXECUTOR_STATUS_NOMEM
:
243 *fail_reason
= "not enough memory";
246 BT_LOGF("Unknown query status: status=%d", status
);
252 *user_result
= result
;
260 destroy_the_query_executor();
261 bt_value_put_ref(result
);
266 const bt_plugin
*find_plugin(const char *name
)
269 const bt_plugin
*plugin
= NULL
;
272 BT_LOGI("Finding plugin: name=\"%s\"", name
);
274 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
275 plugin
= g_ptr_array_index(loaded_plugins
, i
);
277 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
285 BT_LOGI("Found plugin: name=\"%s\", plugin-addr=%p",
288 BT_LOGI("Cannot find plugin: name=\"%s\"", name
);
291 bt_plugin_get_ref(plugin
);
295 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
296 const bt_plugin
*, const char *);
299 const void *find_component_class_from_plugin(const char *plugin_name
,
300 const char *comp_class_name
,
301 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
303 const void *comp_class
= NULL
;
304 const bt_plugin
*plugin
;
306 BT_LOGI("Finding component class: plugin-name=\"%s\", "
307 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
309 plugin
= find_plugin(plugin_name
);
314 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
315 bt_object_get_ref(comp_class
);
316 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
320 BT_LOGI("Found component class: plugin-name=\"%s\", "
321 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
323 BT_LOGI("Cannot find source component class: "
324 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
325 plugin_name
, comp_class_name
);
332 const bt_component_class_source
*find_source_component_class(
333 const char *plugin_name
, const char *comp_class_name
)
335 return (const void *) find_component_class_from_plugin(
336 plugin_name
, comp_class_name
,
337 (plugin_borrow_comp_cls_func_t
)
338 bt_plugin_borrow_source_component_class_by_name_const
);
342 const bt_component_class_filter
*find_filter_component_class(
343 const char *plugin_name
, const char *comp_class_name
)
345 return (const void *) find_component_class_from_plugin(
346 plugin_name
, comp_class_name
,
347 (plugin_borrow_comp_cls_func_t
)
348 bt_plugin_borrow_filter_component_class_by_name_const
);
352 const bt_component_class_sink
*find_sink_component_class(
353 const char *plugin_name
, const char *comp_class_name
)
355 return (const void *) find_component_class_from_plugin(plugin_name
,
357 (plugin_borrow_comp_cls_func_t
)
358 bt_plugin_borrow_sink_component_class_by_name_const
);
362 const bt_component_class
*find_component_class(const char *plugin_name
,
363 const char *comp_class_name
,
364 bt_component_class_type comp_class_type
)
366 const bt_component_class
*comp_cls
= NULL
;
368 switch (comp_class_type
) {
369 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
370 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
372 case BT_COMPONENT_CLASS_TYPE_FILTER
:
373 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
375 case BT_COMPONENT_CLASS_TYPE_SINK
:
376 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
386 void print_indent(FILE *fp
, size_t indent
)
390 for (i
= 0; i
< indent
; i
++) {
396 const char *component_type_str(bt_component_class_type type
)
399 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
401 case BT_COMPONENT_CLASS_TYPE_SINK
:
403 case BT_COMPONENT_CLASS_TYPE_FILTER
:
411 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
412 const char *comp_cls_name
, bt_component_class_type type
)
414 GString
*shell_plugin_name
= NULL
;
415 GString
*shell_comp_cls_name
= NULL
;
417 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
418 if (!shell_plugin_name
) {
422 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
423 if (!shell_comp_cls_name
) {
427 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
428 bt_common_color_bold(),
429 bt_common_color_fg_cyan(),
430 component_type_str(type
),
431 bt_common_color_fg_default(),
432 bt_common_color_fg_blue(),
433 shell_plugin_name
->str
,
434 bt_common_color_fg_default(),
435 bt_common_color_fg_yellow(),
436 shell_comp_cls_name
->str
,
437 bt_common_color_reset());
440 if (shell_plugin_name
) {
441 g_string_free(shell_plugin_name
, TRUE
);
444 if (shell_comp_cls_name
) {
445 g_string_free(shell_comp_cls_name
, TRUE
);
450 void print_value(FILE *, const bt_value
*, size_t);
453 void print_value_rec(FILE *, const bt_value
*, size_t);
455 struct print_map_value_data
{
461 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
464 struct print_map_value_data
*print_map_value_data
= data
;
466 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
467 fprintf(print_map_value_data
->fp
, "%s: ", key
);
470 if (bt_value_is_array(object
) &&
471 bt_value_array_is_empty(object
)) {
472 fprintf(print_map_value_data
->fp
, "[ ]\n");
476 if (bt_value_is_map(object
) &&
477 bt_value_map_is_empty(object
)) {
478 fprintf(print_map_value_data
->fp
, "{ }\n");
482 if (bt_value_is_array(object
) ||
483 bt_value_is_map(object
)) {
484 fprintf(print_map_value_data
->fp
, "\n");
487 print_value_rec(print_map_value_data
->fp
, object
,
488 print_map_value_data
->indent
+ 2);
493 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
507 switch (bt_value_get_type(value
)) {
508 case BT_VALUE_TYPE_NULL
:
509 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
510 bt_common_color_reset());
512 case BT_VALUE_TYPE_BOOL
:
513 bool_val
= bt_value_bool_get(value
);
514 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
515 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
516 bt_common_color_reset());
518 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
519 uint_val
= bt_value_unsigned_integer_get(value
);
520 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
521 bt_common_color_fg_red(), uint_val
,
522 bt_common_color_reset());
524 case BT_VALUE_TYPE_SIGNED_INTEGER
:
525 int_val
= bt_value_signed_integer_get(value
);
526 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
527 bt_common_color_fg_red(), int_val
,
528 bt_common_color_reset());
530 case BT_VALUE_TYPE_REAL
:
531 dbl_val
= bt_value_real_get(value
);
532 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
533 bt_common_color_fg_red(), dbl_val
,
534 bt_common_color_reset());
536 case BT_VALUE_TYPE_STRING
:
537 str_val
= bt_value_string_get(value
);
538 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
539 bt_common_color_fg_green(), str_val
,
540 bt_common_color_reset());
542 case BT_VALUE_TYPE_ARRAY
:
543 size
= bt_value_array_get_size(value
);
549 print_indent(fp
, indent
);
550 fprintf(fp
, "[ ]\n");
554 for (i
= 0; i
< size
; i
++) {
555 const bt_value
*element
=
556 bt_value_array_borrow_element_by_index_const(
562 print_indent(fp
, indent
);
565 if (bt_value_is_array(element
) &&
566 bt_value_array_is_empty(element
)) {
567 fprintf(fp
, "[ ]\n");
571 if (bt_value_is_map(element
) &&
572 bt_value_map_is_empty(element
)) {
573 fprintf(fp
, "{ }\n");
577 if (bt_value_is_array(element
) ||
578 bt_value_is_map(element
)) {
582 print_value_rec(fp
, element
, indent
+ 2);
585 case BT_VALUE_TYPE_MAP
:
587 struct print_map_value_data data
= {
592 if (bt_value_map_is_empty(value
)) {
593 print_indent(fp
, indent
);
594 fprintf(fp
, "{ }\n");
598 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
607 BT_LOGE("Error printing value of type %s.",
608 bt_common_value_type_string(bt_value_get_type(value
)));
612 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
614 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
615 print_indent(fp
, indent
);
618 print_value_rec(fp
, value
, indent
);
622 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
624 fprintf(stderr
, " ");
625 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
626 bt_config_component
->comp_cls_name
->str
,
627 bt_config_component
->type
);
628 fprintf(stderr
, ":\n");
630 if (bt_config_component
->instance_name
->len
> 0) {
631 fprintf(stderr
, " Name: %s\n",
632 bt_config_component
->instance_name
->str
);
635 fprintf(stderr
, " Parameters:\n");
636 print_value(stderr
, bt_config_component
->params
, 8);
640 void print_bt_config_components(GPtrArray
*array
)
644 for (i
= 0; i
< array
->len
; i
++) {
645 struct bt_config_component
*cfg_component
=
646 bt_config_get_component(array
, i
);
647 print_bt_config_component(cfg_component
);
648 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
653 void print_plugin_paths(const bt_value
*plugin_paths
)
655 fprintf(stderr
, " Plugin paths:\n");
656 print_value(stderr
, plugin_paths
, 4);
660 void print_cfg_run(struct bt_config
*cfg
)
664 print_plugin_paths(cfg
->plugin_paths
);
665 fprintf(stderr
, " Source component instances:\n");
666 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
668 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
669 fprintf(stderr
, " Filter component instances:\n");
670 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
673 fprintf(stderr
, " Sink component instances:\n");
674 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
675 fprintf(stderr
, " Connections:\n");
677 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
678 struct bt_config_connection
*cfg_connection
=
679 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
682 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
683 cfg_connection
->upstream_comp_name
->str
,
684 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
685 cfg_connection
->upstream_port_glob
->str
,
686 cfg_connection
->downstream_comp_name
->str
,
687 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
688 cfg_connection
->downstream_port_glob
->str
);
693 void print_cfg_list_plugins(struct bt_config
*cfg
)
695 print_plugin_paths(cfg
->plugin_paths
);
699 void print_cfg_help(struct bt_config
*cfg
)
701 print_plugin_paths(cfg
->plugin_paths
);
705 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
707 print_plugin_paths(cfg
->plugin_paths
);
708 fprintf(stderr
, " Path: %s\n",
709 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
713 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
715 print_plugin_paths(cfg
->plugin_paths
);
716 fprintf(stderr
, " URL: %s\n",
717 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
721 void print_cfg_query(struct bt_config
*cfg
)
723 print_plugin_paths(cfg
->plugin_paths
);
724 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
725 fprintf(stderr
, " Component class:\n");
726 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
730 void print_cfg(struct bt_config
*cfg
)
732 if (!BT_LOG_ON_INFO
) {
736 BT_LOGI_STR("CLI configuration:");
737 BT_LOGI(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
738 BT_LOGI(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
740 switch (cfg
->command
) {
741 case BT_CONFIG_COMMAND_RUN
:
744 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
745 print_cfg_list_plugins(cfg
);
747 case BT_CONFIG_COMMAND_HELP
:
750 case BT_CONFIG_COMMAND_QUERY
:
751 print_cfg_query(cfg
);
753 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
754 print_cfg_print_ctf_metadata(cfg
);
756 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
757 print_cfg_print_lttng_live_sessions(cfg
);
765 void add_to_loaded_plugins(const bt_plugin_set
*plugin_set
)
770 count
= bt_plugin_set_get_plugin_count(plugin_set
);
771 BT_ASSERT(count
>= 0);
773 for (i
= 0; i
< count
; i
++) {
774 const bt_plugin
*plugin
=
775 bt_plugin_set_borrow_plugin_by_index_const(plugin_set
, i
);
776 const bt_plugin
*loaded_plugin
=
777 find_plugin(bt_plugin_get_name(plugin
));
782 BT_LOGI("Not using plugin: another one already exists with the same name: "
783 "plugin-name=\"%s\", plugin-path=\"%s\", "
784 "existing-plugin-path=\"%s\"",
785 bt_plugin_get_name(plugin
),
786 bt_plugin_get_path(plugin
),
787 bt_plugin_get_path(loaded_plugin
));
788 bt_plugin_put_ref(loaded_plugin
);
790 /* Add to global array. */
791 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
792 bt_plugin_get_name(plugin
));
793 bt_plugin_get_ref(plugin
);
794 g_ptr_array_add(loaded_plugins
, (void *) plugin
);
800 int load_dynamic_plugins(const bt_value
*plugin_paths
)
802 int nr_paths
, i
, ret
= 0;
804 nr_paths
= bt_value_array_get_size(plugin_paths
);
806 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
811 BT_LOGI_STR("Loading dynamic plugins.");
813 for (i
= 0; i
< nr_paths
; i
++) {
814 const bt_value
*plugin_path_value
= NULL
;
815 const char *plugin_path
;
816 const bt_plugin_set
*plugin_set
;
819 bt_value_array_borrow_element_by_index_const(
821 plugin_path
= bt_value_string_get(plugin_path_value
);
824 * Skip this if the directory does not exist because
825 * bt_plugin_find_all_from_dir() expects an existing
828 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
829 BT_LOGI("Skipping nonexistent directory path: "
830 "path=\"%s\"", plugin_path
);
834 plugin_set
= bt_plugin_find_all_from_dir(plugin_path
, false);
836 BT_LOGI("Unable to load dynamic plugins from directory: "
837 "path=\"%s\"", plugin_path
);
841 add_to_loaded_plugins(plugin_set
);
842 bt_plugin_set_put_ref(plugin_set
);
849 int load_static_plugins(void)
852 const bt_plugin_set
*plugin_set
;
854 BT_LOGI("Loading static plugins.");
855 plugin_set
= bt_plugin_find_all_from_static();
857 BT_LOGE("Unable to load static plugins.");
862 add_to_loaded_plugins(plugin_set
);
863 bt_plugin_set_put_ref(plugin_set
);
869 int load_all_plugins(const bt_value
*plugin_paths
)
873 if (load_dynamic_plugins(plugin_paths
)) {
878 if (load_static_plugins()) {
883 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
890 void print_plugin_info(const bt_plugin
*plugin
)
892 unsigned int major
, minor
, patch
;
894 bt_property_availability version_avail
;
895 const char *plugin_name
;
899 const char *plugin_description
;
901 plugin_name
= bt_plugin_get_name(plugin
);
902 path
= bt_plugin_get_path(plugin
);
903 author
= bt_plugin_get_author(plugin
);
904 license
= bt_plugin_get_license(plugin
);
905 plugin_description
= bt_plugin_get_description(plugin
);
906 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
908 printf("%s%s%s%s:\n", bt_common_color_bold(),
909 bt_common_color_fg_blue(), plugin_name
,
910 bt_common_color_reset());
912 printf(" %sPath%s: %s\n", bt_common_color_bold(),
913 bt_common_color_reset(), path
);
918 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
919 printf(" %sVersion%s: %u.%u.%u",
920 bt_common_color_bold(), bt_common_color_reset(),
921 major
, minor
, patch
);
930 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
931 bt_common_color_reset(),
932 plugin_description
? plugin_description
: "(None)");
933 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
934 bt_common_color_reset(), author
? author
: "(Unknown)");
935 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
936 bt_common_color_reset(),
937 license
? license
: "(Unknown)");
941 int cmd_query(struct bt_config
*cfg
)
944 const bt_component_class
*comp_cls
= NULL
;
945 const bt_value
*results
= NULL
;
946 const char *fail_reason
= NULL
;
948 comp_cls
= find_component_class(
949 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
950 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
951 cfg
->cmd_data
.query
.cfg_component
->type
);
953 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
954 "comp-cls-name=\"%s\", comp-cls-type=%d",
955 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
956 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
957 cfg
->cmd_data
.query
.cfg_component
->type
);
958 fprintf(stderr
, "%s%sCannot find component class %s",
959 bt_common_color_bold(),
960 bt_common_color_fg_red(),
961 bt_common_color_reset());
962 print_plugin_comp_cls_opt(stderr
,
963 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
964 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
965 cfg
->cmd_data
.query
.cfg_component
->type
);
966 fprintf(stderr
, "\n");
971 ret
= query(comp_cls
, cfg
->cmd_data
.query
.object
->str
,
972 cfg
->cmd_data
.query
.cfg_component
->params
,
973 &results
, &fail_reason
);
978 print_value(stdout
, results
, 0);
982 BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", "
983 "comp-cls-name=\"%s\", comp-cls-type=%d "
984 "object=\"%s\"", fail_reason
,
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 cfg
->cmd_data
.query
.object
->str
);
989 fprintf(stderr
, "%s%sFailed to query info to %s",
990 bt_common_color_bold(),
991 bt_common_color_fg_red(),
992 bt_common_color_reset());
993 print_plugin_comp_cls_opt(stderr
,
994 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
995 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
996 cfg
->cmd_data
.query
.cfg_component
->type
);
997 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
998 bt_common_color_bold(),
999 bt_common_color_fg_red(),
1000 cfg
->cmd_data
.query
.object
->str
,
1002 bt_common_color_reset());
1006 bt_component_class_put_ref(comp_cls
);
1007 bt_value_put_ref(results
);
1012 void print_component_class_help(const char *plugin_name
,
1013 const bt_component_class
*comp_cls
)
1015 const char *comp_class_name
=
1016 bt_component_class_get_name(comp_cls
);
1017 const char *comp_class_description
=
1018 bt_component_class_get_description(comp_cls
);
1019 const char *comp_class_help
=
1020 bt_component_class_get_help(comp_cls
);
1021 bt_component_class_type type
=
1022 bt_component_class_get_type(comp_cls
);
1024 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
1026 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1027 bt_common_color_reset(),
1028 comp_class_description
? comp_class_description
: "(None)");
1030 if (comp_class_help
) {
1031 printf("\n%s\n", comp_class_help
);
1036 int cmd_help(struct bt_config
*cfg
)
1039 const bt_plugin
*plugin
= NULL
;
1040 const bt_component_class
*needed_comp_cls
= NULL
;
1042 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1044 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
1045 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1046 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
1047 bt_common_color_bold(), bt_common_color_fg_red(),
1048 bt_common_color_fg_blue(),
1049 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1050 bt_common_color_reset());
1055 print_plugin_info(plugin
);
1056 printf(" %sSource component classes%s: %d\n",
1057 bt_common_color_bold(),
1058 bt_common_color_reset(),
1059 (int) bt_plugin_get_source_component_class_count(plugin
));
1060 printf(" %sFilter component classes%s: %d\n",
1061 bt_common_color_bold(),
1062 bt_common_color_reset(),
1063 (int) bt_plugin_get_filter_component_class_count(plugin
));
1064 printf(" %sSink component classes%s: %d\n",
1065 bt_common_color_bold(),
1066 bt_common_color_reset(),
1067 (int) bt_plugin_get_sink_component_class_count(plugin
));
1069 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
1070 /* Plugin help only */
1074 needed_comp_cls
= find_component_class(
1075 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1076 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1077 cfg
->cmd_data
.help
.cfg_component
->type
);
1078 if (!needed_comp_cls
) {
1079 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1080 "comp-cls-name=\"%s\", comp-cls-type=%d",
1081 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1082 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1083 cfg
->cmd_data
.help
.cfg_component
->type
);
1084 fprintf(stderr
, "\n%s%sCannot find component class %s",
1085 bt_common_color_bold(),
1086 bt_common_color_fg_red(),
1087 bt_common_color_reset());
1088 print_plugin_comp_cls_opt(stderr
,
1089 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1090 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1091 cfg
->cmd_data
.help
.cfg_component
->type
);
1092 fprintf(stderr
, "\n");
1098 print_component_class_help(
1099 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1103 bt_component_class_put_ref(needed_comp_cls
);
1104 bt_plugin_put_ref(plugin
);
1108 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
1110 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
1113 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
1114 const char *cc_type_name
, uint64_t count
,
1115 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
1116 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
1121 printf(" %s%s component classes%s: (none)\n",
1122 bt_common_color_bold(),
1124 bt_common_color_reset());
1127 printf(" %s%s component classes%s:\n",
1128 bt_common_color_bold(),
1130 bt_common_color_reset());
1133 for (i
= 0; i
< count
; i
++) {
1134 const bt_component_class
*comp_class
=
1135 spec_comp_cls_borrow_comp_cls_func(
1136 borrow_comp_cls_by_index_func(plugin
, i
));
1137 const char *comp_class_name
=
1138 bt_component_class_get_name(comp_class
);
1139 const char *comp_class_description
=
1140 bt_component_class_get_description(comp_class
);
1141 bt_component_class_type type
=
1142 bt_component_class_get_type(comp_class
);
1145 print_plugin_comp_cls_opt(stdout
,
1146 bt_plugin_get_name(plugin
), comp_class_name
,
1149 if (comp_class_description
) {
1150 printf(": %s", comp_class_description
);
1161 int cmd_list_plugins(struct bt_config
*cfg
)
1164 int plugins_count
, component_classes_count
= 0, i
;
1166 printf("From the following plugin paths:\n\n");
1167 print_value(stdout
, cfg
->plugin_paths
, 2);
1169 plugins_count
= loaded_plugins
->len
;
1170 if (plugins_count
== 0) {
1171 printf("No plugins found.\n");
1175 for (i
= 0; i
< plugins_count
; i
++) {
1176 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1178 component_classes_count
+=
1179 bt_plugin_get_source_component_class_count(plugin
) +
1180 bt_plugin_get_filter_component_class_count(plugin
) +
1181 bt_plugin_get_sink_component_class_count(plugin
);
1184 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1185 bt_common_color_bold(),
1186 component_classes_count
,
1187 bt_common_color_reset(),
1188 bt_common_color_bold(),
1190 bt_common_color_reset());
1192 for (i
= 0; i
< plugins_count
; i
++) {
1193 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1196 print_plugin_info(plugin
);
1197 cmd_list_plugins_print_component_classes(plugin
, "Source",
1198 bt_plugin_get_source_component_class_count(plugin
),
1199 (plugin_borrow_comp_cls_by_index_func_t
)
1200 bt_plugin_borrow_source_component_class_by_index_const
,
1201 (spec_comp_cls_borrow_comp_cls_func_t
)
1202 bt_component_class_source_as_component_class
);
1203 cmd_list_plugins_print_component_classes(plugin
, "Filter",
1204 bt_plugin_get_filter_component_class_count(plugin
),
1205 (plugin_borrow_comp_cls_by_index_func_t
)
1206 bt_plugin_borrow_filter_component_class_by_index_const
,
1207 (spec_comp_cls_borrow_comp_cls_func_t
)
1208 bt_component_class_filter_as_component_class
);
1209 cmd_list_plugins_print_component_classes(plugin
, "Sink",
1210 bt_plugin_get_sink_component_class_count(plugin
),
1211 (plugin_borrow_comp_cls_by_index_func_t
)
1212 bt_plugin_borrow_sink_component_class_by_index_const
,
1213 (spec_comp_cls_borrow_comp_cls_func_t
)
1214 bt_component_class_sink_as_component_class
);
1222 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1225 const bt_component_class
*comp_cls
= NULL
;
1226 const bt_value
*results
= NULL
;
1227 bt_value
*params
= NULL
;
1228 const bt_value
*map
= NULL
;
1229 const bt_value
*v
= NULL
;
1230 static const char * const plugin_name
= "ctf";
1231 static const char * const comp_cls_name
= "lttng-live";
1232 static const bt_component_class_type comp_cls_type
=
1233 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1234 int64_t array_size
, i
;
1235 const char *fail_reason
= NULL
;
1236 FILE *out_stream
= stdout
;
1238 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1239 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1242 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1243 "comp-cls-name=\"%s\", comp-cls-type=%d",
1244 plugin_name
, comp_cls_name
,
1245 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1246 fprintf(stderr
, "%s%sCannot find component class %s",
1247 bt_common_color_bold(),
1248 bt_common_color_fg_red(),
1249 bt_common_color_reset());
1250 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1251 comp_cls_name
, comp_cls_type
);
1252 fprintf(stderr
, "\n");
1256 params
= bt_value_map_create();
1261 ret
= bt_value_map_insert_string_entry(params
, "url",
1262 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1267 ret
= query(comp_cls
, "sessions", params
,
1268 &results
, &fail_reason
);
1275 if (!bt_value_is_array(results
)) {
1276 BT_LOGE_STR("Expecting an array for sessions query.");
1277 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1278 bt_common_color_bold(),
1279 bt_common_color_fg_red(),
1280 bt_common_color_reset());
1284 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1286 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1290 BT_LOGE_ERRNO("Cannot open file for writing",
1292 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1297 array_size
= bt_value_array_get_size(results
);
1298 for (i
= 0; i
< array_size
; i
++) {
1299 const char *url_text
;
1300 int64_t timer_us
, streams
, clients
;
1302 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1304 BT_LOGE_STR("Unexpected empty array entry.");
1307 if (!bt_value_is_map(map
)) {
1308 BT_LOGE_STR("Unexpected entry type.");
1312 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1314 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1317 url_text
= bt_value_string_get(v
);
1318 fprintf(out_stream
, "%s", url_text
);
1319 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1321 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1324 timer_us
= bt_value_signed_integer_get(v
);
1325 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1326 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1328 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1331 streams
= bt_value_signed_integer_get(v
);
1332 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1333 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1335 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1338 clients
= bt_value_signed_integer_get(v
);
1339 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1345 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1346 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1347 bt_common_color_bold(),
1348 bt_common_color_fg_red(),
1350 bt_common_color_reset());
1356 bt_value_put_ref(results
);
1357 bt_value_put_ref(params
);
1358 bt_component_class_put_ref(comp_cls
);
1360 if (out_stream
&& out_stream
!= stdout
) {
1361 int fclose_ret
= fclose(out_stream
);
1364 BT_LOGE_ERRNO("Cannot close file stream",
1366 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1374 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1377 const bt_component_class
*comp_cls
= NULL
;
1378 const bt_value
*results
= NULL
;
1379 bt_value
*params
= NULL
;
1380 const bt_value
*metadata_text_value
= NULL
;
1381 const char *metadata_text
= NULL
;
1382 static const char * const plugin_name
= "ctf";
1383 static const char * const comp_cls_name
= "fs";
1384 static const bt_component_class_type comp_cls_type
=
1385 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1386 const char *fail_reason
= NULL
;
1387 FILE *out_stream
= stdout
;
1389 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1390 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1393 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1394 "comp-cls-name=\"%s\", comp-cls-type=%d",
1395 plugin_name
, comp_cls_name
,
1396 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1397 fprintf(stderr
, "%s%sCannot find component class %s",
1398 bt_common_color_bold(),
1399 bt_common_color_fg_red(),
1400 bt_common_color_reset());
1401 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1402 comp_cls_name
, comp_cls_type
);
1403 fprintf(stderr
, "\n");
1408 params
= bt_value_map_create();
1414 ret
= bt_value_map_insert_string_entry(params
, "path",
1415 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1421 ret
= query(comp_cls
, "metadata-info",
1422 params
, &results
, &fail_reason
);
1427 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1429 if (!metadata_text_value
) {
1430 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1435 metadata_text
= bt_value_string_get(metadata_text_value
);
1437 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1439 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1443 BT_LOGE_ERRNO("Cannot open file for writing",
1445 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1450 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1452 BT_LOGE("Cannot write whole metadata text to output stream: "
1463 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1464 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1465 bt_common_color_bold(),
1466 bt_common_color_fg_red(),
1468 bt_common_color_reset());
1471 bt_value_put_ref(results
);
1472 bt_value_put_ref(params
);
1473 bt_component_class_put_ref(comp_cls
);
1475 if (out_stream
&& out_stream
!= stdout
) {
1476 int fclose_ret
= fclose(out_stream
);
1479 BT_LOGE_ERRNO("Cannot close file stream",
1481 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1489 char *instance_name
;
1493 struct trace_range
{
1494 uint64_t intersection_range_begin_ns
;
1495 uint64_t intersection_range_end_ns
;
1499 guint
port_id_hash(gconstpointer v
)
1501 const struct port_id
*id
= v
;
1503 BT_ASSERT(id
->instance_name
);
1504 BT_ASSERT(id
->port_name
);
1506 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1510 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1512 const struct port_id
*id1
= v1
;
1513 const struct port_id
*id2
= v2
;
1515 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1516 !strcmp(id1
->port_name
, id2
->port_name
);
1520 void port_id_destroy(gpointer data
)
1522 struct port_id
*id
= data
;
1524 free(id
->instance_name
);
1525 free(id
->port_name
);
1530 void trace_range_destroy(gpointer data
)
1535 struct cmd_run_ctx
{
1537 GHashTable
*src_components
;
1540 GHashTable
*flt_components
;
1543 GHashTable
*sink_components
;
1549 struct bt_config
*cfg
;
1553 bool stream_intersection_mode
;
1556 * Association of struct port_id -> struct trace_range.
1558 GHashTable
*intersections
;
1561 /* Returns a timestamp of the form "(-)s.ns" */
1563 char *s_from_ns(int64_t ns
)
1568 int64_t ts_sec_abs
, ts_nsec_abs
;
1569 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1570 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1572 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1573 is_negative
= false;
1574 ts_sec_abs
= ts_sec
;
1575 ts_nsec_abs
= ts_nsec
;
1576 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1577 is_negative
= false;
1578 ts_sec_abs
= ts_sec
- 1;
1579 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1580 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1582 ts_sec_abs
= ts_sec
;
1583 ts_nsec_abs
= -ts_nsec
;
1584 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1586 ts_sec_abs
= -(ts_sec
+ 1);
1587 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1588 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1590 ts_sec_abs
= -ts_sec
;
1591 ts_nsec_abs
= ts_nsec
;
1592 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1594 ts_sec_abs
= -ts_sec
;
1595 ts_nsec_abs
= -ts_nsec
;
1598 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1599 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1607 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1608 struct cmd_run_ctx
*ctx
,
1609 const bt_component
*upstream_comp
,
1610 const bt_port_output
*out_upstream_port
,
1611 struct bt_config_connection
*cfg_conn
)
1613 typedef uint64_t (*input_port_count_func_t
)(void *);
1614 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1615 const void *, uint64_t);
1616 const bt_port
*upstream_port
=
1617 bt_port_output_as_port_const(out_upstream_port
);
1620 GQuark downstreamp_comp_name_quark
;
1621 void *downstream_comp
;
1622 uint64_t downstream_port_count
;
1624 input_port_count_func_t port_count_fn
;
1625 borrow_input_port_by_index_func_t port_by_index_fn
;
1626 bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1627 bool insert_trimmer
= false;
1628 bt_value
*trimmer_params
= NULL
;
1629 char *intersection_begin
= NULL
;
1630 char *intersection_end
= NULL
;
1631 const bt_component_filter
*trimmer
= NULL
;
1632 const bt_component_class_filter
*trimmer_class
= NULL
;
1633 const bt_port_input
*trimmer_input
= NULL
;
1634 const bt_port_output
*trimmer_output
= NULL
;
1636 if (ctx
->intersections
&&
1637 bt_component_get_class_type(upstream_comp
) ==
1638 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1639 struct trace_range
*range
;
1640 struct port_id port_id
= {
1641 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1642 .port_name
= (char *) bt_port_get_name(upstream_port
)
1645 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1649 range
= (struct trace_range
*) g_hash_table_lookup(
1650 ctx
->intersections
, &port_id
);
1652 bt_value_status status
;
1654 intersection_begin
= s_from_ns(
1655 range
->intersection_range_begin_ns
);
1656 intersection_end
= s_from_ns(
1657 range
->intersection_range_end_ns
);
1658 if (!intersection_begin
|| !intersection_end
) {
1659 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1663 insert_trimmer
= true;
1664 trimmer_params
= bt_value_map_create();
1665 if (!trimmer_params
) {
1669 status
= bt_value_map_insert_string_entry(
1670 trimmer_params
, "begin", intersection_begin
);
1671 if (status
!= BT_VALUE_STATUS_OK
) {
1674 status
= bt_value_map_insert_string_entry(
1676 "end", intersection_end
);
1677 if (status
!= BT_VALUE_STATUS_OK
) {
1682 trimmer_class
= find_filter_component_class("utils", "trimmer");
1683 if (!trimmer_class
) {
1688 BT_LOGI("Connecting upstream port to the next available downstream port: "
1689 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1690 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1691 upstream_port
, bt_port_get_name(upstream_port
),
1692 cfg_conn
->downstream_comp_name
->str
,
1693 cfg_conn
->arg
->str
);
1694 downstreamp_comp_name_quark
= g_quark_from_string(
1695 cfg_conn
->downstream_comp_name
->str
);
1696 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1697 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1698 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1699 port_count_fn
= (input_port_count_func_t
)
1700 bt_component_filter_get_input_port_count
;
1701 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1702 bt_component_filter_borrow_input_port_by_index_const
;
1704 if (!downstream_comp
) {
1705 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1706 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1707 port_count_fn
= (input_port_count_func_t
)
1708 bt_component_sink_get_input_port_count
;
1709 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1710 bt_component_sink_borrow_input_port_by_index_const
;
1713 if (!downstream_comp
) {
1714 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1715 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1716 cfg_conn
->arg
->str
);
1717 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1718 cfg_conn
->arg
->str
);
1722 downstream_port_count
= port_count_fn(downstream_comp
);
1724 for (i
= 0; i
< downstream_port_count
; i
++) {
1725 const bt_port_input
*in_downstream_port
=
1726 port_by_index_fn(downstream_comp
, i
);
1727 const bt_port
*downstream_port
=
1728 bt_port_input_as_port_const(in_downstream_port
);
1729 const char *upstream_port_name
;
1730 const char *downstream_port_name
;
1732 BT_ASSERT(downstream_port
);
1734 /* Skip port if it's already connected. */
1735 if (bt_port_is_connected(downstream_port
)) {
1736 BT_LOGI("Skipping downstream port: already connected: "
1737 "port-addr=%p, port-name=\"%s\"",
1739 bt_port_get_name(downstream_port
));
1743 downstream_port_name
= bt_port_get_name(downstream_port
);
1744 BT_ASSERT(downstream_port_name
);
1745 upstream_port_name
= bt_port_get_name(upstream_port
);
1746 BT_ASSERT(upstream_port_name
);
1748 if (!bt_common_star_glob_match(
1749 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1750 downstream_port_name
, SIZE_MAX
)) {
1754 if (insert_trimmer
) {
1756 * In order to insert the trimmer between the
1757 * two components that were being connected, we
1758 * create a connection configuration entry which
1759 * describes a connection from the trimmer's
1760 * output to the original input that was being
1763 * Hence, the creation of the trimmer will cause
1764 * the graph "new port" listener to establish
1765 * all downstream connections as its output port
1766 * is connected. We will then establish the
1767 * connection between the original upstream
1768 * source and the trimmer.
1770 char *trimmer_name
= NULL
;
1771 bt_graph_status graph_status
;
1773 ret
= asprintf(&trimmer_name
,
1774 "stream-intersection-trimmer-%s",
1775 upstream_port_name
);
1781 ctx
->connect_ports
= false;
1782 graph_status
= bt_graph_add_filter_component(
1783 ctx
->graph
, trimmer_class
, trimmer_name
,
1784 trimmer_params
, ctx
->cfg
->log_level
,
1787 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1793 bt_component_filter_borrow_input_port_by_index_const(
1795 if (!trimmer_input
) {
1799 bt_component_filter_borrow_output_port_by_index_const(
1801 if (!trimmer_output
) {
1806 * Replace the current downstream port by the trimmer's
1809 in_downstream_port
= trimmer_input
;
1811 bt_port_input_as_port_const(in_downstream_port
);
1812 downstream_port_name
= bt_port_get_name(
1814 BT_ASSERT(downstream_port_name
);
1817 /* We have a winner! */
1818 status
= bt_graph_connect_ports(ctx
->graph
,
1819 out_upstream_port
, in_downstream_port
, NULL
);
1820 downstream_port
= NULL
;
1822 case BT_GRAPH_STATUS_OK
:
1824 case BT_GRAPH_STATUS_CANCELED
:
1825 BT_LOGI_STR("Graph was canceled by user.");
1826 status
= BT_GRAPH_STATUS_OK
;
1828 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1829 BT_LOGE("A component refused a connection to one of its ports: "
1830 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1831 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1832 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1833 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1835 upstream_comp
, bt_component_get_name(upstream_comp
),
1836 upstream_port
, bt_port_get_name(upstream_port
),
1837 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1838 downstream_port
, downstream_port_name
,
1839 cfg_conn
->arg
->str
);
1841 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1842 bt_port_get_name(upstream_port
),
1843 downstream_port_name
,
1844 cfg_conn
->arg
->str
);
1847 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1848 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1849 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1850 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1851 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1853 upstream_comp
, bt_component_get_name(upstream_comp
),
1854 upstream_port
, bt_port_get_name(upstream_port
),
1855 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1856 downstream_port
, downstream_port_name
,
1857 cfg_conn
->arg
->str
);
1859 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1860 bt_port_get_name(upstream_port
),
1861 downstream_port_name
,
1862 cfg_conn
->arg
->str
);
1866 BT_LOGI("Connected component ports: "
1867 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1868 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1869 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1870 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1872 upstream_comp
, bt_component_get_name(upstream_comp
),
1873 upstream_port
, bt_port_get_name(upstream_port
),
1874 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1875 downstream_port
, downstream_port_name
,
1876 cfg_conn
->arg
->str
);
1878 if (insert_trimmer
) {
1880 * The first connection, from the source to the trimmer,
1881 * has been done. We now connect the trimmer to the
1882 * original downstream port.
1884 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1886 bt_component_filter_as_component_const(trimmer
),
1887 trimmer_output
, cfg_conn
);
1891 ctx
->connect_ports
= true;
1895 * We found a matching downstream port: the search is
1901 /* No downstream port found */
1902 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1903 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1904 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1905 upstream_port
, bt_port_get_name(upstream_port
),
1906 cfg_conn
->downstream_comp_name
->str
,
1907 cfg_conn
->arg
->str
);
1909 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1910 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1916 free(intersection_begin
);
1917 free(intersection_end
);
1918 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1919 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1920 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1925 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1926 const bt_port_output
*upstream_port
)
1929 const char *upstream_port_name
;
1930 const char *upstream_comp_name
;
1931 const bt_component
*upstream_comp
= NULL
;
1935 BT_ASSERT(upstream_port
);
1936 upstream_port_name
= bt_port_get_name(
1937 bt_port_output_as_port_const(upstream_port
));
1938 BT_ASSERT(upstream_port_name
);
1939 upstream_comp
= bt_port_borrow_component_const(
1940 bt_port_output_as_port_const(upstream_port
));
1941 if (!upstream_comp
) {
1942 BT_LOGW("Upstream port to connect is not part of a component: "
1943 "port-addr=%p, port-name=\"%s\"",
1944 upstream_port
, upstream_port_name
);
1949 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1950 BT_ASSERT(upstream_comp_name
);
1951 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1952 "port-addr=%p, port-name=\"%s\"",
1953 upstream_comp
, upstream_comp_name
,
1954 upstream_port
, upstream_port_name
);
1956 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1957 struct bt_config_connection
*cfg_conn
=
1959 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1961 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1962 upstream_comp_name
)) {
1966 if (!bt_common_star_glob_match(
1967 cfg_conn
->upstream_port_glob
->str
,
1968 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1972 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1973 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1975 BT_LOGE("Cannot connect upstream port: "
1976 "port-addr=%p, port-name=\"%s\"",
1978 upstream_port_name
);
1980 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1983 cfg_conn
->arg
->str
);
1989 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1990 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1991 upstream_port_name
);
1993 "Cannot create connection: upstream port `%s` does not match any connection\n",
1994 upstream_port_name
);
2004 bt_graph_listener_status
2005 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
2006 const bt_port_output
*out_port
)
2008 const bt_component
*comp
;
2009 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
2010 bt_graph_listener_status ret
= BT_GRAPH_LISTENER_STATUS_OK
;
2012 comp
= bt_port_borrow_component_const(port
);
2013 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
2014 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
2015 comp
, comp
? bt_component_get_name(comp
) : "",
2016 port
, bt_port_get_name(port
));
2018 if (!ctx
->connect_ports
) {
2023 BT_LOGW_STR("Port has no component.");
2027 if (bt_port_is_connected(port
)) {
2028 BT_LOGW_STR("Port is already connected.");
2032 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
2033 BT_LOGF_STR("Cannot connect upstream port.");
2034 fprintf(stderr
, "Added port could not be connected: aborting\n");
2035 ret
= BT_GRAPH_LISTENER_STATUS_ERROR
;
2044 bt_graph_listener_status
graph_source_output_port_added_listener(
2045 const bt_component_source
*component
,
2046 const bt_port_output
*port
, void *data
)
2048 return graph_output_port_added_listener(data
, port
);
2052 bt_graph_listener_status
graph_filter_output_port_added_listener(
2053 const bt_component_filter
*component
,
2054 const bt_port_output
*port
, void *data
)
2056 return graph_output_port_added_listener(data
, port
);
2060 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
2066 if (ctx
->src_components
) {
2067 g_hash_table_destroy(ctx
->src_components
);
2068 ctx
->src_components
= NULL
;
2071 if (ctx
->flt_components
) {
2072 g_hash_table_destroy(ctx
->flt_components
);
2073 ctx
->flt_components
= NULL
;
2076 if (ctx
->sink_components
) {
2077 g_hash_table_destroy(ctx
->sink_components
);
2078 ctx
->sink_components
= NULL
;
2081 if (ctx
->intersections
) {
2082 g_hash_table_destroy(ctx
->intersections
);
2083 ctx
->intersections
= NULL
;
2086 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
2092 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2095 bt_graph_status status
;
2098 ctx
->connect_ports
= false;
2099 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
2100 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2101 if (!ctx
->src_components
) {
2105 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
2106 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2107 if (!ctx
->flt_components
) {
2111 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
2112 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2113 if (!ctx
->sink_components
) {
2117 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2118 ctx
->stream_intersection_mode
= true;
2119 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2120 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2121 if (!ctx
->intersections
) {
2126 ctx
->graph
= bt_graph_create();
2131 the_graph
= ctx
->graph
;
2132 status
= bt_graph_add_source_component_output_port_added_listener(
2133 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
2135 if (status
!= BT_GRAPH_STATUS_OK
) {
2136 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2140 status
= bt_graph_add_filter_component_output_port_added_listener(
2141 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
2143 if (status
!= BT_GRAPH_STATUS_OK
) {
2144 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2151 cmd_run_ctx_destroy(ctx
);
2159 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2160 struct bt_config_component
*cfg_comp
,
2161 const bt_component_class_source
*src_comp_cls
)
2165 int64_t trace_count
;
2166 const char *path
= NULL
;
2167 const bt_value
*query_result
= NULL
;
2168 const bt_value
*trace_info
= NULL
;
2169 const bt_value
*intersection_range
= NULL
;
2170 const bt_value
*intersection_begin
= NULL
;
2171 const bt_value
*intersection_end
= NULL
;
2172 const bt_value
*stream_infos
= NULL
;
2173 const bt_value
*stream_info
= NULL
;
2174 struct port_id
*port_id
= NULL
;
2175 struct trace_range
*trace_range
= NULL
;
2176 const char *fail_reason
= NULL
;
2177 const bt_component_class
*comp_cls
=
2178 bt_component_class_source_as_component_class_const(src_comp_cls
);
2180 ret
= query(comp_cls
, "trace-info",
2181 cfg_comp
->params
, &query_result
,
2184 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2185 "comp-class-name=\"%s\"", fail_reason
,
2186 bt_component_class_get_name(comp_cls
));
2191 BT_ASSERT(query_result
);
2193 if (!bt_value_is_array(query_result
)) {
2194 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2195 "component-class-name=%s",
2196 bt_component_class_get_name(comp_cls
));
2201 trace_count
= bt_value_array_get_size(query_result
);
2202 if (trace_count
< 0) {
2207 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2209 uint64_t stream_idx
;
2210 int64_t stream_count
;
2212 trace_info
= bt_value_array_borrow_element_by_index_const(
2213 query_result
, trace_idx
);
2214 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2216 BT_LOGD_STR("Cannot retrieve trace from query result.");
2220 intersection_range
= bt_value_map_borrow_entry_value_const(
2221 trace_info
, "intersection-range-ns");
2222 if (!intersection_range
) {
2224 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2228 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
2230 if (!intersection_begin
) {
2232 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2236 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
2238 if (!intersection_end
) {
2240 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2244 begin
= bt_value_signed_integer_get(intersection_begin
);
2245 end
= bt_value_signed_integer_get(intersection_end
);
2247 if (begin
< 0 || end
< 0 || end
< begin
) {
2248 BT_LOGW("Invalid trace stream intersection values: "
2249 "intersection-range-ns:begin=%" PRId64
2250 ", intersection-range-ns:end=%" PRId64
,
2256 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
2258 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2260 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2264 stream_count
= bt_value_array_get_size(stream_infos
);
2265 if (stream_count
< 0) {
2270 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2271 const bt_value
*port_name
;
2273 port_id
= g_new0(struct port_id
, 1);
2276 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2279 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2280 if (!port_id
->instance_name
) {
2282 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2286 trace_range
= g_new0(struct trace_range
, 1);
2289 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2292 trace_range
->intersection_range_begin_ns
= begin
;
2293 trace_range
->intersection_range_end_ns
= end
;
2295 stream_info
= bt_value_array_borrow_element_by_index_const(
2296 stream_infos
, stream_idx
);
2297 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2299 BT_LOGE_STR("Cannot retrieve stream informations from trace in query result.");
2303 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2304 if (!port_name
|| !bt_value_is_string(port_name
)) {
2306 BT_LOGE_STR("Cannot retrieve port name in query result.");
2310 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2311 if (!port_id
->port_name
) {
2313 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2317 BT_LOGD("Inserting stream intersection ");
2319 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2329 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2330 bt_common_color_bold(),
2331 bt_common_color_fg_yellow(),
2332 path
? path
: "(unknown)",
2333 bt_common_color_reset());
2335 bt_value_put_ref(query_result
);
2337 g_free(trace_range
);
2342 int cmd_run_ctx_create_components_from_config_components(
2343 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2346 const void *comp_cls
= NULL
;
2347 const void *comp
= NULL
;
2350 for (i
= 0; i
< cfg_components
->len
; i
++) {
2351 struct bt_config_component
*cfg_comp
=
2352 g_ptr_array_index(cfg_components
, i
);
2355 switch (cfg_comp
->type
) {
2356 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2357 comp_cls
= find_source_component_class(
2358 cfg_comp
->plugin_name
->str
,
2359 cfg_comp
->comp_cls_name
->str
);
2361 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2362 comp_cls
= find_filter_component_class(
2363 cfg_comp
->plugin_name
->str
,
2364 cfg_comp
->comp_cls_name
->str
);
2366 case BT_COMPONENT_CLASS_TYPE_SINK
:
2367 comp_cls
= find_sink_component_class(
2368 cfg_comp
->plugin_name
->str
,
2369 cfg_comp
->comp_cls_name
->str
);
2376 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2377 "comp-cls-name=\"%s\", comp-cls-type=%d",
2378 cfg_comp
->plugin_name
->str
,
2379 cfg_comp
->comp_cls_name
->str
,
2381 fprintf(stderr
, "%s%sCannot find component class %s",
2382 bt_common_color_bold(),
2383 bt_common_color_fg_red(),
2384 bt_common_color_reset());
2385 print_plugin_comp_cls_opt(stderr
,
2386 cfg_comp
->plugin_name
->str
,
2387 cfg_comp
->comp_cls_name
->str
,
2389 fprintf(stderr
, "\n");
2393 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_VERBOSE
);
2395 switch (cfg_comp
->type
) {
2396 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2397 ret
= bt_graph_add_source_component(ctx
->graph
,
2398 comp_cls
, cfg_comp
->instance_name
->str
,
2399 cfg_comp
->params
, cfg_comp
->log_level
,
2402 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2403 ret
= bt_graph_add_filter_component(ctx
->graph
,
2404 comp_cls
, cfg_comp
->instance_name
->str
,
2405 cfg_comp
->params
, cfg_comp
->log_level
,
2408 case BT_COMPONENT_CLASS_TYPE_SINK
:
2409 ret
= bt_graph_add_sink_component(ctx
->graph
,
2410 comp_cls
, cfg_comp
->instance_name
->str
,
2411 cfg_comp
->params
, cfg_comp
->log_level
,
2419 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2420 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2422 cfg_comp
->plugin_name
->str
,
2423 cfg_comp
->comp_cls_name
->str
,
2424 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2425 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2426 bt_common_color_bold(),
2427 bt_common_color_fg_red(),
2428 cfg_comp
->instance_name
->str
,
2429 bt_common_color_reset());
2433 if (ctx
->stream_intersection_mode
&&
2434 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2435 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2441 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2442 comp
, cfg_comp
->instance_name
->str
);
2443 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2444 BT_ASSERT(quark
> 0);
2446 switch (cfg_comp
->type
) {
2447 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2448 g_hash_table_insert(ctx
->src_components
,
2449 GUINT_TO_POINTER(quark
), (void *) comp
);
2451 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2452 g_hash_table_insert(ctx
->flt_components
,
2453 GUINT_TO_POINTER(quark
), (void *) comp
);
2455 case BT_COMPONENT_CLASS_TYPE_SINK
:
2456 g_hash_table_insert(ctx
->sink_components
,
2457 GUINT_TO_POINTER(quark
), (void *) comp
);
2464 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2473 bt_object_put_ref(comp
);
2474 bt_object_put_ref(comp_cls
);
2479 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2484 * Make sure that, during this phase, our graph's "port added"
2485 * listener does not connect ports while we are creating the
2486 * components because we have a special, initial phase for
2489 ctx
->connect_ports
= false;
2491 ret
= cmd_run_ctx_create_components_from_config_components(
2492 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2498 ret
= cmd_run_ctx_create_components_from_config_components(
2499 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2505 ret
= cmd_run_ctx_create_components_from_config_components(
2506 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2516 typedef uint64_t (*output_port_count_func_t
)(const void *);
2517 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2518 const void *, uint64_t);
2521 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2522 void *comp
, output_port_count_func_t port_count_fn
,
2523 borrow_output_port_by_index_func_t port_by_index_fn
)
2529 count
= port_count_fn(comp
);
2531 for (i
= 0; i
< count
; i
++) {
2532 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2534 BT_ASSERT(upstream_port
);
2535 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2546 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2549 GHashTableIter iter
;
2550 gpointer g_name_quark
, g_comp
;
2552 ctx
->connect_ports
= true;
2553 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2555 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2556 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2557 (output_port_count_func_t
)
2558 bt_component_source_get_output_port_count
,
2559 (borrow_output_port_by_index_func_t
)
2560 bt_component_source_borrow_output_port_by_index_const
);
2566 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2568 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2569 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2570 (output_port_count_func_t
)
2571 bt_component_filter_get_output_port_count
,
2572 (borrow_output_port_by_index_func_t
)
2573 bt_component_filter_borrow_output_port_by_index_const
);
2584 const char *bt_graph_status_str(bt_graph_status status
)
2587 case BT_GRAPH_STATUS_OK
:
2588 return "BT_GRAPH_STATUS_OK";
2589 case BT_GRAPH_STATUS_END
:
2590 return "BT_GRAPH_STATUS_END";
2591 case BT_GRAPH_STATUS_AGAIN
:
2592 return "BT_GRAPH_STATUS_AGAIN";
2593 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
2594 return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION";
2595 case BT_GRAPH_STATUS_CANCELED
:
2596 return "BT_GRAPH_STATUS_CANCELED";
2597 case BT_GRAPH_STATUS_ERROR
:
2598 return "BT_GRAPH_STATUS_ERROR";
2599 case BT_GRAPH_STATUS_NOMEM
:
2600 return "BT_GRAPH_STATUS_NOMEM";
2607 int cmd_run(struct bt_config
*cfg
)
2610 struct cmd_run_ctx ctx
= { 0 };
2612 /* Initialize the command's context and the graph object */
2613 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2614 BT_LOGE_STR("Cannot initialize the command's context.");
2615 fprintf(stderr
, "Cannot initialize the command's context\n");
2620 BT_LOGI_STR("Canceled by user before creating components.");
2624 BT_LOGI_STR("Creating components.");
2626 /* Create the requested component instances */
2627 if (cmd_run_ctx_create_components(&ctx
)) {
2628 BT_LOGE_STR("Cannot create components.");
2629 fprintf(stderr
, "Cannot create components\n");
2634 BT_LOGI_STR("Canceled by user before connecting components.");
2638 BT_LOGI_STR("Connecting components.");
2640 /* Connect the initially visible component ports */
2641 if (cmd_run_ctx_connect_ports(&ctx
)) {
2642 BT_LOGE_STR("Cannot connect initial component ports.");
2643 fprintf(stderr
, "Cannot connect initial component ports\n");
2648 BT_LOGI_STR("Canceled by user before running the graph.");
2652 BT_LOGI_STR("Running the graph.");
2656 bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2659 * Reset console in case something messed with console
2660 * codes during the graph's execution.
2662 printf("%s", bt_common_color_reset());
2664 fprintf(stderr
, "%s", bt_common_color_reset());
2665 BT_LOGV("bt_graph_run() returned: status=%s",
2666 bt_graph_status_str(graph_status
));
2668 switch (graph_status
) {
2669 case BT_GRAPH_STATUS_OK
:
2671 case BT_GRAPH_STATUS_CANCELED
:
2672 BT_LOGI_STR("Graph was canceled by user.");
2674 case BT_GRAPH_STATUS_AGAIN
:
2675 if (bt_graph_is_canceled(ctx
.graph
)) {
2676 BT_LOGI_STR("Graph was canceled by user.");
2680 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2681 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2683 cfg
->cmd_data
.run
.retry_duration_us
);
2685 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2686 if (bt_graph_is_canceled(ctx
.graph
)) {
2687 BT_LOGI_STR("Graph was canceled by user.");
2693 case BT_GRAPH_STATUS_END
:
2696 BT_LOGE_STR("Graph failed to complete successfully");
2697 fprintf(stderr
, "Graph failed to complete successfully\n");
2710 cmd_run_ctx_destroy(&ctx
);
2715 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2717 const char *env_clash
;
2719 if (!cfg
->command_name
) {
2723 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2724 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2728 if (g_file_test(cfg
->command_name
,
2729 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2730 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2732 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2734 fprintf(stderr
, "\n");
2735 fprintf(stderr
, " babeltrace2 convert %s [OPTIONS]\n",
2741 void init_log_level(void)
2743 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2747 void set_auto_log_levels(struct bt_config
*cfg
)
2749 const char **env_var_name
;
2752 * Override the configuration's default log level if
2753 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2754 * are found for backward compatibility with legacy Babetrace 1.
2756 if (getenv("BABELTRACE_DEBUG") &&
2757 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2758 cfg
->log_level
= BT_LOG_VERBOSE
;
2759 } else if (getenv("BABELTRACE_VERBOSE") &&
2760 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2761 cfg
->log_level
= BT_LOG_INFO
;
2765 * Set log levels according to --debug or --verbose. For
2766 * backward compatibility, --debug is more verbose than
2769 * --verbose: INFO log level
2770 * --debug: VERBOSE log level (includes DEBUG, which is
2771 * is less verbose than VERBOSE in the internal
2772 * logging framework)
2774 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2776 bt_logging_set_global_level(BT_LOG_INFO
);
2777 } else if (cfg
->debug
) {
2778 bt_logging_set_global_level(BT_LOG_VERBOSE
);
2781 * Set library's default log level if not
2782 * explicitly specified.
2784 bt_logging_set_global_level(cfg
->log_level
);
2788 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2790 bt_cli_log_level
= BT_LOG_INFO
;
2791 } else if (cfg
->debug
) {
2792 bt_cli_log_level
= BT_LOG_VERBOSE
;
2795 * Set CLI's default log level if not explicitly
2798 bt_cli_log_level
= cfg
->log_level
;
2802 env_var_name
= log_level_env_var_names
;
2804 while (*env_var_name
) {
2805 if (!getenv(*env_var_name
)) {
2807 g_setenv(*env_var_name
, "I", 1);
2808 } else if (cfg
->debug
) {
2809 g_setenv(*env_var_name
, "V", 1);
2811 char val
[2] = { 0 };
2814 * Set module's default log level if not
2815 * explicitly specified.
2817 val
[0] = bt_log_get_letter_from_level(
2819 g_setenv(*env_var_name
, val
, 1);
2827 int main(int argc
, const char **argv
)
2831 struct bt_config
*cfg
;
2834 set_signal_handler();
2836 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2839 /* Quit without errors; typically usage/version */
2841 BT_LOGI_STR("Quitting without errors.");
2846 BT_LOGE("Command-line error: retcode=%d", retcode
);
2851 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2852 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2857 set_auto_log_levels(cfg
);
2860 if (cfg
->command_needs_plugins
) {
2861 ret
= load_all_plugins(cfg
->plugin_paths
);
2863 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2869 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2870 cfg
->command
, cfg
->command_name
);
2872 switch (cfg
->command
) {
2873 case BT_CONFIG_COMMAND_RUN
:
2876 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2877 ret
= cmd_list_plugins(cfg
);
2879 case BT_CONFIG_COMMAND_HELP
:
2880 ret
= cmd_help(cfg
);
2882 case BT_CONFIG_COMMAND_QUERY
:
2883 ret
= cmd_query(cfg
);
2885 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2886 ret
= cmd_print_ctf_metadata(cfg
);
2888 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2889 ret
= cmd_print_lttng_live_sessions(cfg
);
2892 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2896 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2897 cfg
->command
, cfg
->command_name
, ret
);
2898 warn_command_name_and_directory_clash(cfg
);
2899 retcode
= ret
? 1 : 0;
2902 BT_OBJECT_PUT_REF_AND_RESET(cfg
);