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_TEXT_DETAILS_LOG_LEVEL",
55 "BABELTRACE_SRC_CTF_FS_LOG_LEVEL",
59 /* Application's processing graph (weak) */
60 static bt_graph
*the_graph
;
61 static bt_query_executor
*the_query_executor
;
62 static bool canceled
= false;
64 GPtrArray
*loaded_plugins
;
71 BOOL WINAPI
signal_handler(DWORD signal
) {
73 bt_graph_cancel(the_graph
);
82 void set_signal_handler(void)
84 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
85 BT_LOGE("Failed to set the Ctrl+C handler.");
89 #else /* __MINGW32__ */
92 void signal_handler(int signum
)
94 if (signum
!= SIGINT
) {
99 bt_graph_cancel(the_graph
);
102 if (the_query_executor
) {
103 bt_query_executor_cancel(the_query_executor
);
110 void set_signal_handler(void)
112 struct sigaction new_action
, old_action
;
114 new_action
.sa_handler
= signal_handler
;
115 sigemptyset(&new_action
.sa_mask
);
116 new_action
.sa_flags
= 0;
117 sigaction(SIGINT
, NULL
, &old_action
);
119 if (old_action
.sa_handler
!= SIG_IGN
) {
120 sigaction(SIGINT
, &new_action
, NULL
);
124 #endif /* __MINGW32__ */
127 void init_static_data(void)
129 loaded_plugins
= g_ptr_array_new_with_free_func(
130 (GDestroyNotify
) bt_object_put_ref
);
134 void fini_static_data(void)
136 g_ptr_array_free(loaded_plugins
, TRUE
);
140 int create_the_query_executor(void)
144 the_query_executor
= bt_query_executor_create();
145 if (!the_query_executor
) {
146 BT_LOGE_STR("Cannot create a query executor.");
154 void destroy_the_query_executor(void)
156 BT_QUERY_EXECUTOR_PUT_REF_AND_RESET(the_query_executor
);
160 int query(struct bt_config
*cfg
, const bt_component_class
*comp_cls
,
161 const char *obj
, const bt_value
*params
,
162 const bt_value
**user_result
, const char **fail_reason
)
164 const bt_value
*result
= NULL
;
165 bt_query_executor_status status
;
166 *fail_reason
= "unknown error";
169 BT_ASSERT(fail_reason
);
170 BT_ASSERT(user_result
);
171 ret
= create_the_query_executor();
173 /* create_the_query_executor() logs errors */
178 BT_LOGI("Canceled by user before executing the query: "
179 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
180 "query-obj=\"%s\"", comp_cls
,
181 bt_component_class_get_name(comp_cls
), obj
);
182 *fail_reason
= "canceled by user";
187 status
= bt_query_executor_query(the_query_executor
,
188 comp_cls
, obj
, params
, cfg
->log_level
, &result
);
190 case BT_QUERY_EXECUTOR_STATUS_OK
:
192 case BT_QUERY_EXECUTOR_STATUS_AGAIN
:
194 const uint64_t sleep_time_us
= 100000;
196 /* Wait 100 ms and retry */
197 BT_LOGD("Got BT_QUERY_EXECUTOR_STATUS_AGAIN: sleeping: "
198 "time-us=%" PRIu64
, sleep_time_us
);
200 if (usleep(sleep_time_us
)) {
201 if (bt_query_executor_is_canceled(the_query_executor
)) {
202 BT_LOGI("Query was canceled by user: "
203 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
204 "query-obj=\"%s\"", comp_cls
,
205 bt_component_class_get_name(comp_cls
),
207 *fail_reason
= "canceled by user";
214 case BT_QUERY_EXECUTOR_STATUS_CANCELED
:
215 *fail_reason
= "canceled by user";
217 case BT_QUERY_EXECUTOR_STATUS_ERROR
:
219 case BT_QUERY_EXECUTOR_STATUS_INVALID_OBJECT
:
220 *fail_reason
= "invalid or unknown query object";
222 case BT_QUERY_EXECUTOR_STATUS_INVALID_PARAMS
:
223 *fail_reason
= "invalid query parameters";
225 case BT_QUERY_EXECUTOR_STATUS_UNSUPPORTED
:
226 *fail_reason
= "unsupported action";
228 case BT_QUERY_EXECUTOR_STATUS_NOMEM
:
229 *fail_reason
= "not enough memory";
232 BT_LOGF("Unknown query status: status=%d", status
);
238 *user_result
= result
;
246 destroy_the_query_executor();
247 bt_value_put_ref(result
);
252 const bt_plugin
*find_plugin(const char *name
)
255 const bt_plugin
*plugin
= NULL
;
258 BT_LOGI("Finding plugin: name=\"%s\"", name
);
260 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
261 plugin
= g_ptr_array_index(loaded_plugins
, i
);
263 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
271 BT_LOGI("Found plugin: name=\"%s\", plugin-addr=%p",
274 BT_LOGI("Cannot find plugin: name=\"%s\"", name
);
277 bt_plugin_get_ref(plugin
);
281 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
282 const bt_plugin
*, const char *);
285 const void *find_component_class_from_plugin(const char *plugin_name
,
286 const char *comp_class_name
,
287 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
289 const void *comp_class
= NULL
;
290 const bt_plugin
*plugin
;
292 BT_LOGI("Finding component class: plugin-name=\"%s\", "
293 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
295 plugin
= find_plugin(plugin_name
);
300 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
301 bt_object_get_ref(comp_class
);
302 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
306 BT_LOGI("Found component class: plugin-name=\"%s\", "
307 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
309 BT_LOGI("Cannot find source component class: "
310 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
311 plugin_name
, comp_class_name
);
318 const bt_component_class_source
*find_source_component_class(
319 const char *plugin_name
, const char *comp_class_name
)
321 return (const void *) find_component_class_from_plugin(
322 plugin_name
, comp_class_name
,
323 (plugin_borrow_comp_cls_func_t
)
324 bt_plugin_borrow_source_component_class_by_name_const
);
328 const bt_component_class_filter
*find_filter_component_class(
329 const char *plugin_name
, const char *comp_class_name
)
331 return (const void *) find_component_class_from_plugin(
332 plugin_name
, comp_class_name
,
333 (plugin_borrow_comp_cls_func_t
)
334 bt_plugin_borrow_filter_component_class_by_name_const
);
338 const bt_component_class_sink
*find_sink_component_class(
339 const char *plugin_name
, const char *comp_class_name
)
341 return (const void *) find_component_class_from_plugin(plugin_name
,
343 (plugin_borrow_comp_cls_func_t
)
344 bt_plugin_borrow_sink_component_class_by_name_const
);
348 const bt_component_class
*find_component_class(const char *plugin_name
,
349 const char *comp_class_name
,
350 bt_component_class_type comp_class_type
)
352 const bt_component_class
*comp_cls
= NULL
;
354 switch (comp_class_type
) {
355 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
356 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
358 case BT_COMPONENT_CLASS_TYPE_FILTER
:
359 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
361 case BT_COMPONENT_CLASS_TYPE_SINK
:
362 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
372 void print_indent(FILE *fp
, size_t indent
)
376 for (i
= 0; i
< indent
; i
++) {
382 const char *component_type_str(bt_component_class_type type
)
385 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
387 case BT_COMPONENT_CLASS_TYPE_SINK
:
389 case BT_COMPONENT_CLASS_TYPE_FILTER
:
397 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
398 const char *comp_cls_name
, bt_component_class_type type
)
400 GString
*shell_plugin_name
= NULL
;
401 GString
*shell_comp_cls_name
= NULL
;
403 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
404 if (!shell_plugin_name
) {
408 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
409 if (!shell_comp_cls_name
) {
413 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
414 bt_common_color_bold(),
415 bt_common_color_fg_cyan(),
416 component_type_str(type
),
417 bt_common_color_fg_default(),
418 bt_common_color_fg_blue(),
419 shell_plugin_name
->str
,
420 bt_common_color_fg_default(),
421 bt_common_color_fg_yellow(),
422 shell_comp_cls_name
->str
,
423 bt_common_color_reset());
426 if (shell_plugin_name
) {
427 g_string_free(shell_plugin_name
, TRUE
);
430 if (shell_comp_cls_name
) {
431 g_string_free(shell_comp_cls_name
, TRUE
);
436 void print_value(FILE *, const bt_value
*, size_t);
439 void print_value_rec(FILE *, const bt_value
*, size_t);
441 struct print_map_value_data
{
447 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
450 struct print_map_value_data
*print_map_value_data
= data
;
452 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
453 fprintf(print_map_value_data
->fp
, "%s: ", key
);
456 if (bt_value_is_array(object
) &&
457 bt_value_array_is_empty(object
)) {
458 fprintf(print_map_value_data
->fp
, "[ ]\n");
462 if (bt_value_is_map(object
) &&
463 bt_value_map_is_empty(object
)) {
464 fprintf(print_map_value_data
->fp
, "{ }\n");
468 if (bt_value_is_array(object
) ||
469 bt_value_is_map(object
)) {
470 fprintf(print_map_value_data
->fp
, "\n");
473 print_value_rec(print_map_value_data
->fp
, object
,
474 print_map_value_data
->indent
+ 2);
479 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
493 switch (bt_value_get_type(value
)) {
494 case BT_VALUE_TYPE_NULL
:
495 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
496 bt_common_color_reset());
498 case BT_VALUE_TYPE_BOOL
:
499 bool_val
= bt_value_bool_get(value
);
500 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
501 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
502 bt_common_color_reset());
504 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
505 uint_val
= bt_value_unsigned_integer_get(value
);
506 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
507 bt_common_color_fg_red(), uint_val
,
508 bt_common_color_reset());
510 case BT_VALUE_TYPE_SIGNED_INTEGER
:
511 int_val
= bt_value_signed_integer_get(value
);
512 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
513 bt_common_color_fg_red(), int_val
,
514 bt_common_color_reset());
516 case BT_VALUE_TYPE_REAL
:
517 dbl_val
= bt_value_real_get(value
);
518 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
519 bt_common_color_fg_red(), dbl_val
,
520 bt_common_color_reset());
522 case BT_VALUE_TYPE_STRING
:
523 str_val
= bt_value_string_get(value
);
524 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
525 bt_common_color_fg_green(), str_val
,
526 bt_common_color_reset());
528 case BT_VALUE_TYPE_ARRAY
:
529 size
= bt_value_array_get_size(value
);
535 print_indent(fp
, indent
);
536 fprintf(fp
, "[ ]\n");
540 for (i
= 0; i
< size
; i
++) {
541 const bt_value
*element
=
542 bt_value_array_borrow_element_by_index_const(
548 print_indent(fp
, indent
);
551 if (bt_value_is_array(element
) &&
552 bt_value_array_is_empty(element
)) {
553 fprintf(fp
, "[ ]\n");
557 if (bt_value_is_map(element
) &&
558 bt_value_map_is_empty(element
)) {
559 fprintf(fp
, "{ }\n");
563 if (bt_value_is_array(element
) ||
564 bt_value_is_map(element
)) {
568 print_value_rec(fp
, element
, indent
+ 2);
571 case BT_VALUE_TYPE_MAP
:
573 struct print_map_value_data data
= {
578 if (bt_value_map_is_empty(value
)) {
579 print_indent(fp
, indent
);
580 fprintf(fp
, "{ }\n");
584 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
593 BT_LOGE("Error printing value of type %s.",
594 bt_common_value_type_string(bt_value_get_type(value
)));
598 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
600 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
601 print_indent(fp
, indent
);
604 print_value_rec(fp
, value
, indent
);
608 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
610 fprintf(stderr
, " ");
611 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
612 bt_config_component
->comp_cls_name
->str
,
613 bt_config_component
->type
);
614 fprintf(stderr
, ":\n");
616 if (bt_config_component
->instance_name
->len
> 0) {
617 fprintf(stderr
, " Name: %s\n",
618 bt_config_component
->instance_name
->str
);
621 fprintf(stderr
, " Parameters:\n");
622 print_value(stderr
, bt_config_component
->params
, 8);
626 void print_bt_config_components(GPtrArray
*array
)
630 for (i
= 0; i
< array
->len
; i
++) {
631 struct bt_config_component
*cfg_component
=
632 bt_config_get_component(array
, i
);
633 print_bt_config_component(cfg_component
);
634 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
639 void print_plugin_paths(const bt_value
*plugin_paths
)
641 fprintf(stderr
, " Plugin paths:\n");
642 print_value(stderr
, plugin_paths
, 4);
646 void print_cfg_run(struct bt_config
*cfg
)
650 print_plugin_paths(cfg
->plugin_paths
);
651 fprintf(stderr
, " Source component instances:\n");
652 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
654 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
655 fprintf(stderr
, " Filter component instances:\n");
656 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
659 fprintf(stderr
, " Sink component instances:\n");
660 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
661 fprintf(stderr
, " Connections:\n");
663 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
664 struct bt_config_connection
*cfg_connection
=
665 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
668 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
669 cfg_connection
->upstream_comp_name
->str
,
670 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
671 cfg_connection
->upstream_port_glob
->str
,
672 cfg_connection
->downstream_comp_name
->str
,
673 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
674 cfg_connection
->downstream_port_glob
->str
);
679 void print_cfg_list_plugins(struct bt_config
*cfg
)
681 print_plugin_paths(cfg
->plugin_paths
);
685 void print_cfg_help(struct bt_config
*cfg
)
687 print_plugin_paths(cfg
->plugin_paths
);
691 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
693 print_plugin_paths(cfg
->plugin_paths
);
694 fprintf(stderr
, " Path: %s\n",
695 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
699 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
701 print_plugin_paths(cfg
->plugin_paths
);
702 fprintf(stderr
, " URL: %s\n",
703 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
707 void print_cfg_query(struct bt_config
*cfg
)
709 print_plugin_paths(cfg
->plugin_paths
);
710 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
711 fprintf(stderr
, " Component class:\n");
712 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
716 void print_cfg(struct bt_config
*cfg
)
718 if (!BT_LOG_ON_INFO
) {
722 BT_LOGI_STR("CLI configuration:");
723 BT_LOGI(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
724 BT_LOGI(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
726 switch (cfg
->command
) {
727 case BT_CONFIG_COMMAND_RUN
:
730 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
731 print_cfg_list_plugins(cfg
);
733 case BT_CONFIG_COMMAND_HELP
:
736 case BT_CONFIG_COMMAND_QUERY
:
737 print_cfg_query(cfg
);
739 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
740 print_cfg_print_ctf_metadata(cfg
);
742 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
743 print_cfg_print_lttng_live_sessions(cfg
);
751 void add_to_loaded_plugins(const bt_plugin_set
*plugin_set
)
756 count
= bt_plugin_set_get_plugin_count(plugin_set
);
757 BT_ASSERT(count
>= 0);
759 for (i
= 0; i
< count
; i
++) {
760 const bt_plugin
*plugin
=
761 bt_plugin_set_borrow_plugin_by_index_const(plugin_set
, i
);
762 const bt_plugin
*loaded_plugin
=
763 find_plugin(bt_plugin_get_name(plugin
));
768 BT_LOGI("Not using plugin: another one already exists with the same name: "
769 "plugin-name=\"%s\", plugin-path=\"%s\", "
770 "existing-plugin-path=\"%s\"",
771 bt_plugin_get_name(plugin
),
772 bt_plugin_get_path(plugin
),
773 bt_plugin_get_path(loaded_plugin
));
774 bt_plugin_put_ref(loaded_plugin
);
776 /* Add to global array. */
777 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
778 bt_plugin_get_name(plugin
));
779 bt_plugin_get_ref(plugin
);
780 g_ptr_array_add(loaded_plugins
, (void *) plugin
);
786 int load_dynamic_plugins(const bt_value
*plugin_paths
)
788 int nr_paths
, i
, ret
= 0;
790 nr_paths
= bt_value_array_get_size(plugin_paths
);
792 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
797 BT_LOGI_STR("Loading dynamic plugins.");
799 for (i
= 0; i
< nr_paths
; i
++) {
800 const bt_value
*plugin_path_value
= NULL
;
801 const char *plugin_path
;
802 const bt_plugin_set
*plugin_set
;
805 bt_value_array_borrow_element_by_index_const(
807 plugin_path
= bt_value_string_get(plugin_path_value
);
810 * Skip this if the directory does not exist because
811 * bt_plugin_find_all_from_dir() expects an existing
814 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
815 BT_LOGI("Skipping nonexistent directory path: "
816 "path=\"%s\"", plugin_path
);
820 plugin_set
= bt_plugin_find_all_from_dir(plugin_path
, false);
822 BT_LOGI("Unable to load dynamic plugins from directory: "
823 "path=\"%s\"", plugin_path
);
827 add_to_loaded_plugins(plugin_set
);
828 bt_plugin_set_put_ref(plugin_set
);
835 int load_static_plugins(void)
838 const bt_plugin_set
*plugin_set
;
840 BT_LOGI("Loading static plugins.");
841 plugin_set
= bt_plugin_find_all_from_static();
843 BT_LOGE("Unable to load static plugins.");
848 add_to_loaded_plugins(plugin_set
);
849 bt_plugin_set_put_ref(plugin_set
);
855 int load_all_plugins(const bt_value
*plugin_paths
)
859 if (load_dynamic_plugins(plugin_paths
)) {
864 if (load_static_plugins()) {
869 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
876 void print_plugin_info(const bt_plugin
*plugin
)
878 unsigned int major
, minor
, patch
;
880 bt_property_availability version_avail
;
881 const char *plugin_name
;
885 const char *plugin_description
;
887 plugin_name
= bt_plugin_get_name(plugin
);
888 path
= bt_plugin_get_path(plugin
);
889 author
= bt_plugin_get_author(plugin
);
890 license
= bt_plugin_get_license(plugin
);
891 plugin_description
= bt_plugin_get_description(plugin
);
892 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
894 printf("%s%s%s%s:\n", bt_common_color_bold(),
895 bt_common_color_fg_blue(), plugin_name
,
896 bt_common_color_reset());
898 printf(" %sPath%s: %s\n", bt_common_color_bold(),
899 bt_common_color_reset(), path
);
904 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
905 printf(" %sVersion%s: %u.%u.%u",
906 bt_common_color_bold(), bt_common_color_reset(),
907 major
, minor
, patch
);
916 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
917 bt_common_color_reset(),
918 plugin_description
? plugin_description
: "(None)");
919 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
920 bt_common_color_reset(), author
? author
: "(Unknown)");
921 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
922 bt_common_color_reset(),
923 license
? license
: "(Unknown)");
927 int cmd_query(struct bt_config
*cfg
)
930 const bt_component_class
*comp_cls
= NULL
;
931 const bt_value
*results
= NULL
;
932 const char *fail_reason
= NULL
;
934 comp_cls
= find_component_class(
935 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
936 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
937 cfg
->cmd_data
.query
.cfg_component
->type
);
939 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
940 "comp-cls-name=\"%s\", comp-cls-type=%d",
941 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
942 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
943 cfg
->cmd_data
.query
.cfg_component
->type
);
944 fprintf(stderr
, "%s%sCannot find component class %s",
945 bt_common_color_bold(),
946 bt_common_color_fg_red(),
947 bt_common_color_reset());
948 print_plugin_comp_cls_opt(stderr
,
949 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
950 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
951 cfg
->cmd_data
.query
.cfg_component
->type
);
952 fprintf(stderr
, "\n");
957 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
958 cfg
->cmd_data
.query
.cfg_component
->params
,
959 &results
, &fail_reason
);
964 print_value(stdout
, results
, 0);
968 BT_LOGE("Failed to query component class: %s: plugin-name=\"%s\", "
969 "comp-cls-name=\"%s\", comp-cls-type=%d "
970 "object=\"%s\"", fail_reason
,
971 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
972 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
973 cfg
->cmd_data
.query
.cfg_component
->type
,
974 cfg
->cmd_data
.query
.object
->str
);
975 fprintf(stderr
, "%s%sFailed to query info to %s",
976 bt_common_color_bold(),
977 bt_common_color_fg_red(),
978 bt_common_color_reset());
979 print_plugin_comp_cls_opt(stderr
,
980 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
981 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
982 cfg
->cmd_data
.query
.cfg_component
->type
);
983 fprintf(stderr
, "%s%s with object `%s`: %s%s\n",
984 bt_common_color_bold(),
985 bt_common_color_fg_red(),
986 cfg
->cmd_data
.query
.object
->str
,
988 bt_common_color_reset());
992 bt_component_class_put_ref(comp_cls
);
993 bt_value_put_ref(results
);
998 void print_component_class_help(const char *plugin_name
,
999 const bt_component_class
*comp_cls
)
1001 const char *comp_class_name
=
1002 bt_component_class_get_name(comp_cls
);
1003 const char *comp_class_description
=
1004 bt_component_class_get_description(comp_cls
);
1005 const char *comp_class_help
=
1006 bt_component_class_get_help(comp_cls
);
1007 bt_component_class_type type
=
1008 bt_component_class_get_type(comp_cls
);
1010 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
1012 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
1013 bt_common_color_reset(),
1014 comp_class_description
? comp_class_description
: "(None)");
1016 if (comp_class_help
) {
1017 printf("\n%s\n", comp_class_help
);
1022 int cmd_help(struct bt_config
*cfg
)
1025 const bt_plugin
*plugin
= NULL
;
1026 const bt_component_class
*needed_comp_cls
= NULL
;
1028 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1030 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
1031 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
1032 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
1033 bt_common_color_bold(), bt_common_color_fg_red(),
1034 bt_common_color_fg_blue(),
1035 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1036 bt_common_color_reset());
1041 print_plugin_info(plugin
);
1042 printf(" %sSource component classes%s: %d\n",
1043 bt_common_color_bold(),
1044 bt_common_color_reset(),
1045 (int) bt_plugin_get_source_component_class_count(plugin
));
1046 printf(" %sFilter component classes%s: %d\n",
1047 bt_common_color_bold(),
1048 bt_common_color_reset(),
1049 (int) bt_plugin_get_filter_component_class_count(plugin
));
1050 printf(" %sSink component classes%s: %d\n",
1051 bt_common_color_bold(),
1052 bt_common_color_reset(),
1053 (int) bt_plugin_get_sink_component_class_count(plugin
));
1055 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
1056 /* Plugin help only */
1060 needed_comp_cls
= find_component_class(
1061 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1062 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1063 cfg
->cmd_data
.help
.cfg_component
->type
);
1064 if (!needed_comp_cls
) {
1065 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1066 "comp-cls-name=\"%s\", comp-cls-type=%d",
1067 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1068 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1069 cfg
->cmd_data
.help
.cfg_component
->type
);
1070 fprintf(stderr
, "\n%s%sCannot find component class %s",
1071 bt_common_color_bold(),
1072 bt_common_color_fg_red(),
1073 bt_common_color_reset());
1074 print_plugin_comp_cls_opt(stderr
,
1075 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1076 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
1077 cfg
->cmd_data
.help
.cfg_component
->type
);
1078 fprintf(stderr
, "\n");
1084 print_component_class_help(
1085 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
1089 bt_component_class_put_ref(needed_comp_cls
);
1090 bt_plugin_put_ref(plugin
);
1094 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
1096 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
1099 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
1100 const char *cc_type_name
, uint64_t count
,
1101 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
1102 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
1107 printf(" %s%s component classes%s: (none)\n",
1108 bt_common_color_bold(),
1110 bt_common_color_reset());
1113 printf(" %s%s component classes%s:\n",
1114 bt_common_color_bold(),
1116 bt_common_color_reset());
1119 for (i
= 0; i
< count
; i
++) {
1120 const bt_component_class
*comp_class
=
1121 spec_comp_cls_borrow_comp_cls_func(
1122 borrow_comp_cls_by_index_func(plugin
, i
));
1123 const char *comp_class_name
=
1124 bt_component_class_get_name(comp_class
);
1125 const char *comp_class_description
=
1126 bt_component_class_get_description(comp_class
);
1127 bt_component_class_type type
=
1128 bt_component_class_get_type(comp_class
);
1131 print_plugin_comp_cls_opt(stdout
,
1132 bt_plugin_get_name(plugin
), comp_class_name
,
1135 if (comp_class_description
) {
1136 printf(": %s", comp_class_description
);
1147 int cmd_list_plugins(struct bt_config
*cfg
)
1150 int plugins_count
, component_classes_count
= 0, i
;
1152 printf("From the following plugin paths:\n\n");
1153 print_value(stdout
, cfg
->plugin_paths
, 2);
1155 plugins_count
= loaded_plugins
->len
;
1156 if (plugins_count
== 0) {
1157 printf("No plugins found.\n");
1161 for (i
= 0; i
< plugins_count
; i
++) {
1162 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1164 component_classes_count
+=
1165 bt_plugin_get_source_component_class_count(plugin
) +
1166 bt_plugin_get_filter_component_class_count(plugin
) +
1167 bt_plugin_get_sink_component_class_count(plugin
);
1170 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
1171 bt_common_color_bold(),
1172 component_classes_count
,
1173 bt_common_color_reset(),
1174 bt_common_color_bold(),
1176 bt_common_color_reset());
1178 for (i
= 0; i
< plugins_count
; i
++) {
1179 const bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
1182 print_plugin_info(plugin
);
1183 cmd_list_plugins_print_component_classes(plugin
, "Source",
1184 bt_plugin_get_source_component_class_count(plugin
),
1185 (plugin_borrow_comp_cls_by_index_func_t
)
1186 bt_plugin_borrow_source_component_class_by_index_const
,
1187 (spec_comp_cls_borrow_comp_cls_func_t
)
1188 bt_component_class_source_as_component_class
);
1189 cmd_list_plugins_print_component_classes(plugin
, "Filter",
1190 bt_plugin_get_filter_component_class_count(plugin
),
1191 (plugin_borrow_comp_cls_by_index_func_t
)
1192 bt_plugin_borrow_filter_component_class_by_index_const
,
1193 (spec_comp_cls_borrow_comp_cls_func_t
)
1194 bt_component_class_filter_as_component_class
);
1195 cmd_list_plugins_print_component_classes(plugin
, "Sink",
1196 bt_plugin_get_sink_component_class_count(plugin
),
1197 (plugin_borrow_comp_cls_by_index_func_t
)
1198 bt_plugin_borrow_sink_component_class_by_index_const
,
1199 (spec_comp_cls_borrow_comp_cls_func_t
)
1200 bt_component_class_sink_as_component_class
);
1208 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1211 const bt_component_class
*comp_cls
= NULL
;
1212 const bt_value
*results
= NULL
;
1213 bt_value
*params
= NULL
;
1214 const bt_value
*map
= NULL
;
1215 const bt_value
*v
= NULL
;
1216 static const char * const plugin_name
= "ctf";
1217 static const char * const comp_cls_name
= "lttng-live";
1218 static const bt_component_class_type comp_cls_type
=
1219 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1220 int64_t array_size
, i
;
1221 const char *fail_reason
= NULL
;
1222 FILE *out_stream
= stdout
;
1224 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1225 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1228 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1229 "comp-cls-name=\"%s\", comp-cls-type=%d",
1230 plugin_name
, comp_cls_name
,
1231 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1232 fprintf(stderr
, "%s%sCannot find component class %s",
1233 bt_common_color_bold(),
1234 bt_common_color_fg_red(),
1235 bt_common_color_reset());
1236 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1237 comp_cls_name
, comp_cls_type
);
1238 fprintf(stderr
, "\n");
1242 params
= bt_value_map_create();
1247 ret
= bt_value_map_insert_string_entry(params
, "url",
1248 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1253 ret
= query(cfg
, comp_cls
, "sessions", params
,
1254 &results
, &fail_reason
);
1261 if (!bt_value_is_array(results
)) {
1262 BT_LOGE_STR("Expecting an array for sessions query.");
1263 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1264 bt_common_color_bold(),
1265 bt_common_color_fg_red(),
1266 bt_common_color_reset());
1270 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1272 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1276 BT_LOGE_ERRNO("Cannot open file for writing",
1278 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1283 array_size
= bt_value_array_get_size(results
);
1284 for (i
= 0; i
< array_size
; i
++) {
1285 const char *url_text
;
1286 int64_t timer_us
, streams
, clients
;
1288 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1290 BT_LOGE_STR("Unexpected empty array entry.");
1293 if (!bt_value_is_map(map
)) {
1294 BT_LOGE_STR("Unexpected entry type.");
1298 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1300 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1303 url_text
= bt_value_string_get(v
);
1304 fprintf(out_stream
, "%s", url_text
);
1305 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1307 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1310 timer_us
= bt_value_signed_integer_get(v
);
1311 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1312 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1314 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1317 streams
= bt_value_signed_integer_get(v
);
1318 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1319 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1321 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1324 clients
= bt_value_signed_integer_get(v
);
1325 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1331 BT_LOGE("Failed to query for sessions: %s", fail_reason
);
1332 fprintf(stderr
, "%s%sFailed to request sessions: %s%s\n",
1333 bt_common_color_bold(),
1334 bt_common_color_fg_red(),
1336 bt_common_color_reset());
1342 bt_value_put_ref(results
);
1343 bt_value_put_ref(params
);
1344 bt_component_class_put_ref(comp_cls
);
1346 if (out_stream
&& out_stream
!= stdout
) {
1347 int fclose_ret
= fclose(out_stream
);
1350 BT_LOGE_ERRNO("Cannot close file stream",
1352 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1360 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1363 const bt_component_class
*comp_cls
= NULL
;
1364 const bt_value
*results
= NULL
;
1365 bt_value
*params
= NULL
;
1366 const bt_value
*metadata_text_value
= NULL
;
1367 const char *metadata_text
= NULL
;
1368 static const char * const plugin_name
= "ctf";
1369 static const char * const comp_cls_name
= "fs";
1370 static const bt_component_class_type comp_cls_type
=
1371 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1372 const char *fail_reason
= NULL
;
1373 FILE *out_stream
= stdout
;
1375 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1376 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1379 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1380 "comp-cls-name=\"%s\", comp-cls-type=%d",
1381 plugin_name
, comp_cls_name
,
1382 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1383 fprintf(stderr
, "%s%sCannot find component class %s",
1384 bt_common_color_bold(),
1385 bt_common_color_fg_red(),
1386 bt_common_color_reset());
1387 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1388 comp_cls_name
, comp_cls_type
);
1389 fprintf(stderr
, "\n");
1394 params
= bt_value_map_create();
1400 ret
= bt_value_map_insert_string_entry(params
, "path",
1401 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1407 ret
= query(cfg
, comp_cls
, "metadata-info",
1408 params
, &results
, &fail_reason
);
1413 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1415 if (!metadata_text_value
) {
1416 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1421 metadata_text
= bt_value_string_get(metadata_text_value
);
1423 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1425 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1429 BT_LOGE_ERRNO("Cannot open file for writing",
1431 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1436 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1438 BT_LOGE("Cannot write whole metadata text to output stream: "
1449 BT_LOGE("Failed to query for metadata info: %s", fail_reason
);
1450 fprintf(stderr
, "%s%sFailed to request metadata info: %s%s\n",
1451 bt_common_color_bold(),
1452 bt_common_color_fg_red(),
1454 bt_common_color_reset());
1457 bt_value_put_ref(results
);
1458 bt_value_put_ref(params
);
1459 bt_component_class_put_ref(comp_cls
);
1461 if (out_stream
&& out_stream
!= stdout
) {
1462 int fclose_ret
= fclose(out_stream
);
1465 BT_LOGE_ERRNO("Cannot close file stream",
1467 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1475 char *instance_name
;
1479 struct trace_range
{
1480 uint64_t intersection_range_begin_ns
;
1481 uint64_t intersection_range_end_ns
;
1485 guint
port_id_hash(gconstpointer v
)
1487 const struct port_id
*id
= v
;
1489 BT_ASSERT(id
->instance_name
);
1490 BT_ASSERT(id
->port_name
);
1492 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1496 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1498 const struct port_id
*id1
= v1
;
1499 const struct port_id
*id2
= v2
;
1501 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1502 !strcmp(id1
->port_name
, id2
->port_name
);
1506 void port_id_destroy(gpointer data
)
1508 struct port_id
*id
= data
;
1510 free(id
->instance_name
);
1511 free(id
->port_name
);
1516 void trace_range_destroy(gpointer data
)
1521 struct cmd_run_ctx
{
1523 GHashTable
*src_components
;
1526 GHashTable
*flt_components
;
1529 GHashTable
*sink_components
;
1535 struct bt_config
*cfg
;
1539 bool stream_intersection_mode
;
1542 * Association of struct port_id -> struct trace_range.
1544 GHashTable
*intersections
;
1547 /* Returns a timestamp of the form "(-)s.ns" */
1549 char *s_from_ns(int64_t ns
)
1554 int64_t ts_sec_abs
, ts_nsec_abs
;
1555 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1556 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1558 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1559 is_negative
= false;
1560 ts_sec_abs
= ts_sec
;
1561 ts_nsec_abs
= ts_nsec
;
1562 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1563 is_negative
= false;
1564 ts_sec_abs
= ts_sec
- 1;
1565 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1566 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1568 ts_sec_abs
= ts_sec
;
1569 ts_nsec_abs
= -ts_nsec
;
1570 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1572 ts_sec_abs
= -(ts_sec
+ 1);
1573 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1574 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1576 ts_sec_abs
= -ts_sec
;
1577 ts_nsec_abs
= ts_nsec
;
1578 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1580 ts_sec_abs
= -ts_sec
;
1581 ts_nsec_abs
= -ts_nsec
;
1584 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1585 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1593 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1594 struct cmd_run_ctx
*ctx
,
1595 const bt_component
*upstream_comp
,
1596 const bt_port_output
*out_upstream_port
,
1597 struct bt_config_connection
*cfg_conn
)
1599 typedef uint64_t (*input_port_count_func_t
)(void *);
1600 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1601 const void *, uint64_t);
1602 const bt_port
*upstream_port
=
1603 bt_port_output_as_port_const(out_upstream_port
);
1606 GQuark downstreamp_comp_name_quark
;
1607 void *downstream_comp
;
1608 uint64_t downstream_port_count
;
1610 input_port_count_func_t port_count_fn
;
1611 borrow_input_port_by_index_func_t port_by_index_fn
;
1612 bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1613 bool insert_trimmer
= false;
1614 bt_value
*trimmer_params
= NULL
;
1615 char *intersection_begin
= NULL
;
1616 char *intersection_end
= NULL
;
1617 const bt_component_filter
*trimmer
= NULL
;
1618 const bt_component_class_filter
*trimmer_class
= NULL
;
1619 const bt_port_input
*trimmer_input
= NULL
;
1620 const bt_port_output
*trimmer_output
= NULL
;
1622 if (ctx
->intersections
&&
1623 bt_component_get_class_type(upstream_comp
) ==
1624 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1625 struct trace_range
*range
;
1626 struct port_id port_id
= {
1627 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1628 .port_name
= (char *) bt_port_get_name(upstream_port
)
1631 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1635 range
= (struct trace_range
*) g_hash_table_lookup(
1636 ctx
->intersections
, &port_id
);
1638 bt_value_status status
;
1640 intersection_begin
= s_from_ns(
1641 range
->intersection_range_begin_ns
);
1642 intersection_end
= s_from_ns(
1643 range
->intersection_range_end_ns
);
1644 if (!intersection_begin
|| !intersection_end
) {
1645 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1649 insert_trimmer
= true;
1650 trimmer_params
= bt_value_map_create();
1651 if (!trimmer_params
) {
1655 status
= bt_value_map_insert_string_entry(
1656 trimmer_params
, "begin", intersection_begin
);
1657 if (status
!= BT_VALUE_STATUS_OK
) {
1660 status
= bt_value_map_insert_string_entry(
1662 "end", intersection_end
);
1663 if (status
!= BT_VALUE_STATUS_OK
) {
1668 trimmer_class
= find_filter_component_class("utils", "trimmer");
1669 if (!trimmer_class
) {
1674 BT_LOGI("Connecting upstream port to the next available downstream port: "
1675 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1676 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1677 upstream_port
, bt_port_get_name(upstream_port
),
1678 cfg_conn
->downstream_comp_name
->str
,
1679 cfg_conn
->arg
->str
);
1680 downstreamp_comp_name_quark
= g_quark_from_string(
1681 cfg_conn
->downstream_comp_name
->str
);
1682 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1683 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1684 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1685 port_count_fn
= (input_port_count_func_t
)
1686 bt_component_filter_get_input_port_count
;
1687 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1688 bt_component_filter_borrow_input_port_by_index_const
;
1690 if (!downstream_comp
) {
1691 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1692 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1693 port_count_fn
= (input_port_count_func_t
)
1694 bt_component_sink_get_input_port_count
;
1695 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1696 bt_component_sink_borrow_input_port_by_index_const
;
1699 if (!downstream_comp
) {
1700 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1701 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1702 cfg_conn
->arg
->str
);
1703 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1704 cfg_conn
->arg
->str
);
1708 downstream_port_count
= port_count_fn(downstream_comp
);
1710 for (i
= 0; i
< downstream_port_count
; i
++) {
1711 const bt_port_input
*in_downstream_port
=
1712 port_by_index_fn(downstream_comp
, i
);
1713 const bt_port
*downstream_port
=
1714 bt_port_input_as_port_const(in_downstream_port
);
1715 const char *upstream_port_name
;
1716 const char *downstream_port_name
;
1718 BT_ASSERT(downstream_port
);
1720 /* Skip port if it's already connected. */
1721 if (bt_port_is_connected(downstream_port
)) {
1722 BT_LOGI("Skipping downstream port: already connected: "
1723 "port-addr=%p, port-name=\"%s\"",
1725 bt_port_get_name(downstream_port
));
1729 downstream_port_name
= bt_port_get_name(downstream_port
);
1730 BT_ASSERT(downstream_port_name
);
1731 upstream_port_name
= bt_port_get_name(upstream_port
);
1732 BT_ASSERT(upstream_port_name
);
1734 if (!bt_common_star_glob_match(
1735 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1736 downstream_port_name
, SIZE_MAX
)) {
1740 if (insert_trimmer
) {
1742 * In order to insert the trimmer between the
1743 * two components that were being connected, we
1744 * create a connection configuration entry which
1745 * describes a connection from the trimmer's
1746 * output to the original input that was being
1749 * Hence, the creation of the trimmer will cause
1750 * the graph "new port" listener to establish
1751 * all downstream connections as its output port
1752 * is connected. We will then establish the
1753 * connection between the original upstream
1754 * source and the trimmer.
1756 char *trimmer_name
= NULL
;
1757 bt_graph_status graph_status
;
1759 ret
= asprintf(&trimmer_name
,
1760 "stream-intersection-trimmer-%s",
1761 upstream_port_name
);
1767 ctx
->connect_ports
= false;
1768 graph_status
= bt_graph_add_filter_component(
1769 ctx
->graph
, trimmer_class
, trimmer_name
,
1770 trimmer_params
, ctx
->cfg
->log_level
,
1773 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1779 bt_component_filter_borrow_input_port_by_index_const(
1781 if (!trimmer_input
) {
1785 bt_component_filter_borrow_output_port_by_index_const(
1787 if (!trimmer_output
) {
1792 * Replace the current downstream port by the trimmer's
1795 in_downstream_port
= trimmer_input
;
1797 bt_port_input_as_port_const(in_downstream_port
);
1798 downstream_port_name
= bt_port_get_name(
1800 BT_ASSERT(downstream_port_name
);
1803 /* We have a winner! */
1804 status
= bt_graph_connect_ports(ctx
->graph
,
1805 out_upstream_port
, in_downstream_port
, NULL
);
1806 downstream_port
= NULL
;
1808 case BT_GRAPH_STATUS_OK
:
1810 case BT_GRAPH_STATUS_CANCELED
:
1811 BT_LOGI_STR("Graph was canceled by user.");
1812 status
= BT_GRAPH_STATUS_OK
;
1814 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1815 BT_LOGE("A component refused a connection to one of its ports: "
1816 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1817 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1818 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1819 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1821 upstream_comp
, bt_component_get_name(upstream_comp
),
1822 upstream_port
, bt_port_get_name(upstream_port
),
1823 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1824 downstream_port
, downstream_port_name
,
1825 cfg_conn
->arg
->str
);
1827 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1828 bt_port_get_name(upstream_port
),
1829 downstream_port_name
,
1830 cfg_conn
->arg
->str
);
1833 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1834 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1835 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1836 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1837 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1839 upstream_comp
, bt_component_get_name(upstream_comp
),
1840 upstream_port
, bt_port_get_name(upstream_port
),
1841 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1842 downstream_port
, downstream_port_name
,
1843 cfg_conn
->arg
->str
);
1845 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1846 bt_port_get_name(upstream_port
),
1847 downstream_port_name
,
1848 cfg_conn
->arg
->str
);
1852 BT_LOGI("Connected component ports: "
1853 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1854 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1855 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1856 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1858 upstream_comp
, bt_component_get_name(upstream_comp
),
1859 upstream_port
, bt_port_get_name(upstream_port
),
1860 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1861 downstream_port
, downstream_port_name
,
1862 cfg_conn
->arg
->str
);
1864 if (insert_trimmer
) {
1866 * The first connection, from the source to the trimmer,
1867 * has been done. We now connect the trimmer to the
1868 * original downstream port.
1870 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1872 bt_component_filter_as_component_const(trimmer
),
1873 trimmer_output
, cfg_conn
);
1877 ctx
->connect_ports
= true;
1881 * We found a matching downstream port: the search is
1887 /* No downstream port found */
1888 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1889 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1890 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1891 upstream_port
, bt_port_get_name(upstream_port
),
1892 cfg_conn
->downstream_comp_name
->str
,
1893 cfg_conn
->arg
->str
);
1895 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1896 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1902 free(intersection_begin
);
1903 free(intersection_end
);
1904 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1905 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1906 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1911 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1912 const bt_port_output
*upstream_port
)
1915 const char *upstream_port_name
;
1916 const char *upstream_comp_name
;
1917 const bt_component
*upstream_comp
= NULL
;
1921 BT_ASSERT(upstream_port
);
1922 upstream_port_name
= bt_port_get_name(
1923 bt_port_output_as_port_const(upstream_port
));
1924 BT_ASSERT(upstream_port_name
);
1925 upstream_comp
= bt_port_borrow_component_const(
1926 bt_port_output_as_port_const(upstream_port
));
1927 if (!upstream_comp
) {
1928 BT_LOGW("Upstream port to connect is not part of a component: "
1929 "port-addr=%p, port-name=\"%s\"",
1930 upstream_port
, upstream_port_name
);
1935 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1936 BT_ASSERT(upstream_comp_name
);
1937 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1938 "port-addr=%p, port-name=\"%s\"",
1939 upstream_comp
, upstream_comp_name
,
1940 upstream_port
, upstream_port_name
);
1942 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1943 struct bt_config_connection
*cfg_conn
=
1945 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1947 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1948 upstream_comp_name
)) {
1952 if (!bt_common_star_glob_match(
1953 cfg_conn
->upstream_port_glob
->str
,
1954 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1958 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1959 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1961 BT_LOGE("Cannot connect upstream port: "
1962 "port-addr=%p, port-name=\"%s\"",
1964 upstream_port_name
);
1966 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1969 cfg_conn
->arg
->str
);
1975 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1976 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1977 upstream_port_name
);
1979 "Cannot create connection: upstream port `%s` does not match any connection\n",
1980 upstream_port_name
);
1990 bt_graph_listener_status
1991 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1992 const bt_port_output
*out_port
)
1994 const bt_component
*comp
;
1995 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1996 bt_graph_listener_status ret
= BT_GRAPH_LISTENER_STATUS_OK
;
1998 comp
= bt_port_borrow_component_const(port
);
1999 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
2000 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
2001 comp
, comp
? bt_component_get_name(comp
) : "",
2002 port
, bt_port_get_name(port
));
2004 if (!ctx
->connect_ports
) {
2009 BT_LOGW_STR("Port has no component.");
2013 if (bt_port_is_connected(port
)) {
2014 BT_LOGW_STR("Port is already connected.");
2018 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
2019 BT_LOGF_STR("Cannot connect upstream port.");
2020 fprintf(stderr
, "Added port could not be connected: aborting\n");
2021 ret
= BT_GRAPH_LISTENER_STATUS_ERROR
;
2030 bt_graph_listener_status
graph_source_output_port_added_listener(
2031 const bt_component_source
*component
,
2032 const bt_port_output
*port
, void *data
)
2034 return graph_output_port_added_listener(data
, port
);
2038 bt_graph_listener_status
graph_filter_output_port_added_listener(
2039 const bt_component_filter
*component
,
2040 const bt_port_output
*port
, void *data
)
2042 return graph_output_port_added_listener(data
, port
);
2046 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
2052 if (ctx
->src_components
) {
2053 g_hash_table_destroy(ctx
->src_components
);
2054 ctx
->src_components
= NULL
;
2057 if (ctx
->flt_components
) {
2058 g_hash_table_destroy(ctx
->flt_components
);
2059 ctx
->flt_components
= NULL
;
2062 if (ctx
->sink_components
) {
2063 g_hash_table_destroy(ctx
->sink_components
);
2064 ctx
->sink_components
= NULL
;
2067 if (ctx
->intersections
) {
2068 g_hash_table_destroy(ctx
->intersections
);
2069 ctx
->intersections
= NULL
;
2072 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
2078 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
2081 bt_graph_status status
;
2084 ctx
->connect_ports
= false;
2085 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
2086 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2087 if (!ctx
->src_components
) {
2091 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
2092 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2093 if (!ctx
->flt_components
) {
2097 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
2098 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
2099 if (!ctx
->sink_components
) {
2103 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
2104 ctx
->stream_intersection_mode
= true;
2105 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
2106 port_id_equal
, port_id_destroy
, trace_range_destroy
);
2107 if (!ctx
->intersections
) {
2112 ctx
->graph
= bt_graph_create();
2117 the_graph
= ctx
->graph
;
2118 status
= bt_graph_add_source_component_output_port_added_listener(
2119 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
2121 if (status
!= BT_GRAPH_STATUS_OK
) {
2122 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2126 status
= bt_graph_add_filter_component_output_port_added_listener(
2127 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
2129 if (status
!= BT_GRAPH_STATUS_OK
) {
2130 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
2137 cmd_run_ctx_destroy(ctx
);
2145 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2146 struct bt_config_component
*cfg_comp
,
2147 const bt_component_class_source
*src_comp_cls
)
2151 int64_t trace_count
;
2152 const char *path
= NULL
;
2153 const bt_value
*query_result
= NULL
;
2154 const bt_value
*trace_info
= NULL
;
2155 const bt_value
*intersection_range
= NULL
;
2156 const bt_value
*intersection_begin
= NULL
;
2157 const bt_value
*intersection_end
= NULL
;
2158 const bt_value
*stream_infos
= NULL
;
2159 const bt_value
*stream_info
= NULL
;
2160 struct port_id
*port_id
= NULL
;
2161 struct trace_range
*trace_range
= NULL
;
2162 const char *fail_reason
= NULL
;
2163 const bt_component_class
*comp_cls
=
2164 bt_component_class_source_as_component_class_const(src_comp_cls
);
2166 ret
= query(ctx
->cfg
, comp_cls
, "trace-info",
2167 cfg_comp
->params
, &query_result
,
2170 BT_LOGD("Component class does not support the `trace-info` query: %s: "
2171 "comp-class-name=\"%s\"", fail_reason
,
2172 bt_component_class_get_name(comp_cls
));
2177 BT_ASSERT(query_result
);
2179 if (!bt_value_is_array(query_result
)) {
2180 BT_LOGD("Unexpected format of \'trace-info\' query result: "
2181 "component-class-name=%s",
2182 bt_component_class_get_name(comp_cls
));
2187 trace_count
= bt_value_array_get_size(query_result
);
2188 if (trace_count
< 0) {
2193 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2195 uint64_t stream_idx
;
2196 int64_t stream_count
;
2198 trace_info
= bt_value_array_borrow_element_by_index_const(
2199 query_result
, trace_idx
);
2200 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
2202 BT_LOGD_STR("Cannot retrieve trace from query result.");
2206 intersection_range
= bt_value_map_borrow_entry_value_const(
2207 trace_info
, "intersection-range-ns");
2208 if (!intersection_range
) {
2210 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
2214 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
2216 if (!intersection_begin
) {
2218 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2222 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
2224 if (!intersection_end
) {
2226 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2230 begin
= bt_value_signed_integer_get(intersection_begin
);
2231 end
= bt_value_signed_integer_get(intersection_end
);
2233 if (begin
< 0 || end
< 0 || end
< begin
) {
2234 BT_LOGW("Invalid trace stream intersection values: "
2235 "intersection-range-ns:begin=%" PRId64
2236 ", intersection-range-ns:end=%" PRId64
,
2242 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
2244 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2246 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2250 stream_count
= bt_value_array_get_size(stream_infos
);
2251 if (stream_count
< 0) {
2256 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2257 const bt_value
*port_name
;
2259 port_id
= g_new0(struct port_id
, 1);
2262 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2265 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2266 if (!port_id
->instance_name
) {
2268 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2272 trace_range
= g_new0(struct trace_range
, 1);
2275 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2278 trace_range
->intersection_range_begin_ns
= begin
;
2279 trace_range
->intersection_range_end_ns
= end
;
2281 stream_info
= bt_value_array_borrow_element_by_index_const(
2282 stream_infos
, stream_idx
);
2283 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2285 BT_LOGE_STR("Cannot retrieve stream informations from trace in query result.");
2289 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2290 if (!port_name
|| !bt_value_is_string(port_name
)) {
2292 BT_LOGE_STR("Cannot retrieve port name in query result.");
2296 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2297 if (!port_id
->port_name
) {
2299 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2303 BT_LOGD("Inserting stream intersection ");
2305 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2315 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2316 bt_common_color_bold(),
2317 bt_common_color_fg_yellow(),
2318 path
? path
: "(unknown)",
2319 bt_common_color_reset());
2321 bt_value_put_ref(query_result
);
2323 g_free(trace_range
);
2328 int cmd_run_ctx_create_components_from_config_components(
2329 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2332 const void *comp_cls
= NULL
;
2333 const void *comp
= NULL
;
2336 for (i
= 0; i
< cfg_components
->len
; i
++) {
2337 struct bt_config_component
*cfg_comp
=
2338 g_ptr_array_index(cfg_components
, i
);
2341 switch (cfg_comp
->type
) {
2342 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2343 comp_cls
= find_source_component_class(
2344 cfg_comp
->plugin_name
->str
,
2345 cfg_comp
->comp_cls_name
->str
);
2347 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2348 comp_cls
= find_filter_component_class(
2349 cfg_comp
->plugin_name
->str
,
2350 cfg_comp
->comp_cls_name
->str
);
2352 case BT_COMPONENT_CLASS_TYPE_SINK
:
2353 comp_cls
= find_sink_component_class(
2354 cfg_comp
->plugin_name
->str
,
2355 cfg_comp
->comp_cls_name
->str
);
2362 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2363 "comp-cls-name=\"%s\", comp-cls-type=%d",
2364 cfg_comp
->plugin_name
->str
,
2365 cfg_comp
->comp_cls_name
->str
,
2367 fprintf(stderr
, "%s%sCannot find component class %s",
2368 bt_common_color_bold(),
2369 bt_common_color_fg_red(),
2370 bt_common_color_reset());
2371 print_plugin_comp_cls_opt(stderr
,
2372 cfg_comp
->plugin_name
->str
,
2373 cfg_comp
->comp_cls_name
->str
,
2375 fprintf(stderr
, "\n");
2379 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_VERBOSE
);
2381 switch (cfg_comp
->type
) {
2382 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2383 ret
= bt_graph_add_source_component(ctx
->graph
,
2384 comp_cls
, cfg_comp
->instance_name
->str
,
2385 cfg_comp
->params
, cfg_comp
->log_level
,
2388 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2389 ret
= bt_graph_add_filter_component(ctx
->graph
,
2390 comp_cls
, cfg_comp
->instance_name
->str
,
2391 cfg_comp
->params
, cfg_comp
->log_level
,
2394 case BT_COMPONENT_CLASS_TYPE_SINK
:
2395 ret
= bt_graph_add_sink_component(ctx
->graph
,
2396 comp_cls
, cfg_comp
->instance_name
->str
,
2397 cfg_comp
->params
, cfg_comp
->log_level
,
2405 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2406 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2408 cfg_comp
->plugin_name
->str
,
2409 cfg_comp
->comp_cls_name
->str
,
2410 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2411 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2412 bt_common_color_bold(),
2413 bt_common_color_fg_red(),
2414 cfg_comp
->instance_name
->str
,
2415 bt_common_color_reset());
2419 if (ctx
->stream_intersection_mode
&&
2420 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2421 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2427 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2428 comp
, cfg_comp
->instance_name
->str
);
2429 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2430 BT_ASSERT(quark
> 0);
2432 switch (cfg_comp
->type
) {
2433 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2434 g_hash_table_insert(ctx
->src_components
,
2435 GUINT_TO_POINTER(quark
), (void *) comp
);
2437 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2438 g_hash_table_insert(ctx
->flt_components
,
2439 GUINT_TO_POINTER(quark
), (void *) comp
);
2441 case BT_COMPONENT_CLASS_TYPE_SINK
:
2442 g_hash_table_insert(ctx
->sink_components
,
2443 GUINT_TO_POINTER(quark
), (void *) comp
);
2450 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2459 bt_object_put_ref(comp
);
2460 bt_object_put_ref(comp_cls
);
2465 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2470 * Make sure that, during this phase, our graph's "port added"
2471 * listener does not connect ports while we are creating the
2472 * components because we have a special, initial phase for
2475 ctx
->connect_ports
= false;
2477 ret
= cmd_run_ctx_create_components_from_config_components(
2478 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2484 ret
= cmd_run_ctx_create_components_from_config_components(
2485 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2491 ret
= cmd_run_ctx_create_components_from_config_components(
2492 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2502 typedef uint64_t (*output_port_count_func_t
)(const void *);
2503 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2504 const void *, uint64_t);
2507 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2508 void *comp
, output_port_count_func_t port_count_fn
,
2509 borrow_output_port_by_index_func_t port_by_index_fn
)
2515 count
= port_count_fn(comp
);
2517 for (i
= 0; i
< count
; i
++) {
2518 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2520 BT_ASSERT(upstream_port
);
2521 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2532 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2535 GHashTableIter iter
;
2536 gpointer g_name_quark
, g_comp
;
2538 ctx
->connect_ports
= true;
2539 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2541 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2542 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2543 (output_port_count_func_t
)
2544 bt_component_source_get_output_port_count
,
2545 (borrow_output_port_by_index_func_t
)
2546 bt_component_source_borrow_output_port_by_index_const
);
2552 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2554 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2555 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2556 (output_port_count_func_t
)
2557 bt_component_filter_get_output_port_count
,
2558 (borrow_output_port_by_index_func_t
)
2559 bt_component_filter_borrow_output_port_by_index_const
);
2570 const char *bt_graph_status_str(bt_graph_status status
)
2573 case BT_GRAPH_STATUS_OK
:
2574 return "BT_GRAPH_STATUS_OK";
2575 case BT_GRAPH_STATUS_END
:
2576 return "BT_GRAPH_STATUS_END";
2577 case BT_GRAPH_STATUS_AGAIN
:
2578 return "BT_GRAPH_STATUS_AGAIN";
2579 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
2580 return "BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION";
2581 case BT_GRAPH_STATUS_CANCELED
:
2582 return "BT_GRAPH_STATUS_CANCELED";
2583 case BT_GRAPH_STATUS_ERROR
:
2584 return "BT_GRAPH_STATUS_ERROR";
2585 case BT_GRAPH_STATUS_NOMEM
:
2586 return "BT_GRAPH_STATUS_NOMEM";
2593 int cmd_run(struct bt_config
*cfg
)
2596 struct cmd_run_ctx ctx
= { 0 };
2598 /* Initialize the command's context and the graph object */
2599 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2600 BT_LOGE_STR("Cannot initialize the command's context.");
2601 fprintf(stderr
, "Cannot initialize the command's context\n");
2606 BT_LOGI_STR("Canceled by user before creating components.");
2610 BT_LOGI_STR("Creating components.");
2612 /* Create the requested component instances */
2613 if (cmd_run_ctx_create_components(&ctx
)) {
2614 BT_LOGE_STR("Cannot create components.");
2615 fprintf(stderr
, "Cannot create components\n");
2620 BT_LOGI_STR("Canceled by user before connecting components.");
2624 BT_LOGI_STR("Connecting components.");
2626 /* Connect the initially visible component ports */
2627 if (cmd_run_ctx_connect_ports(&ctx
)) {
2628 BT_LOGE_STR("Cannot connect initial component ports.");
2629 fprintf(stderr
, "Cannot connect initial component ports\n");
2634 BT_LOGI_STR("Canceled by user before running the graph.");
2638 BT_LOGI_STR("Running the graph.");
2642 bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2645 * Reset console in case something messed with console
2646 * codes during the graph's execution.
2648 printf("%s", bt_common_color_reset());
2650 fprintf(stderr
, "%s", bt_common_color_reset());
2651 BT_LOGV("bt_graph_run() returned: status=%s",
2652 bt_graph_status_str(graph_status
));
2654 switch (graph_status
) {
2655 case BT_GRAPH_STATUS_OK
:
2657 case BT_GRAPH_STATUS_CANCELED
:
2658 BT_LOGI_STR("Graph was canceled by user.");
2660 case BT_GRAPH_STATUS_AGAIN
:
2661 if (bt_graph_is_canceled(ctx
.graph
)) {
2662 BT_LOGI_STR("Graph was canceled by user.");
2666 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2667 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2669 cfg
->cmd_data
.run
.retry_duration_us
);
2671 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2672 if (bt_graph_is_canceled(ctx
.graph
)) {
2673 BT_LOGI_STR("Graph was canceled by user.");
2679 case BT_GRAPH_STATUS_END
:
2682 BT_LOGE_STR("Graph failed to complete successfully");
2683 fprintf(stderr
, "Graph failed to complete successfully\n");
2696 cmd_run_ctx_destroy(&ctx
);
2701 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2703 const char *env_clash
;
2705 if (!cfg
->command_name
) {
2709 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2710 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2714 if (g_file_test(cfg
->command_name
,
2715 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2716 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2718 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2720 fprintf(stderr
, "\n");
2721 fprintf(stderr
, " babeltrace2 convert %s [OPTIONS]\n",
2727 void init_log_level(void)
2729 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2733 void set_auto_log_levels(struct bt_config
*cfg
)
2735 const char **env_var_name
;
2738 * Override the configuration's default log level if
2739 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2740 * are found for backward compatibility with legacy Babetrace 1.
2742 if (getenv("BABELTRACE_DEBUG") &&
2743 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2744 cfg
->log_level
= BT_LOG_VERBOSE
;
2745 } else if (getenv("BABELTRACE_VERBOSE") &&
2746 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2747 cfg
->log_level
= BT_LOG_INFO
;
2751 * Set log levels according to --debug or --verbose. For
2752 * backward compatibility, --debug is more verbose than
2755 * --verbose: INFO log level
2756 * --debug: VERBOSE log level (includes DEBUG, which is
2757 * is less verbose than VERBOSE in the internal
2758 * logging framework)
2760 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2762 bt_logging_set_global_level(BT_LOG_INFO
);
2763 } else if (cfg
->debug
) {
2764 bt_logging_set_global_level(BT_LOG_VERBOSE
);
2767 * Set library's default log level if not
2768 * explicitly specified.
2770 bt_logging_set_global_level(cfg
->log_level
);
2774 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2776 bt_cli_log_level
= BT_LOG_INFO
;
2777 } else if (cfg
->debug
) {
2778 bt_cli_log_level
= BT_LOG_VERBOSE
;
2781 * Set CLI's default log level if not explicitly
2784 bt_cli_log_level
= cfg
->log_level
;
2788 env_var_name
= log_level_env_var_names
;
2790 while (*env_var_name
) {
2791 if (!getenv(*env_var_name
)) {
2793 g_setenv(*env_var_name
, "I", 1);
2794 } else if (cfg
->debug
) {
2795 g_setenv(*env_var_name
, "V", 1);
2797 char val
[2] = { 0 };
2800 * Set module's default log level if not
2801 * explicitly specified.
2803 val
[0] = bt_log_get_letter_from_level(
2805 g_setenv(*env_var_name
, val
, 1);
2813 int main(int argc
, const char **argv
)
2817 struct bt_config
*cfg
;
2820 set_signal_handler();
2822 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2825 /* Quit without errors; typically usage/version */
2827 BT_LOGI_STR("Quitting without errors.");
2832 BT_LOGE("Command-line error: retcode=%d", retcode
);
2837 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2838 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2843 set_auto_log_levels(cfg
);
2846 if (cfg
->command_needs_plugins
) {
2847 ret
= load_all_plugins(cfg
->plugin_paths
);
2849 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2855 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2856 cfg
->command
, cfg
->command_name
);
2858 switch (cfg
->command
) {
2859 case BT_CONFIG_COMMAND_RUN
:
2862 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2863 ret
= cmd_list_plugins(cfg
);
2865 case BT_CONFIG_COMMAND_HELP
:
2866 ret
= cmd_help(cfg
);
2868 case BT_CONFIG_COMMAND_QUERY
:
2869 ret
= cmd_query(cfg
);
2871 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2872 ret
= cmd_print_ctf_metadata(cfg
);
2874 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2875 ret
= cmd_print_lttng_live_sessions(cfg
);
2878 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2882 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2883 cfg
->command
, cfg
->command_name
, ret
);
2884 warn_command_name_and_directory_clash(cfg
);
2885 retcode
= ret
? 1 : 0;
2888 BT_OBJECT_PUT_REF_AND_RESET(cfg
);