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_PYTHON_BT2_LOG_LEVEL",
54 "BABELTRACE_SINK_CTF_FS_LOG_LEVEL",
55 "BABELTRACE_SINK_TEXT_DETAILS_LOG_LEVEL",
56 "BABELTRACE_SRC_CTF_FS_LOG_LEVEL",
57 "BABELTRACE_SRC_CTF_LTTNG_LIVE_LOG_LEVEL",
61 /* Application's processing graph (weak) */
62 static bt_graph
*the_graph
;
63 static bt_query_executor
*the_query_executor
;
64 static bool canceled
= false;
66 GPtrArray
*loaded_plugins
;
73 BOOL WINAPI
signal_handler(DWORD signal
) {
75 bt_graph_cancel(the_graph
);
84 void set_signal_handler(void)
86 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
87 BT_LOGE("Failed to set the Ctrl+C handler.");
91 #else /* __MINGW32__ */
94 void signal_handler(int signum
)
96 if (signum
!= SIGINT
) {
101 bt_graph_cancel(the_graph
);
104 if (the_query_executor
) {
105 bt_query_executor_cancel(the_query_executor
);
112 void set_signal_handler(void)
114 struct sigaction new_action
, old_action
;
116 new_action
.sa_handler
= signal_handler
;
117 sigemptyset(&new_action
.sa_mask
);
118 new_action
.sa_flags
= 0;
119 sigaction(SIGINT
, NULL
, &old_action
);
121 if (old_action
.sa_handler
!= SIG_IGN
) {
122 sigaction(SIGINT
, &new_action
, NULL
);
126 #endif /* __MINGW32__ */
129 void init_static_data(void)
131 loaded_plugins
= g_ptr_array_new_with_free_func(
132 (GDestroyNotify
) bt_object_put_ref
);
136 void fini_static_data(void)
138 g_ptr_array_free(loaded_plugins
, TRUE
);
142 int create_the_query_executor(void)
146 the_query_executor
= bt_query_executor_create();
147 if (!the_query_executor
) {
148 BT_LOGE_STR("Cannot create a query executor.");
156 void destroy_the_query_executor(void)
158 BT_QUERY_EXECUTOR_PUT_REF_AND_RESET(the_query_executor
);
162 int query(struct bt_config
*cfg
, const bt_component_class
*comp_cls
,
163 const char *obj
, const bt_value
*params
,
164 const bt_value
**user_result
, const char **fail_reason
)
166 const bt_value
*result
= NULL
;
167 bt_query_executor_status status
;
168 *fail_reason
= "unknown error";
171 BT_ASSERT(fail_reason
);
172 BT_ASSERT(user_result
);
173 ret
= create_the_query_executor();
175 /* create_the_query_executor() logs errors */
180 BT_LOGI("Canceled by user before executing the query: "
181 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
182 "query-obj=\"%s\"", comp_cls
,
183 bt_component_class_get_name(comp_cls
), obj
);
184 *fail_reason
= "canceled by user";
189 status
= bt_query_executor_query(the_query_executor
,
190 comp_cls
, obj
, params
, cfg
->log_level
, &result
);
192 case BT_QUERY_EXECUTOR_STATUS_OK
:
194 case BT_QUERY_EXECUTOR_STATUS_AGAIN
:
196 const uint64_t sleep_time_us
= 100000;
198 /* Wait 100 ms and retry */
199 BT_LOGD("Got BT_QUERY_EXECUTOR_STATUS_AGAIN: sleeping: "
200 "time-us=%" PRIu64
, sleep_time_us
);
202 if (usleep(sleep_time_us
)) {
203 if (bt_query_executor_is_canceled(the_query_executor
)) {
204 BT_LOGI("Query was canceled by user: "
205 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
206 "query-obj=\"%s\"", comp_cls
,
207 bt_component_class_get_name(comp_cls
),
209 *fail_reason
= "canceled by user";
216 case BT_QUERY_EXECUTOR_STATUS_CANCELED
:
217 *fail_reason
= "canceled by user";
219 case BT_QUERY_EXECUTOR_STATUS_ERROR
:
221 case BT_QUERY_EXECUTOR_STATUS_INVALID_OBJECT
:
222 *fail_reason
= "invalid or unknown query object";
224 case BT_QUERY_EXECUTOR_STATUS_INVALID_PARAMS
:
225 *fail_reason
= "invalid query parameters";
227 case BT_QUERY_EXECUTOR_STATUS_UNSUPPORTED
:
228 *fail_reason
= "unsupported action";
230 case BT_QUERY_EXECUTOR_STATUS_NOMEM
:
231 *fail_reason
= "not enough memory";
234 BT_LOGF("Unknown query status: status=%d", status
);
240 *user_result
= result
;
248 destroy_the_query_executor();
249 bt_value_put_ref(result
);
254 const bt_plugin
*find_plugin(const char *name
)
257 const bt_plugin
*plugin
= NULL
;
260 BT_LOGI("Finding plugin: name=\"%s\"", name
);
262 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
263 plugin
= g_ptr_array_index(loaded_plugins
, i
);
265 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
273 BT_LOGI("Found plugin: name=\"%s\", plugin-addr=%p",
276 BT_LOGI("Cannot find plugin: name=\"%s\"", name
);
279 bt_plugin_get_ref(plugin
);
283 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
284 const bt_plugin
*, const char *);
287 const void *find_component_class_from_plugin(const char *plugin_name
,
288 const char *comp_class_name
,
289 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
291 const void *comp_class
= NULL
;
292 const bt_plugin
*plugin
;
294 BT_LOGI("Finding component class: plugin-name=\"%s\", "
295 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
297 plugin
= find_plugin(plugin_name
);
302 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
303 bt_object_get_ref(comp_class
);
304 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
308 BT_LOGI("Found component class: plugin-name=\"%s\", "
309 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
311 BT_LOGI("Cannot find source component class: "
312 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
313 plugin_name
, comp_class_name
);
320 const bt_component_class_source
*find_source_component_class(
321 const char *plugin_name
, const char *comp_class_name
)
323 return (const void *) find_component_class_from_plugin(
324 plugin_name
, comp_class_name
,
325 (plugin_borrow_comp_cls_func_t
)
326 bt_plugin_borrow_source_component_class_by_name_const
);
330 const bt_component_class_filter
*find_filter_component_class(
331 const char *plugin_name
, const char *comp_class_name
)
333 return (const void *) find_component_class_from_plugin(
334 plugin_name
, comp_class_name
,
335 (plugin_borrow_comp_cls_func_t
)
336 bt_plugin_borrow_filter_component_class_by_name_const
);
340 const bt_component_class_sink
*find_sink_component_class(
341 const char *plugin_name
, const char *comp_class_name
)
343 return (const void *) find_component_class_from_plugin(plugin_name
,
345 (plugin_borrow_comp_cls_func_t
)
346 bt_plugin_borrow_sink_component_class_by_name_const
);
350 const bt_component_class
*find_component_class(const char *plugin_name
,
351 const char *comp_class_name
,
352 bt_component_class_type comp_class_type
)
354 const bt_component_class
*comp_cls
= NULL
;
356 switch (comp_class_type
) {
357 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
358 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
360 case BT_COMPONENT_CLASS_TYPE_FILTER
:
361 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
363 case BT_COMPONENT_CLASS_TYPE_SINK
:
364 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
374 void print_indent(FILE *fp
, size_t indent
)
378 for (i
= 0; i
< indent
; i
++) {
384 const char *component_type_str(bt_component_class_type type
)
387 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
389 case BT_COMPONENT_CLASS_TYPE_SINK
:
391 case BT_COMPONENT_CLASS_TYPE_FILTER
:
399 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
400 const char *comp_cls_name
, bt_component_class_type type
)
402 GString
*shell_plugin_name
= NULL
;
403 GString
*shell_comp_cls_name
= NULL
;
405 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
406 if (!shell_plugin_name
) {
410 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
411 if (!shell_comp_cls_name
) {
415 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
416 bt_common_color_bold(),
417 bt_common_color_fg_cyan(),
418 component_type_str(type
),
419 bt_common_color_fg_default(),
420 bt_common_color_fg_blue(),
421 shell_plugin_name
->str
,
422 bt_common_color_fg_default(),
423 bt_common_color_fg_yellow(),
424 shell_comp_cls_name
->str
,
425 bt_common_color_reset());
428 if (shell_plugin_name
) {
429 g_string_free(shell_plugin_name
, TRUE
);
432 if (shell_comp_cls_name
) {
433 g_string_free(shell_comp_cls_name
, TRUE
);
438 void print_value(FILE *, const bt_value
*, size_t);
441 void print_value_rec(FILE *, const bt_value
*, size_t);
443 struct print_map_value_data
{
449 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
452 struct print_map_value_data
*print_map_value_data
= data
;
454 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
455 fprintf(print_map_value_data
->fp
, "%s: ", key
);
458 if (bt_value_is_array(object
) &&
459 bt_value_array_is_empty(object
)) {
460 fprintf(print_map_value_data
->fp
, "[ ]\n");
464 if (bt_value_is_map(object
) &&
465 bt_value_map_is_empty(object
)) {
466 fprintf(print_map_value_data
->fp
, "{ }\n");
470 if (bt_value_is_array(object
) ||
471 bt_value_is_map(object
)) {
472 fprintf(print_map_value_data
->fp
, "\n");
475 print_value_rec(print_map_value_data
->fp
, object
,
476 print_map_value_data
->indent
+ 2);
481 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
495 switch (bt_value_get_type(value
)) {
496 case BT_VALUE_TYPE_NULL
:
497 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
498 bt_common_color_reset());
500 case BT_VALUE_TYPE_BOOL
:
501 bool_val
= bt_value_bool_get(value
);
502 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
503 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
504 bt_common_color_reset());
506 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
507 uint_val
= bt_value_unsigned_integer_get(value
);
508 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
509 bt_common_color_fg_red(), uint_val
,
510 bt_common_color_reset());
512 case BT_VALUE_TYPE_SIGNED_INTEGER
:
513 int_val
= bt_value_signed_integer_get(value
);
514 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
515 bt_common_color_fg_red(), int_val
,
516 bt_common_color_reset());
518 case BT_VALUE_TYPE_REAL
:
519 dbl_val
= bt_value_real_get(value
);
520 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
521 bt_common_color_fg_red(), dbl_val
,
522 bt_common_color_reset());
524 case BT_VALUE_TYPE_STRING
:
525 str_val
= bt_value_string_get(value
);
526 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
527 bt_common_color_fg_green(), str_val
,
528 bt_common_color_reset());
530 case BT_VALUE_TYPE_ARRAY
:
531 size
= bt_value_array_get_size(value
);
537 print_indent(fp
, indent
);
538 fprintf(fp
, "[ ]\n");
542 for (i
= 0; i
< size
; i
++) {
543 const bt_value
*element
=
544 bt_value_array_borrow_element_by_index_const(
550 print_indent(fp
, indent
);
553 if (bt_value_is_array(element
) &&
554 bt_value_array_is_empty(element
)) {
555 fprintf(fp
, "[ ]\n");
559 if (bt_value_is_map(element
) &&
560 bt_value_map_is_empty(element
)) {
561 fprintf(fp
, "{ }\n");
565 if (bt_value_is_array(element
) ||
566 bt_value_is_map(element
)) {
570 print_value_rec(fp
, element
, indent
+ 2);
573 case BT_VALUE_TYPE_MAP
:
575 struct print_map_value_data data
= {
580 if (bt_value_map_is_empty(value
)) {
581 print_indent(fp
, indent
);
582 fprintf(fp
, "{ }\n");
586 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
595 BT_LOGE("Error printing value of type %s.",
596 bt_common_value_type_string(bt_value_get_type(value
)));
600 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
602 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
603 print_indent(fp
, indent
);
606 print_value_rec(fp
, value
, indent
);
610 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
612 fprintf(stderr
, " ");
613 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
614 bt_config_component
->comp_cls_name
->str
,
615 bt_config_component
->type
);
616 fprintf(stderr
, ":\n");
618 if (bt_config_component
->instance_name
->len
> 0) {
619 fprintf(stderr
, " Name: %s\n",
620 bt_config_component
->instance_name
->str
);
623 fprintf(stderr
, " Parameters:\n");
624 print_value(stderr
, bt_config_component
->params
, 8);
628 void print_bt_config_components(GPtrArray
*array
)
632 for (i
= 0; i
< array
->len
; i
++) {
633 struct bt_config_component
*cfg_component
=
634 bt_config_get_component(array
, i
);
635 print_bt_config_component(cfg_component
);
636 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
641 void print_plugin_paths(const bt_value
*plugin_paths
)
643 fprintf(stderr
, " Plugin paths:\n");
644 print_value(stderr
, plugin_paths
, 4);
648 void print_cfg_run(struct bt_config
*cfg
)
652 print_plugin_paths(cfg
->plugin_paths
);
653 fprintf(stderr
, " Source component instances:\n");
654 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
656 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
657 fprintf(stderr
, " Filter component instances:\n");
658 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
661 fprintf(stderr
, " Sink component instances:\n");
662 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
663 fprintf(stderr
, " Connections:\n");
665 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
666 struct bt_config_connection
*cfg_connection
=
667 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
670 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
671 cfg_connection
->upstream_comp_name
->str
,
672 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
673 cfg_connection
->upstream_port_glob
->str
,
674 cfg_connection
->downstream_comp_name
->str
,
675 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
676 cfg_connection
->downstream_port_glob
->str
);
681 void print_cfg_list_plugins(struct bt_config
*cfg
)
683 print_plugin_paths(cfg
->plugin_paths
);
687 void print_cfg_help(struct bt_config
*cfg
)
689 print_plugin_paths(cfg
->plugin_paths
);
693 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
695 print_plugin_paths(cfg
->plugin_paths
);
696 fprintf(stderr
, " Path: %s\n",
697 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
701 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
703 print_plugin_paths(cfg
->plugin_paths
);
704 fprintf(stderr
, " URL: %s\n",
705 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
709 void print_cfg_query(struct bt_config
*cfg
)
711 print_plugin_paths(cfg
->plugin_paths
);
712 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
713 fprintf(stderr
, " Component class:\n");
714 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
718 void print_cfg(struct bt_config
*cfg
)
720 if (!BT_LOG_ON_INFO
) {
724 BT_LOGI_STR("CLI configuration:");
725 BT_LOGI(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
726 BT_LOGI(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
728 switch (cfg
->command
) {
729 case BT_CONFIG_COMMAND_RUN
:
732 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
733 print_cfg_list_plugins(cfg
);
735 case BT_CONFIG_COMMAND_HELP
:
738 case BT_CONFIG_COMMAND_QUERY
:
739 print_cfg_query(cfg
);
741 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
742 print_cfg_print_ctf_metadata(cfg
);
744 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
745 print_cfg_print_lttng_live_sessions(cfg
);
753 void add_to_loaded_plugins(const bt_plugin_set
*plugin_set
)
758 count
= bt_plugin_set_get_plugin_count(plugin_set
);
759 BT_ASSERT(count
>= 0);
761 for (i
= 0; i
< count
; i
++) {
762 const bt_plugin
*plugin
=
763 bt_plugin_set_borrow_plugin_by_index_const(plugin_set
, i
);
764 const bt_plugin
*loaded_plugin
=
765 find_plugin(bt_plugin_get_name(plugin
));
770 BT_LOGI("Not using plugin: another one already exists with the same name: "
771 "plugin-name=\"%s\", plugin-path=\"%s\", "
772 "existing-plugin-path=\"%s\"",
773 bt_plugin_get_name(plugin
),
774 bt_plugin_get_path(plugin
),
775 bt_plugin_get_path(loaded_plugin
));
776 bt_plugin_put_ref(loaded_plugin
);
778 /* Add to global array. */
779 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
780 bt_plugin_get_name(plugin
));
781 bt_plugin_get_ref(plugin
);
782 g_ptr_array_add(loaded_plugins
, (void *) plugin
);
788 int load_dynamic_plugins(const bt_value
*plugin_paths
)
790 int nr_paths
, i
, ret
= 0;
792 nr_paths
= bt_value_array_get_size(plugin_paths
);
794 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
799 BT_LOGI_STR("Loading dynamic plugins.");
801 for (i
= 0; i
< nr_paths
; i
++) {
802 const bt_value
*plugin_path_value
= NULL
;
803 const char *plugin_path
;
804 const bt_plugin_set
*plugin_set
;
807 bt_value_array_borrow_element_by_index_const(
809 plugin_path
= bt_value_string_get(plugin_path_value
);
812 * Skip this if the directory does not exist because
813 * bt_plugin_find_all_from_dir() expects an existing
816 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
817 BT_LOGI("Skipping nonexistent directory path: "
818 "path=\"%s\"", plugin_path
);
822 plugin_set
= bt_plugin_find_all_from_dir(plugin_path
, false);
824 BT_LOGI("Unable to load dynamic plugins from directory: "
825 "path=\"%s\"", plugin_path
);
829 add_to_loaded_plugins(plugin_set
);
830 bt_plugin_set_put_ref(plugin_set
);
837 int load_static_plugins(void)
840 const bt_plugin_set
*plugin_set
;
842 BT_LOGI("Loading static plugins.");
843 plugin_set
= bt_plugin_find_all_from_static();
845 BT_LOGE("Unable to load static plugins.");
850 add_to_loaded_plugins(plugin_set
);
851 bt_plugin_set_put_ref(plugin_set
);
857 int load_all_plugins(const bt_value
*plugin_paths
)
861 if (load_dynamic_plugins(plugin_paths
)) {
866 if (load_static_plugins()) {
871 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
878 void print_plugin_info(const bt_plugin
*plugin
)
880 unsigned int major
, minor
, patch
;
882 bt_property_availability version_avail
;
883 const char *plugin_name
;
887 const char *plugin_description
;
889 plugin_name
= bt_plugin_get_name(plugin
);
890 path
= bt_plugin_get_path(plugin
);
891 author
= bt_plugin_get_author(plugin
);
892 license
= bt_plugin_get_license(plugin
);
893 plugin_description
= bt_plugin_get_description(plugin
);
894 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
896 printf("%s%s%s%s:\n", bt_common_color_bold(),
897 bt_common_color_fg_blue(), plugin_name
,
898 bt_common_color_reset());
900 printf(" %sPath%s: %s\n", bt_common_color_bold(),
901 bt_common_color_reset(), path
);
906 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
907 printf(" %sVersion%s: %u.%u.%u",
908 bt_common_color_bold(), bt_common_color_reset(),
909 major
, minor
, patch
);
918 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
919 bt_common_color_reset(),
920 plugin_description
? plugin_description
: "(None)");
921 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
922 bt_common_color_reset(), author
? author
: "(Unknown)");
923 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
924 bt_common_color_reset(),
925 license
? license
: "(Unknown)");
929 int cmd_query(struct bt_config
*cfg
)
932 const bt_component_class
*comp_cls
= NULL
;
933 const bt_value
*results
= NULL
;
934 const char *fail_reason
= NULL
;
936 comp_cls
= find_component_class(
937 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
938 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
939 cfg
->cmd_data
.query
.cfg_component
->type
);
941 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
942 "comp-cls-name=\"%s\", comp-cls-type=%d",
943 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
944 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
945 cfg
->cmd_data
.query
.cfg_component
->type
);
946 fprintf(stderr
, "%s%sCannot find component class %s",
947 bt_common_color_bold(),
948 bt_common_color_fg_red(),
949 bt_common_color_reset());
950 print_plugin_comp_cls_opt(stderr
,
951 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
952 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
953 cfg
->cmd_data
.query
.cfg_component
->type
);
954 fprintf(stderr
, "\n");
959 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
960 cfg
->cmd_data
.query
.cfg_component
->params
,
961 &results
, &fail_reason
);
966 print_value(stdout
, results
, 0);
970 BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", "
971 "comp-cls-name=\"%s\", comp-cls-type=%d "
972 "object=\"%s\"", fail_reason
,
973 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
974 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
975 cfg
->cmd_data
.query
.cfg_component
->type
,
976 cfg
->cmd_data
.query
.object
->str
);
977 fprintf(stderr
, "%s%sFailed to query info to %s",
978 bt_common_color_bold(),
979 bt_common_color_fg_red(),
980 bt_common_color_reset());
981 print_plugin_comp_cls_opt(stderr
,
982 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
983 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
984 cfg
->cmd_data
.query
.cfg_component
->type
);
985 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
986 bt_common_color_bold(),
987 bt_common_color_fg_red(),
988 cfg
->cmd_data
.query
.object
->str
,
990 bt_common_color_reset());
994 bt_component_class_put_ref(comp_cls
);
995 bt_value_put_ref(results
);
1000 void print_component_class_help(const char *plugin_name
,
1001 const bt_component_class
*comp_cls
)
1003 const char *comp_class_name
=
1004 bt_component_class_get_name(comp_cls
);
1005 const char *comp_class_description
=
1006 bt_component_class_get_description(comp_cls
);
1007 const char *comp_class_help
=
1008 bt_component_class_get_help(comp_cls
);
1009 bt_component_class_type type
=
1010 bt_component_class_get_type(comp_cls
);
1012 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
1014 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1015 bt_common_color_reset(),
1016 comp_class_description
? comp_class_description
: "(None)");
1018 if (comp_class_help
) {
1019 printf("\n%s\n", comp_class_help
);
1024 int cmd_help(struct bt_config
*cfg
)
1027 const bt_plugin
*plugin
= NULL
;
1028 const bt_component_class
*needed_comp_cls
= NULL
;
1030 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1032 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
1033 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1034 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
1035 bt_common_color_bold(), bt_common_color_fg_red(),
1036 bt_common_color_fg_blue(),
1037 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1038 bt_common_color_reset());
1043 print_plugin_info(plugin
);
1044 printf(" %sSource component classes%s: %d\n",
1045 bt_common_color_bold(),
1046 bt_common_color_reset(),
1047 (int) bt_plugin_get_source_component_class_count(plugin
));
1048 printf(" %sFilter component classes%s: %d\n",
1049 bt_common_color_bold(),
1050 bt_common_color_reset(),
1051 (int) bt_plugin_get_filter_component_class_count(plugin
));
1052 printf(" %sSink component classes%s: %d\n",
1053 bt_common_color_bold(),
1054 bt_common_color_reset(),
1055 (int) bt_plugin_get_sink_component_class_count(plugin
));
1057 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
1058 /* Plugin help only */
1062 needed_comp_cls
= find_component_class(
1063 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1064 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1065 cfg
->cmd_data
.help
.cfg_component
->type
);
1066 if (!needed_comp_cls
) {
1067 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1068 "comp-cls-name=\"%s\", comp-cls-type=%d",
1069 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1070 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1071 cfg
->cmd_data
.help
.cfg_component
->type
);
1072 fprintf(stderr
, "\n%s%sCannot find component class %s",
1073 bt_common_color_bold(),
1074 bt_common_color_fg_red(),
1075 bt_common_color_reset());
1076 print_plugin_comp_cls_opt(stderr
,
1077 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1078 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1079 cfg
->cmd_data
.help
.cfg_component
->type
);
1080 fprintf(stderr
, "\n");
1086 print_component_class_help(
1087 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1091 bt_component_class_put_ref(needed_comp_cls
);
1092 bt_plugin_put_ref(plugin
);
1096 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
1098 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
1101 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
1102 const char *cc_type_name
, uint64_t count
,
1103 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
1104 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
1109 printf(" %s%s component classes%s: (none)\n",
1110 bt_common_color_bold(),
1112 bt_common_color_reset());
1115 printf(" %s%s component classes%s:\n",
1116 bt_common_color_bold(),
1118 bt_common_color_reset());
1121 for (i
= 0; i
< count
; i
++) {
1122 const bt_component_class
*comp_class
=
1123 spec_comp_cls_borrow_comp_cls_func(
1124 borrow_comp_cls_by_index_func(plugin
, i
));
1125 const char *comp_class_name
=
1126 bt_component_class_get_name(comp_class
);
1127 const char *comp_class_description
=
1128 bt_component_class_get_description(comp_class
);
1129 bt_component_class_type type
=
1130 bt_component_class_get_type(comp_class
);
1133 print_plugin_comp_cls_opt(stdout
,
1134 bt_plugin_get_name(plugin
), comp_class_name
,
1137 if (comp_class_description
) {
1138 printf(": %s", comp_class_description
);
1149 int cmd_list_plugins(struct bt_config
*cfg
)
1152 int plugins_count
, component_classes_count
= 0, i
;
1154 printf("From the following plugin paths:\n\n");
1155 print_value(stdout
, cfg
->plugin_paths
, 2);
1157 plugins_count
= loaded_plugins
->len
;
1158 if (plugins_count
== 0) {
1159 printf("No plugins found.\n");
1163 for (i
= 0; i
< plugins_count
; i
++) {
1164 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1166 component_classes_count
+=
1167 bt_plugin_get_source_component_class_count(plugin
) +
1168 bt_plugin_get_filter_component_class_count(plugin
) +
1169 bt_plugin_get_sink_component_class_count(plugin
);
1172 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1173 bt_common_color_bold(),
1174 component_classes_count
,
1175 bt_common_color_reset(),
1176 bt_common_color_bold(),
1178 bt_common_color_reset());
1180 for (i
= 0; i
< plugins_count
; i
++) {
1181 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1184 print_plugin_info(plugin
);
1185 cmd_list_plugins_print_component_classes(plugin
, "Source",
1186 bt_plugin_get_source_component_class_count(plugin
),
1187 (plugin_borrow_comp_cls_by_index_func_t
)
1188 bt_plugin_borrow_source_component_class_by_index_const
,
1189 (spec_comp_cls_borrow_comp_cls_func_t
)
1190 bt_component_class_source_as_component_class
);
1191 cmd_list_plugins_print_component_classes(plugin
, "Filter",
1192 bt_plugin_get_filter_component_class_count(plugin
),
1193 (plugin_borrow_comp_cls_by_index_func_t
)
1194 bt_plugin_borrow_filter_component_class_by_index_const
,
1195 (spec_comp_cls_borrow_comp_cls_func_t
)
1196 bt_component_class_filter_as_component_class
);
1197 cmd_list_plugins_print_component_classes(plugin
, "Sink",
1198 bt_plugin_get_sink_component_class_count(plugin
),
1199 (plugin_borrow_comp_cls_by_index_func_t
)
1200 bt_plugin_borrow_sink_component_class_by_index_const
,
1201 (spec_comp_cls_borrow_comp_cls_func_t
)
1202 bt_component_class_sink_as_component_class
);
1210 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1213 const bt_component_class
*comp_cls
= NULL
;
1214 const bt_value
*results
= NULL
;
1215 bt_value
*params
= NULL
;
1216 const bt_value
*map
= NULL
;
1217 const bt_value
*v
= NULL
;
1218 static const char * const plugin_name
= "ctf";
1219 static const char * const comp_cls_name
= "lttng-live";
1220 static const bt_component_class_type comp_cls_type
=
1221 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1222 int64_t array_size
, i
;
1223 const char *fail_reason
= NULL
;
1224 FILE *out_stream
= stdout
;
1226 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1227 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1230 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1231 "comp-cls-name=\"%s\", comp-cls-type=%d",
1232 plugin_name
, comp_cls_name
,
1233 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1234 fprintf(stderr
, "%s%sCannot find component class %s",
1235 bt_common_color_bold(),
1236 bt_common_color_fg_red(),
1237 bt_common_color_reset());
1238 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1239 comp_cls_name
, comp_cls_type
);
1240 fprintf(stderr
, "\n");
1244 params
= bt_value_map_create();
1249 ret
= bt_value_map_insert_string_entry(params
, "url",
1250 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1255 ret
= query(cfg
, comp_cls
, "sessions", params
,
1256 &results
, &fail_reason
);
1263 if (!bt_value_is_array(results
)) {
1264 BT_LOGE_STR("Expecting an array for sessions query.");
1265 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1266 bt_common_color_bold(),
1267 bt_common_color_fg_red(),
1268 bt_common_color_reset());
1272 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1274 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1278 BT_LOGE_ERRNO("Cannot open file for writing",
1280 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1285 array_size
= bt_value_array_get_size(results
);
1286 for (i
= 0; i
< array_size
; i
++) {
1287 const char *url_text
;
1288 int64_t timer_us
, streams
, clients
;
1290 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1292 BT_LOGE_STR("Unexpected empty array entry.");
1295 if (!bt_value_is_map(map
)) {
1296 BT_LOGE_STR("Unexpected entry type.");
1300 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1302 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1305 url_text
= bt_value_string_get(v
);
1306 fprintf(out_stream
, "%s", url_text
);
1307 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1309 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1312 timer_us
= bt_value_signed_integer_get(v
);
1313 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1314 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1316 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1319 streams
= bt_value_signed_integer_get(v
);
1320 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1321 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1323 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1326 clients
= bt_value_signed_integer_get(v
);
1327 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1333 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1334 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1335 bt_common_color_bold(),
1336 bt_common_color_fg_red(),
1338 bt_common_color_reset());
1344 bt_value_put_ref(results
);
1345 bt_value_put_ref(params
);
1346 bt_component_class_put_ref(comp_cls
);
1348 if (out_stream
&& out_stream
!= stdout
) {
1349 int fclose_ret
= fclose(out_stream
);
1352 BT_LOGE_ERRNO("Cannot close file stream",
1354 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1362 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1365 const bt_component_class
*comp_cls
= NULL
;
1366 const bt_value
*results
= NULL
;
1367 bt_value
*params
= NULL
;
1368 const bt_value
*metadata_text_value
= NULL
;
1369 const char *metadata_text
= NULL
;
1370 static const char * const plugin_name
= "ctf";
1371 static const char * const comp_cls_name
= "fs";
1372 static const bt_component_class_type comp_cls_type
=
1373 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1374 const char *fail_reason
= NULL
;
1375 FILE *out_stream
= stdout
;
1377 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1378 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1381 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1382 "comp-cls-name=\"%s\", comp-cls-type=%d",
1383 plugin_name
, comp_cls_name
,
1384 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1385 fprintf(stderr
, "%s%sCannot find component class %s",
1386 bt_common_color_bold(),
1387 bt_common_color_fg_red(),
1388 bt_common_color_reset());
1389 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1390 comp_cls_name
, comp_cls_type
);
1391 fprintf(stderr
, "\n");
1396 params
= bt_value_map_create();
1402 ret
= bt_value_map_insert_string_entry(params
, "path",
1403 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1409 ret
= query(cfg
, comp_cls
, "metadata-info",
1410 params
, &results
, &fail_reason
);
1415 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1417 if (!metadata_text_value
) {
1418 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1423 metadata_text
= bt_value_string_get(metadata_text_value
);
1425 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1427 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1431 BT_LOGE_ERRNO("Cannot open file for writing",
1433 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1438 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1440 BT_LOGE("Cannot write whole metadata text to output stream: "
1451 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1452 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1453 bt_common_color_bold(),
1454 bt_common_color_fg_red(),
1456 bt_common_color_reset());
1459 bt_value_put_ref(results
);
1460 bt_value_put_ref(params
);
1461 bt_component_class_put_ref(comp_cls
);
1463 if (out_stream
&& out_stream
!= stdout
) {
1464 int fclose_ret
= fclose(out_stream
);
1467 BT_LOGE_ERRNO("Cannot close file stream",
1469 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1477 char *instance_name
;
1481 struct trace_range
{
1482 uint64_t intersection_range_begin_ns
;
1483 uint64_t intersection_range_end_ns
;
1487 guint
port_id_hash(gconstpointer v
)
1489 const struct port_id
*id
= v
;
1491 BT_ASSERT(id
->instance_name
);
1492 BT_ASSERT(id
->port_name
);
1494 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1498 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1500 const struct port_id
*id1
= v1
;
1501 const struct port_id
*id2
= v2
;
1503 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1504 !strcmp(id1
->port_name
, id2
->port_name
);
1508 void port_id_destroy(gpointer data
)
1510 struct port_id
*id
= data
;
1512 free(id
->instance_name
);
1513 free(id
->port_name
);
1518 void trace_range_destroy(gpointer data
)
1523 struct cmd_run_ctx
{
1525 GHashTable
*src_components
;
1528 GHashTable
*flt_components
;
1531 GHashTable
*sink_components
;
1537 struct bt_config
*cfg
;
1541 bool stream_intersection_mode
;
1544 * Association of struct port_id -> struct trace_range.
1546 GHashTable
*intersections
;
1549 /* Returns a timestamp of the form "(-)s.ns" */
1551 char *s_from_ns(int64_t ns
)
1556 int64_t ts_sec_abs
, ts_nsec_abs
;
1557 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1558 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1560 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1561 is_negative
= false;
1562 ts_sec_abs
= ts_sec
;
1563 ts_nsec_abs
= ts_nsec
;
1564 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1565 is_negative
= false;
1566 ts_sec_abs
= ts_sec
- 1;
1567 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1568 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1570 ts_sec_abs
= ts_sec
;
1571 ts_nsec_abs
= -ts_nsec
;
1572 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1574 ts_sec_abs
= -(ts_sec
+ 1);
1575 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1576 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1578 ts_sec_abs
= -ts_sec
;
1579 ts_nsec_abs
= ts_nsec
;
1580 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1582 ts_sec_abs
= -ts_sec
;
1583 ts_nsec_abs
= -ts_nsec
;
1586 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1587 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1595 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1596 struct cmd_run_ctx
*ctx
,
1597 const bt_component
*upstream_comp
,
1598 const bt_port_output
*out_upstream_port
,
1599 struct bt_config_connection
*cfg_conn
)
1601 typedef uint64_t (*input_port_count_func_t
)(void *);
1602 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1603 const void *, uint64_t);
1604 const bt_port
*upstream_port
=
1605 bt_port_output_as_port_const(out_upstream_port
);
1608 GQuark downstreamp_comp_name_quark
;
1609 void *downstream_comp
;
1610 uint64_t downstream_port_count
;
1612 input_port_count_func_t port_count_fn
;
1613 borrow_input_port_by_index_func_t port_by_index_fn
;
1614 bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1615 bool insert_trimmer
= false;
1616 bt_value
*trimmer_params
= NULL
;
1617 char *intersection_begin
= NULL
;
1618 char *intersection_end
= NULL
;
1619 const bt_component_filter
*trimmer
= NULL
;
1620 const bt_component_class_filter
*trimmer_class
= NULL
;
1621 const bt_port_input
*trimmer_input
= NULL
;
1622 const bt_port_output
*trimmer_output
= NULL
;
1624 if (ctx
->intersections
&&
1625 bt_component_get_class_type(upstream_comp
) ==
1626 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1627 struct trace_range
*range
;
1628 struct port_id port_id
= {
1629 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1630 .port_name
= (char *) bt_port_get_name(upstream_port
)
1633 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1637 range
= (struct trace_range
*) g_hash_table_lookup(
1638 ctx
->intersections
, &port_id
);
1640 bt_value_status status
;
1642 intersection_begin
= s_from_ns(
1643 range
->intersection_range_begin_ns
);
1644 intersection_end
= s_from_ns(
1645 range
->intersection_range_end_ns
);
1646 if (!intersection_begin
|| !intersection_end
) {
1647 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1651 insert_trimmer
= true;
1652 trimmer_params
= bt_value_map_create();
1653 if (!trimmer_params
) {
1657 status
= bt_value_map_insert_string_entry(
1658 trimmer_params
, "begin", intersection_begin
);
1659 if (status
!= BT_VALUE_STATUS_OK
) {
1662 status
= bt_value_map_insert_string_entry(
1664 "end", intersection_end
);
1665 if (status
!= BT_VALUE_STATUS_OK
) {
1670 trimmer_class
= find_filter_component_class("utils", "trimmer");
1671 if (!trimmer_class
) {
1676 BT_LOGI("Connecting upstream port to the next available downstream port: "
1677 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1678 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1679 upstream_port
, bt_port_get_name(upstream_port
),
1680 cfg_conn
->downstream_comp_name
->str
,
1681 cfg_conn
->arg
->str
);
1682 downstreamp_comp_name_quark
= g_quark_from_string(
1683 cfg_conn
->downstream_comp_name
->str
);
1684 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1685 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1686 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1687 port_count_fn
= (input_port_count_func_t
)
1688 bt_component_filter_get_input_port_count
;
1689 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1690 bt_component_filter_borrow_input_port_by_index_const
;
1692 if (!downstream_comp
) {
1693 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1694 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1695 port_count_fn
= (input_port_count_func_t
)
1696 bt_component_sink_get_input_port_count
;
1697 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1698 bt_component_sink_borrow_input_port_by_index_const
;
1701 if (!downstream_comp
) {
1702 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1703 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1704 cfg_conn
->arg
->str
);
1705 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1706 cfg_conn
->arg
->str
);
1710 downstream_port_count
= port_count_fn(downstream_comp
);
1712 for (i
= 0; i
< downstream_port_count
; i
++) {
1713 const bt_port_input
*in_downstream_port
=
1714 port_by_index_fn(downstream_comp
, i
);
1715 const bt_port
*downstream_port
=
1716 bt_port_input_as_port_const(in_downstream_port
);
1717 const char *upstream_port_name
;
1718 const char *downstream_port_name
;
1720 BT_ASSERT(downstream_port
);
1722 /* Skip port if it's already connected. */
1723 if (bt_port_is_connected(downstream_port
)) {
1724 BT_LOGI("Skipping downstream port: already connected: "
1725 "port-addr=%p, port-name=\"%s\"",
1727 bt_port_get_name(downstream_port
));
1731 downstream_port_name
= bt_port_get_name(downstream_port
);
1732 BT_ASSERT(downstream_port_name
);
1733 upstream_port_name
= bt_port_get_name(upstream_port
);
1734 BT_ASSERT(upstream_port_name
);
1736 if (!bt_common_star_glob_match(
1737 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1738 downstream_port_name
, SIZE_MAX
)) {
1742 if (insert_trimmer
) {
1744 * In order to insert the trimmer between the
1745 * two components that were being connected, we
1746 * create a connection configuration entry which
1747 * describes a connection from the trimmer's
1748 * output to the original input that was being
1751 * Hence, the creation of the trimmer will cause
1752 * the graph "new port" listener to establish
1753 * all downstream connections as its output port
1754 * is connected. We will then establish the
1755 * connection between the original upstream
1756 * source and the trimmer.
1758 char *trimmer_name
= NULL
;
1759 bt_graph_status graph_status
;
1761 ret
= asprintf(&trimmer_name
,
1762 "stream-intersection-trimmer-%s",
1763 upstream_port_name
);
1769 ctx
->connect_ports
= false;
1770 graph_status
= bt_graph_add_filter_component(
1771 ctx
->graph
, trimmer_class
, trimmer_name
,
1772 trimmer_params
, ctx
->cfg
->log_level
,
1775 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1781 bt_component_filter_borrow_input_port_by_index_const(
1783 if (!trimmer_input
) {
1787 bt_component_filter_borrow_output_port_by_index_const(
1789 if (!trimmer_output
) {
1794 * Replace the current downstream port by the trimmer's
1797 in_downstream_port
= trimmer_input
;
1799 bt_port_input_as_port_const(in_downstream_port
);
1800 downstream_port_name
= bt_port_get_name(
1802 BT_ASSERT(downstream_port_name
);
1805 /* We have a winner! */
1806 status
= bt_graph_connect_ports(ctx
->graph
,
1807 out_upstream_port
, in_downstream_port
, NULL
);
1808 downstream_port
= NULL
;
1810 case BT_GRAPH_STATUS_OK
:
1812 case BT_GRAPH_STATUS_CANCELED
:
1813 BT_LOGI_STR("Graph was canceled by user.");
1814 status
= BT_GRAPH_STATUS_OK
;
1816 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1817 BT_LOGE("A component refused a connection to one of its ports: "
1818 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1819 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1820 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1821 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1823 upstream_comp
, bt_component_get_name(upstream_comp
),
1824 upstream_port
, bt_port_get_name(upstream_port
),
1825 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1826 downstream_port
, downstream_port_name
,
1827 cfg_conn
->arg
->str
);
1829 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1830 bt_port_get_name(upstream_port
),
1831 downstream_port_name
,
1832 cfg_conn
->arg
->str
);
1835 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1836 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1837 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1838 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1839 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1841 upstream_comp
, bt_component_get_name(upstream_comp
),
1842 upstream_port
, bt_port_get_name(upstream_port
),
1843 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1844 downstream_port
, downstream_port_name
,
1845 cfg_conn
->arg
->str
);
1847 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1848 bt_port_get_name(upstream_port
),
1849 downstream_port_name
,
1850 cfg_conn
->arg
->str
);
1854 BT_LOGI("Connected component ports: "
1855 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1856 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1857 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1858 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1860 upstream_comp
, bt_component_get_name(upstream_comp
),
1861 upstream_port
, bt_port_get_name(upstream_port
),
1862 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1863 downstream_port
, downstream_port_name
,
1864 cfg_conn
->arg
->str
);
1866 if (insert_trimmer
) {
1868 * The first connection, from the source to the trimmer,
1869 * has been done. We now connect the trimmer to the
1870 * original downstream port.
1872 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1874 bt_component_filter_as_component_const(trimmer
),
1875 trimmer_output
, cfg_conn
);
1879 ctx
->connect_ports
= true;
1883 * We found a matching downstream port: the search is
1889 /* No downstream port found */
1890 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1891 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1892 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1893 upstream_port
, bt_port_get_name(upstream_port
),
1894 cfg_conn
->downstream_comp_name
->str
,
1895 cfg_conn
->arg
->str
);
1897 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1898 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1904 free(intersection_begin
);
1905 free(intersection_end
);
1906 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1907 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1908 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1913 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1914 const bt_port_output
*upstream_port
)
1917 const char *upstream_port_name
;
1918 const char *upstream_comp_name
;
1919 const bt_component
*upstream_comp
= NULL
;
1923 BT_ASSERT(upstream_port
);
1924 upstream_port_name
= bt_port_get_name(
1925 bt_port_output_as_port_const(upstream_port
));
1926 BT_ASSERT(upstream_port_name
);
1927 upstream_comp
= bt_port_borrow_component_const(
1928 bt_port_output_as_port_const(upstream_port
));
1929 if (!upstream_comp
) {
1930 BT_LOGW("Upstream port to connect is not part of a component: "
1931 "port-addr=%p, port-name=\"%s\"",
1932 upstream_port
, upstream_port_name
);
1937 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1938 BT_ASSERT(upstream_comp_name
);
1939 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1940 "port-addr=%p, port-name=\"%s\"",
1941 upstream_comp
, upstream_comp_name
,
1942 upstream_port
, upstream_port_name
);
1944 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1945 struct bt_config_connection
*cfg_conn
=
1947 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1949 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1950 upstream_comp_name
)) {
1954 if (!bt_common_star_glob_match(
1955 cfg_conn
->upstream_port_glob
->str
,
1956 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1960 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1961 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1963 BT_LOGE("Cannot connect upstream port: "
1964 "port-addr=%p, port-name=\"%s\"",
1966 upstream_port_name
);
1968 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1971 cfg_conn
->arg
->str
);
1977 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1978 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1979 upstream_port_name
);
1981 "Cannot create connection: upstream port `%s` does not match any connection\n",
1982 upstream_port_name
);
1992 bt_graph_listener_status
1993 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1994 const bt_port_output
*out_port
)
1996 const bt_component
*comp
;
1997 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1998 bt_graph_listener_status ret
= BT_GRAPH_LISTENER_STATUS_OK
;
2000 comp
= bt_port_borrow_component_const(port
);
2001 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
2002 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
2003 comp
, comp
? bt_component_get_name(comp
) : "",
2004 port
, bt_port_get_name(port
));
2006 if (!ctx
->connect_ports
) {
2011 BT_LOGW_STR("Port has no component.");
2015 if (bt_port_is_connected(port
)) {
2016 BT_LOGW_STR("Port is already connected.");
2020 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
2021 BT_LOGF_STR("Cannot connect upstream port.");
2022 fprintf(stderr
, "Added port could not be connected: aborting\n");
2023 ret
= BT_GRAPH_LISTENER_STATUS_ERROR
;
2032 bt_graph_listener_status
graph_source_output_port_added_listener(
2033 const bt_component_source
*component
,
2034 const bt_port_output
*port
, void *data
)
2036 return graph_output_port_added_listener(data
, port
);
2040 bt_graph_listener_status
graph_filter_output_port_added_listener(
2041 const bt_component_filter
*component
,
2042 const bt_port_output
*port
, void *data
)
2044 return graph_output_port_added_listener(data
, port
);
2048 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
2054 if (ctx
->src_components
) {
2055 g_hash_table_destroy(ctx
->src_components
);
2056 ctx
->src_components
= NULL
;
2059 if (ctx
->flt_components
) {
2060 g_hash_table_destroy(ctx
->flt_components
);
2061 ctx
->flt_components
= NULL
;
2064 if (ctx
->sink_components
) {
2065 g_hash_table_destroy(ctx
->sink_components
);
2066 ctx
->sink_components
= NULL
;
2069 if (ctx
->intersections
) {
2070 g_hash_table_destroy(ctx
->intersections
);
2071 ctx
->intersections
= NULL
;
2074 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
2080 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2083 bt_graph_status status
;
2086 ctx
->connect_ports
= false;
2087 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
2088 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2089 if (!ctx
->src_components
) {
2093 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
2094 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2095 if (!ctx
->flt_components
) {
2099 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
2100 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2101 if (!ctx
->sink_components
) {
2105 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2106 ctx
->stream_intersection_mode
= true;
2107 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2108 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2109 if (!ctx
->intersections
) {
2114 ctx
->graph
= bt_graph_create();
2119 the_graph
= ctx
->graph
;
2120 status
= bt_graph_add_source_component_output_port_added_listener(
2121 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
2123 if (status
!= BT_GRAPH_STATUS_OK
) {
2124 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2128 status
= bt_graph_add_filter_component_output_port_added_listener(
2129 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
2131 if (status
!= BT_GRAPH_STATUS_OK
) {
2132 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2139 cmd_run_ctx_destroy(ctx
);
2147 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2148 struct bt_config_component
*cfg_comp
,
2149 const bt_component_class_source
*src_comp_cls
)
2153 int64_t trace_count
;
2154 const char *path
= NULL
;
2155 const bt_value
*query_result
= NULL
;
2156 const bt_value
*trace_info
= NULL
;
2157 const bt_value
*intersection_range
= NULL
;
2158 const bt_value
*intersection_begin
= NULL
;
2159 const bt_value
*intersection_end
= NULL
;
2160 const bt_value
*stream_infos
= NULL
;
2161 const bt_value
*stream_info
= NULL
;
2162 struct port_id
*port_id
= NULL
;
2163 struct trace_range
*trace_range
= NULL
;
2164 const char *fail_reason
= NULL
;
2165 const bt_component_class
*comp_cls
=
2166 bt_component_class_source_as_component_class_const(src_comp_cls
);
2168 ret
= query(ctx
->cfg
, comp_cls
, "trace-info",
2169 cfg_comp
->params
, &query_result
,
2172 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2173 "comp-class-name=\"%s\"", fail_reason
,
2174 bt_component_class_get_name(comp_cls
));
2179 BT_ASSERT(query_result
);
2181 if (!bt_value_is_array(query_result
)) {
2182 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2183 "component-class-name=%s",
2184 bt_component_class_get_name(comp_cls
));
2189 trace_count
= bt_value_array_get_size(query_result
);
2190 if (trace_count
< 0) {
2195 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2197 uint64_t stream_idx
;
2198 int64_t stream_count
;
2200 trace_info
= bt_value_array_borrow_element_by_index_const(
2201 query_result
, trace_idx
);
2202 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2204 BT_LOGD_STR("Cannot retrieve trace from query result.");
2208 intersection_range
= bt_value_map_borrow_entry_value_const(
2209 trace_info
, "intersection-range-ns");
2210 if (!intersection_range
) {
2212 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2216 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
2218 if (!intersection_begin
) {
2220 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2224 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
2226 if (!intersection_end
) {
2228 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2232 begin
= bt_value_signed_integer_get(intersection_begin
);
2233 end
= bt_value_signed_integer_get(intersection_end
);
2235 if (begin
< 0 || end
< 0 || end
< begin
) {
2236 BT_LOGW("Invalid trace stream intersection values: "
2237 "intersection-range-ns:begin=%" PRId64
2238 ", intersection-range-ns:end=%" PRId64
,
2244 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
2246 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2248 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2252 stream_count
= bt_value_array_get_size(stream_infos
);
2253 if (stream_count
< 0) {
2258 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2259 const bt_value
*port_name
;
2261 port_id
= g_new0(struct port_id
, 1);
2264 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2267 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2268 if (!port_id
->instance_name
) {
2270 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2274 trace_range
= g_new0(struct trace_range
, 1);
2277 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2280 trace_range
->intersection_range_begin_ns
= begin
;
2281 trace_range
->intersection_range_end_ns
= end
;
2283 stream_info
= bt_value_array_borrow_element_by_index_const(
2284 stream_infos
, stream_idx
);
2285 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2287 BT_LOGE_STR("Cannot retrieve stream informations from trace in query result.");
2291 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2292 if (!port_name
|| !bt_value_is_string(port_name
)) {
2294 BT_LOGE_STR("Cannot retrieve port name in query result.");
2298 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2299 if (!port_id
->port_name
) {
2301 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2305 BT_LOGD("Inserting stream intersection ");
2307 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2317 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2318 bt_common_color_bold(),
2319 bt_common_color_fg_yellow(),
2320 path
? path
: "(unknown)",
2321 bt_common_color_reset());
2323 bt_value_put_ref(query_result
);
2325 g_free(trace_range
);
2330 int cmd_run_ctx_create_components_from_config_components(
2331 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2334 const void *comp_cls
= NULL
;
2335 const void *comp
= NULL
;
2338 for (i
= 0; i
< cfg_components
->len
; i
++) {
2339 struct bt_config_component
*cfg_comp
=
2340 g_ptr_array_index(cfg_components
, i
);
2343 switch (cfg_comp
->type
) {
2344 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2345 comp_cls
= find_source_component_class(
2346 cfg_comp
->plugin_name
->str
,
2347 cfg_comp
->comp_cls_name
->str
);
2349 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2350 comp_cls
= find_filter_component_class(
2351 cfg_comp
->plugin_name
->str
,
2352 cfg_comp
->comp_cls_name
->str
);
2354 case BT_COMPONENT_CLASS_TYPE_SINK
:
2355 comp_cls
= find_sink_component_class(
2356 cfg_comp
->plugin_name
->str
,
2357 cfg_comp
->comp_cls_name
->str
);
2364 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2365 "comp-cls-name=\"%s\", comp-cls-type=%d",
2366 cfg_comp
->plugin_name
->str
,
2367 cfg_comp
->comp_cls_name
->str
,
2369 fprintf(stderr
, "%s%sCannot find component class %s",
2370 bt_common_color_bold(),
2371 bt_common_color_fg_red(),
2372 bt_common_color_reset());
2373 print_plugin_comp_cls_opt(stderr
,
2374 cfg_comp
->plugin_name
->str
,
2375 cfg_comp
->comp_cls_name
->str
,
2377 fprintf(stderr
, "\n");
2381 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_VERBOSE
);
2383 switch (cfg_comp
->type
) {
2384 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2385 ret
= bt_graph_add_source_component(ctx
->graph
,
2386 comp_cls
, cfg_comp
->instance_name
->str
,
2387 cfg_comp
->params
, cfg_comp
->log_level
,
2390 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2391 ret
= bt_graph_add_filter_component(ctx
->graph
,
2392 comp_cls
, cfg_comp
->instance_name
->str
,
2393 cfg_comp
->params
, cfg_comp
->log_level
,
2396 case BT_COMPONENT_CLASS_TYPE_SINK
:
2397 ret
= bt_graph_add_sink_component(ctx
->graph
,
2398 comp_cls
, cfg_comp
->instance_name
->str
,
2399 cfg_comp
->params
, cfg_comp
->log_level
,
2407 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2408 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2410 cfg_comp
->plugin_name
->str
,
2411 cfg_comp
->comp_cls_name
->str
,
2412 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2413 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2414 bt_common_color_bold(),
2415 bt_common_color_fg_red(),
2416 cfg_comp
->instance_name
->str
,
2417 bt_common_color_reset());
2421 if (ctx
->stream_intersection_mode
&&
2422 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2423 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2429 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2430 comp
, cfg_comp
->instance_name
->str
);
2431 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2432 BT_ASSERT(quark
> 0);
2434 switch (cfg_comp
->type
) {
2435 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2436 g_hash_table_insert(ctx
->src_components
,
2437 GUINT_TO_POINTER(quark
), (void *) comp
);
2439 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2440 g_hash_table_insert(ctx
->flt_components
,
2441 GUINT_TO_POINTER(quark
), (void *) comp
);
2443 case BT_COMPONENT_CLASS_TYPE_SINK
:
2444 g_hash_table_insert(ctx
->sink_components
,
2445 GUINT_TO_POINTER(quark
), (void *) comp
);
2452 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2461 bt_object_put_ref(comp
);
2462 bt_object_put_ref(comp_cls
);
2467 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2472 * Make sure that, during this phase, our graph's "port added"
2473 * listener does not connect ports while we are creating the
2474 * components because we have a special, initial phase for
2477 ctx
->connect_ports
= false;
2479 ret
= cmd_run_ctx_create_components_from_config_components(
2480 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2486 ret
= cmd_run_ctx_create_components_from_config_components(
2487 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2493 ret
= cmd_run_ctx_create_components_from_config_components(
2494 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2504 typedef uint64_t (*output_port_count_func_t
)(const void *);
2505 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2506 const void *, uint64_t);
2509 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2510 void *comp
, output_port_count_func_t port_count_fn
,
2511 borrow_output_port_by_index_func_t port_by_index_fn
)
2517 count
= port_count_fn(comp
);
2519 for (i
= 0; i
< count
; i
++) {
2520 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2522 BT_ASSERT(upstream_port
);
2523 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2534 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2537 GHashTableIter iter
;
2538 gpointer g_name_quark
, g_comp
;
2540 ctx
->connect_ports
= true;
2541 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2543 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2544 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2545 (output_port_count_func_t
)
2546 bt_component_source_get_output_port_count
,
2547 (borrow_output_port_by_index_func_t
)
2548 bt_component_source_borrow_output_port_by_index_const
);
2554 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2556 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2557 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2558 (output_port_count_func_t
)
2559 bt_component_filter_get_output_port_count
,
2560 (borrow_output_port_by_index_func_t
)
2561 bt_component_filter_borrow_output_port_by_index_const
);
2572 const char *bt_graph_status_str(bt_graph_status status
)
2575 case BT_GRAPH_STATUS_OK
:
2576 return "BT_GRAPH_STATUS_OK";
2577 case BT_GRAPH_STATUS_END
:
2578 return "BT_GRAPH_STATUS_END";
2579 case BT_GRAPH_STATUS_AGAIN
:
2580 return "BT_GRAPH_STATUS_AGAIN";
2581 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
2582 return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION";
2583 case BT_GRAPH_STATUS_CANCELED
:
2584 return "BT_GRAPH_STATUS_CANCELED";
2585 case BT_GRAPH_STATUS_ERROR
:
2586 return "BT_GRAPH_STATUS_ERROR";
2587 case BT_GRAPH_STATUS_NOMEM
:
2588 return "BT_GRAPH_STATUS_NOMEM";
2595 int cmd_run(struct bt_config
*cfg
)
2598 struct cmd_run_ctx ctx
= { 0 };
2600 /* Initialize the command's context and the graph object */
2601 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2602 BT_LOGE_STR("Cannot initialize the command's context.");
2603 fprintf(stderr
, "Cannot initialize the command's context\n");
2608 BT_LOGI_STR("Canceled by user before creating components.");
2612 BT_LOGI_STR("Creating components.");
2614 /* Create the requested component instances */
2615 if (cmd_run_ctx_create_components(&ctx
)) {
2616 BT_LOGE_STR("Cannot create components.");
2617 fprintf(stderr
, "Cannot create components\n");
2622 BT_LOGI_STR("Canceled by user before connecting components.");
2626 BT_LOGI_STR("Connecting components.");
2628 /* Connect the initially visible component ports */
2629 if (cmd_run_ctx_connect_ports(&ctx
)) {
2630 BT_LOGE_STR("Cannot connect initial component ports.");
2631 fprintf(stderr
, "Cannot connect initial component ports\n");
2636 BT_LOGI_STR("Canceled by user before running the graph.");
2640 BT_LOGI_STR("Running the graph.");
2644 bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2647 * Reset console in case something messed with console
2648 * codes during the graph's execution.
2650 printf("%s", bt_common_color_reset());
2652 fprintf(stderr
, "%s", bt_common_color_reset());
2653 BT_LOGV("bt_graph_run() returned: status=%s",
2654 bt_graph_status_str(graph_status
));
2656 switch (graph_status
) {
2657 case BT_GRAPH_STATUS_OK
:
2659 case BT_GRAPH_STATUS_CANCELED
:
2660 BT_LOGI_STR("Graph was canceled by user.");
2662 case BT_GRAPH_STATUS_AGAIN
:
2663 if (bt_graph_is_canceled(ctx
.graph
)) {
2664 BT_LOGI_STR("Graph was canceled by user.");
2668 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2669 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2671 cfg
->cmd_data
.run
.retry_duration_us
);
2673 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2674 if (bt_graph_is_canceled(ctx
.graph
)) {
2675 BT_LOGI_STR("Graph was canceled by user.");
2681 case BT_GRAPH_STATUS_END
:
2684 BT_LOGE_STR("Graph failed to complete successfully");
2685 fprintf(stderr
, "Graph failed to complete successfully\n");
2698 cmd_run_ctx_destroy(&ctx
);
2703 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2705 const char *env_clash
;
2707 if (!cfg
->command_name
) {
2711 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2712 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2716 if (g_file_test(cfg
->command_name
,
2717 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2718 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2720 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2722 fprintf(stderr
, "\n");
2723 fprintf(stderr
, " babeltrace2 convert %s [OPTIONS]\n",
2729 void init_log_level(void)
2731 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2735 void set_auto_log_levels(struct bt_config
*cfg
)
2737 const char **env_var_name
;
2740 * Override the configuration's default log level if
2741 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2742 * are found for backward compatibility with legacy Babetrace 1.
2744 if (getenv("BABELTRACE_DEBUG") &&
2745 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2746 cfg
->log_level
= BT_LOG_VERBOSE
;
2747 } else if (getenv("BABELTRACE_VERBOSE") &&
2748 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2749 cfg
->log_level
= BT_LOG_INFO
;
2753 * Set log levels according to --debug or --verbose. For
2754 * backward compatibility, --debug is more verbose than
2757 * --verbose: INFO log level
2758 * --debug: VERBOSE log level (includes DEBUG, which is
2759 * is less verbose than VERBOSE in the internal
2760 * logging framework)
2762 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2764 bt_logging_set_global_level(BT_LOG_INFO
);
2765 } else if (cfg
->debug
) {
2766 bt_logging_set_global_level(BT_LOG_VERBOSE
);
2769 * Set library's default log level if not
2770 * explicitly specified.
2772 bt_logging_set_global_level(cfg
->log_level
);
2776 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2778 bt_cli_log_level
= BT_LOG_INFO
;
2779 } else if (cfg
->debug
) {
2780 bt_cli_log_level
= BT_LOG_VERBOSE
;
2783 * Set CLI's default log level if not explicitly
2786 bt_cli_log_level
= cfg
->log_level
;
2790 env_var_name
= log_level_env_var_names
;
2792 while (*env_var_name
) {
2793 if (!getenv(*env_var_name
)) {
2795 g_setenv(*env_var_name
, "I", 1);
2796 } else if (cfg
->debug
) {
2797 g_setenv(*env_var_name
, "V", 1);
2799 char val
[2] = { 0 };
2802 * Set module's default log level if not
2803 * explicitly specified.
2805 val
[0] = bt_log_get_letter_from_level(
2807 g_setenv(*env_var_name
, val
, 1);
2815 int main(int argc
, const char **argv
)
2819 struct bt_config
*cfg
;
2822 set_signal_handler();
2824 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2827 /* Quit without errors; typically usage/version */
2829 BT_LOGI_STR("Quitting without errors.");
2834 BT_LOGE("Command-line error: retcode=%d", retcode
);
2839 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2840 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2845 set_auto_log_levels(cfg
);
2848 if (cfg
->command_needs_plugins
) {
2849 ret
= load_all_plugins(cfg
->plugin_paths
);
2851 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2857 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2858 cfg
->command
, cfg
->command_name
);
2860 switch (cfg
->command
) {
2861 case BT_CONFIG_COMMAND_RUN
:
2864 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2865 ret
= cmd_list_plugins(cfg
);
2867 case BT_CONFIG_COMMAND_HELP
:
2868 ret
= cmd_help(cfg
);
2870 case BT_CONFIG_COMMAND_QUERY
:
2871 ret
= cmd_query(cfg
);
2873 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2874 ret
= cmd_print_ctf_metadata(cfg
);
2876 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2877 ret
= cmd_print_lttng_live_sessions(cfg
);
2880 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2884 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2885 cfg
->command
, cfg
->command_name
, ret
);
2886 warn_command_name_and_directory_clash(cfg
);
2887 retcode
= ret
? 1 : 0;
2890 BT_OBJECT_PUT_REF_AND_RESET(cfg
);