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_CTFSER_LOG_LEVEL",
55 "BABELTRACE_FD_CACHE_LOG_LEVEL",
56 "BABELTRACE_FLT_LTTNG_UTILS_DEBUG_INFO_LOG_LEVEL",
57 "BABELTRACE_FLT_UTILS_COUNTER_LOG_LEVEL",
58 "BABELTRACE_FLT_UTILS_MUXER_LOG_LEVEL",
59 "BABELTRACE_FLT_UTILS_TRIMMER_LOG_LEVEL",
60 "BABELTRACE_PLUGIN_CTF_BFCR_LOG_LEVEL",
61 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
62 "BABELTRACE_PLUGIN_CTF_MSG_ITER_LOG_LEVEL",
63 "BABELTRACE_PLUGIN_CTF_UTILS_LOG_LEVEL",
64 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
65 "BABELTRACE_SINK_CTF_FS_LOG_LEVEL",
66 "BABELTRACE_SINK_TEXT_PRETTY_LOG_LEVEL",
67 "BABELTRACE_SRC_CTF_FS_LOG_LEVEL",
68 "BABELTRACE_SRC_CTF_LTTNG_LIVE_LOG_LEVEL",
69 "BABELTRACE_SRC_TEXT_DMESG_LOG_LEVEL",
73 /* Application's processing graph (weak) */
74 static bt_graph
*the_graph
;
75 static bt_query_executor
*the_query_executor
;
76 static bool canceled
= false;
78 GPtrArray
*loaded_plugins
;
85 BOOL WINAPI
signal_handler(DWORD signal
) {
87 bt_graph_cancel(the_graph
);
96 void set_signal_handler(void)
98 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
99 BT_LOGE("Failed to set the ctrl+c handler.");
103 #else /* __MINGW32__ */
106 void signal_handler(int signum
)
108 if (signum
!= SIGINT
) {
113 bt_graph_cancel(the_graph
);
116 if (the_query_executor
) {
117 bt_query_executor_cancel(the_query_executor
);
124 void set_signal_handler(void)
126 struct sigaction new_action
, old_action
;
128 new_action
.sa_handler
= signal_handler
;
129 sigemptyset(&new_action
.sa_mask
);
130 new_action
.sa_flags
= 0;
131 sigaction(SIGINT
, NULL
, &old_action
);
133 if (old_action
.sa_handler
!= SIG_IGN
) {
134 sigaction(SIGINT
, &new_action
, NULL
);
138 #endif /* __MINGW32__ */
141 void init_static_data(void)
143 loaded_plugins
= g_ptr_array_new_with_free_func(
144 (GDestroyNotify
) bt_object_put_ref
);
148 void fini_static_data(void)
150 g_ptr_array_free(loaded_plugins
, TRUE
);
154 int create_the_query_executor(void)
158 the_query_executor
= bt_query_executor_create();
159 if (!the_query_executor
) {
160 BT_LOGE_STR("Cannot create a query executor.");
168 void destroy_the_query_executor(void)
170 BT_QUERY_EXECUTOR_PUT_REF_AND_RESET(the_query_executor
);
174 int query(const bt_component_class
*comp_cls
, const char *obj
,
175 const bt_value
*params
, const bt_value
**user_result
,
176 const char **fail_reason
)
178 const bt_value
*result
= NULL
;
179 bt_query_executor_status status
;
180 *fail_reason
= "unknown error";
183 BT_ASSERT(fail_reason
);
184 BT_ASSERT(user_result
);
185 ret
= create_the_query_executor();
187 /* create_the_query_executor() logs errors */
192 BT_LOGI("Canceled by user before executing the query: "
193 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
194 "query-obj=\"%s\"", comp_cls
,
195 bt_component_class_get_name(comp_cls
), obj
);
196 *fail_reason
= "canceled by user";
201 status
= bt_query_executor_query(the_query_executor
,
202 comp_cls
, obj
, params
, &result
);
204 case BT_QUERY_EXECUTOR_STATUS_OK
:
206 case BT_QUERY_EXECUTOR_STATUS_AGAIN
:
208 const uint64_t sleep_time_us
= 100000;
210 /* Wait 100 ms and retry */
211 BT_LOGV("Got BT_QUERY_EXECUTOR_STATUS_AGAIN: sleeping: "
212 "time-us=%" PRIu64
, sleep_time_us
);
214 if (usleep(sleep_time_us
)) {
215 if (bt_query_executor_is_canceled(the_query_executor
)) {
216 BT_LOGI("Query was canceled by user: "
217 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
218 "query-obj=\"%s\"", comp_cls
,
219 bt_component_class_get_name(comp_cls
),
221 *fail_reason
= "canceled by user";
228 case BT_QUERY_EXECUTOR_STATUS_CANCELED
:
229 *fail_reason
= "canceled by user";
231 case BT_QUERY_EXECUTOR_STATUS_ERROR
:
233 case BT_QUERY_EXECUTOR_STATUS_INVALID_OBJECT
:
234 *fail_reason
= "invalid or unknown query object";
236 case BT_QUERY_EXECUTOR_STATUS_INVALID_PARAMS
:
237 *fail_reason
= "invalid query parameters";
239 case BT_QUERY_EXECUTOR_STATUS_UNSUPPORTED
:
240 *fail_reason
= "unsupported action";
242 case BT_QUERY_EXECUTOR_STATUS_NOMEM
:
243 *fail_reason
= "not enough memory";
246 BT_LOGF("Unknown query status: status=%d", status
);
252 *user_result
= result
;
260 destroy_the_query_executor();
261 bt_value_put_ref(result
);
266 const bt_plugin
*find_plugin(const char *name
)
269 const bt_plugin
*plugin
= NULL
;
272 BT_LOGD("Finding plugin: name=\"%s\"", name
);
274 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
275 plugin
= g_ptr_array_index(loaded_plugins
, i
);
277 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
284 if (BT_LOG_ON_DEBUG
) {
286 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
288 BT_LOGD("Cannot find plugin.");
292 bt_plugin_get_ref(plugin
);
296 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
297 const bt_plugin
*, const char *);
300 const void *find_component_class_from_plugin(const char *plugin_name
,
301 const char *comp_class_name
,
302 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
304 const void *comp_class
= NULL
;
305 const bt_plugin
*plugin
;
307 BT_LOGD("Finding component class: plugin-name=\"%s\", "
308 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
310 plugin
= find_plugin(plugin_name
);
315 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
316 bt_object_get_ref(comp_class
);
317 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
320 if (BT_LOG_ON_DEBUG
) {
322 BT_LOGD("Found component class: comp-cls-addr=%p",
325 BT_LOGD("Cannot find source component class.");
333 const bt_component_class_source
*find_source_component_class(
334 const char *plugin_name
, const char *comp_class_name
)
336 return (const void *) find_component_class_from_plugin(
337 plugin_name
, comp_class_name
,
338 (plugin_borrow_comp_cls_func_t
)
339 bt_plugin_borrow_source_component_class_by_name_const
);
343 const bt_component_class_filter
*find_filter_component_class(
344 const char *plugin_name
, const char *comp_class_name
)
346 return (const void *) find_component_class_from_plugin(
347 plugin_name
, comp_class_name
,
348 (plugin_borrow_comp_cls_func_t
)
349 bt_plugin_borrow_filter_component_class_by_name_const
);
353 const bt_component_class_sink
*find_sink_component_class(
354 const char *plugin_name
, const char *comp_class_name
)
356 return (const void *) find_component_class_from_plugin(plugin_name
,
358 (plugin_borrow_comp_cls_func_t
)
359 bt_plugin_borrow_sink_component_class_by_name_const
);
363 const bt_component_class
*find_component_class(const char *plugin_name
,
364 const char *comp_class_name
,
365 bt_component_class_type comp_class_type
)
367 const bt_component_class
*comp_cls
= NULL
;
369 switch (comp_class_type
) {
370 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
371 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
373 case BT_COMPONENT_CLASS_TYPE_FILTER
:
374 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
376 case BT_COMPONENT_CLASS_TYPE_SINK
:
377 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
387 void print_indent(FILE *fp
, size_t indent
)
391 for (i
= 0; i
< indent
; i
++) {
397 const char *component_type_str(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
, 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 *, const bt_value
*, size_t);
454 void print_value_rec(FILE *, const bt_value
*, size_t);
456 struct print_map_value_data
{
462 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
465 struct print_map_value_data
*print_map_value_data
= data
;
467 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
468 fprintf(print_map_value_data
->fp
, "%s: ", key
);
471 if (bt_value_is_array(object
) &&
472 bt_value_array_is_empty(object
)) {
473 fprintf(print_map_value_data
->fp
, "[ ]\n");
477 if (bt_value_is_map(object
) &&
478 bt_value_map_is_empty(object
)) {
479 fprintf(print_map_value_data
->fp
, "{ }\n");
483 if (bt_value_is_array(object
) ||
484 bt_value_is_map(object
)) {
485 fprintf(print_map_value_data
->fp
, "\n");
488 print_value_rec(print_map_value_data
->fp
, object
,
489 print_map_value_data
->indent
+ 2);
494 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
508 switch (bt_value_get_type(value
)) {
509 case BT_VALUE_TYPE_NULL
:
510 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
511 bt_common_color_reset());
513 case BT_VALUE_TYPE_BOOL
:
514 bool_val
= bt_value_bool_get(value
);
515 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
516 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
517 bt_common_color_reset());
519 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
520 uint_val
= bt_value_unsigned_integer_get(value
);
521 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
522 bt_common_color_fg_red(), uint_val
,
523 bt_common_color_reset());
525 case BT_VALUE_TYPE_SIGNED_INTEGER
:
526 int_val
= bt_value_signed_integer_get(value
);
527 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
528 bt_common_color_fg_red(), int_val
,
529 bt_common_color_reset());
531 case BT_VALUE_TYPE_REAL
:
532 dbl_val
= bt_value_real_get(value
);
533 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
534 bt_common_color_fg_red(), dbl_val
,
535 bt_common_color_reset());
537 case BT_VALUE_TYPE_STRING
:
538 str_val
= bt_value_string_get(value
);
539 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
540 bt_common_color_fg_green(), str_val
,
541 bt_common_color_reset());
543 case BT_VALUE_TYPE_ARRAY
:
544 size
= bt_value_array_get_size(value
);
550 print_indent(fp
, indent
);
551 fprintf(fp
, "[ ]\n");
555 for (i
= 0; i
< size
; i
++) {
556 const bt_value
*element
=
557 bt_value_array_borrow_element_by_index_const(
563 print_indent(fp
, indent
);
566 if (bt_value_is_array(element
) &&
567 bt_value_array_is_empty(element
)) {
568 fprintf(fp
, "[ ]\n");
572 if (bt_value_is_map(element
) &&
573 bt_value_map_is_empty(element
)) {
574 fprintf(fp
, "{ }\n");
578 if (bt_value_is_array(element
) ||
579 bt_value_is_map(element
)) {
583 print_value_rec(fp
, element
, indent
+ 2);
586 case BT_VALUE_TYPE_MAP
:
588 struct print_map_value_data data
= {
593 if (bt_value_map_is_empty(value
)) {
594 print_indent(fp
, indent
);
595 fprintf(fp
, "{ }\n");
599 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
608 BT_LOGE("Error printing value of type %s.",
609 bt_common_value_type_string(bt_value_get_type(value
)));
613 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
615 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
616 print_indent(fp
, indent
);
619 print_value_rec(fp
, value
, indent
);
623 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
625 fprintf(stderr
, " ");
626 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
627 bt_config_component
->comp_cls_name
->str
,
628 bt_config_component
->type
);
629 fprintf(stderr
, ":\n");
631 if (bt_config_component
->instance_name
->len
> 0) {
632 fprintf(stderr
, " Name: %s\n",
633 bt_config_component
->instance_name
->str
);
636 fprintf(stderr
, " Parameters:\n");
637 print_value(stderr
, bt_config_component
->params
, 8);
641 void print_bt_config_components(GPtrArray
*array
)
645 for (i
= 0; i
< array
->len
; i
++) {
646 struct bt_config_component
*cfg_component
=
647 bt_config_get_component(array
, i
);
648 print_bt_config_component(cfg_component
);
649 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
654 void print_plugin_paths(const bt_value
*plugin_paths
)
656 fprintf(stderr
, " Plugin paths:\n");
657 print_value(stderr
, plugin_paths
, 4);
661 void print_cfg_run(struct bt_config
*cfg
)
665 print_plugin_paths(cfg
->plugin_paths
);
666 fprintf(stderr
, " Source component instances:\n");
667 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
669 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
670 fprintf(stderr
, " Filter component instances:\n");
671 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
674 fprintf(stderr
, " Sink component instances:\n");
675 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
676 fprintf(stderr
, " Connections:\n");
678 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
679 struct bt_config_connection
*cfg_connection
=
680 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
683 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
684 cfg_connection
->upstream_comp_name
->str
,
685 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
686 cfg_connection
->upstream_port_glob
->str
,
687 cfg_connection
->downstream_comp_name
->str
,
688 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
689 cfg_connection
->downstream_port_glob
->str
);
694 void print_cfg_list_plugins(struct bt_config
*cfg
)
696 print_plugin_paths(cfg
->plugin_paths
);
700 void print_cfg_help(struct bt_config
*cfg
)
702 print_plugin_paths(cfg
->plugin_paths
);
706 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
708 print_plugin_paths(cfg
->plugin_paths
);
709 fprintf(stderr
, " Path: %s\n",
710 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
714 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
716 print_plugin_paths(cfg
->plugin_paths
);
717 fprintf(stderr
, " URL: %s\n",
718 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
722 void print_cfg_query(struct bt_config
*cfg
)
724 print_plugin_paths(cfg
->plugin_paths
);
725 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
726 fprintf(stderr
, " Component class:\n");
727 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
731 void print_cfg(struct bt_config
*cfg
)
733 if (!BT_LOG_ON_INFO
) {
737 BT_LOGI_STR("Configuration:");
738 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
739 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
741 switch (cfg
->command
) {
742 case BT_CONFIG_COMMAND_RUN
:
745 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
746 print_cfg_list_plugins(cfg
);
748 case BT_CONFIG_COMMAND_HELP
:
751 case BT_CONFIG_COMMAND_QUERY
:
752 print_cfg_query(cfg
);
754 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
755 print_cfg_print_ctf_metadata(cfg
);
757 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
758 print_cfg_print_lttng_live_sessions(cfg
);
766 void add_to_loaded_plugins(const bt_plugin_set
*plugin_set
)
771 count
= bt_plugin_set_get_plugin_count(plugin_set
);
772 BT_ASSERT(count
>= 0);
774 for (i
= 0; i
< count
; i
++) {
775 const bt_plugin
*plugin
=
776 bt_plugin_set_borrow_plugin_by_index_const(plugin_set
, i
);
777 const bt_plugin
*loaded_plugin
=
778 find_plugin(bt_plugin_get_name(plugin
));
783 BT_LOGI("Not using plugin: another one already exists with the same name: "
784 "plugin-name=\"%s\", plugin-path=\"%s\", "
785 "existing-plugin-path=\"%s\"",
786 bt_plugin_get_name(plugin
),
787 bt_plugin_get_path(plugin
),
788 bt_plugin_get_path(loaded_plugin
));
789 bt_plugin_put_ref(loaded_plugin
);
791 /* Add to global array. */
792 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
793 bt_plugin_get_name(plugin
));
794 bt_plugin_get_ref(plugin
);
795 g_ptr_array_add(loaded_plugins
, (void *) plugin
);
801 int load_dynamic_plugins(const bt_value
*plugin_paths
)
803 int nr_paths
, i
, ret
= 0;
805 nr_paths
= bt_value_array_get_size(plugin_paths
);
807 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
812 BT_LOGI("Loading dynamic plugins.");
814 for (i
= 0; i
< nr_paths
; i
++) {
815 const bt_value
*plugin_path_value
= NULL
;
816 const char *plugin_path
;
817 const bt_plugin_set
*plugin_set
;
820 bt_value_array_borrow_element_by_index_const(
822 plugin_path
= bt_value_string_get(plugin_path_value
);
825 * Skip this if the directory does not exist because
826 * bt_plugin_find_all_from_dir() expects an existing
829 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
830 BT_LOGV("Skipping nonexistent directory path: "
831 "path=\"%s\"", plugin_path
);
835 plugin_set
= bt_plugin_find_all_from_dir(plugin_path
, false);
837 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
842 add_to_loaded_plugins(plugin_set
);
843 bt_plugin_set_put_ref(plugin_set
);
850 int load_static_plugins(void)
853 const bt_plugin_set
*plugin_set
;
855 BT_LOGI("Loading static plugins.");
856 plugin_set
= bt_plugin_find_all_from_static();
858 BT_LOGE("Unable to load static plugins.");
863 add_to_loaded_plugins(plugin_set
);
864 bt_plugin_set_put_ref(plugin_set
);
870 int load_all_plugins(const bt_value
*plugin_paths
)
874 if (load_dynamic_plugins(plugin_paths
)) {
879 if (load_static_plugins()) {
884 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
891 void print_plugin_info(const bt_plugin
*plugin
)
893 unsigned int major
, minor
, patch
;
895 bt_property_availability version_avail
;
896 const char *plugin_name
;
900 const char *plugin_description
;
902 plugin_name
= bt_plugin_get_name(plugin
);
903 path
= bt_plugin_get_path(plugin
);
904 author
= bt_plugin_get_author(plugin
);
905 license
= bt_plugin_get_license(plugin
);
906 plugin_description
= bt_plugin_get_description(plugin
);
907 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
909 printf("%s%s%s%s:\n", bt_common_color_bold(),
910 bt_common_color_fg_blue(), plugin_name
,
911 bt_common_color_reset());
913 printf(" %sPath%s: %s\n", bt_common_color_bold(),
914 bt_common_color_reset(), path
);
919 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
920 printf(" %sVersion%s: %u.%u.%u",
921 bt_common_color_bold(), bt_common_color_reset(),
922 major
, minor
, patch
);
931 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
932 bt_common_color_reset(),
933 plugin_description
? plugin_description
: "(None)");
934 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
935 bt_common_color_reset(), author
? author
: "(Unknown)");
936 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
937 bt_common_color_reset(),
938 license
? license
: "(Unknown)");
942 int cmd_query(struct bt_config
*cfg
)
945 const bt_component_class
*comp_cls
= NULL
;
946 const bt_value
*results
= NULL
;
947 const char *fail_reason
= NULL
;
949 comp_cls
= find_component_class(
950 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
951 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
952 cfg
->cmd_data
.query
.cfg_component
->type
);
954 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
955 "comp-cls-name=\"%s\", comp-cls-type=%d",
956 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
957 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
958 cfg
->cmd_data
.query
.cfg_component
->type
);
959 fprintf(stderr
, "%s%sCannot find component class %s",
960 bt_common_color_bold(),
961 bt_common_color_fg_red(),
962 bt_common_color_reset());
963 print_plugin_comp_cls_opt(stderr
,
964 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
965 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
966 cfg
->cmd_data
.query
.cfg_component
->type
);
967 fprintf(stderr
, "\n");
972 ret
= query(comp_cls
, cfg
->cmd_data
.query
.object
->str
,
973 cfg
->cmd_data
.query
.cfg_component
->params
,
974 &results
, &fail_reason
);
979 print_value(stdout
, results
, 0);
983 BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", "
984 "comp-cls-name=\"%s\", comp-cls-type=%d "
985 "object=\"%s\"", fail_reason
,
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 cfg
->cmd_data
.query
.object
->str
);
990 fprintf(stderr
, "%s%sFailed to query info to %s",
991 bt_common_color_bold(),
992 bt_common_color_fg_red(),
993 bt_common_color_reset());
994 print_plugin_comp_cls_opt(stderr
,
995 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
996 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
997 cfg
->cmd_data
.query
.cfg_component
->type
);
998 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
999 bt_common_color_bold(),
1000 bt_common_color_fg_red(),
1001 cfg
->cmd_data
.query
.object
->str
,
1003 bt_common_color_reset());
1007 bt_component_class_put_ref(comp_cls
);
1008 bt_value_put_ref(results
);
1013 void print_component_class_help(const char *plugin_name
,
1014 const bt_component_class
*comp_cls
)
1016 const char *comp_class_name
=
1017 bt_component_class_get_name(comp_cls
);
1018 const char *comp_class_description
=
1019 bt_component_class_get_description(comp_cls
);
1020 const char *comp_class_help
=
1021 bt_component_class_get_help(comp_cls
);
1022 bt_component_class_type type
=
1023 bt_component_class_get_type(comp_cls
);
1025 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
1027 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1028 bt_common_color_reset(),
1029 comp_class_description
? comp_class_description
: "(None)");
1031 if (comp_class_help
) {
1032 printf("\n%s\n", comp_class_help
);
1037 int cmd_help(struct bt_config
*cfg
)
1040 const bt_plugin
*plugin
= NULL
;
1041 const bt_component_class
*needed_comp_cls
= NULL
;
1043 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1045 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
1046 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1047 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
1048 bt_common_color_bold(), bt_common_color_fg_red(),
1049 bt_common_color_fg_blue(),
1050 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1051 bt_common_color_reset());
1056 print_plugin_info(plugin
);
1057 printf(" %sSource component classes%s: %d\n",
1058 bt_common_color_bold(),
1059 bt_common_color_reset(),
1060 (int) bt_plugin_get_source_component_class_count(plugin
));
1061 printf(" %sFilter component classes%s: %d\n",
1062 bt_common_color_bold(),
1063 bt_common_color_reset(),
1064 (int) bt_plugin_get_filter_component_class_count(plugin
));
1065 printf(" %sSink component classes%s: %d\n",
1066 bt_common_color_bold(),
1067 bt_common_color_reset(),
1068 (int) bt_plugin_get_sink_component_class_count(plugin
));
1070 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
1071 /* Plugin help only */
1075 needed_comp_cls
= find_component_class(
1076 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1077 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1078 cfg
->cmd_data
.help
.cfg_component
->type
);
1079 if (!needed_comp_cls
) {
1080 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1081 "comp-cls-name=\"%s\", comp-cls-type=%d",
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%s%sCannot find component class %s",
1086 bt_common_color_bold(),
1087 bt_common_color_fg_red(),
1088 bt_common_color_reset());
1089 print_plugin_comp_cls_opt(stderr
,
1090 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1091 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1092 cfg
->cmd_data
.help
.cfg_component
->type
);
1093 fprintf(stderr
, "\n");
1099 print_component_class_help(
1100 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1104 bt_component_class_put_ref(needed_comp_cls
);
1105 bt_plugin_put_ref(plugin
);
1109 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
1111 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
1114 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
1115 const char *cc_type_name
, uint64_t count
,
1116 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
1117 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
1122 printf(" %s%s component classes%s: (none)\n",
1123 bt_common_color_bold(),
1125 bt_common_color_reset());
1128 printf(" %s%s component classes%s:\n",
1129 bt_common_color_bold(),
1131 bt_common_color_reset());
1134 for (i
= 0; i
< count
; i
++) {
1135 const bt_component_class
*comp_class
=
1136 spec_comp_cls_borrow_comp_cls_func(
1137 borrow_comp_cls_by_index_func(plugin
, i
));
1138 const char *comp_class_name
=
1139 bt_component_class_get_name(comp_class
);
1140 const char *comp_class_description
=
1141 bt_component_class_get_description(comp_class
);
1142 bt_component_class_type type
=
1143 bt_component_class_get_type(comp_class
);
1146 print_plugin_comp_cls_opt(stdout
,
1147 bt_plugin_get_name(plugin
), comp_class_name
,
1150 if (comp_class_description
) {
1151 printf(": %s", comp_class_description
);
1162 int cmd_list_plugins(struct bt_config
*cfg
)
1165 int plugins_count
, component_classes_count
= 0, i
;
1167 printf("From the following plugin paths:\n\n");
1168 print_value(stdout
, cfg
->plugin_paths
, 2);
1170 plugins_count
= loaded_plugins
->len
;
1171 if (plugins_count
== 0) {
1172 printf("No plugins found.\n");
1176 for (i
= 0; i
< plugins_count
; i
++) {
1177 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1179 component_classes_count
+=
1180 bt_plugin_get_source_component_class_count(plugin
) +
1181 bt_plugin_get_filter_component_class_count(plugin
) +
1182 bt_plugin_get_sink_component_class_count(plugin
);
1185 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1186 bt_common_color_bold(),
1187 component_classes_count
,
1188 bt_common_color_reset(),
1189 bt_common_color_bold(),
1191 bt_common_color_reset());
1193 for (i
= 0; i
< plugins_count
; i
++) {
1194 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1197 print_plugin_info(plugin
);
1198 cmd_list_plugins_print_component_classes(plugin
, "Source",
1199 bt_plugin_get_source_component_class_count(plugin
),
1200 (plugin_borrow_comp_cls_by_index_func_t
)
1201 bt_plugin_borrow_source_component_class_by_index_const
,
1202 (spec_comp_cls_borrow_comp_cls_func_t
)
1203 bt_component_class_source_as_component_class
);
1204 cmd_list_plugins_print_component_classes(plugin
, "Filter",
1205 bt_plugin_get_filter_component_class_count(plugin
),
1206 (plugin_borrow_comp_cls_by_index_func_t
)
1207 bt_plugin_borrow_filter_component_class_by_index_const
,
1208 (spec_comp_cls_borrow_comp_cls_func_t
)
1209 bt_component_class_filter_as_component_class
);
1210 cmd_list_plugins_print_component_classes(plugin
, "Sink",
1211 bt_plugin_get_sink_component_class_count(plugin
),
1212 (plugin_borrow_comp_cls_by_index_func_t
)
1213 bt_plugin_borrow_sink_component_class_by_index_const
,
1214 (spec_comp_cls_borrow_comp_cls_func_t
)
1215 bt_component_class_sink_as_component_class
);
1223 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1226 const bt_component_class
*comp_cls
= NULL
;
1227 const bt_value
*results
= NULL
;
1228 bt_value
*params
= NULL
;
1229 const bt_value
*map
= NULL
;
1230 const bt_value
*v
= NULL
;
1231 static const char * const plugin_name
= "ctf";
1232 static const char * const comp_cls_name
= "lttng-live";
1233 static const bt_component_class_type comp_cls_type
=
1234 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1235 int64_t array_size
, i
;
1236 const char *fail_reason
= NULL
;
1237 FILE *out_stream
= stdout
;
1239 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1240 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1243 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1244 "comp-cls-name=\"%s\", comp-cls-type=%d",
1245 plugin_name
, comp_cls_name
,
1246 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1247 fprintf(stderr
, "%s%sCannot find component class %s",
1248 bt_common_color_bold(),
1249 bt_common_color_fg_red(),
1250 bt_common_color_reset());
1251 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1252 comp_cls_name
, comp_cls_type
);
1253 fprintf(stderr
, "\n");
1257 params
= bt_value_map_create();
1262 ret
= bt_value_map_insert_string_entry(params
, "url",
1263 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1268 ret
= query(comp_cls
, "sessions", params
,
1269 &results
, &fail_reason
);
1276 if (!bt_value_is_array(results
)) {
1277 BT_LOGE_STR("Expecting an array for sessions query.");
1278 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1279 bt_common_color_bold(),
1280 bt_common_color_fg_red(),
1281 bt_common_color_reset());
1285 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1287 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1291 BT_LOGE_ERRNO("Cannot open file for writing",
1293 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1298 array_size
= bt_value_array_get_size(results
);
1299 for (i
= 0; i
< array_size
; i
++) {
1300 const char *url_text
;
1301 int64_t timer_us
, streams
, clients
;
1303 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1305 BT_LOGE_STR("Unexpected empty array entry.");
1308 if (!bt_value_is_map(map
)) {
1309 BT_LOGE_STR("Unexpected entry type.");
1313 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1315 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1318 url_text
= bt_value_string_get(v
);
1319 fprintf(out_stream
, "%s", url_text
);
1320 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1322 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1325 timer_us
= bt_value_signed_integer_get(v
);
1326 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1327 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1329 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1332 streams
= bt_value_signed_integer_get(v
);
1333 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1334 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1336 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1339 clients
= bt_value_signed_integer_get(v
);
1340 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1346 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1347 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1348 bt_common_color_bold(),
1349 bt_common_color_fg_red(),
1351 bt_common_color_reset());
1357 bt_value_put_ref(results
);
1358 bt_value_put_ref(params
);
1359 bt_component_class_put_ref(comp_cls
);
1361 if (out_stream
&& out_stream
!= stdout
) {
1362 int fclose_ret
= fclose(out_stream
);
1365 BT_LOGE_ERRNO("Cannot close file stream",
1367 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1375 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1378 const bt_component_class
*comp_cls
= NULL
;
1379 const bt_value
*results
= NULL
;
1380 bt_value
*params
= NULL
;
1381 const bt_value
*metadata_text_value
= NULL
;
1382 const char *metadata_text
= NULL
;
1383 static const char * const plugin_name
= "ctf";
1384 static const char * const comp_cls_name
= "fs";
1385 static const bt_component_class_type comp_cls_type
=
1386 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1387 const char *fail_reason
= NULL
;
1388 FILE *out_stream
= stdout
;
1390 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1391 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1394 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1395 "comp-cls-name=\"%s\", comp-cls-type=%d",
1396 plugin_name
, comp_cls_name
,
1397 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1398 fprintf(stderr
, "%s%sCannot find component class %s",
1399 bt_common_color_bold(),
1400 bt_common_color_fg_red(),
1401 bt_common_color_reset());
1402 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1403 comp_cls_name
, comp_cls_type
);
1404 fprintf(stderr
, "\n");
1409 params
= bt_value_map_create();
1415 ret
= bt_value_map_insert_string_entry(params
, "path",
1416 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1422 ret
= query(comp_cls
, "metadata-info",
1423 params
, &results
, &fail_reason
);
1428 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1430 if (!metadata_text_value
) {
1431 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1436 metadata_text
= bt_value_string_get(metadata_text_value
);
1438 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1440 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1444 BT_LOGE_ERRNO("Cannot open file for writing",
1446 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1451 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1453 BT_LOGE("Cannot write whole metadata text to output stream: "
1464 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1465 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1466 bt_common_color_bold(),
1467 bt_common_color_fg_red(),
1469 bt_common_color_reset());
1472 bt_value_put_ref(results
);
1473 bt_value_put_ref(params
);
1474 bt_component_class_put_ref(comp_cls
);
1476 if (out_stream
&& out_stream
!= stdout
) {
1477 int fclose_ret
= fclose(out_stream
);
1480 BT_LOGE_ERRNO("Cannot close file stream",
1482 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1490 char *instance_name
;
1494 struct trace_range
{
1495 uint64_t intersection_range_begin_ns
;
1496 uint64_t intersection_range_end_ns
;
1500 guint
port_id_hash(gconstpointer v
)
1502 const struct port_id
*id
= v
;
1504 BT_ASSERT(id
->instance_name
);
1505 BT_ASSERT(id
->port_name
);
1507 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1511 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1513 const struct port_id
*id1
= v1
;
1514 const struct port_id
*id2
= v2
;
1516 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1517 !strcmp(id1
->port_name
, id2
->port_name
);
1521 void port_id_destroy(gpointer data
)
1523 struct port_id
*id
= data
;
1525 free(id
->instance_name
);
1526 free(id
->port_name
);
1531 void trace_range_destroy(gpointer data
)
1536 struct cmd_run_ctx
{
1538 GHashTable
*src_components
;
1541 GHashTable
*flt_components
;
1544 GHashTable
*sink_components
;
1550 struct bt_config
*cfg
;
1554 bool stream_intersection_mode
;
1557 * Association of struct port_id -> struct trace_range.
1559 GHashTable
*intersections
;
1562 /* Returns a timestamp of the form "(-)s.ns" */
1564 char *s_from_ns(int64_t ns
)
1569 int64_t ts_sec_abs
, ts_nsec_abs
;
1570 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1571 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1573 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1574 is_negative
= false;
1575 ts_sec_abs
= ts_sec
;
1576 ts_nsec_abs
= ts_nsec
;
1577 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1578 is_negative
= false;
1579 ts_sec_abs
= ts_sec
- 1;
1580 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1581 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1583 ts_sec_abs
= ts_sec
;
1584 ts_nsec_abs
= -ts_nsec
;
1585 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1587 ts_sec_abs
= -(ts_sec
+ 1);
1588 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1589 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1591 ts_sec_abs
= -ts_sec
;
1592 ts_nsec_abs
= ts_nsec
;
1593 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1595 ts_sec_abs
= -ts_sec
;
1596 ts_nsec_abs
= -ts_nsec
;
1599 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1600 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1608 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1609 struct cmd_run_ctx
*ctx
,
1610 const bt_component
*upstream_comp
,
1611 const bt_port_output
*out_upstream_port
,
1612 struct bt_config_connection
*cfg_conn
)
1614 typedef uint64_t (*input_port_count_func_t
)(void *);
1615 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1616 const void *, uint64_t);
1617 const bt_port
*upstream_port
=
1618 bt_port_output_as_port_const(out_upstream_port
);
1621 GQuark downstreamp_comp_name_quark
;
1622 void *downstream_comp
;
1623 uint64_t downstream_port_count
;
1625 input_port_count_func_t port_count_fn
;
1626 borrow_input_port_by_index_func_t port_by_index_fn
;
1627 bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1628 bool insert_trimmer
= false;
1629 bt_value
*trimmer_params
= NULL
;
1630 char *intersection_begin
= NULL
;
1631 char *intersection_end
= NULL
;
1632 const bt_component_filter
*trimmer
= NULL
;
1633 const bt_component_class_filter
*trimmer_class
= NULL
;
1634 const bt_port_input
*trimmer_input
= NULL
;
1635 const bt_port_output
*trimmer_output
= NULL
;
1637 if (ctx
->intersections
&&
1638 bt_component_get_class_type(upstream_comp
) ==
1639 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1640 struct trace_range
*range
;
1641 struct port_id port_id
= {
1642 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1643 .port_name
= (char *) bt_port_get_name(upstream_port
)
1646 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1650 range
= (struct trace_range
*) g_hash_table_lookup(
1651 ctx
->intersections
, &port_id
);
1653 bt_value_status status
;
1655 intersection_begin
= s_from_ns(
1656 range
->intersection_range_begin_ns
);
1657 intersection_end
= s_from_ns(
1658 range
->intersection_range_end_ns
);
1659 if (!intersection_begin
|| !intersection_end
) {
1660 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1664 insert_trimmer
= true;
1665 trimmer_params
= bt_value_map_create();
1666 if (!trimmer_params
) {
1670 status
= bt_value_map_insert_string_entry(
1671 trimmer_params
, "begin", intersection_begin
);
1672 if (status
!= BT_VALUE_STATUS_OK
) {
1675 status
= bt_value_map_insert_string_entry(
1677 "end", intersection_end
);
1678 if (status
!= BT_VALUE_STATUS_OK
) {
1683 trimmer_class
= find_filter_component_class("utils", "trimmer");
1684 if (!trimmer_class
) {
1689 BT_LOGI("Connecting upstream port to the next available downstream port: "
1690 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1691 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1692 upstream_port
, bt_port_get_name(upstream_port
),
1693 cfg_conn
->downstream_comp_name
->str
,
1694 cfg_conn
->arg
->str
);
1695 downstreamp_comp_name_quark
= g_quark_from_string(
1696 cfg_conn
->downstream_comp_name
->str
);
1697 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1698 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1699 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1700 port_count_fn
= (input_port_count_func_t
)
1701 bt_component_filter_get_input_port_count
;
1702 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1703 bt_component_filter_borrow_input_port_by_index_const
;
1705 if (!downstream_comp
) {
1706 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1707 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1708 port_count_fn
= (input_port_count_func_t
)
1709 bt_component_sink_get_input_port_count
;
1710 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1711 bt_component_sink_borrow_input_port_by_index_const
;
1714 if (!downstream_comp
) {
1715 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1716 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1717 cfg_conn
->arg
->str
);
1718 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1719 cfg_conn
->arg
->str
);
1723 downstream_port_count
= port_count_fn(downstream_comp
);
1725 for (i
= 0; i
< downstream_port_count
; i
++) {
1726 const bt_port_input
*in_downstream_port
=
1727 port_by_index_fn(downstream_comp
, i
);
1728 const bt_port
*downstream_port
=
1729 bt_port_input_as_port_const(in_downstream_port
);
1730 const char *upstream_port_name
;
1731 const char *downstream_port_name
;
1733 BT_ASSERT(downstream_port
);
1735 /* Skip port if it's already connected. */
1736 if (bt_port_is_connected(downstream_port
)) {
1737 BT_LOGD("Skipping downstream port: already connected: "
1738 "port-addr=%p, port-name=\"%s\"",
1740 bt_port_get_name(downstream_port
));
1744 downstream_port_name
= bt_port_get_name(downstream_port
);
1745 BT_ASSERT(downstream_port_name
);
1746 upstream_port_name
= bt_port_get_name(upstream_port
);
1747 BT_ASSERT(upstream_port_name
);
1749 if (!bt_common_star_glob_match(
1750 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1751 downstream_port_name
, SIZE_MAX
)) {
1755 if (insert_trimmer
) {
1757 * In order to insert the trimmer between the
1758 * two components that were being connected, we
1759 * create a connection configuration entry which
1760 * describes a connection from the trimmer's
1761 * output to the original input that was being
1764 * Hence, the creation of the trimmer will cause
1765 * the graph "new port" listener to establish
1766 * all downstream connections as its output port
1767 * is connected. We will then establish the
1768 * connection between the original upstream
1769 * source and the trimmer.
1771 char *trimmer_name
= NULL
;
1772 bt_graph_status graph_status
;
1774 ret
= asprintf(&trimmer_name
,
1775 "stream-intersection-trimmer-%s",
1776 upstream_port_name
);
1782 ctx
->connect_ports
= false;
1783 graph_status
= bt_graph_add_filter_component(
1784 ctx
->graph
, trimmer_class
, trimmer_name
,
1785 trimmer_params
, &trimmer
);
1787 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1793 bt_component_filter_borrow_input_port_by_index_const(
1795 if (!trimmer_input
) {
1799 bt_component_filter_borrow_output_port_by_index_const(
1801 if (!trimmer_output
) {
1806 * Replace the current downstream port by the trimmer's
1809 in_downstream_port
= trimmer_input
;
1811 bt_port_input_as_port_const(in_downstream_port
);
1812 downstream_port_name
= bt_port_get_name(
1814 BT_ASSERT(downstream_port_name
);
1817 /* We have a winner! */
1818 status
= bt_graph_connect_ports(ctx
->graph
,
1819 out_upstream_port
, in_downstream_port
, NULL
);
1820 downstream_port
= NULL
;
1822 case BT_GRAPH_STATUS_OK
:
1824 case BT_GRAPH_STATUS_CANCELED
:
1825 BT_LOGI_STR("Graph was canceled by user.");
1826 status
= BT_GRAPH_STATUS_OK
;
1828 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1829 BT_LOGE("A component refused a connection to one of its ports: "
1830 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1831 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1832 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1833 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1835 upstream_comp
, bt_component_get_name(upstream_comp
),
1836 upstream_port
, bt_port_get_name(upstream_port
),
1837 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1838 downstream_port
, downstream_port_name
,
1839 cfg_conn
->arg
->str
);
1841 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1842 bt_port_get_name(upstream_port
),
1843 downstream_port_name
,
1844 cfg_conn
->arg
->str
);
1847 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1848 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1849 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1850 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1851 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1853 upstream_comp
, bt_component_get_name(upstream_comp
),
1854 upstream_port
, bt_port_get_name(upstream_port
),
1855 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1856 downstream_port
, downstream_port_name
,
1857 cfg_conn
->arg
->str
);
1859 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1860 bt_port_get_name(upstream_port
),
1861 downstream_port_name
,
1862 cfg_conn
->arg
->str
);
1866 BT_LOGI("Connected component ports: "
1867 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1868 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1869 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1870 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1872 upstream_comp
, bt_component_get_name(upstream_comp
),
1873 upstream_port
, bt_port_get_name(upstream_port
),
1874 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1875 downstream_port
, downstream_port_name
,
1876 cfg_conn
->arg
->str
);
1878 if (insert_trimmer
) {
1880 * The first connection, from the source to the trimmer,
1881 * has been done. We now connect the trimmer to the
1882 * original downstream port.
1884 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1886 bt_component_filter_as_component_const(trimmer
),
1887 trimmer_output
, cfg_conn
);
1891 ctx
->connect_ports
= true;
1895 * We found a matching downstream port: the search is
1901 /* No downstream port found */
1902 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1903 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1904 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1905 upstream_port
, bt_port_get_name(upstream_port
),
1906 cfg_conn
->downstream_comp_name
->str
,
1907 cfg_conn
->arg
->str
);
1909 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1910 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1916 free(intersection_begin
);
1917 free(intersection_end
);
1918 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1919 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1920 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1925 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1926 const bt_port_output
*upstream_port
)
1929 const char *upstream_port_name
;
1930 const char *upstream_comp_name
;
1931 const bt_component
*upstream_comp
= NULL
;
1935 BT_ASSERT(upstream_port
);
1936 upstream_port_name
= bt_port_get_name(
1937 bt_port_output_as_port_const(upstream_port
));
1938 BT_ASSERT(upstream_port_name
);
1939 upstream_comp
= bt_port_borrow_component_const(
1940 bt_port_output_as_port_const(upstream_port
));
1941 if (!upstream_comp
) {
1942 BT_LOGW("Upstream port to connect is not part of a component: "
1943 "port-addr=%p, port-name=\"%s\"",
1944 upstream_port
, upstream_port_name
);
1949 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1950 BT_ASSERT(upstream_comp_name
);
1951 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1952 "port-addr=%p, port-name=\"%s\"",
1953 upstream_comp
, upstream_comp_name
,
1954 upstream_port
, upstream_port_name
);
1956 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1957 struct bt_config_connection
*cfg_conn
=
1959 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1961 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1962 upstream_comp_name
)) {
1966 if (!bt_common_star_glob_match(
1967 cfg_conn
->upstream_port_glob
->str
,
1968 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1972 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1973 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1975 BT_LOGE("Cannot connect upstream port: "
1976 "port-addr=%p, port-name=\"%s\"",
1978 upstream_port_name
);
1980 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1983 cfg_conn
->arg
->str
);
1989 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1990 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1991 upstream_port_name
);
1993 "Cannot create connection: upstream port `%s` does not match any connection\n",
1994 upstream_port_name
);
2004 bt_graph_listener_status
2005 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
2006 const bt_port_output
*out_port
)
2008 const bt_component
*comp
;
2009 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
2010 bt_graph_listener_status ret
= BT_GRAPH_LISTENER_STATUS_OK
;
2012 comp
= bt_port_borrow_component_const(port
);
2013 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
2014 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
2015 comp
, comp
? bt_component_get_name(comp
) : "",
2016 port
, bt_port_get_name(port
));
2018 if (!ctx
->connect_ports
) {
2023 BT_LOGW_STR("Port has no component.");
2027 if (bt_port_is_connected(port
)) {
2028 BT_LOGW_STR("Port is already connected.");
2032 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
2033 BT_LOGF_STR("Cannot connect upstream port.");
2034 fprintf(stderr
, "Added port could not be connected: aborting\n");
2035 ret
= BT_GRAPH_LISTENER_STATUS_ERROR
;
2044 bt_graph_listener_status
graph_source_output_port_added_listener(
2045 const bt_component_source
*component
,
2046 const bt_port_output
*port
, void *data
)
2048 return graph_output_port_added_listener(data
, port
);
2052 bt_graph_listener_status
graph_filter_output_port_added_listener(
2053 const bt_component_filter
*component
,
2054 const bt_port_output
*port
, void *data
)
2056 return graph_output_port_added_listener(data
, port
);
2060 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
2066 if (ctx
->src_components
) {
2067 g_hash_table_destroy(ctx
->src_components
);
2068 ctx
->src_components
= NULL
;
2071 if (ctx
->flt_components
) {
2072 g_hash_table_destroy(ctx
->flt_components
);
2073 ctx
->flt_components
= NULL
;
2076 if (ctx
->sink_components
) {
2077 g_hash_table_destroy(ctx
->sink_components
);
2078 ctx
->sink_components
= NULL
;
2081 if (ctx
->intersections
) {
2082 g_hash_table_destroy(ctx
->intersections
);
2083 ctx
->intersections
= NULL
;
2086 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
2092 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2095 bt_graph_status status
;
2098 ctx
->connect_ports
= false;
2099 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
2100 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2101 if (!ctx
->src_components
) {
2105 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
2106 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2107 if (!ctx
->flt_components
) {
2111 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
2112 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2113 if (!ctx
->sink_components
) {
2117 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2118 ctx
->stream_intersection_mode
= true;
2119 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2120 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2121 if (!ctx
->intersections
) {
2126 ctx
->graph
= bt_graph_create();
2131 the_graph
= ctx
->graph
;
2132 status
= bt_graph_add_source_component_output_port_added_listener(
2133 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
2135 if (status
!= BT_GRAPH_STATUS_OK
) {
2136 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2140 status
= bt_graph_add_filter_component_output_port_added_listener(
2141 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
2143 if (status
!= BT_GRAPH_STATUS_OK
) {
2144 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2151 cmd_run_ctx_destroy(ctx
);
2159 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2160 struct bt_config_component
*cfg_comp
,
2161 const bt_component_class_source
*src_comp_cls
)
2165 int64_t trace_count
;
2166 const char *path
= NULL
;
2167 const bt_value
*query_result
= NULL
;
2168 const bt_value
*trace_info
= NULL
;
2169 const bt_value
*intersection_range
= NULL
;
2170 const bt_value
*intersection_begin
= NULL
;
2171 const bt_value
*intersection_end
= NULL
;
2172 const bt_value
*stream_infos
= NULL
;
2173 const bt_value
*stream_info
= NULL
;
2174 struct port_id
*port_id
= NULL
;
2175 struct trace_range
*trace_range
= NULL
;
2176 const char *fail_reason
= NULL
;
2177 const bt_component_class
*comp_cls
=
2178 bt_component_class_source_as_component_class_const(src_comp_cls
);
2180 ret
= query(comp_cls
, "trace-info",
2181 cfg_comp
->params
, &query_result
,
2184 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2185 "comp-class-name=\"%s\"", fail_reason
,
2186 bt_component_class_get_name(comp_cls
));
2191 BT_ASSERT(query_result
);
2193 if (!bt_value_is_array(query_result
)) {
2194 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2195 "component-class-name=%s",
2196 bt_component_class_get_name(comp_cls
));
2201 trace_count
= bt_value_array_get_size(query_result
);
2202 if (trace_count
< 0) {
2207 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2209 uint64_t stream_idx
;
2210 int64_t stream_count
;
2212 trace_info
= bt_value_array_borrow_element_by_index_const(
2213 query_result
, trace_idx
);
2214 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2216 BT_LOGD_STR("Cannot retrieve trace from query result.");
2220 intersection_range
= bt_value_map_borrow_entry_value_const(
2221 trace_info
, "intersection-range-ns");
2222 if (!intersection_range
) {
2224 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2228 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
2230 if (!intersection_begin
) {
2232 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2236 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
2238 if (!intersection_end
) {
2240 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2244 begin
= bt_value_signed_integer_get(intersection_begin
);
2245 end
= bt_value_signed_integer_get(intersection_end
);
2247 if (begin
< 0 || end
< 0 || end
< begin
) {
2248 BT_LOGW("Invalid trace stream intersection values: "
2249 "intersection-range-ns:begin=%" PRId64
2250 ", intersection-range-ns:end=%" PRId64
,
2256 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
2258 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2260 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2264 stream_count
= bt_value_array_get_size(stream_infos
);
2265 if (stream_count
< 0) {
2270 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2271 const bt_value
*port_name
;
2273 port_id
= g_new0(struct port_id
, 1);
2276 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2279 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2280 if (!port_id
->instance_name
) {
2282 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2286 trace_range
= g_new0(struct trace_range
, 1);
2289 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2292 trace_range
->intersection_range_begin_ns
= begin
;
2293 trace_range
->intersection_range_end_ns
= end
;
2295 stream_info
= bt_value_array_borrow_element_by_index_const(
2296 stream_infos
, stream_idx
);
2297 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2299 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2303 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2304 if (!port_name
|| !bt_value_is_string(port_name
)) {
2306 BT_LOGD_STR("Cannot retrieve port name in query result.");
2310 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2311 if (!port_id
->port_name
) {
2313 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2317 BT_LOGD("Inserting stream intersection ");
2319 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2329 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2330 bt_common_color_bold(),
2331 bt_common_color_fg_yellow(),
2332 path
? path
: "(unknown)",
2333 bt_common_color_reset());
2335 bt_value_put_ref(query_result
);
2337 g_free(trace_range
);
2342 int cmd_run_ctx_create_components_from_config_components(
2343 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2346 const void *comp_cls
= NULL
;
2347 const void *comp
= NULL
;
2350 for (i
= 0; i
< cfg_components
->len
; i
++) {
2351 struct bt_config_component
*cfg_comp
=
2352 g_ptr_array_index(cfg_components
, i
);
2355 switch (cfg_comp
->type
) {
2356 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2357 comp_cls
= find_source_component_class(
2358 cfg_comp
->plugin_name
->str
,
2359 cfg_comp
->comp_cls_name
->str
);
2361 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2362 comp_cls
= find_filter_component_class(
2363 cfg_comp
->plugin_name
->str
,
2364 cfg_comp
->comp_cls_name
->str
);
2366 case BT_COMPONENT_CLASS_TYPE_SINK
:
2367 comp_cls
= find_sink_component_class(
2368 cfg_comp
->plugin_name
->str
,
2369 cfg_comp
->comp_cls_name
->str
);
2376 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2377 "comp-cls-name=\"%s\", comp-cls-type=%d",
2378 cfg_comp
->plugin_name
->str
,
2379 cfg_comp
->comp_cls_name
->str
,
2381 fprintf(stderr
, "%s%sCannot find component class %s",
2382 bt_common_color_bold(),
2383 bt_common_color_fg_red(),
2384 bt_common_color_reset());
2385 print_plugin_comp_cls_opt(stderr
,
2386 cfg_comp
->plugin_name
->str
,
2387 cfg_comp
->comp_cls_name
->str
,
2389 fprintf(stderr
, "\n");
2393 switch (cfg_comp
->type
) {
2394 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2395 ret
= bt_graph_add_source_component(ctx
->graph
,
2396 comp_cls
, cfg_comp
->instance_name
->str
,
2400 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2401 ret
= bt_graph_add_filter_component(ctx
->graph
,
2402 comp_cls
, cfg_comp
->instance_name
->str
,
2406 case BT_COMPONENT_CLASS_TYPE_SINK
:
2407 ret
= bt_graph_add_sink_component(ctx
->graph
,
2408 comp_cls
, cfg_comp
->instance_name
->str
,
2417 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2418 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2420 cfg_comp
->plugin_name
->str
,
2421 cfg_comp
->comp_cls_name
->str
,
2422 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2423 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2424 bt_common_color_bold(),
2425 bt_common_color_fg_red(),
2426 cfg_comp
->instance_name
->str
,
2427 bt_common_color_reset());
2431 if (ctx
->stream_intersection_mode
&&
2432 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2433 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2439 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2440 comp
, cfg_comp
->instance_name
->str
);
2441 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2442 BT_ASSERT(quark
> 0);
2444 switch (cfg_comp
->type
) {
2445 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2446 g_hash_table_insert(ctx
->src_components
,
2447 GUINT_TO_POINTER(quark
), (void *) comp
);
2449 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2450 g_hash_table_insert(ctx
->flt_components
,
2451 GUINT_TO_POINTER(quark
), (void *) comp
);
2453 case BT_COMPONENT_CLASS_TYPE_SINK
:
2454 g_hash_table_insert(ctx
->sink_components
,
2455 GUINT_TO_POINTER(quark
), (void *) comp
);
2462 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2471 bt_object_put_ref(comp
);
2472 bt_object_put_ref(comp_cls
);
2477 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2482 * Make sure that, during this phase, our graph's "port added"
2483 * listener does not connect ports while we are creating the
2484 * components because we have a special, initial phase for
2487 ctx
->connect_ports
= false;
2489 ret
= cmd_run_ctx_create_components_from_config_components(
2490 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2496 ret
= cmd_run_ctx_create_components_from_config_components(
2497 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2503 ret
= cmd_run_ctx_create_components_from_config_components(
2504 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2514 typedef uint64_t (*output_port_count_func_t
)(const void *);
2515 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2516 const void *, uint64_t);
2519 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2520 void *comp
, output_port_count_func_t port_count_fn
,
2521 borrow_output_port_by_index_func_t port_by_index_fn
)
2527 count
= port_count_fn(comp
);
2529 for (i
= 0; i
< count
; i
++) {
2530 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2532 BT_ASSERT(upstream_port
);
2533 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2544 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2547 GHashTableIter iter
;
2548 gpointer g_name_quark
, g_comp
;
2550 ctx
->connect_ports
= true;
2551 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2553 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2554 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2555 (output_port_count_func_t
)
2556 bt_component_source_get_output_port_count
,
2557 (borrow_output_port_by_index_func_t
)
2558 bt_component_source_borrow_output_port_by_index_const
);
2564 g_hash_table_iter_init(&iter
, ctx
->flt_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_filter_get_output_port_count
,
2570 (borrow_output_port_by_index_func_t
)
2571 bt_component_filter_borrow_output_port_by_index_const
);
2582 const char *bt_graph_status_str(bt_graph_status status
)
2585 case BT_GRAPH_STATUS_OK
:
2586 return "BT_GRAPH_STATUS_OK";
2587 case BT_GRAPH_STATUS_END
:
2588 return "BT_GRAPH_STATUS_END";
2589 case BT_GRAPH_STATUS_AGAIN
:
2590 return "BT_GRAPH_STATUS_AGAIN";
2591 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
2592 return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION";
2593 case BT_GRAPH_STATUS_CANCELED
:
2594 return "BT_GRAPH_STATUS_CANCELED";
2595 case BT_GRAPH_STATUS_ERROR
:
2596 return "BT_GRAPH_STATUS_ERROR";
2597 case BT_GRAPH_STATUS_NOMEM
:
2598 return "BT_GRAPH_STATUS_NOMEM";
2605 int cmd_run(struct bt_config
*cfg
)
2608 struct cmd_run_ctx ctx
= { 0 };
2610 /* Initialize the command's context and the graph object */
2611 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2612 BT_LOGE_STR("Cannot initialize the command's context.");
2613 fprintf(stderr
, "Cannot initialize the command's context\n");
2618 BT_LOGI_STR("Canceled by user before creating components.");
2622 BT_LOGI_STR("Creating components.");
2624 /* Create the requested component instances */
2625 if (cmd_run_ctx_create_components(&ctx
)) {
2626 BT_LOGE_STR("Cannot create components.");
2627 fprintf(stderr
, "Cannot create components\n");
2632 BT_LOGI_STR("Canceled by user before connecting components.");
2636 BT_LOGI_STR("Connecting components.");
2638 /* Connect the initially visible component ports */
2639 if (cmd_run_ctx_connect_ports(&ctx
)) {
2640 BT_LOGE_STR("Cannot connect initial component ports.");
2641 fprintf(stderr
, "Cannot connect initial component ports\n");
2646 BT_LOGI_STR("Canceled by user before running the graph.");
2650 BT_LOGI_STR("Running the graph.");
2654 bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2657 * Reset console in case something messed with console
2658 * codes during the graph's execution.
2660 printf("%s", bt_common_color_reset());
2662 fprintf(stderr
, "%s", bt_common_color_reset());
2663 BT_LOGV("bt_graph_run() returned: status=%s",
2664 bt_graph_status_str(graph_status
));
2666 switch (graph_status
) {
2667 case BT_GRAPH_STATUS_OK
:
2669 case BT_GRAPH_STATUS_CANCELED
:
2670 BT_LOGI_STR("Graph was canceled by user.");
2672 case BT_GRAPH_STATUS_AGAIN
:
2673 if (bt_graph_is_canceled(ctx
.graph
)) {
2674 BT_LOGI_STR("Graph was canceled by user.");
2678 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2679 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2681 cfg
->cmd_data
.run
.retry_duration_us
);
2683 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2684 if (bt_graph_is_canceled(ctx
.graph
)) {
2685 BT_LOGI_STR("Graph was canceled by user.");
2691 case BT_GRAPH_STATUS_END
:
2694 BT_LOGE_STR("Graph failed to complete successfully");
2695 fprintf(stderr
, "Graph failed to complete successfully\n");
2708 cmd_run_ctx_destroy(&ctx
);
2713 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2715 const char *env_clash
;
2717 if (!cfg
->command_name
) {
2721 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2722 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2726 if (g_file_test(cfg
->command_name
,
2727 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2728 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2730 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2732 fprintf(stderr
, "\n");
2733 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
2739 void init_log_level(void)
2741 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2745 void set_auto_log_levels(struct bt_config
*cfg
)
2747 const char **env_var_name
;
2750 * Override the configuration's default log level if
2751 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2752 * are found for backward compatibility with legacy Babetrace 1.
2754 if (getenv("BABELTRACE_DEBUG") &&
2755 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2756 cfg
->log_level
= 'V';
2757 } else if (getenv("BABELTRACE_VERBOSE") &&
2758 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2759 cfg
->log_level
= 'I';
2763 * Set log levels according to --debug or --verbose. For
2764 * backward compatibility, --debug is more verbose than
2767 * --verbose: INFO log level
2768 * --debug: VERBOSE log level (includes DEBUG, which is
2769 * is less verbose than VERBOSE in the internal
2770 * logging framework)
2772 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2774 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2775 } else if (cfg
->debug
) {
2776 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2779 * Set library's default log level if not
2780 * explicitly specified.
2782 switch (cfg
->log_level
) {
2784 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE
);
2787 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2790 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
2793 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2796 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN
);
2799 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR
);
2802 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL
);
2810 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2812 bt_cli_log_level
= BT_LOG_INFO
;
2813 } else if (cfg
->debug
) {
2814 bt_cli_log_level
= BT_LOG_VERBOSE
;
2817 * Set CLI's default log level if not explicitly
2820 switch (cfg
->log_level
) {
2822 bt_cli_log_level
= BT_LOG_NONE
;
2825 bt_cli_log_level
= BT_LOG_VERBOSE
;
2828 bt_cli_log_level
= BT_LOG_DEBUG
;
2831 bt_cli_log_level
= BT_LOG_INFO
;
2834 bt_cli_log_level
= BT_LOG_WARN
;
2837 bt_cli_log_level
= BT_LOG_ERROR
;
2840 bt_cli_log_level
= BT_LOG_FATAL
;
2848 env_var_name
= log_level_env_var_names
;
2850 while (*env_var_name
) {
2851 if (!getenv(*env_var_name
)) {
2853 g_setenv(*env_var_name
, "I", 1);
2854 } else if (cfg
->debug
) {
2855 g_setenv(*env_var_name
, "V", 1);
2857 char val
[2] = { 0 };
2860 * Set module's default log level if not
2861 * explicitly specified.
2863 val
[0] = cfg
->log_level
;
2864 g_setenv(*env_var_name
, val
, 1);
2872 int main(int argc
, const char **argv
)
2876 struct bt_config
*cfg
;
2879 set_signal_handler();
2881 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2884 /* Quit without errors; typically usage/version */
2886 BT_LOGI_STR("Quitting without errors.");
2891 BT_LOGE("Command-line error: retcode=%d", retcode
);
2896 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2897 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2902 set_auto_log_levels(cfg
);
2905 if (cfg
->command_needs_plugins
) {
2906 ret
= load_all_plugins(cfg
->plugin_paths
);
2908 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2914 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2915 cfg
->command
, cfg
->command_name
);
2917 switch (cfg
->command
) {
2918 case BT_CONFIG_COMMAND_RUN
:
2921 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2922 ret
= cmd_list_plugins(cfg
);
2924 case BT_CONFIG_COMMAND_HELP
:
2925 ret
= cmd_help(cfg
);
2927 case BT_CONFIG_COMMAND_QUERY
:
2928 ret
= cmd_query(cfg
);
2930 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2931 ret
= cmd_print_ctf_metadata(cfg
);
2933 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2934 ret
= cmd_print_lttng_live_sessions(cfg
);
2937 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2941 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2942 cfg
->command
, cfg
->command_name
, ret
);
2943 warn_command_name_and_directory_clash(cfg
);
2944 retcode
= ret
? 1 : 0;
2947 BT_OBJECT_PUT_REF_AND_RESET(cfg
);