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"
42 #include "babeltrace2-plugins.h"
44 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
45 #define ENV_BABELTRACE_CLI_LOG_LEVEL "BABELTRACE_CLI_LOG_LEVEL"
46 #define NSEC_PER_SEC 1000000000LL
49 * Known environment variable names for the log levels of the project's
52 static const char* log_level_env_var_names
[] = {
53 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
54 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
58 /* Application's interrupter (owned by this) */
59 static bt_interrupter
*the_interrupter
;
60 static volatile bool interrupted
= false;
67 BOOL WINAPI
signal_handler(DWORD signal
) {
68 if (the_interrupter
) {
69 bt_interrupter_set(the_interrupter
);
77 void set_signal_handler(void)
79 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
80 BT_LOGE("Failed to set the Ctrl+C handler.");
84 #else /* __MINGW32__ */
87 void signal_handler(int signum
)
89 if (signum
!= SIGINT
) {
93 if (the_interrupter
) {
94 bt_interrupter_set(the_interrupter
);
101 void set_signal_handler(void)
103 struct sigaction new_action
, old_action
;
105 new_action
.sa_handler
= signal_handler
;
106 sigemptyset(&new_action
.sa_mask
);
107 new_action
.sa_flags
= 0;
108 sigaction(SIGINT
, NULL
, &old_action
);
110 if (old_action
.sa_handler
!= SIG_IGN
) {
111 sigaction(SIGINT
, &new_action
, NULL
);
115 #endif /* __MINGW32__ */
118 int query(struct bt_config
*cfg
, const bt_component_class
*comp_cls
,
119 const char *obj
, const bt_value
*params
,
120 const bt_value
**user_result
, const char **fail_reason
)
122 const bt_value
*result
= NULL
;
123 bt_query_executor_query_status query_status
;
124 bt_query_executor
*query_exec
;
125 *fail_reason
= "unknown error";
128 BT_ASSERT(fail_reason
);
129 BT_ASSERT(user_result
);
130 query_exec
= bt_query_executor_create();
132 BT_CLI_LOGE_APPEND_CAUSE("Cannot create a query executor.");
136 bt_query_executor_add_interrupter(query_exec
, the_interrupter
);
139 BT_CLI_LOGW_APPEND_CAUSE(
140 "Interrupted by user before executing the query: "
141 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
142 "query-obj=\"%s\"", comp_cls
,
143 bt_component_class_get_name(comp_cls
), obj
);
144 *fail_reason
= "interrupted by user";
149 query_status
= bt_query_executor_query(
150 query_exec
, comp_cls
, obj
, params
,
151 cfg
->log_level
, &result
);
152 switch (query_status
) {
153 case BT_QUERY_EXECUTOR_QUERY_STATUS_OK
:
155 case BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN
:
157 const uint64_t sleep_time_us
= 100000;
159 if (bt_interrupter_is_set(the_interrupter
)) {
160 *fail_reason
= "interrupted by user";
164 /* Wait 100 ms and retry */
165 BT_LOGD("Got BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN: sleeping: "
166 "time-us=%" PRIu64
, sleep_time_us
);
168 if (usleep(sleep_time_us
)) {
169 if (bt_interrupter_is_set(the_interrupter
)) {
170 BT_CLI_LOGW_APPEND_CAUSE(
171 "Query was interrupted by user: "
172 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
173 "query-obj=\"%s\"", comp_cls
,
174 bt_component_class_get_name(comp_cls
),
176 *fail_reason
= "interrupted by user";
183 case BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR
:
184 if (bt_interrupter_is_set(the_interrupter
)) {
185 *fail_reason
= "interrupted by user";
190 case BT_QUERY_EXECUTOR_QUERY_STATUS_INVALID_OBJECT
:
191 *fail_reason
= "invalid or unknown query object";
193 case BT_QUERY_EXECUTOR_QUERY_STATUS_INVALID_PARAMS
:
194 *fail_reason
= "invalid query parameters";
196 case BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR
:
197 *fail_reason
= "not enough memory";
200 BT_LOGF("Unknown query status: status=%s",
201 bt_common_func_status_string(query_status
));
207 *user_result
= result
;
215 bt_query_executor_put_ref(query_exec
);
216 bt_value_put_ref(result
);
222 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
223 const bt_plugin
*, const char *);
226 const void *find_component_class_from_plugin(const char *plugin_name
,
227 const char *comp_class_name
,
228 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
230 const void *comp_class
= NULL
;
231 const bt_plugin
*plugin
;
233 BT_LOGI("Finding component class: plugin-name=\"%s\", "
234 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
236 plugin
= find_loaded_plugin(plugin_name
);
241 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
242 bt_object_get_ref(comp_class
);
243 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
247 BT_LOGI("Found component class: plugin-name=\"%s\", "
248 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
250 BT_LOGI("Cannot find source component class: "
251 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
252 plugin_name
, comp_class_name
);
259 const bt_component_class_source
*find_source_component_class(
260 const char *plugin_name
, const char *comp_class_name
)
262 return (const void *) find_component_class_from_plugin(
263 plugin_name
, comp_class_name
,
264 (plugin_borrow_comp_cls_func_t
)
265 bt_plugin_borrow_source_component_class_by_name_const
);
269 const bt_component_class_filter
*find_filter_component_class(
270 const char *plugin_name
, const char *comp_class_name
)
272 return (const void *) find_component_class_from_plugin(
273 plugin_name
, comp_class_name
,
274 (plugin_borrow_comp_cls_func_t
)
275 bt_plugin_borrow_filter_component_class_by_name_const
);
279 const bt_component_class_sink
*find_sink_component_class(
280 const char *plugin_name
, const char *comp_class_name
)
282 return (const void *) find_component_class_from_plugin(plugin_name
,
284 (plugin_borrow_comp_cls_func_t
)
285 bt_plugin_borrow_sink_component_class_by_name_const
);
289 const bt_component_class
*find_component_class(const char *plugin_name
,
290 const char *comp_class_name
,
291 bt_component_class_type comp_class_type
)
293 const bt_component_class
*comp_cls
= NULL
;
295 switch (comp_class_type
) {
296 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
297 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
299 case BT_COMPONENT_CLASS_TYPE_FILTER
:
300 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
302 case BT_COMPONENT_CLASS_TYPE_SINK
:
303 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
313 void print_indent(FILE *fp
, size_t indent
)
317 for (i
= 0; i
< indent
; i
++) {
323 const char *component_type_str(bt_component_class_type type
)
326 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
328 case BT_COMPONENT_CLASS_TYPE_SINK
:
330 case BT_COMPONENT_CLASS_TYPE_FILTER
:
338 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
339 const char *comp_cls_name
, bt_component_class_type type
)
341 GString
*shell_plugin_name
= NULL
;
342 GString
*shell_comp_cls_name
= NULL
;
345 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
346 if (!shell_plugin_name
) {
351 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
352 if (!shell_comp_cls_name
) {
356 fprintf(fh
, "'%s%s%s%s",
357 bt_common_color_bold(),
358 bt_common_color_fg_cyan(),
359 component_type_str(type
),
360 bt_common_color_fg_default());
362 if (shell_plugin_name
) {
363 fprintf(fh
, ".%s%s%s",
364 bt_common_color_fg_blue(),
365 shell_plugin_name
->str
,
366 bt_common_color_fg_default());
369 fprintf(fh
, ".%s%s%s'",
370 bt_common_color_fg_yellow(),
371 shell_comp_cls_name
->str
,
372 bt_common_color_reset());
375 if (shell_plugin_name
) {
376 g_string_free(shell_plugin_name
, TRUE
);
379 if (shell_comp_cls_name
) {
380 g_string_free(shell_comp_cls_name
, TRUE
);
385 void print_value(FILE *, const bt_value
*, size_t);
388 void print_value_rec(FILE *, const bt_value
*, size_t);
390 struct print_map_value_data
{
396 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
399 struct print_map_value_data
*print_map_value_data
= data
;
401 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
402 fprintf(print_map_value_data
->fp
, "%s: ", key
);
405 if (bt_value_is_array(object
) &&
406 bt_value_array_is_empty(object
)) {
407 fprintf(print_map_value_data
->fp
, "[ ]\n");
411 if (bt_value_is_map(object
) &&
412 bt_value_map_is_empty(object
)) {
413 fprintf(print_map_value_data
->fp
, "{ }\n");
417 if (bt_value_is_array(object
) ||
418 bt_value_is_map(object
)) {
419 fprintf(print_map_value_data
->fp
, "\n");
422 print_value_rec(print_map_value_data
->fp
, object
,
423 print_map_value_data
->indent
+ 2);
428 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
442 switch (bt_value_get_type(value
)) {
443 case BT_VALUE_TYPE_NULL
:
444 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
445 bt_common_color_reset());
447 case BT_VALUE_TYPE_BOOL
:
448 bool_val
= bt_value_bool_get(value
);
449 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
450 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
451 bt_common_color_reset());
453 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
454 uint_val
= bt_value_integer_unsigned_get(value
);
455 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
456 bt_common_color_fg_red(), uint_val
,
457 bt_common_color_reset());
459 case BT_VALUE_TYPE_SIGNED_INTEGER
:
460 int_val
= bt_value_integer_signed_get(value
);
461 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
462 bt_common_color_fg_red(), int_val
,
463 bt_common_color_reset());
465 case BT_VALUE_TYPE_REAL
:
466 dbl_val
= bt_value_real_get(value
);
467 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
468 bt_common_color_fg_red(), dbl_val
,
469 bt_common_color_reset());
471 case BT_VALUE_TYPE_STRING
:
472 str_val
= bt_value_string_get(value
);
473 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
474 bt_common_color_fg_green(), str_val
,
475 bt_common_color_reset());
477 case BT_VALUE_TYPE_ARRAY
:
478 size
= bt_value_array_get_size(value
);
484 print_indent(fp
, indent
);
485 fprintf(fp
, "[ ]\n");
489 for (i
= 0; i
< size
; i
++) {
490 const bt_value
*element
=
491 bt_value_array_borrow_element_by_index_const(
497 print_indent(fp
, indent
);
500 if (bt_value_is_array(element
) &&
501 bt_value_array_is_empty(element
)) {
502 fprintf(fp
, "[ ]\n");
506 if (bt_value_is_map(element
) &&
507 bt_value_map_is_empty(element
)) {
508 fprintf(fp
, "{ }\n");
512 if (bt_value_is_array(element
) ||
513 bt_value_is_map(element
)) {
517 print_value_rec(fp
, element
, indent
+ 2);
520 case BT_VALUE_TYPE_MAP
:
522 struct print_map_value_data data
= {
527 if (bt_value_map_is_empty(value
)) {
528 print_indent(fp
, indent
);
529 fprintf(fp
, "{ }\n");
533 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
542 BT_LOGE("Error printing value of type %s.",
543 bt_common_value_type_string(bt_value_get_type(value
)));
547 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
549 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
550 print_indent(fp
, indent
);
553 print_value_rec(fp
, value
, indent
);
557 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
559 fprintf(stderr
, " ");
560 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
561 bt_config_component
->comp_cls_name
->str
,
562 bt_config_component
->type
);
563 fprintf(stderr
, ":\n");
565 if (bt_config_component
->instance_name
->len
> 0) {
566 fprintf(stderr
, " Name: %s\n",
567 bt_config_component
->instance_name
->str
);
570 fprintf(stderr
, " Parameters:\n");
571 print_value(stderr
, bt_config_component
->params
, 8);
575 void print_bt_config_components(GPtrArray
*array
)
579 for (i
= 0; i
< array
->len
; i
++) {
580 struct bt_config_component
*cfg_component
=
581 bt_config_get_component(array
, i
);
582 print_bt_config_component(cfg_component
);
583 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
588 void print_plugin_paths(const bt_value
*plugin_paths
)
590 fprintf(stderr
, " Plugin paths:\n");
591 print_value(stderr
, plugin_paths
, 4);
595 void print_cfg_run(struct bt_config
*cfg
)
599 print_plugin_paths(cfg
->plugin_paths
);
600 fprintf(stderr
, " Source component instances:\n");
601 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
603 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
604 fprintf(stderr
, " Filter component instances:\n");
605 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
608 fprintf(stderr
, " Sink component instances:\n");
609 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
610 fprintf(stderr
, " Connections:\n");
612 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
613 struct bt_config_connection
*cfg_connection
=
614 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
617 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
618 cfg_connection
->upstream_comp_name
->str
,
619 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
620 cfg_connection
->upstream_port_glob
->str
,
621 cfg_connection
->downstream_comp_name
->str
,
622 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
623 cfg_connection
->downstream_port_glob
->str
);
628 void print_cfg_list_plugins(struct bt_config
*cfg
)
630 print_plugin_paths(cfg
->plugin_paths
);
634 void print_cfg_help(struct bt_config
*cfg
)
636 print_plugin_paths(cfg
->plugin_paths
);
640 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
642 print_plugin_paths(cfg
->plugin_paths
);
643 fprintf(stderr
, " Path: %s\n",
644 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
648 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
650 print_plugin_paths(cfg
->plugin_paths
);
651 fprintf(stderr
, " URL: %s\n",
652 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
656 void print_cfg_query(struct bt_config
*cfg
)
658 print_plugin_paths(cfg
->plugin_paths
);
659 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
660 fprintf(stderr
, " Component class:\n");
661 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
665 void print_cfg(struct bt_config
*cfg
)
667 if (!BT_LOG_ON_INFO
) {
671 BT_LOGI_STR("CLI configuration:");
672 BT_LOGI(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
673 BT_LOGI(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
675 switch (cfg
->command
) {
676 case BT_CONFIG_COMMAND_RUN
:
679 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
680 print_cfg_list_plugins(cfg
);
682 case BT_CONFIG_COMMAND_HELP
:
685 case BT_CONFIG_COMMAND_QUERY
:
686 print_cfg_query(cfg
);
688 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
689 print_cfg_print_ctf_metadata(cfg
);
691 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
692 print_cfg_print_lttng_live_sessions(cfg
);
700 void print_plugin_info(const bt_plugin
*plugin
)
702 unsigned int major
, minor
, patch
;
704 bt_property_availability version_avail
;
705 const char *plugin_name
;
709 const char *plugin_description
;
711 plugin_name
= bt_plugin_get_name(plugin
);
712 path
= bt_plugin_get_path(plugin
);
713 author
= bt_plugin_get_author(plugin
);
714 license
= bt_plugin_get_license(plugin
);
715 plugin_description
= bt_plugin_get_description(plugin
);
716 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
718 printf("%s%s%s%s:\n", bt_common_color_bold(),
719 bt_common_color_fg_blue(), plugin_name
,
720 bt_common_color_reset());
722 printf(" %sPath%s: %s\n", bt_common_color_bold(),
723 bt_common_color_reset(), path
);
728 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
729 printf(" %sVersion%s: %u.%u.%u",
730 bt_common_color_bold(), bt_common_color_reset(),
731 major
, minor
, patch
);
740 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
741 bt_common_color_reset(),
742 plugin_description
? plugin_description
: "(None)");
743 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
744 bt_common_color_reset(), author
? author
: "(Unknown)");
745 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
746 bt_common_color_reset(),
747 license
? license
: "(Unknown)");
751 int cmd_query(struct bt_config
*cfg
)
754 const bt_component_class
*comp_cls
= NULL
;
755 const bt_value
*results
= NULL
;
756 const char *fail_reason
= NULL
;
758 comp_cls
= find_component_class(
759 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
760 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
761 cfg
->cmd_data
.query
.cfg_component
->type
);
763 BT_CLI_LOGE_APPEND_CAUSE(
764 "Cannot find component class: plugin-name=\"%s\", "
765 "comp-cls-name=\"%s\", comp-cls-type=%d",
766 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
767 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
768 cfg
->cmd_data
.query
.cfg_component
->type
);
773 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
774 cfg
->cmd_data
.query
.cfg_component
->params
,
775 &results
, &fail_reason
);
780 print_value(stdout
, results
, 0);
784 BT_CLI_LOGE_APPEND_CAUSE(
785 "Failed to query component class: %s: plugin-name=\"%s\", "
786 "comp-cls-name=\"%s\", comp-cls-type=%d "
787 "object=\"%s\"", fail_reason
,
788 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
789 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
790 cfg
->cmd_data
.query
.cfg_component
->type
,
791 cfg
->cmd_data
.query
.object
->str
);
795 bt_component_class_put_ref(comp_cls
);
796 bt_value_put_ref(results
);
801 void print_component_class_help(const char *plugin_name
,
802 const bt_component_class
*comp_cls
)
804 const char *comp_class_name
=
805 bt_component_class_get_name(comp_cls
);
806 const char *comp_class_description
=
807 bt_component_class_get_description(comp_cls
);
808 const char *comp_class_help
=
809 bt_component_class_get_help(comp_cls
);
810 bt_component_class_type type
=
811 bt_component_class_get_type(comp_cls
);
813 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
815 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
816 bt_common_color_reset(),
817 comp_class_description
? comp_class_description
: "(None)");
819 if (comp_class_help
) {
820 printf("\n%s\n", comp_class_help
);
825 int cmd_help(struct bt_config
*cfg
)
828 const bt_plugin
*plugin
= NULL
;
829 const bt_component_class
*needed_comp_cls
= NULL
;
831 plugin
= find_loaded_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
833 BT_CLI_LOGE_APPEND_CAUSE(
834 "Cannot find plugin: plugin-name=\"%s\"",
835 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
840 print_plugin_info(plugin
);
841 printf(" %sSource component classes%s: %d\n",
842 bt_common_color_bold(),
843 bt_common_color_reset(),
844 (int) bt_plugin_get_source_component_class_count(plugin
));
845 printf(" %sFilter component classes%s: %d\n",
846 bt_common_color_bold(),
847 bt_common_color_reset(),
848 (int) bt_plugin_get_filter_component_class_count(plugin
));
849 printf(" %sSink component classes%s: %d\n",
850 bt_common_color_bold(),
851 bt_common_color_reset(),
852 (int) bt_plugin_get_sink_component_class_count(plugin
));
854 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
855 /* Plugin help only */
859 needed_comp_cls
= find_component_class(
860 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
861 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
862 cfg
->cmd_data
.help
.cfg_component
->type
);
863 if (!needed_comp_cls
) {
864 BT_CLI_LOGE_APPEND_CAUSE(
865 "Cannot find component class: plugin-name=\"%s\", "
866 "comp-cls-name=\"%s\", comp-cls-type=%d",
867 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
868 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
869 cfg
->cmd_data
.help
.cfg_component
->type
);
875 print_component_class_help(
876 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
880 bt_component_class_put_ref(needed_comp_cls
);
881 bt_plugin_put_ref(plugin
);
885 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
887 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
890 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
891 const char *cc_type_name
, uint64_t count
,
892 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
893 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
898 printf(" %s%s component classes%s: (none)\n",
899 bt_common_color_bold(),
901 bt_common_color_reset());
904 printf(" %s%s component classes%s:\n",
905 bt_common_color_bold(),
907 bt_common_color_reset());
910 for (i
= 0; i
< count
; i
++) {
911 const bt_component_class
*comp_class
=
912 spec_comp_cls_borrow_comp_cls_func(
913 borrow_comp_cls_by_index_func(plugin
, i
));
914 const char *comp_class_name
=
915 bt_component_class_get_name(comp_class
);
916 const char *comp_class_description
=
917 bt_component_class_get_description(comp_class
);
918 bt_component_class_type type
=
919 bt_component_class_get_type(comp_class
);
922 print_plugin_comp_cls_opt(stdout
,
923 bt_plugin_get_name(plugin
), comp_class_name
,
926 if (comp_class_description
) {
927 printf(": %s", comp_class_description
);
938 int cmd_list_plugins(struct bt_config
*cfg
)
941 int plugins_count
, component_classes_count
= 0, i
;
943 printf("From the following plugin paths:\n\n");
944 print_value(stdout
, cfg
->plugin_paths
, 2);
946 plugins_count
= get_loaded_plugins_count();
947 if (plugins_count
== 0) {
948 printf("No plugins found.\n");
952 for (i
= 0; i
< plugins_count
; i
++) {
953 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
955 component_classes_count
+=
956 bt_plugin_get_source_component_class_count(plugin
) +
957 bt_plugin_get_filter_component_class_count(plugin
) +
958 bt_plugin_get_sink_component_class_count(plugin
);
961 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
962 bt_common_color_bold(),
963 component_classes_count
,
964 bt_common_color_reset(),
965 bt_common_color_bold(),
967 bt_common_color_reset());
969 for (i
= 0; i
< plugins_count
; i
++) {
970 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
973 print_plugin_info(plugin
);
974 cmd_list_plugins_print_component_classes(plugin
, "Source",
975 bt_plugin_get_source_component_class_count(plugin
),
976 (plugin_borrow_comp_cls_by_index_func_t
)
977 bt_plugin_borrow_source_component_class_by_index_const
,
978 (spec_comp_cls_borrow_comp_cls_func_t
)
979 bt_component_class_source_as_component_class
);
980 cmd_list_plugins_print_component_classes(plugin
, "Filter",
981 bt_plugin_get_filter_component_class_count(plugin
),
982 (plugin_borrow_comp_cls_by_index_func_t
)
983 bt_plugin_borrow_filter_component_class_by_index_const
,
984 (spec_comp_cls_borrow_comp_cls_func_t
)
985 bt_component_class_filter_as_component_class
);
986 cmd_list_plugins_print_component_classes(plugin
, "Sink",
987 bt_plugin_get_sink_component_class_count(plugin
),
988 (plugin_borrow_comp_cls_by_index_func_t
)
989 bt_plugin_borrow_sink_component_class_by_index_const
,
990 (spec_comp_cls_borrow_comp_cls_func_t
)
991 bt_component_class_sink_as_component_class
);
999 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1002 const bt_component_class
*comp_cls
= NULL
;
1003 const bt_value
*results
= NULL
;
1004 bt_value
*params
= NULL
;
1005 const bt_value
*map
= NULL
;
1006 const bt_value
*v
= NULL
;
1007 static const char * const plugin_name
= "ctf";
1008 static const char * const comp_cls_name
= "lttng-live";
1009 static const bt_component_class_type comp_cls_type
=
1010 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1011 int64_t array_size
, i
;
1012 const char *fail_reason
= NULL
;
1013 FILE *out_stream
= stdout
;
1015 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1016 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1019 BT_CLI_LOGE_APPEND_CAUSE(
1020 "Cannot find component class: plugin-name=\"%s\", "
1021 "comp-cls-name=\"%s\", comp-cls-type=%d",
1022 plugin_name
, comp_cls_name
,
1023 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1027 params
= bt_value_map_create();
1032 ret
= bt_value_map_insert_string_entry(params
, "url",
1033 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1038 ret
= query(cfg
, comp_cls
, "sessions", params
,
1039 &results
, &fail_reason
);
1046 if (!bt_value_is_array(results
)) {
1047 BT_CLI_LOGE_APPEND_CAUSE(
1048 "Expecting an array for LTTng live `sessions` query.");
1052 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1054 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1058 BT_LOGE_ERRNO("Cannot open file for writing",
1060 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1061 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1063 "Cannot open file for writing: path=\"%s\"",
1064 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1069 array_size
= bt_value_array_get_size(results
);
1070 for (i
= 0; i
< array_size
; i
++) {
1071 const char *url_text
;
1072 int64_t timer_us
, streams
, clients
;
1074 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1076 BT_CLI_LOGE_APPEND_CAUSE("Unexpected empty array entry.");
1079 if (!bt_value_is_map(map
)) {
1080 BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type.");
1084 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1086 BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry.");
1089 url_text
= bt_value_string_get(v
);
1090 fprintf(out_stream
, "%s", url_text
);
1091 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1093 BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry.");
1096 timer_us
= bt_value_integer_signed_get(v
);
1097 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1098 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1100 BT_CLI_LOGE_APPEND_CAUSE(
1101 "Missing `stream-count` entry.");
1104 streams
= bt_value_integer_signed_get(v
);
1105 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1106 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1108 BT_CLI_LOGE_APPEND_CAUSE(
1109 "Missing `client-count` entry.");
1112 clients
= bt_value_integer_signed_get(v
);
1113 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1119 BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s",
1126 bt_value_put_ref(results
);
1127 bt_value_put_ref(params
);
1128 bt_component_class_put_ref(comp_cls
);
1130 if (out_stream
&& out_stream
!= stdout
) {
1131 int fclose_ret
= fclose(out_stream
);
1134 BT_LOGE_ERRNO("Cannot close file stream",
1136 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1144 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1147 const bt_component_class
*comp_cls
= NULL
;
1148 const bt_value
*results
= NULL
;
1149 bt_value
*params
= NULL
;
1150 const bt_value
*metadata_text_value
= NULL
;
1151 const char *metadata_text
= NULL
;
1152 static const char * const plugin_name
= "ctf";
1153 static const char * const comp_cls_name
= "fs";
1154 static const bt_component_class_type comp_cls_type
=
1155 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1156 const char *fail_reason
= NULL
;
1157 FILE *out_stream
= stdout
;
1159 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1160 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1163 BT_CLI_LOGE_APPEND_CAUSE(
1164 "Cannot find component class: plugin-name=\"%s\", "
1165 "comp-cls-name=\"%s\", comp-cls-type=%d",
1166 plugin_name
, comp_cls_name
,
1167 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1172 params
= bt_value_map_create();
1178 ret
= bt_value_map_insert_string_entry(params
, "path",
1179 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1185 ret
= query(cfg
, comp_cls
, "metadata-info",
1186 params
, &results
, &fail_reason
);
1191 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1193 if (!metadata_text_value
) {
1194 BT_CLI_LOGE_APPEND_CAUSE(
1195 "Cannot find `text` string value in the resulting metadata info object.");
1200 metadata_text
= bt_value_string_get(metadata_text_value
);
1202 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1204 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1207 BT_LOGE_ERRNO("Cannot open file for writing",
1209 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1210 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1212 "Cannot open file for writing: path=\"%s\"",
1213 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1219 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1221 BT_CLI_LOGE_APPEND_CAUSE(
1222 "Cannot write whole metadata text to output stream: "
1232 BT_CLI_LOGE_APPEND_CAUSE(
1233 "Failed to query `metadata-info` object: %s", fail_reason
);
1236 bt_value_put_ref(results
);
1237 bt_value_put_ref(params
);
1238 bt_component_class_put_ref(comp_cls
);
1240 if (out_stream
&& out_stream
!= stdout
) {
1241 int fclose_ret
= fclose(out_stream
);
1244 BT_LOGE_ERRNO("Cannot close file stream",
1246 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1254 char *instance_name
;
1258 struct trace_range
{
1259 uint64_t intersection_range_begin_ns
;
1260 uint64_t intersection_range_end_ns
;
1264 guint
port_id_hash(gconstpointer v
)
1266 const struct port_id
*id
= v
;
1268 BT_ASSERT(id
->instance_name
);
1269 BT_ASSERT(id
->port_name
);
1271 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1275 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1277 const struct port_id
*id1
= v1
;
1278 const struct port_id
*id2
= v2
;
1280 return strcmp(id1
->instance_name
, id2
->instance_name
) == 0 &&
1281 strcmp(id1
->port_name
, id2
->port_name
) == 0;
1285 void port_id_destroy(gpointer data
)
1287 struct port_id
*id
= data
;
1289 free(id
->instance_name
);
1290 free(id
->port_name
);
1295 void trace_range_destroy(gpointer data
)
1300 struct cmd_run_ctx
{
1302 GHashTable
*src_components
;
1305 GHashTable
*flt_components
;
1308 GHashTable
*sink_components
;
1314 struct bt_config
*cfg
;
1318 bool stream_intersection_mode
;
1321 * Association of struct port_id -> struct trace_range.
1323 GHashTable
*intersections
;
1326 /* Returns a timestamp of the form "(-)s.ns" */
1328 char *s_from_ns(int64_t ns
)
1333 int64_t ts_sec_abs
, ts_nsec_abs
;
1334 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1335 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1337 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1338 is_negative
= false;
1339 ts_sec_abs
= ts_sec
;
1340 ts_nsec_abs
= ts_nsec
;
1341 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1342 is_negative
= false;
1343 ts_sec_abs
= ts_sec
- 1;
1344 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1345 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1347 ts_sec_abs
= ts_sec
;
1348 ts_nsec_abs
= -ts_nsec
;
1349 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1351 ts_sec_abs
= -(ts_sec
+ 1);
1352 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1353 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1355 ts_sec_abs
= -ts_sec
;
1356 ts_nsec_abs
= ts_nsec
;
1357 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1359 ts_sec_abs
= -ts_sec
;
1360 ts_nsec_abs
= -ts_nsec
;
1363 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1364 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1372 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1373 struct cmd_run_ctx
*ctx
,
1374 const bt_component
*upstream_comp
,
1375 const bt_port_output
*out_upstream_port
,
1376 struct bt_config_connection
*cfg_conn
)
1378 typedef uint64_t (*input_port_count_func_t
)(void *);
1379 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1380 const void *, uint64_t);
1381 const bt_port
*upstream_port
=
1382 bt_port_output_as_port_const(out_upstream_port
);
1385 GQuark downstreamp_comp_name_quark
;
1386 void *downstream_comp
;
1387 uint64_t downstream_port_count
;
1389 input_port_count_func_t port_count_fn
;
1390 borrow_input_port_by_index_func_t port_by_index_fn
;
1391 bt_graph_connect_ports_status connect_ports_status
=
1392 BT_GRAPH_CONNECT_PORTS_STATUS_OK
;
1393 bool insert_trimmer
= false;
1394 bt_value
*trimmer_params
= NULL
;
1395 char *intersection_begin
= NULL
;
1396 char *intersection_end
= NULL
;
1397 const bt_component_filter
*trimmer
= NULL
;
1398 const bt_component_class_filter
*trimmer_class
= NULL
;
1399 const bt_port_input
*trimmer_input
= NULL
;
1400 const bt_port_output
*trimmer_output
= NULL
;
1402 if (ctx
->intersections
&&
1403 bt_component_get_class_type(upstream_comp
) ==
1404 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1405 struct trace_range
*range
;
1406 struct port_id port_id
= {
1407 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1408 .port_name
= (char *) bt_port_get_name(upstream_port
)
1411 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1415 range
= (struct trace_range
*) g_hash_table_lookup(
1416 ctx
->intersections
, &port_id
);
1418 bt_value_map_insert_entry_status insert_status
;
1420 intersection_begin
= s_from_ns(
1421 range
->intersection_range_begin_ns
);
1422 intersection_end
= s_from_ns(
1423 range
->intersection_range_end_ns
);
1424 if (!intersection_begin
|| !intersection_end
) {
1425 BT_CLI_LOGE_APPEND_CAUSE(
1426 "Cannot create trimmer argument timestamp string.");
1430 insert_trimmer
= true;
1431 trimmer_params
= bt_value_map_create();
1432 if (!trimmer_params
) {
1436 insert_status
= bt_value_map_insert_string_entry(
1437 trimmer_params
, "begin", intersection_begin
);
1438 if (insert_status
< 0) {
1441 insert_status
= bt_value_map_insert_string_entry(
1443 "end", intersection_end
);
1444 if (insert_status
< 0) {
1449 trimmer_class
= find_filter_component_class("utils", "trimmer");
1450 if (!trimmer_class
) {
1455 BT_LOGI("Connecting upstream port to the next available downstream port: "
1456 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1457 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1458 upstream_port
, bt_port_get_name(upstream_port
),
1459 cfg_conn
->downstream_comp_name
->str
,
1460 cfg_conn
->arg
->str
);
1461 downstreamp_comp_name_quark
= g_quark_from_string(
1462 cfg_conn
->downstream_comp_name
->str
);
1463 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1464 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1465 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1466 port_count_fn
= (input_port_count_func_t
)
1467 bt_component_filter_get_input_port_count
;
1468 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1469 bt_component_filter_borrow_input_port_by_index_const
;
1471 if (!downstream_comp
) {
1472 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1473 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1474 port_count_fn
= (input_port_count_func_t
)
1475 bt_component_sink_get_input_port_count
;
1476 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1477 bt_component_sink_borrow_input_port_by_index_const
;
1480 if (!downstream_comp
) {
1481 BT_CLI_LOGE_APPEND_CAUSE("Cannot find downstream component: "
1482 "comp-name=\"%s\", conn-arg=\"%s\"",
1483 cfg_conn
->downstream_comp_name
->str
,
1484 cfg_conn
->arg
->str
);
1488 downstream_port_count
= port_count_fn(downstream_comp
);
1490 for (i
= 0; i
< downstream_port_count
; i
++) {
1491 const bt_port_input
*in_downstream_port
=
1492 port_by_index_fn(downstream_comp
, i
);
1493 const bt_port
*downstream_port
=
1494 bt_port_input_as_port_const(in_downstream_port
);
1495 const char *upstream_port_name
;
1496 const char *downstream_port_name
;
1498 BT_ASSERT(downstream_port
);
1500 /* Skip port if it's already connected. */
1501 if (bt_port_is_connected(downstream_port
)) {
1502 BT_LOGI("Skipping downstream port: already connected: "
1503 "port-addr=%p, port-name=\"%s\"",
1505 bt_port_get_name(downstream_port
));
1509 downstream_port_name
= bt_port_get_name(downstream_port
);
1510 BT_ASSERT(downstream_port_name
);
1511 upstream_port_name
= bt_port_get_name(upstream_port
);
1512 BT_ASSERT(upstream_port_name
);
1514 if (!bt_common_star_glob_match(
1515 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1516 downstream_port_name
, SIZE_MAX
)) {
1520 if (insert_trimmer
) {
1522 * In order to insert the trimmer between the
1523 * two components that were being connected, we
1524 * create a connection configuration entry which
1525 * describes a connection from the trimmer's
1526 * output to the original input that was being
1529 * Hence, the creation of the trimmer will cause
1530 * the graph "new port" listener to establish
1531 * all downstream connections as its output port
1532 * is connected. We will then establish the
1533 * connection between the original upstream
1534 * source and the trimmer.
1536 char *trimmer_name
= NULL
;
1537 bt_graph_add_component_status add_comp_status
;
1539 ret
= asprintf(&trimmer_name
,
1540 "stream-intersection-trimmer-%s",
1541 upstream_port_name
);
1547 ctx
->connect_ports
= false;
1548 add_comp_status
= bt_graph_add_filter_component(
1549 ctx
->graph
, trimmer_class
, trimmer_name
,
1550 trimmer_params
, ctx
->cfg
->log_level
,
1553 if (add_comp_status
!=
1554 BT_GRAPH_ADD_COMPONENT_STATUS_OK
) {
1560 bt_component_filter_borrow_input_port_by_index_const(
1562 if (!trimmer_input
) {
1566 bt_component_filter_borrow_output_port_by_index_const(
1568 if (!trimmer_output
) {
1573 * Replace the current downstream port by the trimmer's
1576 in_downstream_port
= trimmer_input
;
1578 bt_port_input_as_port_const(in_downstream_port
);
1579 downstream_port_name
= bt_port_get_name(
1581 BT_ASSERT(downstream_port_name
);
1584 /* We have a winner! */
1585 connect_ports_status
= bt_graph_connect_ports(ctx
->graph
,
1586 out_upstream_port
, in_downstream_port
, NULL
);
1587 downstream_port
= NULL
;
1588 switch (connect_ports_status
) {
1589 case BT_GRAPH_CONNECT_PORTS_STATUS_OK
:
1592 BT_CLI_LOGE_APPEND_CAUSE(
1593 "Cannot create connection: graph refuses to connect ports: "
1594 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1595 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1596 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1597 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1599 upstream_comp
, bt_component_get_name(upstream_comp
),
1600 upstream_port
, bt_port_get_name(upstream_port
),
1601 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1602 downstream_port
, downstream_port_name
,
1603 cfg_conn
->arg
->str
);
1607 BT_LOGI("Connected component ports: "
1608 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1609 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1610 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1611 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1613 upstream_comp
, bt_component_get_name(upstream_comp
),
1614 upstream_port
, bt_port_get_name(upstream_port
),
1615 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1616 downstream_port
, downstream_port_name
,
1617 cfg_conn
->arg
->str
);
1619 if (insert_trimmer
) {
1621 * The first connection, from the source to the trimmer,
1622 * has been done. We now connect the trimmer to the
1623 * original downstream port.
1625 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1627 bt_component_filter_as_component_const(trimmer
),
1628 trimmer_output
, cfg_conn
);
1632 ctx
->connect_ports
= true;
1636 * We found a matching downstream port: the search is
1642 /* No downstream port found */
1643 BT_CLI_LOGE_APPEND_CAUSE(
1644 "Cannot create connection: cannot find a matching downstream port for upstream port: "
1645 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1646 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1647 upstream_port
, bt_port_get_name(upstream_port
),
1648 cfg_conn
->downstream_comp_name
->str
,
1649 cfg_conn
->arg
->str
);
1655 free(intersection_begin
);
1656 free(intersection_end
);
1657 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1658 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1659 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1664 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1665 const bt_port_output
*upstream_port
)
1668 const char *upstream_port_name
;
1669 const char *upstream_comp_name
;
1670 const bt_component
*upstream_comp
= NULL
;
1674 BT_ASSERT(upstream_port
);
1675 upstream_port_name
= bt_port_get_name(
1676 bt_port_output_as_port_const(upstream_port
));
1677 BT_ASSERT(upstream_port_name
);
1678 upstream_comp
= bt_port_borrow_component_const(
1679 bt_port_output_as_port_const(upstream_port
));
1680 BT_ASSERT(upstream_comp
);
1681 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1682 BT_ASSERT(upstream_comp_name
);
1683 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1684 "port-addr=%p, port-name=\"%s\"",
1685 upstream_comp
, upstream_comp_name
,
1686 upstream_port
, upstream_port_name
);
1688 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1689 struct bt_config_connection
*cfg_conn
=
1691 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1693 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1694 upstream_comp_name
)) {
1698 if (!bt_common_star_glob_match(
1699 cfg_conn
->upstream_port_glob
->str
,
1700 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1704 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1705 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1707 BT_CLI_LOGE_APPEND_CAUSE(
1708 "Cannot connect upstream port: "
1709 "port-addr=%p, port-name=\"%s\"",
1711 upstream_port_name
);
1717 BT_CLI_LOGE_APPEND_CAUSE(
1718 "Cannot connect upstream port: port does not match any connection argument: "
1719 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1720 upstream_port_name
);
1730 bt_graph_listener_func_status
1731 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1732 const bt_port_output
*out_port
)
1734 const bt_component
*comp
;
1735 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1736 bt_graph_listener_func_status ret
=
1737 BT_GRAPH_LISTENER_FUNC_STATUS_OK
;
1739 comp
= bt_port_borrow_component_const(port
);
1740 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1741 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1742 comp
, comp
? bt_component_get_name(comp
) : "",
1743 port
, bt_port_get_name(port
));
1745 if (!ctx
->connect_ports
) {
1751 if (bt_port_is_connected(port
)) {
1752 BT_LOGW_STR("Port is already connected.");
1756 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
1757 BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port.");
1758 ret
= BT_GRAPH_LISTENER_FUNC_STATUS_ERROR
;
1767 bt_graph_listener_func_status
graph_source_output_port_added_listener(
1768 const bt_component_source
*component
,
1769 const bt_port_output
*port
, void *data
)
1771 return graph_output_port_added_listener(data
, port
);
1775 bt_graph_listener_func_status
graph_filter_output_port_added_listener(
1776 const bt_component_filter
*component
,
1777 const bt_port_output
*port
, void *data
)
1779 return graph_output_port_added_listener(data
, port
);
1783 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1789 if (ctx
->src_components
) {
1790 g_hash_table_destroy(ctx
->src_components
);
1791 ctx
->src_components
= NULL
;
1794 if (ctx
->flt_components
) {
1795 g_hash_table_destroy(ctx
->flt_components
);
1796 ctx
->flt_components
= NULL
;
1799 if (ctx
->sink_components
) {
1800 g_hash_table_destroy(ctx
->sink_components
);
1801 ctx
->sink_components
= NULL
;
1804 if (ctx
->intersections
) {
1805 g_hash_table_destroy(ctx
->intersections
);
1806 ctx
->intersections
= NULL
;
1809 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
1814 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1817 bt_graph_add_listener_status add_listener_status
;
1820 ctx
->connect_ports
= false;
1821 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
1822 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1823 if (!ctx
->src_components
) {
1827 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
1828 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1829 if (!ctx
->flt_components
) {
1833 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
1834 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1835 if (!ctx
->sink_components
) {
1839 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1840 ctx
->stream_intersection_mode
= true;
1841 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1842 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1843 if (!ctx
->intersections
) {
1848 ctx
->graph
= bt_graph_create();
1853 bt_graph_add_interrupter(ctx
->graph
, the_interrupter
);
1854 add_listener_status
= bt_graph_add_source_component_output_port_added_listener(
1855 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
1857 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1858 BT_CLI_LOGE_APPEND_CAUSE(
1859 "Cannot add \"port added\" listener to graph.");
1863 add_listener_status
= bt_graph_add_filter_component_output_port_added_listener(
1864 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
1866 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1867 BT_CLI_LOGE_APPEND_CAUSE(
1868 "Cannot add \"port added\" listener to graph.");
1875 cmd_run_ctx_destroy(ctx
);
1883 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
1884 struct bt_config_component
*cfg_comp
,
1885 const bt_component_class_source
*src_comp_cls
)
1889 int64_t trace_count
;
1890 const char *path
= NULL
;
1891 const bt_value
*query_result
= NULL
;
1892 const bt_value
*trace_info
= NULL
;
1893 const bt_value
*intersection_range
= NULL
;
1894 const bt_value
*intersection_begin
= NULL
;
1895 const bt_value
*intersection_end
= NULL
;
1896 const bt_value
*stream_infos
= NULL
;
1897 const bt_value
*stream_info
= NULL
;
1898 struct port_id
*port_id
= NULL
;
1899 struct trace_range
*trace_range
= NULL
;
1900 const char *fail_reason
= NULL
;
1901 const bt_component_class
*comp_cls
=
1902 bt_component_class_source_as_component_class_const(src_comp_cls
);
1904 ret
= query(ctx
->cfg
, comp_cls
, "trace-info",
1905 cfg_comp
->params
, &query_result
,
1908 BT_LOGD("Component class does not support the `trace-info` query: %s: "
1909 "comp-class-name=\"%s\"", fail_reason
,
1910 bt_component_class_get_name(comp_cls
));
1915 BT_ASSERT(query_result
);
1917 if (!bt_value_is_array(query_result
)) {
1918 BT_LOGD("Unexpected format of \'trace-info\' query result: "
1919 "component-class-name=%s",
1920 bt_component_class_get_name(comp_cls
));
1925 trace_count
= bt_value_array_get_size(query_result
);
1926 if (trace_count
< 0) {
1931 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
1933 uint64_t stream_idx
;
1934 int64_t stream_count
;
1936 trace_info
= bt_value_array_borrow_element_by_index_const(
1937 query_result
, trace_idx
);
1938 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
1940 BT_LOGD_STR("Cannot retrieve trace from query result.");
1944 intersection_range
= bt_value_map_borrow_entry_value_const(
1945 trace_info
, "intersection-range-ns");
1946 if (!intersection_range
) {
1948 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
1952 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
1954 if (!intersection_begin
) {
1956 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
1960 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
1962 if (!intersection_end
) {
1964 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
1968 begin
= bt_value_integer_signed_get(intersection_begin
);
1969 end
= bt_value_integer_signed_get(intersection_end
);
1971 if (begin
< 0 || end
< 0 || end
< begin
) {
1972 BT_CLI_LOGE_APPEND_CAUSE(
1973 "Invalid trace stream intersection values: "
1974 "intersection-range-ns:begin=%" PRId64
1975 ", intersection-range-ns:end=%" PRId64
,
1981 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
1983 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
1985 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
1989 stream_count
= bt_value_array_get_size(stream_infos
);
1990 if (stream_count
< 0) {
1995 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
1996 const bt_value
*port_name
;
1998 port_id
= g_new0(struct port_id
, 1);
2001 BT_CLI_LOGE_APPEND_CAUSE(
2002 "Cannot allocate memory for port_id structure.");
2005 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2006 if (!port_id
->instance_name
) {
2008 BT_CLI_LOGE_APPEND_CAUSE(
2009 "Cannot allocate memory for port_id component instance name.");
2013 trace_range
= g_new0(struct trace_range
, 1);
2016 BT_CLI_LOGE_APPEND_CAUSE(
2017 "Cannot allocate memory for trace_range structure.");
2020 trace_range
->intersection_range_begin_ns
= begin
;
2021 trace_range
->intersection_range_end_ns
= end
;
2023 stream_info
= bt_value_array_borrow_element_by_index_const(
2024 stream_infos
, stream_idx
);
2025 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2027 BT_CLI_LOGE_APPEND_CAUSE(
2028 "Cannot retrieve stream informations from trace in query result.");
2032 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2033 if (!port_name
|| !bt_value_is_string(port_name
)) {
2035 BT_CLI_LOGE_APPEND_CAUSE(
2036 "Cannot retrieve port name in query result.");
2040 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2041 if (!port_id
->port_name
) {
2043 BT_CLI_LOGE_APPEND_CAUSE(
2044 "Cannot allocate memory for port_id port_name.");
2048 BT_LOGD("Inserting stream intersection ");
2050 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2060 BT_CLI_LOGE_APPEND_CAUSE(
2061 "Cannot determine stream intersection of trace: path=\"%s\"",
2062 path
? path
: "(unknown)");
2065 bt_value_put_ref(query_result
);
2067 g_free(trace_range
);
2072 int cmd_run_ctx_create_components_from_config_components(
2073 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2076 const void *comp_cls
= NULL
;
2077 const void *comp
= NULL
;
2080 for (i
= 0; i
< cfg_components
->len
; i
++) {
2081 struct bt_config_component
*cfg_comp
=
2082 g_ptr_array_index(cfg_components
, i
);
2085 switch (cfg_comp
->type
) {
2086 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2087 comp_cls
= find_source_component_class(
2088 cfg_comp
->plugin_name
->str
,
2089 cfg_comp
->comp_cls_name
->str
);
2091 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2092 comp_cls
= find_filter_component_class(
2093 cfg_comp
->plugin_name
->str
,
2094 cfg_comp
->comp_cls_name
->str
);
2096 case BT_COMPONENT_CLASS_TYPE_SINK
:
2097 comp_cls
= find_sink_component_class(
2098 cfg_comp
->plugin_name
->str
,
2099 cfg_comp
->comp_cls_name
->str
);
2106 BT_CLI_LOGE_APPEND_CAUSE(
2107 "Cannot find component class: plugin-name=\"%s\", "
2108 "comp-cls-name=\"%s\", comp-cls-type=%d",
2109 cfg_comp
->plugin_name
->str
,
2110 cfg_comp
->comp_cls_name
->str
,
2115 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_TRACE
);
2117 switch (cfg_comp
->type
) {
2118 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2119 ret
= bt_graph_add_source_component(ctx
->graph
,
2120 comp_cls
, cfg_comp
->instance_name
->str
,
2121 cfg_comp
->params
, cfg_comp
->log_level
,
2124 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2125 ret
= bt_graph_add_filter_component(ctx
->graph
,
2126 comp_cls
, cfg_comp
->instance_name
->str
,
2127 cfg_comp
->params
, cfg_comp
->log_level
,
2130 case BT_COMPONENT_CLASS_TYPE_SINK
:
2131 ret
= bt_graph_add_sink_component(ctx
->graph
,
2132 comp_cls
, cfg_comp
->instance_name
->str
,
2133 cfg_comp
->params
, cfg_comp
->log_level
,
2141 BT_CLI_LOGE_APPEND_CAUSE(
2142 "Cannot create component: plugin-name=\"%s\", "
2143 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2145 cfg_comp
->plugin_name
->str
,
2146 cfg_comp
->comp_cls_name
->str
,
2147 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2151 if (ctx
->stream_intersection_mode
&&
2152 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2153 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2159 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2160 comp
, cfg_comp
->instance_name
->str
);
2161 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2162 BT_ASSERT(quark
> 0);
2164 switch (cfg_comp
->type
) {
2165 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2166 g_hash_table_insert(ctx
->src_components
,
2167 GUINT_TO_POINTER(quark
), (void *) comp
);
2169 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2170 g_hash_table_insert(ctx
->flt_components
,
2171 GUINT_TO_POINTER(quark
), (void *) comp
);
2173 case BT_COMPONENT_CLASS_TYPE_SINK
:
2174 g_hash_table_insert(ctx
->sink_components
,
2175 GUINT_TO_POINTER(quark
), (void *) comp
);
2182 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2191 bt_object_put_ref(comp
);
2192 bt_object_put_ref(comp_cls
);
2197 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2202 * Make sure that, during this phase, our graph's "port added"
2203 * listener does not connect ports while we are creating the
2204 * components because we have a special, initial phase for
2207 ctx
->connect_ports
= false;
2209 ret
= cmd_run_ctx_create_components_from_config_components(
2210 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2216 ret
= cmd_run_ctx_create_components_from_config_components(
2217 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2223 ret
= cmd_run_ctx_create_components_from_config_components(
2224 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2234 typedef uint64_t (*output_port_count_func_t
)(const void *);
2235 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2236 const void *, uint64_t);
2239 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2240 void *comp
, output_port_count_func_t port_count_fn
,
2241 borrow_output_port_by_index_func_t port_by_index_fn
)
2247 count
= port_count_fn(comp
);
2249 for (i
= 0; i
< count
; i
++) {
2250 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2252 BT_ASSERT(upstream_port
);
2253 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2264 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2267 GHashTableIter iter
;
2268 gpointer g_name_quark
, g_comp
;
2270 ctx
->connect_ports
= true;
2271 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2273 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2274 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2275 (output_port_count_func_t
)
2276 bt_component_source_get_output_port_count
,
2277 (borrow_output_port_by_index_func_t
)
2278 bt_component_source_borrow_output_port_by_index_const
);
2284 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2286 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2287 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2288 (output_port_count_func_t
)
2289 bt_component_filter_get_output_port_count
,
2290 (borrow_output_port_by_index_func_t
)
2291 bt_component_filter_borrow_output_port_by_index_const
);
2302 int cmd_run(struct bt_config
*cfg
)
2305 struct cmd_run_ctx ctx
= { 0 };
2307 /* Initialize the command's context and the graph object */
2308 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2309 BT_CLI_LOGE_APPEND_CAUSE(
2310 "Cannot initialize the command's context.");
2315 BT_CLI_LOGW_APPEND_CAUSE(
2316 "Interrupted by user before creating components.");
2320 BT_LOGI_STR("Creating components.");
2322 /* Create the requested component instances */
2323 if (cmd_run_ctx_create_components(&ctx
)) {
2324 BT_CLI_LOGE_APPEND_CAUSE("Cannot create components.");
2329 BT_CLI_LOGW_APPEND_CAUSE(
2330 "Interrupted by user before connecting components.");
2334 BT_LOGI_STR("Connecting components.");
2336 /* Connect the initially visible component ports */
2337 if (cmd_run_ctx_connect_ports(&ctx
)) {
2338 BT_CLI_LOGE_APPEND_CAUSE(
2339 "Cannot connect initial component ports.");
2344 BT_CLI_LOGW_APPEND_CAUSE(
2345 "Interrupted by user before running the graph.");
2349 BT_LOGI_STR("Running the graph.");
2353 bt_graph_run_status run_status
= bt_graph_run(ctx
.graph
);
2356 * Reset console in case something messed with console
2357 * codes during the graph's execution.
2359 printf("%s", bt_common_color_reset());
2361 fprintf(stderr
, "%s", bt_common_color_reset());
2362 BT_LOGT("bt_graph_run() returned: status=%s",
2363 bt_common_func_status_string(run_status
));
2365 switch (run_status
) {
2366 case BT_GRAPH_RUN_STATUS_OK
:
2368 case BT_GRAPH_RUN_STATUS_AGAIN
:
2369 if (bt_interrupter_is_set(the_interrupter
)) {
2370 BT_CLI_LOGW_APPEND_CAUSE(
2371 "Graph was interrupted by user.");
2375 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2376 BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: "
2378 cfg
->cmd_data
.run
.retry_duration_us
);
2380 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2381 if (bt_interrupter_is_set(the_interrupter
)) {
2382 BT_CLI_LOGW_APPEND_CAUSE(
2383 "Graph was interrupted by user.");
2389 case BT_GRAPH_RUN_STATUS_END
:
2392 if (bt_interrupter_is_set(the_interrupter
)) {
2393 BT_CLI_LOGW_APPEND_CAUSE(
2394 "Graph was interrupted by user and failed: "
2396 bt_common_func_status_string(run_status
));
2400 BT_CLI_LOGE_APPEND_CAUSE(
2401 "Graph failed to complete successfully");
2414 cmd_run_ctx_destroy(&ctx
);
2419 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2421 const char *env_clash
;
2423 if (!cfg
->command_name
) {
2427 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2428 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2432 if (g_file_test(cfg
->command_name
,
2433 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2434 _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION
, __FILE__
, __LINE__
,
2435 BT_LOG_WARNING
, BT_LOG_TAG
,
2436 "The `%s` command was executed. "
2437 "If you meant to convert a trace located in "
2438 "the local `%s` directory, please use:\n\n"
2439 " babeltrace2 convert %s [OPTIONS]",
2440 cfg
->command_name
, cfg
->command_name
,
2446 void init_log_level(void)
2448 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2452 void set_auto_log_levels(struct bt_config
*cfg
)
2454 const char **env_var_name
;
2457 * Override the configuration's default log level if
2458 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2459 * are found for backward compatibility with legacy Babetrace 1.
2461 if (getenv("BABELTRACE_DEBUG") &&
2462 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2463 cfg
->log_level
= BT_LOG_TRACE
;
2464 } else if (getenv("BABELTRACE_VERBOSE") &&
2465 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2466 cfg
->log_level
= BT_LOG_INFO
;
2470 * Set log levels according to --debug or --verbose. For
2471 * backward compatibility, --debug is more verbose than
2474 * --verbose: INFO log level
2475 * --debug: TRACE log level (includes DEBUG, which is
2476 * is less verbose than TRACE in the internal
2477 * logging framework)
2479 if (!getenv("LIBBABELTRACE2_INIT_LOG_LEVEL")) {
2481 bt_logging_set_global_level(BT_LOG_INFO
);
2482 } else if (cfg
->debug
) {
2483 bt_logging_set_global_level(BT_LOG_TRACE
);
2486 * Set library's default log level if not
2487 * explicitly specified.
2489 bt_logging_set_global_level(cfg
->log_level
);
2493 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2495 bt_cli_log_level
= BT_LOG_INFO
;
2496 } else if (cfg
->debug
) {
2497 bt_cli_log_level
= BT_LOG_TRACE
;
2500 * Set CLI's default log level if not explicitly
2503 bt_cli_log_level
= cfg
->log_level
;
2507 env_var_name
= log_level_env_var_names
;
2509 while (*env_var_name
) {
2510 if (!getenv(*env_var_name
)) {
2512 g_setenv(*env_var_name
, "INFO", 1);
2513 } else if (cfg
->debug
) {
2514 g_setenv(*env_var_name
, "TRACE", 1);
2516 char val
[2] = { 0 };
2519 * Set module's default log level if not
2520 * explicitly specified.
2522 val
[0] = bt_log_get_letter_from_level(
2524 g_setenv(*env_var_name
, val
, 1);
2533 void print_error_causes(void)
2535 const bt_error
*error
= bt_current_thread_take_error();
2537 GString
*folded
= NULL
;
2538 unsigned int columns
;
2540 if (!error
|| bt_error_get_cause_count(error
) == 0) {
2541 fprintf(stderr
, "%s%sUnknown command-line error.%s\n",
2542 bt_common_color_bold(), bt_common_color_fg_red(),
2543 bt_common_color_reset());
2547 /* Try to get terminal width to fold the error cause messages */
2548 if (bt_common_get_term_size(&columns
, NULL
) < 0) {
2549 /* Width not found: default to 80 */
2554 * This helps visually separate the error causes from the last
2555 * logging statement.
2557 fprintf(stderr
, "\n");
2559 /* Reverse order: deepest (root) cause printed at the end */
2560 for (i
= bt_error_get_cause_count(error
) - 1; i
>= 0; i
--) {
2561 const bt_error_cause
*cause
=
2562 bt_error_borrow_cause_by_index(error
, (uint64_t) i
);
2563 const char *prefix_fmt
=
2564 i
== bt_error_get_cause_count(error
) - 1 ?
2565 "%s%sERROR%s: " : "%s%sCAUSED BY%s ";
2568 fprintf(stderr
, prefix_fmt
,
2569 bt_common_color_bold(), bt_common_color_fg_red(),
2570 bt_common_color_reset());
2572 /* Print actor name */
2573 fprintf(stderr
, "[");
2574 switch (bt_error_cause_get_actor_type(cause
)) {
2575 case BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN
:
2576 fprintf(stderr
, "%s%s%s",
2577 bt_common_color_bold(),
2578 bt_error_cause_get_module_name(cause
),
2579 bt_common_color_reset());
2581 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
:
2582 fprintf(stderr
, "%s%s%s: ",
2583 bt_common_color_bold(),
2584 bt_error_cause_component_actor_get_component_name(cause
),
2585 bt_common_color_reset());
2586 print_plugin_comp_cls_opt(stderr
,
2587 bt_error_cause_component_actor_get_plugin_name(cause
),
2588 bt_error_cause_component_actor_get_component_class_name(cause
),
2589 bt_error_cause_component_actor_get_component_class_type(cause
));
2591 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
:
2592 print_plugin_comp_cls_opt(stderr
,
2593 bt_error_cause_component_class_actor_get_plugin_name(cause
),
2594 bt_error_cause_component_class_actor_get_component_class_name(cause
),
2595 bt_error_cause_component_class_actor_get_component_class_type(cause
));
2597 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
:
2598 fprintf(stderr
, "%s%s%s (%s%s%s): ",
2599 bt_common_color_bold(),
2600 bt_error_cause_message_iterator_actor_get_component_name(cause
),
2601 bt_common_color_reset(),
2602 bt_common_color_bold(),
2603 bt_error_cause_message_iterator_actor_get_component_output_port_name(cause
),
2604 bt_common_color_reset());
2605 print_plugin_comp_cls_opt(stderr
,
2606 bt_error_cause_message_iterator_actor_get_plugin_name(cause
),
2607 bt_error_cause_message_iterator_actor_get_component_class_name(cause
),
2608 bt_error_cause_message_iterator_actor_get_component_class_type(cause
));
2614 /* Print file name and line number */
2615 fprintf(stderr
, "] (%s%s%s%s:%s%" PRIu64
"%s)\n",
2616 bt_common_color_bold(),
2617 bt_common_color_fg_magenta(),
2618 bt_error_cause_get_file_name(cause
),
2619 bt_common_color_reset(),
2620 bt_common_color_fg_green(),
2621 bt_error_cause_get_line_number(cause
),
2622 bt_common_color_reset());
2625 folded
= bt_common_fold(bt_error_cause_get_message(cause
),
2628 BT_LOGE_STR("Could not fold string.");
2629 fprintf(stderr
, "%s\n",
2630 bt_error_cause_get_message(cause
));
2634 fprintf(stderr
, "%s\n", folded
->str
);
2635 g_string_free(folded
, TRUE
);
2641 g_string_free(folded
, TRUE
);
2645 bt_error_release(error
);
2649 int main(int argc
, const char **argv
)
2653 struct bt_config
*cfg
;
2656 set_signal_handler();
2657 init_loaded_plugins();
2658 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2661 /* Quit without errors; typically usage/version */
2663 BT_LOGI_STR("Quitting without errors.");
2668 BT_CLI_LOGE_APPEND_CAUSE(
2669 "Command-line error: retcode=%d", retcode
);
2674 BT_CLI_LOGE_APPEND_CAUSE(
2675 "Failed to create a valid Babeltrace configuration.");
2680 set_auto_log_levels(cfg
);
2683 if (cfg
->command_needs_plugins
) {
2684 ret
= require_loaded_plugins(cfg
->plugin_paths
);
2686 BT_CLI_LOGE_APPEND_CAUSE(
2687 "Failed to load plugins: ret=%d", ret
);
2693 BT_ASSERT(!the_interrupter
);
2694 the_interrupter
= bt_interrupter_create();
2695 if (!the_interrupter
) {
2696 BT_CLI_LOGE_APPEND_CAUSE("Failed to create an interrupter object.");
2701 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2702 cfg
->command
, cfg
->command_name
);
2704 switch (cfg
->command
) {
2705 case BT_CONFIG_COMMAND_RUN
:
2708 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2709 ret
= cmd_list_plugins(cfg
);
2711 case BT_CONFIG_COMMAND_HELP
:
2712 ret
= cmd_help(cfg
);
2714 case BT_CONFIG_COMMAND_QUERY
:
2715 ret
= cmd_query(cfg
);
2717 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2718 ret
= cmd_print_ctf_metadata(cfg
);
2720 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2721 ret
= cmd_print_lttng_live_sessions(cfg
);
2724 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2728 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2729 cfg
->command
, cfg
->command_name
, ret
);
2730 warn_command_name_and_directory_clash(cfg
);
2731 retcode
= ret
? 1 : 0;
2734 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2735 fini_loaded_plugins();
2736 bt_interrupter_put_ref(the_interrupter
);
2739 print_error_causes();
2743 * Clear current thread's error in case there is one to avoid a
2746 bt_current_thread_clear_error();