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 <babeltrace/babeltrace.h>
29 #include <babeltrace/common-internal.h>
39 #include "babeltrace-cfg.h"
40 #include "babeltrace-cfg-cli-args.h"
41 #include "babeltrace-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_PLUGIN_CTF_BTR_LOG_LEVEL",
55 "BABELTRACE_SINK_CTF_FS_LOG_LEVEL",
56 "BABELTRACE_SRC_CTF_FS_LOG_LEVEL",
57 "BABELTRACE_SRC_CTF_LTTNG_LIVE_LOG_LEVEL",
58 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
59 "BABELTRACE_PLUGIN_CTF_MSG_ITER_LOG_LEVEL",
60 "BABELTRACE_PLUGIN_CTFCOPYTRACE_LIB_LOG_LEVEL",
61 "BABELTRACE_FLT_LTTNG_UTILS_DEBUG_INFO_LOG_LEVEL",
62 "BABELTRACE_SRC_TEXT_DMESG_LOG_LEVEL",
63 "BABELTRACE_SINK_TEXT_PRETTY_LOG_LEVEL",
64 "BABELTRACE_FLT_UTILS_MUXER_LOG_LEVEL",
65 "BABELTRACE_FLT_UTILS_TRIMMER_LOG_LEVEL",
66 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
67 "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
71 /* Application's processing graph (weak) */
72 static bt_graph
*the_graph
;
73 static bt_query_executor
*the_query_executor
;
74 static bool canceled
= false;
76 GPtrArray
*loaded_plugins
;
83 BOOL WINAPI
signal_handler(DWORD signal
) {
85 bt_graph_cancel(the_graph
);
94 void set_signal_handler(void)
96 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
97 BT_LOGE("Failed to set the ctrl+c handler.");
101 #else /* __MINGW32__ */
104 void signal_handler(int signum
)
106 if (signum
!= SIGINT
) {
111 bt_graph_cancel(the_graph
);
114 if (the_query_executor
) {
115 bt_query_executor_cancel(the_query_executor
);
122 void set_signal_handler(void)
124 struct sigaction new_action
, old_action
;
126 new_action
.sa_handler
= signal_handler
;
127 sigemptyset(&new_action
.sa_mask
);
128 new_action
.sa_flags
= 0;
129 sigaction(SIGINT
, NULL
, &old_action
);
131 if (old_action
.sa_handler
!= SIG_IGN
) {
132 sigaction(SIGINT
, &new_action
, NULL
);
136 #endif /* __MINGW32__ */
139 void init_static_data(void)
141 loaded_plugins
= g_ptr_array_new_with_free_func(
142 (GDestroyNotify
) bt_object_put_ref
);
146 void fini_static_data(void)
148 g_ptr_array_free(loaded_plugins
, TRUE
);
152 int create_the_query_executor(void)
156 the_query_executor
= bt_query_executor_create();
157 if (!the_query_executor
) {
158 BT_LOGE_STR("Cannot create a query executor.");
166 void destroy_the_query_executor(void)
168 BT_QUERY_EXECUTOR_PUT_REF_AND_RESET(the_query_executor
);
172 int query(const bt_component_class
*comp_cls
, const char *obj
,
173 const bt_value
*params
, const bt_value
**user_result
,
174 const char **fail_reason
)
176 const bt_value
*result
= NULL
;
177 bt_query_executor_status status
;
178 *fail_reason
= "unknown error";
181 BT_ASSERT(fail_reason
);
182 BT_ASSERT(user_result
);
183 ret
= create_the_query_executor();
185 /* create_the_query_executor() logs errors */
190 BT_LOGI("Canceled by user before executing the query: "
191 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
192 "query-obj=\"%s\"", comp_cls
,
193 bt_component_class_get_name(comp_cls
), obj
);
194 *fail_reason
= "canceled by user";
199 status
= bt_query_executor_query(the_query_executor
,
200 comp_cls
, obj
, params
, &result
);
202 case BT_QUERY_EXECUTOR_STATUS_OK
:
204 case BT_QUERY_EXECUTOR_STATUS_AGAIN
:
206 const uint64_t sleep_time_us
= 100000;
208 /* Wait 100 ms and retry */
209 BT_LOGV("Got BT_QUERY_EXECUTOR_STATUS_AGAIN: sleeping: "
210 "time-us=%" PRIu64
, sleep_time_us
);
212 if (usleep(sleep_time_us
)) {
213 if (bt_query_executor_is_canceled(the_query_executor
)) {
214 BT_LOGI("Query was canceled by user: "
215 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
216 "query-obj=\"%s\"", comp_cls
,
217 bt_component_class_get_name(comp_cls
),
219 *fail_reason
= "canceled by user";
226 case BT_QUERY_EXECUTOR_STATUS_CANCELED
:
227 *fail_reason
= "canceled by user";
229 case BT_QUERY_EXECUTOR_STATUS_ERROR
:
231 case BT_QUERY_EXECUTOR_STATUS_INVALID_OBJECT
:
232 *fail_reason
= "invalid or unknown query object";
234 case BT_QUERY_EXECUTOR_STATUS_INVALID_PARAMS
:
235 *fail_reason
= "invalid query parameters";
237 case BT_QUERY_EXECUTOR_STATUS_UNSUPPORTED
:
238 *fail_reason
= "unsupported action";
240 case BT_QUERY_EXECUTOR_STATUS_NOMEM
:
241 *fail_reason
= "not enough memory";
244 BT_LOGF("Unknown query status: status=%d", status
);
250 *user_result
= result
;
258 destroy_the_query_executor();
259 bt_value_put_ref(result
);
264 const bt_plugin
*find_plugin(const char *name
)
267 const bt_plugin
*plugin
= NULL
;
270 BT_LOGD("Finding plugin: name=\"%s\"", name
);
272 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
273 plugin
= g_ptr_array_index(loaded_plugins
, i
);
275 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
282 if (BT_LOG_ON_DEBUG
) {
284 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
286 BT_LOGD("Cannot find plugin.");
290 bt_plugin_get_ref(plugin
);
294 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
295 const bt_plugin
*, const char *);
298 const void *find_component_class_from_plugin(const char *plugin_name
,
299 const char *comp_class_name
,
300 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
302 const void *comp_class
= NULL
;
303 const bt_plugin
*plugin
;
305 BT_LOGD("Finding component class: plugin-name=\"%s\", "
306 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
308 plugin
= find_plugin(plugin_name
);
313 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
314 bt_object_get_ref(comp_class
);
315 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
318 if (BT_LOG_ON_DEBUG
) {
320 BT_LOGD("Found component class: comp-cls-addr=%p",
323 BT_LOGD("Cannot find source component class.");
331 const bt_component_class_source
*find_source_component_class(
332 const char *plugin_name
, const char *comp_class_name
)
334 return (const void *) find_component_class_from_plugin(
335 plugin_name
, comp_class_name
,
336 (plugin_borrow_comp_cls_func_t
)
337 bt_plugin_borrow_source_component_class_by_name_const
);
341 const bt_component_class_filter
*find_filter_component_class(
342 const char *plugin_name
, const char *comp_class_name
)
344 return (const void *) find_component_class_from_plugin(
345 plugin_name
, comp_class_name
,
346 (plugin_borrow_comp_cls_func_t
)
347 bt_plugin_borrow_filter_component_class_by_name_const
);
351 const bt_component_class_sink
*find_sink_component_class(
352 const char *plugin_name
, const char *comp_class_name
)
354 return (const void *) find_component_class_from_plugin(plugin_name
,
356 (plugin_borrow_comp_cls_func_t
)
357 bt_plugin_borrow_sink_component_class_by_name_const
);
361 const bt_component_class
*find_component_class(const char *plugin_name
,
362 const char *comp_class_name
,
363 bt_component_class_type comp_class_type
)
365 const bt_component_class
*comp_cls
= NULL
;
367 switch (comp_class_type
) {
368 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
369 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
371 case BT_COMPONENT_CLASS_TYPE_FILTER
:
372 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
374 case BT_COMPONENT_CLASS_TYPE_SINK
:
375 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
385 void print_indent(FILE *fp
, size_t indent
)
389 for (i
= 0; i
< indent
; i
++) {
395 const char *component_type_str(bt_component_class_type type
)
398 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
400 case BT_COMPONENT_CLASS_TYPE_SINK
:
402 case BT_COMPONENT_CLASS_TYPE_FILTER
:
410 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
411 const char *comp_cls_name
, bt_component_class_type type
)
413 GString
*shell_plugin_name
= NULL
;
414 GString
*shell_comp_cls_name
= NULL
;
416 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
417 if (!shell_plugin_name
) {
421 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
422 if (!shell_comp_cls_name
) {
426 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
427 bt_common_color_bold(),
428 bt_common_color_fg_cyan(),
429 component_type_str(type
),
430 bt_common_color_fg_default(),
431 bt_common_color_fg_blue(),
432 shell_plugin_name
->str
,
433 bt_common_color_fg_default(),
434 bt_common_color_fg_yellow(),
435 shell_comp_cls_name
->str
,
436 bt_common_color_reset());
439 if (shell_plugin_name
) {
440 g_string_free(shell_plugin_name
, TRUE
);
443 if (shell_comp_cls_name
) {
444 g_string_free(shell_comp_cls_name
, TRUE
);
449 void print_value(FILE *, const bt_value
*, size_t);
452 void print_value_rec(FILE *, const bt_value
*, size_t);
454 struct print_map_value_data
{
460 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
463 struct print_map_value_data
*print_map_value_data
= data
;
465 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
466 fprintf(print_map_value_data
->fp
, "%s: ", key
);
469 if (bt_value_is_array(object
) &&
470 bt_value_array_is_empty(object
)) {
471 fprintf(print_map_value_data
->fp
, "[ ]\n");
475 if (bt_value_is_map(object
) &&
476 bt_value_map_is_empty(object
)) {
477 fprintf(print_map_value_data
->fp
, "{ }\n");
481 if (bt_value_is_array(object
) ||
482 bt_value_is_map(object
)) {
483 fprintf(print_map_value_data
->fp
, "\n");
486 print_value_rec(print_map_value_data
->fp
, object
,
487 print_map_value_data
->indent
+ 2);
492 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
505 switch (bt_value_get_type(value
)) {
506 case BT_VALUE_TYPE_NULL
:
507 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
508 bt_common_color_reset());
510 case BT_VALUE_TYPE_BOOL
:
511 bool_val
= bt_value_bool_get(value
);
512 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
513 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
514 bt_common_color_reset());
516 case BT_VALUE_TYPE_INTEGER
:
517 int_val
= bt_value_integer_get(value
);
518 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
519 bt_common_color_fg_red(), int_val
,
520 bt_common_color_reset());
522 case BT_VALUE_TYPE_REAL
:
523 dbl_val
= bt_value_real_get(value
);
524 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
525 bt_common_color_fg_red(), dbl_val
,
526 bt_common_color_reset());
528 case BT_VALUE_TYPE_STRING
:
529 str_val
= bt_value_string_get(value
);
530 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
531 bt_common_color_fg_green(), str_val
,
532 bt_common_color_reset());
534 case BT_VALUE_TYPE_ARRAY
:
535 size
= bt_value_array_get_size(value
);
541 print_indent(fp
, indent
);
542 fprintf(fp
, "[ ]\n");
546 for (i
= 0; i
< size
; i
++) {
547 const bt_value
*element
=
548 bt_value_array_borrow_element_by_index_const(
554 print_indent(fp
, indent
);
557 if (bt_value_is_array(element
) &&
558 bt_value_array_is_empty(element
)) {
559 fprintf(fp
, "[ ]\n");
563 if (bt_value_is_map(element
) &&
564 bt_value_map_is_empty(element
)) {
565 fprintf(fp
, "{ }\n");
569 if (bt_value_is_array(element
) ||
570 bt_value_is_map(element
)) {
574 print_value_rec(fp
, element
, indent
+ 2);
577 case BT_VALUE_TYPE_MAP
:
579 struct print_map_value_data data
= {
584 if (bt_value_map_is_empty(value
)) {
585 print_indent(fp
, indent
);
586 fprintf(fp
, "{ }\n");
590 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
599 BT_LOGE("Error printing value of type %s.",
600 bt_common_value_type_string(bt_value_get_type(value
)));
604 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
606 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
607 print_indent(fp
, indent
);
610 print_value_rec(fp
, value
, indent
);
614 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
616 fprintf(stderr
, " ");
617 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
618 bt_config_component
->comp_cls_name
->str
,
619 bt_config_component
->type
);
620 fprintf(stderr
, ":\n");
622 if (bt_config_component
->instance_name
->len
> 0) {
623 fprintf(stderr
, " Name: %s\n",
624 bt_config_component
->instance_name
->str
);
627 fprintf(stderr
, " Parameters:\n");
628 print_value(stderr
, bt_config_component
->params
, 8);
632 void print_bt_config_components(GPtrArray
*array
)
636 for (i
= 0; i
< array
->len
; i
++) {
637 struct bt_config_component
*cfg_component
=
638 bt_config_get_component(array
, i
);
639 print_bt_config_component(cfg_component
);
640 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
645 void print_plugin_paths(const bt_value
*plugin_paths
)
647 fprintf(stderr
, " Plugin paths:\n");
648 print_value(stderr
, plugin_paths
, 4);
652 void print_cfg_run(struct bt_config
*cfg
)
656 print_plugin_paths(cfg
->plugin_paths
);
657 fprintf(stderr
, " Source component instances:\n");
658 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
660 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
661 fprintf(stderr
, " Filter component instances:\n");
662 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
665 fprintf(stderr
, " Sink component instances:\n");
666 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
667 fprintf(stderr
, " Connections:\n");
669 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
670 struct bt_config_connection
*cfg_connection
=
671 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
674 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
675 cfg_connection
->upstream_comp_name
->str
,
676 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
677 cfg_connection
->upstream_port_glob
->str
,
678 cfg_connection
->downstream_comp_name
->str
,
679 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
680 cfg_connection
->downstream_port_glob
->str
);
685 void print_cfg_list_plugins(struct bt_config
*cfg
)
687 print_plugin_paths(cfg
->plugin_paths
);
691 void print_cfg_help(struct bt_config
*cfg
)
693 print_plugin_paths(cfg
->plugin_paths
);
697 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
699 print_plugin_paths(cfg
->plugin_paths
);
700 fprintf(stderr
, " Path: %s\n",
701 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
705 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
707 print_plugin_paths(cfg
->plugin_paths
);
708 fprintf(stderr
, " URL: %s\n",
709 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
713 void print_cfg_query(struct bt_config
*cfg
)
715 print_plugin_paths(cfg
->plugin_paths
);
716 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
717 fprintf(stderr
, " Component class:\n");
718 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
722 void print_cfg(struct bt_config
*cfg
)
724 if (!BT_LOG_ON_INFO
) {
728 BT_LOGI_STR("Configuration:");
729 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
730 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
732 switch (cfg
->command
) {
733 case BT_CONFIG_COMMAND_RUN
:
736 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
737 print_cfg_list_plugins(cfg
);
739 case BT_CONFIG_COMMAND_HELP
:
742 case BT_CONFIG_COMMAND_QUERY
:
743 print_cfg_query(cfg
);
745 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
746 print_cfg_print_ctf_metadata(cfg
);
748 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
749 print_cfg_print_lttng_live_sessions(cfg
);
757 void add_to_loaded_plugins(const bt_plugin_set
*plugin_set
)
762 count
= bt_plugin_set_get_plugin_count(plugin_set
);
763 BT_ASSERT(count
>= 0);
765 for (i
= 0; i
< count
; i
++) {
766 const bt_plugin
*plugin
=
767 bt_plugin_set_borrow_plugin_by_index_const(plugin_set
, i
);
768 const bt_plugin
*loaded_plugin
=
769 find_plugin(bt_plugin_get_name(plugin
));
774 BT_LOGI("Not using plugin: another one already exists with the same name: "
775 "plugin-name=\"%s\", plugin-path=\"%s\", "
776 "existing-plugin-path=\"%s\"",
777 bt_plugin_get_name(plugin
),
778 bt_plugin_get_path(plugin
),
779 bt_plugin_get_path(loaded_plugin
));
780 bt_plugin_put_ref(loaded_plugin
);
782 /* Add to global array. */
783 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
784 bt_plugin_get_name(plugin
));
785 bt_plugin_get_ref(plugin
);
786 g_ptr_array_add(loaded_plugins
, (void *) plugin
);
792 int load_dynamic_plugins(const bt_value
*plugin_paths
)
794 int nr_paths
, i
, ret
= 0;
796 nr_paths
= bt_value_array_get_size(plugin_paths
);
798 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
803 BT_LOGI("Loading dynamic plugins.");
805 for (i
= 0; i
< nr_paths
; i
++) {
806 const bt_value
*plugin_path_value
= NULL
;
807 const char *plugin_path
;
808 const bt_plugin_set
*plugin_set
;
811 bt_value_array_borrow_element_by_index_const(
813 plugin_path
= bt_value_string_get(plugin_path_value
);
816 * Skip this if the directory does not exist because
817 * bt_plugin_find_all_from_dir() expects an existing
820 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
821 BT_LOGV("Skipping nonexistent directory path: "
822 "path=\"%s\"", plugin_path
);
826 plugin_set
= bt_plugin_find_all_from_dir(plugin_path
, false);
828 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
833 add_to_loaded_plugins(plugin_set
);
834 bt_plugin_set_put_ref(plugin_set
);
841 int load_static_plugins(void)
844 const bt_plugin_set
*plugin_set
;
846 BT_LOGI("Loading static plugins.");
847 plugin_set
= bt_plugin_find_all_from_static();
849 BT_LOGE("Unable to load static plugins.");
854 add_to_loaded_plugins(plugin_set
);
855 bt_plugin_set_put_ref(plugin_set
);
861 int load_all_plugins(const bt_value
*plugin_paths
)
865 if (load_dynamic_plugins(plugin_paths
)) {
870 if (load_static_plugins()) {
875 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
882 void print_plugin_info(const bt_plugin
*plugin
)
884 unsigned int major
, minor
, patch
;
886 bt_property_availability version_avail
;
887 const char *plugin_name
;
891 const char *plugin_description
;
893 plugin_name
= bt_plugin_get_name(plugin
);
894 path
= bt_plugin_get_path(plugin
);
895 author
= bt_plugin_get_author(plugin
);
896 license
= bt_plugin_get_license(plugin
);
897 plugin_description
= bt_plugin_get_description(plugin
);
898 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
900 printf("%s%s%s%s:\n", bt_common_color_bold(),
901 bt_common_color_fg_blue(), plugin_name
,
902 bt_common_color_reset());
904 printf(" %sPath%s: %s\n", bt_common_color_bold(),
905 bt_common_color_reset(), path
);
910 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
911 printf(" %sVersion%s: %u.%u.%u",
912 bt_common_color_bold(), bt_common_color_reset(),
913 major
, minor
, patch
);
922 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
923 bt_common_color_reset(),
924 plugin_description
? plugin_description
: "(None)");
925 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
926 bt_common_color_reset(), author
? author
: "(Unknown)");
927 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
928 bt_common_color_reset(),
929 license
? license
: "(Unknown)");
933 int cmd_query(struct bt_config
*cfg
)
936 const bt_component_class
*comp_cls
= NULL
;
937 const bt_value
*results
= NULL
;
938 const char *fail_reason
= NULL
;
940 comp_cls
= find_component_class(
941 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
942 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
943 cfg
->cmd_data
.query
.cfg_component
->type
);
945 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
946 "comp-cls-name=\"%s\", comp-cls-type=%d",
947 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
948 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
949 cfg
->cmd_data
.query
.cfg_component
->type
);
950 fprintf(stderr
, "%s%sCannot find component class %s",
951 bt_common_color_bold(),
952 bt_common_color_fg_red(),
953 bt_common_color_reset());
954 print_plugin_comp_cls_opt(stderr
,
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
, "\n");
963 ret
= query(comp_cls
, cfg
->cmd_data
.query
.object
->str
,
964 cfg
->cmd_data
.query
.cfg_component
->params
,
965 &results
, &fail_reason
);
970 print_value(stdout
, results
, 0);
974 BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", "
975 "comp-cls-name=\"%s\", comp-cls-type=%d "
976 "object=\"%s\"", fail_reason
,
977 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
978 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
979 cfg
->cmd_data
.query
.cfg_component
->type
,
980 cfg
->cmd_data
.query
.object
->str
);
981 fprintf(stderr
, "%s%sFailed to query info to %s",
982 bt_common_color_bold(),
983 bt_common_color_fg_red(),
984 bt_common_color_reset());
985 print_plugin_comp_cls_opt(stderr
,
986 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
987 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
988 cfg
->cmd_data
.query
.cfg_component
->type
);
989 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
990 bt_common_color_bold(),
991 bt_common_color_fg_red(),
992 cfg
->cmd_data
.query
.object
->str
,
994 bt_common_color_reset());
998 bt_component_class_put_ref(comp_cls
);
999 bt_value_put_ref(results
);
1004 void print_component_class_help(const char *plugin_name
,
1005 const bt_component_class
*comp_cls
)
1007 const char *comp_class_name
=
1008 bt_component_class_get_name(comp_cls
);
1009 const char *comp_class_description
=
1010 bt_component_class_get_description(comp_cls
);
1011 const char *comp_class_help
=
1012 bt_component_class_get_help(comp_cls
);
1013 bt_component_class_type type
=
1014 bt_component_class_get_type(comp_cls
);
1016 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
1018 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1019 bt_common_color_reset(),
1020 comp_class_description
? comp_class_description
: "(None)");
1022 if (comp_class_help
) {
1023 printf("\n%s\n", comp_class_help
);
1028 int cmd_help(struct bt_config
*cfg
)
1031 const bt_plugin
*plugin
= NULL
;
1032 const bt_component_class
*needed_comp_cls
= NULL
;
1034 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1036 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
1037 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1038 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
1039 bt_common_color_bold(), bt_common_color_fg_red(),
1040 bt_common_color_fg_blue(),
1041 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1042 bt_common_color_reset());
1047 print_plugin_info(plugin
);
1048 printf(" %sSource component classes%s: %d\n",
1049 bt_common_color_bold(),
1050 bt_common_color_reset(),
1051 (int) bt_plugin_get_source_component_class_count(plugin
));
1052 printf(" %sFilter component classes%s: %d\n",
1053 bt_common_color_bold(),
1054 bt_common_color_reset(),
1055 (int) bt_plugin_get_filter_component_class_count(plugin
));
1056 printf(" %sSink component classes%s: %d\n",
1057 bt_common_color_bold(),
1058 bt_common_color_reset(),
1059 (int) bt_plugin_get_sink_component_class_count(plugin
));
1061 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
1062 /* Plugin help only */
1066 needed_comp_cls
= find_component_class(
1067 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1068 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1069 cfg
->cmd_data
.help
.cfg_component
->type
);
1070 if (!needed_comp_cls
) {
1071 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1072 "comp-cls-name=\"%s\", comp-cls-type=%d",
1073 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1074 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1075 cfg
->cmd_data
.help
.cfg_component
->type
);
1076 fprintf(stderr
, "\n%s%sCannot find component class %s",
1077 bt_common_color_bold(),
1078 bt_common_color_fg_red(),
1079 bt_common_color_reset());
1080 print_plugin_comp_cls_opt(stderr
,
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");
1090 print_component_class_help(
1091 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1095 bt_component_class_put_ref(needed_comp_cls
);
1096 bt_plugin_put_ref(plugin
);
1100 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
1102 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
1105 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
1106 const char *cc_type_name
, uint64_t count
,
1107 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
1108 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
1113 printf(" %s%s component classes%s: (none)\n",
1114 bt_common_color_bold(),
1116 bt_common_color_reset());
1119 printf(" %s%s component classes%s:\n",
1120 bt_common_color_bold(),
1122 bt_common_color_reset());
1125 for (i
= 0; i
< count
; i
++) {
1126 const bt_component_class
*comp_class
=
1127 spec_comp_cls_borrow_comp_cls_func(
1128 borrow_comp_cls_by_index_func(plugin
, i
));
1129 const char *comp_class_name
=
1130 bt_component_class_get_name(comp_class
);
1131 const char *comp_class_description
=
1132 bt_component_class_get_description(comp_class
);
1133 bt_component_class_type type
=
1134 bt_component_class_get_type(comp_class
);
1137 print_plugin_comp_cls_opt(stdout
,
1138 bt_plugin_get_name(plugin
), comp_class_name
,
1141 if (comp_class_description
) {
1142 printf(": %s", comp_class_description
);
1153 int cmd_list_plugins(struct bt_config
*cfg
)
1156 int plugins_count
, component_classes_count
= 0, i
;
1158 printf("From the following plugin paths:\n\n");
1159 print_value(stdout
, cfg
->plugin_paths
, 2);
1161 plugins_count
= loaded_plugins
->len
;
1162 if (plugins_count
== 0) {
1163 printf("No plugins found.\n");
1167 for (i
= 0; i
< plugins_count
; i
++) {
1168 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1170 component_classes_count
+=
1171 bt_plugin_get_source_component_class_count(plugin
) +
1172 bt_plugin_get_filter_component_class_count(plugin
) +
1173 bt_plugin_get_sink_component_class_count(plugin
);
1176 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1177 bt_common_color_bold(),
1178 component_classes_count
,
1179 bt_common_color_reset(),
1180 bt_common_color_bold(),
1182 bt_common_color_reset());
1184 for (i
= 0; i
< plugins_count
; i
++) {
1185 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1188 print_plugin_info(plugin
);
1189 cmd_list_plugins_print_component_classes(plugin
, "Source",
1190 bt_plugin_get_source_component_class_count(plugin
),
1191 (plugin_borrow_comp_cls_by_index_func_t
)
1192 bt_plugin_borrow_source_component_class_by_index_const
,
1193 (spec_comp_cls_borrow_comp_cls_func_t
)
1194 bt_component_class_source_as_component_class
);
1195 cmd_list_plugins_print_component_classes(plugin
, "Filter",
1196 bt_plugin_get_filter_component_class_count(plugin
),
1197 (plugin_borrow_comp_cls_by_index_func_t
)
1198 bt_plugin_borrow_filter_component_class_by_index_const
,
1199 (spec_comp_cls_borrow_comp_cls_func_t
)
1200 bt_component_class_filter_as_component_class
);
1201 cmd_list_plugins_print_component_classes(plugin
, "Sink",
1202 bt_plugin_get_sink_component_class_count(plugin
),
1203 (plugin_borrow_comp_cls_by_index_func_t
)
1204 bt_plugin_borrow_sink_component_class_by_index_const
,
1205 (spec_comp_cls_borrow_comp_cls_func_t
)
1206 bt_component_class_sink_as_component_class
);
1214 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1217 const bt_component_class
*comp_cls
= NULL
;
1218 const bt_value
*results
= NULL
;
1219 bt_value
*params
= NULL
;
1220 const bt_value
*map
= NULL
;
1221 const bt_value
*v
= NULL
;
1222 static const char * const plugin_name
= "ctf";
1223 static const char * const comp_cls_name
= "lttng-live";
1224 static const bt_component_class_type comp_cls_type
=
1225 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1226 int64_t array_size
, i
;
1227 const char *fail_reason
= NULL
;
1228 FILE *out_stream
= stdout
;
1230 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1231 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1234 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1235 "comp-cls-name=\"%s\", comp-cls-type=%d",
1236 plugin_name
, comp_cls_name
,
1237 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1238 fprintf(stderr
, "%s%sCannot find component class %s",
1239 bt_common_color_bold(),
1240 bt_common_color_fg_red(),
1241 bt_common_color_reset());
1242 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1243 comp_cls_name
, comp_cls_type
);
1244 fprintf(stderr
, "\n");
1248 params
= bt_value_map_create();
1253 ret
= bt_value_map_insert_string_entry(params
, "url",
1254 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1259 ret
= query(comp_cls
, "sessions", params
,
1260 &results
, &fail_reason
);
1267 if (!bt_value_is_array(results
)) {
1268 BT_LOGE_STR("Expecting an array for sessions query.");
1269 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1270 bt_common_color_bold(),
1271 bt_common_color_fg_red(),
1272 bt_common_color_reset());
1276 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1278 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1282 BT_LOGE_ERRNO("Cannot open file for writing",
1284 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1289 array_size
= bt_value_array_get_size(results
);
1290 for (i
= 0; i
< array_size
; i
++) {
1291 const char *url_text
;
1292 int64_t timer_us
, streams
, clients
;
1294 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1296 BT_LOGE_STR("Unexpected empty array entry.");
1299 if (!bt_value_is_map(map
)) {
1300 BT_LOGE_STR("Unexpected entry type.");
1304 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1306 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1309 url_text
= bt_value_string_get(v
);
1310 fprintf(out_stream
, "%s", url_text
);
1311 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1313 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1316 timer_us
= bt_value_integer_get(v
);
1317 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1318 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1320 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1323 streams
= bt_value_integer_get(v
);
1324 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1325 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1327 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1330 clients
= bt_value_integer_get(v
);
1331 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1337 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1338 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1339 bt_common_color_bold(),
1340 bt_common_color_fg_red(),
1342 bt_common_color_reset());
1348 bt_value_put_ref(results
);
1349 bt_value_put_ref(params
);
1350 bt_component_class_put_ref(comp_cls
);
1352 if (out_stream
&& out_stream
!= stdout
) {
1353 int fclose_ret
= fclose(out_stream
);
1356 BT_LOGE_ERRNO("Cannot close file stream",
1358 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1366 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1369 const bt_component_class
*comp_cls
= NULL
;
1370 const bt_value
*results
= NULL
;
1371 bt_value
*params
= NULL
;
1372 const bt_value
*metadata_text_value
= NULL
;
1373 const char *metadata_text
= NULL
;
1374 static const char * const plugin_name
= "ctf";
1375 static const char * const comp_cls_name
= "fs";
1376 static const bt_component_class_type comp_cls_type
=
1377 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1378 const char *fail_reason
= NULL
;
1379 FILE *out_stream
= stdout
;
1381 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1382 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1385 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1386 "comp-cls-name=\"%s\", comp-cls-type=%d",
1387 plugin_name
, comp_cls_name
,
1388 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1389 fprintf(stderr
, "%s%sCannot find component class %s",
1390 bt_common_color_bold(),
1391 bt_common_color_fg_red(),
1392 bt_common_color_reset());
1393 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1394 comp_cls_name
, comp_cls_type
);
1395 fprintf(stderr
, "\n");
1400 params
= bt_value_map_create();
1406 ret
= bt_value_map_insert_string_entry(params
, "path",
1407 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1413 ret
= query(comp_cls
, "metadata-info",
1414 params
, &results
, &fail_reason
);
1419 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1421 if (!metadata_text_value
) {
1422 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1427 metadata_text
= bt_value_string_get(metadata_text_value
);
1429 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1431 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1435 BT_LOGE_ERRNO("Cannot open file for writing",
1437 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1442 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1444 BT_LOGE("Cannot write whole metadata text to output stream: "
1452 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1453 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1454 bt_common_color_bold(),
1455 bt_common_color_fg_red(),
1457 bt_common_color_reset());
1460 bt_value_put_ref(results
);
1461 bt_value_put_ref(params
);
1462 bt_component_class_put_ref(comp_cls
);
1464 if (out_stream
&& out_stream
!= stdout
) {
1465 int fclose_ret
= fclose(out_stream
);
1468 BT_LOGE_ERRNO("Cannot close file stream",
1470 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1478 char *instance_name
;
1482 struct trace_range
{
1483 uint64_t intersection_range_begin_ns
;
1484 uint64_t intersection_range_end_ns
;
1488 guint
port_id_hash(gconstpointer v
)
1490 const struct port_id
*id
= v
;
1492 BT_ASSERT(id
->instance_name
);
1493 BT_ASSERT(id
->port_name
);
1495 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1499 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1501 const struct port_id
*id1
= v1
;
1502 const struct port_id
*id2
= v2
;
1504 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1505 !strcmp(id1
->port_name
, id2
->port_name
);
1509 void port_id_destroy(gpointer data
)
1511 struct port_id
*id
= data
;
1513 free(id
->instance_name
);
1514 free(id
->port_name
);
1519 void trace_range_destroy(gpointer data
)
1524 struct cmd_run_ctx
{
1526 GHashTable
*src_components
;
1529 GHashTable
*flt_components
;
1532 GHashTable
*sink_components
;
1538 struct bt_config
*cfg
;
1542 bool stream_intersection_mode
;
1545 * Association of struct port_id -> struct trace_range.
1547 GHashTable
*intersections
;
1550 /* Returns a timestamp of the form "(-)s.ns" */
1552 char *s_from_ns(int64_t ns
)
1557 int64_t ts_sec_abs
, ts_nsec_abs
;
1558 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1559 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1561 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1562 is_negative
= false;
1563 ts_sec_abs
= ts_sec
;
1564 ts_nsec_abs
= ts_nsec
;
1565 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1566 is_negative
= false;
1567 ts_sec_abs
= ts_sec
- 1;
1568 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1569 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1571 ts_sec_abs
= ts_sec
;
1572 ts_nsec_abs
= -ts_nsec
;
1573 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1575 ts_sec_abs
= -(ts_sec
+ 1);
1576 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1577 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1579 ts_sec_abs
= -ts_sec
;
1580 ts_nsec_abs
= ts_nsec
;
1581 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1583 ts_sec_abs
= -ts_sec
;
1584 ts_nsec_abs
= -ts_nsec
;
1587 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1588 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1596 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1597 struct cmd_run_ctx
*ctx
,
1598 const bt_component
*upstream_comp
,
1599 const bt_port_output
*out_upstream_port
,
1600 struct bt_config_connection
*cfg_conn
)
1602 typedef uint64_t (*input_port_count_func_t
)(void *);
1603 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1604 const void *, uint64_t);
1605 const bt_port
*upstream_port
=
1606 bt_port_output_as_port_const(out_upstream_port
);
1609 GQuark downstreamp_comp_name_quark
;
1610 void *downstream_comp
;
1611 uint64_t downstream_port_count
;
1613 input_port_count_func_t port_count_fn
;
1614 borrow_input_port_by_index_func_t port_by_index_fn
;
1615 bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1616 bool insert_trimmer
= false;
1617 bt_value
*trimmer_params
= NULL
;
1618 char *intersection_begin
= NULL
;
1619 char *intersection_end
= NULL
;
1620 const bt_component_filter
*trimmer
= NULL
;
1621 const bt_component_class_filter
*trimmer_class
= NULL
;
1622 const bt_port_input
*trimmer_input
= NULL
;
1623 const bt_port_output
*trimmer_output
= NULL
;
1625 if (ctx
->intersections
&&
1626 bt_component_get_class_type(upstream_comp
) ==
1627 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1628 struct trace_range
*range
;
1629 struct port_id port_id
= {
1630 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1631 .port_name
= (char *) bt_port_get_name(upstream_port
)
1634 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1638 range
= (struct trace_range
*) g_hash_table_lookup(
1639 ctx
->intersections
, &port_id
);
1641 bt_value_status status
;
1643 intersection_begin
= s_from_ns(
1644 range
->intersection_range_begin_ns
);
1645 intersection_end
= s_from_ns(
1646 range
->intersection_range_end_ns
);
1647 if (!intersection_begin
|| !intersection_end
) {
1648 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1652 insert_trimmer
= true;
1653 trimmer_params
= bt_value_map_create();
1654 if (!trimmer_params
) {
1658 status
= bt_value_map_insert_string_entry(
1659 trimmer_params
, "begin", intersection_begin
);
1660 if (status
!= BT_VALUE_STATUS_OK
) {
1663 status
= bt_value_map_insert_string_entry(
1665 "end", intersection_end
);
1666 if (status
!= BT_VALUE_STATUS_OK
) {
1671 trimmer_class
= find_filter_component_class("utils", "trimmer");
1672 if (!trimmer_class
) {
1677 BT_LOGI("Connecting upstream port to the next available downstream port: "
1678 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1679 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1680 upstream_port
, bt_port_get_name(upstream_port
),
1681 cfg_conn
->downstream_comp_name
->str
,
1682 cfg_conn
->arg
->str
);
1683 downstreamp_comp_name_quark
= g_quark_from_string(
1684 cfg_conn
->downstream_comp_name
->str
);
1685 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1686 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1687 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1688 port_count_fn
= (input_port_count_func_t
)
1689 bt_component_filter_get_input_port_count
;
1690 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1691 bt_component_filter_borrow_input_port_by_index_const
;
1693 if (!downstream_comp
) {
1694 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1695 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1696 port_count_fn
= (input_port_count_func_t
)
1697 bt_component_sink_get_input_port_count
;
1698 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1699 bt_component_sink_borrow_input_port_by_index_const
;
1702 if (!downstream_comp
) {
1703 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1704 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1705 cfg_conn
->arg
->str
);
1706 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1707 cfg_conn
->arg
->str
);
1711 downstream_port_count
= port_count_fn(downstream_comp
);
1712 BT_ASSERT(downstream_port_count
>= 0);
1714 for (i
= 0; i
< downstream_port_count
; i
++) {
1715 const bt_port_input
*in_downstream_port
=
1716 port_by_index_fn(downstream_comp
, i
);
1717 const bt_port
*downstream_port
=
1718 bt_port_input_as_port_const(in_downstream_port
);
1719 const char *upstream_port_name
;
1720 const char *downstream_port_name
;
1722 BT_ASSERT(downstream_port
);
1724 /* Skip port if it's already connected. */
1725 if (bt_port_is_connected(downstream_port
)) {
1726 BT_LOGD("Skipping downstream port: already connected: "
1727 "port-addr=%p, port-name=\"%s\"",
1729 bt_port_get_name(downstream_port
));
1733 downstream_port_name
= bt_port_get_name(downstream_port
);
1734 BT_ASSERT(downstream_port_name
);
1735 upstream_port_name
= bt_port_get_name(upstream_port
);
1736 BT_ASSERT(upstream_port_name
);
1738 if (!bt_common_star_glob_match(
1739 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1740 downstream_port_name
, SIZE_MAX
)) {
1744 if (insert_trimmer
) {
1746 * In order to insert the trimmer between the
1747 * two components that were being connected, we
1748 * create a connection configuration entry which
1749 * describes a connection from the trimmer's
1750 * output to the original input that was being
1753 * Hence, the creation of the trimmer will cause
1754 * the graph "new port" listener to establish
1755 * all downstream connections as its output port
1756 * is connected. We will then establish the
1757 * connection between the original upstream
1758 * source and the trimmer.
1760 char *trimmer_name
= NULL
;
1761 bt_graph_status graph_status
;
1763 ret
= asprintf(&trimmer_name
,
1764 "stream-intersection-trimmer-%s",
1765 upstream_port_name
);
1771 ctx
->connect_ports
= false;
1772 graph_status
= bt_graph_add_filter_component(
1773 ctx
->graph
, trimmer_class
, trimmer_name
,
1774 trimmer_params
, &trimmer
);
1776 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1782 bt_component_filter_borrow_input_port_by_index_const(
1784 if (!trimmer_input
) {
1788 bt_component_filter_borrow_output_port_by_index_const(
1790 if (!trimmer_output
) {
1795 * Replace the current downstream port by the trimmer's
1798 in_downstream_port
= trimmer_input
;
1800 bt_port_input_as_port_const(in_downstream_port
);
1801 downstream_port_name
= bt_port_get_name(
1803 BT_ASSERT(downstream_port_name
);
1806 /* We have a winner! */
1807 status
= bt_graph_connect_ports(ctx
->graph
,
1808 out_upstream_port
, in_downstream_port
, NULL
);
1809 downstream_port
= NULL
;
1811 case BT_GRAPH_STATUS_OK
:
1813 case BT_GRAPH_STATUS_CANCELED
:
1814 BT_LOGI_STR("Graph was canceled by user.");
1815 status
= BT_GRAPH_STATUS_OK
;
1817 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1818 BT_LOGE("A component refused a connection to one of its ports: "
1819 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1820 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1821 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1822 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1824 upstream_comp
, bt_component_get_name(upstream_comp
),
1825 upstream_port
, bt_port_get_name(upstream_port
),
1826 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1827 downstream_port
, downstream_port_name
,
1828 cfg_conn
->arg
->str
);
1830 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1831 bt_port_get_name(upstream_port
),
1832 downstream_port_name
,
1833 cfg_conn
->arg
->str
);
1836 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1837 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1838 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1839 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1840 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1842 upstream_comp
, bt_component_get_name(upstream_comp
),
1843 upstream_port
, bt_port_get_name(upstream_port
),
1844 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1845 downstream_port
, downstream_port_name
,
1846 cfg_conn
->arg
->str
);
1848 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1849 bt_port_get_name(upstream_port
),
1850 downstream_port_name
,
1851 cfg_conn
->arg
->str
);
1855 BT_LOGI("Connected component ports: "
1856 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1857 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1858 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1859 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1861 upstream_comp
, bt_component_get_name(upstream_comp
),
1862 upstream_port
, bt_port_get_name(upstream_port
),
1863 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1864 downstream_port
, downstream_port_name
,
1865 cfg_conn
->arg
->str
);
1867 if (insert_trimmer
) {
1869 * The first connection, from the source to the trimmer,
1870 * has been done. We now connect the trimmer to the
1871 * original downstream port.
1873 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1875 bt_component_filter_as_component_const(trimmer
),
1876 trimmer_output
, cfg_conn
);
1880 ctx
->connect_ports
= true;
1884 * We found a matching downstream port: the search is
1890 /* No downstream port found */
1891 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1892 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1893 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1894 upstream_port
, bt_port_get_name(upstream_port
),
1895 cfg_conn
->downstream_comp_name
->str
,
1896 cfg_conn
->arg
->str
);
1898 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1899 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1905 free(intersection_begin
);
1906 free(intersection_end
);
1907 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1908 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1909 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1914 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1915 const bt_port_output
*upstream_port
)
1918 const char *upstream_port_name
;
1919 const char *upstream_comp_name
;
1920 const bt_component
*upstream_comp
= NULL
;
1924 BT_ASSERT(upstream_port
);
1925 upstream_port_name
= bt_port_get_name(
1926 bt_port_output_as_port_const(upstream_port
));
1927 BT_ASSERT(upstream_port_name
);
1928 upstream_comp
= bt_port_borrow_component_const(
1929 bt_port_output_as_port_const(upstream_port
));
1930 if (!upstream_comp
) {
1931 BT_LOGW("Upstream port to connect is not part of a component: "
1932 "port-addr=%p, port-name=\"%s\"",
1933 upstream_port
, upstream_port_name
);
1938 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1939 BT_ASSERT(upstream_comp_name
);
1940 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1941 "port-addr=%p, port-name=\"%s\"",
1942 upstream_comp
, upstream_comp_name
,
1943 upstream_port
, upstream_port_name
);
1945 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1946 struct bt_config_connection
*cfg_conn
=
1948 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1950 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1951 upstream_comp_name
)) {
1955 if (!bt_common_star_glob_match(
1956 cfg_conn
->upstream_port_glob
->str
,
1957 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1961 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1962 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1964 BT_LOGE("Cannot connect upstream port: "
1965 "port-addr=%p, port-name=\"%s\"",
1967 upstream_port_name
);
1969 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1972 cfg_conn
->arg
->str
);
1978 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1979 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1980 upstream_port_name
);
1982 "Cannot create connection: upstream port `%s` does not match any connection\n",
1983 upstream_port_name
);
1993 void graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1994 const bt_port_output
*out_port
)
1996 const bt_component
*comp
;
1997 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1999 comp
= bt_port_borrow_component_const(port
);
2000 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
2001 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
2002 comp
, comp
? bt_component_get_name(comp
) : "",
2003 port
, bt_port_get_name(port
));
2005 if (!ctx
->connect_ports
) {
2010 BT_LOGW_STR("Port has no component.");
2014 if (bt_port_is_connected(port
)) {
2015 BT_LOGW_STR("Port is already connected.");
2019 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
2020 BT_LOGF_STR("Cannot connect upstream port.");
2021 fprintf(stderr
, "Added port could not be connected: aborting\n");
2030 void graph_source_output_port_added_listener(
2031 const bt_component_source
*component
,
2032 const bt_port_output
*port
, void *data
)
2034 graph_output_port_added_listener(data
, port
);
2038 void graph_filter_output_port_added_listener(
2039 const bt_component_filter
*component
,
2040 const bt_port_output
*port
, void *data
)
2042 graph_output_port_added_listener(data
, port
);
2046 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
2052 if (ctx
->src_components
) {
2053 g_hash_table_destroy(ctx
->src_components
);
2054 ctx
->src_components
= NULL
;
2057 if (ctx
->flt_components
) {
2058 g_hash_table_destroy(ctx
->flt_components
);
2059 ctx
->flt_components
= NULL
;
2062 if (ctx
->sink_components
) {
2063 g_hash_table_destroy(ctx
->sink_components
);
2064 ctx
->sink_components
= NULL
;
2067 if (ctx
->intersections
) {
2068 g_hash_table_destroy(ctx
->intersections
);
2069 ctx
->intersections
= NULL
;
2072 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
2078 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2081 bt_graph_status status
;
2084 ctx
->connect_ports
= false;
2085 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
2086 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2087 if (!ctx
->src_components
) {
2091 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
2092 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2093 if (!ctx
->flt_components
) {
2097 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
2098 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2099 if (!ctx
->sink_components
) {
2103 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2104 ctx
->stream_intersection_mode
= true;
2105 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2106 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2107 if (!ctx
->intersections
) {
2112 ctx
->graph
= bt_graph_create();
2117 the_graph
= ctx
->graph
;
2118 status
= bt_graph_add_source_component_output_port_added_listener(
2119 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
2121 if (status
!= BT_GRAPH_STATUS_OK
) {
2122 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2126 status
= bt_graph_add_filter_component_output_port_added_listener(
2127 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
2129 if (status
!= BT_GRAPH_STATUS_OK
) {
2130 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2137 cmd_run_ctx_destroy(ctx
);
2145 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2146 struct bt_config_component
*cfg_comp
,
2147 const bt_component_class_source
*src_comp_cls
)
2151 int64_t trace_count
;
2152 const char *path
= NULL
;
2153 const bt_value
*query_result
= NULL
;
2154 const bt_value
*trace_info
= NULL
;
2155 const bt_value
*intersection_range
= NULL
;
2156 const bt_value
*intersection_begin
= NULL
;
2157 const bt_value
*intersection_end
= NULL
;
2158 const bt_value
*stream_path_value
= NULL
;
2159 const bt_value
*stream_paths
= NULL
;
2160 const bt_value
*stream_infos
= NULL
;
2161 const bt_value
*stream_info
= NULL
;
2162 struct port_id
*port_id
= NULL
;
2163 struct trace_range
*trace_range
= NULL
;
2164 const char *fail_reason
= NULL
;
2165 const bt_component_class
*comp_cls
=
2166 bt_component_class_source_as_component_class_const(src_comp_cls
);
2168 ret
= query(comp_cls
, "trace-info",
2169 cfg_comp
->params
, &query_result
,
2172 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2173 "comp-class-name=\"%s\"", fail_reason
,
2174 bt_component_class_get_name(comp_cls
));
2179 BT_ASSERT(query_result
);
2181 if (!bt_value_is_array(query_result
)) {
2182 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2183 "component-class-name=%s",
2184 bt_component_class_get_name(comp_cls
));
2189 trace_count
= bt_value_array_get_size(query_result
);
2190 if (trace_count
< 0) {
2195 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2197 uint64_t stream_idx
;
2198 int64_t stream_count
;
2200 trace_info
= bt_value_array_borrow_element_by_index_const(
2201 query_result
, trace_idx
);
2202 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2204 BT_LOGD_STR("Cannot retrieve trace from query result.");
2208 intersection_range
= bt_value_map_borrow_entry_value_const(
2209 trace_info
, "intersection-range-ns");
2210 if (!intersection_range
) {
2212 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2216 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
2218 if (!intersection_begin
) {
2220 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2224 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
2226 if (!intersection_end
) {
2228 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2232 begin
= bt_value_integer_get(intersection_begin
);
2233 end
= bt_value_integer_get(intersection_end
);
2235 if (begin
< 0 || end
< 0 || end
< begin
) {
2236 BT_LOGW("Invalid trace stream intersection values: "
2237 "intersection-range-ns:begin=%" PRId64
2238 ", intersection-range-ns:end=%" PRId64
,
2244 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
2246 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2248 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2252 stream_count
= bt_value_array_get_size(stream_infos
);
2253 if (stream_count
< 0) {
2261 * The first path of a stream's "paths" is currently used to
2262 * associate streams/ports to a given trace intersection.
2264 * This is a fragile hack as it relies on the port names
2265 * being set to the various streams path.
2267 * A stream name should be introduced as part of the trace-info
2270 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2271 const char *stream_path
;
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_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2303 stream_paths
= bt_value_map_borrow_entry_value_const(stream_info
,
2305 if (!stream_paths
|| !bt_value_is_array(stream_paths
)) {
2307 BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
2312 bt_value_array_borrow_element_by_index_const(
2314 if (!stream_path_value
||
2315 !bt_value_is_string(stream_path_value
)) {
2317 BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
2321 stream_path
= bt_value_string_get(stream_path_value
);
2322 port_id
->port_name
= strdup(stream_path
);
2323 if (!port_id
->port_name
) {
2325 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2329 BT_LOGD("Inserting stream intersection ");
2331 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2341 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2342 bt_common_color_bold(),
2343 bt_common_color_fg_yellow(),
2344 path
? path
: "(unknown)",
2345 bt_common_color_reset());
2347 bt_value_put_ref(query_result
);
2349 g_free(trace_range
);
2354 int cmd_run_ctx_create_components_from_config_components(
2355 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2358 const void *comp_cls
= NULL
;
2359 const void *comp
= NULL
;
2362 for (i
= 0; i
< cfg_components
->len
; i
++) {
2363 struct bt_config_component
*cfg_comp
=
2364 g_ptr_array_index(cfg_components
, i
);
2367 switch (cfg_comp
->type
) {
2368 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2369 comp_cls
= find_source_component_class(
2370 cfg_comp
->plugin_name
->str
,
2371 cfg_comp
->comp_cls_name
->str
);
2373 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2374 comp_cls
= find_filter_component_class(
2375 cfg_comp
->plugin_name
->str
,
2376 cfg_comp
->comp_cls_name
->str
);
2378 case BT_COMPONENT_CLASS_TYPE_SINK
:
2379 comp_cls
= find_sink_component_class(
2380 cfg_comp
->plugin_name
->str
,
2381 cfg_comp
->comp_cls_name
->str
);
2388 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2389 "comp-cls-name=\"%s\", comp-cls-type=%d",
2390 cfg_comp
->plugin_name
->str
,
2391 cfg_comp
->comp_cls_name
->str
,
2393 fprintf(stderr
, "%s%sCannot find component class %s",
2394 bt_common_color_bold(),
2395 bt_common_color_fg_red(),
2396 bt_common_color_reset());
2397 print_plugin_comp_cls_opt(stderr
,
2398 cfg_comp
->plugin_name
->str
,
2399 cfg_comp
->comp_cls_name
->str
,
2401 fprintf(stderr
, "\n");
2405 switch (cfg_comp
->type
) {
2406 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2407 ret
= bt_graph_add_source_component(ctx
->graph
,
2408 comp_cls
, cfg_comp
->instance_name
->str
,
2412 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2413 ret
= bt_graph_add_filter_component(ctx
->graph
,
2414 comp_cls
, cfg_comp
->instance_name
->str
,
2418 case BT_COMPONENT_CLASS_TYPE_SINK
:
2419 ret
= bt_graph_add_sink_component(ctx
->graph
,
2420 comp_cls
, cfg_comp
->instance_name
->str
,
2429 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2430 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2432 cfg_comp
->plugin_name
->str
,
2433 cfg_comp
->comp_cls_name
->str
,
2434 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2435 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2436 bt_common_color_bold(),
2437 bt_common_color_fg_red(),
2438 cfg_comp
->instance_name
->str
,
2439 bt_common_color_reset());
2443 if (ctx
->stream_intersection_mode
&&
2444 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2445 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2451 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2452 comp
, cfg_comp
->instance_name
->str
);
2453 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2454 BT_ASSERT(quark
> 0);
2456 switch (cfg_comp
->type
) {
2457 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2458 g_hash_table_insert(ctx
->src_components
,
2459 GUINT_TO_POINTER(quark
), (void *) comp
);
2461 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2462 g_hash_table_insert(ctx
->flt_components
,
2463 GUINT_TO_POINTER(quark
), (void *) comp
);
2465 case BT_COMPONENT_CLASS_TYPE_SINK
:
2466 g_hash_table_insert(ctx
->sink_components
,
2467 GUINT_TO_POINTER(quark
), (void *) comp
);
2474 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2483 bt_object_put_ref(comp
);
2484 bt_object_put_ref(comp_cls
);
2489 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2494 * Make sure that, during this phase, our graph's "port added"
2495 * listener does not connect ports while we are creating the
2496 * components because we have a special, initial phase for
2499 ctx
->connect_ports
= false;
2501 ret
= cmd_run_ctx_create_components_from_config_components(
2502 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2508 ret
= cmd_run_ctx_create_components_from_config_components(
2509 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2515 ret
= cmd_run_ctx_create_components_from_config_components(
2516 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2526 typedef uint64_t (*output_port_count_func_t
)(const void *);
2527 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2528 const void *, uint64_t);
2531 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2532 void *comp
, output_port_count_func_t port_count_fn
,
2533 borrow_output_port_by_index_func_t port_by_index_fn
)
2539 count
= port_count_fn(comp
);
2540 BT_ASSERT(count
>= 0);
2542 for (i
= 0; i
< count
; i
++) {
2543 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2545 BT_ASSERT(upstream_port
);
2546 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2557 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2560 GHashTableIter iter
;
2561 gpointer g_name_quark
, g_comp
;
2563 ctx
->connect_ports
= true;
2564 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2566 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2567 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2568 (output_port_count_func_t
)
2569 bt_component_source_get_output_port_count
,
2570 (borrow_output_port_by_index_func_t
)
2571 bt_component_source_borrow_output_port_by_index_const
);
2577 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2579 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2580 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2581 (output_port_count_func_t
)
2582 bt_component_filter_get_output_port_count
,
2583 (borrow_output_port_by_index_func_t
)
2584 bt_component_filter_borrow_output_port_by_index_const
);
2595 const char *bt_graph_status_str(bt_graph_status status
)
2598 case BT_GRAPH_STATUS_OK
:
2599 return "BT_GRAPH_STATUS_OK";
2600 case BT_GRAPH_STATUS_END
:
2601 return "BT_GRAPH_STATUS_END";
2602 case BT_GRAPH_STATUS_AGAIN
:
2603 return "BT_GRAPH_STATUS_AGAIN";
2604 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
2605 return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION";
2606 case BT_GRAPH_STATUS_CANCELED
:
2607 return "BT_GRAPH_STATUS_CANCELED";
2608 case BT_GRAPH_STATUS_ERROR
:
2609 return "BT_GRAPH_STATUS_ERROR";
2610 case BT_GRAPH_STATUS_NO_SINK
:
2611 return "BT_GRAPH_STATUS_NO_SINK";
2612 case BT_GRAPH_STATUS_NOMEM
:
2613 return "BT_GRAPH_STATUS_NOMEM";
2620 int cmd_run(struct bt_config
*cfg
)
2623 struct cmd_run_ctx ctx
= { 0 };
2625 /* Initialize the command's context and the graph object */
2626 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2627 BT_LOGE_STR("Cannot initialize the command's context.");
2628 fprintf(stderr
, "Cannot initialize the command's context\n");
2633 BT_LOGI_STR("Canceled by user before creating components.");
2637 BT_LOGI_STR("Creating components.");
2639 /* Create the requested component instances */
2640 if (cmd_run_ctx_create_components(&ctx
)) {
2641 BT_LOGE_STR("Cannot create components.");
2642 fprintf(stderr
, "Cannot create components\n");
2647 BT_LOGI_STR("Canceled by user before connecting components.");
2651 BT_LOGI_STR("Connecting components.");
2653 /* Connect the initially visible component ports */
2654 if (cmd_run_ctx_connect_ports(&ctx
)) {
2655 BT_LOGE_STR("Cannot connect initial component ports.");
2656 fprintf(stderr
, "Cannot connect initial component ports\n");
2661 BT_LOGI_STR("Canceled by user before running the graph.");
2665 BT_LOGI_STR("Running the graph.");
2669 bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2672 * Reset console in case something messed with console
2673 * codes during the graph's execution.
2675 printf("%s", bt_common_color_reset());
2677 fprintf(stderr
, "%s", bt_common_color_reset());
2678 BT_LOGV("bt_graph_run() returned: status=%s",
2679 bt_graph_status_str(graph_status
));
2681 switch (graph_status
) {
2682 case BT_GRAPH_STATUS_OK
:
2684 case BT_GRAPH_STATUS_CANCELED
:
2685 BT_LOGI_STR("Graph was canceled by user.");
2687 case BT_GRAPH_STATUS_AGAIN
:
2688 if (bt_graph_is_canceled(ctx
.graph
)) {
2689 BT_LOGI_STR("Graph was canceled by user.");
2693 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2694 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2696 cfg
->cmd_data
.run
.retry_duration_us
);
2698 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2699 if (bt_graph_is_canceled(ctx
.graph
)) {
2700 BT_LOGI_STR("Graph was canceled by user.");
2706 case BT_GRAPH_STATUS_END
:
2709 BT_LOGE_STR("Graph failed to complete successfully");
2710 fprintf(stderr
, "Graph failed to complete successfully\n");
2723 cmd_run_ctx_destroy(&ctx
);
2728 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2730 const char *env_clash
;
2732 if (!cfg
->command_name
) {
2736 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2737 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2741 if (g_file_test(cfg
->command_name
,
2742 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2743 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2745 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2747 fprintf(stderr
, "\n");
2748 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
2754 void init_log_level(void)
2756 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2760 void set_auto_log_levels(struct bt_config
*cfg
)
2762 const char **env_var_name
;
2765 * Override the configuration's default log level if
2766 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2767 * are found for backward compatibility with legacy Babetrace 1.
2769 if (getenv("BABELTRACE_DEBUG") &&
2770 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2771 cfg
->log_level
= 'V';
2772 } else if (getenv("BABELTRACE_VERBOSE") &&
2773 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2774 cfg
->log_level
= 'I';
2778 * Set log levels according to --debug or --verbose. For
2779 * backward compatibility, --debug is more verbose than
2782 * --verbose: INFO log level
2783 * --debug: VERBOSE log level (includes DEBUG, which is
2784 * is less verbose than VERBOSE in the internal
2785 * logging framework)
2787 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2789 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2790 } else if (cfg
->debug
) {
2791 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2794 * Set library's default log level if not
2795 * explicitly specified.
2797 switch (cfg
->log_level
) {
2799 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE
);
2802 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2805 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
2808 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2811 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN
);
2814 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR
);
2817 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL
);
2825 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2827 bt_cli_log_level
= BT_LOG_INFO
;
2828 } else if (cfg
->debug
) {
2829 bt_cli_log_level
= BT_LOG_VERBOSE
;
2832 * Set CLI's default log level if not explicitly
2835 switch (cfg
->log_level
) {
2837 bt_cli_log_level
= BT_LOG_NONE
;
2840 bt_cli_log_level
= BT_LOG_VERBOSE
;
2843 bt_cli_log_level
= BT_LOG_DEBUG
;
2846 bt_cli_log_level
= BT_LOG_INFO
;
2849 bt_cli_log_level
= BT_LOG_WARN
;
2852 bt_cli_log_level
= BT_LOG_ERROR
;
2855 bt_cli_log_level
= BT_LOG_FATAL
;
2863 env_var_name
= log_level_env_var_names
;
2865 while (*env_var_name
) {
2866 if (!getenv(*env_var_name
)) {
2868 g_setenv(*env_var_name
, "I", 1);
2869 } else if (cfg
->debug
) {
2870 g_setenv(*env_var_name
, "V", 1);
2872 char val
[2] = { 0 };
2875 * Set module's default log level if not
2876 * explicitly specified.
2878 val
[0] = cfg
->log_level
;
2879 g_setenv(*env_var_name
, val
, 1);
2887 int main(int argc
, const char **argv
)
2891 struct bt_config
*cfg
;
2894 set_signal_handler();
2896 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2899 /* Quit without errors; typically usage/version */
2901 BT_LOGI_STR("Quitting without errors.");
2906 BT_LOGE("Command-line error: retcode=%d", retcode
);
2911 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2912 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2917 set_auto_log_levels(cfg
);
2920 if (cfg
->command_needs_plugins
) {
2921 ret
= load_all_plugins(cfg
->plugin_paths
);
2923 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2929 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2930 cfg
->command
, cfg
->command_name
);
2932 switch (cfg
->command
) {
2933 case BT_CONFIG_COMMAND_RUN
:
2936 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2937 ret
= cmd_list_plugins(cfg
);
2939 case BT_CONFIG_COMMAND_HELP
:
2940 ret
= cmd_help(cfg
);
2942 case BT_CONFIG_COMMAND_QUERY
:
2943 ret
= cmd_query(cfg
);
2945 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2946 ret
= cmd_print_ctf_metadata(cfg
);
2948 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2949 ret
= cmd_print_lttng_live_sessions(cfg
);
2952 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2956 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2957 cfg
->command
, cfg
->command_name
, ret
);
2958 warn_command_name_and_directory_clash(cfg
);
2959 retcode
= ret
? 1 : 0;
2962 BT_OBJECT_PUT_REF_AND_RESET(cfg
);