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_NOTIF_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 struct bt_private_graph
*the_graph
;
73 static struct bt_private_query_executor
*the_query_executor
;
74 static bool canceled
= false;
76 GPtrArray
*loaded_plugins
;
83 BOOL WINAPI
signal_handler(DWORD signal
) {
85 bt_private_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_private_graph_cancel(the_graph
);
114 if (the_query_executor
) {
115 bt_private_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(bt_object_put_ref
);
145 void fini_static_data(void)
147 g_ptr_array_free(loaded_plugins
, TRUE
);
151 int create_the_query_executor(void)
155 the_query_executor
= bt_private_query_executor_create();
156 if (!the_query_executor
) {
157 BT_LOGE_STR("Cannot create a query executor.");
165 void destroy_the_query_executor(void)
167 BT_OBJECT_PUT_REF_AND_RESET(the_query_executor
);
171 int query(struct bt_component_class
*comp_cls
, const char *obj
,
172 struct bt_value
*params
, struct bt_value
**user_result
,
173 const char **fail_reason
)
175 struct bt_value
*result
= NULL
;
176 enum bt_query_status status
;
177 *fail_reason
= "unknown error";
180 BT_ASSERT(fail_reason
);
181 BT_ASSERT(user_result
);
182 ret
= create_the_query_executor();
184 /* create_the_query_executor() logs errors */
189 BT_LOGI("Canceled by user before executing the query: "
190 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
191 "query-obj=\"%s\"", comp_cls
,
192 bt_component_class_get_name(comp_cls
), obj
);
193 *fail_reason
= "canceled by user";
198 status
= bt_private_query_executor_query(the_query_executor
,
199 comp_cls
, obj
, params
, &result
);
201 case BT_QUERY_STATUS_OK
:
203 case BT_QUERY_STATUS_AGAIN
:
205 const uint64_t sleep_time_us
= 100000;
207 /* Wait 100 ms and retry */
208 BT_LOGV("Got BT_QUERY_STATUS_AGAIN: sleeping: "
209 "time-us=%" PRIu64
, sleep_time_us
);
211 if (usleep(sleep_time_us
)) {
212 if (bt_query_executor_is_canceled(
213 bt_private_query_executor_borrow_query_executor(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_STATUS_EXECUTOR_CANCELED
:
227 *fail_reason
= "canceled by user";
229 case BT_QUERY_STATUS_ERROR
:
231 case BT_QUERY_STATUS_INVALID_OBJECT
:
232 *fail_reason
= "invalid or unknown query object";
234 case BT_QUERY_STATUS_INVALID_PARAMS
:
235 *fail_reason
= "invalid query parameters";
237 case BT_QUERY_STATUS_NOMEM
:
238 *fail_reason
= "not enough memory";
241 BT_LOGF("Unknown query status: status=%d", status
);
247 *user_result
= result
;
255 destroy_the_query_executor();
256 bt_object_put_ref(result
);
261 struct bt_plugin
*find_plugin(const char *name
)
264 struct bt_plugin
*plugin
= NULL
;
267 BT_LOGD("Finding plugin: name=\"%s\"", name
);
269 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
270 plugin
= g_ptr_array_index(loaded_plugins
, i
);
272 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
279 if (BT_LOG_ON_DEBUG
) {
281 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
283 BT_LOGD("Cannot find plugin.");
287 return bt_object_get_ref(plugin
);
290 typedef void *(*plugin_borrow_comp_cls_func_t
)(struct bt_plugin
*,
294 void *find_component_class_from_plugin(const char *plugin_name
,
295 const char *comp_class_name
,
296 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
298 void *comp_class
= NULL
;
299 struct bt_plugin
*plugin
;
301 BT_LOGD("Finding component class: plugin-name=\"%s\", "
302 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
304 plugin
= find_plugin(plugin_name
);
309 comp_class
= bt_object_get_ref(
310 plugin_borrow_comp_cls_func(plugin
, comp_class_name
));
311 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
314 if (BT_LOG_ON_DEBUG
) {
316 BT_LOGD("Found component class: comp-cls-addr=%p",
319 BT_LOGD("Cannot find source component class.");
327 struct bt_component_class_source
*find_source_component_class(
328 const char *plugin_name
, const char *comp_class_name
)
330 return (void *) find_component_class_from_plugin(plugin_name
,
332 (plugin_borrow_comp_cls_func_t
)
333 bt_plugin_borrow_source_component_class_by_name
);
337 struct bt_component_class_filter
*find_filter_component_class(
338 const char *plugin_name
, const char *comp_class_name
)
340 return (void *) find_component_class_from_plugin(plugin_name
,
342 (plugin_borrow_comp_cls_func_t
)
343 bt_plugin_borrow_filter_component_class_by_name
);
347 struct bt_component_class_sink
*find_sink_component_class(
348 const char *plugin_name
, const char *comp_class_name
)
350 return (void *) find_component_class_from_plugin(plugin_name
,
352 (plugin_borrow_comp_cls_func_t
)
353 bt_plugin_borrow_sink_component_class_by_name
);
357 struct bt_component_class
*find_component_class(const char *plugin_name
,
358 const char *comp_class_name
,
359 enum bt_component_class_type comp_class_type
)
361 struct bt_component_class
*comp_cls
= NULL
;
363 switch (comp_class_type
) {
364 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
365 comp_cls
= bt_component_class_source_borrow_component_class(
366 find_source_component_class(plugin_name
,
369 case BT_COMPONENT_CLASS_TYPE_FILTER
:
370 comp_cls
= bt_component_class_filter_borrow_component_class(
371 find_filter_component_class(plugin_name
,
374 case BT_COMPONENT_CLASS_TYPE_SINK
:
375 comp_cls
= bt_component_class_sink_borrow_component_class(
376 find_sink_component_class(plugin_name
,
387 void print_indent(FILE *fp
, size_t indent
)
391 for (i
= 0; i
< indent
; i
++) {
397 const char *component_type_str(enum bt_component_class_type type
)
400 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
402 case BT_COMPONENT_CLASS_TYPE_SINK
:
404 case BT_COMPONENT_CLASS_TYPE_FILTER
:
412 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
413 const char *comp_cls_name
, enum bt_component_class_type type
)
415 GString
*shell_plugin_name
= NULL
;
416 GString
*shell_comp_cls_name
= NULL
;
418 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
419 if (!shell_plugin_name
) {
423 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
424 if (!shell_comp_cls_name
) {
428 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
429 bt_common_color_bold(),
430 bt_common_color_fg_cyan(),
431 component_type_str(type
),
432 bt_common_color_fg_default(),
433 bt_common_color_fg_blue(),
434 shell_plugin_name
->str
,
435 bt_common_color_fg_default(),
436 bt_common_color_fg_yellow(),
437 shell_comp_cls_name
->str
,
438 bt_common_color_reset());
441 if (shell_plugin_name
) {
442 g_string_free(shell_plugin_name
, TRUE
);
445 if (shell_comp_cls_name
) {
446 g_string_free(shell_comp_cls_name
, TRUE
);
451 void print_value(FILE *, struct bt_value
*, size_t);
454 void print_value_rec(FILE *, struct bt_value
*, size_t);
456 struct print_map_value_data
{
462 bt_bool
print_map_value(const char *key
, struct bt_value
*object
, void *data
)
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
, struct bt_value
*value
, size_t indent
)
506 switch (bt_value_get_type(value
)) {
507 case BT_VALUE_TYPE_NULL
:
508 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
509 bt_common_color_reset());
511 case BT_VALUE_TYPE_BOOL
:
512 bool_val
= bt_value_bool_get(value
);
513 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
514 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
515 bt_common_color_reset());
517 case BT_VALUE_TYPE_INTEGER
:
518 int_val
= bt_value_integer_get(value
);
519 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
520 bt_common_color_fg_red(), int_val
,
521 bt_common_color_reset());
523 case BT_VALUE_TYPE_REAL
:
524 dbl_val
= bt_value_real_get(value
);
525 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
526 bt_common_color_fg_red(), dbl_val
,
527 bt_common_color_reset());
529 case BT_VALUE_TYPE_STRING
:
530 str_val
= bt_value_string_get(value
);
531 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
532 bt_common_color_fg_green(), str_val
,
533 bt_common_color_reset());
535 case BT_VALUE_TYPE_ARRAY
:
536 size
= bt_value_array_get_size(value
);
542 print_indent(fp
, indent
);
543 fprintf(fp
, "[ ]\n");
547 for (i
= 0; i
< size
; i
++) {
548 struct bt_value
*element
=
549 bt_value_array_borrow_element_by_index(
555 print_indent(fp
, indent
);
558 if (bt_value_is_array(element
) &&
559 bt_value_array_is_empty(element
)) {
560 fprintf(fp
, "[ ]\n");
564 if (bt_value_is_map(element
) &&
565 bt_value_map_is_empty(element
)) {
566 fprintf(fp
, "{ }\n");
570 if (bt_value_is_array(element
) ||
571 bt_value_is_map(element
)) {
575 print_value_rec(fp
, element
, indent
+ 2);
578 case BT_VALUE_TYPE_MAP
:
580 struct print_map_value_data data
= {
585 if (bt_value_map_is_empty(value
)) {
586 print_indent(fp
, indent
);
587 fprintf(fp
, "{ }\n");
591 bt_value_map_foreach_entry(value
, print_map_value
, &data
);
600 BT_LOGE("Error printing value of type %s.",
601 bt_common_value_type_string(bt_value_get_type(value
)));
605 void print_value(FILE *fp
, struct bt_value
*value
, size_t indent
)
607 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
608 print_indent(fp
, indent
);
611 print_value_rec(fp
, value
, indent
);
615 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
617 fprintf(stderr
, " ");
618 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
619 bt_config_component
->comp_cls_name
->str
,
620 bt_config_component
->type
);
621 fprintf(stderr
, ":\n");
623 if (bt_config_component
->instance_name
->len
> 0) {
624 fprintf(stderr
, " Name: %s\n",
625 bt_config_component
->instance_name
->str
);
628 fprintf(stderr
, " Parameters:\n");
630 bt_private_value_borrow_value(bt_config_component
->params
), 8);
634 void print_bt_config_components(GPtrArray
*array
)
638 for (i
= 0; i
< array
->len
; i
++) {
639 struct bt_config_component
*cfg_component
=
640 bt_config_get_component(array
, i
);
641 print_bt_config_component(cfg_component
);
642 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
647 void print_plugin_paths(struct bt_value
*plugin_paths
)
649 fprintf(stderr
, " Plugin paths:\n");
650 print_value(stderr
, plugin_paths
, 4);
654 void print_cfg_run(struct bt_config
*cfg
)
658 print_plugin_paths(bt_private_value_borrow_value(cfg
->plugin_paths
));
659 fprintf(stderr
, " Source component instances:\n");
660 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
662 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
663 fprintf(stderr
, " Filter component instances:\n");
664 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
667 fprintf(stderr
, " Sink component instances:\n");
668 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
669 fprintf(stderr
, " Connections:\n");
671 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
672 struct bt_config_connection
*cfg_connection
=
673 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
676 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
677 cfg_connection
->upstream_comp_name
->str
,
678 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
679 cfg_connection
->upstream_port_glob
->str
,
680 cfg_connection
->downstream_comp_name
->str
,
681 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
682 cfg_connection
->downstream_port_glob
->str
);
687 void print_cfg_list_plugins(struct bt_config
*cfg
)
689 print_plugin_paths(bt_private_value_borrow_value(cfg
->plugin_paths
));
693 void print_cfg_help(struct bt_config
*cfg
)
695 print_plugin_paths(bt_private_value_borrow_value(cfg
->plugin_paths
));
699 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
701 print_plugin_paths(bt_private_value_borrow_value(cfg
->plugin_paths
));
702 fprintf(stderr
, " Path: %s\n",
703 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
707 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
709 print_plugin_paths(bt_private_value_borrow_value(cfg
->plugin_paths
));
710 fprintf(stderr
, " URL: %s\n",
711 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
715 void print_cfg_query(struct bt_config
*cfg
)
717 print_plugin_paths(bt_private_value_borrow_value(cfg
->plugin_paths
));
718 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
719 fprintf(stderr
, " Component class:\n");
720 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
724 void print_cfg(struct bt_config
*cfg
)
726 if (!BT_LOG_ON_INFO
) {
730 BT_LOGI_STR("Configuration:");
731 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
732 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
734 switch (cfg
->command
) {
735 case BT_CONFIG_COMMAND_RUN
:
738 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
739 print_cfg_list_plugins(cfg
);
741 case BT_CONFIG_COMMAND_HELP
:
744 case BT_CONFIG_COMMAND_QUERY
:
745 print_cfg_query(cfg
);
747 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
748 print_cfg_print_ctf_metadata(cfg
);
750 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
751 print_cfg_print_lttng_live_sessions(cfg
);
759 void add_to_loaded_plugins(struct bt_plugin_set
*plugin_set
)
764 count
= bt_plugin_set_get_plugin_count(plugin_set
);
765 BT_ASSERT(count
>= 0);
767 for (i
= 0; i
< count
; i
++) {
768 struct bt_plugin
*plugin
=
769 bt_plugin_set_borrow_plugin_by_index(plugin_set
, i
);
770 struct bt_plugin
*loaded_plugin
=
771 find_plugin(bt_plugin_get_name(plugin
));
776 BT_LOGI("Not using plugin: another one already exists with the same name: "
777 "plugin-name=\"%s\", plugin-path=\"%s\", "
778 "existing-plugin-path=\"%s\"",
779 bt_plugin_get_name(plugin
),
780 bt_plugin_get_path(plugin
),
781 bt_plugin_get_path(loaded_plugin
));
782 bt_object_put_ref(loaded_plugin
);
784 /* Add to global array. */
785 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
786 bt_plugin_get_name(plugin
));
787 g_ptr_array_add(loaded_plugins
, bt_object_get_ref(plugin
));
793 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
795 int nr_paths
, i
, ret
= 0;
797 nr_paths
= bt_value_array_get_size(plugin_paths
);
799 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
804 BT_LOGI("Loading dynamic plugins.");
806 for (i
= 0; i
< nr_paths
; i
++) {
807 struct bt_value
*plugin_path_value
= NULL
;
808 const char *plugin_path
;
809 struct bt_plugin_set
*plugin_set
;
811 plugin_path_value
= bt_value_array_borrow_element_by_index(
813 plugin_path
= bt_value_string_get(plugin_path_value
);
816 * Skip this if the directory does not exist because
817 * bt_plugin_create_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_create_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_object_put_ref(plugin_set
);
841 int load_static_plugins(void)
844 struct bt_plugin_set
*plugin_set
;
846 BT_LOGI("Loading static plugins.");
847 plugin_set
= bt_plugin_create_all_from_static();
849 BT_LOGE("Unable to load static plugins.");
854 add_to_loaded_plugins(plugin_set
);
855 bt_object_put_ref(plugin_set
);
861 int load_all_plugins(struct 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(struct bt_plugin
*plugin
)
884 unsigned int major
, minor
, patch
;
886 enum bt_plugin_status version_status
;
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_status
= 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_status
== BT_PLUGIN_STATUS_OK
) {
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 struct bt_component_class
*comp_cls
= NULL
;
937 struct 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 bt_private_value_borrow_value(
965 cfg
->cmd_data
.query
.cfg_component
->params
),
966 &results
, &fail_reason
);
971 print_value(stdout
, results
, 0);
975 BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", "
976 "comp-cls-name=\"%s\", comp-cls-type=%d "
977 "object=\"%s\"", fail_reason
,
978 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
979 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
980 cfg
->cmd_data
.query
.cfg_component
->type
,
981 cfg
->cmd_data
.query
.object
->str
);
982 fprintf(stderr
, "%s%sFailed to query info to %s",
983 bt_common_color_bold(),
984 bt_common_color_fg_red(),
985 bt_common_color_reset());
986 print_plugin_comp_cls_opt(stderr
,
987 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
988 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
989 cfg
->cmd_data
.query
.cfg_component
->type
);
990 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
991 bt_common_color_bold(),
992 bt_common_color_fg_red(),
993 cfg
->cmd_data
.query
.object
->str
,
995 bt_common_color_reset());
999 bt_object_put_ref(comp_cls
);
1000 bt_object_put_ref(results
);
1005 void print_component_class_help(const char *plugin_name
,
1006 struct bt_component_class
*comp_cls
)
1008 const char *comp_class_name
=
1009 bt_component_class_get_name(comp_cls
);
1010 const char *comp_class_description
=
1011 bt_component_class_get_description(comp_cls
);
1012 const char *comp_class_help
=
1013 bt_component_class_get_help(comp_cls
);
1014 enum bt_component_class_type type
=
1015 bt_component_class_get_type(comp_cls
);
1017 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
1019 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1020 bt_common_color_reset(),
1021 comp_class_description
? comp_class_description
: "(None)");
1023 if (comp_class_help
) {
1024 printf("\n%s\n", comp_class_help
);
1029 int cmd_help(struct bt_config
*cfg
)
1032 struct bt_plugin
*plugin
= NULL
;
1033 struct bt_component_class
*needed_comp_cls
= NULL
;
1035 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1037 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
1038 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1039 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
1040 bt_common_color_bold(), bt_common_color_fg_red(),
1041 bt_common_color_fg_blue(),
1042 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1043 bt_common_color_reset());
1048 print_plugin_info(plugin
);
1049 printf(" %sSource component classes%s: %d\n",
1050 bt_common_color_bold(),
1051 bt_common_color_reset(),
1052 (int) bt_plugin_get_source_component_class_count(plugin
));
1053 printf(" %sFilter component classes%s: %d\n",
1054 bt_common_color_bold(),
1055 bt_common_color_reset(),
1056 (int) bt_plugin_get_filter_component_class_count(plugin
));
1057 printf(" %sSink component classes%s: %d\n",
1058 bt_common_color_bold(),
1059 bt_common_color_reset(),
1060 (int) bt_plugin_get_sink_component_class_count(plugin
));
1062 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
1063 /* Plugin help only */
1067 needed_comp_cls
= find_component_class(
1068 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1069 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1070 cfg
->cmd_data
.help
.cfg_component
->type
);
1071 if (!needed_comp_cls
) {
1072 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1073 "comp-cls-name=\"%s\", comp-cls-type=%d",
1074 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1075 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1076 cfg
->cmd_data
.help
.cfg_component
->type
);
1077 fprintf(stderr
, "\n%s%sCannot find component class %s",
1078 bt_common_color_bold(),
1079 bt_common_color_fg_red(),
1080 bt_common_color_reset());
1081 print_plugin_comp_cls_opt(stderr
,
1082 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1083 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1084 cfg
->cmd_data
.help
.cfg_component
->type
);
1085 fprintf(stderr
, "\n");
1091 print_component_class_help(
1092 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1096 bt_object_put_ref(needed_comp_cls
);
1097 bt_object_put_ref(plugin
);
1101 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(struct bt_plugin
*,
1103 typedef struct bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
1106 void cmd_list_plugins_print_component_classes(struct bt_plugin
*plugin
,
1107 const char *cc_type_name
, uint64_t count
,
1108 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
1109 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
1114 printf(" %s%s component classes%s: (none)\n", cc_type_name
,
1115 bt_common_color_bold(),
1116 bt_common_color_reset());
1119 printf(" %s%s component classes%s:\n", cc_type_name
,
1120 bt_common_color_bold(),
1121 bt_common_color_reset());
1124 for (i
= 0; i
< count
; i
++) {
1125 struct bt_component_class
*comp_class
=
1126 spec_comp_cls_borrow_comp_cls_func(
1127 borrow_comp_cls_by_index_func(plugin
, i
));
1128 const char *comp_class_name
=
1129 bt_component_class_get_name(comp_class
);
1130 const char *comp_class_description
=
1131 bt_component_class_get_description(comp_class
);
1132 enum bt_component_class_type type
=
1133 bt_component_class_get_type(comp_class
);
1136 print_plugin_comp_cls_opt(stdout
,
1137 bt_plugin_get_name(plugin
), comp_class_name
,
1140 if (comp_class_description
) {
1141 printf(": %s", comp_class_description
);
1152 int cmd_list_plugins(struct bt_config
*cfg
)
1155 int plugins_count
, component_classes_count
= 0, i
;
1157 printf("From the following plugin paths:\n\n");
1158 print_value(stdout
, bt_private_value_borrow_value(cfg
->plugin_paths
), 2);
1160 plugins_count
= loaded_plugins
->len
;
1161 if (plugins_count
== 0) {
1162 printf("No plugins found.\n");
1166 for (i
= 0; i
< plugins_count
; i
++) {
1167 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1169 component_classes_count
+=
1170 bt_plugin_get_source_component_class_count(plugin
) +
1171 bt_plugin_get_filter_component_class_count(plugin
) +
1172 bt_plugin_get_sink_component_class_count(plugin
);
1175 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1176 bt_common_color_bold(),
1177 component_classes_count
,
1178 bt_common_color_reset(),
1179 bt_common_color_bold(),
1181 bt_common_color_reset());
1183 for (i
= 0; i
< plugins_count
; i
++) {
1184 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1187 print_plugin_info(plugin
);
1188 cmd_list_plugins_print_component_classes(plugin
, "Source",
1189 bt_plugin_get_source_component_class_count(plugin
),
1190 (plugin_borrow_comp_cls_by_index_func_t
)
1191 bt_plugin_borrow_source_component_class_by_name
,
1192 (spec_comp_cls_borrow_comp_cls_func_t
)
1193 bt_component_class_source_borrow_component_class
);
1194 cmd_list_plugins_print_component_classes(plugin
, "Filter",
1195 bt_plugin_get_filter_component_class_count(plugin
),
1196 (plugin_borrow_comp_cls_by_index_func_t
)
1197 bt_plugin_borrow_filter_component_class_by_name
,
1198 (spec_comp_cls_borrow_comp_cls_func_t
)
1199 bt_component_class_filter_borrow_component_class
);
1200 cmd_list_plugins_print_component_classes(plugin
, "Sink",
1201 bt_plugin_get_sink_component_class_count(plugin
),
1202 (plugin_borrow_comp_cls_by_index_func_t
)
1203 bt_plugin_borrow_sink_component_class_by_name
,
1204 (spec_comp_cls_borrow_comp_cls_func_t
)
1205 bt_component_class_sink_borrow_component_class
);
1213 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1216 struct bt_component_class
*comp_cls
= NULL
;
1217 struct bt_value
*results
= NULL
;
1218 struct bt_private_value
*params
= NULL
;
1219 struct bt_value
*map
= NULL
;
1220 struct bt_value
*v
= NULL
;
1221 static const char * const plugin_name
= "ctf";
1222 static const char * const comp_cls_name
= "lttng-live";
1223 static const enum bt_component_class_type comp_cls_type
=
1224 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1225 int64_t array_size
, i
;
1226 const char *fail_reason
= NULL
;
1227 FILE *out_stream
= stdout
;
1229 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1230 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1233 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1234 "comp-cls-name=\"%s\", comp-cls-type=%d",
1235 plugin_name
, comp_cls_name
,
1236 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1237 fprintf(stderr
, "%s%sCannot find component class %s",
1238 bt_common_color_bold(),
1239 bt_common_color_fg_red(),
1240 bt_common_color_reset());
1241 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1242 comp_cls_name
, comp_cls_type
);
1243 fprintf(stderr
, "\n");
1247 params
= bt_private_value_map_create();
1252 ret
= bt_private_value_map_insert_string_entry(params
, "url",
1253 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1258 ret
= query(comp_cls
, "sessions", bt_private_value_borrow_value(params
),
1259 &results
, &fail_reason
);
1266 if (!bt_value_is_array(results
)) {
1267 BT_LOGE_STR("Expecting an array for sessions query.");
1268 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1269 bt_common_color_bold(),
1270 bt_common_color_fg_red(),
1271 bt_common_color_reset());
1275 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1277 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1281 BT_LOGE_ERRNO("Cannot open file for writing",
1283 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1288 array_size
= bt_value_array_get_size(results
);
1289 for (i
= 0; i
< array_size
; i
++) {
1290 const char *url_text
;
1291 int64_t timer_us
, streams
, clients
;
1293 map
= bt_value_array_borrow_element_by_index(results
, i
);
1295 BT_LOGE_STR("Unexpected empty array entry.");
1298 if (!bt_value_is_map(map
)) {
1299 BT_LOGE_STR("Unexpected entry type.");
1303 v
= bt_value_map_borrow_entry_value(map
, "url");
1305 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1308 url_text
= bt_value_string_get(v
);
1309 fprintf(out_stream
, "%s", url_text
);
1310 v
= bt_value_map_borrow_entry_value(map
, "timer-us");
1312 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1315 timer_us
= bt_value_integer_get(v
);
1316 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1317 v
= bt_value_map_borrow_entry_value(map
, "stream-count");
1319 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1322 streams
= bt_value_integer_get(v
);
1323 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1324 v
= bt_value_map_borrow_entry_value(map
, "client-count");
1326 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1329 clients
= bt_value_integer_get(v
);
1330 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1336 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1337 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1338 bt_common_color_bold(),
1339 bt_common_color_fg_red(),
1341 bt_common_color_reset());
1347 bt_object_put_ref(results
);
1348 bt_object_put_ref(params
);
1349 bt_object_put_ref(comp_cls
);
1351 if (out_stream
&& out_stream
!= stdout
) {
1352 int fclose_ret
= fclose(out_stream
);
1355 BT_LOGE_ERRNO("Cannot close file stream",
1357 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1365 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1368 struct bt_component_class
*comp_cls
= NULL
;
1369 struct bt_value
*results
= NULL
;
1370 struct bt_private_value
*params
= NULL
;
1371 struct bt_value
*metadata_text_value
= NULL
;
1372 const char *metadata_text
= NULL
;
1373 static const char * const plugin_name
= "ctf";
1374 static const char * const comp_cls_name
= "fs";
1375 static const enum bt_component_class_type comp_cls_type
=
1376 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1377 const char *fail_reason
= NULL
;
1378 FILE *out_stream
= stdout
;
1380 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1381 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1384 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1385 "comp-cls-name=\"%s\", comp-cls-type=%d",
1386 plugin_name
, comp_cls_name
,
1387 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1388 fprintf(stderr
, "%s%sCannot find component class %s",
1389 bt_common_color_bold(),
1390 bt_common_color_fg_red(),
1391 bt_common_color_reset());
1392 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1393 comp_cls_name
, comp_cls_type
);
1394 fprintf(stderr
, "\n");
1399 params
= bt_private_value_map_create();
1405 ret
= bt_private_value_map_insert_string_entry(params
, "path",
1406 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1412 ret
= query(comp_cls
, "metadata-info",
1413 bt_private_value_borrow_value(params
), &results
, &fail_reason
);
1418 metadata_text_value
= bt_value_map_borrow_entry_value(results
, "text");
1419 if (!metadata_text_value
) {
1420 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1425 metadata_text
= bt_value_string_get(metadata_text_value
);
1427 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1429 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1433 BT_LOGE_ERRNO("Cannot open file for writing",
1435 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1440 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1442 BT_LOGE("Cannot write whole metadata text to output stream: "
1450 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1451 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1452 bt_common_color_bold(),
1453 bt_common_color_fg_red(),
1455 bt_common_color_reset());
1458 bt_object_put_ref(results
);
1459 bt_object_put_ref(params
);
1460 bt_object_put_ref(comp_cls
);
1462 if (out_stream
&& out_stream
!= stdout
) {
1463 int fclose_ret
= fclose(out_stream
);
1466 BT_LOGE_ERRNO("Cannot close file stream",
1468 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1476 char *instance_name
;
1480 struct trace_range
{
1481 uint64_t intersection_range_begin_ns
;
1482 uint64_t intersection_range_end_ns
;
1486 guint
port_id_hash(gconstpointer v
)
1488 const struct port_id
*id
= v
;
1490 BT_ASSERT(id
->instance_name
);
1491 BT_ASSERT(id
->port_name
);
1493 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1497 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1499 const struct port_id
*id1
= v1
;
1500 const struct port_id
*id2
= v2
;
1502 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1503 !strcmp(id1
->port_name
, id2
->port_name
);
1507 void port_id_destroy(gpointer data
)
1509 struct port_id
*id
= data
;
1511 free(id
->instance_name
);
1512 free(id
->port_name
);
1517 void trace_range_destroy(gpointer data
)
1522 struct cmd_run_ctx
{
1524 GHashTable
*src_components
;
1527 GHashTable
*flt_components
;
1530 GHashTable
*sink_components
;
1533 struct bt_private_graph
*graph
;
1536 struct bt_config
*cfg
;
1540 bool stream_intersection_mode
;
1543 * Association of struct port_id -> struct trace_range.
1545 GHashTable
*intersections
;
1548 /* Returns a timestamp of the form "(-)s.ns" */
1550 char *s_from_ns(int64_t ns
)
1555 int64_t ts_sec_abs
, ts_nsec_abs
;
1556 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1557 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1559 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1560 is_negative
= false;
1561 ts_sec_abs
= ts_sec
;
1562 ts_nsec_abs
= ts_nsec
;
1563 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1564 is_negative
= false;
1565 ts_sec_abs
= ts_sec
- 1;
1566 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1567 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1569 ts_sec_abs
= ts_sec
;
1570 ts_nsec_abs
= -ts_nsec
;
1571 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1573 ts_sec_abs
= -(ts_sec
+ 1);
1574 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1575 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1577 ts_sec_abs
= -ts_sec
;
1578 ts_nsec_abs
= ts_nsec
;
1579 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1581 ts_sec_abs
= -ts_sec
;
1582 ts_nsec_abs
= -ts_nsec
;
1585 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1586 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1594 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1595 struct cmd_run_ctx
*ctx
,
1596 struct bt_component
*upstream_comp
,
1597 struct bt_port_output
*out_upstream_port
,
1598 struct bt_config_connection
*cfg_conn
)
1600 typedef uint64_t (*input_port_count_func_t
)(void *);
1601 typedef struct bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1603 struct bt_port
*upstream_port
=
1604 bt_port_output_borrow_port(out_upstream_port
);
1607 GQuark downstreamp_comp_name_quark
;
1608 void *downstream_comp
;
1609 uint64_t downstream_port_count
;
1611 input_port_count_func_t port_count_fn
;
1612 borrow_input_port_by_index_func_t port_by_index_fn
;
1613 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1614 bool insert_trimmer
= false;
1615 struct bt_private_value
*trimmer_params
= NULL
;
1616 char *intersection_begin
= NULL
;
1617 char *intersection_end
= NULL
;
1618 struct bt_component_filter
*trimmer
= NULL
;
1619 struct bt_component_class_filter
*trimmer_class
= NULL
;
1620 struct bt_port_input
*trimmer_input
= NULL
;
1621 struct bt_port_output
*trimmer_output
= NULL
;
1623 if (ctx
->intersections
&&
1624 bt_component_get_class_type(upstream_comp
) ==
1625 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1626 struct trace_range
*range
;
1627 struct port_id port_id
= {
1628 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1629 .port_name
= (char *) bt_port_get_name(upstream_port
)
1632 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1636 range
= (struct trace_range
*) g_hash_table_lookup(
1637 ctx
->intersections
, &port_id
);
1639 enum bt_value_status status
;
1641 intersection_begin
= s_from_ns(
1642 range
->intersection_range_begin_ns
);
1643 intersection_end
= s_from_ns(
1644 range
->intersection_range_end_ns
);
1645 if (!intersection_begin
|| !intersection_end
) {
1646 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1650 insert_trimmer
= true;
1651 trimmer_params
= bt_private_value_map_create();
1652 if (!trimmer_params
) {
1656 status
= bt_private_value_map_insert_string_entry(
1657 trimmer_params
, "begin", intersection_begin
);
1658 if (status
!= BT_VALUE_STATUS_OK
) {
1661 status
= bt_private_value_map_insert_string_entry(
1663 "end", intersection_end
);
1664 if (status
!= BT_VALUE_STATUS_OK
) {
1669 trimmer_class
= find_filter_component_class("utils", "trimmer");
1670 if (!trimmer_class
) {
1675 BT_LOGI("Connecting upstream port to the next available downstream port: "
1676 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1677 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1678 upstream_port
, bt_port_get_name(upstream_port
),
1679 cfg_conn
->downstream_comp_name
->str
,
1680 cfg_conn
->arg
->str
);
1681 downstreamp_comp_name_quark
= g_quark_from_string(
1682 cfg_conn
->downstream_comp_name
->str
);
1683 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1684 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1685 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1686 port_count_fn
= (input_port_count_func_t
)
1687 bt_component_filter_get_input_port_count
;
1688 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1689 bt_component_filter_borrow_input_port_by_index
;
1691 if (!downstream_comp
) {
1692 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1693 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1694 port_count_fn
= (input_port_count_func_t
)
1695 bt_component_sink_get_input_port_count
;
1696 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1697 bt_component_sink_borrow_input_port_by_index
;
1700 if (!downstream_comp
) {
1701 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1702 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1703 cfg_conn
->arg
->str
);
1704 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1705 cfg_conn
->arg
->str
);
1709 downstream_port_count
= port_count_fn(downstream_comp
);
1710 BT_ASSERT(downstream_port_count
>= 0);
1712 for (i
= 0; i
< downstream_port_count
; i
++) {
1713 struct bt_port_input
*in_downstream_port
=
1714 port_by_index_fn(downstream_comp
, i
);
1715 struct bt_port
*downstream_port
=
1716 bt_port_input_borrow_port(in_downstream_port
);
1717 const char *upstream_port_name
;
1718 const char *downstream_port_name
;
1720 BT_ASSERT(downstream_port
);
1722 /* Skip port if it's already connected. */
1723 if (bt_port_is_connected(downstream_port
)) {
1724 BT_LOGD("Skipping downstream port: already connected: "
1725 "port-addr=%p, port-name=\"%s\"",
1727 bt_port_get_name(downstream_port
));
1731 downstream_port_name
= bt_port_get_name(downstream_port
);
1732 BT_ASSERT(downstream_port_name
);
1733 upstream_port_name
= bt_port_get_name(upstream_port
);
1734 BT_ASSERT(upstream_port_name
);
1736 if (!bt_common_star_glob_match(
1737 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1738 downstream_port_name
, SIZE_MAX
)) {
1742 if (insert_trimmer
) {
1744 * In order to insert the trimmer between the
1745 * two components that were being connected, we
1746 * create a connection configuration entry which
1747 * describes a connection from the trimmer's
1748 * output to the original input that was being
1751 * Hence, the creation of the trimmer will cause
1752 * the graph "new port" listener to establish
1753 * all downstream connections as its output port
1754 * is connected. We will then establish the
1755 * connection between the original upstream
1756 * source and the trimmer.
1758 char *trimmer_name
= NULL
;
1759 enum bt_graph_status graph_status
;
1761 ret
= asprintf(&trimmer_name
,
1762 "stream-intersection-trimmer-%s",
1763 upstream_port_name
);
1769 ctx
->connect_ports
= false;
1770 graph_status
= bt_private_graph_add_filter_component(
1771 ctx
->graph
, trimmer_class
, trimmer_name
,
1772 bt_private_value_borrow_value(trimmer_params
),
1775 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1781 bt_component_filter_borrow_input_port_by_index(
1783 if (!trimmer_input
) {
1787 bt_component_filter_borrow_output_port_by_index(
1789 if (!trimmer_output
) {
1794 * Replace the current downstream port by the trimmer's
1797 in_downstream_port
= trimmer_input
;
1799 bt_port_input_borrow_port(in_downstream_port
);
1800 downstream_port_name
= bt_port_get_name(
1802 BT_ASSERT(downstream_port_name
);
1805 /* We have a winner! */
1806 status
= bt_private_graph_connect_ports(ctx
->graph
,
1807 out_upstream_port
, in_downstream_port
, NULL
);
1808 downstream_port
= NULL
;
1810 case BT_GRAPH_STATUS_OK
:
1812 case BT_GRAPH_STATUS_CANCELED
:
1813 BT_LOGI_STR("Graph was canceled by user.");
1814 status
= BT_GRAPH_STATUS_OK
;
1816 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1817 BT_LOGE("A component refused a connection to one of its ports: "
1818 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1819 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1820 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1821 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1823 upstream_comp
, bt_component_get_name(upstream_comp
),
1824 upstream_port
, bt_port_get_name(upstream_port
),
1825 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1826 downstream_port
, downstream_port_name
,
1827 cfg_conn
->arg
->str
);
1829 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1830 bt_port_get_name(upstream_port
),
1831 downstream_port_name
,
1832 cfg_conn
->arg
->str
);
1835 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1836 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1837 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1838 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1839 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1841 upstream_comp
, bt_component_get_name(upstream_comp
),
1842 upstream_port
, bt_port_get_name(upstream_port
),
1843 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1844 downstream_port
, downstream_port_name
,
1845 cfg_conn
->arg
->str
);
1847 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1848 bt_port_get_name(upstream_port
),
1849 downstream_port_name
,
1850 cfg_conn
->arg
->str
);
1854 BT_LOGI("Connected component ports: "
1855 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1856 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1857 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1858 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1860 upstream_comp
, bt_component_get_name(upstream_comp
),
1861 upstream_port
, bt_port_get_name(upstream_port
),
1862 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1863 downstream_port
, downstream_port_name
,
1864 cfg_conn
->arg
->str
);
1866 if (insert_trimmer
) {
1868 * The first connection, from the source to the trimmer,
1869 * has been done. We now connect the trimmer to the
1870 * original downstream port.
1872 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1874 bt_component_filter_borrow_component(trimmer
),
1875 trimmer_output
, cfg_conn
);
1879 ctx
->connect_ports
= true;
1883 * We found a matching downstream port: the search is
1889 /* No downstream port found */
1890 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1891 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1892 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1893 upstream_port
, bt_port_get_name(upstream_port
),
1894 cfg_conn
->downstream_comp_name
->str
,
1895 cfg_conn
->arg
->str
);
1897 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1898 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1904 free(intersection_begin
);
1905 free(intersection_end
);
1906 BT_OBJECT_PUT_REF_AND_RESET(trimmer_params
);
1907 BT_OBJECT_PUT_REF_AND_RESET(trimmer_class
);
1908 BT_OBJECT_PUT_REF_AND_RESET(trimmer
);
1913 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1914 struct bt_port_output
*upstream_port
)
1917 const char *upstream_port_name
;
1918 const char *upstream_comp_name
;
1919 struct bt_component
*upstream_comp
= NULL
;
1923 BT_ASSERT(upstream_port
);
1924 upstream_port_name
= bt_port_get_name(
1925 bt_port_output_borrow_port(upstream_port
));
1926 BT_ASSERT(upstream_port_name
);
1927 upstream_comp
= bt_port_borrow_component(
1928 bt_port_output_borrow_port(upstream_port
));
1929 if (!upstream_comp
) {
1930 BT_LOGW("Upstream port to connect is not part of a component: "
1931 "port-addr=%p, port-name=\"%s\"",
1932 upstream_port
, upstream_port_name
);
1937 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1938 BT_ASSERT(upstream_comp_name
);
1939 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1940 "port-addr=%p, port-name=\"%s\"",
1941 upstream_comp
, upstream_comp_name
,
1942 upstream_port
, upstream_port_name
);
1944 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1945 struct bt_config_connection
*cfg_conn
=
1947 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1949 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1950 upstream_comp_name
)) {
1954 if (!bt_common_star_glob_match(
1955 cfg_conn
->upstream_port_glob
->str
,
1956 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1960 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1961 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1963 BT_LOGE("Cannot connect upstream port: "
1964 "port-addr=%p, port-name=\"%s\"",
1966 upstream_port_name
);
1968 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1971 cfg_conn
->arg
->str
);
1977 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1978 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1979 upstream_port_name
);
1981 "Cannot create connection: upstream port `%s` does not match any connection\n",
1982 upstream_port_name
);
1992 void graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1993 struct bt_port_output
*out_port
)
1995 struct bt_component
*comp
;
1996 struct bt_port
*port
= bt_port_output_borrow_port(out_port
);
1998 comp
= bt_port_borrow_component(port
);
1999 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
2000 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
2001 comp
, comp
? bt_component_get_name(comp
) : "",
2002 port
, bt_port_get_name(port
));
2004 if (!ctx
->connect_ports
) {
2009 BT_LOGW_STR("Port has no component.");
2013 if (bt_port_is_connected(port
)) {
2014 BT_LOGW_STR("Port is already connected.");
2018 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
2019 BT_LOGF_STR("Cannot connect upstream port.");
2020 fprintf(stderr
, "Added port could not be connected: aborting\n");
2029 void graph_source_output_port_added_listener(
2030 struct bt_component_source
*component
,
2031 struct bt_port_output
*port
, void *data
)
2033 graph_output_port_added_listener(data
, port
);
2037 void graph_filter_output_port_added_listener(
2038 struct bt_component_filter
*component
,
2039 struct bt_port_output
*port
, void *data
)
2041 graph_output_port_added_listener(data
, port
);
2045 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
2051 if (ctx
->src_components
) {
2052 g_hash_table_destroy(ctx
->src_components
);
2053 ctx
->src_components
= NULL
;
2056 if (ctx
->flt_components
) {
2057 g_hash_table_destroy(ctx
->flt_components
);
2058 ctx
->flt_components
= NULL
;
2061 if (ctx
->sink_components
) {
2062 g_hash_table_destroy(ctx
->sink_components
);
2063 ctx
->sink_components
= NULL
;
2066 if (ctx
->intersections
) {
2067 g_hash_table_destroy(ctx
->intersections
);
2068 ctx
->intersections
= NULL
;
2071 BT_OBJECT_PUT_REF_AND_RESET(ctx
->graph
);
2077 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2080 enum bt_graph_status status
;
2083 ctx
->connect_ports
= false;
2084 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
2085 g_direct_equal
, NULL
, bt_object_put_ref
);
2086 if (!ctx
->src_components
) {
2090 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
2091 g_direct_equal
, NULL
, bt_object_put_ref
);
2092 if (!ctx
->flt_components
) {
2096 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
2097 g_direct_equal
, NULL
, bt_object_put_ref
);
2098 if (!ctx
->sink_components
) {
2102 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2103 ctx
->stream_intersection_mode
= true;
2104 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2105 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2106 if (!ctx
->intersections
) {
2111 ctx
->graph
= bt_private_graph_create();
2116 the_graph
= ctx
->graph
;
2117 status
= bt_private_graph_add_source_component_output_port_added_listener(
2118 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
2120 if (status
!= BT_GRAPH_STATUS_OK
) {
2121 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2125 status
= bt_private_graph_add_filter_component_output_port_added_listener(
2126 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
2128 if (status
!= BT_GRAPH_STATUS_OK
) {
2129 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2136 cmd_run_ctx_destroy(ctx
);
2144 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2145 struct bt_config_component
*cfg_comp
,
2146 struct bt_component_class_source
*src_comp_cls
)
2150 int64_t trace_count
;
2151 enum bt_value_status value_status
;
2152 const char *path
= NULL
;
2153 struct bt_value
*component_path_value
= NULL
;
2154 struct bt_private_value
*query_params
= NULL
;
2155 struct bt_value
*query_result
= NULL
;
2156 struct bt_value
*trace_info
= NULL
;
2157 struct bt_value
*intersection_range
= NULL
;
2158 struct bt_value
*intersection_begin
= NULL
;
2159 struct bt_value
*intersection_end
= NULL
;
2160 struct bt_value
*stream_path_value
= NULL
;
2161 struct bt_value
*stream_paths
= NULL
;
2162 struct bt_value
*stream_infos
= NULL
;
2163 struct bt_value
*stream_info
= NULL
;
2164 struct port_id
*port_id
= NULL
;
2165 struct trace_range
*trace_range
= NULL
;
2166 const char *fail_reason
= NULL
;
2167 struct bt_component_class
*comp_cls
=
2168 bt_component_class_source_borrow_component_class(src_comp_cls
);
2170 component_path_value
= bt_value_map_borrow_entry_value(
2171 bt_private_value_borrow_value(cfg_comp
->params
),
2173 if (component_path_value
&& !bt_value_is_string(component_path_value
)) {
2174 BT_LOGD("Cannot get path parameter: component-name=%s",
2175 cfg_comp
->instance_name
->str
);
2180 path
= bt_value_string_get(component_path_value
);
2181 query_params
= bt_private_value_map_create();
2182 if (!query_params
) {
2183 BT_LOGE_STR("Cannot create query parameters.");
2188 value_status
= bt_private_value_map_insert_entry(query_params
, "path",
2189 component_path_value
);
2190 if (value_status
!= BT_VALUE_STATUS_OK
) {
2191 BT_LOGE_STR("Cannot insert path parameter in query parameter map.");
2196 ret
= query(comp_cls
, "trace-info",
2197 bt_private_value_borrow_value(query_params
), &query_result
,
2200 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2201 "comp-class-name=\"%s\"", fail_reason
,
2202 bt_component_class_get_name(comp_cls
));
2207 BT_ASSERT(query_result
);
2209 if (!bt_value_is_array(query_result
)) {
2210 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2211 "component-class-name=%s",
2212 bt_component_class_get_name(comp_cls
));
2217 trace_count
= bt_value_array_get_size(query_result
);
2218 if (trace_count
< 0) {
2223 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2225 uint64_t stream_idx
;
2226 int64_t stream_count
;
2228 trace_info
= bt_value_array_borrow_element_by_index(
2229 query_result
, trace_idx
);
2230 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2232 BT_LOGD_STR("Cannot retrieve trace from query result.");
2236 intersection_range
= bt_value_map_borrow_entry_value(trace_info
,
2237 "intersection-range-ns");
2238 if (!intersection_range
) {
2240 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2244 intersection_begin
= bt_value_map_borrow_entry_value(
2245 intersection_range
, "begin");
2246 if (!intersection_begin
) {
2248 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2252 intersection_end
= bt_value_map_borrow_entry_value(
2253 intersection_range
, "end");
2254 if (!intersection_end
) {
2256 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2260 begin
= bt_value_integer_get(intersection_begin
);
2261 end
= bt_value_integer_get(intersection_end
);
2263 if (begin
< 0 || end
< 0 || end
< begin
) {
2264 BT_LOGW("Invalid trace stream intersection values: "
2265 "intersection-range-ns:begin=%" PRId64
2266 ", intersection-range-ns:end=%" PRId64
,
2272 stream_infos
= bt_value_map_borrow_entry_value(trace_info
,
2274 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2276 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2280 stream_count
= bt_value_array_get_size(stream_infos
);
2281 if (stream_count
< 0) {
2289 * The first path of a stream's "paths" is currently used to
2290 * associate streams/ports to a given trace intersection.
2292 * This is a fragile hack as it relies on the port names
2293 * being set to the various streams path.
2295 * A stream name should be introduced as part of the trace-info
2298 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2299 const char *stream_path
;
2301 port_id
= g_new0(struct port_id
, 1);
2304 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2307 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2308 if (!port_id
->instance_name
) {
2310 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2314 trace_range
= g_new0(struct trace_range
, 1);
2317 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2320 trace_range
->intersection_range_begin_ns
= begin
;
2321 trace_range
->intersection_range_end_ns
= end
;
2323 stream_info
= bt_value_array_borrow_element_by_index(
2324 stream_infos
, stream_idx
);
2325 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2327 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2331 stream_paths
= bt_value_map_borrow_entry_value(
2332 stream_info
, "paths");
2333 if (!stream_paths
|| !bt_value_is_array(stream_paths
)) {
2335 BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
2340 bt_value_array_borrow_element_by_index(
2342 if (!stream_path_value
||
2343 !bt_value_is_string(stream_path_value
)) {
2345 BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
2349 stream_path
= bt_value_string_get(stream_path_value
);
2350 port_id
->port_name
= strdup(stream_path
);
2351 if (!port_id
->port_name
) {
2353 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2357 BT_LOGD("Inserting stream intersection ");
2359 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2369 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2370 bt_common_color_bold(),
2371 bt_common_color_fg_yellow(),
2372 path
? path
: "(unknown)",
2373 bt_common_color_reset());
2375 bt_object_put_ref(query_params
);
2376 bt_object_put_ref(query_result
);
2378 g_free(trace_range
);
2383 int cmd_run_ctx_create_components_from_config_components(
2384 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2387 void *comp_cls
= NULL
;
2391 for (i
= 0; i
< cfg_components
->len
; i
++) {
2392 struct bt_config_component
*cfg_comp
=
2393 g_ptr_array_index(cfg_components
, i
);
2396 switch (cfg_comp
->type
) {
2397 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2398 comp_cls
= find_source_component_class(
2399 cfg_comp
->plugin_name
->str
,
2400 cfg_comp
->comp_cls_name
->str
);
2402 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2403 comp_cls
= find_filter_component_class(
2404 cfg_comp
->plugin_name
->str
,
2405 cfg_comp
->comp_cls_name
->str
);
2407 case BT_COMPONENT_CLASS_TYPE_SINK
:
2408 comp_cls
= find_sink_component_class(
2409 cfg_comp
->plugin_name
->str
,
2410 cfg_comp
->comp_cls_name
->str
);
2417 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2418 "comp-cls-name=\"%s\", comp-cls-type=%d",
2419 cfg_comp
->plugin_name
->str
,
2420 cfg_comp
->comp_cls_name
->str
,
2422 fprintf(stderr
, "%s%sCannot find component class %s",
2423 bt_common_color_bold(),
2424 bt_common_color_fg_red(),
2425 bt_common_color_reset());
2426 print_plugin_comp_cls_opt(stderr
,
2427 cfg_comp
->plugin_name
->str
,
2428 cfg_comp
->comp_cls_name
->str
,
2430 fprintf(stderr
, "\n");
2434 switch (cfg_comp
->type
) {
2435 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2436 ret
= bt_private_graph_add_source_component(ctx
->graph
,
2437 comp_cls
, cfg_comp
->instance_name
->str
,
2438 bt_private_value_borrow_value(cfg_comp
->params
),
2441 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2442 ret
= bt_private_graph_add_filter_component(ctx
->graph
,
2443 comp_cls
, cfg_comp
->instance_name
->str
,
2444 bt_private_value_borrow_value(cfg_comp
->params
),
2447 case BT_COMPONENT_CLASS_TYPE_SINK
:
2448 ret
= bt_private_graph_add_sink_component(ctx
->graph
,
2449 comp_cls
, cfg_comp
->instance_name
->str
,
2450 bt_private_value_borrow_value(cfg_comp
->params
),
2458 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2459 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2461 cfg_comp
->plugin_name
->str
,
2462 cfg_comp
->comp_cls_name
->str
,
2463 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2464 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2465 bt_common_color_bold(),
2466 bt_common_color_fg_red(),
2467 cfg_comp
->instance_name
->str
,
2468 bt_common_color_reset());
2472 if (ctx
->stream_intersection_mode
&&
2473 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2474 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2480 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2481 comp
, cfg_comp
->instance_name
->str
);
2482 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2483 BT_ASSERT(quark
> 0);
2485 switch (cfg_comp
->type
) {
2486 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2487 g_hash_table_insert(ctx
->src_components
,
2488 GUINT_TO_POINTER(quark
), comp
);
2490 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2491 g_hash_table_insert(ctx
->flt_components
,
2492 GUINT_TO_POINTER(quark
), comp
);
2494 case BT_COMPONENT_CLASS_TYPE_SINK
:
2495 g_hash_table_insert(ctx
->sink_components
,
2496 GUINT_TO_POINTER(quark
), comp
);
2503 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2512 bt_object_put_ref(comp
);
2513 bt_object_put_ref(comp_cls
);
2518 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2523 * Make sure that, during this phase, our graph's "port added"
2524 * listener does not connect ports while we are creating the
2525 * components because we have a special, initial phase for
2528 ctx
->connect_ports
= false;
2530 ret
= cmd_run_ctx_create_components_from_config_components(
2531 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2537 ret
= cmd_run_ctx_create_components_from_config_components(
2538 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2544 ret
= cmd_run_ctx_create_components_from_config_components(
2545 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2555 typedef uint64_t (*output_port_count_func_t
)(void *);
2556 typedef struct bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2560 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2561 void *comp
, output_port_count_func_t port_count_fn
,
2562 borrow_output_port_by_index_func_t port_by_index_fn
)
2568 count
= port_count_fn(comp
);
2569 BT_ASSERT(count
>= 0);
2571 for (i
= 0; i
< count
; i
++) {
2572 struct bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2574 BT_ASSERT(upstream_port
);
2575 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2586 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2589 GHashTableIter iter
;
2590 gpointer g_name_quark
, g_comp
;
2592 ctx
->connect_ports
= true;
2593 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2595 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2596 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2597 (output_port_count_func_t
)
2598 bt_component_source_get_output_port_count
,
2599 (borrow_output_port_by_index_func_t
)
2600 bt_component_source_borrow_output_port_by_index
);
2606 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2608 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2609 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2610 (output_port_count_func_t
)
2611 bt_component_filter_get_output_port_count
,
2612 (borrow_output_port_by_index_func_t
)
2613 bt_component_filter_borrow_output_port_by_index
);
2624 const char *bt_graph_status_str(enum bt_graph_status status
)
2627 case BT_GRAPH_STATUS_OK
:
2628 return "BT_GRAPH_STATUS_OK";
2629 case BT_GRAPH_STATUS_END
:
2630 return "BT_GRAPH_STATUS_END";
2631 case BT_GRAPH_STATUS_AGAIN
:
2632 return "BT_GRAPH_STATUS_AGAIN";
2633 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
2634 return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION";
2635 case BT_GRAPH_STATUS_CANCELED
:
2636 return "BT_GRAPH_STATUS_CANCELED";
2637 case BT_GRAPH_STATUS_ERROR
:
2638 return "BT_GRAPH_STATUS_ERROR";
2639 case BT_GRAPH_STATUS_NO_SINK
:
2640 return "BT_GRAPH_STATUS_NO_SINK";
2641 case BT_GRAPH_STATUS_NOMEM
:
2642 return "BT_GRAPH_STATUS_NOMEM";
2649 int cmd_run(struct bt_config
*cfg
)
2652 struct cmd_run_ctx ctx
= { 0 };
2654 /* Initialize the command's context and the graph object */
2655 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2656 BT_LOGE_STR("Cannot initialize the command's context.");
2657 fprintf(stderr
, "Cannot initialize the command's context\n");
2662 BT_LOGI_STR("Canceled by user before creating components.");
2666 BT_LOGI_STR("Creating components.");
2668 /* Create the requested component instances */
2669 if (cmd_run_ctx_create_components(&ctx
)) {
2670 BT_LOGE_STR("Cannot create components.");
2671 fprintf(stderr
, "Cannot create components\n");
2676 BT_LOGI_STR("Canceled by user before connecting components.");
2680 BT_LOGI_STR("Connecting components.");
2682 /* Connect the initially visible component ports */
2683 if (cmd_run_ctx_connect_ports(&ctx
)) {
2684 BT_LOGE_STR("Cannot connect initial component ports.");
2685 fprintf(stderr
, "Cannot connect initial component ports\n");
2690 BT_LOGI_STR("Canceled by user before running the graph.");
2694 BT_LOGI_STR("Running the graph.");
2698 enum bt_graph_status graph_status
= bt_private_graph_run(ctx
.graph
);
2701 * Reset console in case something messed with console
2702 * codes during the graph's execution.
2704 printf("%s", bt_common_color_reset());
2706 fprintf(stderr
, "%s", bt_common_color_reset());
2707 BT_LOGV("bt_private_graph_run() returned: status=%s",
2708 bt_graph_status_str(graph_status
));
2710 switch (graph_status
) {
2711 case BT_GRAPH_STATUS_OK
:
2713 case BT_GRAPH_STATUS_CANCELED
:
2714 BT_LOGI_STR("Graph was canceled by user.");
2716 case BT_GRAPH_STATUS_AGAIN
:
2717 if (bt_graph_is_canceled(
2718 bt_private_graph_borrow_graph(ctx
.graph
))) {
2719 BT_LOGI_STR("Graph was canceled by user.");
2723 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2724 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2726 cfg
->cmd_data
.run
.retry_duration_us
);
2728 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2729 if (bt_graph_is_canceled(
2730 bt_private_graph_borrow_graph(ctx
.graph
))) {
2731 BT_LOGI_STR("Graph was canceled by user.");
2737 case BT_GRAPH_STATUS_END
:
2740 BT_LOGE_STR("Graph failed to complete successfully");
2741 fprintf(stderr
, "Graph failed to complete successfully\n");
2754 cmd_run_ctx_destroy(&ctx
);
2759 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2761 const char *env_clash
;
2763 if (!cfg
->command_name
) {
2767 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2768 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2772 if (g_file_test(cfg
->command_name
,
2773 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2774 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2776 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2778 fprintf(stderr
, "\n");
2779 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
2785 void init_log_level(void)
2787 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2791 void set_auto_log_levels(struct bt_config
*cfg
)
2793 const char **env_var_name
;
2796 * Override the configuration's default log level if
2797 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2798 * are found for backward compatibility with legacy Babetrace 1.
2800 if (getenv("BABELTRACE_DEBUG") &&
2801 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2802 cfg
->log_level
= 'V';
2803 } else if (getenv("BABELTRACE_VERBOSE") &&
2804 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2805 cfg
->log_level
= 'I';
2809 * Set log levels according to --debug or --verbose. For
2810 * backward compatibility, --debug is more verbose than
2813 * --verbose: INFO log level
2814 * --debug: VERBOSE log level (includes DEBUG, which is
2815 * is less verbose than VERBOSE in the internal
2816 * logging framework)
2818 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2820 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2821 } else if (cfg
->debug
) {
2822 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2825 * Set library's default log level if not
2826 * explicitly specified.
2828 switch (cfg
->log_level
) {
2830 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE
);
2833 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2836 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
2839 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2842 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN
);
2845 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR
);
2848 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL
);
2856 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2858 bt_cli_log_level
= BT_LOG_INFO
;
2859 } else if (cfg
->debug
) {
2860 bt_cli_log_level
= BT_LOG_VERBOSE
;
2863 * Set CLI's default log level if not explicitly
2866 switch (cfg
->log_level
) {
2868 bt_cli_log_level
= BT_LOG_NONE
;
2871 bt_cli_log_level
= BT_LOG_VERBOSE
;
2874 bt_cli_log_level
= BT_LOG_DEBUG
;
2877 bt_cli_log_level
= BT_LOG_INFO
;
2880 bt_cli_log_level
= BT_LOG_WARN
;
2883 bt_cli_log_level
= BT_LOG_ERROR
;
2886 bt_cli_log_level
= BT_LOG_FATAL
;
2894 env_var_name
= log_level_env_var_names
;
2896 while (*env_var_name
) {
2897 if (!getenv(*env_var_name
)) {
2899 g_setenv(*env_var_name
, "I", 1);
2900 } else if (cfg
->debug
) {
2901 g_setenv(*env_var_name
, "V", 1);
2903 char val
[2] = { 0 };
2906 * Set module's default log level if not
2907 * explicitly specified.
2909 val
[0] = cfg
->log_level
;
2910 g_setenv(*env_var_name
, val
, 1);
2918 int main(int argc
, const char **argv
)
2922 struct bt_config
*cfg
;
2925 set_signal_handler();
2927 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2930 /* Quit without errors; typically usage/version */
2932 BT_LOGI_STR("Quitting without errors.");
2937 BT_LOGE("Command-line error: retcode=%d", retcode
);
2942 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2943 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2948 set_auto_log_levels(cfg
);
2951 if (cfg
->command_needs_plugins
) {
2952 ret
= load_all_plugins(
2953 bt_private_value_borrow_value(cfg
->plugin_paths
));
2955 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2961 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2962 cfg
->command
, cfg
->command_name
);
2964 switch (cfg
->command
) {
2965 case BT_CONFIG_COMMAND_RUN
:
2968 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2969 ret
= cmd_list_plugins(cfg
);
2971 case BT_CONFIG_COMMAND_HELP
:
2972 ret
= cmd_help(cfg
);
2974 case BT_CONFIG_COMMAND_QUERY
:
2975 ret
= cmd_query(cfg
);
2977 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2978 ret
= cmd_print_ctf_metadata(cfg
);
2980 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2981 ret
= cmd_print_lttng_live_sessions(cfg
);
2984 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2988 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2989 cfg
->command
, cfg
->command_name
, ret
);
2990 warn_command_name_and_directory_clash(cfg
);
2991 retcode
= ret
? 1 : 0;
2994 BT_OBJECT_PUT_REF_AND_RESET(cfg
);