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 processing graph (weak) */
59 static bt_graph
*the_graph
;
60 static bt_query_executor
*the_query_executor
;
61 static bool canceled
= false;
68 BOOL WINAPI
signal_handler(DWORD signal
) {
70 bt_graph_cancel(the_graph
);
79 void set_signal_handler(void)
81 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
82 BT_LOGE("Failed to set the Ctrl+C handler.");
86 #else /* __MINGW32__ */
89 void signal_handler(int signum
)
91 if (signum
!= SIGINT
) {
96 bt_graph_cancel(the_graph
);
99 if (the_query_executor
) {
100 bt_query_executor_cancel(the_query_executor
);
107 void set_signal_handler(void)
109 struct sigaction new_action
, old_action
;
111 new_action
.sa_handler
= signal_handler
;
112 sigemptyset(&new_action
.sa_mask
);
113 new_action
.sa_flags
= 0;
114 sigaction(SIGINT
, NULL
, &old_action
);
116 if (old_action
.sa_handler
!= SIG_IGN
) {
117 sigaction(SIGINT
, &new_action
, NULL
);
121 #endif /* __MINGW32__ */
124 int create_the_query_executor(void)
128 the_query_executor
= bt_query_executor_create();
129 if (!the_query_executor
) {
130 BT_CLI_LOGE_APPEND_CAUSE("Cannot create a query executor.");
138 void destroy_the_query_executor(void)
140 BT_QUERY_EXECUTOR_PUT_REF_AND_RESET(the_query_executor
);
144 int query(struct bt_config
*cfg
, const bt_component_class
*comp_cls
,
145 const char *obj
, const bt_value
*params
,
146 const bt_value
**user_result
, const char **fail_reason
)
148 const bt_value
*result
= NULL
;
149 bt_query_executor_query_status query_status
;
150 *fail_reason
= "unknown error";
153 BT_ASSERT(fail_reason
);
154 BT_ASSERT(user_result
);
155 ret
= create_the_query_executor();
157 /* create_the_query_executor() logs errors */
162 BT_CLI_LOGW_APPEND_CAUSE(
163 "Canceled by user before executing the query: "
164 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
165 "query-obj=\"%s\"", comp_cls
,
166 bt_component_class_get_name(comp_cls
), obj
);
167 *fail_reason
= "canceled by user";
172 query_status
= bt_query_executor_query(
173 the_query_executor
, comp_cls
, obj
, params
,
174 cfg
->log_level
, &result
);
175 switch (query_status
) {
176 case BT_QUERY_EXECUTOR_QUERY_STATUS_OK
:
178 case BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN
:
180 const uint64_t sleep_time_us
= 100000;
182 /* Wait 100 ms and retry */
183 BT_LOGD("Got BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN: sleeping: "
184 "time-us=%" PRIu64
, sleep_time_us
);
186 if (usleep(sleep_time_us
)) {
187 if (bt_query_executor_is_canceled(the_query_executor
)) {
188 BT_CLI_LOGW_APPEND_CAUSE(
189 "Query was canceled by user: "
190 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
191 "query-obj=\"%s\"", comp_cls
,
192 bt_component_class_get_name(comp_cls
),
194 *fail_reason
= "canceled by user";
201 case BT_QUERY_EXECUTOR_QUERY_STATUS_CANCELED
:
202 *fail_reason
= "canceled by user";
204 case BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR
:
206 case BT_QUERY_EXECUTOR_QUERY_STATUS_INVALID_OBJECT
:
207 *fail_reason
= "invalid or unknown query object";
209 case BT_QUERY_EXECUTOR_QUERY_STATUS_INVALID_PARAMS
:
210 *fail_reason
= "invalid query parameters";
212 case BT_QUERY_EXECUTOR_QUERY_STATUS_UNSUPPORTED
:
213 *fail_reason
= "unsupported action";
215 case BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR
:
216 *fail_reason
= "not enough memory";
219 BT_LOGF("Unknown query status: status=%s",
220 bt_common_func_status_string(query_status
));
226 *user_result
= result
;
234 destroy_the_query_executor();
235 bt_value_put_ref(result
);
241 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
242 const bt_plugin
*, const char *);
245 const void *find_component_class_from_plugin(const char *plugin_name
,
246 const char *comp_class_name
,
247 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
249 const void *comp_class
= NULL
;
250 const bt_plugin
*plugin
;
252 BT_LOGI("Finding component class: plugin-name=\"%s\", "
253 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
255 plugin
= find_loaded_plugin(plugin_name
);
260 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
261 bt_object_get_ref(comp_class
);
262 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
266 BT_LOGI("Found component class: plugin-name=\"%s\", "
267 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
269 BT_LOGI("Cannot find source component class: "
270 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
271 plugin_name
, comp_class_name
);
278 const bt_component_class_source
*find_source_component_class(
279 const char *plugin_name
, const char *comp_class_name
)
281 return (const void *) find_component_class_from_plugin(
282 plugin_name
, comp_class_name
,
283 (plugin_borrow_comp_cls_func_t
)
284 bt_plugin_borrow_source_component_class_by_name_const
);
288 const bt_component_class_filter
*find_filter_component_class(
289 const char *plugin_name
, const char *comp_class_name
)
291 return (const void *) find_component_class_from_plugin(
292 plugin_name
, comp_class_name
,
293 (plugin_borrow_comp_cls_func_t
)
294 bt_plugin_borrow_filter_component_class_by_name_const
);
298 const bt_component_class_sink
*find_sink_component_class(
299 const char *plugin_name
, const char *comp_class_name
)
301 return (const void *) find_component_class_from_plugin(plugin_name
,
303 (plugin_borrow_comp_cls_func_t
)
304 bt_plugin_borrow_sink_component_class_by_name_const
);
308 const bt_component_class
*find_component_class(const char *plugin_name
,
309 const char *comp_class_name
,
310 bt_component_class_type comp_class_type
)
312 const bt_component_class
*comp_cls
= NULL
;
314 switch (comp_class_type
) {
315 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
316 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
318 case BT_COMPONENT_CLASS_TYPE_FILTER
:
319 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
321 case BT_COMPONENT_CLASS_TYPE_SINK
:
322 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
332 void print_indent(FILE *fp
, size_t indent
)
336 for (i
= 0; i
< indent
; i
++) {
342 const char *component_type_str(bt_component_class_type type
)
345 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
347 case BT_COMPONENT_CLASS_TYPE_SINK
:
349 case BT_COMPONENT_CLASS_TYPE_FILTER
:
357 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
358 const char *comp_cls_name
, bt_component_class_type type
)
360 GString
*shell_plugin_name
= NULL
;
361 GString
*shell_comp_cls_name
= NULL
;
364 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
365 if (!shell_plugin_name
) {
370 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
371 if (!shell_comp_cls_name
) {
375 fprintf(fh
, "'%s%s%s%s",
376 bt_common_color_bold(),
377 bt_common_color_fg_cyan(),
378 component_type_str(type
),
379 bt_common_color_fg_default());
381 if (shell_plugin_name
) {
382 fprintf(fh
, ".%s%s%s",
383 bt_common_color_fg_blue(),
384 shell_plugin_name
->str
,
385 bt_common_color_fg_default());
388 fprintf(fh
, ".%s%s%s'",
389 bt_common_color_fg_yellow(),
390 shell_comp_cls_name
->str
,
391 bt_common_color_reset());
394 if (shell_plugin_name
) {
395 g_string_free(shell_plugin_name
, TRUE
);
398 if (shell_comp_cls_name
) {
399 g_string_free(shell_comp_cls_name
, TRUE
);
404 void print_value(FILE *, const bt_value
*, size_t);
407 void print_value_rec(FILE *, const bt_value
*, size_t);
409 struct print_map_value_data
{
415 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
418 struct print_map_value_data
*print_map_value_data
= data
;
420 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
421 fprintf(print_map_value_data
->fp
, "%s: ", key
);
424 if (bt_value_is_array(object
) &&
425 bt_value_array_is_empty(object
)) {
426 fprintf(print_map_value_data
->fp
, "[ ]\n");
430 if (bt_value_is_map(object
) &&
431 bt_value_map_is_empty(object
)) {
432 fprintf(print_map_value_data
->fp
, "{ }\n");
436 if (bt_value_is_array(object
) ||
437 bt_value_is_map(object
)) {
438 fprintf(print_map_value_data
->fp
, "\n");
441 print_value_rec(print_map_value_data
->fp
, object
,
442 print_map_value_data
->indent
+ 2);
447 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
461 switch (bt_value_get_type(value
)) {
462 case BT_VALUE_TYPE_NULL
:
463 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
464 bt_common_color_reset());
466 case BT_VALUE_TYPE_BOOL
:
467 bool_val
= bt_value_bool_get(value
);
468 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
469 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
470 bt_common_color_reset());
472 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
473 uint_val
= bt_value_integer_unsigned_get(value
);
474 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
475 bt_common_color_fg_red(), uint_val
,
476 bt_common_color_reset());
478 case BT_VALUE_TYPE_SIGNED_INTEGER
:
479 int_val
= bt_value_integer_signed_get(value
);
480 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
481 bt_common_color_fg_red(), int_val
,
482 bt_common_color_reset());
484 case BT_VALUE_TYPE_REAL
:
485 dbl_val
= bt_value_real_get(value
);
486 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
487 bt_common_color_fg_red(), dbl_val
,
488 bt_common_color_reset());
490 case BT_VALUE_TYPE_STRING
:
491 str_val
= bt_value_string_get(value
);
492 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
493 bt_common_color_fg_green(), str_val
,
494 bt_common_color_reset());
496 case BT_VALUE_TYPE_ARRAY
:
497 size
= bt_value_array_get_size(value
);
503 print_indent(fp
, indent
);
504 fprintf(fp
, "[ ]\n");
508 for (i
= 0; i
< size
; i
++) {
509 const bt_value
*element
=
510 bt_value_array_borrow_element_by_index_const(
516 print_indent(fp
, indent
);
519 if (bt_value_is_array(element
) &&
520 bt_value_array_is_empty(element
)) {
521 fprintf(fp
, "[ ]\n");
525 if (bt_value_is_map(element
) &&
526 bt_value_map_is_empty(element
)) {
527 fprintf(fp
, "{ }\n");
531 if (bt_value_is_array(element
) ||
532 bt_value_is_map(element
)) {
536 print_value_rec(fp
, element
, indent
+ 2);
539 case BT_VALUE_TYPE_MAP
:
541 struct print_map_value_data data
= {
546 if (bt_value_map_is_empty(value
)) {
547 print_indent(fp
, indent
);
548 fprintf(fp
, "{ }\n");
552 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
561 BT_LOGE("Error printing value of type %s.",
562 bt_common_value_type_string(bt_value_get_type(value
)));
566 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
568 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
569 print_indent(fp
, indent
);
572 print_value_rec(fp
, value
, indent
);
576 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
578 fprintf(stderr
, " ");
579 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
580 bt_config_component
->comp_cls_name
->str
,
581 bt_config_component
->type
);
582 fprintf(stderr
, ":\n");
584 if (bt_config_component
->instance_name
->len
> 0) {
585 fprintf(stderr
, " Name: %s\n",
586 bt_config_component
->instance_name
->str
);
589 fprintf(stderr
, " Parameters:\n");
590 print_value(stderr
, bt_config_component
->params
, 8);
594 void print_bt_config_components(GPtrArray
*array
)
598 for (i
= 0; i
< array
->len
; i
++) {
599 struct bt_config_component
*cfg_component
=
600 bt_config_get_component(array
, i
);
601 print_bt_config_component(cfg_component
);
602 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
607 void print_plugin_paths(const bt_value
*plugin_paths
)
609 fprintf(stderr
, " Plugin paths:\n");
610 print_value(stderr
, plugin_paths
, 4);
614 void print_cfg_run(struct bt_config
*cfg
)
618 print_plugin_paths(cfg
->plugin_paths
);
619 fprintf(stderr
, " Source component instances:\n");
620 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
622 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
623 fprintf(stderr
, " Filter component instances:\n");
624 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
627 fprintf(stderr
, " Sink component instances:\n");
628 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
629 fprintf(stderr
, " Connections:\n");
631 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
632 struct bt_config_connection
*cfg_connection
=
633 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
636 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
637 cfg_connection
->upstream_comp_name
->str
,
638 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
639 cfg_connection
->upstream_port_glob
->str
,
640 cfg_connection
->downstream_comp_name
->str
,
641 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
642 cfg_connection
->downstream_port_glob
->str
);
647 void print_cfg_list_plugins(struct bt_config
*cfg
)
649 print_plugin_paths(cfg
->plugin_paths
);
653 void print_cfg_help(struct bt_config
*cfg
)
655 print_plugin_paths(cfg
->plugin_paths
);
659 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
661 print_plugin_paths(cfg
->plugin_paths
);
662 fprintf(stderr
, " Path: %s\n",
663 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
667 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
669 print_plugin_paths(cfg
->plugin_paths
);
670 fprintf(stderr
, " URL: %s\n",
671 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
675 void print_cfg_query(struct bt_config
*cfg
)
677 print_plugin_paths(cfg
->plugin_paths
);
678 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
679 fprintf(stderr
, " Component class:\n");
680 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
684 void print_cfg(struct bt_config
*cfg
)
686 if (!BT_LOG_ON_INFO
) {
690 BT_LOGI_STR("CLI configuration:");
691 BT_LOGI(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
692 BT_LOGI(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
694 switch (cfg
->command
) {
695 case BT_CONFIG_COMMAND_RUN
:
698 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
699 print_cfg_list_plugins(cfg
);
701 case BT_CONFIG_COMMAND_HELP
:
704 case BT_CONFIG_COMMAND_QUERY
:
705 print_cfg_query(cfg
);
707 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
708 print_cfg_print_ctf_metadata(cfg
);
710 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
711 print_cfg_print_lttng_live_sessions(cfg
);
719 void print_plugin_info(const bt_plugin
*plugin
)
721 unsigned int major
, minor
, patch
;
723 bt_property_availability version_avail
;
724 const char *plugin_name
;
728 const char *plugin_description
;
730 plugin_name
= bt_plugin_get_name(plugin
);
731 path
= bt_plugin_get_path(plugin
);
732 author
= bt_plugin_get_author(plugin
);
733 license
= bt_plugin_get_license(plugin
);
734 plugin_description
= bt_plugin_get_description(plugin
);
735 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
737 printf("%s%s%s%s:\n", bt_common_color_bold(),
738 bt_common_color_fg_blue(), plugin_name
,
739 bt_common_color_reset());
741 printf(" %sPath%s: %s\n", bt_common_color_bold(),
742 bt_common_color_reset(), path
);
747 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
748 printf(" %sVersion%s: %u.%u.%u",
749 bt_common_color_bold(), bt_common_color_reset(),
750 major
, minor
, patch
);
759 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
760 bt_common_color_reset(),
761 plugin_description
? plugin_description
: "(None)");
762 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
763 bt_common_color_reset(), author
? author
: "(Unknown)");
764 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
765 bt_common_color_reset(),
766 license
? license
: "(Unknown)");
770 int cmd_query(struct bt_config
*cfg
)
773 const bt_component_class
*comp_cls
= NULL
;
774 const bt_value
*results
= NULL
;
775 const char *fail_reason
= NULL
;
777 comp_cls
= find_component_class(
778 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
779 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
780 cfg
->cmd_data
.query
.cfg_component
->type
);
782 BT_CLI_LOGE_APPEND_CAUSE(
783 "Cannot find component class: plugin-name=\"%s\", "
784 "comp-cls-name=\"%s\", comp-cls-type=%d",
785 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
786 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
787 cfg
->cmd_data
.query
.cfg_component
->type
);
792 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
793 cfg
->cmd_data
.query
.cfg_component
->params
,
794 &results
, &fail_reason
);
799 print_value(stdout
, results
, 0);
803 BT_CLI_LOGE_APPEND_CAUSE(
804 "Failed to query component class: %s: plugin-name=\"%s\", "
805 "comp-cls-name=\"%s\", comp-cls-type=%d "
806 "object=\"%s\"", fail_reason
,
807 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
808 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
809 cfg
->cmd_data
.query
.cfg_component
->type
,
810 cfg
->cmd_data
.query
.object
->str
);
814 bt_component_class_put_ref(comp_cls
);
815 bt_value_put_ref(results
);
820 void print_component_class_help(const char *plugin_name
,
821 const bt_component_class
*comp_cls
)
823 const char *comp_class_name
=
824 bt_component_class_get_name(comp_cls
);
825 const char *comp_class_description
=
826 bt_component_class_get_description(comp_cls
);
827 const char *comp_class_help
=
828 bt_component_class_get_help(comp_cls
);
829 bt_component_class_type type
=
830 bt_component_class_get_type(comp_cls
);
832 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
834 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
835 bt_common_color_reset(),
836 comp_class_description
? comp_class_description
: "(None)");
838 if (comp_class_help
) {
839 printf("\n%s\n", comp_class_help
);
844 int cmd_help(struct bt_config
*cfg
)
847 const bt_plugin
*plugin
= NULL
;
848 const bt_component_class
*needed_comp_cls
= NULL
;
850 plugin
= find_loaded_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
852 BT_CLI_LOGE_APPEND_CAUSE(
853 "Cannot find plugin: plugin-name=\"%s\"",
854 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
859 print_plugin_info(plugin
);
860 printf(" %sSource component classes%s: %d\n",
861 bt_common_color_bold(),
862 bt_common_color_reset(),
863 (int) bt_plugin_get_source_component_class_count(plugin
));
864 printf(" %sFilter component classes%s: %d\n",
865 bt_common_color_bold(),
866 bt_common_color_reset(),
867 (int) bt_plugin_get_filter_component_class_count(plugin
));
868 printf(" %sSink component classes%s: %d\n",
869 bt_common_color_bold(),
870 bt_common_color_reset(),
871 (int) bt_plugin_get_sink_component_class_count(plugin
));
873 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
874 /* Plugin help only */
878 needed_comp_cls
= find_component_class(
879 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
880 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
881 cfg
->cmd_data
.help
.cfg_component
->type
);
882 if (!needed_comp_cls
) {
883 BT_CLI_LOGE_APPEND_CAUSE(
884 "Cannot find component class: plugin-name=\"%s\", "
885 "comp-cls-name=\"%s\", comp-cls-type=%d",
886 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
887 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
888 cfg
->cmd_data
.help
.cfg_component
->type
);
894 print_component_class_help(
895 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
899 bt_component_class_put_ref(needed_comp_cls
);
900 bt_plugin_put_ref(plugin
);
904 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
906 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
909 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
910 const char *cc_type_name
, uint64_t count
,
911 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
912 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
917 printf(" %s%s component classes%s: (none)\n",
918 bt_common_color_bold(),
920 bt_common_color_reset());
923 printf(" %s%s component classes%s:\n",
924 bt_common_color_bold(),
926 bt_common_color_reset());
929 for (i
= 0; i
< count
; i
++) {
930 const bt_component_class
*comp_class
=
931 spec_comp_cls_borrow_comp_cls_func(
932 borrow_comp_cls_by_index_func(plugin
, i
));
933 const char *comp_class_name
=
934 bt_component_class_get_name(comp_class
);
935 const char *comp_class_description
=
936 bt_component_class_get_description(comp_class
);
937 bt_component_class_type type
=
938 bt_component_class_get_type(comp_class
);
941 print_plugin_comp_cls_opt(stdout
,
942 bt_plugin_get_name(plugin
), comp_class_name
,
945 if (comp_class_description
) {
946 printf(": %s", comp_class_description
);
957 int cmd_list_plugins(struct bt_config
*cfg
)
960 int plugins_count
, component_classes_count
= 0, i
;
962 printf("From the following plugin paths:\n\n");
963 print_value(stdout
, cfg
->plugin_paths
, 2);
965 plugins_count
= get_loaded_plugins_count();
966 if (plugins_count
== 0) {
967 printf("No plugins found.\n");
971 for (i
= 0; i
< plugins_count
; i
++) {
972 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
974 component_classes_count
+=
975 bt_plugin_get_source_component_class_count(plugin
) +
976 bt_plugin_get_filter_component_class_count(plugin
) +
977 bt_plugin_get_sink_component_class_count(plugin
);
980 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
981 bt_common_color_bold(),
982 component_classes_count
,
983 bt_common_color_reset(),
984 bt_common_color_bold(),
986 bt_common_color_reset());
988 for (i
= 0; i
< plugins_count
; i
++) {
989 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
992 print_plugin_info(plugin
);
993 cmd_list_plugins_print_component_classes(plugin
, "Source",
994 bt_plugin_get_source_component_class_count(plugin
),
995 (plugin_borrow_comp_cls_by_index_func_t
)
996 bt_plugin_borrow_source_component_class_by_index_const
,
997 (spec_comp_cls_borrow_comp_cls_func_t
)
998 bt_component_class_source_as_component_class
);
999 cmd_list_plugins_print_component_classes(plugin
, "Filter",
1000 bt_plugin_get_filter_component_class_count(plugin
),
1001 (plugin_borrow_comp_cls_by_index_func_t
)
1002 bt_plugin_borrow_filter_component_class_by_index_const
,
1003 (spec_comp_cls_borrow_comp_cls_func_t
)
1004 bt_component_class_filter_as_component_class
);
1005 cmd_list_plugins_print_component_classes(plugin
, "Sink",
1006 bt_plugin_get_sink_component_class_count(plugin
),
1007 (plugin_borrow_comp_cls_by_index_func_t
)
1008 bt_plugin_borrow_sink_component_class_by_index_const
,
1009 (spec_comp_cls_borrow_comp_cls_func_t
)
1010 bt_component_class_sink_as_component_class
);
1018 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1021 const bt_component_class
*comp_cls
= NULL
;
1022 const bt_value
*results
= NULL
;
1023 bt_value
*params
= NULL
;
1024 const bt_value
*map
= NULL
;
1025 const bt_value
*v
= NULL
;
1026 static const char * const plugin_name
= "ctf";
1027 static const char * const comp_cls_name
= "lttng-live";
1028 static const bt_component_class_type comp_cls_type
=
1029 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1030 int64_t array_size
, i
;
1031 const char *fail_reason
= NULL
;
1032 FILE *out_stream
= stdout
;
1034 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1035 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1038 BT_CLI_LOGE_APPEND_CAUSE(
1039 "Cannot find component class: plugin-name=\"%s\", "
1040 "comp-cls-name=\"%s\", comp-cls-type=%d",
1041 plugin_name
, comp_cls_name
,
1042 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1046 params
= bt_value_map_create();
1051 ret
= bt_value_map_insert_string_entry(params
, "url",
1052 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1057 ret
= query(cfg
, comp_cls
, "sessions", params
,
1058 &results
, &fail_reason
);
1065 if (!bt_value_is_array(results
)) {
1066 BT_CLI_LOGE_APPEND_CAUSE(
1067 "Expecting an array for LTTng live `sessions` query.");
1071 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1073 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1077 BT_LOGE_ERRNO("Cannot open file for writing",
1079 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1080 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1082 "Cannot open file for writing: path=\"%s\"",
1083 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1088 array_size
= bt_value_array_get_size(results
);
1089 for (i
= 0; i
< array_size
; i
++) {
1090 const char *url_text
;
1091 int64_t timer_us
, streams
, clients
;
1093 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1095 BT_CLI_LOGE_APPEND_CAUSE("Unexpected empty array entry.");
1098 if (!bt_value_is_map(map
)) {
1099 BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type.");
1103 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1105 BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry.");
1108 url_text
= bt_value_string_get(v
);
1109 fprintf(out_stream
, "%s", url_text
);
1110 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1112 BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry.");
1115 timer_us
= bt_value_integer_signed_get(v
);
1116 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1117 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1119 BT_CLI_LOGE_APPEND_CAUSE(
1120 "Missing `stream-count` entry.");
1123 streams
= bt_value_integer_signed_get(v
);
1124 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1125 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1127 BT_CLI_LOGE_APPEND_CAUSE(
1128 "Missing `client-count` entry.");
1131 clients
= bt_value_integer_signed_get(v
);
1132 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1138 BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s",
1145 bt_value_put_ref(results
);
1146 bt_value_put_ref(params
);
1147 bt_component_class_put_ref(comp_cls
);
1149 if (out_stream
&& out_stream
!= stdout
) {
1150 int fclose_ret
= fclose(out_stream
);
1153 BT_LOGE_ERRNO("Cannot close file stream",
1155 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1163 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1166 const bt_component_class
*comp_cls
= NULL
;
1167 const bt_value
*results
= NULL
;
1168 bt_value
*params
= NULL
;
1169 const bt_value
*metadata_text_value
= NULL
;
1170 const char *metadata_text
= NULL
;
1171 static const char * const plugin_name
= "ctf";
1172 static const char * const comp_cls_name
= "fs";
1173 static const bt_component_class_type comp_cls_type
=
1174 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1175 const char *fail_reason
= NULL
;
1176 FILE *out_stream
= stdout
;
1178 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1179 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1182 BT_CLI_LOGE_APPEND_CAUSE(
1183 "Cannot find component class: plugin-name=\"%s\", "
1184 "comp-cls-name=\"%s\", comp-cls-type=%d",
1185 plugin_name
, comp_cls_name
,
1186 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1191 params
= bt_value_map_create();
1197 ret
= bt_value_map_insert_string_entry(params
, "path",
1198 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1204 ret
= query(cfg
, comp_cls
, "metadata-info",
1205 params
, &results
, &fail_reason
);
1210 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1212 if (!metadata_text_value
) {
1213 BT_CLI_LOGE_APPEND_CAUSE(
1214 "Cannot find `text` string value in the resulting metadata info object.");
1219 metadata_text
= bt_value_string_get(metadata_text_value
);
1221 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1223 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1226 BT_LOGE_ERRNO("Cannot open file for writing",
1228 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1229 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1231 "Cannot open file for writing: path=\"%s\"",
1232 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1238 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1240 BT_CLI_LOGE_APPEND_CAUSE(
1241 "Cannot write whole metadata text to output stream: "
1251 BT_CLI_LOGE_APPEND_CAUSE(
1252 "Failed to query `metadata-info` object: %s", fail_reason
);
1255 bt_value_put_ref(results
);
1256 bt_value_put_ref(params
);
1257 bt_component_class_put_ref(comp_cls
);
1259 if (out_stream
&& out_stream
!= stdout
) {
1260 int fclose_ret
= fclose(out_stream
);
1263 BT_LOGE_ERRNO("Cannot close file stream",
1265 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1273 char *instance_name
;
1277 struct trace_range
{
1278 uint64_t intersection_range_begin_ns
;
1279 uint64_t intersection_range_end_ns
;
1283 guint
port_id_hash(gconstpointer v
)
1285 const struct port_id
*id
= v
;
1287 BT_ASSERT(id
->instance_name
);
1288 BT_ASSERT(id
->port_name
);
1290 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1294 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1296 const struct port_id
*id1
= v1
;
1297 const struct port_id
*id2
= v2
;
1299 return strcmp(id1
->instance_name
, id2
->instance_name
) == 0 &&
1300 strcmp(id1
->port_name
, id2
->port_name
) == 0;
1304 void port_id_destroy(gpointer data
)
1306 struct port_id
*id
= data
;
1308 free(id
->instance_name
);
1309 free(id
->port_name
);
1314 void trace_range_destroy(gpointer data
)
1319 struct cmd_run_ctx
{
1321 GHashTable
*src_components
;
1324 GHashTable
*flt_components
;
1327 GHashTable
*sink_components
;
1333 struct bt_config
*cfg
;
1337 bool stream_intersection_mode
;
1340 * Association of struct port_id -> struct trace_range.
1342 GHashTable
*intersections
;
1345 /* Returns a timestamp of the form "(-)s.ns" */
1347 char *s_from_ns(int64_t ns
)
1352 int64_t ts_sec_abs
, ts_nsec_abs
;
1353 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1354 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1356 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1357 is_negative
= false;
1358 ts_sec_abs
= ts_sec
;
1359 ts_nsec_abs
= ts_nsec
;
1360 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1361 is_negative
= false;
1362 ts_sec_abs
= ts_sec
- 1;
1363 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1364 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1366 ts_sec_abs
= ts_sec
;
1367 ts_nsec_abs
= -ts_nsec
;
1368 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1370 ts_sec_abs
= -(ts_sec
+ 1);
1371 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1372 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1374 ts_sec_abs
= -ts_sec
;
1375 ts_nsec_abs
= ts_nsec
;
1376 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1378 ts_sec_abs
= -ts_sec
;
1379 ts_nsec_abs
= -ts_nsec
;
1382 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1383 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1391 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1392 struct cmd_run_ctx
*ctx
,
1393 const bt_component
*upstream_comp
,
1394 const bt_port_output
*out_upstream_port
,
1395 struct bt_config_connection
*cfg_conn
)
1397 typedef uint64_t (*input_port_count_func_t
)(void *);
1398 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1399 const void *, uint64_t);
1400 const bt_port
*upstream_port
=
1401 bt_port_output_as_port_const(out_upstream_port
);
1404 GQuark downstreamp_comp_name_quark
;
1405 void *downstream_comp
;
1406 uint64_t downstream_port_count
;
1408 input_port_count_func_t port_count_fn
;
1409 borrow_input_port_by_index_func_t port_by_index_fn
;
1410 bt_graph_connect_ports_status connect_ports_status
=
1411 BT_GRAPH_CONNECT_PORTS_STATUS_OK
;
1412 bool insert_trimmer
= false;
1413 bt_value
*trimmer_params
= NULL
;
1414 char *intersection_begin
= NULL
;
1415 char *intersection_end
= NULL
;
1416 const bt_component_filter
*trimmer
= NULL
;
1417 const bt_component_class_filter
*trimmer_class
= NULL
;
1418 const bt_port_input
*trimmer_input
= NULL
;
1419 const bt_port_output
*trimmer_output
= NULL
;
1421 if (ctx
->intersections
&&
1422 bt_component_get_class_type(upstream_comp
) ==
1423 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1424 struct trace_range
*range
;
1425 struct port_id port_id
= {
1426 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1427 .port_name
= (char *) bt_port_get_name(upstream_port
)
1430 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1434 range
= (struct trace_range
*) g_hash_table_lookup(
1435 ctx
->intersections
, &port_id
);
1437 bt_value_map_insert_entry_status insert_status
;
1439 intersection_begin
= s_from_ns(
1440 range
->intersection_range_begin_ns
);
1441 intersection_end
= s_from_ns(
1442 range
->intersection_range_end_ns
);
1443 if (!intersection_begin
|| !intersection_end
) {
1444 BT_CLI_LOGE_APPEND_CAUSE(
1445 "Cannot create trimmer argument timestamp string.");
1449 insert_trimmer
= true;
1450 trimmer_params
= bt_value_map_create();
1451 if (!trimmer_params
) {
1455 insert_status
= bt_value_map_insert_string_entry(
1456 trimmer_params
, "begin", intersection_begin
);
1457 if (insert_status
< 0) {
1460 insert_status
= bt_value_map_insert_string_entry(
1462 "end", intersection_end
);
1463 if (insert_status
< 0) {
1468 trimmer_class
= find_filter_component_class("utils", "trimmer");
1469 if (!trimmer_class
) {
1474 BT_LOGI("Connecting upstream port to the next available downstream port: "
1475 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1476 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1477 upstream_port
, bt_port_get_name(upstream_port
),
1478 cfg_conn
->downstream_comp_name
->str
,
1479 cfg_conn
->arg
->str
);
1480 downstreamp_comp_name_quark
= g_quark_from_string(
1481 cfg_conn
->downstream_comp_name
->str
);
1482 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1483 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1484 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1485 port_count_fn
= (input_port_count_func_t
)
1486 bt_component_filter_get_input_port_count
;
1487 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1488 bt_component_filter_borrow_input_port_by_index_const
;
1490 if (!downstream_comp
) {
1491 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1492 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1493 port_count_fn
= (input_port_count_func_t
)
1494 bt_component_sink_get_input_port_count
;
1495 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1496 bt_component_sink_borrow_input_port_by_index_const
;
1499 if (!downstream_comp
) {
1500 BT_CLI_LOGE_APPEND_CAUSE("Cannot find downstream component: "
1501 "comp-name=\"%s\", conn-arg=\"%s\"",
1502 cfg_conn
->downstream_comp_name
->str
,
1503 cfg_conn
->arg
->str
);
1507 downstream_port_count
= port_count_fn(downstream_comp
);
1509 for (i
= 0; i
< downstream_port_count
; i
++) {
1510 const bt_port_input
*in_downstream_port
=
1511 port_by_index_fn(downstream_comp
, i
);
1512 const bt_port
*downstream_port
=
1513 bt_port_input_as_port_const(in_downstream_port
);
1514 const char *upstream_port_name
;
1515 const char *downstream_port_name
;
1517 BT_ASSERT(downstream_port
);
1519 /* Skip port if it's already connected. */
1520 if (bt_port_is_connected(downstream_port
)) {
1521 BT_LOGI("Skipping downstream port: already connected: "
1522 "port-addr=%p, port-name=\"%s\"",
1524 bt_port_get_name(downstream_port
));
1528 downstream_port_name
= bt_port_get_name(downstream_port
);
1529 BT_ASSERT(downstream_port_name
);
1530 upstream_port_name
= bt_port_get_name(upstream_port
);
1531 BT_ASSERT(upstream_port_name
);
1533 if (!bt_common_star_glob_match(
1534 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1535 downstream_port_name
, SIZE_MAX
)) {
1539 if (insert_trimmer
) {
1541 * In order to insert the trimmer between the
1542 * two components that were being connected, we
1543 * create a connection configuration entry which
1544 * describes a connection from the trimmer's
1545 * output to the original input that was being
1548 * Hence, the creation of the trimmer will cause
1549 * the graph "new port" listener to establish
1550 * all downstream connections as its output port
1551 * is connected. We will then establish the
1552 * connection between the original upstream
1553 * source and the trimmer.
1555 char *trimmer_name
= NULL
;
1556 bt_graph_add_component_status add_comp_status
;
1558 ret
= asprintf(&trimmer_name
,
1559 "stream-intersection-trimmer-%s",
1560 upstream_port_name
);
1566 ctx
->connect_ports
= false;
1567 add_comp_status
= bt_graph_add_filter_component(
1568 ctx
->graph
, trimmer_class
, trimmer_name
,
1569 trimmer_params
, ctx
->cfg
->log_level
,
1572 if (add_comp_status
!=
1573 BT_GRAPH_ADD_COMPONENT_STATUS_OK
) {
1579 bt_component_filter_borrow_input_port_by_index_const(
1581 if (!trimmer_input
) {
1585 bt_component_filter_borrow_output_port_by_index_const(
1587 if (!trimmer_output
) {
1592 * Replace the current downstream port by the trimmer's
1595 in_downstream_port
= trimmer_input
;
1597 bt_port_input_as_port_const(in_downstream_port
);
1598 downstream_port_name
= bt_port_get_name(
1600 BT_ASSERT(downstream_port_name
);
1603 /* We have a winner! */
1604 connect_ports_status
= bt_graph_connect_ports(ctx
->graph
,
1605 out_upstream_port
, in_downstream_port
, NULL
);
1606 downstream_port
= NULL
;
1607 switch (connect_ports_status
) {
1608 case BT_GRAPH_CONNECT_PORTS_STATUS_OK
:
1610 case BT_GRAPH_CONNECT_PORTS_STATUS_CANCELED
:
1611 BT_CLI_LOGW_APPEND_CAUSE("Graph was canceled by user.");
1614 BT_CLI_LOGE_APPEND_CAUSE(
1615 "Cannot create connection: graph refuses to connect ports: "
1616 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1617 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1618 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1619 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1621 upstream_comp
, bt_component_get_name(upstream_comp
),
1622 upstream_port
, bt_port_get_name(upstream_port
),
1623 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1624 downstream_port
, downstream_port_name
,
1625 cfg_conn
->arg
->str
);
1629 BT_LOGI("Connected component ports: "
1630 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1631 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1632 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1633 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1635 upstream_comp
, bt_component_get_name(upstream_comp
),
1636 upstream_port
, bt_port_get_name(upstream_port
),
1637 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1638 downstream_port
, downstream_port_name
,
1639 cfg_conn
->arg
->str
);
1641 if (insert_trimmer
) {
1643 * The first connection, from the source to the trimmer,
1644 * has been done. We now connect the trimmer to the
1645 * original downstream port.
1647 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1649 bt_component_filter_as_component_const(trimmer
),
1650 trimmer_output
, cfg_conn
);
1654 ctx
->connect_ports
= true;
1658 * We found a matching downstream port: the search is
1664 /* No downstream port found */
1665 BT_CLI_LOGE_APPEND_CAUSE(
1666 "Cannot create connection: cannot find a matching downstream port for upstream port: "
1667 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1668 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1669 upstream_port
, bt_port_get_name(upstream_port
),
1670 cfg_conn
->downstream_comp_name
->str
,
1671 cfg_conn
->arg
->str
);
1677 free(intersection_begin
);
1678 free(intersection_end
);
1679 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1680 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1681 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1686 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1687 const bt_port_output
*upstream_port
)
1690 const char *upstream_port_name
;
1691 const char *upstream_comp_name
;
1692 const bt_component
*upstream_comp
= NULL
;
1696 BT_ASSERT(upstream_port
);
1697 upstream_port_name
= bt_port_get_name(
1698 bt_port_output_as_port_const(upstream_port
));
1699 BT_ASSERT(upstream_port_name
);
1700 upstream_comp
= bt_port_borrow_component_const(
1701 bt_port_output_as_port_const(upstream_port
));
1702 BT_ASSERT(upstream_comp
);
1703 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1704 BT_ASSERT(upstream_comp_name
);
1705 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1706 "port-addr=%p, port-name=\"%s\"",
1707 upstream_comp
, upstream_comp_name
,
1708 upstream_port
, upstream_port_name
);
1710 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1711 struct bt_config_connection
*cfg_conn
=
1713 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1715 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1716 upstream_comp_name
)) {
1720 if (!bt_common_star_glob_match(
1721 cfg_conn
->upstream_port_glob
->str
,
1722 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1726 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1727 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1729 BT_CLI_LOGE_APPEND_CAUSE(
1730 "Cannot connect upstream port: "
1731 "port-addr=%p, port-name=\"%s\"",
1733 upstream_port_name
);
1739 BT_CLI_LOGE_APPEND_CAUSE(
1740 "Cannot connect upstream port: port does not match any connection argument: "
1741 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1742 upstream_port_name
);
1752 bt_graph_listener_func_status
1753 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1754 const bt_port_output
*out_port
)
1756 const bt_component
*comp
;
1757 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1758 bt_graph_listener_func_status ret
=
1759 BT_GRAPH_LISTENER_FUNC_STATUS_OK
;
1761 comp
= bt_port_borrow_component_const(port
);
1762 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1763 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1764 comp
, comp
? bt_component_get_name(comp
) : "",
1765 port
, bt_port_get_name(port
));
1767 if (!ctx
->connect_ports
) {
1773 if (bt_port_is_connected(port
)) {
1774 BT_LOGW_STR("Port is already connected.");
1778 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
1779 BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port.");
1780 ret
= BT_GRAPH_LISTENER_FUNC_STATUS_ERROR
;
1789 bt_graph_listener_func_status
graph_source_output_port_added_listener(
1790 const bt_component_source
*component
,
1791 const bt_port_output
*port
, void *data
)
1793 return graph_output_port_added_listener(data
, port
);
1797 bt_graph_listener_func_status
graph_filter_output_port_added_listener(
1798 const bt_component_filter
*component
,
1799 const bt_port_output
*port
, void *data
)
1801 return graph_output_port_added_listener(data
, port
);
1805 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1811 if (ctx
->src_components
) {
1812 g_hash_table_destroy(ctx
->src_components
);
1813 ctx
->src_components
= NULL
;
1816 if (ctx
->flt_components
) {
1817 g_hash_table_destroy(ctx
->flt_components
);
1818 ctx
->flt_components
= NULL
;
1821 if (ctx
->sink_components
) {
1822 g_hash_table_destroy(ctx
->sink_components
);
1823 ctx
->sink_components
= NULL
;
1826 if (ctx
->intersections
) {
1827 g_hash_table_destroy(ctx
->intersections
);
1828 ctx
->intersections
= NULL
;
1831 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
1837 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1840 bt_graph_add_listener_status add_listener_status
;
1843 ctx
->connect_ports
= false;
1844 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
1845 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1846 if (!ctx
->src_components
) {
1850 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
1851 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1852 if (!ctx
->flt_components
) {
1856 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
1857 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1858 if (!ctx
->sink_components
) {
1862 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1863 ctx
->stream_intersection_mode
= true;
1864 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1865 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1866 if (!ctx
->intersections
) {
1871 ctx
->graph
= bt_graph_create();
1876 the_graph
= ctx
->graph
;
1877 add_listener_status
= bt_graph_add_source_component_output_port_added_listener(
1878 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
1880 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1881 BT_CLI_LOGE_APPEND_CAUSE(
1882 "Cannot add \"port added\" listener to graph.");
1886 add_listener_status
= bt_graph_add_filter_component_output_port_added_listener(
1887 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
1889 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1890 BT_CLI_LOGE_APPEND_CAUSE(
1891 "Cannot add \"port added\" listener to graph.");
1898 cmd_run_ctx_destroy(ctx
);
1906 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
1907 struct bt_config_component
*cfg_comp
,
1908 const bt_component_class_source
*src_comp_cls
)
1912 int64_t trace_count
;
1913 const char *path
= NULL
;
1914 const bt_value
*query_result
= NULL
;
1915 const bt_value
*trace_info
= NULL
;
1916 const bt_value
*intersection_range
= NULL
;
1917 const bt_value
*intersection_begin
= NULL
;
1918 const bt_value
*intersection_end
= NULL
;
1919 const bt_value
*stream_infos
= NULL
;
1920 const bt_value
*stream_info
= NULL
;
1921 struct port_id
*port_id
= NULL
;
1922 struct trace_range
*trace_range
= NULL
;
1923 const char *fail_reason
= NULL
;
1924 const bt_component_class
*comp_cls
=
1925 bt_component_class_source_as_component_class_const(src_comp_cls
);
1927 ret
= query(ctx
->cfg
, comp_cls
, "trace-info",
1928 cfg_comp
->params
, &query_result
,
1931 BT_LOGD("Component class does not support the `trace-info` query: %s: "
1932 "comp-class-name=\"%s\"", fail_reason
,
1933 bt_component_class_get_name(comp_cls
));
1938 BT_ASSERT(query_result
);
1940 if (!bt_value_is_array(query_result
)) {
1941 BT_LOGD("Unexpected format of \'trace-info\' query result: "
1942 "component-class-name=%s",
1943 bt_component_class_get_name(comp_cls
));
1948 trace_count
= bt_value_array_get_size(query_result
);
1949 if (trace_count
< 0) {
1954 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
1956 uint64_t stream_idx
;
1957 int64_t stream_count
;
1959 trace_info
= bt_value_array_borrow_element_by_index_const(
1960 query_result
, trace_idx
);
1961 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
1963 BT_LOGD_STR("Cannot retrieve trace from query result.");
1967 intersection_range
= bt_value_map_borrow_entry_value_const(
1968 trace_info
, "intersection-range-ns");
1969 if (!intersection_range
) {
1971 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
1975 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
1977 if (!intersection_begin
) {
1979 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
1983 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
1985 if (!intersection_end
) {
1987 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
1991 begin
= bt_value_integer_signed_get(intersection_begin
);
1992 end
= bt_value_integer_signed_get(intersection_end
);
1994 if (begin
< 0 || end
< 0 || end
< begin
) {
1995 BT_CLI_LOGE_APPEND_CAUSE(
1996 "Invalid trace stream intersection values: "
1997 "intersection-range-ns:begin=%" PRId64
1998 ", intersection-range-ns:end=%" PRId64
,
2004 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
2006 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2008 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2012 stream_count
= bt_value_array_get_size(stream_infos
);
2013 if (stream_count
< 0) {
2018 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2019 const bt_value
*port_name
;
2021 port_id
= g_new0(struct port_id
, 1);
2024 BT_CLI_LOGE_APPEND_CAUSE(
2025 "Cannot allocate memory for port_id structure.");
2028 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2029 if (!port_id
->instance_name
) {
2031 BT_CLI_LOGE_APPEND_CAUSE(
2032 "Cannot allocate memory for port_id component instance name.");
2036 trace_range
= g_new0(struct trace_range
, 1);
2039 BT_CLI_LOGE_APPEND_CAUSE(
2040 "Cannot allocate memory for trace_range structure.");
2043 trace_range
->intersection_range_begin_ns
= begin
;
2044 trace_range
->intersection_range_end_ns
= end
;
2046 stream_info
= bt_value_array_borrow_element_by_index_const(
2047 stream_infos
, stream_idx
);
2048 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2050 BT_CLI_LOGE_APPEND_CAUSE(
2051 "Cannot retrieve stream informations from trace in query result.");
2055 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2056 if (!port_name
|| !bt_value_is_string(port_name
)) {
2058 BT_CLI_LOGE_APPEND_CAUSE(
2059 "Cannot retrieve port name in query result.");
2063 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2064 if (!port_id
->port_name
) {
2066 BT_CLI_LOGE_APPEND_CAUSE(
2067 "Cannot allocate memory for port_id port_name.");
2071 BT_LOGD("Inserting stream intersection ");
2073 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2083 BT_CLI_LOGE_APPEND_CAUSE(
2084 "Cannot determine stream intersection of trace: path=\"%s\"",
2085 path
? path
: "(unknown)");
2088 bt_value_put_ref(query_result
);
2090 g_free(trace_range
);
2095 int cmd_run_ctx_create_components_from_config_components(
2096 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2099 const void *comp_cls
= NULL
;
2100 const void *comp
= NULL
;
2103 for (i
= 0; i
< cfg_components
->len
; i
++) {
2104 struct bt_config_component
*cfg_comp
=
2105 g_ptr_array_index(cfg_components
, i
);
2108 switch (cfg_comp
->type
) {
2109 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2110 comp_cls
= find_source_component_class(
2111 cfg_comp
->plugin_name
->str
,
2112 cfg_comp
->comp_cls_name
->str
);
2114 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2115 comp_cls
= find_filter_component_class(
2116 cfg_comp
->plugin_name
->str
,
2117 cfg_comp
->comp_cls_name
->str
);
2119 case BT_COMPONENT_CLASS_TYPE_SINK
:
2120 comp_cls
= find_sink_component_class(
2121 cfg_comp
->plugin_name
->str
,
2122 cfg_comp
->comp_cls_name
->str
);
2129 BT_CLI_LOGE_APPEND_CAUSE(
2130 "Cannot find component class: plugin-name=\"%s\", "
2131 "comp-cls-name=\"%s\", comp-cls-type=%d",
2132 cfg_comp
->plugin_name
->str
,
2133 cfg_comp
->comp_cls_name
->str
,
2138 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_TRACE
);
2140 switch (cfg_comp
->type
) {
2141 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2142 ret
= bt_graph_add_source_component(ctx
->graph
,
2143 comp_cls
, cfg_comp
->instance_name
->str
,
2144 cfg_comp
->params
, cfg_comp
->log_level
,
2147 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2148 ret
= bt_graph_add_filter_component(ctx
->graph
,
2149 comp_cls
, cfg_comp
->instance_name
->str
,
2150 cfg_comp
->params
, cfg_comp
->log_level
,
2153 case BT_COMPONENT_CLASS_TYPE_SINK
:
2154 ret
= bt_graph_add_sink_component(ctx
->graph
,
2155 comp_cls
, cfg_comp
->instance_name
->str
,
2156 cfg_comp
->params
, cfg_comp
->log_level
,
2164 BT_CLI_LOGE_APPEND_CAUSE(
2165 "Cannot create component: plugin-name=\"%s\", "
2166 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2168 cfg_comp
->plugin_name
->str
,
2169 cfg_comp
->comp_cls_name
->str
,
2170 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2174 if (ctx
->stream_intersection_mode
&&
2175 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2176 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2182 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2183 comp
, cfg_comp
->instance_name
->str
);
2184 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2185 BT_ASSERT(quark
> 0);
2187 switch (cfg_comp
->type
) {
2188 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2189 g_hash_table_insert(ctx
->src_components
,
2190 GUINT_TO_POINTER(quark
), (void *) comp
);
2192 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2193 g_hash_table_insert(ctx
->flt_components
,
2194 GUINT_TO_POINTER(quark
), (void *) comp
);
2196 case BT_COMPONENT_CLASS_TYPE_SINK
:
2197 g_hash_table_insert(ctx
->sink_components
,
2198 GUINT_TO_POINTER(quark
), (void *) comp
);
2205 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2214 bt_object_put_ref(comp
);
2215 bt_object_put_ref(comp_cls
);
2220 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2225 * Make sure that, during this phase, our graph's "port added"
2226 * listener does not connect ports while we are creating the
2227 * components because we have a special, initial phase for
2230 ctx
->connect_ports
= false;
2232 ret
= cmd_run_ctx_create_components_from_config_components(
2233 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2239 ret
= cmd_run_ctx_create_components_from_config_components(
2240 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2246 ret
= cmd_run_ctx_create_components_from_config_components(
2247 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2257 typedef uint64_t (*output_port_count_func_t
)(const void *);
2258 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2259 const void *, uint64_t);
2262 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2263 void *comp
, output_port_count_func_t port_count_fn
,
2264 borrow_output_port_by_index_func_t port_by_index_fn
)
2270 count
= port_count_fn(comp
);
2272 for (i
= 0; i
< count
; i
++) {
2273 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2275 BT_ASSERT(upstream_port
);
2276 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2287 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2290 GHashTableIter iter
;
2291 gpointer g_name_quark
, g_comp
;
2293 ctx
->connect_ports
= true;
2294 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2296 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2297 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2298 (output_port_count_func_t
)
2299 bt_component_source_get_output_port_count
,
2300 (borrow_output_port_by_index_func_t
)
2301 bt_component_source_borrow_output_port_by_index_const
);
2307 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2309 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2310 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2311 (output_port_count_func_t
)
2312 bt_component_filter_get_output_port_count
,
2313 (borrow_output_port_by_index_func_t
)
2314 bt_component_filter_borrow_output_port_by_index_const
);
2325 int cmd_run(struct bt_config
*cfg
)
2328 struct cmd_run_ctx ctx
= { 0 };
2330 /* Initialize the command's context and the graph object */
2331 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2332 BT_CLI_LOGE_APPEND_CAUSE(
2333 "Cannot initialize the command's context.");
2338 BT_CLI_LOGW_APPEND_CAUSE(
2339 "Canceled by user before creating components.");
2343 BT_LOGI_STR("Creating components.");
2345 /* Create the requested component instances */
2346 if (cmd_run_ctx_create_components(&ctx
)) {
2347 BT_CLI_LOGE_APPEND_CAUSE("Cannot create components.");
2352 BT_CLI_LOGW_APPEND_CAUSE(
2353 "Canceled by user before connecting components.");
2357 BT_LOGI_STR("Connecting components.");
2359 /* Connect the initially visible component ports */
2360 if (cmd_run_ctx_connect_ports(&ctx
)) {
2361 BT_CLI_LOGE_APPEND_CAUSE(
2362 "Cannot connect initial component ports.");
2367 BT_CLI_LOGW_APPEND_CAUSE(
2368 "Canceled by user before running the graph.");
2372 BT_LOGI_STR("Running the graph.");
2376 bt_graph_run_status run_status
= bt_graph_run(ctx
.graph
);
2379 * Reset console in case something messed with console
2380 * codes during the graph's execution.
2382 printf("%s", bt_common_color_reset());
2384 fprintf(stderr
, "%s", bt_common_color_reset());
2385 BT_LOGT("bt_graph_run() returned: status=%s",
2386 bt_common_func_status_string(run_status
));
2388 switch (run_status
) {
2389 case BT_GRAPH_RUN_STATUS_OK
:
2391 case BT_GRAPH_RUN_STATUS_CANCELED
:
2392 BT_CLI_LOGW_APPEND_CAUSE("Graph was canceled by user.");
2394 case BT_GRAPH_RUN_STATUS_AGAIN
:
2395 if (bt_graph_is_canceled(ctx
.graph
)) {
2396 BT_CLI_LOGW_APPEND_CAUSE(
2397 "Graph was canceled by user.");
2401 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2402 BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: "
2404 cfg
->cmd_data
.run
.retry_duration_us
);
2406 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2407 if (bt_graph_is_canceled(ctx
.graph
)) {
2408 BT_CLI_LOGW_APPEND_CAUSE(
2409 "Graph was canceled by user.");
2415 case BT_GRAPH_RUN_STATUS_END
:
2418 BT_CLI_LOGE_APPEND_CAUSE(
2419 "Graph failed to complete successfully");
2432 cmd_run_ctx_destroy(&ctx
);
2437 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2439 const char *env_clash
;
2441 if (!cfg
->command_name
) {
2445 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2446 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2450 if (g_file_test(cfg
->command_name
,
2451 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2452 _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION
, __FILE__
, __LINE__
,
2453 BT_LOG_WARNING
, BT_LOG_TAG
,
2454 "The `%s` command was executed. "
2455 "If you meant to convert a trace located in "
2456 "the local `%s` directory, please use:\n\n"
2457 " babeltrace2 convert %s [OPTIONS]",
2458 cfg
->command_name
, cfg
->command_name
,
2464 void init_log_level(void)
2466 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2470 void set_auto_log_levels(struct bt_config
*cfg
)
2472 const char **env_var_name
;
2475 * Override the configuration's default log level if
2476 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2477 * are found for backward compatibility with legacy Babetrace 1.
2479 if (getenv("BABELTRACE_DEBUG") &&
2480 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2481 cfg
->log_level
= BT_LOG_TRACE
;
2482 } else if (getenv("BABELTRACE_VERBOSE") &&
2483 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2484 cfg
->log_level
= BT_LOG_INFO
;
2488 * Set log levels according to --debug or --verbose. For
2489 * backward compatibility, --debug is more verbose than
2492 * --verbose: INFO log level
2493 * --debug: TRACE log level (includes DEBUG, which is
2494 * is less verbose than TRACE in the internal
2495 * logging framework)
2497 if (!getenv("LIBBABELTRACE2_INIT_LOG_LEVEL")) {
2499 bt_logging_set_global_level(BT_LOG_INFO
);
2500 } else if (cfg
->debug
) {
2501 bt_logging_set_global_level(BT_LOG_TRACE
);
2504 * Set library's default log level if not
2505 * explicitly specified.
2507 bt_logging_set_global_level(cfg
->log_level
);
2511 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2513 bt_cli_log_level
= BT_LOG_INFO
;
2514 } else if (cfg
->debug
) {
2515 bt_cli_log_level
= BT_LOG_TRACE
;
2518 * Set CLI's default log level if not explicitly
2521 bt_cli_log_level
= cfg
->log_level
;
2525 env_var_name
= log_level_env_var_names
;
2527 while (*env_var_name
) {
2528 if (!getenv(*env_var_name
)) {
2530 g_setenv(*env_var_name
, "INFO", 1);
2531 } else if (cfg
->debug
) {
2532 g_setenv(*env_var_name
, "TRACE", 1);
2534 char val
[2] = { 0 };
2537 * Set module's default log level if not
2538 * explicitly specified.
2540 val
[0] = bt_log_get_letter_from_level(
2542 g_setenv(*env_var_name
, val
, 1);
2551 void print_error_causes(void)
2553 const bt_error
*error
= bt_current_thread_take_error();
2555 GString
*folded
= NULL
;
2556 unsigned int columns
;
2558 if (!error
|| bt_error_get_cause_count(error
) == 0) {
2559 fprintf(stderr
, "%s%sUnknown command-line error.%s\n",
2560 bt_common_color_bold(), bt_common_color_fg_red(),
2561 bt_common_color_reset());
2565 /* Try to get terminal width to fold the error cause messages */
2566 if (bt_common_get_term_size(&columns
, NULL
) < 0) {
2567 /* Width not found: default to 80 */
2572 * This helps visually separate the error causes from the last
2573 * logging statement.
2575 fprintf(stderr
, "\n");
2577 /* Reverse order: deepest (root) cause printed at the end */
2578 for (i
= bt_error_get_cause_count(error
) - 1; i
>= 0; i
--) {
2579 const bt_error_cause
*cause
=
2580 bt_error_borrow_cause_by_index(error
, (uint64_t) i
);
2581 const char *prefix_fmt
=
2582 i
== bt_error_get_cause_count(error
) - 1 ?
2583 "%s%sERROR%s: " : "%s%sCAUSED BY%s ";
2586 fprintf(stderr
, prefix_fmt
,
2587 bt_common_color_bold(), bt_common_color_fg_red(),
2588 bt_common_color_reset());
2590 /* Print actor name */
2591 fprintf(stderr
, "[");
2592 switch (bt_error_cause_get_actor_type(cause
)) {
2593 case BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN
:
2594 fprintf(stderr
, "%s%s%s",
2595 bt_common_color_bold(),
2596 bt_error_cause_get_module_name(cause
),
2597 bt_common_color_reset());
2599 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
:
2600 fprintf(stderr
, "%s%s%s: ",
2601 bt_common_color_bold(),
2602 bt_error_cause_component_actor_get_component_name(cause
),
2603 bt_common_color_reset());
2604 print_plugin_comp_cls_opt(stderr
,
2605 bt_error_cause_component_actor_get_plugin_name(cause
),
2606 bt_error_cause_component_actor_get_component_class_name(cause
),
2607 bt_error_cause_component_actor_get_component_class_type(cause
));
2609 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
:
2610 print_plugin_comp_cls_opt(stderr
,
2611 bt_error_cause_component_class_actor_get_plugin_name(cause
),
2612 bt_error_cause_component_class_actor_get_component_class_name(cause
),
2613 bt_error_cause_component_class_actor_get_component_class_type(cause
));
2615 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
:
2616 fprintf(stderr
, "%s%s%s (%s%s%s): ",
2617 bt_common_color_bold(),
2618 bt_error_cause_message_iterator_actor_get_component_name(cause
),
2619 bt_common_color_reset(),
2620 bt_common_color_bold(),
2621 bt_error_cause_message_iterator_actor_get_component_output_port_name(cause
),
2622 bt_common_color_reset());
2623 print_plugin_comp_cls_opt(stderr
,
2624 bt_error_cause_message_iterator_actor_get_plugin_name(cause
),
2625 bt_error_cause_message_iterator_actor_get_component_class_name(cause
),
2626 bt_error_cause_message_iterator_actor_get_component_class_type(cause
));
2632 /* Print file name and line number */
2633 fprintf(stderr
, "] (%s%s%s%s:%s%" PRIu64
"%s)\n",
2634 bt_common_color_bold(),
2635 bt_common_color_fg_magenta(),
2636 bt_error_cause_get_file_name(cause
),
2637 bt_common_color_reset(),
2638 bt_common_color_fg_green(),
2639 bt_error_cause_get_line_number(cause
),
2640 bt_common_color_reset());
2643 folded
= bt_common_fold(bt_error_cause_get_message(cause
),
2646 BT_LOGE_STR("Could not fold string.");
2647 fprintf(stderr
, "%s\n",
2648 bt_error_cause_get_message(cause
));
2652 fprintf(stderr
, "%s\n", folded
->str
);
2653 g_string_free(folded
, TRUE
);
2659 g_string_free(folded
, TRUE
);
2663 bt_error_release(error
);
2667 int main(int argc
, const char **argv
)
2671 struct bt_config
*cfg
;
2674 set_signal_handler();
2675 init_loaded_plugins();
2676 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2679 /* Quit without errors; typically usage/version */
2681 BT_LOGI_STR("Quitting without errors.");
2686 BT_CLI_LOGE_APPEND_CAUSE(
2687 "Command-line error: retcode=%d", retcode
);
2692 BT_CLI_LOGE_APPEND_CAUSE(
2693 "Failed to create a valid Babeltrace configuration.");
2698 set_auto_log_levels(cfg
);
2701 if (cfg
->command_needs_plugins
) {
2702 ret
= load_all_plugins(cfg
->plugin_paths
);
2704 BT_CLI_LOGE_APPEND_CAUSE(
2705 "Failed to load plugins: ret=%d", ret
);
2711 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2712 cfg
->command
, cfg
->command_name
);
2714 switch (cfg
->command
) {
2715 case BT_CONFIG_COMMAND_RUN
:
2718 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2719 ret
= cmd_list_plugins(cfg
);
2721 case BT_CONFIG_COMMAND_HELP
:
2722 ret
= cmd_help(cfg
);
2724 case BT_CONFIG_COMMAND_QUERY
:
2725 ret
= cmd_query(cfg
);
2727 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2728 ret
= cmd_print_ctf_metadata(cfg
);
2730 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2731 ret
= cmd_print_lttng_live_sessions(cfg
);
2734 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2738 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2739 cfg
->command
, cfg
->command_name
, ret
);
2740 warn_command_name_and_directory_clash(cfg
);
2741 retcode
= ret
? 1 : 0;
2744 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2745 fini_loaded_plugins();
2748 print_error_causes();
2752 * Clear current thread's error in case there is one to avoid a
2755 bt_current_thread_clear_error();