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_MEMORY_ERROR
:
213 *fail_reason
= "not enough memory";
216 BT_LOGF("Unknown query status: status=%s",
217 bt_common_func_status_string(query_status
));
223 *user_result
= result
;
231 destroy_the_query_executor();
232 bt_value_put_ref(result
);
238 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
239 const bt_plugin
*, const char *);
242 const void *find_component_class_from_plugin(const char *plugin_name
,
243 const char *comp_class_name
,
244 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
246 const void *comp_class
= NULL
;
247 const bt_plugin
*plugin
;
249 BT_LOGI("Finding component class: plugin-name=\"%s\", "
250 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
252 plugin
= find_loaded_plugin(plugin_name
);
257 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
258 bt_object_get_ref(comp_class
);
259 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
263 BT_LOGI("Found component class: plugin-name=\"%s\", "
264 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
266 BT_LOGI("Cannot find source component class: "
267 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
268 plugin_name
, comp_class_name
);
275 const bt_component_class_source
*find_source_component_class(
276 const char *plugin_name
, const char *comp_class_name
)
278 return (const void *) find_component_class_from_plugin(
279 plugin_name
, comp_class_name
,
280 (plugin_borrow_comp_cls_func_t
)
281 bt_plugin_borrow_source_component_class_by_name_const
);
285 const bt_component_class_filter
*find_filter_component_class(
286 const char *plugin_name
, const char *comp_class_name
)
288 return (const void *) find_component_class_from_plugin(
289 plugin_name
, comp_class_name
,
290 (plugin_borrow_comp_cls_func_t
)
291 bt_plugin_borrow_filter_component_class_by_name_const
);
295 const bt_component_class_sink
*find_sink_component_class(
296 const char *plugin_name
, const char *comp_class_name
)
298 return (const void *) find_component_class_from_plugin(plugin_name
,
300 (plugin_borrow_comp_cls_func_t
)
301 bt_plugin_borrow_sink_component_class_by_name_const
);
305 const bt_component_class
*find_component_class(const char *plugin_name
,
306 const char *comp_class_name
,
307 bt_component_class_type comp_class_type
)
309 const bt_component_class
*comp_cls
= NULL
;
311 switch (comp_class_type
) {
312 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
313 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
315 case BT_COMPONENT_CLASS_TYPE_FILTER
:
316 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
318 case BT_COMPONENT_CLASS_TYPE_SINK
:
319 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
329 void print_indent(FILE *fp
, size_t indent
)
333 for (i
= 0; i
< indent
; i
++) {
339 const char *component_type_str(bt_component_class_type type
)
342 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
344 case BT_COMPONENT_CLASS_TYPE_SINK
:
346 case BT_COMPONENT_CLASS_TYPE_FILTER
:
354 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
355 const char *comp_cls_name
, bt_component_class_type type
)
357 GString
*shell_plugin_name
= NULL
;
358 GString
*shell_comp_cls_name
= NULL
;
361 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
362 if (!shell_plugin_name
) {
367 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
368 if (!shell_comp_cls_name
) {
372 fprintf(fh
, "'%s%s%s%s",
373 bt_common_color_bold(),
374 bt_common_color_fg_cyan(),
375 component_type_str(type
),
376 bt_common_color_fg_default());
378 if (shell_plugin_name
) {
379 fprintf(fh
, ".%s%s%s",
380 bt_common_color_fg_blue(),
381 shell_plugin_name
->str
,
382 bt_common_color_fg_default());
385 fprintf(fh
, ".%s%s%s'",
386 bt_common_color_fg_yellow(),
387 shell_comp_cls_name
->str
,
388 bt_common_color_reset());
391 if (shell_plugin_name
) {
392 g_string_free(shell_plugin_name
, TRUE
);
395 if (shell_comp_cls_name
) {
396 g_string_free(shell_comp_cls_name
, TRUE
);
401 void print_value(FILE *, const bt_value
*, size_t);
404 void print_value_rec(FILE *, const bt_value
*, size_t);
406 struct print_map_value_data
{
412 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
415 struct print_map_value_data
*print_map_value_data
= data
;
417 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
418 fprintf(print_map_value_data
->fp
, "%s: ", key
);
421 if (bt_value_is_array(object
) &&
422 bt_value_array_is_empty(object
)) {
423 fprintf(print_map_value_data
->fp
, "[ ]\n");
427 if (bt_value_is_map(object
) &&
428 bt_value_map_is_empty(object
)) {
429 fprintf(print_map_value_data
->fp
, "{ }\n");
433 if (bt_value_is_array(object
) ||
434 bt_value_is_map(object
)) {
435 fprintf(print_map_value_data
->fp
, "\n");
438 print_value_rec(print_map_value_data
->fp
, object
,
439 print_map_value_data
->indent
+ 2);
444 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
458 switch (bt_value_get_type(value
)) {
459 case BT_VALUE_TYPE_NULL
:
460 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
461 bt_common_color_reset());
463 case BT_VALUE_TYPE_BOOL
:
464 bool_val
= bt_value_bool_get(value
);
465 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
466 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
467 bt_common_color_reset());
469 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
470 uint_val
= bt_value_integer_unsigned_get(value
);
471 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
472 bt_common_color_fg_red(), uint_val
,
473 bt_common_color_reset());
475 case BT_VALUE_TYPE_SIGNED_INTEGER
:
476 int_val
= bt_value_integer_signed_get(value
);
477 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
478 bt_common_color_fg_red(), int_val
,
479 bt_common_color_reset());
481 case BT_VALUE_TYPE_REAL
:
482 dbl_val
= bt_value_real_get(value
);
483 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
484 bt_common_color_fg_red(), dbl_val
,
485 bt_common_color_reset());
487 case BT_VALUE_TYPE_STRING
:
488 str_val
= bt_value_string_get(value
);
489 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
490 bt_common_color_fg_green(), str_val
,
491 bt_common_color_reset());
493 case BT_VALUE_TYPE_ARRAY
:
494 size
= bt_value_array_get_size(value
);
500 print_indent(fp
, indent
);
501 fprintf(fp
, "[ ]\n");
505 for (i
= 0; i
< size
; i
++) {
506 const bt_value
*element
=
507 bt_value_array_borrow_element_by_index_const(
513 print_indent(fp
, indent
);
516 if (bt_value_is_array(element
) &&
517 bt_value_array_is_empty(element
)) {
518 fprintf(fp
, "[ ]\n");
522 if (bt_value_is_map(element
) &&
523 bt_value_map_is_empty(element
)) {
524 fprintf(fp
, "{ }\n");
528 if (bt_value_is_array(element
) ||
529 bt_value_is_map(element
)) {
533 print_value_rec(fp
, element
, indent
+ 2);
536 case BT_VALUE_TYPE_MAP
:
538 struct print_map_value_data data
= {
543 if (bt_value_map_is_empty(value
)) {
544 print_indent(fp
, indent
);
545 fprintf(fp
, "{ }\n");
549 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
558 BT_LOGE("Error printing value of type %s.",
559 bt_common_value_type_string(bt_value_get_type(value
)));
563 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
565 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
566 print_indent(fp
, indent
);
569 print_value_rec(fp
, value
, indent
);
573 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
575 fprintf(stderr
, " ");
576 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
577 bt_config_component
->comp_cls_name
->str
,
578 bt_config_component
->type
);
579 fprintf(stderr
, ":\n");
581 if (bt_config_component
->instance_name
->len
> 0) {
582 fprintf(stderr
, " Name: %s\n",
583 bt_config_component
->instance_name
->str
);
586 fprintf(stderr
, " Parameters:\n");
587 print_value(stderr
, bt_config_component
->params
, 8);
591 void print_bt_config_components(GPtrArray
*array
)
595 for (i
= 0; i
< array
->len
; i
++) {
596 struct bt_config_component
*cfg_component
=
597 bt_config_get_component(array
, i
);
598 print_bt_config_component(cfg_component
);
599 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
604 void print_plugin_paths(const bt_value
*plugin_paths
)
606 fprintf(stderr
, " Plugin paths:\n");
607 print_value(stderr
, plugin_paths
, 4);
611 void print_cfg_run(struct bt_config
*cfg
)
615 print_plugin_paths(cfg
->plugin_paths
);
616 fprintf(stderr
, " Source component instances:\n");
617 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
619 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
620 fprintf(stderr
, " Filter component instances:\n");
621 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
624 fprintf(stderr
, " Sink component instances:\n");
625 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
626 fprintf(stderr
, " Connections:\n");
628 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
629 struct bt_config_connection
*cfg_connection
=
630 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
633 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
634 cfg_connection
->upstream_comp_name
->str
,
635 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
636 cfg_connection
->upstream_port_glob
->str
,
637 cfg_connection
->downstream_comp_name
->str
,
638 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
639 cfg_connection
->downstream_port_glob
->str
);
644 void print_cfg_list_plugins(struct bt_config
*cfg
)
646 print_plugin_paths(cfg
->plugin_paths
);
650 void print_cfg_help(struct bt_config
*cfg
)
652 print_plugin_paths(cfg
->plugin_paths
);
656 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
658 print_plugin_paths(cfg
->plugin_paths
);
659 fprintf(stderr
, " Path: %s\n",
660 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
664 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
666 print_plugin_paths(cfg
->plugin_paths
);
667 fprintf(stderr
, " URL: %s\n",
668 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
672 void print_cfg_query(struct bt_config
*cfg
)
674 print_plugin_paths(cfg
->plugin_paths
);
675 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
676 fprintf(stderr
, " Component class:\n");
677 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
681 void print_cfg(struct bt_config
*cfg
)
683 if (!BT_LOG_ON_INFO
) {
687 BT_LOGI_STR("CLI configuration:");
688 BT_LOGI(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
689 BT_LOGI(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
691 switch (cfg
->command
) {
692 case BT_CONFIG_COMMAND_RUN
:
695 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
696 print_cfg_list_plugins(cfg
);
698 case BT_CONFIG_COMMAND_HELP
:
701 case BT_CONFIG_COMMAND_QUERY
:
702 print_cfg_query(cfg
);
704 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
705 print_cfg_print_ctf_metadata(cfg
);
707 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
708 print_cfg_print_lttng_live_sessions(cfg
);
716 void print_plugin_info(const bt_plugin
*plugin
)
718 unsigned int major
, minor
, patch
;
720 bt_property_availability version_avail
;
721 const char *plugin_name
;
725 const char *plugin_description
;
727 plugin_name
= bt_plugin_get_name(plugin
);
728 path
= bt_plugin_get_path(plugin
);
729 author
= bt_plugin_get_author(plugin
);
730 license
= bt_plugin_get_license(plugin
);
731 plugin_description
= bt_plugin_get_description(plugin
);
732 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
734 printf("%s%s%s%s:\n", bt_common_color_bold(),
735 bt_common_color_fg_blue(), plugin_name
,
736 bt_common_color_reset());
738 printf(" %sPath%s: %s\n", bt_common_color_bold(),
739 bt_common_color_reset(), path
);
744 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
745 printf(" %sVersion%s: %u.%u.%u",
746 bt_common_color_bold(), bt_common_color_reset(),
747 major
, minor
, patch
);
756 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
757 bt_common_color_reset(),
758 plugin_description
? plugin_description
: "(None)");
759 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
760 bt_common_color_reset(), author
? author
: "(Unknown)");
761 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
762 bt_common_color_reset(),
763 license
? license
: "(Unknown)");
767 int cmd_query(struct bt_config
*cfg
)
770 const bt_component_class
*comp_cls
= NULL
;
771 const bt_value
*results
= NULL
;
772 const char *fail_reason
= NULL
;
774 comp_cls
= find_component_class(
775 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
776 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
777 cfg
->cmd_data
.query
.cfg_component
->type
);
779 BT_CLI_LOGE_APPEND_CAUSE(
780 "Cannot find component class: plugin-name=\"%s\", "
781 "comp-cls-name=\"%s\", comp-cls-type=%d",
782 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
783 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
784 cfg
->cmd_data
.query
.cfg_component
->type
);
789 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
790 cfg
->cmd_data
.query
.cfg_component
->params
,
791 &results
, &fail_reason
);
796 print_value(stdout
, results
, 0);
800 BT_CLI_LOGE_APPEND_CAUSE(
801 "Failed to query component class: %s: plugin-name=\"%s\", "
802 "comp-cls-name=\"%s\", comp-cls-type=%d "
803 "object=\"%s\"", fail_reason
,
804 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
805 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
806 cfg
->cmd_data
.query
.cfg_component
->type
,
807 cfg
->cmd_data
.query
.object
->str
);
811 bt_component_class_put_ref(comp_cls
);
812 bt_value_put_ref(results
);
817 void print_component_class_help(const char *plugin_name
,
818 const bt_component_class
*comp_cls
)
820 const char *comp_class_name
=
821 bt_component_class_get_name(comp_cls
);
822 const char *comp_class_description
=
823 bt_component_class_get_description(comp_cls
);
824 const char *comp_class_help
=
825 bt_component_class_get_help(comp_cls
);
826 bt_component_class_type type
=
827 bt_component_class_get_type(comp_cls
);
829 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
831 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
832 bt_common_color_reset(),
833 comp_class_description
? comp_class_description
: "(None)");
835 if (comp_class_help
) {
836 printf("\n%s\n", comp_class_help
);
841 int cmd_help(struct bt_config
*cfg
)
844 const bt_plugin
*plugin
= NULL
;
845 const bt_component_class
*needed_comp_cls
= NULL
;
847 plugin
= find_loaded_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
849 BT_CLI_LOGE_APPEND_CAUSE(
850 "Cannot find plugin: plugin-name=\"%s\"",
851 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
856 print_plugin_info(plugin
);
857 printf(" %sSource component classes%s: %d\n",
858 bt_common_color_bold(),
859 bt_common_color_reset(),
860 (int) bt_plugin_get_source_component_class_count(plugin
));
861 printf(" %sFilter component classes%s: %d\n",
862 bt_common_color_bold(),
863 bt_common_color_reset(),
864 (int) bt_plugin_get_filter_component_class_count(plugin
));
865 printf(" %sSink component classes%s: %d\n",
866 bt_common_color_bold(),
867 bt_common_color_reset(),
868 (int) bt_plugin_get_sink_component_class_count(plugin
));
870 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
871 /* Plugin help only */
875 needed_comp_cls
= find_component_class(
876 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
877 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
878 cfg
->cmd_data
.help
.cfg_component
->type
);
879 if (!needed_comp_cls
) {
880 BT_CLI_LOGE_APPEND_CAUSE(
881 "Cannot find component class: plugin-name=\"%s\", "
882 "comp-cls-name=\"%s\", comp-cls-type=%d",
883 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
884 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
885 cfg
->cmd_data
.help
.cfg_component
->type
);
891 print_component_class_help(
892 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
896 bt_component_class_put_ref(needed_comp_cls
);
897 bt_plugin_put_ref(plugin
);
901 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
903 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
906 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
907 const char *cc_type_name
, uint64_t count
,
908 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
909 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
914 printf(" %s%s component classes%s: (none)\n",
915 bt_common_color_bold(),
917 bt_common_color_reset());
920 printf(" %s%s component classes%s:\n",
921 bt_common_color_bold(),
923 bt_common_color_reset());
926 for (i
= 0; i
< count
; i
++) {
927 const bt_component_class
*comp_class
=
928 spec_comp_cls_borrow_comp_cls_func(
929 borrow_comp_cls_by_index_func(plugin
, i
));
930 const char *comp_class_name
=
931 bt_component_class_get_name(comp_class
);
932 const char *comp_class_description
=
933 bt_component_class_get_description(comp_class
);
934 bt_component_class_type type
=
935 bt_component_class_get_type(comp_class
);
938 print_plugin_comp_cls_opt(stdout
,
939 bt_plugin_get_name(plugin
), comp_class_name
,
942 if (comp_class_description
) {
943 printf(": %s", comp_class_description
);
954 int cmd_list_plugins(struct bt_config
*cfg
)
957 int plugins_count
, component_classes_count
= 0, i
;
959 printf("From the following plugin paths:\n\n");
960 print_value(stdout
, cfg
->plugin_paths
, 2);
962 plugins_count
= get_loaded_plugins_count();
963 if (plugins_count
== 0) {
964 printf("No plugins found.\n");
968 for (i
= 0; i
< plugins_count
; i
++) {
969 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
971 component_classes_count
+=
972 bt_plugin_get_source_component_class_count(plugin
) +
973 bt_plugin_get_filter_component_class_count(plugin
) +
974 bt_plugin_get_sink_component_class_count(plugin
);
977 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
978 bt_common_color_bold(),
979 component_classes_count
,
980 bt_common_color_reset(),
981 bt_common_color_bold(),
983 bt_common_color_reset());
985 for (i
= 0; i
< plugins_count
; i
++) {
986 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
989 print_plugin_info(plugin
);
990 cmd_list_plugins_print_component_classes(plugin
, "Source",
991 bt_plugin_get_source_component_class_count(plugin
),
992 (plugin_borrow_comp_cls_by_index_func_t
)
993 bt_plugin_borrow_source_component_class_by_index_const
,
994 (spec_comp_cls_borrow_comp_cls_func_t
)
995 bt_component_class_source_as_component_class
);
996 cmd_list_plugins_print_component_classes(plugin
, "Filter",
997 bt_plugin_get_filter_component_class_count(plugin
),
998 (plugin_borrow_comp_cls_by_index_func_t
)
999 bt_plugin_borrow_filter_component_class_by_index_const
,
1000 (spec_comp_cls_borrow_comp_cls_func_t
)
1001 bt_component_class_filter_as_component_class
);
1002 cmd_list_plugins_print_component_classes(plugin
, "Sink",
1003 bt_plugin_get_sink_component_class_count(plugin
),
1004 (plugin_borrow_comp_cls_by_index_func_t
)
1005 bt_plugin_borrow_sink_component_class_by_index_const
,
1006 (spec_comp_cls_borrow_comp_cls_func_t
)
1007 bt_component_class_sink_as_component_class
);
1015 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
1018 const bt_component_class
*comp_cls
= NULL
;
1019 const bt_value
*results
= NULL
;
1020 bt_value
*params
= NULL
;
1021 const bt_value
*map
= NULL
;
1022 const bt_value
*v
= NULL
;
1023 static const char * const plugin_name
= "ctf";
1024 static const char * const comp_cls_name
= "lttng-live";
1025 static const bt_component_class_type comp_cls_type
=
1026 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1027 int64_t array_size
, i
;
1028 const char *fail_reason
= NULL
;
1029 FILE *out_stream
= stdout
;
1031 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1032 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1035 BT_CLI_LOGE_APPEND_CAUSE(
1036 "Cannot find component class: plugin-name=\"%s\", "
1037 "comp-cls-name=\"%s\", comp-cls-type=%d",
1038 plugin_name
, comp_cls_name
,
1039 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1043 params
= bt_value_map_create();
1048 ret
= bt_value_map_insert_string_entry(params
, "url",
1049 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1054 ret
= query(cfg
, comp_cls
, "sessions", params
,
1055 &results
, &fail_reason
);
1062 if (!bt_value_is_array(results
)) {
1063 BT_CLI_LOGE_APPEND_CAUSE(
1064 "Expecting an array for LTTng live `sessions` query.");
1068 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1070 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1074 BT_LOGE_ERRNO("Cannot open file for writing",
1076 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1077 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1079 "Cannot open file for writing: path=\"%s\"",
1080 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1085 array_size
= bt_value_array_get_size(results
);
1086 for (i
= 0; i
< array_size
; i
++) {
1087 const char *url_text
;
1088 int64_t timer_us
, streams
, clients
;
1090 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1092 BT_CLI_LOGE_APPEND_CAUSE("Unexpected empty array entry.");
1095 if (!bt_value_is_map(map
)) {
1096 BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type.");
1100 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1102 BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry.");
1105 url_text
= bt_value_string_get(v
);
1106 fprintf(out_stream
, "%s", url_text
);
1107 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1109 BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry.");
1112 timer_us
= bt_value_integer_signed_get(v
);
1113 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1114 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1116 BT_CLI_LOGE_APPEND_CAUSE(
1117 "Missing `stream-count` entry.");
1120 streams
= bt_value_integer_signed_get(v
);
1121 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1122 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1124 BT_CLI_LOGE_APPEND_CAUSE(
1125 "Missing `client-count` entry.");
1128 clients
= bt_value_integer_signed_get(v
);
1129 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1135 BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s",
1142 bt_value_put_ref(results
);
1143 bt_value_put_ref(params
);
1144 bt_component_class_put_ref(comp_cls
);
1146 if (out_stream
&& out_stream
!= stdout
) {
1147 int fclose_ret
= fclose(out_stream
);
1150 BT_LOGE_ERRNO("Cannot close file stream",
1152 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1160 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1163 const bt_component_class
*comp_cls
= NULL
;
1164 const bt_value
*results
= NULL
;
1165 bt_value
*params
= NULL
;
1166 const bt_value
*metadata_text_value
= NULL
;
1167 const char *metadata_text
= NULL
;
1168 static const char * const plugin_name
= "ctf";
1169 static const char * const comp_cls_name
= "fs";
1170 static const bt_component_class_type comp_cls_type
=
1171 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1172 const char *fail_reason
= NULL
;
1173 FILE *out_stream
= stdout
;
1175 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1176 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1179 BT_CLI_LOGE_APPEND_CAUSE(
1180 "Cannot find component class: plugin-name=\"%s\", "
1181 "comp-cls-name=\"%s\", comp-cls-type=%d",
1182 plugin_name
, comp_cls_name
,
1183 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1188 params
= bt_value_map_create();
1194 ret
= bt_value_map_insert_string_entry(params
, "path",
1195 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1201 ret
= query(cfg
, comp_cls
, "metadata-info",
1202 params
, &results
, &fail_reason
);
1207 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1209 if (!metadata_text_value
) {
1210 BT_CLI_LOGE_APPEND_CAUSE(
1211 "Cannot find `text` string value in the resulting metadata info object.");
1216 metadata_text
= bt_value_string_get(metadata_text_value
);
1218 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1220 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1223 BT_LOGE_ERRNO("Cannot open file for writing",
1225 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1226 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1228 "Cannot open file for writing: path=\"%s\"",
1229 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1235 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1237 BT_CLI_LOGE_APPEND_CAUSE(
1238 "Cannot write whole metadata text to output stream: "
1248 BT_CLI_LOGE_APPEND_CAUSE(
1249 "Failed to query `metadata-info` object: %s", fail_reason
);
1252 bt_value_put_ref(results
);
1253 bt_value_put_ref(params
);
1254 bt_component_class_put_ref(comp_cls
);
1256 if (out_stream
&& out_stream
!= stdout
) {
1257 int fclose_ret
= fclose(out_stream
);
1260 BT_LOGE_ERRNO("Cannot close file stream",
1262 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1270 char *instance_name
;
1274 struct trace_range
{
1275 uint64_t intersection_range_begin_ns
;
1276 uint64_t intersection_range_end_ns
;
1280 guint
port_id_hash(gconstpointer v
)
1282 const struct port_id
*id
= v
;
1284 BT_ASSERT(id
->instance_name
);
1285 BT_ASSERT(id
->port_name
);
1287 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1291 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1293 const struct port_id
*id1
= v1
;
1294 const struct port_id
*id2
= v2
;
1296 return strcmp(id1
->instance_name
, id2
->instance_name
) == 0 &&
1297 strcmp(id1
->port_name
, id2
->port_name
) == 0;
1301 void port_id_destroy(gpointer data
)
1303 struct port_id
*id
= data
;
1305 free(id
->instance_name
);
1306 free(id
->port_name
);
1311 void trace_range_destroy(gpointer data
)
1316 struct cmd_run_ctx
{
1318 GHashTable
*src_components
;
1321 GHashTable
*flt_components
;
1324 GHashTable
*sink_components
;
1330 struct bt_config
*cfg
;
1334 bool stream_intersection_mode
;
1337 * Association of struct port_id -> struct trace_range.
1339 GHashTable
*intersections
;
1342 /* Returns a timestamp of the form "(-)s.ns" */
1344 char *s_from_ns(int64_t ns
)
1349 int64_t ts_sec_abs
, ts_nsec_abs
;
1350 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1351 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1353 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1354 is_negative
= false;
1355 ts_sec_abs
= ts_sec
;
1356 ts_nsec_abs
= ts_nsec
;
1357 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1358 is_negative
= false;
1359 ts_sec_abs
= ts_sec
- 1;
1360 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1361 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1363 ts_sec_abs
= ts_sec
;
1364 ts_nsec_abs
= -ts_nsec
;
1365 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1367 ts_sec_abs
= -(ts_sec
+ 1);
1368 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1369 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1371 ts_sec_abs
= -ts_sec
;
1372 ts_nsec_abs
= ts_nsec
;
1373 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1375 ts_sec_abs
= -ts_sec
;
1376 ts_nsec_abs
= -ts_nsec
;
1379 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1380 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1388 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1389 struct cmd_run_ctx
*ctx
,
1390 const bt_component
*upstream_comp
,
1391 const bt_port_output
*out_upstream_port
,
1392 struct bt_config_connection
*cfg_conn
)
1394 typedef uint64_t (*input_port_count_func_t
)(void *);
1395 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1396 const void *, uint64_t);
1397 const bt_port
*upstream_port
=
1398 bt_port_output_as_port_const(out_upstream_port
);
1401 GQuark downstreamp_comp_name_quark
;
1402 void *downstream_comp
;
1403 uint64_t downstream_port_count
;
1405 input_port_count_func_t port_count_fn
;
1406 borrow_input_port_by_index_func_t port_by_index_fn
;
1407 bt_graph_connect_ports_status connect_ports_status
=
1408 BT_GRAPH_CONNECT_PORTS_STATUS_OK
;
1409 bool insert_trimmer
= false;
1410 bt_value
*trimmer_params
= NULL
;
1411 char *intersection_begin
= NULL
;
1412 char *intersection_end
= NULL
;
1413 const bt_component_filter
*trimmer
= NULL
;
1414 const bt_component_class_filter
*trimmer_class
= NULL
;
1415 const bt_port_input
*trimmer_input
= NULL
;
1416 const bt_port_output
*trimmer_output
= NULL
;
1418 if (ctx
->intersections
&&
1419 bt_component_get_class_type(upstream_comp
) ==
1420 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1421 struct trace_range
*range
;
1422 struct port_id port_id
= {
1423 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1424 .port_name
= (char *) bt_port_get_name(upstream_port
)
1427 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1431 range
= (struct trace_range
*) g_hash_table_lookup(
1432 ctx
->intersections
, &port_id
);
1434 bt_value_map_insert_entry_status insert_status
;
1436 intersection_begin
= s_from_ns(
1437 range
->intersection_range_begin_ns
);
1438 intersection_end
= s_from_ns(
1439 range
->intersection_range_end_ns
);
1440 if (!intersection_begin
|| !intersection_end
) {
1441 BT_CLI_LOGE_APPEND_CAUSE(
1442 "Cannot create trimmer argument timestamp string.");
1446 insert_trimmer
= true;
1447 trimmer_params
= bt_value_map_create();
1448 if (!trimmer_params
) {
1452 insert_status
= bt_value_map_insert_string_entry(
1453 trimmer_params
, "begin", intersection_begin
);
1454 if (insert_status
< 0) {
1457 insert_status
= bt_value_map_insert_string_entry(
1459 "end", intersection_end
);
1460 if (insert_status
< 0) {
1465 trimmer_class
= find_filter_component_class("utils", "trimmer");
1466 if (!trimmer_class
) {
1471 BT_LOGI("Connecting upstream port to the next available downstream port: "
1472 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1473 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1474 upstream_port
, bt_port_get_name(upstream_port
),
1475 cfg_conn
->downstream_comp_name
->str
,
1476 cfg_conn
->arg
->str
);
1477 downstreamp_comp_name_quark
= g_quark_from_string(
1478 cfg_conn
->downstream_comp_name
->str
);
1479 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1480 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1481 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1482 port_count_fn
= (input_port_count_func_t
)
1483 bt_component_filter_get_input_port_count
;
1484 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1485 bt_component_filter_borrow_input_port_by_index_const
;
1487 if (!downstream_comp
) {
1488 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1489 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1490 port_count_fn
= (input_port_count_func_t
)
1491 bt_component_sink_get_input_port_count
;
1492 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1493 bt_component_sink_borrow_input_port_by_index_const
;
1496 if (!downstream_comp
) {
1497 BT_CLI_LOGE_APPEND_CAUSE("Cannot find downstream component: "
1498 "comp-name=\"%s\", conn-arg=\"%s\"",
1499 cfg_conn
->downstream_comp_name
->str
,
1500 cfg_conn
->arg
->str
);
1504 downstream_port_count
= port_count_fn(downstream_comp
);
1506 for (i
= 0; i
< downstream_port_count
; i
++) {
1507 const bt_port_input
*in_downstream_port
=
1508 port_by_index_fn(downstream_comp
, i
);
1509 const bt_port
*downstream_port
=
1510 bt_port_input_as_port_const(in_downstream_port
);
1511 const char *upstream_port_name
;
1512 const char *downstream_port_name
;
1514 BT_ASSERT(downstream_port
);
1516 /* Skip port if it's already connected. */
1517 if (bt_port_is_connected(downstream_port
)) {
1518 BT_LOGI("Skipping downstream port: already connected: "
1519 "port-addr=%p, port-name=\"%s\"",
1521 bt_port_get_name(downstream_port
));
1525 downstream_port_name
= bt_port_get_name(downstream_port
);
1526 BT_ASSERT(downstream_port_name
);
1527 upstream_port_name
= bt_port_get_name(upstream_port
);
1528 BT_ASSERT(upstream_port_name
);
1530 if (!bt_common_star_glob_match(
1531 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1532 downstream_port_name
, SIZE_MAX
)) {
1536 if (insert_trimmer
) {
1538 * In order to insert the trimmer between the
1539 * two components that were being connected, we
1540 * create a connection configuration entry which
1541 * describes a connection from the trimmer's
1542 * output to the original input that was being
1545 * Hence, the creation of the trimmer will cause
1546 * the graph "new port" listener to establish
1547 * all downstream connections as its output port
1548 * is connected. We will then establish the
1549 * connection between the original upstream
1550 * source and the trimmer.
1552 char *trimmer_name
= NULL
;
1553 bt_graph_add_component_status add_comp_status
;
1555 ret
= asprintf(&trimmer_name
,
1556 "stream-intersection-trimmer-%s",
1557 upstream_port_name
);
1563 ctx
->connect_ports
= false;
1564 add_comp_status
= bt_graph_add_filter_component(
1565 ctx
->graph
, trimmer_class
, trimmer_name
,
1566 trimmer_params
, ctx
->cfg
->log_level
,
1569 if (add_comp_status
!=
1570 BT_GRAPH_ADD_COMPONENT_STATUS_OK
) {
1576 bt_component_filter_borrow_input_port_by_index_const(
1578 if (!trimmer_input
) {
1582 bt_component_filter_borrow_output_port_by_index_const(
1584 if (!trimmer_output
) {
1589 * Replace the current downstream port by the trimmer's
1592 in_downstream_port
= trimmer_input
;
1594 bt_port_input_as_port_const(in_downstream_port
);
1595 downstream_port_name
= bt_port_get_name(
1597 BT_ASSERT(downstream_port_name
);
1600 /* We have a winner! */
1601 connect_ports_status
= bt_graph_connect_ports(ctx
->graph
,
1602 out_upstream_port
, in_downstream_port
, NULL
);
1603 downstream_port
= NULL
;
1604 switch (connect_ports_status
) {
1605 case BT_GRAPH_CONNECT_PORTS_STATUS_OK
:
1607 case BT_GRAPH_CONNECT_PORTS_STATUS_CANCELED
:
1608 BT_CLI_LOGW_APPEND_CAUSE("Graph was canceled by user.");
1611 BT_CLI_LOGE_APPEND_CAUSE(
1612 "Cannot create connection: graph refuses to connect ports: "
1613 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1614 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1615 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1616 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1618 upstream_comp
, bt_component_get_name(upstream_comp
),
1619 upstream_port
, bt_port_get_name(upstream_port
),
1620 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1621 downstream_port
, downstream_port_name
,
1622 cfg_conn
->arg
->str
);
1626 BT_LOGI("Connected component ports: "
1627 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1628 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1629 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1630 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1632 upstream_comp
, bt_component_get_name(upstream_comp
),
1633 upstream_port
, bt_port_get_name(upstream_port
),
1634 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1635 downstream_port
, downstream_port_name
,
1636 cfg_conn
->arg
->str
);
1638 if (insert_trimmer
) {
1640 * The first connection, from the source to the trimmer,
1641 * has been done. We now connect the trimmer to the
1642 * original downstream port.
1644 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1646 bt_component_filter_as_component_const(trimmer
),
1647 trimmer_output
, cfg_conn
);
1651 ctx
->connect_ports
= true;
1655 * We found a matching downstream port: the search is
1661 /* No downstream port found */
1662 BT_CLI_LOGE_APPEND_CAUSE(
1663 "Cannot create connection: cannot find a matching downstream port for upstream port: "
1664 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1665 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1666 upstream_port
, bt_port_get_name(upstream_port
),
1667 cfg_conn
->downstream_comp_name
->str
,
1668 cfg_conn
->arg
->str
);
1674 free(intersection_begin
);
1675 free(intersection_end
);
1676 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1677 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1678 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1683 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1684 const bt_port_output
*upstream_port
)
1687 const char *upstream_port_name
;
1688 const char *upstream_comp_name
;
1689 const bt_component
*upstream_comp
= NULL
;
1693 BT_ASSERT(upstream_port
);
1694 upstream_port_name
= bt_port_get_name(
1695 bt_port_output_as_port_const(upstream_port
));
1696 BT_ASSERT(upstream_port_name
);
1697 upstream_comp
= bt_port_borrow_component_const(
1698 bt_port_output_as_port_const(upstream_port
));
1699 BT_ASSERT(upstream_comp
);
1700 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1701 BT_ASSERT(upstream_comp_name
);
1702 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1703 "port-addr=%p, port-name=\"%s\"",
1704 upstream_comp
, upstream_comp_name
,
1705 upstream_port
, upstream_port_name
);
1707 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1708 struct bt_config_connection
*cfg_conn
=
1710 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1712 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1713 upstream_comp_name
)) {
1717 if (!bt_common_star_glob_match(
1718 cfg_conn
->upstream_port_glob
->str
,
1719 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1723 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1724 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1726 BT_CLI_LOGE_APPEND_CAUSE(
1727 "Cannot connect upstream port: "
1728 "port-addr=%p, port-name=\"%s\"",
1730 upstream_port_name
);
1736 BT_CLI_LOGE_APPEND_CAUSE(
1737 "Cannot connect upstream port: port does not match any connection argument: "
1738 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1739 upstream_port_name
);
1749 bt_graph_listener_func_status
1750 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1751 const bt_port_output
*out_port
)
1753 const bt_component
*comp
;
1754 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1755 bt_graph_listener_func_status ret
=
1756 BT_GRAPH_LISTENER_FUNC_STATUS_OK
;
1758 comp
= bt_port_borrow_component_const(port
);
1759 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1760 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1761 comp
, comp
? bt_component_get_name(comp
) : "",
1762 port
, bt_port_get_name(port
));
1764 if (!ctx
->connect_ports
) {
1770 if (bt_port_is_connected(port
)) {
1771 BT_LOGW_STR("Port is already connected.");
1775 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
1776 BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port.");
1777 ret
= BT_GRAPH_LISTENER_FUNC_STATUS_ERROR
;
1786 bt_graph_listener_func_status
graph_source_output_port_added_listener(
1787 const bt_component_source
*component
,
1788 const bt_port_output
*port
, void *data
)
1790 return graph_output_port_added_listener(data
, port
);
1794 bt_graph_listener_func_status
graph_filter_output_port_added_listener(
1795 const bt_component_filter
*component
,
1796 const bt_port_output
*port
, void *data
)
1798 return graph_output_port_added_listener(data
, port
);
1802 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1808 if (ctx
->src_components
) {
1809 g_hash_table_destroy(ctx
->src_components
);
1810 ctx
->src_components
= NULL
;
1813 if (ctx
->flt_components
) {
1814 g_hash_table_destroy(ctx
->flt_components
);
1815 ctx
->flt_components
= NULL
;
1818 if (ctx
->sink_components
) {
1819 g_hash_table_destroy(ctx
->sink_components
);
1820 ctx
->sink_components
= NULL
;
1823 if (ctx
->intersections
) {
1824 g_hash_table_destroy(ctx
->intersections
);
1825 ctx
->intersections
= NULL
;
1828 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
1834 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1837 bt_graph_add_listener_status add_listener_status
;
1840 ctx
->connect_ports
= false;
1841 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
1842 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1843 if (!ctx
->src_components
) {
1847 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
1848 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1849 if (!ctx
->flt_components
) {
1853 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
1854 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1855 if (!ctx
->sink_components
) {
1859 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1860 ctx
->stream_intersection_mode
= true;
1861 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1862 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1863 if (!ctx
->intersections
) {
1868 ctx
->graph
= bt_graph_create();
1873 the_graph
= ctx
->graph
;
1874 add_listener_status
= bt_graph_add_source_component_output_port_added_listener(
1875 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
1877 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1878 BT_CLI_LOGE_APPEND_CAUSE(
1879 "Cannot add \"port added\" listener to graph.");
1883 add_listener_status
= bt_graph_add_filter_component_output_port_added_listener(
1884 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
1886 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1887 BT_CLI_LOGE_APPEND_CAUSE(
1888 "Cannot add \"port added\" listener to graph.");
1895 cmd_run_ctx_destroy(ctx
);
1903 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
1904 struct bt_config_component
*cfg_comp
,
1905 const bt_component_class_source
*src_comp_cls
)
1909 int64_t trace_count
;
1910 const char *path
= NULL
;
1911 const bt_value
*query_result
= NULL
;
1912 const bt_value
*trace_info
= NULL
;
1913 const bt_value
*intersection_range
= NULL
;
1914 const bt_value
*intersection_begin
= NULL
;
1915 const bt_value
*intersection_end
= NULL
;
1916 const bt_value
*stream_infos
= NULL
;
1917 const bt_value
*stream_info
= NULL
;
1918 struct port_id
*port_id
= NULL
;
1919 struct trace_range
*trace_range
= NULL
;
1920 const char *fail_reason
= NULL
;
1921 const bt_component_class
*comp_cls
=
1922 bt_component_class_source_as_component_class_const(src_comp_cls
);
1924 ret
= query(ctx
->cfg
, comp_cls
, "trace-info",
1925 cfg_comp
->params
, &query_result
,
1928 BT_LOGD("Component class does not support the `trace-info` query: %s: "
1929 "comp-class-name=\"%s\"", fail_reason
,
1930 bt_component_class_get_name(comp_cls
));
1935 BT_ASSERT(query_result
);
1937 if (!bt_value_is_array(query_result
)) {
1938 BT_LOGD("Unexpected format of \'trace-info\' query result: "
1939 "component-class-name=%s",
1940 bt_component_class_get_name(comp_cls
));
1945 trace_count
= bt_value_array_get_size(query_result
);
1946 if (trace_count
< 0) {
1951 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
1953 uint64_t stream_idx
;
1954 int64_t stream_count
;
1956 trace_info
= bt_value_array_borrow_element_by_index_const(
1957 query_result
, trace_idx
);
1958 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
1960 BT_LOGD_STR("Cannot retrieve trace from query result.");
1964 intersection_range
= bt_value_map_borrow_entry_value_const(
1965 trace_info
, "intersection-range-ns");
1966 if (!intersection_range
) {
1968 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
1972 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
1974 if (!intersection_begin
) {
1976 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
1980 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
1982 if (!intersection_end
) {
1984 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
1988 begin
= bt_value_integer_signed_get(intersection_begin
);
1989 end
= bt_value_integer_signed_get(intersection_end
);
1991 if (begin
< 0 || end
< 0 || end
< begin
) {
1992 BT_CLI_LOGE_APPEND_CAUSE(
1993 "Invalid trace stream intersection values: "
1994 "intersection-range-ns:begin=%" PRId64
1995 ", intersection-range-ns:end=%" PRId64
,
2001 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
2003 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2005 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2009 stream_count
= bt_value_array_get_size(stream_infos
);
2010 if (stream_count
< 0) {
2015 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2016 const bt_value
*port_name
;
2018 port_id
= g_new0(struct port_id
, 1);
2021 BT_CLI_LOGE_APPEND_CAUSE(
2022 "Cannot allocate memory for port_id structure.");
2025 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2026 if (!port_id
->instance_name
) {
2028 BT_CLI_LOGE_APPEND_CAUSE(
2029 "Cannot allocate memory for port_id component instance name.");
2033 trace_range
= g_new0(struct trace_range
, 1);
2036 BT_CLI_LOGE_APPEND_CAUSE(
2037 "Cannot allocate memory for trace_range structure.");
2040 trace_range
->intersection_range_begin_ns
= begin
;
2041 trace_range
->intersection_range_end_ns
= end
;
2043 stream_info
= bt_value_array_borrow_element_by_index_const(
2044 stream_infos
, stream_idx
);
2045 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2047 BT_CLI_LOGE_APPEND_CAUSE(
2048 "Cannot retrieve stream informations from trace in query result.");
2052 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2053 if (!port_name
|| !bt_value_is_string(port_name
)) {
2055 BT_CLI_LOGE_APPEND_CAUSE(
2056 "Cannot retrieve port name in query result.");
2060 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2061 if (!port_id
->port_name
) {
2063 BT_CLI_LOGE_APPEND_CAUSE(
2064 "Cannot allocate memory for port_id port_name.");
2068 BT_LOGD("Inserting stream intersection ");
2070 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2080 BT_CLI_LOGE_APPEND_CAUSE(
2081 "Cannot determine stream intersection of trace: path=\"%s\"",
2082 path
? path
: "(unknown)");
2085 bt_value_put_ref(query_result
);
2087 g_free(trace_range
);
2092 int cmd_run_ctx_create_components_from_config_components(
2093 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2096 const void *comp_cls
= NULL
;
2097 const void *comp
= NULL
;
2100 for (i
= 0; i
< cfg_components
->len
; i
++) {
2101 struct bt_config_component
*cfg_comp
=
2102 g_ptr_array_index(cfg_components
, i
);
2105 switch (cfg_comp
->type
) {
2106 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2107 comp_cls
= find_source_component_class(
2108 cfg_comp
->plugin_name
->str
,
2109 cfg_comp
->comp_cls_name
->str
);
2111 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2112 comp_cls
= find_filter_component_class(
2113 cfg_comp
->plugin_name
->str
,
2114 cfg_comp
->comp_cls_name
->str
);
2116 case BT_COMPONENT_CLASS_TYPE_SINK
:
2117 comp_cls
= find_sink_component_class(
2118 cfg_comp
->plugin_name
->str
,
2119 cfg_comp
->comp_cls_name
->str
);
2126 BT_CLI_LOGE_APPEND_CAUSE(
2127 "Cannot find component class: plugin-name=\"%s\", "
2128 "comp-cls-name=\"%s\", comp-cls-type=%d",
2129 cfg_comp
->plugin_name
->str
,
2130 cfg_comp
->comp_cls_name
->str
,
2135 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_TRACE
);
2137 switch (cfg_comp
->type
) {
2138 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2139 ret
= bt_graph_add_source_component(ctx
->graph
,
2140 comp_cls
, cfg_comp
->instance_name
->str
,
2141 cfg_comp
->params
, cfg_comp
->log_level
,
2144 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2145 ret
= bt_graph_add_filter_component(ctx
->graph
,
2146 comp_cls
, cfg_comp
->instance_name
->str
,
2147 cfg_comp
->params
, cfg_comp
->log_level
,
2150 case BT_COMPONENT_CLASS_TYPE_SINK
:
2151 ret
= bt_graph_add_sink_component(ctx
->graph
,
2152 comp_cls
, cfg_comp
->instance_name
->str
,
2153 cfg_comp
->params
, cfg_comp
->log_level
,
2161 BT_CLI_LOGE_APPEND_CAUSE(
2162 "Cannot create component: plugin-name=\"%s\", "
2163 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2165 cfg_comp
->plugin_name
->str
,
2166 cfg_comp
->comp_cls_name
->str
,
2167 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2171 if (ctx
->stream_intersection_mode
&&
2172 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2173 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2179 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2180 comp
, cfg_comp
->instance_name
->str
);
2181 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2182 BT_ASSERT(quark
> 0);
2184 switch (cfg_comp
->type
) {
2185 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2186 g_hash_table_insert(ctx
->src_components
,
2187 GUINT_TO_POINTER(quark
), (void *) comp
);
2189 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2190 g_hash_table_insert(ctx
->flt_components
,
2191 GUINT_TO_POINTER(quark
), (void *) comp
);
2193 case BT_COMPONENT_CLASS_TYPE_SINK
:
2194 g_hash_table_insert(ctx
->sink_components
,
2195 GUINT_TO_POINTER(quark
), (void *) comp
);
2202 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2211 bt_object_put_ref(comp
);
2212 bt_object_put_ref(comp_cls
);
2217 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2222 * Make sure that, during this phase, our graph's "port added"
2223 * listener does not connect ports while we are creating the
2224 * components because we have a special, initial phase for
2227 ctx
->connect_ports
= false;
2229 ret
= cmd_run_ctx_create_components_from_config_components(
2230 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2236 ret
= cmd_run_ctx_create_components_from_config_components(
2237 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2243 ret
= cmd_run_ctx_create_components_from_config_components(
2244 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2254 typedef uint64_t (*output_port_count_func_t
)(const void *);
2255 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2256 const void *, uint64_t);
2259 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2260 void *comp
, output_port_count_func_t port_count_fn
,
2261 borrow_output_port_by_index_func_t port_by_index_fn
)
2267 count
= port_count_fn(comp
);
2269 for (i
= 0; i
< count
; i
++) {
2270 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2272 BT_ASSERT(upstream_port
);
2273 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2284 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2287 GHashTableIter iter
;
2288 gpointer g_name_quark
, g_comp
;
2290 ctx
->connect_ports
= true;
2291 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2293 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2294 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2295 (output_port_count_func_t
)
2296 bt_component_source_get_output_port_count
,
2297 (borrow_output_port_by_index_func_t
)
2298 bt_component_source_borrow_output_port_by_index_const
);
2304 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2306 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2307 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2308 (output_port_count_func_t
)
2309 bt_component_filter_get_output_port_count
,
2310 (borrow_output_port_by_index_func_t
)
2311 bt_component_filter_borrow_output_port_by_index_const
);
2322 int cmd_run(struct bt_config
*cfg
)
2325 struct cmd_run_ctx ctx
= { 0 };
2327 /* Initialize the command's context and the graph object */
2328 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2329 BT_CLI_LOGE_APPEND_CAUSE(
2330 "Cannot initialize the command's context.");
2335 BT_CLI_LOGW_APPEND_CAUSE(
2336 "Canceled by user before creating components.");
2340 BT_LOGI_STR("Creating components.");
2342 /* Create the requested component instances */
2343 if (cmd_run_ctx_create_components(&ctx
)) {
2344 BT_CLI_LOGE_APPEND_CAUSE("Cannot create components.");
2349 BT_CLI_LOGW_APPEND_CAUSE(
2350 "Canceled by user before connecting components.");
2354 BT_LOGI_STR("Connecting components.");
2356 /* Connect the initially visible component ports */
2357 if (cmd_run_ctx_connect_ports(&ctx
)) {
2358 BT_CLI_LOGE_APPEND_CAUSE(
2359 "Cannot connect initial component ports.");
2364 BT_CLI_LOGW_APPEND_CAUSE(
2365 "Canceled by user before running the graph.");
2369 BT_LOGI_STR("Running the graph.");
2373 bt_graph_run_status run_status
= bt_graph_run(ctx
.graph
);
2376 * Reset console in case something messed with console
2377 * codes during the graph's execution.
2379 printf("%s", bt_common_color_reset());
2381 fprintf(stderr
, "%s", bt_common_color_reset());
2382 BT_LOGT("bt_graph_run() returned: status=%s",
2383 bt_common_func_status_string(run_status
));
2385 switch (run_status
) {
2386 case BT_GRAPH_RUN_STATUS_OK
:
2388 case BT_GRAPH_RUN_STATUS_CANCELED
:
2389 BT_CLI_LOGW_APPEND_CAUSE("Graph was canceled by user.");
2391 case BT_GRAPH_RUN_STATUS_AGAIN
:
2392 if (bt_graph_is_canceled(ctx
.graph
)) {
2393 BT_CLI_LOGW_APPEND_CAUSE(
2394 "Graph was canceled by user.");
2398 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2399 BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: "
2401 cfg
->cmd_data
.run
.retry_duration_us
);
2403 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2404 if (bt_graph_is_canceled(ctx
.graph
)) {
2405 BT_CLI_LOGW_APPEND_CAUSE(
2406 "Graph was canceled by user.");
2412 case BT_GRAPH_RUN_STATUS_END
:
2415 BT_CLI_LOGE_APPEND_CAUSE(
2416 "Graph failed to complete successfully");
2429 cmd_run_ctx_destroy(&ctx
);
2434 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2436 const char *env_clash
;
2438 if (!cfg
->command_name
) {
2442 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2443 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2447 if (g_file_test(cfg
->command_name
,
2448 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2449 _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION
, __FILE__
, __LINE__
,
2450 BT_LOG_WARNING
, BT_LOG_TAG
,
2451 "The `%s` command was executed. "
2452 "If you meant to convert a trace located in "
2453 "the local `%s` directory, please use:\n\n"
2454 " babeltrace2 convert %s [OPTIONS]",
2455 cfg
->command_name
, cfg
->command_name
,
2461 void init_log_level(void)
2463 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2467 void set_auto_log_levels(struct bt_config
*cfg
)
2469 const char **env_var_name
;
2472 * Override the configuration's default log level if
2473 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2474 * are found for backward compatibility with legacy Babetrace 1.
2476 if (getenv("BABELTRACE_DEBUG") &&
2477 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2478 cfg
->log_level
= BT_LOG_TRACE
;
2479 } else if (getenv("BABELTRACE_VERBOSE") &&
2480 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2481 cfg
->log_level
= BT_LOG_INFO
;
2485 * Set log levels according to --debug or --verbose. For
2486 * backward compatibility, --debug is more verbose than
2489 * --verbose: INFO log level
2490 * --debug: TRACE log level (includes DEBUG, which is
2491 * is less verbose than TRACE in the internal
2492 * logging framework)
2494 if (!getenv("LIBBABELTRACE2_INIT_LOG_LEVEL")) {
2496 bt_logging_set_global_level(BT_LOG_INFO
);
2497 } else if (cfg
->debug
) {
2498 bt_logging_set_global_level(BT_LOG_TRACE
);
2501 * Set library's default log level if not
2502 * explicitly specified.
2504 bt_logging_set_global_level(cfg
->log_level
);
2508 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2510 bt_cli_log_level
= BT_LOG_INFO
;
2511 } else if (cfg
->debug
) {
2512 bt_cli_log_level
= BT_LOG_TRACE
;
2515 * Set CLI's default log level if not explicitly
2518 bt_cli_log_level
= cfg
->log_level
;
2522 env_var_name
= log_level_env_var_names
;
2524 while (*env_var_name
) {
2525 if (!getenv(*env_var_name
)) {
2527 g_setenv(*env_var_name
, "INFO", 1);
2528 } else if (cfg
->debug
) {
2529 g_setenv(*env_var_name
, "TRACE", 1);
2531 char val
[2] = { 0 };
2534 * Set module's default log level if not
2535 * explicitly specified.
2537 val
[0] = bt_log_get_letter_from_level(
2539 g_setenv(*env_var_name
, val
, 1);
2548 void print_error_causes(void)
2550 const bt_error
*error
= bt_current_thread_take_error();
2552 GString
*folded
= NULL
;
2553 unsigned int columns
;
2555 if (!error
|| bt_error_get_cause_count(error
) == 0) {
2556 fprintf(stderr
, "%s%sUnknown command-line error.%s\n",
2557 bt_common_color_bold(), bt_common_color_fg_red(),
2558 bt_common_color_reset());
2562 /* Try to get terminal width to fold the error cause messages */
2563 if (bt_common_get_term_size(&columns
, NULL
) < 0) {
2564 /* Width not found: default to 80 */
2569 * This helps visually separate the error causes from the last
2570 * logging statement.
2572 fprintf(stderr
, "\n");
2574 /* Reverse order: deepest (root) cause printed at the end */
2575 for (i
= bt_error_get_cause_count(error
) - 1; i
>= 0; i
--) {
2576 const bt_error_cause
*cause
=
2577 bt_error_borrow_cause_by_index(error
, (uint64_t) i
);
2578 const char *prefix_fmt
=
2579 i
== bt_error_get_cause_count(error
) - 1 ?
2580 "%s%sERROR%s: " : "%s%sCAUSED BY%s ";
2583 fprintf(stderr
, prefix_fmt
,
2584 bt_common_color_bold(), bt_common_color_fg_red(),
2585 bt_common_color_reset());
2587 /* Print actor name */
2588 fprintf(stderr
, "[");
2589 switch (bt_error_cause_get_actor_type(cause
)) {
2590 case BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN
:
2591 fprintf(stderr
, "%s%s%s",
2592 bt_common_color_bold(),
2593 bt_error_cause_get_module_name(cause
),
2594 bt_common_color_reset());
2596 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
:
2597 fprintf(stderr
, "%s%s%s: ",
2598 bt_common_color_bold(),
2599 bt_error_cause_component_actor_get_component_name(cause
),
2600 bt_common_color_reset());
2601 print_plugin_comp_cls_opt(stderr
,
2602 bt_error_cause_component_actor_get_plugin_name(cause
),
2603 bt_error_cause_component_actor_get_component_class_name(cause
),
2604 bt_error_cause_component_actor_get_component_class_type(cause
));
2606 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
:
2607 print_plugin_comp_cls_opt(stderr
,
2608 bt_error_cause_component_class_actor_get_plugin_name(cause
),
2609 bt_error_cause_component_class_actor_get_component_class_name(cause
),
2610 bt_error_cause_component_class_actor_get_component_class_type(cause
));
2612 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
:
2613 fprintf(stderr
, "%s%s%s (%s%s%s): ",
2614 bt_common_color_bold(),
2615 bt_error_cause_message_iterator_actor_get_component_name(cause
),
2616 bt_common_color_reset(),
2617 bt_common_color_bold(),
2618 bt_error_cause_message_iterator_actor_get_component_output_port_name(cause
),
2619 bt_common_color_reset());
2620 print_plugin_comp_cls_opt(stderr
,
2621 bt_error_cause_message_iterator_actor_get_plugin_name(cause
),
2622 bt_error_cause_message_iterator_actor_get_component_class_name(cause
),
2623 bt_error_cause_message_iterator_actor_get_component_class_type(cause
));
2629 /* Print file name and line number */
2630 fprintf(stderr
, "] (%s%s%s%s:%s%" PRIu64
"%s)\n",
2631 bt_common_color_bold(),
2632 bt_common_color_fg_magenta(),
2633 bt_error_cause_get_file_name(cause
),
2634 bt_common_color_reset(),
2635 bt_common_color_fg_green(),
2636 bt_error_cause_get_line_number(cause
),
2637 bt_common_color_reset());
2640 folded
= bt_common_fold(bt_error_cause_get_message(cause
),
2643 BT_LOGE_STR("Could not fold string.");
2644 fprintf(stderr
, "%s\n",
2645 bt_error_cause_get_message(cause
));
2649 fprintf(stderr
, "%s\n", folded
->str
);
2650 g_string_free(folded
, TRUE
);
2656 g_string_free(folded
, TRUE
);
2660 bt_error_release(error
);
2664 int main(int argc
, const char **argv
)
2668 struct bt_config
*cfg
;
2671 set_signal_handler();
2672 init_loaded_plugins();
2673 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2676 /* Quit without errors; typically usage/version */
2678 BT_LOGI_STR("Quitting without errors.");
2683 BT_CLI_LOGE_APPEND_CAUSE(
2684 "Command-line error: retcode=%d", retcode
);
2689 BT_CLI_LOGE_APPEND_CAUSE(
2690 "Failed to create a valid Babeltrace configuration.");
2695 set_auto_log_levels(cfg
);
2698 if (cfg
->command_needs_plugins
) {
2699 ret
= require_loaded_plugins(cfg
->plugin_paths
);
2701 BT_CLI_LOGE_APPEND_CAUSE(
2702 "Failed to load plugins: ret=%d", ret
);
2708 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2709 cfg
->command
, cfg
->command_name
);
2711 switch (cfg
->command
) {
2712 case BT_CONFIG_COMMAND_RUN
:
2715 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2716 ret
= cmd_list_plugins(cfg
);
2718 case BT_CONFIG_COMMAND_HELP
:
2719 ret
= cmd_help(cfg
);
2721 case BT_CONFIG_COMMAND_QUERY
:
2722 ret
= cmd_query(cfg
);
2724 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2725 ret
= cmd_print_ctf_metadata(cfg
);
2727 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2728 ret
= cmd_print_lttng_live_sessions(cfg
);
2731 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2735 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2736 cfg
->command
, cfg
->command_name
, ret
);
2737 warn_command_name_and_directory_clash(cfg
);
2738 retcode
= ret
? 1 : 0;
2741 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2742 fini_loaded_plugins();
2745 print_error_causes();
2749 * Clear current thread's error in case there is one to avoid a
2752 bt_current_thread_clear_error();