2 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
4 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "CLI"
28 #include <babeltrace2/babeltrace.h>
29 #include "common/common.h"
39 #include "babeltrace2-cfg.h"
40 #include "babeltrace2-cfg-cli-args.h"
41 #include "babeltrace2-cfg-cli-args-default.h"
42 #include "babeltrace2-plugins.h"
44 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
45 #define ENV_BABELTRACE_CLI_LOG_LEVEL "BABELTRACE_CLI_LOG_LEVEL"
46 #define NSEC_PER_SEC 1000000000LL
49 * Known environment variable names for the log levels of the project's
52 static const char* log_level_env_var_names
[] = {
53 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
54 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
58 /* Application's interrupter (owned by this) */
59 static bt_interrupter
*the_interrupter
;
66 BOOL WINAPI
signal_handler(DWORD signal
) {
67 if (the_interrupter
) {
68 bt_interrupter_set(the_interrupter
);
75 void set_signal_handler(void)
77 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
78 BT_LOGE("Failed to set the Ctrl+C handler.");
82 #else /* __MINGW32__ */
85 void signal_handler(int signum
)
87 if (signum
!= SIGINT
) {
91 if (the_interrupter
) {
92 bt_interrupter_set(the_interrupter
);
97 void set_signal_handler(void)
99 struct sigaction new_action
, old_action
;
101 new_action
.sa_handler
= signal_handler
;
102 sigemptyset(&new_action
.sa_mask
);
103 new_action
.sa_flags
= 0;
104 sigaction(SIGINT
, NULL
, &old_action
);
106 if (old_action
.sa_handler
!= SIG_IGN
) {
107 sigaction(SIGINT
, &new_action
, NULL
);
111 #endif /* __MINGW32__ */
114 int query(struct bt_config
*cfg
, const bt_component_class
*comp_cls
,
115 const char *obj
, const bt_value
*params
,
116 const bt_value
**user_result
, const char **fail_reason
)
118 const bt_value
*result
= NULL
;
119 bt_query_executor_query_status query_status
;
120 bt_query_executor
*query_exec
;
121 *fail_reason
= "unknown error";
124 BT_ASSERT(fail_reason
);
125 BT_ASSERT(user_result
);
126 query_exec
= bt_query_executor_create();
128 BT_CLI_LOGE_APPEND_CAUSE("Cannot create a query executor.");
132 bt_query_executor_add_interrupter(query_exec
, the_interrupter
);
135 query_status
= bt_query_executor_query(
136 query_exec
, comp_cls
, obj
, params
,
137 cfg
->log_level
, &result
);
138 switch (query_status
) {
139 case BT_QUERY_EXECUTOR_QUERY_STATUS_OK
:
141 case BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN
:
143 const uint64_t sleep_time_us
= 100000;
145 if (bt_interrupter_is_set(the_interrupter
)) {
146 *fail_reason
= "interrupted by user";
150 /* Wait 100 ms and retry */
151 BT_LOGD("Got BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN: sleeping: "
152 "time-us=%" PRIu64
, sleep_time_us
);
154 if (usleep(sleep_time_us
)) {
155 if (bt_interrupter_is_set(the_interrupter
)) {
156 BT_CLI_LOGW_APPEND_CAUSE(
157 "Query was interrupted by user: "
158 "comp-cls-addr=%p, comp-cls-name=\"%s\", "
159 "query-obj=\"%s\"", comp_cls
,
160 bt_component_class_get_name(comp_cls
),
162 *fail_reason
= "interrupted by user";
169 case BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR
:
170 if (bt_interrupter_is_set(the_interrupter
)) {
171 *fail_reason
= "interrupted by user";
176 case BT_QUERY_EXECUTOR_QUERY_STATUS_INVALID_OBJECT
:
177 *fail_reason
= "invalid or unknown query object";
179 case BT_QUERY_EXECUTOR_QUERY_STATUS_INVALID_PARAMS
:
180 *fail_reason
= "invalid query parameters";
182 case BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR
:
183 *fail_reason
= "not enough memory";
186 BT_LOGF("Unknown query status: status=%s",
187 bt_common_func_status_string(query_status
));
193 *user_result
= result
;
201 bt_query_executor_put_ref(query_exec
);
202 bt_value_put_ref(result
);
208 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
209 const bt_plugin
*, const char *);
212 const void *find_component_class_from_plugin(const char *plugin_name
,
213 const char *comp_class_name
,
214 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
216 const void *comp_class
= NULL
;
217 const bt_plugin
*plugin
;
219 BT_LOGI("Finding component class: plugin-name=\"%s\", "
220 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
222 plugin
= find_loaded_plugin(plugin_name
);
227 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
228 bt_object_get_ref(comp_class
);
229 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
233 BT_LOGI("Found component class: plugin-name=\"%s\", "
234 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
236 BT_LOGI("Cannot find source component class: "
237 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
238 plugin_name
, comp_class_name
);
245 const bt_component_class_source
*find_source_component_class(
246 const char *plugin_name
, const char *comp_class_name
)
248 return (const void *) find_component_class_from_plugin(
249 plugin_name
, comp_class_name
,
250 (plugin_borrow_comp_cls_func_t
)
251 bt_plugin_borrow_source_component_class_by_name_const
);
255 const bt_component_class_filter
*find_filter_component_class(
256 const char *plugin_name
, const char *comp_class_name
)
258 return (const void *) find_component_class_from_plugin(
259 plugin_name
, comp_class_name
,
260 (plugin_borrow_comp_cls_func_t
)
261 bt_plugin_borrow_filter_component_class_by_name_const
);
265 const bt_component_class_sink
*find_sink_component_class(
266 const char *plugin_name
, const char *comp_class_name
)
268 return (const void *) find_component_class_from_plugin(plugin_name
,
270 (plugin_borrow_comp_cls_func_t
)
271 bt_plugin_borrow_sink_component_class_by_name_const
);
275 const bt_component_class
*find_component_class(const char *plugin_name
,
276 const char *comp_class_name
,
277 bt_component_class_type comp_class_type
)
279 const bt_component_class
*comp_cls
= NULL
;
281 switch (comp_class_type
) {
282 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
283 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
285 case BT_COMPONENT_CLASS_TYPE_FILTER
:
286 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
288 case BT_COMPONENT_CLASS_TYPE_SINK
:
289 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
299 void print_indent(FILE *fp
, size_t indent
)
303 for (i
= 0; i
< indent
; i
++) {
309 const char *component_type_str(bt_component_class_type type
)
312 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
314 case BT_COMPONENT_CLASS_TYPE_SINK
:
316 case BT_COMPONENT_CLASS_TYPE_FILTER
:
324 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
325 const char *comp_cls_name
, bt_component_class_type type
)
327 GString
*shell_plugin_name
= NULL
;
328 GString
*shell_comp_cls_name
= NULL
;
331 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
332 if (!shell_plugin_name
) {
337 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
338 if (!shell_comp_cls_name
) {
342 fprintf(fh
, "'%s%s%s%s",
343 bt_common_color_bold(),
344 bt_common_color_fg_cyan(),
345 component_type_str(type
),
346 bt_common_color_fg_default());
348 if (shell_plugin_name
) {
349 fprintf(fh
, ".%s%s%s",
350 bt_common_color_fg_blue(),
351 shell_plugin_name
->str
,
352 bt_common_color_fg_default());
355 fprintf(fh
, ".%s%s%s'",
356 bt_common_color_fg_yellow(),
357 shell_comp_cls_name
->str
,
358 bt_common_color_reset());
361 if (shell_plugin_name
) {
362 g_string_free(shell_plugin_name
, TRUE
);
365 if (shell_comp_cls_name
) {
366 g_string_free(shell_comp_cls_name
, TRUE
);
371 void print_value(FILE *, const bt_value
*, size_t);
374 void print_value_rec(FILE *, const bt_value
*, size_t);
376 struct print_map_value_data
{
382 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
385 struct print_map_value_data
*print_map_value_data
= data
;
387 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
388 fprintf(print_map_value_data
->fp
, "%s: ", key
);
391 if (bt_value_is_array(object
) &&
392 bt_value_array_is_empty(object
)) {
393 fprintf(print_map_value_data
->fp
, "[ ]\n");
397 if (bt_value_is_map(object
) &&
398 bt_value_map_is_empty(object
)) {
399 fprintf(print_map_value_data
->fp
, "{ }\n");
403 if (bt_value_is_array(object
) ||
404 bt_value_is_map(object
)) {
405 fprintf(print_map_value_data
->fp
, "\n");
408 print_value_rec(print_map_value_data
->fp
, object
,
409 print_map_value_data
->indent
+ 2);
414 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
428 switch (bt_value_get_type(value
)) {
429 case BT_VALUE_TYPE_NULL
:
430 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
431 bt_common_color_reset());
433 case BT_VALUE_TYPE_BOOL
:
434 bool_val
= bt_value_bool_get(value
);
435 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
436 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
437 bt_common_color_reset());
439 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
440 uint_val
= bt_value_integer_unsigned_get(value
);
441 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
442 bt_common_color_fg_red(), uint_val
,
443 bt_common_color_reset());
445 case BT_VALUE_TYPE_SIGNED_INTEGER
:
446 int_val
= bt_value_integer_signed_get(value
);
447 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
448 bt_common_color_fg_red(), int_val
,
449 bt_common_color_reset());
451 case BT_VALUE_TYPE_REAL
:
452 dbl_val
= bt_value_real_get(value
);
453 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
454 bt_common_color_fg_red(), dbl_val
,
455 bt_common_color_reset());
457 case BT_VALUE_TYPE_STRING
:
458 str_val
= bt_value_string_get(value
);
459 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
460 bt_common_color_fg_green(), str_val
,
461 bt_common_color_reset());
463 case BT_VALUE_TYPE_ARRAY
:
464 size
= bt_value_array_get_size(value
);
470 print_indent(fp
, indent
);
471 fprintf(fp
, "[ ]\n");
475 for (i
= 0; i
< size
; i
++) {
476 const bt_value
*element
=
477 bt_value_array_borrow_element_by_index_const(
483 print_indent(fp
, indent
);
486 if (bt_value_is_array(element
) &&
487 bt_value_array_is_empty(element
)) {
488 fprintf(fp
, "[ ]\n");
492 if (bt_value_is_map(element
) &&
493 bt_value_map_is_empty(element
)) {
494 fprintf(fp
, "{ }\n");
498 if (bt_value_is_array(element
) ||
499 bt_value_is_map(element
)) {
503 print_value_rec(fp
, element
, indent
+ 2);
506 case BT_VALUE_TYPE_MAP
:
508 struct print_map_value_data data
= {
513 if (bt_value_map_is_empty(value
)) {
514 print_indent(fp
, indent
);
515 fprintf(fp
, "{ }\n");
519 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
528 BT_LOGE("Error printing value of type %s.",
529 bt_common_value_type_string(bt_value_get_type(value
)));
533 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
535 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
536 print_indent(fp
, indent
);
539 print_value_rec(fp
, value
, indent
);
543 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
545 fprintf(stderr
, " ");
546 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
547 bt_config_component
->comp_cls_name
->str
,
548 bt_config_component
->type
);
549 fprintf(stderr
, ":\n");
551 if (bt_config_component
->instance_name
->len
> 0) {
552 fprintf(stderr
, " Name: %s\n",
553 bt_config_component
->instance_name
->str
);
556 fprintf(stderr
, " Parameters:\n");
557 print_value(stderr
, bt_config_component
->params
, 8);
561 void print_bt_config_components(GPtrArray
*array
)
565 for (i
= 0; i
< array
->len
; i
++) {
566 struct bt_config_component
*cfg_component
=
567 bt_config_get_component(array
, i
);
568 print_bt_config_component(cfg_component
);
569 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
574 void print_plugin_paths(const bt_value
*plugin_paths
)
576 fprintf(stderr
, " Plugin paths:\n");
577 print_value(stderr
, plugin_paths
, 4);
581 void print_cfg_run(struct bt_config
*cfg
)
585 print_plugin_paths(cfg
->plugin_paths
);
586 fprintf(stderr
, " Source component instances:\n");
587 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
589 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
590 fprintf(stderr
, " Filter component instances:\n");
591 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
594 fprintf(stderr
, " Sink component instances:\n");
595 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
596 fprintf(stderr
, " Connections:\n");
598 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
599 struct bt_config_connection
*cfg_connection
=
600 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
603 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
604 cfg_connection
->upstream_comp_name
->str
,
605 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
606 cfg_connection
->upstream_port_glob
->str
,
607 cfg_connection
->downstream_comp_name
->str
,
608 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
609 cfg_connection
->downstream_port_glob
->str
);
614 void print_cfg_list_plugins(struct bt_config
*cfg
)
616 print_plugin_paths(cfg
->plugin_paths
);
620 void print_cfg_help(struct bt_config
*cfg
)
622 print_plugin_paths(cfg
->plugin_paths
);
626 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
628 print_plugin_paths(cfg
->plugin_paths
);
629 fprintf(stderr
, " Path: %s\n",
630 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
634 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
636 print_plugin_paths(cfg
->plugin_paths
);
637 fprintf(stderr
, " URL: %s\n",
638 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
642 void print_cfg_query(struct bt_config
*cfg
)
644 print_plugin_paths(cfg
->plugin_paths
);
645 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
646 fprintf(stderr
, " Component class:\n");
647 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
651 void print_cfg(struct bt_config
*cfg
)
653 if (!BT_LOG_ON_INFO
) {
657 BT_LOGI_STR("CLI configuration:");
658 BT_LOGI(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
659 BT_LOGI(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
661 switch (cfg
->command
) {
662 case BT_CONFIG_COMMAND_RUN
:
665 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
666 print_cfg_list_plugins(cfg
);
668 case BT_CONFIG_COMMAND_HELP
:
671 case BT_CONFIG_COMMAND_QUERY
:
672 print_cfg_query(cfg
);
674 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
675 print_cfg_print_ctf_metadata(cfg
);
677 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
678 print_cfg_print_lttng_live_sessions(cfg
);
686 void print_plugin_info(const bt_plugin
*plugin
)
688 unsigned int major
, minor
, patch
;
690 bt_property_availability version_avail
;
691 const char *plugin_name
;
695 const char *plugin_description
;
697 plugin_name
= bt_plugin_get_name(plugin
);
698 path
= bt_plugin_get_path(plugin
);
699 author
= bt_plugin_get_author(plugin
);
700 license
= bt_plugin_get_license(plugin
);
701 plugin_description
= bt_plugin_get_description(plugin
);
702 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
704 printf("%s%s%s%s:\n", bt_common_color_bold(),
705 bt_common_color_fg_blue(), plugin_name
,
706 bt_common_color_reset());
708 printf(" %sPath%s: %s\n", bt_common_color_bold(),
709 bt_common_color_reset(), path
);
714 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
715 printf(" %sVersion%s: %u.%u.%u",
716 bt_common_color_bold(), bt_common_color_reset(),
717 major
, minor
, patch
);
726 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
727 bt_common_color_reset(),
728 plugin_description
? plugin_description
: "(None)");
729 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
730 bt_common_color_reset(), author
? author
: "(Unknown)");
731 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
732 bt_common_color_reset(),
733 license
? license
: "(Unknown)");
737 int cmd_query(struct bt_config
*cfg
)
740 const bt_component_class
*comp_cls
= NULL
;
741 const bt_value
*results
= NULL
;
742 const char *fail_reason
= NULL
;
744 comp_cls
= find_component_class(
745 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
746 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
747 cfg
->cmd_data
.query
.cfg_component
->type
);
749 BT_CLI_LOGE_APPEND_CAUSE(
750 "Cannot find component class: plugin-name=\"%s\", "
751 "comp-cls-name=\"%s\", comp-cls-type=%d",
752 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
753 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
754 cfg
->cmd_data
.query
.cfg_component
->type
);
759 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
760 cfg
->cmd_data
.query
.cfg_component
->params
,
761 &results
, &fail_reason
);
766 print_value(stdout
, results
, 0);
770 BT_CLI_LOGE_APPEND_CAUSE(
771 "Failed to query component class: %s: plugin-name=\"%s\", "
772 "comp-cls-name=\"%s\", comp-cls-type=%d "
773 "object=\"%s\"", fail_reason
,
774 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
775 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
776 cfg
->cmd_data
.query
.cfg_component
->type
,
777 cfg
->cmd_data
.query
.object
->str
);
781 bt_component_class_put_ref(comp_cls
);
782 bt_value_put_ref(results
);
787 void print_component_class_help(const char *plugin_name
,
788 const bt_component_class
*comp_cls
)
790 const char *comp_class_name
=
791 bt_component_class_get_name(comp_cls
);
792 const char *comp_class_description
=
793 bt_component_class_get_description(comp_cls
);
794 const char *comp_class_help
=
795 bt_component_class_get_help(comp_cls
);
796 bt_component_class_type type
=
797 bt_component_class_get_type(comp_cls
);
799 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
801 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
802 bt_common_color_reset(),
803 comp_class_description
? comp_class_description
: "(None)");
805 if (comp_class_help
) {
806 printf("\n%s\n", comp_class_help
);
811 int cmd_help(struct bt_config
*cfg
)
814 const bt_plugin
*plugin
= NULL
;
815 const bt_component_class
*needed_comp_cls
= NULL
;
817 plugin
= find_loaded_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
819 BT_CLI_LOGE_APPEND_CAUSE(
820 "Cannot find plugin: plugin-name=\"%s\"",
821 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
826 print_plugin_info(plugin
);
827 printf(" %sSource component classes%s: %d\n",
828 bt_common_color_bold(),
829 bt_common_color_reset(),
830 (int) bt_plugin_get_source_component_class_count(plugin
));
831 printf(" %sFilter component classes%s: %d\n",
832 bt_common_color_bold(),
833 bt_common_color_reset(),
834 (int) bt_plugin_get_filter_component_class_count(plugin
));
835 printf(" %sSink component classes%s: %d\n",
836 bt_common_color_bold(),
837 bt_common_color_reset(),
838 (int) bt_plugin_get_sink_component_class_count(plugin
));
840 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
841 /* Plugin help only */
845 needed_comp_cls
= find_component_class(
846 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
847 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
848 cfg
->cmd_data
.help
.cfg_component
->type
);
849 if (!needed_comp_cls
) {
850 BT_CLI_LOGE_APPEND_CAUSE(
851 "Cannot find component class: plugin-name=\"%s\", "
852 "comp-cls-name=\"%s\", comp-cls-type=%d",
853 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
854 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
855 cfg
->cmd_data
.help
.cfg_component
->type
);
861 print_component_class_help(
862 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
866 bt_component_class_put_ref(needed_comp_cls
);
867 bt_plugin_put_ref(plugin
);
871 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
873 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
876 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
877 const char *cc_type_name
, uint64_t count
,
878 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
879 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
884 printf(" %s%s component classes%s: (none)\n",
885 bt_common_color_bold(),
887 bt_common_color_reset());
890 printf(" %s%s component classes%s:\n",
891 bt_common_color_bold(),
893 bt_common_color_reset());
896 for (i
= 0; i
< count
; i
++) {
897 const bt_component_class
*comp_class
=
898 spec_comp_cls_borrow_comp_cls_func(
899 borrow_comp_cls_by_index_func(plugin
, i
));
900 const char *comp_class_name
=
901 bt_component_class_get_name(comp_class
);
902 const char *comp_class_description
=
903 bt_component_class_get_description(comp_class
);
904 bt_component_class_type type
=
905 bt_component_class_get_type(comp_class
);
908 print_plugin_comp_cls_opt(stdout
,
909 bt_plugin_get_name(plugin
), comp_class_name
,
912 if (comp_class_description
) {
913 printf(": %s", comp_class_description
);
924 int cmd_list_plugins(struct bt_config
*cfg
)
927 int plugins_count
, component_classes_count
= 0, i
;
929 printf("From the following plugin paths:\n\n");
930 print_value(stdout
, cfg
->plugin_paths
, 2);
932 plugins_count
= get_loaded_plugins_count();
933 if (plugins_count
== 0) {
934 printf("No plugins found.\n");
938 for (i
= 0; i
< plugins_count
; i
++) {
939 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
941 component_classes_count
+=
942 bt_plugin_get_source_component_class_count(plugin
) +
943 bt_plugin_get_filter_component_class_count(plugin
) +
944 bt_plugin_get_sink_component_class_count(plugin
);
947 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
948 bt_common_color_bold(),
949 component_classes_count
,
950 bt_common_color_reset(),
951 bt_common_color_bold(),
953 bt_common_color_reset());
955 for (i
= 0; i
< plugins_count
; i
++) {
956 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
959 print_plugin_info(plugin
);
960 cmd_list_plugins_print_component_classes(plugin
, "Source",
961 bt_plugin_get_source_component_class_count(plugin
),
962 (plugin_borrow_comp_cls_by_index_func_t
)
963 bt_plugin_borrow_source_component_class_by_index_const
,
964 (spec_comp_cls_borrow_comp_cls_func_t
)
965 bt_component_class_source_as_component_class
);
966 cmd_list_plugins_print_component_classes(plugin
, "Filter",
967 bt_plugin_get_filter_component_class_count(plugin
),
968 (plugin_borrow_comp_cls_by_index_func_t
)
969 bt_plugin_borrow_filter_component_class_by_index_const
,
970 (spec_comp_cls_borrow_comp_cls_func_t
)
971 bt_component_class_filter_as_component_class
);
972 cmd_list_plugins_print_component_classes(plugin
, "Sink",
973 bt_plugin_get_sink_component_class_count(plugin
),
974 (plugin_borrow_comp_cls_by_index_func_t
)
975 bt_plugin_borrow_sink_component_class_by_index_const
,
976 (spec_comp_cls_borrow_comp_cls_func_t
)
977 bt_component_class_sink_as_component_class
);
985 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
988 const bt_component_class
*comp_cls
= NULL
;
989 const bt_value
*results
= NULL
;
990 bt_value
*params
= NULL
;
991 const bt_value
*map
= NULL
;
992 const bt_value
*v
= NULL
;
993 static const char * const plugin_name
= "ctf";
994 static const char * const comp_cls_name
= "lttng-live";
995 static const bt_component_class_type comp_cls_type
=
996 BT_COMPONENT_CLASS_TYPE_SOURCE
;
997 int64_t array_size
, i
;
998 const char *fail_reason
= NULL
;
999 FILE *out_stream
= stdout
;
1001 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1002 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1005 BT_CLI_LOGE_APPEND_CAUSE(
1006 "Cannot find component class: plugin-name=\"%s\", "
1007 "comp-cls-name=\"%s\", comp-cls-type=%d",
1008 plugin_name
, comp_cls_name
,
1009 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1013 params
= bt_value_map_create();
1018 ret
= bt_value_map_insert_string_entry(params
, "url",
1019 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1024 ret
= query(cfg
, comp_cls
, "sessions", params
,
1025 &results
, &fail_reason
);
1032 if (!bt_value_is_array(results
)) {
1033 BT_CLI_LOGE_APPEND_CAUSE(
1034 "Expecting an array for LTTng live `sessions` query.");
1038 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1040 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1044 BT_LOGE_ERRNO("Cannot open file for writing",
1046 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1047 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1049 "Cannot open file for writing: path=\"%s\"",
1050 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1055 array_size
= bt_value_array_get_size(results
);
1056 for (i
= 0; i
< array_size
; i
++) {
1057 const char *url_text
;
1058 int64_t timer_us
, streams
, clients
;
1060 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1062 BT_CLI_LOGE_APPEND_CAUSE("Unexpected empty array entry.");
1065 if (!bt_value_is_map(map
)) {
1066 BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type.");
1070 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1072 BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry.");
1075 url_text
= bt_value_string_get(v
);
1076 fprintf(out_stream
, "%s", url_text
);
1077 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1079 BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry.");
1082 timer_us
= bt_value_integer_signed_get(v
);
1083 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1084 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1086 BT_CLI_LOGE_APPEND_CAUSE(
1087 "Missing `stream-count` entry.");
1090 streams
= bt_value_integer_signed_get(v
);
1091 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1092 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1094 BT_CLI_LOGE_APPEND_CAUSE(
1095 "Missing `client-count` entry.");
1098 clients
= bt_value_integer_signed_get(v
);
1099 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1105 BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s",
1112 bt_value_put_ref(results
);
1113 bt_value_put_ref(params
);
1114 bt_component_class_put_ref(comp_cls
);
1116 if (out_stream
&& out_stream
!= stdout
) {
1117 int fclose_ret
= fclose(out_stream
);
1120 BT_LOGE_ERRNO("Cannot close file stream",
1122 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1130 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1133 const bt_component_class
*comp_cls
= NULL
;
1134 const bt_value
*results
= NULL
;
1135 bt_value
*params
= NULL
;
1136 const bt_value
*metadata_text_value
= NULL
;
1137 const char *metadata_text
= NULL
;
1138 static const char * const plugin_name
= "ctf";
1139 static const char * const comp_cls_name
= "fs";
1140 static const bt_component_class_type comp_cls_type
=
1141 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1142 const char *fail_reason
= NULL
;
1143 FILE *out_stream
= stdout
;
1145 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1146 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1149 BT_CLI_LOGE_APPEND_CAUSE(
1150 "Cannot find component class: plugin-name=\"%s\", "
1151 "comp-cls-name=\"%s\", comp-cls-type=%d",
1152 plugin_name
, comp_cls_name
,
1153 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1158 params
= bt_value_map_create();
1164 ret
= bt_value_map_insert_string_entry(params
, "path",
1165 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1171 ret
= query(cfg
, comp_cls
, "metadata-info",
1172 params
, &results
, &fail_reason
);
1177 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1179 if (!metadata_text_value
) {
1180 BT_CLI_LOGE_APPEND_CAUSE(
1181 "Cannot find `text` string value in the resulting metadata info object.");
1186 metadata_text
= bt_value_string_get(metadata_text_value
);
1188 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1190 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1193 BT_LOGE_ERRNO("Cannot open file for writing",
1195 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1196 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1198 "Cannot open file for writing: path=\"%s\"",
1199 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1205 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1207 BT_CLI_LOGE_APPEND_CAUSE(
1208 "Cannot write whole metadata text to output stream: "
1218 BT_CLI_LOGE_APPEND_CAUSE(
1219 "Failed to query `metadata-info` object: %s", fail_reason
);
1222 bt_value_put_ref(results
);
1223 bt_value_put_ref(params
);
1224 bt_component_class_put_ref(comp_cls
);
1226 if (out_stream
&& out_stream
!= stdout
) {
1227 int fclose_ret
= fclose(out_stream
);
1230 BT_LOGE_ERRNO("Cannot close file stream",
1232 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1240 char *instance_name
;
1244 struct trace_range
{
1245 uint64_t intersection_range_begin_ns
;
1246 uint64_t intersection_range_end_ns
;
1250 guint
port_id_hash(gconstpointer v
)
1252 const struct port_id
*id
= v
;
1254 BT_ASSERT(id
->instance_name
);
1255 BT_ASSERT(id
->port_name
);
1257 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1261 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1263 const struct port_id
*id1
= v1
;
1264 const struct port_id
*id2
= v2
;
1266 return strcmp(id1
->instance_name
, id2
->instance_name
) == 0 &&
1267 strcmp(id1
->port_name
, id2
->port_name
) == 0;
1271 void port_id_destroy(gpointer data
)
1273 struct port_id
*id
= data
;
1275 free(id
->instance_name
);
1276 free(id
->port_name
);
1281 void trace_range_destroy(gpointer data
)
1286 struct cmd_run_ctx
{
1288 GHashTable
*src_components
;
1291 GHashTable
*flt_components
;
1294 GHashTable
*sink_components
;
1300 struct bt_config
*cfg
;
1304 bool stream_intersection_mode
;
1307 * Association of struct port_id -> struct trace_range.
1309 GHashTable
*intersections
;
1312 /* Returns a timestamp of the form "(-)s.ns" */
1314 char *s_from_ns(int64_t ns
)
1319 int64_t ts_sec_abs
, ts_nsec_abs
;
1320 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1321 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1323 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1324 is_negative
= false;
1325 ts_sec_abs
= ts_sec
;
1326 ts_nsec_abs
= ts_nsec
;
1327 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1328 is_negative
= false;
1329 ts_sec_abs
= ts_sec
- 1;
1330 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1331 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1333 ts_sec_abs
= ts_sec
;
1334 ts_nsec_abs
= -ts_nsec
;
1335 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1337 ts_sec_abs
= -(ts_sec
+ 1);
1338 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1339 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1341 ts_sec_abs
= -ts_sec
;
1342 ts_nsec_abs
= ts_nsec
;
1343 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1345 ts_sec_abs
= -ts_sec
;
1346 ts_nsec_abs
= -ts_nsec
;
1349 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1350 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1358 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1359 struct cmd_run_ctx
*ctx
,
1360 const bt_component
*upstream_comp
,
1361 const bt_port_output
*out_upstream_port
,
1362 struct bt_config_connection
*cfg_conn
)
1364 typedef uint64_t (*input_port_count_func_t
)(void *);
1365 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1366 const void *, uint64_t);
1367 const bt_port
*upstream_port
=
1368 bt_port_output_as_port_const(out_upstream_port
);
1371 GQuark downstreamp_comp_name_quark
;
1372 void *downstream_comp
;
1373 uint64_t downstream_port_count
;
1375 input_port_count_func_t port_count_fn
;
1376 borrow_input_port_by_index_func_t port_by_index_fn
;
1377 bt_graph_connect_ports_status connect_ports_status
=
1378 BT_GRAPH_CONNECT_PORTS_STATUS_OK
;
1379 bool insert_trimmer
= false;
1380 bt_value
*trimmer_params
= NULL
;
1381 char *intersection_begin
= NULL
;
1382 char *intersection_end
= NULL
;
1383 const bt_component_filter
*trimmer
= NULL
;
1384 const bt_component_class_filter
*trimmer_class
= NULL
;
1385 const bt_port_input
*trimmer_input
= NULL
;
1386 const bt_port_output
*trimmer_output
= NULL
;
1388 if (ctx
->intersections
&&
1389 bt_component_get_class_type(upstream_comp
) ==
1390 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1391 struct trace_range
*range
;
1392 struct port_id port_id
= {
1393 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1394 .port_name
= (char *) bt_port_get_name(upstream_port
)
1397 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1401 range
= (struct trace_range
*) g_hash_table_lookup(
1402 ctx
->intersections
, &port_id
);
1404 bt_value_map_insert_entry_status insert_status
;
1406 intersection_begin
= s_from_ns(
1407 range
->intersection_range_begin_ns
);
1408 intersection_end
= s_from_ns(
1409 range
->intersection_range_end_ns
);
1410 if (!intersection_begin
|| !intersection_end
) {
1411 BT_CLI_LOGE_APPEND_CAUSE(
1412 "Cannot create trimmer argument timestamp string.");
1416 insert_trimmer
= true;
1417 trimmer_params
= bt_value_map_create();
1418 if (!trimmer_params
) {
1422 insert_status
= bt_value_map_insert_string_entry(
1423 trimmer_params
, "begin", intersection_begin
);
1424 if (insert_status
< 0) {
1427 insert_status
= bt_value_map_insert_string_entry(
1429 "end", intersection_end
);
1430 if (insert_status
< 0) {
1435 trimmer_class
= find_filter_component_class("utils", "trimmer");
1436 if (!trimmer_class
) {
1441 BT_LOGI("Connecting upstream port to the next available downstream port: "
1442 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1443 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1444 upstream_port
, bt_port_get_name(upstream_port
),
1445 cfg_conn
->downstream_comp_name
->str
,
1446 cfg_conn
->arg
->str
);
1447 downstreamp_comp_name_quark
= g_quark_from_string(
1448 cfg_conn
->downstream_comp_name
->str
);
1449 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1450 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1451 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1452 port_count_fn
= (input_port_count_func_t
)
1453 bt_component_filter_get_input_port_count
;
1454 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1455 bt_component_filter_borrow_input_port_by_index_const
;
1457 if (!downstream_comp
) {
1458 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1459 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1460 port_count_fn
= (input_port_count_func_t
)
1461 bt_component_sink_get_input_port_count
;
1462 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1463 bt_component_sink_borrow_input_port_by_index_const
;
1466 if (!downstream_comp
) {
1467 BT_CLI_LOGE_APPEND_CAUSE("Cannot find downstream component: "
1468 "comp-name=\"%s\", conn-arg=\"%s\"",
1469 cfg_conn
->downstream_comp_name
->str
,
1470 cfg_conn
->arg
->str
);
1474 downstream_port_count
= port_count_fn(downstream_comp
);
1476 for (i
= 0; i
< downstream_port_count
; i
++) {
1477 const bt_port_input
*in_downstream_port
=
1478 port_by_index_fn(downstream_comp
, i
);
1479 const bt_port
*downstream_port
=
1480 bt_port_input_as_port_const(in_downstream_port
);
1481 const char *upstream_port_name
;
1482 const char *downstream_port_name
;
1484 BT_ASSERT(downstream_port
);
1486 /* Skip port if it's already connected. */
1487 if (bt_port_is_connected(downstream_port
)) {
1488 BT_LOGI("Skipping downstream port: already connected: "
1489 "port-addr=%p, port-name=\"%s\"",
1491 bt_port_get_name(downstream_port
));
1495 downstream_port_name
= bt_port_get_name(downstream_port
);
1496 BT_ASSERT(downstream_port_name
);
1497 upstream_port_name
= bt_port_get_name(upstream_port
);
1498 BT_ASSERT(upstream_port_name
);
1500 if (!bt_common_star_glob_match(
1501 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1502 downstream_port_name
, SIZE_MAX
)) {
1506 if (insert_trimmer
) {
1508 * In order to insert the trimmer between the
1509 * two components that were being connected, we
1510 * create a connection configuration entry which
1511 * describes a connection from the trimmer's
1512 * output to the original input that was being
1515 * Hence, the creation of the trimmer will cause
1516 * the graph "new port" listener to establish
1517 * all downstream connections as its output port
1518 * is connected. We will then establish the
1519 * connection between the original upstream
1520 * source and the trimmer.
1522 char *trimmer_name
= NULL
;
1523 bt_graph_add_component_status add_comp_status
;
1525 ret
= asprintf(&trimmer_name
,
1526 "stream-intersection-trimmer-%s",
1527 upstream_port_name
);
1533 ctx
->connect_ports
= false;
1534 add_comp_status
= bt_graph_add_filter_component(
1535 ctx
->graph
, trimmer_class
, trimmer_name
,
1536 trimmer_params
, ctx
->cfg
->log_level
,
1539 if (add_comp_status
!=
1540 BT_GRAPH_ADD_COMPONENT_STATUS_OK
) {
1546 bt_component_filter_borrow_input_port_by_index_const(
1548 if (!trimmer_input
) {
1552 bt_component_filter_borrow_output_port_by_index_const(
1554 if (!trimmer_output
) {
1559 * Replace the current downstream port by the trimmer's
1562 in_downstream_port
= trimmer_input
;
1564 bt_port_input_as_port_const(in_downstream_port
);
1565 downstream_port_name
= bt_port_get_name(
1567 BT_ASSERT(downstream_port_name
);
1570 /* We have a winner! */
1571 connect_ports_status
= bt_graph_connect_ports(ctx
->graph
,
1572 out_upstream_port
, in_downstream_port
, NULL
);
1573 downstream_port
= NULL
;
1574 switch (connect_ports_status
) {
1575 case BT_GRAPH_CONNECT_PORTS_STATUS_OK
:
1578 BT_CLI_LOGE_APPEND_CAUSE(
1579 "Cannot create connection: graph refuses to connect ports: "
1580 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1581 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1582 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1583 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1585 upstream_comp
, bt_component_get_name(upstream_comp
),
1586 upstream_port
, bt_port_get_name(upstream_port
),
1587 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1588 downstream_port
, downstream_port_name
,
1589 cfg_conn
->arg
->str
);
1593 BT_LOGI("Connected component ports: "
1594 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1595 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1596 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1597 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1599 upstream_comp
, bt_component_get_name(upstream_comp
),
1600 upstream_port
, bt_port_get_name(upstream_port
),
1601 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1602 downstream_port
, downstream_port_name
,
1603 cfg_conn
->arg
->str
);
1605 if (insert_trimmer
) {
1607 * The first connection, from the source to the trimmer,
1608 * has been done. We now connect the trimmer to the
1609 * original downstream port.
1611 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1613 bt_component_filter_as_component_const(trimmer
),
1614 trimmer_output
, cfg_conn
);
1618 ctx
->connect_ports
= true;
1622 * We found a matching downstream port: the search is
1628 /* No downstream port found */
1629 BT_CLI_LOGE_APPEND_CAUSE(
1630 "Cannot create connection: cannot find a matching downstream port for upstream port: "
1631 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1632 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1633 upstream_port
, bt_port_get_name(upstream_port
),
1634 cfg_conn
->downstream_comp_name
->str
,
1635 cfg_conn
->arg
->str
);
1641 free(intersection_begin
);
1642 free(intersection_end
);
1643 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1644 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1645 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1650 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1651 const bt_port_output
*upstream_port
)
1654 const char *upstream_port_name
;
1655 const char *upstream_comp_name
;
1656 const bt_component
*upstream_comp
= NULL
;
1660 BT_ASSERT(upstream_port
);
1661 upstream_port_name
= bt_port_get_name(
1662 bt_port_output_as_port_const(upstream_port
));
1663 BT_ASSERT(upstream_port_name
);
1664 upstream_comp
= bt_port_borrow_component_const(
1665 bt_port_output_as_port_const(upstream_port
));
1666 BT_ASSERT(upstream_comp
);
1667 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1668 BT_ASSERT(upstream_comp_name
);
1669 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1670 "port-addr=%p, port-name=\"%s\"",
1671 upstream_comp
, upstream_comp_name
,
1672 upstream_port
, upstream_port_name
);
1674 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1675 struct bt_config_connection
*cfg_conn
=
1677 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1679 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1680 upstream_comp_name
)) {
1684 if (!bt_common_star_glob_match(
1685 cfg_conn
->upstream_port_glob
->str
,
1686 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1690 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1691 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1693 BT_CLI_LOGE_APPEND_CAUSE(
1694 "Cannot connect upstream port: "
1695 "port-addr=%p, port-name=\"%s\"",
1697 upstream_port_name
);
1703 BT_CLI_LOGE_APPEND_CAUSE(
1704 "Cannot connect upstream port: port does not match any connection argument: "
1705 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1706 upstream_port_name
);
1716 bt_graph_listener_func_status
1717 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1718 const bt_port_output
*out_port
)
1720 const bt_component
*comp
;
1721 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1722 bt_graph_listener_func_status ret
=
1723 BT_GRAPH_LISTENER_FUNC_STATUS_OK
;
1725 comp
= bt_port_borrow_component_const(port
);
1726 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1727 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1728 comp
, comp
? bt_component_get_name(comp
) : "",
1729 port
, bt_port_get_name(port
));
1731 if (!ctx
->connect_ports
) {
1737 if (bt_port_is_connected(port
)) {
1738 BT_LOGW_STR("Port is already connected.");
1742 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
1743 BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port.");
1744 ret
= BT_GRAPH_LISTENER_FUNC_STATUS_ERROR
;
1753 bt_graph_listener_func_status
graph_source_output_port_added_listener(
1754 const bt_component_source
*component
,
1755 const bt_port_output
*port
, void *data
)
1757 return graph_output_port_added_listener(data
, port
);
1761 bt_graph_listener_func_status
graph_filter_output_port_added_listener(
1762 const bt_component_filter
*component
,
1763 const bt_port_output
*port
, void *data
)
1765 return graph_output_port_added_listener(data
, port
);
1769 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1775 if (ctx
->src_components
) {
1776 g_hash_table_destroy(ctx
->src_components
);
1777 ctx
->src_components
= NULL
;
1780 if (ctx
->flt_components
) {
1781 g_hash_table_destroy(ctx
->flt_components
);
1782 ctx
->flt_components
= NULL
;
1785 if (ctx
->sink_components
) {
1786 g_hash_table_destroy(ctx
->sink_components
);
1787 ctx
->sink_components
= NULL
;
1790 if (ctx
->intersections
) {
1791 g_hash_table_destroy(ctx
->intersections
);
1792 ctx
->intersections
= NULL
;
1795 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
1800 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1803 bt_graph_add_listener_status add_listener_status
;
1806 ctx
->connect_ports
= false;
1807 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
1808 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1809 if (!ctx
->src_components
) {
1813 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
1814 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1815 if (!ctx
->flt_components
) {
1819 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
1820 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1821 if (!ctx
->sink_components
) {
1825 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1826 ctx
->stream_intersection_mode
= true;
1827 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1828 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1829 if (!ctx
->intersections
) {
1834 ctx
->graph
= bt_graph_create();
1839 bt_graph_add_interrupter(ctx
->graph
, the_interrupter
);
1840 add_listener_status
= bt_graph_add_source_component_output_port_added_listener(
1841 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
1843 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1844 BT_CLI_LOGE_APPEND_CAUSE(
1845 "Cannot add \"port added\" listener to graph.");
1849 add_listener_status
= bt_graph_add_filter_component_output_port_added_listener(
1850 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
1852 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1853 BT_CLI_LOGE_APPEND_CAUSE(
1854 "Cannot add \"port added\" listener to graph.");
1861 cmd_run_ctx_destroy(ctx
);
1869 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
1870 struct bt_config_component
*cfg_comp
,
1871 const bt_component_class_source
*src_comp_cls
)
1875 int64_t trace_count
;
1876 const char *path
= NULL
;
1877 const bt_value
*query_result
= NULL
;
1878 const bt_value
*trace_info
= NULL
;
1879 const bt_value
*intersection_range
= NULL
;
1880 const bt_value
*intersection_begin
= NULL
;
1881 const bt_value
*intersection_end
= NULL
;
1882 const bt_value
*stream_infos
= NULL
;
1883 const bt_value
*stream_info
= NULL
;
1884 struct port_id
*port_id
= NULL
;
1885 struct trace_range
*trace_range
= NULL
;
1886 const char *fail_reason
= NULL
;
1887 const bt_component_class
*comp_cls
=
1888 bt_component_class_source_as_component_class_const(src_comp_cls
);
1890 ret
= query(ctx
->cfg
, comp_cls
, "trace-info",
1891 cfg_comp
->params
, &query_result
,
1894 BT_LOGD("Component class does not support the `trace-info` query: %s: "
1895 "comp-class-name=\"%s\"", fail_reason
,
1896 bt_component_class_get_name(comp_cls
));
1901 BT_ASSERT(query_result
);
1903 if (!bt_value_is_array(query_result
)) {
1904 BT_LOGD("Unexpected format of \'trace-info\' query result: "
1905 "component-class-name=%s",
1906 bt_component_class_get_name(comp_cls
));
1911 trace_count
= bt_value_array_get_size(query_result
);
1912 if (trace_count
< 0) {
1917 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
1919 uint64_t stream_idx
;
1920 int64_t stream_count
;
1922 trace_info
= bt_value_array_borrow_element_by_index_const(
1923 query_result
, trace_idx
);
1924 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
1926 BT_LOGD_STR("Cannot retrieve trace from query result.");
1930 intersection_range
= bt_value_map_borrow_entry_value_const(
1931 trace_info
, "intersection-range-ns");
1932 if (!intersection_range
) {
1934 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
1938 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
1940 if (!intersection_begin
) {
1942 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
1946 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
1948 if (!intersection_end
) {
1950 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
1954 begin
= bt_value_integer_signed_get(intersection_begin
);
1955 end
= bt_value_integer_signed_get(intersection_end
);
1957 if (begin
< 0 || end
< 0 || end
< begin
) {
1958 BT_CLI_LOGE_APPEND_CAUSE(
1959 "Invalid trace stream intersection values: "
1960 "intersection-range-ns:begin=%" PRId64
1961 ", intersection-range-ns:end=%" PRId64
,
1967 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
1969 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
1971 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
1975 stream_count
= bt_value_array_get_size(stream_infos
);
1976 if (stream_count
< 0) {
1981 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
1982 const bt_value
*port_name
;
1984 port_id
= g_new0(struct port_id
, 1);
1987 BT_CLI_LOGE_APPEND_CAUSE(
1988 "Cannot allocate memory for port_id structure.");
1991 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
1992 if (!port_id
->instance_name
) {
1994 BT_CLI_LOGE_APPEND_CAUSE(
1995 "Cannot allocate memory for port_id component instance name.");
1999 trace_range
= g_new0(struct trace_range
, 1);
2002 BT_CLI_LOGE_APPEND_CAUSE(
2003 "Cannot allocate memory for trace_range structure.");
2006 trace_range
->intersection_range_begin_ns
= begin
;
2007 trace_range
->intersection_range_end_ns
= end
;
2009 stream_info
= bt_value_array_borrow_element_by_index_const(
2010 stream_infos
, stream_idx
);
2011 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2013 BT_CLI_LOGE_APPEND_CAUSE(
2014 "Cannot retrieve stream informations from trace in query result.");
2018 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2019 if (!port_name
|| !bt_value_is_string(port_name
)) {
2021 BT_CLI_LOGE_APPEND_CAUSE(
2022 "Cannot retrieve port name in query result.");
2026 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2027 if (!port_id
->port_name
) {
2029 BT_CLI_LOGE_APPEND_CAUSE(
2030 "Cannot allocate memory for port_id port_name.");
2034 BT_LOGD("Inserting stream intersection ");
2036 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2046 BT_CLI_LOGE_APPEND_CAUSE(
2047 "Cannot determine stream intersection of trace: path=\"%s\"",
2048 path
? path
: "(unknown)");
2051 bt_value_put_ref(query_result
);
2053 g_free(trace_range
);
2058 int cmd_run_ctx_create_components_from_config_components(
2059 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2062 const void *comp_cls
= NULL
;
2063 const void *comp
= NULL
;
2066 for (i
= 0; i
< cfg_components
->len
; i
++) {
2067 struct bt_config_component
*cfg_comp
=
2068 g_ptr_array_index(cfg_components
, i
);
2071 switch (cfg_comp
->type
) {
2072 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2073 comp_cls
= find_source_component_class(
2074 cfg_comp
->plugin_name
->str
,
2075 cfg_comp
->comp_cls_name
->str
);
2077 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2078 comp_cls
= find_filter_component_class(
2079 cfg_comp
->plugin_name
->str
,
2080 cfg_comp
->comp_cls_name
->str
);
2082 case BT_COMPONENT_CLASS_TYPE_SINK
:
2083 comp_cls
= find_sink_component_class(
2084 cfg_comp
->plugin_name
->str
,
2085 cfg_comp
->comp_cls_name
->str
);
2092 BT_CLI_LOGE_APPEND_CAUSE(
2093 "Cannot find component class: plugin-name=\"%s\", "
2094 "comp-cls-name=\"%s\", comp-cls-type=%d",
2095 cfg_comp
->plugin_name
->str
,
2096 cfg_comp
->comp_cls_name
->str
,
2101 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_TRACE
);
2103 switch (cfg_comp
->type
) {
2104 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2105 ret
= bt_graph_add_source_component(ctx
->graph
,
2106 comp_cls
, cfg_comp
->instance_name
->str
,
2107 cfg_comp
->params
, cfg_comp
->log_level
,
2110 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2111 ret
= bt_graph_add_filter_component(ctx
->graph
,
2112 comp_cls
, cfg_comp
->instance_name
->str
,
2113 cfg_comp
->params
, cfg_comp
->log_level
,
2116 case BT_COMPONENT_CLASS_TYPE_SINK
:
2117 ret
= bt_graph_add_sink_component(ctx
->graph
,
2118 comp_cls
, cfg_comp
->instance_name
->str
,
2119 cfg_comp
->params
, cfg_comp
->log_level
,
2127 BT_CLI_LOGE_APPEND_CAUSE(
2128 "Cannot create component: plugin-name=\"%s\", "
2129 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2131 cfg_comp
->plugin_name
->str
,
2132 cfg_comp
->comp_cls_name
->str
,
2133 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2137 if (ctx
->stream_intersection_mode
&&
2138 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2139 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2145 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2146 comp
, cfg_comp
->instance_name
->str
);
2147 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2148 BT_ASSERT(quark
> 0);
2150 switch (cfg_comp
->type
) {
2151 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2152 g_hash_table_insert(ctx
->src_components
,
2153 GUINT_TO_POINTER(quark
), (void *) comp
);
2155 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2156 g_hash_table_insert(ctx
->flt_components
,
2157 GUINT_TO_POINTER(quark
), (void *) comp
);
2159 case BT_COMPONENT_CLASS_TYPE_SINK
:
2160 g_hash_table_insert(ctx
->sink_components
,
2161 GUINT_TO_POINTER(quark
), (void *) comp
);
2168 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2177 bt_object_put_ref(comp
);
2178 bt_object_put_ref(comp_cls
);
2183 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2188 * Make sure that, during this phase, our graph's "port added"
2189 * listener does not connect ports while we are creating the
2190 * components because we have a special, initial phase for
2193 ctx
->connect_ports
= false;
2195 ret
= cmd_run_ctx_create_components_from_config_components(
2196 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2202 ret
= cmd_run_ctx_create_components_from_config_components(
2203 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2209 ret
= cmd_run_ctx_create_components_from_config_components(
2210 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2220 typedef uint64_t (*output_port_count_func_t
)(const void *);
2221 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2222 const void *, uint64_t);
2225 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2226 void *comp
, output_port_count_func_t port_count_fn
,
2227 borrow_output_port_by_index_func_t port_by_index_fn
)
2233 count
= port_count_fn(comp
);
2235 for (i
= 0; i
< count
; i
++) {
2236 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2238 BT_ASSERT(upstream_port
);
2239 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2250 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2253 GHashTableIter iter
;
2254 gpointer g_name_quark
, g_comp
;
2256 ctx
->connect_ports
= true;
2257 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2259 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2260 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2261 (output_port_count_func_t
)
2262 bt_component_source_get_output_port_count
,
2263 (borrow_output_port_by_index_func_t
)
2264 bt_component_source_borrow_output_port_by_index_const
);
2270 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2272 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2273 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2274 (output_port_count_func_t
)
2275 bt_component_filter_get_output_port_count
,
2276 (borrow_output_port_by_index_func_t
)
2277 bt_component_filter_borrow_output_port_by_index_const
);
2288 int cmd_run(struct bt_config
*cfg
)
2291 struct cmd_run_ctx ctx
= { 0 };
2293 /* Initialize the command's context and the graph object */
2294 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2295 BT_CLI_LOGE_APPEND_CAUSE(
2296 "Cannot initialize the command's context.");
2300 if (bt_interrupter_is_set(the_interrupter
)) {
2301 BT_CLI_LOGW_APPEND_CAUSE(
2302 "Interrupted by user before creating components.");
2306 BT_LOGI_STR("Creating components.");
2308 /* Create the requested component instances */
2309 if (cmd_run_ctx_create_components(&ctx
)) {
2310 BT_CLI_LOGE_APPEND_CAUSE("Cannot create components.");
2314 if (bt_interrupter_is_set(the_interrupter
)) {
2315 BT_CLI_LOGW_APPEND_CAUSE(
2316 "Interrupted by user before connecting components.");
2320 BT_LOGI_STR("Connecting components.");
2322 /* Connect the initially visible component ports */
2323 if (cmd_run_ctx_connect_ports(&ctx
)) {
2324 BT_CLI_LOGE_APPEND_CAUSE(
2325 "Cannot connect initial component ports.");
2329 BT_LOGI_STR("Running the graph.");
2333 bt_graph_run_status run_status
= bt_graph_run(ctx
.graph
);
2336 * Reset console in case something messed with console
2337 * codes during the graph's execution.
2339 printf("%s", bt_common_color_reset());
2341 fprintf(stderr
, "%s", bt_common_color_reset());
2342 BT_LOGT("bt_graph_run() returned: status=%s",
2343 bt_common_func_status_string(run_status
));
2345 switch (run_status
) {
2346 case BT_GRAPH_RUN_STATUS_OK
:
2348 case BT_GRAPH_RUN_STATUS_AGAIN
:
2349 if (bt_interrupter_is_set(the_interrupter
)) {
2350 BT_CLI_LOGW_APPEND_CAUSE(
2351 "Graph was interrupted by user.");
2355 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2356 BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: "
2358 cfg
->cmd_data
.run
.retry_duration_us
);
2360 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2361 if (bt_interrupter_is_set(the_interrupter
)) {
2362 BT_CLI_LOGW_APPEND_CAUSE(
2363 "Graph was interrupted by user.");
2369 case BT_GRAPH_RUN_STATUS_END
:
2372 if (bt_interrupter_is_set(the_interrupter
)) {
2373 BT_CLI_LOGW_APPEND_CAUSE(
2374 "Graph was interrupted by user and failed: "
2376 bt_common_func_status_string(run_status
));
2380 BT_CLI_LOGE_APPEND_CAUSE(
2381 "Graph failed to complete successfully");
2394 cmd_run_ctx_destroy(&ctx
);
2399 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2401 const char *env_clash
;
2403 if (!cfg
->command_name
) {
2407 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2408 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2412 if (g_file_test(cfg
->command_name
,
2413 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2414 _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION
, __FILE__
, __LINE__
,
2415 BT_LOG_WARNING
, BT_LOG_TAG
,
2416 "The `%s` command was executed. "
2417 "If you meant to convert a trace located in "
2418 "the local `%s` directory, please use:\n\n"
2419 " babeltrace2 convert %s [OPTIONS]",
2420 cfg
->command_name
, cfg
->command_name
,
2426 void init_log_level(void)
2428 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2432 void set_auto_log_levels(struct bt_config
*cfg
)
2434 const char **env_var_name
;
2437 * Override the configuration's default log level if
2438 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2439 * are found for backward compatibility with legacy Babetrace 1.
2441 if (getenv("BABELTRACE_DEBUG") &&
2442 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2443 cfg
->log_level
= BT_LOG_TRACE
;
2444 } else if (getenv("BABELTRACE_VERBOSE") &&
2445 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2446 cfg
->log_level
= BT_LOG_INFO
;
2450 * Set log levels according to --debug or --verbose. For
2451 * backward compatibility, --debug is more verbose than
2454 * --verbose: INFO log level
2455 * --debug: TRACE log level (includes DEBUG, which is
2456 * is less verbose than TRACE in the internal
2457 * logging framework)
2459 if (!getenv("LIBBABELTRACE2_INIT_LOG_LEVEL")) {
2461 bt_logging_set_global_level(BT_LOG_INFO
);
2462 } else if (cfg
->debug
) {
2463 bt_logging_set_global_level(BT_LOG_TRACE
);
2466 * Set library's default log level if not
2467 * explicitly specified.
2469 bt_logging_set_global_level(cfg
->log_level
);
2473 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2475 bt_cli_log_level
= BT_LOG_INFO
;
2476 } else if (cfg
->debug
) {
2477 bt_cli_log_level
= BT_LOG_TRACE
;
2480 * Set CLI's default log level if not explicitly
2483 bt_cli_log_level
= cfg
->log_level
;
2487 env_var_name
= log_level_env_var_names
;
2489 while (*env_var_name
) {
2490 if (!getenv(*env_var_name
)) {
2492 g_setenv(*env_var_name
, "INFO", 1);
2493 } else if (cfg
->debug
) {
2494 g_setenv(*env_var_name
, "TRACE", 1);
2496 char val
[2] = { 0 };
2499 * Set module's default log level if not
2500 * explicitly specified.
2502 val
[0] = bt_log_get_letter_from_level(
2504 g_setenv(*env_var_name
, val
, 1);
2513 void print_error_causes(void)
2515 const bt_error
*error
= bt_current_thread_take_error();
2517 GString
*folded
= NULL
;
2518 unsigned int columns
;
2520 if (!error
|| bt_error_get_cause_count(error
) == 0) {
2521 fprintf(stderr
, "%s%sUnknown command-line error.%s\n",
2522 bt_common_color_bold(), bt_common_color_fg_red(),
2523 bt_common_color_reset());
2527 /* Try to get terminal width to fold the error cause messages */
2528 if (bt_common_get_term_size(&columns
, NULL
) < 0) {
2529 /* Width not found: default to 80 */
2534 * This helps visually separate the error causes from the last
2535 * logging statement.
2537 fprintf(stderr
, "\n");
2539 /* Reverse order: deepest (root) cause printed at the end */
2540 for (i
= bt_error_get_cause_count(error
) - 1; i
>= 0; i
--) {
2541 const bt_error_cause
*cause
=
2542 bt_error_borrow_cause_by_index(error
, (uint64_t) i
);
2543 const char *prefix_fmt
=
2544 i
== bt_error_get_cause_count(error
) - 1 ?
2545 "%s%sERROR%s: " : "%s%sCAUSED BY%s ";
2548 fprintf(stderr
, prefix_fmt
,
2549 bt_common_color_bold(), bt_common_color_fg_red(),
2550 bt_common_color_reset());
2552 /* Print actor name */
2553 fprintf(stderr
, "[");
2554 switch (bt_error_cause_get_actor_type(cause
)) {
2555 case BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN
:
2556 fprintf(stderr
, "%s%s%s",
2557 bt_common_color_bold(),
2558 bt_error_cause_get_module_name(cause
),
2559 bt_common_color_reset());
2561 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
:
2562 fprintf(stderr
, "%s%s%s: ",
2563 bt_common_color_bold(),
2564 bt_error_cause_component_actor_get_component_name(cause
),
2565 bt_common_color_reset());
2566 print_plugin_comp_cls_opt(stderr
,
2567 bt_error_cause_component_actor_get_plugin_name(cause
),
2568 bt_error_cause_component_actor_get_component_class_name(cause
),
2569 bt_error_cause_component_actor_get_component_class_type(cause
));
2571 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
:
2572 print_plugin_comp_cls_opt(stderr
,
2573 bt_error_cause_component_class_actor_get_plugin_name(cause
),
2574 bt_error_cause_component_class_actor_get_component_class_name(cause
),
2575 bt_error_cause_component_class_actor_get_component_class_type(cause
));
2577 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
:
2578 fprintf(stderr
, "%s%s%s (%s%s%s): ",
2579 bt_common_color_bold(),
2580 bt_error_cause_message_iterator_actor_get_component_name(cause
),
2581 bt_common_color_reset(),
2582 bt_common_color_bold(),
2583 bt_error_cause_message_iterator_actor_get_component_output_port_name(cause
),
2584 bt_common_color_reset());
2585 print_plugin_comp_cls_opt(stderr
,
2586 bt_error_cause_message_iterator_actor_get_plugin_name(cause
),
2587 bt_error_cause_message_iterator_actor_get_component_class_name(cause
),
2588 bt_error_cause_message_iterator_actor_get_component_class_type(cause
));
2594 /* Print file name and line number */
2595 fprintf(stderr
, "] (%s%s%s%s:%s%" PRIu64
"%s)\n",
2596 bt_common_color_bold(),
2597 bt_common_color_fg_magenta(),
2598 bt_error_cause_get_file_name(cause
),
2599 bt_common_color_reset(),
2600 bt_common_color_fg_green(),
2601 bt_error_cause_get_line_number(cause
),
2602 bt_common_color_reset());
2605 folded
= bt_common_fold(bt_error_cause_get_message(cause
),
2608 BT_LOGE_STR("Could not fold string.");
2609 fprintf(stderr
, "%s\n",
2610 bt_error_cause_get_message(cause
));
2614 fprintf(stderr
, "%s\n", folded
->str
);
2615 g_string_free(folded
, TRUE
);
2621 g_string_free(folded
, TRUE
);
2625 bt_error_release(error
);
2629 int main(int argc
, const char **argv
)
2633 struct bt_config
*cfg
;
2636 set_signal_handler();
2637 init_loaded_plugins();
2638 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2641 /* Quit without errors; typically usage/version */
2643 BT_LOGI_STR("Quitting without errors.");
2648 BT_CLI_LOGE_APPEND_CAUSE(
2649 "Command-line error: retcode=%d", retcode
);
2654 BT_CLI_LOGE_APPEND_CAUSE(
2655 "Failed to create a valid Babeltrace configuration.");
2660 set_auto_log_levels(cfg
);
2663 if (cfg
->command_needs_plugins
) {
2664 ret
= require_loaded_plugins(cfg
->plugin_paths
);
2666 BT_CLI_LOGE_APPEND_CAUSE(
2667 "Failed to load plugins: ret=%d", ret
);
2673 BT_ASSERT(!the_interrupter
);
2674 the_interrupter
= bt_interrupter_create();
2675 if (!the_interrupter
) {
2676 BT_CLI_LOGE_APPEND_CAUSE("Failed to create an interrupter object.");
2681 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2682 cfg
->command
, cfg
->command_name
);
2684 switch (cfg
->command
) {
2685 case BT_CONFIG_COMMAND_RUN
:
2688 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2689 ret
= cmd_list_plugins(cfg
);
2691 case BT_CONFIG_COMMAND_HELP
:
2692 ret
= cmd_help(cfg
);
2694 case BT_CONFIG_COMMAND_QUERY
:
2695 ret
= cmd_query(cfg
);
2697 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2698 ret
= cmd_print_ctf_metadata(cfg
);
2700 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2701 ret
= cmd_print_lttng_live_sessions(cfg
);
2704 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2708 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2709 cfg
->command
, cfg
->command_name
, ret
);
2710 warn_command_name_and_directory_clash(cfg
);
2711 retcode
= ret
? 1 : 0;
2714 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2715 fini_loaded_plugins();
2716 bt_interrupter_put_ref(the_interrupter
);
2719 print_error_causes();
2723 * Clear current thread's error in case there is one to avoid a
2726 bt_current_thread_clear_error();