4 * Babeltrace Trace Converter
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define BT_LOG_TAG "CLI"
32 #include <babeltrace/babeltrace.h>
33 #include <babeltrace/common-internal.h>
43 #include "babeltrace-cfg.h"
44 #include "babeltrace-cfg-cli-args.h"
45 #include "babeltrace-cfg-cli-args-default.h"
47 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
48 #define ENV_BABELTRACE_CLI_LOG_LEVEL "BABELTRACE_CLI_LOG_LEVEL"
49 #define NSEC_PER_SEC 1000000000LL
52 * Known environment variable names for the log levels of the project's
55 static const char* log_level_env_var_names
[] = {
56 "BABELTRACE_COMMON_LOG_LEVEL",
57 "BABELTRACE_COMPAT_LOG_LEVEL",
58 "BABELTRACE_PLUGIN_CTF_BTR_LOG_LEVEL",
59 "BABELTRACE_SINK_CTF_FS_LOG_LEVEL",
60 "BABELTRACE_SRC_CTF_FS_LOG_LEVEL",
61 "BABELTRACE_SRC_CTF_LTTNG_LIVE_LOG_LEVEL",
62 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
63 "BABELTRACE_PLUGIN_CTF_NOTIF_ITER_LOG_LEVEL",
64 "BABELTRACE_PLUGIN_CTFCOPYTRACE_LIB_LOG_LEVEL",
65 "BABELTRACE_FLT_LTTNG_UTILS_DEBUG_INFO_LOG_LEVEL",
66 "BABELTRACE_SRC_TEXT_DMESG_LOG_LEVEL",
67 "BABELTRACE_SINK_TEXT_PRETTY_LOG_LEVEL",
68 "BABELTRACE_FLT_UTILS_MUXER_LOG_LEVEL",
69 "BABELTRACE_FLT_UTILS_TRIMMER_LOG_LEVEL",
70 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
71 "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
75 /* Application's processing graph (weak) */
76 static struct bt_graph
*the_graph
;
77 static struct bt_query_executor
*the_query_executor
;
78 static bool canceled
= false;
80 GPtrArray
*loaded_plugins
;
86 BOOL WINAPI
signal_handler(DWORD signal
) {
88 bt_graph_cancel(the_graph
);
97 void set_signal_handler(void)
99 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
100 BT_LOGE("Failed to set the ctrl+c handler.");
103 #else /* __MINGW32__ */
105 void signal_handler(int signum
)
107 if (signum
!= SIGINT
) {
112 bt_graph_cancel(the_graph
);
115 if (the_query_executor
) {
116 bt_query_executor_cancel(the_query_executor
);
123 void set_signal_handler(void)
125 struct sigaction new_action
, old_action
;
127 new_action
.sa_handler
= signal_handler
;
128 sigemptyset(&new_action
.sa_mask
);
129 new_action
.sa_flags
= 0;
130 sigaction(SIGINT
, NULL
, &old_action
);
132 if (old_action
.sa_handler
!= SIG_IGN
) {
133 sigaction(SIGINT
, &new_action
, NULL
);
136 #endif /* __MINGW32__ */
139 void init_static_data(void)
141 loaded_plugins
= g_ptr_array_new_with_free_func(bt_object_put_ref
);
145 void fini_static_data(void)
147 g_ptr_array_free(loaded_plugins
, TRUE
);
151 int create_the_query_executor(void)
155 the_query_executor
= bt_query_executor_create();
156 if (!the_query_executor
) {
157 BT_LOGE_STR("Cannot create a query executor.");
165 void destroy_the_query_executor(void)
167 BT_OBJECT_PUT_REF_AND_RESET(the_query_executor
);
171 int query(struct bt_component_class
*comp_cls
, const char *obj
,
172 struct bt_value
*params
, struct bt_value
**user_result
,
173 const char **fail_reason
)
175 struct bt_value
*result
= NULL
;
176 enum bt_query_status status
;
177 *fail_reason
= "unknown error";
180 BT_ASSERT(fail_reason
);
181 BT_ASSERT(user_result
);
182 ret
= create_the_query_executor();
184 /* create_the_query_executor() logs errors */
189 BT_LOGI("Canceled by user before executing the query: "
190 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
191 "query-obj=\"%s\"", comp_cls
,
192 bt_component_class_get_name(comp_cls
), obj
);
193 *fail_reason
= "canceled by user";
198 status
= bt_query_executor_query(the_query_executor
, comp_cls
,
199 obj
, params
, &result
);
201 case BT_QUERY_STATUS_OK
:
203 case BT_QUERY_STATUS_AGAIN
:
205 const uint64_t sleep_time_us
= 100000;
207 /* Wait 100 ms and retry */
208 BT_LOGV("Got BT_QUERY_STATUS_AGAIN: sleeping: "
209 "time-us=%" PRIu64
, sleep_time_us
);
211 if (usleep(sleep_time_us
)) {
212 if (bt_query_executor_is_canceled(the_query_executor
)) {
213 BT_LOGI("Query was canceled by user: "
214 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
215 "query-obj=\"%s\"", comp_cls
,
216 bt_component_class_get_name(comp_cls
),
218 *fail_reason
= "canceled by user";
225 case BT_QUERY_STATUS_EXECUTOR_CANCELED
:
226 *fail_reason
= "canceled by user";
228 case BT_QUERY_STATUS_ERROR
:
229 case BT_QUERY_STATUS_INVALID
:
231 case BT_QUERY_STATUS_INVALID_OBJECT
:
232 *fail_reason
= "invalid or unknown query object";
234 case BT_QUERY_STATUS_INVALID_PARAMS
:
235 *fail_reason
= "invalid query parameters";
237 case BT_QUERY_STATUS_NOMEM
:
238 *fail_reason
= "not enough memory";
241 BT_LOGF("Unknown query status: status=%d", status
);
247 *user_result
= result
;
255 destroy_the_query_executor();
256 bt_object_put_ref(result
);
261 struct bt_plugin
*find_plugin(const char *name
)
264 struct bt_plugin
*plugin
= NULL
;
267 BT_LOGD("Finding plugin: name=\"%s\"", name
);
269 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
270 plugin
= g_ptr_array_index(loaded_plugins
, i
);
272 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
279 if (BT_LOG_ON_DEBUG
) {
281 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
283 BT_LOGD("Cannot find plugin.");
287 return bt_object_get_ref(plugin
);
291 struct bt_component_class
*find_component_class(const char *plugin_name
,
292 const char *comp_class_name
,
293 enum bt_component_class_type comp_class_type
)
295 struct bt_component_class
*comp_class
= NULL
;
296 struct bt_plugin
*plugin
;
298 BT_LOGD("Finding component class: plugin-name=\"%s\", "
299 "comp-cls-name=\"%s\", comp-cls-type=%d",
300 plugin_name
, comp_class_name
, comp_class_type
);
302 plugin
= find_plugin(plugin_name
);
308 comp_class
= bt_plugin_get_component_class_by_name_and_type(plugin
,
309 comp_class_name
, comp_class_type
);
310 BT_OBJECT_PUT_REF_AND_RESET(plugin
);
313 if (BT_LOG_ON_DEBUG
) {
315 BT_LOGD("Found component class: comp-cls-addr=%p",
318 BT_LOGD("Cannot find component class.");
326 void print_indent(FILE *fp
, size_t indent
)
330 for (i
= 0; i
< indent
; i
++) {
336 const char *component_type_str(enum bt_component_class_type type
)
339 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
341 case BT_COMPONENT_CLASS_TYPE_SINK
:
343 case BT_COMPONENT_CLASS_TYPE_FILTER
:
345 case BT_COMPONENT_CLASS_TYPE_UNKNOWN
:
352 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
353 const char *comp_cls_name
, enum bt_component_class_type type
)
355 GString
*shell_plugin_name
= NULL
;
356 GString
*shell_comp_cls_name
= NULL
;
358 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
359 if (!shell_plugin_name
) {
363 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
364 if (!shell_comp_cls_name
) {
368 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
369 bt_common_color_bold(),
370 bt_common_color_fg_cyan(),
371 component_type_str(type
),
372 bt_common_color_fg_default(),
373 bt_common_color_fg_blue(),
374 shell_plugin_name
->str
,
375 bt_common_color_fg_default(),
376 bt_common_color_fg_yellow(),
377 shell_comp_cls_name
->str
,
378 bt_common_color_reset());
381 if (shell_plugin_name
) {
382 g_string_free(shell_plugin_name
, TRUE
);
385 if (shell_comp_cls_name
) {
386 g_string_free(shell_comp_cls_name
, TRUE
);
391 void print_value(FILE *, struct bt_value
*, size_t);
394 void print_value_rec(FILE *, struct bt_value
*, size_t);
396 struct print_map_value_data
{
402 bt_bool
print_map_value(const char *key
, struct bt_value
*object
, void *data
)
404 struct print_map_value_data
*print_map_value_data
= data
;
406 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
407 fprintf(print_map_value_data
->fp
, "%s: ", key
);
410 if (bt_value_is_array(object
) &&
411 bt_value_array_is_empty(object
)) {
412 fprintf(print_map_value_data
->fp
, "[ ]\n");
416 if (bt_value_is_map(object
) &&
417 bt_value_map_is_empty(object
)) {
418 fprintf(print_map_value_data
->fp
, "{ }\n");
422 if (bt_value_is_array(object
) ||
423 bt_value_is_map(object
)) {
424 fprintf(print_map_value_data
->fp
, "\n");
427 print_value_rec(print_map_value_data
->fp
, object
,
428 print_map_value_data
->indent
+ 2);
433 void print_value_rec(FILE *fp
, struct bt_value
*value
, size_t indent
)
446 switch (bt_value_get_type(value
)) {
447 case BT_VALUE_TYPE_NULL
:
448 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
449 bt_common_color_reset());
451 case BT_VALUE_TYPE_BOOL
:
452 bool_val
= bt_value_bool_get(value
);
453 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
454 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
455 bt_common_color_reset());
457 case BT_VALUE_TYPE_INTEGER
:
458 int_val
= bt_value_integer_get(value
);
459 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
460 bt_common_color_fg_red(), int_val
,
461 bt_common_color_reset());
463 case BT_VALUE_TYPE_REAL
:
464 dbl_val
= bt_value_real_get(value
);
465 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
466 bt_common_color_fg_red(), dbl_val
,
467 bt_common_color_reset());
469 case BT_VALUE_TYPE_STRING
:
470 str_val
= bt_value_string_get(value
);
471 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
472 bt_common_color_fg_green(), str_val
,
473 bt_common_color_reset());
475 case BT_VALUE_TYPE_ARRAY
:
476 size
= bt_value_array_get_size(value
);
482 print_indent(fp
, indent
);
483 fprintf(fp
, "[ ]\n");
487 for (i
= 0; i
< size
; i
++) {
488 struct bt_value
*element
=
489 bt_value_array_borrow_element_by_index(
495 print_indent(fp
, indent
);
498 if (bt_value_is_array(element
) &&
499 bt_value_array_is_empty(element
)) {
500 fprintf(fp
, "[ ]\n");
504 if (bt_value_is_map(element
) &&
505 bt_value_map_is_empty(element
)) {
506 fprintf(fp
, "{ }\n");
510 if (bt_value_is_array(element
) ||
511 bt_value_is_map(element
)) {
515 print_value_rec(fp
, element
, indent
+ 2);
518 case BT_VALUE_TYPE_MAP
:
520 struct print_map_value_data data
= {
525 if (bt_value_map_is_empty(value
)) {
526 print_indent(fp
, indent
);
527 fprintf(fp
, "{ }\n");
531 bt_value_map_foreach_entry(value
, print_map_value
, &data
);
540 BT_LOGE("Error printing value of type %s.",
541 bt_common_value_type_string(bt_value_get_type(value
)));
545 void print_value(FILE *fp
, struct bt_value
*value
, size_t indent
)
547 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
548 print_indent(fp
, indent
);
551 print_value_rec(fp
, value
, indent
);
555 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
557 fprintf(stderr
, " ");
558 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
559 bt_config_component
->comp_cls_name
->str
,
560 bt_config_component
->type
);
561 fprintf(stderr
, ":\n");
563 if (bt_config_component
->instance_name
->len
> 0) {
564 fprintf(stderr
, " Name: %s\n",
565 bt_config_component
->instance_name
->str
);
568 fprintf(stderr
, " Parameters:\n");
570 bt_value_borrow_from_private(bt_config_component
->params
), 8);
574 void print_bt_config_components(GPtrArray
*array
)
578 for (i
= 0; i
< array
->len
; i
++) {
579 struct bt_config_component
*cfg_component
=
580 bt_config_get_component(array
, i
);
581 print_bt_config_component(cfg_component
);
582 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
587 void print_plugin_paths(struct bt_value
*plugin_paths
)
589 fprintf(stderr
, " Plugin paths:\n");
590 print_value(stderr
, plugin_paths
, 4);
594 void print_cfg_run(struct bt_config
*cfg
)
598 print_plugin_paths(bt_value_borrow_from_private(cfg
->plugin_paths
));
599 fprintf(stderr
, " Source component instances:\n");
600 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
602 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
603 fprintf(stderr
, " Filter component instances:\n");
604 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
607 fprintf(stderr
, " Sink component instances:\n");
608 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
609 fprintf(stderr
, " Connections:\n");
611 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
612 struct bt_config_connection
*cfg_connection
=
613 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
616 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
617 cfg_connection
->upstream_comp_name
->str
,
618 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
619 cfg_connection
->upstream_port_glob
->str
,
620 cfg_connection
->downstream_comp_name
->str
,
621 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
622 cfg_connection
->downstream_port_glob
->str
);
627 void print_cfg_list_plugins(struct bt_config
*cfg
)
629 print_plugin_paths(bt_value_borrow_from_private(cfg
->plugin_paths
));
633 void print_cfg_help(struct bt_config
*cfg
)
635 print_plugin_paths(bt_value_borrow_from_private(cfg
->plugin_paths
));
639 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
641 print_plugin_paths(bt_value_borrow_from_private(cfg
->plugin_paths
));
642 fprintf(stderr
, " Path: %s\n",
643 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
647 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
649 print_plugin_paths(bt_value_borrow_from_private(cfg
->plugin_paths
));
650 fprintf(stderr
, " URL: %s\n",
651 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
655 void print_cfg_query(struct bt_config
*cfg
)
657 print_plugin_paths(bt_value_borrow_from_private(cfg
->plugin_paths
));
658 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
659 fprintf(stderr
, " Component class:\n");
660 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
664 void print_cfg(struct bt_config
*cfg
)
666 if (!BT_LOG_ON_INFO
) {
670 BT_LOGI_STR("Configuration:");
671 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
672 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
674 switch (cfg
->command
) {
675 case BT_CONFIG_COMMAND_RUN
:
678 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
679 print_cfg_list_plugins(cfg
);
681 case BT_CONFIG_COMMAND_HELP
:
684 case BT_CONFIG_COMMAND_QUERY
:
685 print_cfg_query(cfg
);
687 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
688 print_cfg_print_ctf_metadata(cfg
);
690 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
691 print_cfg_print_lttng_live_sessions(cfg
);
699 void add_to_loaded_plugins(struct bt_plugin_set
*plugin_set
)
704 count
= bt_plugin_set_get_plugin_count(plugin_set
);
705 BT_ASSERT(count
>= 0);
707 for (i
= 0; i
< count
; i
++) {
708 struct bt_plugin
*plugin
=
709 bt_plugin_set_get_plugin(plugin_set
, i
);
710 struct bt_plugin
*loaded_plugin
=
711 find_plugin(bt_plugin_get_name(plugin
));
716 BT_LOGI("Not using plugin: another one already exists with the same name: "
717 "plugin-name=\"%s\", plugin-path=\"%s\", "
718 "existing-plugin-path=\"%s\"",
719 bt_plugin_get_name(plugin
),
720 bt_plugin_get_path(plugin
),
721 bt_plugin_get_path(loaded_plugin
));
722 bt_object_put_ref(loaded_plugin
);
724 /* Add to global array. */
725 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
726 bt_plugin_get_name(plugin
));
727 g_ptr_array_add(loaded_plugins
, bt_object_get_ref(plugin
));
730 bt_object_put_ref(plugin
);
735 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
737 int nr_paths
, i
, ret
= 0;
739 nr_paths
= bt_value_array_get_size(plugin_paths
);
741 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
746 BT_LOGI("Loading dynamic plugins.");
748 for (i
= 0; i
< nr_paths
; i
++) {
749 struct bt_value
*plugin_path_value
= NULL
;
750 const char *plugin_path
;
751 struct bt_plugin_set
*plugin_set
;
753 plugin_path_value
= bt_value_array_borrow_element_by_index(
755 plugin_path
= bt_value_string_get(plugin_path_value
);
758 * Skip this if the directory does not exist because
759 * bt_plugin_create_all_from_dir() expects an existing
762 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
763 BT_LOGV("Skipping nonexistent directory path: "
764 "path=\"%s\"", plugin_path
);
768 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
770 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
775 add_to_loaded_plugins(plugin_set
);
776 bt_object_put_ref(plugin_set
);
783 int load_static_plugins(void)
786 struct bt_plugin_set
*plugin_set
;
788 BT_LOGI("Loading static plugins.");
789 plugin_set
= bt_plugin_create_all_from_static();
791 BT_LOGE("Unable to load static plugins.");
796 add_to_loaded_plugins(plugin_set
);
797 bt_object_put_ref(plugin_set
);
803 int load_all_plugins(struct bt_value
*plugin_paths
)
807 if (load_dynamic_plugins(plugin_paths
)) {
812 if (load_static_plugins()) {
817 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
824 void print_plugin_info(struct bt_plugin
*plugin
)
826 unsigned int major
, minor
, patch
;
828 enum bt_plugin_status version_status
;
829 const char *plugin_name
;
833 const char *plugin_description
;
835 plugin_name
= bt_plugin_get_name(plugin
);
836 path
= bt_plugin_get_path(plugin
);
837 author
= bt_plugin_get_author(plugin
);
838 license
= bt_plugin_get_license(plugin
);
839 plugin_description
= bt_plugin_get_description(plugin
);
840 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
842 printf("%s%s%s%s:\n", bt_common_color_bold(),
843 bt_common_color_fg_blue(), plugin_name
,
844 bt_common_color_reset());
846 printf(" %sPath%s: %s\n", bt_common_color_bold(),
847 bt_common_color_reset(), path
);
852 if (version_status
== BT_PLUGIN_STATUS_OK
) {
853 printf(" %sVersion%s: %u.%u.%u",
854 bt_common_color_bold(), bt_common_color_reset(),
855 major
, minor
, patch
);
864 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
865 bt_common_color_reset(),
866 plugin_description
? plugin_description
: "(None)");
867 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
868 bt_common_color_reset(), author
? author
: "(Unknown)");
869 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
870 bt_common_color_reset(),
871 license
? license
: "(Unknown)");
875 int cmd_query(struct bt_config
*cfg
)
878 struct bt_component_class
*comp_cls
= NULL
;
879 struct bt_value
*results
= NULL
;
880 const char *fail_reason
= NULL
;
882 comp_cls
= find_component_class(cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
883 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
884 cfg
->cmd_data
.query
.cfg_component
->type
);
886 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
887 "comp-cls-name=\"%s\", comp-cls-type=%d",
888 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
889 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
890 cfg
->cmd_data
.query
.cfg_component
->type
);
891 fprintf(stderr
, "%s%sCannot find component class %s",
892 bt_common_color_bold(),
893 bt_common_color_fg_red(),
894 bt_common_color_reset());
895 print_plugin_comp_cls_opt(stderr
,
896 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
897 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
898 cfg
->cmd_data
.query
.cfg_component
->type
);
899 fprintf(stderr
, "\n");
904 ret
= query(comp_cls
, cfg
->cmd_data
.query
.object
->str
,
905 bt_value_borrow_from_private(cfg
->cmd_data
.query
.cfg_component
->params
),
906 &results
, &fail_reason
);
911 print_value(stdout
, results
, 0);
915 BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", "
916 "comp-cls-name=\"%s\", comp-cls-type=%d "
917 "object=\"%s\"", fail_reason
,
918 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
919 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
920 cfg
->cmd_data
.query
.cfg_component
->type
,
921 cfg
->cmd_data
.query
.object
->str
);
922 fprintf(stderr
, "%s%sFailed to query info to %s",
923 bt_common_color_bold(),
924 bt_common_color_fg_red(),
925 bt_common_color_reset());
926 print_plugin_comp_cls_opt(stderr
,
927 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
928 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
929 cfg
->cmd_data
.query
.cfg_component
->type
);
930 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
931 bt_common_color_bold(),
932 bt_common_color_fg_red(),
933 cfg
->cmd_data
.query
.object
->str
,
935 bt_common_color_reset());
939 bt_object_put_ref(comp_cls
);
940 bt_object_put_ref(results
);
945 int cmd_help(struct bt_config
*cfg
)
948 struct bt_plugin
*plugin
= NULL
;
951 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
953 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
954 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
955 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
956 bt_common_color_bold(), bt_common_color_fg_red(),
957 bt_common_color_fg_blue(),
958 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
959 bt_common_color_reset());
964 print_plugin_info(plugin
);
965 printf(" %sComponent classes%s: %d\n",
966 bt_common_color_bold(),
967 bt_common_color_reset(),
968 (int) bt_plugin_get_component_class_count(plugin
));
971 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
972 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
973 struct bt_component_class
*needed_comp_cls
=
974 find_component_class(
975 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
976 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
977 cfg
->cmd_data
.help
.cfg_component
->type
);
979 if (!needed_comp_cls
) {
980 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
981 "comp-cls-name=\"%s\", comp-cls-type=%d",
982 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
983 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
984 cfg
->cmd_data
.help
.cfg_component
->type
);
985 fprintf(stderr
, "\n%s%sCannot find component class %s",
986 bt_common_color_bold(),
987 bt_common_color_fg_red(),
988 bt_common_color_reset());
989 print_plugin_comp_cls_opt(stderr
,
990 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
991 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
992 cfg
->cmd_data
.help
.cfg_component
->type
);
993 fprintf(stderr
, "\n");
998 bt_object_put_ref(needed_comp_cls
);
1001 for (i
= 0; i
< bt_plugin_get_component_class_count(plugin
); i
++) {
1002 struct bt_component_class
*comp_cls
=
1003 bt_plugin_get_component_class_by_index(plugin
, i
);
1004 const char *comp_class_name
=
1005 bt_component_class_get_name(comp_cls
);
1006 const char *comp_class_description
=
1007 bt_component_class_get_description(comp_cls
);
1008 const char *comp_class_help
=
1009 bt_component_class_get_help(comp_cls
);
1010 enum bt_component_class_type type
=
1011 bt_component_class_get_type(comp_cls
);
1013 BT_ASSERT(comp_cls
);
1015 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
1016 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
1017 if (strcmp(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1018 comp_class_name
) != 0 ||
1019 type
!= cfg
->cmd_data
.help
.cfg_component
->type
) {
1020 bt_object_put_ref(comp_cls
);
1026 print_plugin_comp_cls_opt(stdout
,
1027 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1031 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1032 bt_common_color_reset(),
1033 comp_class_description
? comp_class_description
: "(None)");
1035 if (comp_class_help
) {
1036 printf("\n%s\n", comp_class_help
);
1039 bt_object_put_ref(comp_cls
);
1043 bt_object_put_ref(plugin
);
1048 int cmd_list_plugins(struct bt_config
*cfg
)
1051 int plugins_count
, component_classes_count
= 0, i
;
1053 printf("From the following plugin paths:\n\n");
1054 print_value(stdout
, bt_value_borrow_from_private(cfg
->plugin_paths
), 2);
1056 plugins_count
= loaded_plugins
->len
;
1057 if (plugins_count
== 0) {
1058 printf("No plugins found.\n");
1062 for (i
= 0; i
< plugins_count
; i
++) {
1063 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1065 component_classes_count
+= bt_plugin_get_component_class_count(plugin
);
1068 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1069 bt_common_color_bold(),
1070 component_classes_count
,
1071 bt_common_color_reset(),
1072 bt_common_color_bold(),
1074 bt_common_color_reset());
1076 for (i
= 0; i
< plugins_count
; i
++) {
1078 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1080 component_classes_count
=
1081 bt_plugin_get_component_class_count(plugin
);
1083 print_plugin_info(plugin
);
1085 if (component_classes_count
== 0) {
1086 printf(" %sComponent classes%s: (none)\n",
1087 bt_common_color_bold(),
1088 bt_common_color_reset());
1090 printf(" %sComponent classes%s:\n",
1091 bt_common_color_bold(),
1092 bt_common_color_reset());
1095 for (j
= 0; j
< component_classes_count
; j
++) {
1096 struct bt_component_class
*comp_class
=
1097 bt_plugin_get_component_class_by_index(
1099 const char *comp_class_name
=
1100 bt_component_class_get_name(comp_class
);
1101 const char *comp_class_description
=
1102 bt_component_class_get_description(comp_class
);
1103 enum bt_component_class_type type
=
1104 bt_component_class_get_type(comp_class
);
1107 print_plugin_comp_cls_opt(stdout
,
1108 bt_plugin_get_name(plugin
), comp_class_name
,
1111 if (comp_class_description
) {
1112 printf(": %s", comp_class_description
);
1116 bt_object_put_ref(comp_class
);
1125 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1128 struct bt_component_class
*comp_cls
= NULL
;
1129 struct bt_value
*results
= NULL
;
1130 struct bt_private_value
*params
= NULL
;
1131 struct bt_value
*map
= NULL
;
1132 struct bt_value
*v
= NULL
;
1133 static const char * const plugin_name
= "ctf";
1134 static const char * const comp_cls_name
= "lttng-live";
1135 static const enum bt_component_class_type comp_cls_type
=
1136 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1137 int64_t array_size
, i
;
1138 const char *fail_reason
= NULL
;
1139 FILE *out_stream
= stdout
;
1141 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1142 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1145 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1146 "comp-cls-name=\"%s\", comp-cls-type=%d",
1147 plugin_name
, comp_cls_name
,
1148 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1149 fprintf(stderr
, "%s%sCannot find component class %s",
1150 bt_common_color_bold(),
1151 bt_common_color_fg_red(),
1152 bt_common_color_reset());
1153 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1154 comp_cls_name
, comp_cls_type
);
1155 fprintf(stderr
, "\n");
1159 params
= bt_private_value_map_create();
1164 ret
= bt_private_value_map_insert_string_entry(params
, "url",
1165 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1170 ret
= query(comp_cls
, "sessions", bt_value_borrow_from_private(params
),
1171 &results
, &fail_reason
);
1178 if (!bt_value_is_array(results
)) {
1179 BT_LOGE_STR("Expecting an array for sessions query.");
1180 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1181 bt_common_color_bold(),
1182 bt_common_color_fg_red(),
1183 bt_common_color_reset());
1187 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1189 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1193 BT_LOGE_ERRNO("Cannot open file for writing",
1195 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1200 array_size
= bt_value_array_get_size(results
);
1201 for (i
= 0; i
< array_size
; i
++) {
1202 const char *url_text
;
1203 int64_t timer_us
, streams
, clients
;
1205 map
= bt_value_array_borrow_element_by_index(results
, i
);
1207 BT_LOGE_STR("Unexpected empty array entry.");
1210 if (!bt_value_is_map(map
)) {
1211 BT_LOGE_STR("Unexpected entry type.");
1215 v
= bt_value_map_borrow_entry_value(map
, "url");
1217 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1220 url_text
= bt_value_string_get(v
);
1221 fprintf(out_stream
, "%s", url_text
);
1222 v
= bt_value_map_borrow_entry_value(map
, "timer-us");
1224 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1227 timer_us
= bt_value_integer_get(v
);
1228 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1229 v
= bt_value_map_borrow_entry_value(map
, "stream-count");
1231 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1234 streams
= bt_value_integer_get(v
);
1235 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1236 v
= bt_value_map_borrow_entry_value(map
, "client-count");
1238 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1241 clients
= bt_value_integer_get(v
);
1242 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1248 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1249 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1250 bt_common_color_bold(),
1251 bt_common_color_fg_red(),
1253 bt_common_color_reset());
1259 bt_object_put_ref(results
);
1260 bt_object_put_ref(params
);
1261 bt_object_put_ref(comp_cls
);
1263 if (out_stream
&& out_stream
!= stdout
) {
1264 int fclose_ret
= fclose(out_stream
);
1267 BT_LOGE_ERRNO("Cannot close file stream",
1269 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1277 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1280 struct bt_component_class
*comp_cls
= NULL
;
1281 struct bt_value
*results
= NULL
;
1282 struct bt_private_value
*params
= NULL
;
1283 struct bt_value
*metadata_text_value
= NULL
;
1284 const char *metadata_text
= NULL
;
1285 static const char * const plugin_name
= "ctf";
1286 static const char * const comp_cls_name
= "fs";
1287 static const enum bt_component_class_type comp_cls_type
=
1288 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1289 const char *fail_reason
= NULL
;
1290 FILE *out_stream
= stdout
;
1292 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1293 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1296 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1297 "comp-cls-name=\"%s\", comp-cls-type=%d",
1298 plugin_name
, comp_cls_name
,
1299 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1300 fprintf(stderr
, "%s%sCannot find component class %s",
1301 bt_common_color_bold(),
1302 bt_common_color_fg_red(),
1303 bt_common_color_reset());
1304 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1305 comp_cls_name
, comp_cls_type
);
1306 fprintf(stderr
, "\n");
1311 params
= bt_private_value_map_create();
1317 ret
= bt_private_value_map_insert_string_entry(params
, "path",
1318 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1324 ret
= query(comp_cls
, "metadata-info",
1325 bt_value_borrow_from_private(params
), &results
, &fail_reason
);
1330 metadata_text_value
= bt_value_map_borrow_entry_value(results
, "text");
1331 if (!metadata_text_value
) {
1332 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1337 metadata_text
= bt_value_string_get(metadata_text_value
);
1339 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1341 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1345 BT_LOGE_ERRNO("Cannot open file for writing",
1347 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1352 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1354 BT_LOGE("Cannot write whole metadata text to output stream: "
1362 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1363 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1364 bt_common_color_bold(),
1365 bt_common_color_fg_red(),
1367 bt_common_color_reset());
1370 destroy_the_query_executor();
1371 bt_object_put_ref(results
);
1372 bt_object_put_ref(params
);
1373 bt_object_put_ref(comp_cls
);
1375 if (out_stream
&& out_stream
!= stdout
) {
1376 int fclose_ret
= fclose(out_stream
);
1379 BT_LOGE_ERRNO("Cannot close file stream",
1381 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1389 char *instance_name
;
1393 struct trace_range
{
1394 uint64_t intersection_range_begin_ns
;
1395 uint64_t intersection_range_end_ns
;
1399 guint
port_id_hash(gconstpointer v
)
1401 const struct port_id
*id
= v
;
1403 BT_ASSERT(id
->instance_name
);
1404 BT_ASSERT(id
->port_name
);
1406 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1410 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1412 const struct port_id
*id1
= v1
;
1413 const struct port_id
*id2
= v2
;
1415 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1416 !strcmp(id1
->port_name
, id2
->port_name
);
1420 void port_id_destroy(gpointer data
)
1422 struct port_id
*id
= data
;
1424 free(id
->instance_name
);
1425 free(id
->port_name
);
1430 void trace_range_destroy(gpointer data
)
1435 struct cmd_run_ctx
{
1437 GHashTable
*components
;
1440 struct bt_graph
*graph
;
1443 struct bt_config
*cfg
;
1447 bool stream_intersection_mode
;
1450 * Association of struct port_id -> struct trace_range.
1452 GHashTable
*intersections
;
1455 /* Returns a timestamp of the form "(-)s.ns" */
1457 char *s_from_ns(int64_t ns
)
1462 int64_t ts_sec_abs
, ts_nsec_abs
;
1463 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1464 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1466 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1467 is_negative
= false;
1468 ts_sec_abs
= ts_sec
;
1469 ts_nsec_abs
= ts_nsec
;
1470 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1471 is_negative
= false;
1472 ts_sec_abs
= ts_sec
- 1;
1473 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1474 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1476 ts_sec_abs
= ts_sec
;
1477 ts_nsec_abs
= -ts_nsec
;
1478 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1480 ts_sec_abs
= -(ts_sec
+ 1);
1481 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1482 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1484 ts_sec_abs
= -ts_sec
;
1485 ts_nsec_abs
= ts_nsec
;
1486 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1488 ts_sec_abs
= -ts_sec
;
1489 ts_nsec_abs
= -ts_nsec
;
1492 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1493 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1501 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1502 struct cmd_run_ctx
*ctx
, struct bt_component
*upstream_comp
,
1503 struct bt_port
*upstream_port
,
1504 struct bt_config_connection
*cfg_conn
)
1507 GQuark downstreamp_comp_name_quark
;
1508 struct bt_component
*downstream_comp
;
1509 int64_t downstream_port_count
;
1511 int64_t (*port_count_fn
)(struct bt_component
*);
1512 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
1513 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1514 bool insert_trimmer
= false;
1515 struct bt_private_value
*trimmer_params
= NULL
;
1516 char *intersection_begin
= NULL
;
1517 char *intersection_end
= NULL
;
1518 struct bt_component
*trimmer
= NULL
;
1519 struct bt_component_class
*trimmer_class
= NULL
;
1520 struct bt_port
*trimmer_input
= NULL
;
1521 struct bt_port
*trimmer_output
= NULL
;
1523 if (ctx
->intersections
&&
1524 bt_component_get_class_type(upstream_comp
) ==
1525 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1526 struct trace_range
*range
;
1527 struct port_id port_id
= {
1528 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1529 .port_name
= (char *) bt_port_get_name(upstream_port
)
1532 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1536 range
= (struct trace_range
*) g_hash_table_lookup(
1537 ctx
->intersections
, &port_id
);
1539 enum bt_value_status status
;
1541 intersection_begin
= s_from_ns(
1542 range
->intersection_range_begin_ns
);
1543 intersection_end
= s_from_ns(
1544 range
->intersection_range_end_ns
);
1545 if (!intersection_begin
|| !intersection_end
) {
1546 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1550 insert_trimmer
= true;
1551 trimmer_params
= bt_private_value_map_create();
1552 if (!trimmer_params
) {
1556 status
= bt_private_value_map_insert_string_entry(
1557 trimmer_params
, "begin", intersection_begin
);
1558 if (status
!= BT_VALUE_STATUS_OK
) {
1561 status
= bt_private_value_map_insert_string_entry(
1563 "end", intersection_end
);
1564 if (status
!= BT_VALUE_STATUS_OK
) {
1569 trimmer_class
= find_component_class("utils", "trimmer",
1570 BT_COMPONENT_CLASS_TYPE_FILTER
);
1571 if (!trimmer_class
) {
1576 BT_LOGI("Connecting upstream port to the next available downstream port: "
1577 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1578 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1579 upstream_port
, bt_port_get_name(upstream_port
),
1580 cfg_conn
->downstream_comp_name
->str
,
1581 cfg_conn
->arg
->str
);
1582 downstreamp_comp_name_quark
= g_quark_from_string(
1583 cfg_conn
->downstream_comp_name
->str
);
1584 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1585 downstream_comp
= g_hash_table_lookup(ctx
->components
,
1586 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1587 if (!downstream_comp
) {
1588 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1589 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1590 cfg_conn
->arg
->str
);
1591 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1592 cfg_conn
->arg
->str
);
1596 if (bt_component_is_filter(downstream_comp
)) {
1597 port_count_fn
= bt_component_filter_get_input_port_count
;
1598 port_by_index_fn
= bt_component_filter_get_input_port_by_index
;
1599 } else if (bt_component_is_sink(downstream_comp
)) {
1600 port_count_fn
= bt_component_sink_get_input_port_count
;
1601 port_by_index_fn
= bt_component_sink_get_input_port_by_index
;
1604 * Should never happen because the connections are
1605 * validated before we get here.
1607 BT_LOGF("Invalid connection: downstream component is a source: "
1608 "conn-arg=\"%s\"", cfg_conn
->arg
->str
);
1612 downstream_port_count
= port_count_fn(downstream_comp
);
1613 BT_ASSERT(downstream_port_count
>= 0);
1615 for (i
= 0; i
< downstream_port_count
; i
++) {
1616 struct bt_port
*downstream_port
=
1617 port_by_index_fn(downstream_comp
, i
);
1618 const char *upstream_port_name
;
1619 const char *downstream_port_name
;
1621 BT_ASSERT(downstream_port
);
1623 /* Skip port if it's already connected. */
1624 if (bt_port_is_connected(downstream_port
)) {
1625 bt_object_put_ref(downstream_port
);
1626 BT_LOGD("Skipping downstream port: already connected: "
1627 "port-addr=%p, port-name=\"%s\"",
1629 bt_port_get_name(downstream_port
));
1633 downstream_port_name
= bt_port_get_name(downstream_port
);
1634 BT_ASSERT(downstream_port_name
);
1635 upstream_port_name
= bt_port_get_name(upstream_port
);
1636 BT_ASSERT(upstream_port_name
);
1638 if (!bt_common_star_glob_match(
1639 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1640 downstream_port_name
, SIZE_MAX
)) {
1641 bt_object_put_ref(downstream_port
);
1645 if (insert_trimmer
) {
1647 * In order to insert the trimmer between the two
1648 * components that were being connected, we create
1649 * a connection configuration entry which describes
1650 * a connection from the trimmer's output to the
1651 * original input that was being connected.
1653 * Hence, the creation of the trimmer will cause the
1654 * graph "new port" listener to establish all downstream
1655 * connections as its output port is connected. We will
1656 * then establish the connection between the original
1657 * upstream source and the trimmer.
1659 char *trimmer_name
= NULL
;
1660 enum bt_graph_status graph_status
;
1662 ret
= asprintf(&trimmer_name
, "%s-%s",
1663 "stream-intersection-trimmer",
1664 upstream_port_name
);
1670 ctx
->connect_ports
= false;
1671 graph_status
= bt_graph_add_component(ctx
->graph
,
1672 trimmer_class
, trimmer_name
,
1673 bt_value_borrow_from_private(trimmer_params
),
1676 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1682 bt_component_filter_get_input_port_by_index(
1684 if (!trimmer_input
) {
1688 bt_component_filter_get_output_port_by_index(
1690 if (!trimmer_output
) {
1695 * Replace the current downstream port by the trimmer's
1698 BT_OBJECT_MOVE_REF(downstream_port
, trimmer_input
);
1699 downstream_port_name
= bt_port_get_name(
1701 if (!downstream_port_name
) {
1706 /* We have a winner! */
1707 status
= bt_graph_connect_ports(ctx
->graph
,
1708 upstream_port
, downstream_port
, NULL
);
1709 BT_OBJECT_PUT_REF_AND_RESET(downstream_port
);
1711 case BT_GRAPH_STATUS_OK
:
1713 case BT_GRAPH_STATUS_CANCELED
:
1714 BT_LOGI_STR("Graph was canceled by user.");
1715 status
= BT_GRAPH_STATUS_OK
;
1717 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1718 BT_LOGE("A component refused a connection to one of its ports: "
1719 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1720 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1721 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1722 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1724 upstream_comp
, bt_component_get_name(upstream_comp
),
1725 upstream_port
, bt_port_get_name(upstream_port
),
1726 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1727 downstream_port
, downstream_port_name
,
1728 cfg_conn
->arg
->str
);
1730 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1731 bt_port_get_name(upstream_port
),
1732 downstream_port_name
,
1733 cfg_conn
->arg
->str
);
1736 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1737 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1738 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1739 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1740 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1742 upstream_comp
, bt_component_get_name(upstream_comp
),
1743 upstream_port
, bt_port_get_name(upstream_port
),
1744 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1745 downstream_port
, downstream_port_name
,
1746 cfg_conn
->arg
->str
);
1748 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1749 bt_port_get_name(upstream_port
),
1750 downstream_port_name
,
1751 cfg_conn
->arg
->str
);
1755 BT_LOGI("Connected component ports: "
1756 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1757 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1758 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1759 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1761 upstream_comp
, bt_component_get_name(upstream_comp
),
1762 upstream_port
, bt_port_get_name(upstream_port
),
1763 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1764 downstream_port
, downstream_port_name
,
1765 cfg_conn
->arg
->str
);
1767 if (insert_trimmer
) {
1769 * The first connection, from the source to the trimmer,
1770 * has been done. We now connect the trimmer to the
1771 * original downstream port.
1773 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1774 ctx
, trimmer
, trimmer_output
, cfg_conn
);
1778 ctx
->connect_ports
= true;
1782 * We found a matching downstream port: the search is
1788 /* No downstream port found */
1789 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1790 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1791 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1792 upstream_port
, bt_port_get_name(upstream_port
),
1793 cfg_conn
->downstream_comp_name
->str
,
1794 cfg_conn
->arg
->str
);
1796 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1797 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1803 free(intersection_begin
);
1804 free(intersection_end
);
1805 BT_OBJECT_PUT_REF_AND_RESET(trimmer_params
);
1806 BT_OBJECT_PUT_REF_AND_RESET(trimmer_class
);
1807 BT_OBJECT_PUT_REF_AND_RESET(trimmer
);
1808 BT_OBJECT_PUT_REF_AND_RESET(trimmer_input
);
1809 BT_OBJECT_PUT_REF_AND_RESET(trimmer_output
);
1814 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1815 struct bt_port
*upstream_port
)
1818 const char *upstream_port_name
;
1819 const char *upstream_comp_name
;
1820 struct bt_component
*upstream_comp
= NULL
;
1824 BT_ASSERT(upstream_port
);
1825 upstream_port_name
= bt_port_get_name(upstream_port
);
1826 BT_ASSERT(upstream_port_name
);
1827 upstream_comp
= bt_port_get_component(upstream_port
);
1828 if (!upstream_comp
) {
1829 BT_LOGW("Upstream port to connect is not part of a component: "
1830 "port-addr=%p, port-name=\"%s\"",
1831 upstream_port
, upstream_port_name
);
1836 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1837 BT_ASSERT(upstream_comp_name
);
1838 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1839 "port-addr=%p, port-name=\"%s\"",
1840 upstream_comp
, upstream_comp_name
,
1841 upstream_port
, upstream_port_name
);
1843 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1844 struct bt_config_connection
*cfg_conn
=
1846 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1848 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1849 upstream_comp_name
)) {
1853 if (!bt_common_star_glob_match(
1854 cfg_conn
->upstream_port_glob
->str
,
1855 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1859 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1860 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1862 BT_LOGE("Cannot connect upstream port: "
1863 "port-addr=%p, port-name=\"%s\"",
1865 upstream_port_name
);
1867 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1870 cfg_conn
->arg
->str
);
1876 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1877 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1878 upstream_port_name
);
1880 "Cannot create connection: upstream port `%s` does not match any connection\n",
1881 upstream_port_name
);
1887 bt_object_put_ref(upstream_comp
);
1892 void graph_port_added_listener(struct bt_port
*port
, void *data
)
1894 struct bt_component
*comp
= NULL
;
1895 struct cmd_run_ctx
*ctx
= data
;
1897 comp
= bt_port_get_component(port
);
1898 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1899 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1900 comp
, comp
? bt_component_get_name(comp
) : "",
1901 port
, bt_port_get_name(port
));
1903 if (!ctx
->connect_ports
) {
1908 BT_LOGW_STR("Port has no component.");
1912 if (bt_port_is_connected(port
)) {
1913 BT_LOGW_STR("Port is already connected.");
1917 if (!bt_port_is_output(port
)) {
1918 BT_LOGI_STR("Skipping input port.");
1922 if (cmd_run_ctx_connect_upstream_port(ctx
, port
)) {
1923 BT_LOGF_STR("Cannot connect upstream port.");
1924 fprintf(stderr
, "Added port could not be connected: aborting\n");
1929 bt_object_put_ref(comp
);
1934 void graph_port_removed_listener(struct bt_component
*component
,
1935 struct bt_port
*port
, void *data
)
1937 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1938 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1939 component
, bt_component_get_name(component
),
1940 port
, bt_port_get_name(port
));
1944 void graph_ports_connected_listener(struct bt_port
*upstream_port
,
1945 struct bt_port
*downstream_port
, void *data
)
1947 struct bt_component
*upstream_comp
= bt_port_get_component(upstream_port
);
1948 struct bt_component
*downstream_comp
= bt_port_get_component(downstream_port
);
1950 BT_ASSERT(upstream_comp
);
1951 BT_ASSERT(downstream_comp
);
1952 BT_LOGI("Graph's component ports connected: "
1953 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1954 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1955 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1956 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1957 upstream_comp
, bt_component_get_name(upstream_comp
),
1958 upstream_port
, bt_port_get_name(upstream_port
),
1959 downstream_comp
, bt_component_get_name(downstream_comp
),
1960 downstream_port
, bt_port_get_name(downstream_port
));
1961 bt_object_put_ref(upstream_comp
);
1962 bt_object_put_ref(downstream_comp
);
1966 void graph_ports_disconnected_listener(
1967 struct bt_component
*upstream_component
,
1968 struct bt_component
*downstream_component
,
1969 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
1972 BT_LOGI("Graph's component ports disconnected: "
1973 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1974 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1975 upstream_port
, bt_port_get_name(upstream_port
),
1976 downstream_port
, bt_port_get_name(downstream_port
));
1980 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1986 if (ctx
->components
) {
1987 g_hash_table_destroy(ctx
->components
);
1988 ctx
->components
= NULL
;
1991 if (ctx
->intersections
) {
1992 g_hash_table_destroy(ctx
->intersections
);
1993 ctx
->intersections
= NULL
;
1996 BT_OBJECT_PUT_REF_AND_RESET(ctx
->graph
);
2002 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2007 ctx
->connect_ports
= false;
2008 ctx
->components
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
2009 NULL
, bt_object_put_ref
);
2010 if (!ctx
->components
) {
2014 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2015 ctx
->stream_intersection_mode
= true;
2016 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2017 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2018 if (!ctx
->intersections
) {
2023 ctx
->graph
= bt_graph_create();
2028 the_graph
= ctx
->graph
;
2029 ret
= bt_graph_add_port_added_listener(ctx
->graph
,
2030 graph_port_added_listener
, NULL
, ctx
);
2032 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2036 ret
= bt_graph_add_port_removed_listener(ctx
->graph
,
2037 graph_port_removed_listener
, NULL
, ctx
);
2039 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
2043 ret
= bt_graph_add_ports_connected_listener(ctx
->graph
,
2044 graph_ports_connected_listener
, NULL
, ctx
);
2046 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
2050 ret
= bt_graph_add_ports_disconnected_listener(ctx
->graph
,
2051 graph_ports_disconnected_listener
, NULL
, ctx
);
2053 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
2060 cmd_run_ctx_destroy(ctx
);
2068 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2069 struct bt_config_component
*cfg_comp
,
2070 struct bt_component_class
*comp_cls
)
2074 int64_t trace_count
;
2075 enum bt_value_status value_status
;
2076 const char *path
= NULL
;
2077 struct bt_value
*component_path_value
= NULL
;
2078 struct bt_private_value
*query_params
= NULL
;
2079 struct bt_value
*query_result
= NULL
;
2080 struct bt_value
*trace_info
= NULL
;
2081 struct bt_value
*intersection_range
= NULL
;
2082 struct bt_value
*intersection_begin
= NULL
;
2083 struct bt_value
*intersection_end
= NULL
;
2084 struct bt_value
*stream_path_value
= NULL
;
2085 struct bt_value
*stream_paths
= NULL
;
2086 struct bt_value
*stream_infos
= NULL
;
2087 struct bt_value
*stream_info
= NULL
;
2088 struct port_id
*port_id
= NULL
;
2089 struct trace_range
*trace_range
= NULL
;
2090 const char *fail_reason
= NULL
;
2092 component_path_value
= bt_value_map_borrow_entry_value(
2093 bt_value_borrow_from_private(cfg_comp
->params
),
2095 if (component_path_value
&& !bt_value_is_string(component_path_value
)) {
2096 BT_LOGD("Cannot get path parameter: component-name=%s",
2097 cfg_comp
->instance_name
->str
);
2102 path
= bt_value_string_get(component_path_value
);
2103 query_params
= bt_private_value_map_create();
2104 if (!query_params
) {
2105 BT_LOGE_STR("Cannot create query parameters.");
2110 value_status
= bt_private_value_map_insert_entry(query_params
, "path",
2111 component_path_value
);
2112 if (value_status
!= BT_VALUE_STATUS_OK
) {
2113 BT_LOGE_STR("Cannot insert path parameter in query parameter map.");
2118 ret
= query(comp_cls
, "trace-info",
2119 bt_value_borrow_from_private(query_params
), &query_result
,
2122 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2123 "comp-class-name=\"%s\"", fail_reason
,
2124 bt_component_class_get_name(comp_cls
));
2129 BT_ASSERT(query_result
);
2131 if (!bt_value_is_array(query_result
)) {
2132 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2133 "component-class-name=%s",
2134 bt_component_class_get_name(comp_cls
));
2139 trace_count
= bt_value_array_get_size(query_result
);
2140 if (trace_count
< 0) {
2145 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2147 uint64_t stream_idx
;
2148 int64_t stream_count
;
2150 trace_info
= bt_value_array_borrow_element_by_index(
2151 query_result
, trace_idx
);
2152 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2154 BT_LOGD_STR("Cannot retrieve trace from query result.");
2158 intersection_range
= bt_value_map_borrow_entry_value(trace_info
,
2159 "intersection-range-ns");
2160 if (!intersection_range
) {
2162 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2166 intersection_begin
= bt_value_map_borrow_entry_value(
2167 intersection_range
, "begin");
2168 if (!intersection_begin
) {
2170 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2174 intersection_end
= bt_value_map_borrow_entry_value(
2175 intersection_range
, "end");
2176 if (!intersection_end
) {
2178 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2182 begin
= bt_value_integer_get(intersection_begin
);
2183 end
= bt_value_integer_get(intersection_end
);
2185 if (begin
< 0 || end
< 0 || end
< begin
) {
2186 BT_LOGW("Invalid trace stream intersection values: "
2187 "intersection-range-ns:begin=%" PRId64
2188 ", intersection-range-ns:end=%" PRId64
,
2194 stream_infos
= bt_value_map_borrow_entry_value(trace_info
,
2196 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2198 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2202 stream_count
= bt_value_array_get_size(stream_infos
);
2203 if (stream_count
< 0) {
2211 * The first path of a stream's "paths" is currently used to
2212 * associate streams/ports to a given trace intersection.
2214 * This is a fragile hack as it relies on the port names
2215 * being set to the various streams path.
2217 * A stream name should be introduced as part of the trace-info
2220 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2221 const char *stream_path
;
2223 port_id
= g_new0(struct port_id
, 1);
2226 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2229 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2230 if (!port_id
->instance_name
) {
2232 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2236 trace_range
= g_new0(struct trace_range
, 1);
2239 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2242 trace_range
->intersection_range_begin_ns
= begin
;
2243 trace_range
->intersection_range_end_ns
= end
;
2245 stream_info
= bt_value_array_borrow_element_by_index(
2246 stream_infos
, stream_idx
);
2247 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2249 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2253 stream_paths
= bt_value_map_borrow_entry_value(
2254 stream_info
, "paths");
2255 if (!stream_paths
|| !bt_value_is_array(stream_paths
)) {
2257 BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
2262 bt_value_array_borrow_element_by_index(
2264 if (!stream_path_value
||
2265 !bt_value_is_string(stream_path_value
)) {
2267 BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
2271 stream_path
= bt_value_string_get(stream_path_value
);
2272 port_id
->port_name
= strdup(stream_path
);
2273 if (!port_id
->port_name
) {
2275 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2279 BT_LOGD("Inserting stream intersection ");
2281 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2291 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2292 bt_common_color_bold(),
2293 bt_common_color_fg_yellow(),
2294 path
? path
: "(unknown)",
2295 bt_common_color_reset());
2297 bt_object_put_ref(query_params
);
2298 bt_object_put_ref(query_result
);
2300 g_free(trace_range
);
2305 int cmd_run_ctx_create_components_from_config_components(
2306 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2309 struct bt_component_class
*comp_cls
= NULL
;
2310 struct bt_component
*comp
= NULL
;
2313 for (i
= 0; i
< cfg_components
->len
; i
++) {
2314 struct bt_config_component
*cfg_comp
=
2315 g_ptr_array_index(cfg_components
, i
);
2318 comp_cls
= find_component_class(cfg_comp
->plugin_name
->str
,
2319 cfg_comp
->comp_cls_name
->str
, cfg_comp
->type
);
2321 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2322 "comp-cls-name=\"%s\", comp-cls-type=%d",
2323 cfg_comp
->plugin_name
->str
,
2324 cfg_comp
->comp_cls_name
->str
,
2326 fprintf(stderr
, "%s%sCannot find component class %s",
2327 bt_common_color_bold(),
2328 bt_common_color_fg_red(),
2329 bt_common_color_reset());
2330 print_plugin_comp_cls_opt(stderr
,
2331 cfg_comp
->plugin_name
->str
,
2332 cfg_comp
->comp_cls_name
->str
,
2334 fprintf(stderr
, "\n");
2338 ret
= bt_graph_add_component(ctx
->graph
, comp_cls
,
2339 cfg_comp
->instance_name
->str
,
2340 bt_value_borrow_from_private(cfg_comp
->params
), &comp
);
2342 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2343 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2345 cfg_comp
->plugin_name
->str
,
2346 cfg_comp
->comp_cls_name
->str
,
2347 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2348 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2349 bt_common_color_bold(),
2350 bt_common_color_fg_red(),
2351 cfg_comp
->instance_name
->str
,
2352 bt_common_color_reset());
2356 if (ctx
->stream_intersection_mode
&&
2357 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2358 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2364 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2365 comp
, cfg_comp
->instance_name
->str
);
2366 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2367 BT_ASSERT(quark
> 0);
2368 g_hash_table_insert(ctx
->components
,
2369 GUINT_TO_POINTER(quark
), comp
);
2371 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2380 bt_object_put_ref(comp
);
2381 bt_object_put_ref(comp_cls
);
2386 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2391 * Make sure that, during this phase, our graph's "port added"
2392 * listener does not connect ports while we are creating the
2393 * components because we have a special, initial phase for
2396 ctx
->connect_ports
= false;
2398 ret
= cmd_run_ctx_create_components_from_config_components(
2399 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2405 ret
= cmd_run_ctx_create_components_from_config_components(
2406 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2412 ret
= cmd_run_ctx_create_components_from_config_components(
2413 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2424 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2425 struct bt_component
*comp
,
2426 int64_t (*port_count_fn
)(struct bt_component
*),
2427 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t))
2433 count
= port_count_fn(comp
);
2434 BT_ASSERT(count
>= 0);
2436 for (i
= 0; i
< count
; i
++) {
2437 struct bt_port
*upstream_port
= port_by_index_fn(comp
, i
);
2439 BT_ASSERT(upstream_port
);
2440 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2441 bt_object_put_ref(upstream_port
);
2452 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2455 GHashTableIter iter
;
2456 gpointer g_name_quark
, g_comp
;
2458 ctx
->connect_ports
= true;
2459 g_hash_table_iter_init(&iter
, ctx
->components
);
2461 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2462 int64_t (*port_count_fn
)(struct bt_component
*);
2463 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
2465 if (bt_component_is_source(g_comp
)) {
2467 bt_component_source_get_output_port_count
;
2469 bt_component_source_get_output_port_by_index
;
2470 } else if (bt_component_is_filter(g_comp
)) {
2472 bt_component_filter_get_output_port_count
;
2474 bt_component_filter_get_output_port_by_index
;
2479 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
2480 g_comp
, port_count_fn
, port_by_index_fn
);
2491 const char *bt_graph_status_str(enum bt_graph_status status
)
2494 case BT_GRAPH_STATUS_CANCELED
:
2495 return "BT_GRAPH_STATUS_CANCELED";
2496 case BT_GRAPH_STATUS_AGAIN
:
2497 return "BT_GRAPH_STATUS_AGAIN";
2498 case BT_GRAPH_STATUS_END
:
2499 return "BT_GRAPH_STATUS_END";
2500 case BT_GRAPH_STATUS_OK
:
2501 return "BT_GRAPH_STATUS_OK";
2502 case BT_GRAPH_STATUS_INVALID
:
2503 return "BT_GRAPH_STATUS_INVALID";
2504 case BT_GRAPH_STATUS_NO_SINK
:
2505 return "BT_GRAPH_STATUS_NO_SINK";
2506 case BT_GRAPH_STATUS_ERROR
:
2507 return "BT_GRAPH_STATUS_ERROR";
2514 int cmd_run(struct bt_config
*cfg
)
2517 struct cmd_run_ctx ctx
= { 0 };
2519 /* Initialize the command's context and the graph object */
2520 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2521 BT_LOGE_STR("Cannot initialize the command's context.");
2522 fprintf(stderr
, "Cannot initialize the command's context\n");
2527 BT_LOGI_STR("Canceled by user before creating components.");
2531 BT_LOGI_STR("Creating components.");
2533 /* Create the requested component instances */
2534 if (cmd_run_ctx_create_components(&ctx
)) {
2535 BT_LOGE_STR("Cannot create components.");
2536 fprintf(stderr
, "Cannot create components\n");
2541 BT_LOGI_STR("Canceled by user before connecting components.");
2545 BT_LOGI_STR("Connecting components.");
2547 /* Connect the initially visible component ports */
2548 if (cmd_run_ctx_connect_ports(&ctx
)) {
2549 BT_LOGE_STR("Cannot connect initial component ports.");
2550 fprintf(stderr
, "Cannot connect initial component ports\n");
2555 BT_LOGI_STR("Canceled by user before running the graph.");
2559 BT_LOGI_STR("Running the graph.");
2563 enum bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2566 * Reset console in case something messed with console
2567 * codes during the graph's execution.
2569 printf("%s", bt_common_color_reset());
2571 fprintf(stderr
, "%s", bt_common_color_reset());
2572 BT_LOGV("bt_graph_run() returned: status=%s",
2573 bt_graph_status_str(graph_status
));
2575 switch (graph_status
) {
2576 case BT_GRAPH_STATUS_OK
:
2578 case BT_GRAPH_STATUS_CANCELED
:
2579 BT_LOGI_STR("Graph was canceled by user.");
2581 case BT_GRAPH_STATUS_AGAIN
:
2582 if (bt_graph_is_canceled(ctx
.graph
)) {
2583 BT_LOGI_STR("Graph was canceled by user.");
2587 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2588 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2590 cfg
->cmd_data
.run
.retry_duration_us
);
2592 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2593 if (bt_graph_is_canceled(ctx
.graph
)) {
2594 BT_LOGI_STR("Graph was canceled by user.");
2600 case BT_COMPONENT_STATUS_END
:
2603 BT_LOGE_STR("Graph failed to complete successfully");
2604 fprintf(stderr
, "Graph failed to complete successfully\n");
2617 cmd_run_ctx_destroy(&ctx
);
2622 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2624 const char *env_clash
;
2626 if (!cfg
->command_name
) {
2630 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2631 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2635 if (g_file_test(cfg
->command_name
,
2636 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2637 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2639 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2641 fprintf(stderr
, "\n");
2642 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
2648 void init_log_level(void)
2650 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2654 void set_auto_log_levels(struct bt_config
*cfg
)
2656 const char **env_var_name
;
2659 * Override the configuration's default log level if
2660 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2661 * are found for backward compatibility with legacy Babetrace 1.
2663 if (getenv("BABELTRACE_DEBUG") &&
2664 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2665 cfg
->log_level
= 'V';
2666 } else if (getenv("BABELTRACE_VERBOSE") &&
2667 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2668 cfg
->log_level
= 'I';
2672 * Set log levels according to --debug or --verbose. For
2673 * backward compatibility, --debug is more verbose than
2676 * --verbose: INFO log level
2677 * --debug: VERBOSE log level (includes DEBUG, which is
2678 * is less verbose than VERBOSE in the internal
2679 * logging framework)
2681 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2683 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2684 } else if (cfg
->debug
) {
2685 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2688 * Set library's default log level if not
2689 * explicitly specified.
2691 switch (cfg
->log_level
) {
2693 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE
);
2696 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2699 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
2702 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2705 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN
);
2708 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR
);
2711 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL
);
2719 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2721 bt_cli_log_level
= BT_LOG_INFO
;
2722 } else if (cfg
->debug
) {
2723 bt_cli_log_level
= BT_LOG_VERBOSE
;
2726 * Set CLI's default log level if not explicitly
2729 switch (cfg
->log_level
) {
2731 bt_cli_log_level
= BT_LOG_NONE
;
2734 bt_cli_log_level
= BT_LOG_VERBOSE
;
2737 bt_cli_log_level
= BT_LOG_DEBUG
;
2740 bt_cli_log_level
= BT_LOG_INFO
;
2743 bt_cli_log_level
= BT_LOG_WARN
;
2746 bt_cli_log_level
= BT_LOG_ERROR
;
2749 bt_cli_log_level
= BT_LOG_FATAL
;
2757 env_var_name
= log_level_env_var_names
;
2759 while (*env_var_name
) {
2760 if (!getenv(*env_var_name
)) {
2762 g_setenv(*env_var_name
, "I", 1);
2763 } else if (cfg
->debug
) {
2764 g_setenv(*env_var_name
, "V", 1);
2766 char val
[2] = { 0 };
2769 * Set module's default log level if not
2770 * explicitly specified.
2772 val
[0] = cfg
->log_level
;
2773 g_setenv(*env_var_name
, val
, 1);
2781 int main(int argc
, const char **argv
)
2785 struct bt_config
*cfg
;
2788 set_signal_handler();
2790 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2793 /* Quit without errors; typically usage/version */
2795 BT_LOGI_STR("Quitting without errors.");
2800 BT_LOGE("Command-line error: retcode=%d", retcode
);
2805 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2806 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2811 set_auto_log_levels(cfg
);
2814 if (cfg
->command_needs_plugins
) {
2815 ret
= load_all_plugins(
2816 bt_value_borrow_from_private(cfg
->plugin_paths
));
2818 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2824 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2825 cfg
->command
, cfg
->command_name
);
2827 switch (cfg
->command
) {
2828 case BT_CONFIG_COMMAND_RUN
:
2831 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2832 ret
= cmd_list_plugins(cfg
);
2834 case BT_CONFIG_COMMAND_HELP
:
2835 ret
= cmd_help(cfg
);
2837 case BT_CONFIG_COMMAND_QUERY
:
2838 ret
= cmd_query(cfg
);
2840 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2841 ret
= cmd_print_ctf_metadata(cfg
);
2843 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2844 ret
= cmd_print_lttng_live_sessions(cfg
);
2847 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2851 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2852 cfg
->command
, cfg
->command_name
, ret
);
2853 warn_command_name_and_directory_clash(cfg
);
2854 retcode
= ret
? 1 : 0;
2857 BT_OBJECT_PUT_REF_AND_RESET(cfg
);