2 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
4 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "CLI"
28 #include <babeltrace2/babeltrace.h>
29 #include "common/common.h"
39 #include "babeltrace2-cfg.h"
40 #include "babeltrace2-cfg-cli-args.h"
41 #include "babeltrace2-cfg-cli-args-default.h"
43 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
44 #define ENV_BABELTRACE_CLI_LOG_LEVEL "BABELTRACE_CLI_LOG_LEVEL"
45 #define NSEC_PER_SEC 1000000000LL
48 * Known environment variable names for the log levels of the project's
51 static const char* log_level_env_var_names
[] = {
52 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
53 "BABELTRACE_PLUGIN_CTF_MSG_ITER_LOG_LEVEL",
54 "BABELTRACE_PLUGIN_CTF_UTILS_LOG_LEVEL",
55 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
56 "BABELTRACE_SINK_CTF_FS_LOG_LEVEL",
57 "BABELTRACE_SINK_TEXT_DETAILS_LOG_LEVEL",
58 "BABELTRACE_SRC_CTF_FS_LOG_LEVEL",
59 "BABELTRACE_SRC_CTF_LTTNG_LIVE_LOG_LEVEL",
63 /* Application's processing graph (weak) */
64 static bt_graph
*the_graph
;
65 static bt_query_executor
*the_query_executor
;
66 static bool canceled
= false;
68 GPtrArray
*loaded_plugins
;
75 BOOL WINAPI
signal_handler(DWORD signal
) {
77 bt_graph_cancel(the_graph
);
86 void set_signal_handler(void)
88 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
89 BT_LOGE("Failed to set the Ctrl+C handler.");
93 #else /* __MINGW32__ */
96 void signal_handler(int signum
)
98 if (signum
!= SIGINT
) {
103 bt_graph_cancel(the_graph
);
106 if (the_query_executor
) {
107 bt_query_executor_cancel(the_query_executor
);
114 void set_signal_handler(void)
116 struct sigaction new_action
, old_action
;
118 new_action
.sa_handler
= signal_handler
;
119 sigemptyset(&new_action
.sa_mask
);
120 new_action
.sa_flags
= 0;
121 sigaction(SIGINT
, NULL
, &old_action
);
123 if (old_action
.sa_handler
!= SIG_IGN
) {
124 sigaction(SIGINT
, &new_action
, NULL
);
128 #endif /* __MINGW32__ */
131 void init_static_data(void)
133 loaded_plugins
= g_ptr_array_new_with_free_func(
134 (GDestroyNotify
) bt_object_put_ref
);
138 void fini_static_data(void)
140 g_ptr_array_free(loaded_plugins
, TRUE
);
144 int create_the_query_executor(void)
148 the_query_executor
= bt_query_executor_create();
149 if (!the_query_executor
) {
150 BT_LOGE_STR("Cannot create a query executor.");
158 void destroy_the_query_executor(void)
160 BT_QUERY_EXECUTOR_PUT_REF_AND_RESET(the_query_executor
);
164 int query(struct bt_config
*cfg
, const bt_component_class
*comp_cls
,
165 const char *obj
, const bt_value
*params
,
166 const bt_value
**user_result
, const char **fail_reason
)
168 const bt_value
*result
= NULL
;
169 bt_query_executor_status status
;
170 *fail_reason
= "unknown error";
173 BT_ASSERT(fail_reason
);
174 BT_ASSERT(user_result
);
175 ret
= create_the_query_executor();
177 /* create_the_query_executor() logs errors */
182 BT_LOGI("Canceled by user before executing the query: "
183 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
184 "query-obj=\"%s\"", comp_cls
,
185 bt_component_class_get_name(comp_cls
), obj
);
186 *fail_reason
= "canceled by user";
191 status
= bt_query_executor_query(the_query_executor
,
192 comp_cls
, obj
, params
, cfg
->log_level
, &result
);
194 case BT_QUERY_EXECUTOR_STATUS_OK
:
196 case BT_QUERY_EXECUTOR_STATUS_AGAIN
:
198 const uint64_t sleep_time_us
= 100000;
200 /* Wait 100 ms and retry */
201 BT_LOGD("Got BT_QUERY_EXECUTOR_STATUS_AGAIN: sleeping: "
202 "time-us=%" PRIu64
, sleep_time_us
);
204 if (usleep(sleep_time_us
)) {
205 if (bt_query_executor_is_canceled(the_query_executor
)) {
206 BT_LOGI("Query was canceled by user: "
207 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
208 "query-obj=\"%s\"", comp_cls
,
209 bt_component_class_get_name(comp_cls
),
211 *fail_reason
= "canceled by user";
218 case BT_QUERY_EXECUTOR_STATUS_CANCELED
:
219 *fail_reason
= "canceled by user";
221 case BT_QUERY_EXECUTOR_STATUS_ERROR
:
223 case BT_QUERY_EXECUTOR_STATUS_INVALID_OBJECT
:
224 *fail_reason
= "invalid or unknown query object";
226 case BT_QUERY_EXECUTOR_STATUS_INVALID_PARAMS
:
227 *fail_reason
= "invalid query parameters";
229 case BT_QUERY_EXECUTOR_STATUS_UNSUPPORTED
:
230 *fail_reason
= "unsupported action";
232 case BT_QUERY_EXECUTOR_STATUS_NOMEM
:
233 *fail_reason
= "not enough memory";
236 BT_LOGF("Unknown query status: status=%d", status
);
242 *user_result
= result
;
250 destroy_the_query_executor();
251 bt_value_put_ref(result
);
256 const bt_plugin
*find_plugin(const char *name
)
259 const bt_plugin
*plugin
= NULL
;
262 BT_LOGI("Finding plugin: name=\"%s\"", name
);
264 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
265 plugin
= g_ptr_array_index(loaded_plugins
, i
);
267 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
275 BT_LOGI("Found plugin: name=\"%s\", plugin-addr=%p",
278 BT_LOGI("Cannot find plugin: name=\"%s\"", name
);
281 bt_plugin_get_ref(plugin
);
285 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
286 const bt_plugin
*, const char *);
289 const void *find_component_class_from_plugin(const char *plugin_name
,
290 const char *comp_class_name
,
291 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
293 const void *comp_class
= NULL
;
294 const bt_plugin
*plugin
;
296 BT_LOGI("Finding component class: plugin-name=\"%s\", "
297 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
299 plugin
= find_plugin(plugin_name
);
304 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
305 bt_object_get_ref(comp_class
);
306 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
310 BT_LOGI("Found component class: plugin-name=\"%s\", "
311 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
313 BT_LOGI("Cannot find source component class: "
314 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
315 plugin_name
, comp_class_name
);
322 const bt_component_class_source
*find_source_component_class(
323 const char *plugin_name
, const char *comp_class_name
)
325 return (const void *) find_component_class_from_plugin(
326 plugin_name
, comp_class_name
,
327 (plugin_borrow_comp_cls_func_t
)
328 bt_plugin_borrow_source_component_class_by_name_const
);
332 const bt_component_class_filter
*find_filter_component_class(
333 const char *plugin_name
, const char *comp_class_name
)
335 return (const void *) find_component_class_from_plugin(
336 plugin_name
, comp_class_name
,
337 (plugin_borrow_comp_cls_func_t
)
338 bt_plugin_borrow_filter_component_class_by_name_const
);
342 const bt_component_class_sink
*find_sink_component_class(
343 const char *plugin_name
, const char *comp_class_name
)
345 return (const void *) find_component_class_from_plugin(plugin_name
,
347 (plugin_borrow_comp_cls_func_t
)
348 bt_plugin_borrow_sink_component_class_by_name_const
);
352 const bt_component_class
*find_component_class(const char *plugin_name
,
353 const char *comp_class_name
,
354 bt_component_class_type comp_class_type
)
356 const bt_component_class
*comp_cls
= NULL
;
358 switch (comp_class_type
) {
359 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
360 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
362 case BT_COMPONENT_CLASS_TYPE_FILTER
:
363 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
365 case BT_COMPONENT_CLASS_TYPE_SINK
:
366 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
376 void print_indent(FILE *fp
, size_t indent
)
380 for (i
= 0; i
< indent
; i
++) {
386 const char *component_type_str(bt_component_class_type type
)
389 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
391 case BT_COMPONENT_CLASS_TYPE_SINK
:
393 case BT_COMPONENT_CLASS_TYPE_FILTER
:
401 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
402 const char *comp_cls_name
, bt_component_class_type type
)
404 GString
*shell_plugin_name
= NULL
;
405 GString
*shell_comp_cls_name
= NULL
;
407 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
408 if (!shell_plugin_name
) {
412 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
413 if (!shell_comp_cls_name
) {
417 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
418 bt_common_color_bold(),
419 bt_common_color_fg_cyan(),
420 component_type_str(type
),
421 bt_common_color_fg_default(),
422 bt_common_color_fg_blue(),
423 shell_plugin_name
->str
,
424 bt_common_color_fg_default(),
425 bt_common_color_fg_yellow(),
426 shell_comp_cls_name
->str
,
427 bt_common_color_reset());
430 if (shell_plugin_name
) {
431 g_string_free(shell_plugin_name
, TRUE
);
434 if (shell_comp_cls_name
) {
435 g_string_free(shell_comp_cls_name
, TRUE
);
440 void print_value(FILE *, const bt_value
*, size_t);
443 void print_value_rec(FILE *, const bt_value
*, size_t);
445 struct print_map_value_data
{
451 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
454 struct print_map_value_data
*print_map_value_data
= data
;
456 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
457 fprintf(print_map_value_data
->fp
, "%s: ", key
);
460 if (bt_value_is_array(object
) &&
461 bt_value_array_is_empty(object
)) {
462 fprintf(print_map_value_data
->fp
, "[ ]\n");
466 if (bt_value_is_map(object
) &&
467 bt_value_map_is_empty(object
)) {
468 fprintf(print_map_value_data
->fp
, "{ }\n");
472 if (bt_value_is_array(object
) ||
473 bt_value_is_map(object
)) {
474 fprintf(print_map_value_data
->fp
, "\n");
477 print_value_rec(print_map_value_data
->fp
, object
,
478 print_map_value_data
->indent
+ 2);
483 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
497 switch (bt_value_get_type(value
)) {
498 case BT_VALUE_TYPE_NULL
:
499 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
500 bt_common_color_reset());
502 case BT_VALUE_TYPE_BOOL
:
503 bool_val
= bt_value_bool_get(value
);
504 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
505 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
506 bt_common_color_reset());
508 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
509 uint_val
= bt_value_unsigned_integer_get(value
);
510 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
511 bt_common_color_fg_red(), uint_val
,
512 bt_common_color_reset());
514 case BT_VALUE_TYPE_SIGNED_INTEGER
:
515 int_val
= bt_value_signed_integer_get(value
);
516 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
517 bt_common_color_fg_red(), int_val
,
518 bt_common_color_reset());
520 case BT_VALUE_TYPE_REAL
:
521 dbl_val
= bt_value_real_get(value
);
522 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
523 bt_common_color_fg_red(), dbl_val
,
524 bt_common_color_reset());
526 case BT_VALUE_TYPE_STRING
:
527 str_val
= bt_value_string_get(value
);
528 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
529 bt_common_color_fg_green(), str_val
,
530 bt_common_color_reset());
532 case BT_VALUE_TYPE_ARRAY
:
533 size
= bt_value_array_get_size(value
);
539 print_indent(fp
, indent
);
540 fprintf(fp
, "[ ]\n");
544 for (i
= 0; i
< size
; i
++) {
545 const bt_value
*element
=
546 bt_value_array_borrow_element_by_index_const(
552 print_indent(fp
, indent
);
555 if (bt_value_is_array(element
) &&
556 bt_value_array_is_empty(element
)) {
557 fprintf(fp
, "[ ]\n");
561 if (bt_value_is_map(element
) &&
562 bt_value_map_is_empty(element
)) {
563 fprintf(fp
, "{ }\n");
567 if (bt_value_is_array(element
) ||
568 bt_value_is_map(element
)) {
572 print_value_rec(fp
, element
, indent
+ 2);
575 case BT_VALUE_TYPE_MAP
:
577 struct print_map_value_data data
= {
582 if (bt_value_map_is_empty(value
)) {
583 print_indent(fp
, indent
);
584 fprintf(fp
, "{ }\n");
588 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
597 BT_LOGE("Error printing value of type %s.",
598 bt_common_value_type_string(bt_value_get_type(value
)));
602 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
604 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
605 print_indent(fp
, indent
);
608 print_value_rec(fp
, value
, indent
);
612 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
614 fprintf(stderr
, " ");
615 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
616 bt_config_component
->comp_cls_name
->str
,
617 bt_config_component
->type
);
618 fprintf(stderr
, ":\n");
620 if (bt_config_component
->instance_name
->len
> 0) {
621 fprintf(stderr
, " Name: %s\n",
622 bt_config_component
->instance_name
->str
);
625 fprintf(stderr
, " Parameters:\n");
626 print_value(stderr
, bt_config_component
->params
, 8);
630 void print_bt_config_components(GPtrArray
*array
)
634 for (i
= 0; i
< array
->len
; i
++) {
635 struct bt_config_component
*cfg_component
=
636 bt_config_get_component(array
, i
);
637 print_bt_config_component(cfg_component
);
638 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
643 void print_plugin_paths(const bt_value
*plugin_paths
)
645 fprintf(stderr
, " Plugin paths:\n");
646 print_value(stderr
, plugin_paths
, 4);
650 void print_cfg_run(struct bt_config
*cfg
)
654 print_plugin_paths(cfg
->plugin_paths
);
655 fprintf(stderr
, " Source component instances:\n");
656 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
658 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
659 fprintf(stderr
, " Filter component instances:\n");
660 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
663 fprintf(stderr
, " Sink component instances:\n");
664 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
665 fprintf(stderr
, " Connections:\n");
667 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
668 struct bt_config_connection
*cfg_connection
=
669 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
672 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
673 cfg_connection
->upstream_comp_name
->str
,
674 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
675 cfg_connection
->upstream_port_glob
->str
,
676 cfg_connection
->downstream_comp_name
->str
,
677 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
678 cfg_connection
->downstream_port_glob
->str
);
683 void print_cfg_list_plugins(struct bt_config
*cfg
)
685 print_plugin_paths(cfg
->plugin_paths
);
689 void print_cfg_help(struct bt_config
*cfg
)
691 print_plugin_paths(cfg
->plugin_paths
);
695 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
697 print_plugin_paths(cfg
->plugin_paths
);
698 fprintf(stderr
, " Path: %s\n",
699 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
703 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
705 print_plugin_paths(cfg
->plugin_paths
);
706 fprintf(stderr
, " URL: %s\n",
707 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
711 void print_cfg_query(struct bt_config
*cfg
)
713 print_plugin_paths(cfg
->plugin_paths
);
714 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
715 fprintf(stderr
, " Component class:\n");
716 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
720 void print_cfg(struct bt_config
*cfg
)
722 if (!BT_LOG_ON_INFO
) {
726 BT_LOGI_STR("CLI configuration:");
727 BT_LOGI(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
728 BT_LOGI(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
730 switch (cfg
->command
) {
731 case BT_CONFIG_COMMAND_RUN
:
734 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
735 print_cfg_list_plugins(cfg
);
737 case BT_CONFIG_COMMAND_HELP
:
740 case BT_CONFIG_COMMAND_QUERY
:
741 print_cfg_query(cfg
);
743 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
744 print_cfg_print_ctf_metadata(cfg
);
746 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
747 print_cfg_print_lttng_live_sessions(cfg
);
755 void add_to_loaded_plugins(const bt_plugin_set
*plugin_set
)
760 count
= bt_plugin_set_get_plugin_count(plugin_set
);
761 BT_ASSERT(count
>= 0);
763 for (i
= 0; i
< count
; i
++) {
764 const bt_plugin
*plugin
=
765 bt_plugin_set_borrow_plugin_by_index_const(plugin_set
, i
);
766 const bt_plugin
*loaded_plugin
=
767 find_plugin(bt_plugin_get_name(plugin
));
772 BT_LOGI("Not using plugin: another one already exists with the same name: "
773 "plugin-name=\"%s\", plugin-path=\"%s\", "
774 "existing-plugin-path=\"%s\"",
775 bt_plugin_get_name(plugin
),
776 bt_plugin_get_path(plugin
),
777 bt_plugin_get_path(loaded_plugin
));
778 bt_plugin_put_ref(loaded_plugin
);
780 /* Add to global array. */
781 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
782 bt_plugin_get_name(plugin
));
783 bt_plugin_get_ref(plugin
);
784 g_ptr_array_add(loaded_plugins
, (void *) plugin
);
790 int load_dynamic_plugins(const bt_value
*plugin_paths
)
792 int nr_paths
, i
, ret
= 0;
794 nr_paths
= bt_value_array_get_size(plugin_paths
);
796 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
801 BT_LOGI_STR("Loading dynamic plugins.");
803 for (i
= 0; i
< nr_paths
; i
++) {
804 const bt_value
*plugin_path_value
= NULL
;
805 const char *plugin_path
;
806 const bt_plugin_set
*plugin_set
;
809 bt_value_array_borrow_element_by_index_const(
811 plugin_path
= bt_value_string_get(plugin_path_value
);
814 * Skip this if the directory does not exist because
815 * bt_plugin_find_all_from_dir() expects an existing
818 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
819 BT_LOGI("Skipping nonexistent directory path: "
820 "path=\"%s\"", plugin_path
);
824 plugin_set
= bt_plugin_find_all_from_dir(plugin_path
, false);
826 BT_LOGI("Unable to load dynamic plugins from directory: "
827 "path=\"%s\"", plugin_path
);
831 add_to_loaded_plugins(plugin_set
);
832 bt_plugin_set_put_ref(plugin_set
);
839 int load_static_plugins(void)
842 const bt_plugin_set
*plugin_set
;
844 BT_LOGI("Loading static plugins.");
845 plugin_set
= bt_plugin_find_all_from_static();
847 BT_LOGE("Unable to load static plugins.");
852 add_to_loaded_plugins(plugin_set
);
853 bt_plugin_set_put_ref(plugin_set
);
859 int load_all_plugins(const bt_value
*plugin_paths
)
863 if (load_dynamic_plugins(plugin_paths
)) {
868 if (load_static_plugins()) {
873 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
880 void print_plugin_info(const bt_plugin
*plugin
)
882 unsigned int major
, minor
, patch
;
884 bt_property_availability version_avail
;
885 const char *plugin_name
;
889 const char *plugin_description
;
891 plugin_name
= bt_plugin_get_name(plugin
);
892 path
= bt_plugin_get_path(plugin
);
893 author
= bt_plugin_get_author(plugin
);
894 license
= bt_plugin_get_license(plugin
);
895 plugin_description
= bt_plugin_get_description(plugin
);
896 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
898 printf("%s%s%s%s:\n", bt_common_color_bold(),
899 bt_common_color_fg_blue(), plugin_name
,
900 bt_common_color_reset());
902 printf(" %sPath%s: %s\n", bt_common_color_bold(),
903 bt_common_color_reset(), path
);
908 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
909 printf(" %sVersion%s: %u.%u.%u",
910 bt_common_color_bold(), bt_common_color_reset(),
911 major
, minor
, patch
);
920 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
921 bt_common_color_reset(),
922 plugin_description
? plugin_description
: "(None)");
923 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
924 bt_common_color_reset(), author
? author
: "(Unknown)");
925 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
926 bt_common_color_reset(),
927 license
? license
: "(Unknown)");
931 int cmd_query(struct bt_config
*cfg
)
934 const bt_component_class
*comp_cls
= NULL
;
935 const bt_value
*results
= NULL
;
936 const char *fail_reason
= NULL
;
938 comp_cls
= find_component_class(
939 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
940 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
941 cfg
->cmd_data
.query
.cfg_component
->type
);
943 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
944 "comp-cls-name=\"%s\", comp-cls-type=%d",
945 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
946 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
947 cfg
->cmd_data
.query
.cfg_component
->type
);
948 fprintf(stderr
, "%s%sCannot find component class %s",
949 bt_common_color_bold(),
950 bt_common_color_fg_red(),
951 bt_common_color_reset());
952 print_plugin_comp_cls_opt(stderr
,
953 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
954 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
955 cfg
->cmd_data
.query
.cfg_component
->type
);
956 fprintf(stderr
, "\n");
961 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
962 cfg
->cmd_data
.query
.cfg_component
->params
,
963 &results
, &fail_reason
);
968 print_value(stdout
, results
, 0);
972 BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", "
973 "comp-cls-name=\"%s\", comp-cls-type=%d "
974 "object=\"%s\"", fail_reason
,
975 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
976 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
977 cfg
->cmd_data
.query
.cfg_component
->type
,
978 cfg
->cmd_data
.query
.object
->str
);
979 fprintf(stderr
, "%s%sFailed to query info to %s",
980 bt_common_color_bold(),
981 bt_common_color_fg_red(),
982 bt_common_color_reset());
983 print_plugin_comp_cls_opt(stderr
,
984 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
985 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
986 cfg
->cmd_data
.query
.cfg_component
->type
);
987 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
988 bt_common_color_bold(),
989 bt_common_color_fg_red(),
990 cfg
->cmd_data
.query
.object
->str
,
992 bt_common_color_reset());
996 bt_component_class_put_ref(comp_cls
);
997 bt_value_put_ref(results
);
1002 void print_component_class_help(const char *plugin_name
,
1003 const bt_component_class
*comp_cls
)
1005 const char *comp_class_name
=
1006 bt_component_class_get_name(comp_cls
);
1007 const char *comp_class_description
=
1008 bt_component_class_get_description(comp_cls
);
1009 const char *comp_class_help
=
1010 bt_component_class_get_help(comp_cls
);
1011 bt_component_class_type type
=
1012 bt_component_class_get_type(comp_cls
);
1014 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
1016 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1017 bt_common_color_reset(),
1018 comp_class_description
? comp_class_description
: "(None)");
1020 if (comp_class_help
) {
1021 printf("\n%s\n", comp_class_help
);
1026 int cmd_help(struct bt_config
*cfg
)
1029 const bt_plugin
*plugin
= NULL
;
1030 const bt_component_class
*needed_comp_cls
= NULL
;
1032 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1034 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
1035 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1036 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
1037 bt_common_color_bold(), bt_common_color_fg_red(),
1038 bt_common_color_fg_blue(),
1039 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1040 bt_common_color_reset());
1045 print_plugin_info(plugin
);
1046 printf(" %sSource component classes%s: %d\n",
1047 bt_common_color_bold(),
1048 bt_common_color_reset(),
1049 (int) bt_plugin_get_source_component_class_count(plugin
));
1050 printf(" %sFilter component classes%s: %d\n",
1051 bt_common_color_bold(),
1052 bt_common_color_reset(),
1053 (int) bt_plugin_get_filter_component_class_count(plugin
));
1054 printf(" %sSink component classes%s: %d\n",
1055 bt_common_color_bold(),
1056 bt_common_color_reset(),
1057 (int) bt_plugin_get_sink_component_class_count(plugin
));
1059 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
1060 /* Plugin help only */
1064 needed_comp_cls
= find_component_class(
1065 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1066 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1067 cfg
->cmd_data
.help
.cfg_component
->type
);
1068 if (!needed_comp_cls
) {
1069 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1070 "comp-cls-name=\"%s\", comp-cls-type=%d",
1071 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1072 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1073 cfg
->cmd_data
.help
.cfg_component
->type
);
1074 fprintf(stderr
, "\n%s%sCannot find component class %s",
1075 bt_common_color_bold(),
1076 bt_common_color_fg_red(),
1077 bt_common_color_reset());
1078 print_plugin_comp_cls_opt(stderr
,
1079 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1080 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1081 cfg
->cmd_data
.help
.cfg_component
->type
);
1082 fprintf(stderr
, "\n");
1088 print_component_class_help(
1089 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1093 bt_component_class_put_ref(needed_comp_cls
);
1094 bt_plugin_put_ref(plugin
);
1098 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
1100 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
1103 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
1104 const char *cc_type_name
, uint64_t count
,
1105 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
1106 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
1111 printf(" %s%s component classes%s: (none)\n",
1112 bt_common_color_bold(),
1114 bt_common_color_reset());
1117 printf(" %s%s component classes%s:\n",
1118 bt_common_color_bold(),
1120 bt_common_color_reset());
1123 for (i
= 0; i
< count
; i
++) {
1124 const bt_component_class
*comp_class
=
1125 spec_comp_cls_borrow_comp_cls_func(
1126 borrow_comp_cls_by_index_func(plugin
, i
));
1127 const char *comp_class_name
=
1128 bt_component_class_get_name(comp_class
);
1129 const char *comp_class_description
=
1130 bt_component_class_get_description(comp_class
);
1131 bt_component_class_type type
=
1132 bt_component_class_get_type(comp_class
);
1135 print_plugin_comp_cls_opt(stdout
,
1136 bt_plugin_get_name(plugin
), comp_class_name
,
1139 if (comp_class_description
) {
1140 printf(": %s", comp_class_description
);
1151 int cmd_list_plugins(struct bt_config
*cfg
)
1154 int plugins_count
, component_classes_count
= 0, i
;
1156 printf("From the following plugin paths:\n\n");
1157 print_value(stdout
, cfg
->plugin_paths
, 2);
1159 plugins_count
= loaded_plugins
->len
;
1160 if (plugins_count
== 0) {
1161 printf("No plugins found.\n");
1165 for (i
= 0; i
< plugins_count
; i
++) {
1166 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1168 component_classes_count
+=
1169 bt_plugin_get_source_component_class_count(plugin
) +
1170 bt_plugin_get_filter_component_class_count(plugin
) +
1171 bt_plugin_get_sink_component_class_count(plugin
);
1174 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1175 bt_common_color_bold(),
1176 component_classes_count
,
1177 bt_common_color_reset(),
1178 bt_common_color_bold(),
1180 bt_common_color_reset());
1182 for (i
= 0; i
< plugins_count
; i
++) {
1183 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1186 print_plugin_info(plugin
);
1187 cmd_list_plugins_print_component_classes(plugin
, "Source",
1188 bt_plugin_get_source_component_class_count(plugin
),
1189 (plugin_borrow_comp_cls_by_index_func_t
)
1190 bt_plugin_borrow_source_component_class_by_index_const
,
1191 (spec_comp_cls_borrow_comp_cls_func_t
)
1192 bt_component_class_source_as_component_class
);
1193 cmd_list_plugins_print_component_classes(plugin
, "Filter",
1194 bt_plugin_get_filter_component_class_count(plugin
),
1195 (plugin_borrow_comp_cls_by_index_func_t
)
1196 bt_plugin_borrow_filter_component_class_by_index_const
,
1197 (spec_comp_cls_borrow_comp_cls_func_t
)
1198 bt_component_class_filter_as_component_class
);
1199 cmd_list_plugins_print_component_classes(plugin
, "Sink",
1200 bt_plugin_get_sink_component_class_count(plugin
),
1201 (plugin_borrow_comp_cls_by_index_func_t
)
1202 bt_plugin_borrow_sink_component_class_by_index_const
,
1203 (spec_comp_cls_borrow_comp_cls_func_t
)
1204 bt_component_class_sink_as_component_class
);
1212 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1215 const bt_component_class
*comp_cls
= NULL
;
1216 const bt_value
*results
= NULL
;
1217 bt_value
*params
= NULL
;
1218 const bt_value
*map
= NULL
;
1219 const bt_value
*v
= NULL
;
1220 static const char * const plugin_name
= "ctf";
1221 static const char * const comp_cls_name
= "lttng-live";
1222 static const bt_component_class_type comp_cls_type
=
1223 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1224 int64_t array_size
, i
;
1225 const char *fail_reason
= NULL
;
1226 FILE *out_stream
= stdout
;
1228 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1229 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1232 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1233 "comp-cls-name=\"%s\", comp-cls-type=%d",
1234 plugin_name
, comp_cls_name
,
1235 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1236 fprintf(stderr
, "%s%sCannot find component class %s",
1237 bt_common_color_bold(),
1238 bt_common_color_fg_red(),
1239 bt_common_color_reset());
1240 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1241 comp_cls_name
, comp_cls_type
);
1242 fprintf(stderr
, "\n");
1246 params
= bt_value_map_create();
1251 ret
= bt_value_map_insert_string_entry(params
, "url",
1252 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1257 ret
= query(cfg
, comp_cls
, "sessions", params
,
1258 &results
, &fail_reason
);
1265 if (!bt_value_is_array(results
)) {
1266 BT_LOGE_STR("Expecting an array for sessions query.");
1267 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1268 bt_common_color_bold(),
1269 bt_common_color_fg_red(),
1270 bt_common_color_reset());
1274 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1276 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1280 BT_LOGE_ERRNO("Cannot open file for writing",
1282 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1287 array_size
= bt_value_array_get_size(results
);
1288 for (i
= 0; i
< array_size
; i
++) {
1289 const char *url_text
;
1290 int64_t timer_us
, streams
, clients
;
1292 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1294 BT_LOGE_STR("Unexpected empty array entry.");
1297 if (!bt_value_is_map(map
)) {
1298 BT_LOGE_STR("Unexpected entry type.");
1302 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1304 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1307 url_text
= bt_value_string_get(v
);
1308 fprintf(out_stream
, "%s", url_text
);
1309 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1311 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1314 timer_us
= bt_value_signed_integer_get(v
);
1315 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1316 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1318 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1321 streams
= bt_value_signed_integer_get(v
);
1322 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1323 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1325 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1328 clients
= bt_value_signed_integer_get(v
);
1329 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1335 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1336 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1337 bt_common_color_bold(),
1338 bt_common_color_fg_red(),
1340 bt_common_color_reset());
1346 bt_value_put_ref(results
);
1347 bt_value_put_ref(params
);
1348 bt_component_class_put_ref(comp_cls
);
1350 if (out_stream
&& out_stream
!= stdout
) {
1351 int fclose_ret
= fclose(out_stream
);
1354 BT_LOGE_ERRNO("Cannot close file stream",
1356 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1364 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1367 const bt_component_class
*comp_cls
= NULL
;
1368 const bt_value
*results
= NULL
;
1369 bt_value
*params
= NULL
;
1370 const bt_value
*metadata_text_value
= NULL
;
1371 const char *metadata_text
= NULL
;
1372 static const char * const plugin_name
= "ctf";
1373 static const char * const comp_cls_name
= "fs";
1374 static const bt_component_class_type comp_cls_type
=
1375 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1376 const char *fail_reason
= NULL
;
1377 FILE *out_stream
= stdout
;
1379 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1380 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1383 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1384 "comp-cls-name=\"%s\", comp-cls-type=%d",
1385 plugin_name
, comp_cls_name
,
1386 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1387 fprintf(stderr
, "%s%sCannot find component class %s",
1388 bt_common_color_bold(),
1389 bt_common_color_fg_red(),
1390 bt_common_color_reset());
1391 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1392 comp_cls_name
, comp_cls_type
);
1393 fprintf(stderr
, "\n");
1398 params
= bt_value_map_create();
1404 ret
= bt_value_map_insert_string_entry(params
, "path",
1405 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1411 ret
= query(cfg
, comp_cls
, "metadata-info",
1412 params
, &results
, &fail_reason
);
1417 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
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: "
1453 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1454 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1455 bt_common_color_bold(),
1456 bt_common_color_fg_red(),
1458 bt_common_color_reset());
1461 bt_value_put_ref(results
);
1462 bt_value_put_ref(params
);
1463 bt_component_class_put_ref(comp_cls
);
1465 if (out_stream
&& out_stream
!= stdout
) {
1466 int fclose_ret
= fclose(out_stream
);
1469 BT_LOGE_ERRNO("Cannot close file stream",
1471 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1479 char *instance_name
;
1483 struct trace_range
{
1484 uint64_t intersection_range_begin_ns
;
1485 uint64_t intersection_range_end_ns
;
1489 guint
port_id_hash(gconstpointer v
)
1491 const struct port_id
*id
= v
;
1493 BT_ASSERT(id
->instance_name
);
1494 BT_ASSERT(id
->port_name
);
1496 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1500 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1502 const struct port_id
*id1
= v1
;
1503 const struct port_id
*id2
= v2
;
1505 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1506 !strcmp(id1
->port_name
, id2
->port_name
);
1510 void port_id_destroy(gpointer data
)
1512 struct port_id
*id
= data
;
1514 free(id
->instance_name
);
1515 free(id
->port_name
);
1520 void trace_range_destroy(gpointer data
)
1525 struct cmd_run_ctx
{
1527 GHashTable
*src_components
;
1530 GHashTable
*flt_components
;
1533 GHashTable
*sink_components
;
1539 struct bt_config
*cfg
;
1543 bool stream_intersection_mode
;
1546 * Association of struct port_id -> struct trace_range.
1548 GHashTable
*intersections
;
1551 /* Returns a timestamp of the form "(-)s.ns" */
1553 char *s_from_ns(int64_t ns
)
1558 int64_t ts_sec_abs
, ts_nsec_abs
;
1559 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1560 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1562 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1563 is_negative
= false;
1564 ts_sec_abs
= ts_sec
;
1565 ts_nsec_abs
= ts_nsec
;
1566 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1567 is_negative
= false;
1568 ts_sec_abs
= ts_sec
- 1;
1569 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1570 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1572 ts_sec_abs
= ts_sec
;
1573 ts_nsec_abs
= -ts_nsec
;
1574 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1576 ts_sec_abs
= -(ts_sec
+ 1);
1577 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1578 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1580 ts_sec_abs
= -ts_sec
;
1581 ts_nsec_abs
= ts_nsec
;
1582 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1584 ts_sec_abs
= -ts_sec
;
1585 ts_nsec_abs
= -ts_nsec
;
1588 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1589 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1597 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1598 struct cmd_run_ctx
*ctx
,
1599 const bt_component
*upstream_comp
,
1600 const bt_port_output
*out_upstream_port
,
1601 struct bt_config_connection
*cfg_conn
)
1603 typedef uint64_t (*input_port_count_func_t
)(void *);
1604 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1605 const void *, uint64_t);
1606 const bt_port
*upstream_port
=
1607 bt_port_output_as_port_const(out_upstream_port
);
1610 GQuark downstreamp_comp_name_quark
;
1611 void *downstream_comp
;
1612 uint64_t downstream_port_count
;
1614 input_port_count_func_t port_count_fn
;
1615 borrow_input_port_by_index_func_t port_by_index_fn
;
1616 bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1617 bool insert_trimmer
= false;
1618 bt_value
*trimmer_params
= NULL
;
1619 char *intersection_begin
= NULL
;
1620 char *intersection_end
= NULL
;
1621 const bt_component_filter
*trimmer
= NULL
;
1622 const bt_component_class_filter
*trimmer_class
= NULL
;
1623 const bt_port_input
*trimmer_input
= NULL
;
1624 const bt_port_output
*trimmer_output
= NULL
;
1626 if (ctx
->intersections
&&
1627 bt_component_get_class_type(upstream_comp
) ==
1628 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1629 struct trace_range
*range
;
1630 struct port_id port_id
= {
1631 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1632 .port_name
= (char *) bt_port_get_name(upstream_port
)
1635 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1639 range
= (struct trace_range
*) g_hash_table_lookup(
1640 ctx
->intersections
, &port_id
);
1642 bt_value_status status
;
1644 intersection_begin
= s_from_ns(
1645 range
->intersection_range_begin_ns
);
1646 intersection_end
= s_from_ns(
1647 range
->intersection_range_end_ns
);
1648 if (!intersection_begin
|| !intersection_end
) {
1649 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1653 insert_trimmer
= true;
1654 trimmer_params
= bt_value_map_create();
1655 if (!trimmer_params
) {
1659 status
= bt_value_map_insert_string_entry(
1660 trimmer_params
, "begin", intersection_begin
);
1661 if (status
!= BT_VALUE_STATUS_OK
) {
1664 status
= bt_value_map_insert_string_entry(
1666 "end", intersection_end
);
1667 if (status
!= BT_VALUE_STATUS_OK
) {
1672 trimmer_class
= find_filter_component_class("utils", "trimmer");
1673 if (!trimmer_class
) {
1678 BT_LOGI("Connecting upstream port to the next available downstream port: "
1679 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1680 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1681 upstream_port
, bt_port_get_name(upstream_port
),
1682 cfg_conn
->downstream_comp_name
->str
,
1683 cfg_conn
->arg
->str
);
1684 downstreamp_comp_name_quark
= g_quark_from_string(
1685 cfg_conn
->downstream_comp_name
->str
);
1686 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1687 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1688 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1689 port_count_fn
= (input_port_count_func_t
)
1690 bt_component_filter_get_input_port_count
;
1691 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1692 bt_component_filter_borrow_input_port_by_index_const
;
1694 if (!downstream_comp
) {
1695 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1696 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1697 port_count_fn
= (input_port_count_func_t
)
1698 bt_component_sink_get_input_port_count
;
1699 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1700 bt_component_sink_borrow_input_port_by_index_const
;
1703 if (!downstream_comp
) {
1704 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1705 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1706 cfg_conn
->arg
->str
);
1707 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1708 cfg_conn
->arg
->str
);
1712 downstream_port_count
= port_count_fn(downstream_comp
);
1714 for (i
= 0; i
< downstream_port_count
; i
++) {
1715 const bt_port_input
*in_downstream_port
=
1716 port_by_index_fn(downstream_comp
, i
);
1717 const bt_port
*downstream_port
=
1718 bt_port_input_as_port_const(in_downstream_port
);
1719 const char *upstream_port_name
;
1720 const char *downstream_port_name
;
1722 BT_ASSERT(downstream_port
);
1724 /* Skip port if it's already connected. */
1725 if (bt_port_is_connected(downstream_port
)) {
1726 BT_LOGI("Skipping downstream port: already connected: "
1727 "port-addr=%p, port-name=\"%s\"",
1729 bt_port_get_name(downstream_port
));
1733 downstream_port_name
= bt_port_get_name(downstream_port
);
1734 BT_ASSERT(downstream_port_name
);
1735 upstream_port_name
= bt_port_get_name(upstream_port
);
1736 BT_ASSERT(upstream_port_name
);
1738 if (!bt_common_star_glob_match(
1739 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1740 downstream_port_name
, SIZE_MAX
)) {
1744 if (insert_trimmer
) {
1746 * In order to insert the trimmer between the
1747 * two components that were being connected, we
1748 * create a connection configuration entry which
1749 * describes a connection from the trimmer's
1750 * output to the original input that was being
1753 * Hence, the creation of the trimmer will cause
1754 * the graph "new port" listener to establish
1755 * all downstream connections as its output port
1756 * is connected. We will then establish the
1757 * connection between the original upstream
1758 * source and the trimmer.
1760 char *trimmer_name
= NULL
;
1761 bt_graph_status graph_status
;
1763 ret
= asprintf(&trimmer_name
,
1764 "stream-intersection-trimmer-%s",
1765 upstream_port_name
);
1771 ctx
->connect_ports
= false;
1772 graph_status
= bt_graph_add_filter_component(
1773 ctx
->graph
, trimmer_class
, trimmer_name
,
1774 trimmer_params
, ctx
->cfg
->log_level
,
1777 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1783 bt_component_filter_borrow_input_port_by_index_const(
1785 if (!trimmer_input
) {
1789 bt_component_filter_borrow_output_port_by_index_const(
1791 if (!trimmer_output
) {
1796 * Replace the current downstream port by the trimmer's
1799 in_downstream_port
= trimmer_input
;
1801 bt_port_input_as_port_const(in_downstream_port
);
1802 downstream_port_name
= bt_port_get_name(
1804 BT_ASSERT(downstream_port_name
);
1807 /* We have a winner! */
1808 status
= bt_graph_connect_ports(ctx
->graph
,
1809 out_upstream_port
, in_downstream_port
, NULL
);
1810 downstream_port
= NULL
;
1812 case BT_GRAPH_STATUS_OK
:
1814 case BT_GRAPH_STATUS_CANCELED
:
1815 BT_LOGI_STR("Graph was canceled by user.");
1816 status
= BT_GRAPH_STATUS_OK
;
1818 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1819 BT_LOGE("A component refused a connection to one of its ports: "
1820 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1821 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1822 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1823 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1825 upstream_comp
, bt_component_get_name(upstream_comp
),
1826 upstream_port
, bt_port_get_name(upstream_port
),
1827 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1828 downstream_port
, downstream_port_name
,
1829 cfg_conn
->arg
->str
);
1831 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1832 bt_port_get_name(upstream_port
),
1833 downstream_port_name
,
1834 cfg_conn
->arg
->str
);
1837 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1838 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1839 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1840 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1841 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1843 upstream_comp
, bt_component_get_name(upstream_comp
),
1844 upstream_port
, bt_port_get_name(upstream_port
),
1845 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1846 downstream_port
, downstream_port_name
,
1847 cfg_conn
->arg
->str
);
1849 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1850 bt_port_get_name(upstream_port
),
1851 downstream_port_name
,
1852 cfg_conn
->arg
->str
);
1856 BT_LOGI("Connected component ports: "
1857 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1858 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1859 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1860 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1862 upstream_comp
, bt_component_get_name(upstream_comp
),
1863 upstream_port
, bt_port_get_name(upstream_port
),
1864 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1865 downstream_port
, downstream_port_name
,
1866 cfg_conn
->arg
->str
);
1868 if (insert_trimmer
) {
1870 * The first connection, from the source to the trimmer,
1871 * has been done. We now connect the trimmer to the
1872 * original downstream port.
1874 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1876 bt_component_filter_as_component_const(trimmer
),
1877 trimmer_output
, cfg_conn
);
1881 ctx
->connect_ports
= true;
1885 * We found a matching downstream port: the search is
1891 /* No downstream port found */
1892 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1893 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1894 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1895 upstream_port
, bt_port_get_name(upstream_port
),
1896 cfg_conn
->downstream_comp_name
->str
,
1897 cfg_conn
->arg
->str
);
1899 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1900 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1906 free(intersection_begin
);
1907 free(intersection_end
);
1908 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1909 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1910 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1915 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1916 const bt_port_output
*upstream_port
)
1919 const char *upstream_port_name
;
1920 const char *upstream_comp_name
;
1921 const bt_component
*upstream_comp
= NULL
;
1925 BT_ASSERT(upstream_port
);
1926 upstream_port_name
= bt_port_get_name(
1927 bt_port_output_as_port_const(upstream_port
));
1928 BT_ASSERT(upstream_port_name
);
1929 upstream_comp
= bt_port_borrow_component_const(
1930 bt_port_output_as_port_const(upstream_port
));
1931 if (!upstream_comp
) {
1932 BT_LOGW("Upstream port to connect is not part of a component: "
1933 "port-addr=%p, port-name=\"%s\"",
1934 upstream_port
, upstream_port_name
);
1939 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1940 BT_ASSERT(upstream_comp_name
);
1941 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1942 "port-addr=%p, port-name=\"%s\"",
1943 upstream_comp
, upstream_comp_name
,
1944 upstream_port
, upstream_port_name
);
1946 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1947 struct bt_config_connection
*cfg_conn
=
1949 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1951 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1952 upstream_comp_name
)) {
1956 if (!bt_common_star_glob_match(
1957 cfg_conn
->upstream_port_glob
->str
,
1958 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1962 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1963 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1965 BT_LOGE("Cannot connect upstream port: "
1966 "port-addr=%p, port-name=\"%s\"",
1968 upstream_port_name
);
1970 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1973 cfg_conn
->arg
->str
);
1979 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1980 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1981 upstream_port_name
);
1983 "Cannot create connection: upstream port `%s` does not match any connection\n",
1984 upstream_port_name
);
1994 bt_graph_listener_status
1995 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1996 const bt_port_output
*out_port
)
1998 const bt_component
*comp
;
1999 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
2000 bt_graph_listener_status ret
= BT_GRAPH_LISTENER_STATUS_OK
;
2002 comp
= bt_port_borrow_component_const(port
);
2003 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
2004 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
2005 comp
, comp
? bt_component_get_name(comp
) : "",
2006 port
, bt_port_get_name(port
));
2008 if (!ctx
->connect_ports
) {
2013 BT_LOGW_STR("Port has no component.");
2017 if (bt_port_is_connected(port
)) {
2018 BT_LOGW_STR("Port is already connected.");
2022 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
2023 BT_LOGF_STR("Cannot connect upstream port.");
2024 fprintf(stderr
, "Added port could not be connected: aborting\n");
2025 ret
= BT_GRAPH_LISTENER_STATUS_ERROR
;
2034 bt_graph_listener_status
graph_source_output_port_added_listener(
2035 const bt_component_source
*component
,
2036 const bt_port_output
*port
, void *data
)
2038 return graph_output_port_added_listener(data
, port
);
2042 bt_graph_listener_status
graph_filter_output_port_added_listener(
2043 const bt_component_filter
*component
,
2044 const bt_port_output
*port
, void *data
)
2046 return graph_output_port_added_listener(data
, port
);
2050 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
2056 if (ctx
->src_components
) {
2057 g_hash_table_destroy(ctx
->src_components
);
2058 ctx
->src_components
= NULL
;
2061 if (ctx
->flt_components
) {
2062 g_hash_table_destroy(ctx
->flt_components
);
2063 ctx
->flt_components
= NULL
;
2066 if (ctx
->sink_components
) {
2067 g_hash_table_destroy(ctx
->sink_components
);
2068 ctx
->sink_components
= NULL
;
2071 if (ctx
->intersections
) {
2072 g_hash_table_destroy(ctx
->intersections
);
2073 ctx
->intersections
= NULL
;
2076 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
2082 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2085 bt_graph_status status
;
2088 ctx
->connect_ports
= false;
2089 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
2090 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2091 if (!ctx
->src_components
) {
2095 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
2096 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2097 if (!ctx
->flt_components
) {
2101 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
2102 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2103 if (!ctx
->sink_components
) {
2107 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2108 ctx
->stream_intersection_mode
= true;
2109 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2110 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2111 if (!ctx
->intersections
) {
2116 ctx
->graph
= bt_graph_create();
2121 the_graph
= ctx
->graph
;
2122 status
= bt_graph_add_source_component_output_port_added_listener(
2123 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
2125 if (status
!= BT_GRAPH_STATUS_OK
) {
2126 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2130 status
= bt_graph_add_filter_component_output_port_added_listener(
2131 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
2133 if (status
!= BT_GRAPH_STATUS_OK
) {
2134 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2141 cmd_run_ctx_destroy(ctx
);
2149 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2150 struct bt_config_component
*cfg_comp
,
2151 const bt_component_class_source
*src_comp_cls
)
2155 int64_t trace_count
;
2156 const char *path
= NULL
;
2157 const bt_value
*query_result
= NULL
;
2158 const bt_value
*trace_info
= NULL
;
2159 const bt_value
*intersection_range
= NULL
;
2160 const bt_value
*intersection_begin
= NULL
;
2161 const bt_value
*intersection_end
= NULL
;
2162 const bt_value
*stream_infos
= NULL
;
2163 const 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 const bt_component_class
*comp_cls
=
2168 bt_component_class_source_as_component_class_const(src_comp_cls
);
2170 ret
= query(ctx
->cfg
, comp_cls
, "trace-info",
2171 cfg_comp
->params
, &query_result
,
2174 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2175 "comp-class-name=\"%s\"", fail_reason
,
2176 bt_component_class_get_name(comp_cls
));
2181 BT_ASSERT(query_result
);
2183 if (!bt_value_is_array(query_result
)) {
2184 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2185 "component-class-name=%s",
2186 bt_component_class_get_name(comp_cls
));
2191 trace_count
= bt_value_array_get_size(query_result
);
2192 if (trace_count
< 0) {
2197 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2199 uint64_t stream_idx
;
2200 int64_t stream_count
;
2202 trace_info
= bt_value_array_borrow_element_by_index_const(
2203 query_result
, trace_idx
);
2204 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2206 BT_LOGD_STR("Cannot retrieve trace from query result.");
2210 intersection_range
= bt_value_map_borrow_entry_value_const(
2211 trace_info
, "intersection-range-ns");
2212 if (!intersection_range
) {
2214 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2218 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
2220 if (!intersection_begin
) {
2222 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2226 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
2228 if (!intersection_end
) {
2230 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2234 begin
= bt_value_signed_integer_get(intersection_begin
);
2235 end
= bt_value_signed_integer_get(intersection_end
);
2237 if (begin
< 0 || end
< 0 || end
< begin
) {
2238 BT_LOGW("Invalid trace stream intersection values: "
2239 "intersection-range-ns:begin=%" PRId64
2240 ", intersection-range-ns:end=%" PRId64
,
2246 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
2248 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2250 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2254 stream_count
= bt_value_array_get_size(stream_infos
);
2255 if (stream_count
< 0) {
2260 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2261 const bt_value
*port_name
;
2263 port_id
= g_new0(struct port_id
, 1);
2266 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2269 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2270 if (!port_id
->instance_name
) {
2272 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2276 trace_range
= g_new0(struct trace_range
, 1);
2279 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2282 trace_range
->intersection_range_begin_ns
= begin
;
2283 trace_range
->intersection_range_end_ns
= end
;
2285 stream_info
= bt_value_array_borrow_element_by_index_const(
2286 stream_infos
, stream_idx
);
2287 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2289 BT_LOGE_STR("Cannot retrieve stream informations from trace in query result.");
2293 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2294 if (!port_name
|| !bt_value_is_string(port_name
)) {
2296 BT_LOGE_STR("Cannot retrieve port name in query result.");
2300 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2301 if (!port_id
->port_name
) {
2303 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2307 BT_LOGD("Inserting stream intersection ");
2309 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2319 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2320 bt_common_color_bold(),
2321 bt_common_color_fg_yellow(),
2322 path
? path
: "(unknown)",
2323 bt_common_color_reset());
2325 bt_value_put_ref(query_result
);
2327 g_free(trace_range
);
2332 int cmd_run_ctx_create_components_from_config_components(
2333 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2336 const void *comp_cls
= NULL
;
2337 const void *comp
= NULL
;
2340 for (i
= 0; i
< cfg_components
->len
; i
++) {
2341 struct bt_config_component
*cfg_comp
=
2342 g_ptr_array_index(cfg_components
, i
);
2345 switch (cfg_comp
->type
) {
2346 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2347 comp_cls
= find_source_component_class(
2348 cfg_comp
->plugin_name
->str
,
2349 cfg_comp
->comp_cls_name
->str
);
2351 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2352 comp_cls
= find_filter_component_class(
2353 cfg_comp
->plugin_name
->str
,
2354 cfg_comp
->comp_cls_name
->str
);
2356 case BT_COMPONENT_CLASS_TYPE_SINK
:
2357 comp_cls
= find_sink_component_class(
2358 cfg_comp
->plugin_name
->str
,
2359 cfg_comp
->comp_cls_name
->str
);
2366 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2367 "comp-cls-name=\"%s\", comp-cls-type=%d",
2368 cfg_comp
->plugin_name
->str
,
2369 cfg_comp
->comp_cls_name
->str
,
2371 fprintf(stderr
, "%s%sCannot find component class %s",
2372 bt_common_color_bold(),
2373 bt_common_color_fg_red(),
2374 bt_common_color_reset());
2375 print_plugin_comp_cls_opt(stderr
,
2376 cfg_comp
->plugin_name
->str
,
2377 cfg_comp
->comp_cls_name
->str
,
2379 fprintf(stderr
, "\n");
2383 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_VERBOSE
);
2385 switch (cfg_comp
->type
) {
2386 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2387 ret
= bt_graph_add_source_component(ctx
->graph
,
2388 comp_cls
, cfg_comp
->instance_name
->str
,
2389 cfg_comp
->params
, cfg_comp
->log_level
,
2392 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2393 ret
= bt_graph_add_filter_component(ctx
->graph
,
2394 comp_cls
, cfg_comp
->instance_name
->str
,
2395 cfg_comp
->params
, cfg_comp
->log_level
,
2398 case BT_COMPONENT_CLASS_TYPE_SINK
:
2399 ret
= bt_graph_add_sink_component(ctx
->graph
,
2400 comp_cls
, cfg_comp
->instance_name
->str
,
2401 cfg_comp
->params
, cfg_comp
->log_level
,
2409 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2410 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2412 cfg_comp
->plugin_name
->str
,
2413 cfg_comp
->comp_cls_name
->str
,
2414 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2415 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2416 bt_common_color_bold(),
2417 bt_common_color_fg_red(),
2418 cfg_comp
->instance_name
->str
,
2419 bt_common_color_reset());
2423 if (ctx
->stream_intersection_mode
&&
2424 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2425 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2431 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2432 comp
, cfg_comp
->instance_name
->str
);
2433 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2434 BT_ASSERT(quark
> 0);
2436 switch (cfg_comp
->type
) {
2437 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2438 g_hash_table_insert(ctx
->src_components
,
2439 GUINT_TO_POINTER(quark
), (void *) comp
);
2441 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2442 g_hash_table_insert(ctx
->flt_components
,
2443 GUINT_TO_POINTER(quark
), (void *) comp
);
2445 case BT_COMPONENT_CLASS_TYPE_SINK
:
2446 g_hash_table_insert(ctx
->sink_components
,
2447 GUINT_TO_POINTER(quark
), (void *) comp
);
2454 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2463 bt_object_put_ref(comp
);
2464 bt_object_put_ref(comp_cls
);
2469 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2474 * Make sure that, during this phase, our graph's "port added"
2475 * listener does not connect ports while we are creating the
2476 * components because we have a special, initial phase for
2479 ctx
->connect_ports
= false;
2481 ret
= cmd_run_ctx_create_components_from_config_components(
2482 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2488 ret
= cmd_run_ctx_create_components_from_config_components(
2489 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2495 ret
= cmd_run_ctx_create_components_from_config_components(
2496 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2506 typedef uint64_t (*output_port_count_func_t
)(const void *);
2507 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2508 const void *, uint64_t);
2511 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2512 void *comp
, output_port_count_func_t port_count_fn
,
2513 borrow_output_port_by_index_func_t port_by_index_fn
)
2519 count
= port_count_fn(comp
);
2521 for (i
= 0; i
< count
; i
++) {
2522 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2524 BT_ASSERT(upstream_port
);
2525 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2536 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2539 GHashTableIter iter
;
2540 gpointer g_name_quark
, g_comp
;
2542 ctx
->connect_ports
= true;
2543 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2545 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2546 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2547 (output_port_count_func_t
)
2548 bt_component_source_get_output_port_count
,
2549 (borrow_output_port_by_index_func_t
)
2550 bt_component_source_borrow_output_port_by_index_const
);
2556 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2558 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2559 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2560 (output_port_count_func_t
)
2561 bt_component_filter_get_output_port_count
,
2562 (borrow_output_port_by_index_func_t
)
2563 bt_component_filter_borrow_output_port_by_index_const
);
2574 const char *bt_graph_status_str(bt_graph_status status
)
2577 case BT_GRAPH_STATUS_OK
:
2578 return "BT_GRAPH_STATUS_OK";
2579 case BT_GRAPH_STATUS_END
:
2580 return "BT_GRAPH_STATUS_END";
2581 case BT_GRAPH_STATUS_AGAIN
:
2582 return "BT_GRAPH_STATUS_AGAIN";
2583 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
2584 return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION";
2585 case BT_GRAPH_STATUS_CANCELED
:
2586 return "BT_GRAPH_STATUS_CANCELED";
2587 case BT_GRAPH_STATUS_ERROR
:
2588 return "BT_GRAPH_STATUS_ERROR";
2589 case BT_GRAPH_STATUS_NOMEM
:
2590 return "BT_GRAPH_STATUS_NOMEM";
2597 int cmd_run(struct bt_config
*cfg
)
2600 struct cmd_run_ctx ctx
= { 0 };
2602 /* Initialize the command's context and the graph object */
2603 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2604 BT_LOGE_STR("Cannot initialize the command's context.");
2605 fprintf(stderr
, "Cannot initialize the command's context\n");
2610 BT_LOGI_STR("Canceled by user before creating components.");
2614 BT_LOGI_STR("Creating components.");
2616 /* Create the requested component instances */
2617 if (cmd_run_ctx_create_components(&ctx
)) {
2618 BT_LOGE_STR("Cannot create components.");
2619 fprintf(stderr
, "Cannot create components\n");
2624 BT_LOGI_STR("Canceled by user before connecting components.");
2628 BT_LOGI_STR("Connecting components.");
2630 /* Connect the initially visible component ports */
2631 if (cmd_run_ctx_connect_ports(&ctx
)) {
2632 BT_LOGE_STR("Cannot connect initial component ports.");
2633 fprintf(stderr
, "Cannot connect initial component ports\n");
2638 BT_LOGI_STR("Canceled by user before running the graph.");
2642 BT_LOGI_STR("Running the graph.");
2646 bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2649 * Reset console in case something messed with console
2650 * codes during the graph's execution.
2652 printf("%s", bt_common_color_reset());
2654 fprintf(stderr
, "%s", bt_common_color_reset());
2655 BT_LOGV("bt_graph_run() returned: status=%s",
2656 bt_graph_status_str(graph_status
));
2658 switch (graph_status
) {
2659 case BT_GRAPH_STATUS_OK
:
2661 case BT_GRAPH_STATUS_CANCELED
:
2662 BT_LOGI_STR("Graph was canceled by user.");
2664 case BT_GRAPH_STATUS_AGAIN
:
2665 if (bt_graph_is_canceled(ctx
.graph
)) {
2666 BT_LOGI_STR("Graph was canceled by user.");
2670 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2671 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2673 cfg
->cmd_data
.run
.retry_duration_us
);
2675 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2676 if (bt_graph_is_canceled(ctx
.graph
)) {
2677 BT_LOGI_STR("Graph was canceled by user.");
2683 case BT_GRAPH_STATUS_END
:
2686 BT_LOGE_STR("Graph failed to complete successfully");
2687 fprintf(stderr
, "Graph failed to complete successfully\n");
2700 cmd_run_ctx_destroy(&ctx
);
2705 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2707 const char *env_clash
;
2709 if (!cfg
->command_name
) {
2713 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2714 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2718 if (g_file_test(cfg
->command_name
,
2719 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2720 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2722 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2724 fprintf(stderr
, "\n");
2725 fprintf(stderr
, " babeltrace2 convert %s [OPTIONS]\n",
2731 void init_log_level(void)
2733 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2737 void set_auto_log_levels(struct bt_config
*cfg
)
2739 const char **env_var_name
;
2742 * Override the configuration's default log level if
2743 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2744 * are found for backward compatibility with legacy Babetrace 1.
2746 if (getenv("BABELTRACE_DEBUG") &&
2747 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2748 cfg
->log_level
= BT_LOG_VERBOSE
;
2749 } else if (getenv("BABELTRACE_VERBOSE") &&
2750 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2751 cfg
->log_level
= BT_LOG_INFO
;
2755 * Set log levels according to --debug or --verbose. For
2756 * backward compatibility, --debug is more verbose than
2759 * --verbose: INFO log level
2760 * --debug: VERBOSE log level (includes DEBUG, which is
2761 * is less verbose than VERBOSE in the internal
2762 * logging framework)
2764 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2766 bt_logging_set_global_level(BT_LOG_INFO
);
2767 } else if (cfg
->debug
) {
2768 bt_logging_set_global_level(BT_LOG_VERBOSE
);
2771 * Set library's default log level if not
2772 * explicitly specified.
2774 bt_logging_set_global_level(cfg
->log_level
);
2778 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2780 bt_cli_log_level
= BT_LOG_INFO
;
2781 } else if (cfg
->debug
) {
2782 bt_cli_log_level
= BT_LOG_VERBOSE
;
2785 * Set CLI's default log level if not explicitly
2788 bt_cli_log_level
= cfg
->log_level
;
2792 env_var_name
= log_level_env_var_names
;
2794 while (*env_var_name
) {
2795 if (!getenv(*env_var_name
)) {
2797 g_setenv(*env_var_name
, "I", 1);
2798 } else if (cfg
->debug
) {
2799 g_setenv(*env_var_name
, "V", 1);
2801 char val
[2] = { 0 };
2804 * Set module's default log level if not
2805 * explicitly specified.
2807 val
[0] = bt_log_get_letter_from_level(
2809 g_setenv(*env_var_name
, val
, 1);
2817 int main(int argc
, const char **argv
)
2821 struct bt_config
*cfg
;
2824 set_signal_handler();
2826 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2829 /* Quit without errors; typically usage/version */
2831 BT_LOGI_STR("Quitting without errors.");
2836 BT_LOGE("Command-line error: retcode=%d", retcode
);
2841 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2842 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2847 set_auto_log_levels(cfg
);
2850 if (cfg
->command_needs_plugins
) {
2851 ret
= load_all_plugins(cfg
->plugin_paths
);
2853 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2859 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2860 cfg
->command
, cfg
->command_name
);
2862 switch (cfg
->command
) {
2863 case BT_CONFIG_COMMAND_RUN
:
2866 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2867 ret
= cmd_list_plugins(cfg
);
2869 case BT_CONFIG_COMMAND_HELP
:
2870 ret
= cmd_help(cfg
);
2872 case BT_CONFIG_COMMAND_QUERY
:
2873 ret
= cmd_query(cfg
);
2875 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2876 ret
= cmd_print_ctf_metadata(cfg
);
2878 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2879 ret
= cmd_print_lttng_live_sessions(cfg
);
2882 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2886 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2887 cfg
->command
, cfg
->command_name
, ret
);
2888 warn_command_name_and_directory_clash(cfg
);
2889 retcode
= ret
? 1 : 0;
2892 BT_OBJECT_PUT_REF_AND_RESET(cfg
);