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_UNKNOWN_OBJECT
:
177 *fail_reason
= "unknown query object";
179 case BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR
:
180 *fail_reason
= "not enough memory";
183 BT_LOGF("Unknown query status: status=%s",
184 bt_common_func_status_string(query_status
));
190 *user_result
= result
;
198 bt_query_executor_put_ref(query_exec
);
199 bt_value_put_ref(result
);
205 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
206 const bt_plugin
*, const char *);
209 const void *find_component_class_from_plugin(const char *plugin_name
,
210 const char *comp_class_name
,
211 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
213 const void *comp_class
= NULL
;
214 const bt_plugin
*plugin
;
216 BT_LOGI("Finding component class: plugin-name=\"%s\", "
217 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
219 plugin
= find_loaded_plugin(plugin_name
);
224 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
225 bt_object_get_ref(comp_class
);
226 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
230 BT_LOGI("Found component class: plugin-name=\"%s\", "
231 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
233 BT_LOGI("Cannot find source component class: "
234 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
235 plugin_name
, comp_class_name
);
242 const bt_component_class_source
*find_source_component_class(
243 const char *plugin_name
, const char *comp_class_name
)
245 return (const void *) find_component_class_from_plugin(
246 plugin_name
, comp_class_name
,
247 (plugin_borrow_comp_cls_func_t
)
248 bt_plugin_borrow_source_component_class_by_name_const
);
252 const bt_component_class_filter
*find_filter_component_class(
253 const char *plugin_name
, const char *comp_class_name
)
255 return (const void *) find_component_class_from_plugin(
256 plugin_name
, comp_class_name
,
257 (plugin_borrow_comp_cls_func_t
)
258 bt_plugin_borrow_filter_component_class_by_name_const
);
262 const bt_component_class_sink
*find_sink_component_class(
263 const char *plugin_name
, const char *comp_class_name
)
265 return (const void *) find_component_class_from_plugin(plugin_name
,
267 (plugin_borrow_comp_cls_func_t
)
268 bt_plugin_borrow_sink_component_class_by_name_const
);
272 const bt_component_class
*find_component_class(const char *plugin_name
,
273 const char *comp_class_name
,
274 bt_component_class_type comp_class_type
)
276 const bt_component_class
*comp_cls
= NULL
;
278 switch (comp_class_type
) {
279 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
280 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
282 case BT_COMPONENT_CLASS_TYPE_FILTER
:
283 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
285 case BT_COMPONENT_CLASS_TYPE_SINK
:
286 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
296 void print_indent(FILE *fp
, size_t indent
)
300 for (i
= 0; i
< indent
; i
++) {
306 const char *component_type_str(bt_component_class_type type
)
309 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
311 case BT_COMPONENT_CLASS_TYPE_SINK
:
313 case BT_COMPONENT_CLASS_TYPE_FILTER
:
321 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
322 const char *comp_cls_name
, bt_component_class_type type
)
324 GString
*shell_plugin_name
= NULL
;
325 GString
*shell_comp_cls_name
= NULL
;
328 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
329 if (!shell_plugin_name
) {
334 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
335 if (!shell_comp_cls_name
) {
339 fprintf(fh
, "'%s%s%s%s",
340 bt_common_color_bold(),
341 bt_common_color_fg_cyan(),
342 component_type_str(type
),
343 bt_common_color_fg_default());
345 if (shell_plugin_name
) {
346 fprintf(fh
, ".%s%s%s",
347 bt_common_color_fg_blue(),
348 shell_plugin_name
->str
,
349 bt_common_color_fg_default());
352 fprintf(fh
, ".%s%s%s'",
353 bt_common_color_fg_yellow(),
354 shell_comp_cls_name
->str
,
355 bt_common_color_reset());
358 if (shell_plugin_name
) {
359 g_string_free(shell_plugin_name
, TRUE
);
362 if (shell_comp_cls_name
) {
363 g_string_free(shell_comp_cls_name
, TRUE
);
368 void print_value(FILE *, const bt_value
*, size_t);
371 void print_value_rec(FILE *, const bt_value
*, size_t);
373 struct print_map_value_data
{
379 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
382 struct print_map_value_data
*print_map_value_data
= data
;
384 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
385 fprintf(print_map_value_data
->fp
, "%s: ", key
);
388 if (bt_value_is_array(object
) &&
389 bt_value_array_is_empty(object
)) {
390 fprintf(print_map_value_data
->fp
, "[ ]\n");
394 if (bt_value_is_map(object
) &&
395 bt_value_map_is_empty(object
)) {
396 fprintf(print_map_value_data
->fp
, "{ }\n");
400 if (bt_value_is_array(object
) ||
401 bt_value_is_map(object
)) {
402 fprintf(print_map_value_data
->fp
, "\n");
405 print_value_rec(print_map_value_data
->fp
, object
,
406 print_map_value_data
->indent
+ 2);
411 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
425 switch (bt_value_get_type(value
)) {
426 case BT_VALUE_TYPE_NULL
:
427 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
428 bt_common_color_reset());
430 case BT_VALUE_TYPE_BOOL
:
431 bool_val
= bt_value_bool_get(value
);
432 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
433 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
434 bt_common_color_reset());
436 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
437 uint_val
= bt_value_integer_unsigned_get(value
);
438 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
439 bt_common_color_fg_red(), uint_val
,
440 bt_common_color_reset());
442 case BT_VALUE_TYPE_SIGNED_INTEGER
:
443 int_val
= bt_value_integer_signed_get(value
);
444 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
445 bt_common_color_fg_red(), int_val
,
446 bt_common_color_reset());
448 case BT_VALUE_TYPE_REAL
:
449 dbl_val
= bt_value_real_get(value
);
450 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
451 bt_common_color_fg_red(), dbl_val
,
452 bt_common_color_reset());
454 case BT_VALUE_TYPE_STRING
:
455 str_val
= bt_value_string_get(value
);
456 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
457 bt_common_color_fg_green(), str_val
,
458 bt_common_color_reset());
460 case BT_VALUE_TYPE_ARRAY
:
461 size
= bt_value_array_get_size(value
);
467 print_indent(fp
, indent
);
468 fprintf(fp
, "[ ]\n");
472 for (i
= 0; i
< size
; i
++) {
473 const bt_value
*element
=
474 bt_value_array_borrow_element_by_index_const(
480 print_indent(fp
, indent
);
483 if (bt_value_is_array(element
) &&
484 bt_value_array_is_empty(element
)) {
485 fprintf(fp
, "[ ]\n");
489 if (bt_value_is_map(element
) &&
490 bt_value_map_is_empty(element
)) {
491 fprintf(fp
, "{ }\n");
495 if (bt_value_is_array(element
) ||
496 bt_value_is_map(element
)) {
500 print_value_rec(fp
, element
, indent
+ 2);
503 case BT_VALUE_TYPE_MAP
:
505 struct print_map_value_data data
= {
510 if (bt_value_map_is_empty(value
)) {
511 print_indent(fp
, indent
);
512 fprintf(fp
, "{ }\n");
516 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
525 BT_LOGE("Error printing value of type %s.",
526 bt_common_value_type_string(bt_value_get_type(value
)));
530 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
532 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
533 print_indent(fp
, indent
);
536 print_value_rec(fp
, value
, indent
);
540 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
542 fprintf(stderr
, " ");
543 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
544 bt_config_component
->comp_cls_name
->str
,
545 bt_config_component
->type
);
546 fprintf(stderr
, ":\n");
548 if (bt_config_component
->instance_name
->len
> 0) {
549 fprintf(stderr
, " Name: %s\n",
550 bt_config_component
->instance_name
->str
);
553 fprintf(stderr
, " Parameters:\n");
554 print_value(stderr
, bt_config_component
->params
, 8);
558 void print_bt_config_components(GPtrArray
*array
)
562 for (i
= 0; i
< array
->len
; i
++) {
563 struct bt_config_component
*cfg_component
=
564 bt_config_get_component(array
, i
);
565 print_bt_config_component(cfg_component
);
566 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
571 void print_plugin_paths(const bt_value
*plugin_paths
)
573 fprintf(stderr
, " Plugin paths:\n");
574 print_value(stderr
, plugin_paths
, 4);
578 void print_cfg_run(struct bt_config
*cfg
)
582 print_plugin_paths(cfg
->plugin_paths
);
583 fprintf(stderr
, " Source component instances:\n");
584 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
586 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
587 fprintf(stderr
, " Filter component instances:\n");
588 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
591 fprintf(stderr
, " Sink component instances:\n");
592 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
593 fprintf(stderr
, " Connections:\n");
595 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
596 struct bt_config_connection
*cfg_connection
=
597 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
600 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
601 cfg_connection
->upstream_comp_name
->str
,
602 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
603 cfg_connection
->upstream_port_glob
->str
,
604 cfg_connection
->downstream_comp_name
->str
,
605 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
606 cfg_connection
->downstream_port_glob
->str
);
611 void print_cfg_list_plugins(struct bt_config
*cfg
)
613 print_plugin_paths(cfg
->plugin_paths
);
617 void print_cfg_help(struct bt_config
*cfg
)
619 print_plugin_paths(cfg
->plugin_paths
);
623 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
625 print_plugin_paths(cfg
->plugin_paths
);
626 fprintf(stderr
, " Path: %s\n",
627 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
631 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
633 print_plugin_paths(cfg
->plugin_paths
);
634 fprintf(stderr
, " URL: %s\n",
635 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
639 void print_cfg_query(struct bt_config
*cfg
)
641 print_plugin_paths(cfg
->plugin_paths
);
642 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
643 fprintf(stderr
, " Component class:\n");
644 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
648 void print_cfg(struct bt_config
*cfg
)
650 if (!BT_LOG_ON_INFO
) {
654 BT_LOGI_STR("CLI configuration:");
655 BT_LOGI(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
656 BT_LOGI(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
658 switch (cfg
->command
) {
659 case BT_CONFIG_COMMAND_RUN
:
662 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
663 print_cfg_list_plugins(cfg
);
665 case BT_CONFIG_COMMAND_HELP
:
668 case BT_CONFIG_COMMAND_QUERY
:
669 print_cfg_query(cfg
);
671 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
672 print_cfg_print_ctf_metadata(cfg
);
674 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
675 print_cfg_print_lttng_live_sessions(cfg
);
683 void print_plugin_info(const bt_plugin
*plugin
)
685 unsigned int major
, minor
, patch
;
687 bt_property_availability version_avail
;
688 const char *plugin_name
;
692 const char *plugin_description
;
694 plugin_name
= bt_plugin_get_name(plugin
);
695 path
= bt_plugin_get_path(plugin
);
696 author
= bt_plugin_get_author(plugin
);
697 license
= bt_plugin_get_license(plugin
);
698 plugin_description
= bt_plugin_get_description(plugin
);
699 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
701 printf("%s%s%s%s:\n", bt_common_color_bold(),
702 bt_common_color_fg_blue(), plugin_name
,
703 bt_common_color_reset());
705 printf(" %sPath%s: %s\n", bt_common_color_bold(),
706 bt_common_color_reset(), path
);
711 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
712 printf(" %sVersion%s: %u.%u.%u",
713 bt_common_color_bold(), bt_common_color_reset(),
714 major
, minor
, patch
);
723 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
724 bt_common_color_reset(),
725 plugin_description
? plugin_description
: "(None)");
726 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
727 bt_common_color_reset(), author
? author
: "(Unknown)");
728 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
729 bt_common_color_reset(),
730 license
? license
: "(Unknown)");
734 int cmd_query(struct bt_config
*cfg
)
737 const bt_component_class
*comp_cls
= NULL
;
738 const bt_value
*results
= NULL
;
739 const char *fail_reason
= NULL
;
741 comp_cls
= find_component_class(
742 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
743 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
744 cfg
->cmd_data
.query
.cfg_component
->type
);
746 BT_CLI_LOGE_APPEND_CAUSE(
747 "Cannot find component class: plugin-name=\"%s\", "
748 "comp-cls-name=\"%s\", comp-cls-type=%d",
749 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
750 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
751 cfg
->cmd_data
.query
.cfg_component
->type
);
756 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
757 cfg
->cmd_data
.query
.cfg_component
->params
,
758 &results
, &fail_reason
);
763 print_value(stdout
, results
, 0);
767 BT_CLI_LOGE_APPEND_CAUSE(
768 "Failed to query component class: %s: plugin-name=\"%s\", "
769 "comp-cls-name=\"%s\", comp-cls-type=%d "
770 "object=\"%s\"", fail_reason
,
771 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
772 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
773 cfg
->cmd_data
.query
.cfg_component
->type
,
774 cfg
->cmd_data
.query
.object
->str
);
778 bt_component_class_put_ref(comp_cls
);
779 bt_value_put_ref(results
);
784 void print_component_class_help(const char *plugin_name
,
785 const bt_component_class
*comp_cls
)
787 const char *comp_class_name
=
788 bt_component_class_get_name(comp_cls
);
789 const char *comp_class_description
=
790 bt_component_class_get_description(comp_cls
);
791 const char *comp_class_help
=
792 bt_component_class_get_help(comp_cls
);
793 bt_component_class_type type
=
794 bt_component_class_get_type(comp_cls
);
796 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
798 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
799 bt_common_color_reset(),
800 comp_class_description
? comp_class_description
: "(None)");
802 if (comp_class_help
) {
803 printf("\n%s\n", comp_class_help
);
808 int cmd_help(struct bt_config
*cfg
)
811 const bt_plugin
*plugin
= NULL
;
812 const bt_component_class
*needed_comp_cls
= NULL
;
814 plugin
= find_loaded_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
816 BT_CLI_LOGE_APPEND_CAUSE(
817 "Cannot find plugin: plugin-name=\"%s\"",
818 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
823 print_plugin_info(plugin
);
824 printf(" %sSource component classes%s: %d\n",
825 bt_common_color_bold(),
826 bt_common_color_reset(),
827 (int) bt_plugin_get_source_component_class_count(plugin
));
828 printf(" %sFilter component classes%s: %d\n",
829 bt_common_color_bold(),
830 bt_common_color_reset(),
831 (int) bt_plugin_get_filter_component_class_count(plugin
));
832 printf(" %sSink component classes%s: %d\n",
833 bt_common_color_bold(),
834 bt_common_color_reset(),
835 (int) bt_plugin_get_sink_component_class_count(plugin
));
837 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
838 /* Plugin help only */
842 needed_comp_cls
= find_component_class(
843 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
844 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
845 cfg
->cmd_data
.help
.cfg_component
->type
);
846 if (!needed_comp_cls
) {
847 BT_CLI_LOGE_APPEND_CAUSE(
848 "Cannot find component class: plugin-name=\"%s\", "
849 "comp-cls-name=\"%s\", comp-cls-type=%d",
850 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
851 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
852 cfg
->cmd_data
.help
.cfg_component
->type
);
858 print_component_class_help(
859 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
863 bt_component_class_put_ref(needed_comp_cls
);
864 bt_plugin_put_ref(plugin
);
868 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
870 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
873 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
874 const char *cc_type_name
, uint64_t count
,
875 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
876 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
881 printf(" %s%s component classes%s: (none)\n",
882 bt_common_color_bold(),
884 bt_common_color_reset());
887 printf(" %s%s component classes%s:\n",
888 bt_common_color_bold(),
890 bt_common_color_reset());
893 for (i
= 0; i
< count
; i
++) {
894 const bt_component_class
*comp_class
=
895 spec_comp_cls_borrow_comp_cls_func(
896 borrow_comp_cls_by_index_func(plugin
, i
));
897 const char *comp_class_name
=
898 bt_component_class_get_name(comp_class
);
899 const char *comp_class_description
=
900 bt_component_class_get_description(comp_class
);
901 bt_component_class_type type
=
902 bt_component_class_get_type(comp_class
);
905 print_plugin_comp_cls_opt(stdout
,
906 bt_plugin_get_name(plugin
), comp_class_name
,
909 if (comp_class_description
) {
910 printf(": %s", comp_class_description
);
921 int cmd_list_plugins(struct bt_config
*cfg
)
924 int plugins_count
, component_classes_count
= 0, i
;
926 printf("From the following plugin paths:\n\n");
927 print_value(stdout
, cfg
->plugin_paths
, 2);
929 plugins_count
= get_loaded_plugins_count();
930 if (plugins_count
== 0) {
931 printf("No plugins found.\n");
935 for (i
= 0; i
< plugins_count
; i
++) {
936 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
938 component_classes_count
+=
939 bt_plugin_get_source_component_class_count(plugin
) +
940 bt_plugin_get_filter_component_class_count(plugin
) +
941 bt_plugin_get_sink_component_class_count(plugin
);
944 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
945 bt_common_color_bold(),
946 component_classes_count
,
947 bt_common_color_reset(),
948 bt_common_color_bold(),
950 bt_common_color_reset());
952 for (i
= 0; i
< plugins_count
; i
++) {
953 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
956 print_plugin_info(plugin
);
957 cmd_list_plugins_print_component_classes(plugin
, "Source",
958 bt_plugin_get_source_component_class_count(plugin
),
959 (plugin_borrow_comp_cls_by_index_func_t
)
960 bt_plugin_borrow_source_component_class_by_index_const
,
961 (spec_comp_cls_borrow_comp_cls_func_t
)
962 bt_component_class_source_as_component_class
);
963 cmd_list_plugins_print_component_classes(plugin
, "Filter",
964 bt_plugin_get_filter_component_class_count(plugin
),
965 (plugin_borrow_comp_cls_by_index_func_t
)
966 bt_plugin_borrow_filter_component_class_by_index_const
,
967 (spec_comp_cls_borrow_comp_cls_func_t
)
968 bt_component_class_filter_as_component_class
);
969 cmd_list_plugins_print_component_classes(plugin
, "Sink",
970 bt_plugin_get_sink_component_class_count(plugin
),
971 (plugin_borrow_comp_cls_by_index_func_t
)
972 bt_plugin_borrow_sink_component_class_by_index_const
,
973 (spec_comp_cls_borrow_comp_cls_func_t
)
974 bt_component_class_sink_as_component_class
);
982 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
985 const bt_component_class
*comp_cls
= NULL
;
986 const bt_value
*results
= NULL
;
987 bt_value
*params
= NULL
;
988 const bt_value
*map
= NULL
;
989 const bt_value
*v
= NULL
;
990 static const char * const plugin_name
= "ctf";
991 static const char * const comp_cls_name
= "lttng-live";
992 static const bt_component_class_type comp_cls_type
=
993 BT_COMPONENT_CLASS_TYPE_SOURCE
;
994 int64_t array_size
, i
;
995 const char *fail_reason
= NULL
;
996 FILE *out_stream
= stdout
;
998 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
999 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1002 BT_CLI_LOGE_APPEND_CAUSE(
1003 "Cannot find component class: plugin-name=\"%s\", "
1004 "comp-cls-name=\"%s\", comp-cls-type=%d",
1005 plugin_name
, comp_cls_name
,
1006 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1010 params
= bt_value_map_create();
1015 ret
= bt_value_map_insert_string_entry(params
, "url",
1016 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1021 ret
= query(cfg
, comp_cls
, "sessions", params
,
1022 &results
, &fail_reason
);
1029 if (!bt_value_is_array(results
)) {
1030 BT_CLI_LOGE_APPEND_CAUSE(
1031 "Expecting an array for LTTng live `sessions` query.");
1035 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1037 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1041 BT_LOGE_ERRNO("Cannot open file for writing",
1043 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1044 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1046 "Cannot open file for writing: path=\"%s\"",
1047 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1052 array_size
= bt_value_array_get_size(results
);
1053 for (i
= 0; i
< array_size
; i
++) {
1054 const char *url_text
;
1055 int64_t timer_us
, streams
, clients
;
1057 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1059 BT_CLI_LOGE_APPEND_CAUSE("Unexpected empty array entry.");
1062 if (!bt_value_is_map(map
)) {
1063 BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type.");
1067 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1069 BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry.");
1072 url_text
= bt_value_string_get(v
);
1073 fprintf(out_stream
, "%s", url_text
);
1074 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1076 BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry.");
1079 timer_us
= bt_value_integer_signed_get(v
);
1080 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1081 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1083 BT_CLI_LOGE_APPEND_CAUSE(
1084 "Missing `stream-count` entry.");
1087 streams
= bt_value_integer_signed_get(v
);
1088 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1089 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1091 BT_CLI_LOGE_APPEND_CAUSE(
1092 "Missing `client-count` entry.");
1095 clients
= bt_value_integer_signed_get(v
);
1096 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1102 BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s",
1109 bt_value_put_ref(results
);
1110 bt_value_put_ref(params
);
1111 bt_component_class_put_ref(comp_cls
);
1113 if (out_stream
&& out_stream
!= stdout
) {
1114 int fclose_ret
= fclose(out_stream
);
1117 BT_LOGE_ERRNO("Cannot close file stream",
1119 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1127 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1130 const bt_component_class
*comp_cls
= NULL
;
1131 const bt_value
*results
= NULL
;
1132 bt_value
*params
= NULL
;
1133 const bt_value
*metadata_text_value
= NULL
;
1134 const char *metadata_text
= NULL
;
1135 static const char * const plugin_name
= "ctf";
1136 static const char * const comp_cls_name
= "fs";
1137 static const bt_component_class_type comp_cls_type
=
1138 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1139 const char *fail_reason
= NULL
;
1140 FILE *out_stream
= stdout
;
1142 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1143 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1146 BT_CLI_LOGE_APPEND_CAUSE(
1147 "Cannot find component class: plugin-name=\"%s\", "
1148 "comp-cls-name=\"%s\", comp-cls-type=%d",
1149 plugin_name
, comp_cls_name
,
1150 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1155 params
= bt_value_map_create();
1161 ret
= bt_value_map_insert_string_entry(params
, "path",
1162 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1168 ret
= query(cfg
, comp_cls
, "metadata-info",
1169 params
, &results
, &fail_reason
);
1174 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1176 if (!metadata_text_value
) {
1177 BT_CLI_LOGE_APPEND_CAUSE(
1178 "Cannot find `text` string value in the resulting metadata info object.");
1183 metadata_text
= bt_value_string_get(metadata_text_value
);
1185 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1187 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1190 BT_LOGE_ERRNO("Cannot open file for writing",
1192 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1193 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1195 "Cannot open file for writing: path=\"%s\"",
1196 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1202 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1204 BT_CLI_LOGE_APPEND_CAUSE(
1205 "Cannot write whole metadata text to output stream: "
1215 BT_CLI_LOGE_APPEND_CAUSE(
1216 "Failed to query `metadata-info` object: %s", fail_reason
);
1219 bt_value_put_ref(results
);
1220 bt_value_put_ref(params
);
1221 bt_component_class_put_ref(comp_cls
);
1223 if (out_stream
&& out_stream
!= stdout
) {
1224 int fclose_ret
= fclose(out_stream
);
1227 BT_LOGE_ERRNO("Cannot close file stream",
1229 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1237 char *instance_name
;
1241 struct trace_range
{
1242 uint64_t intersection_range_begin_ns
;
1243 uint64_t intersection_range_end_ns
;
1247 guint
port_id_hash(gconstpointer v
)
1249 const struct port_id
*id
= v
;
1251 BT_ASSERT(id
->instance_name
);
1252 BT_ASSERT(id
->port_name
);
1254 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1258 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1260 const struct port_id
*id1
= v1
;
1261 const struct port_id
*id2
= v2
;
1263 return strcmp(id1
->instance_name
, id2
->instance_name
) == 0 &&
1264 strcmp(id1
->port_name
, id2
->port_name
) == 0;
1268 void port_id_destroy(gpointer data
)
1270 struct port_id
*id
= data
;
1272 free(id
->instance_name
);
1273 free(id
->port_name
);
1278 void trace_range_destroy(gpointer data
)
1283 struct cmd_run_ctx
{
1285 GHashTable
*src_components
;
1288 GHashTable
*flt_components
;
1291 GHashTable
*sink_components
;
1297 struct bt_config
*cfg
;
1301 bool stream_intersection_mode
;
1304 * Association of struct port_id -> struct trace_range.
1306 GHashTable
*intersections
;
1309 /* Returns a timestamp of the form "(-)s.ns" */
1311 char *s_from_ns(int64_t ns
)
1316 int64_t ts_sec_abs
, ts_nsec_abs
;
1317 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1318 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1320 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1321 is_negative
= false;
1322 ts_sec_abs
= ts_sec
;
1323 ts_nsec_abs
= ts_nsec
;
1324 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1325 is_negative
= false;
1326 ts_sec_abs
= ts_sec
- 1;
1327 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1328 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1330 ts_sec_abs
= ts_sec
;
1331 ts_nsec_abs
= -ts_nsec
;
1332 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1334 ts_sec_abs
= -(ts_sec
+ 1);
1335 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1336 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1338 ts_sec_abs
= -ts_sec
;
1339 ts_nsec_abs
= ts_nsec
;
1340 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1342 ts_sec_abs
= -ts_sec
;
1343 ts_nsec_abs
= -ts_nsec
;
1346 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1347 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1355 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1356 struct cmd_run_ctx
*ctx
,
1357 const bt_component
*upstream_comp
,
1358 const bt_port_output
*out_upstream_port
,
1359 struct bt_config_connection
*cfg_conn
)
1361 typedef uint64_t (*input_port_count_func_t
)(void *);
1362 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1363 const void *, uint64_t);
1364 const bt_port
*upstream_port
=
1365 bt_port_output_as_port_const(out_upstream_port
);
1368 GQuark downstreamp_comp_name_quark
;
1369 void *downstream_comp
;
1370 uint64_t downstream_port_count
;
1372 input_port_count_func_t port_count_fn
;
1373 borrow_input_port_by_index_func_t port_by_index_fn
;
1374 bt_graph_connect_ports_status connect_ports_status
=
1375 BT_GRAPH_CONNECT_PORTS_STATUS_OK
;
1376 bool insert_trimmer
= false;
1377 bt_value
*trimmer_params
= NULL
;
1378 char *intersection_begin
= NULL
;
1379 char *intersection_end
= NULL
;
1380 const bt_component_filter
*trimmer
= NULL
;
1381 const bt_component_class_filter
*trimmer_class
= NULL
;
1382 const bt_port_input
*trimmer_input
= NULL
;
1383 const bt_port_output
*trimmer_output
= NULL
;
1385 if (ctx
->intersections
&&
1386 bt_component_get_class_type(upstream_comp
) ==
1387 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1388 struct trace_range
*range
;
1389 struct port_id port_id
= {
1390 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1391 .port_name
= (char *) bt_port_get_name(upstream_port
)
1394 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1398 range
= (struct trace_range
*) g_hash_table_lookup(
1399 ctx
->intersections
, &port_id
);
1401 bt_value_map_insert_entry_status insert_status
;
1403 intersection_begin
= s_from_ns(
1404 range
->intersection_range_begin_ns
);
1405 intersection_end
= s_from_ns(
1406 range
->intersection_range_end_ns
);
1407 if (!intersection_begin
|| !intersection_end
) {
1408 BT_CLI_LOGE_APPEND_CAUSE(
1409 "Cannot create trimmer argument timestamp string.");
1413 insert_trimmer
= true;
1414 trimmer_params
= bt_value_map_create();
1415 if (!trimmer_params
) {
1419 insert_status
= bt_value_map_insert_string_entry(
1420 trimmer_params
, "begin", intersection_begin
);
1421 if (insert_status
< 0) {
1424 insert_status
= bt_value_map_insert_string_entry(
1426 "end", intersection_end
);
1427 if (insert_status
< 0) {
1432 trimmer_class
= find_filter_component_class("utils", "trimmer");
1433 if (!trimmer_class
) {
1438 BT_LOGI("Connecting upstream port to the next available downstream port: "
1439 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1440 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1441 upstream_port
, bt_port_get_name(upstream_port
),
1442 cfg_conn
->downstream_comp_name
->str
,
1443 cfg_conn
->arg
->str
);
1444 downstreamp_comp_name_quark
= g_quark_from_string(
1445 cfg_conn
->downstream_comp_name
->str
);
1446 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1447 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1448 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1449 port_count_fn
= (input_port_count_func_t
)
1450 bt_component_filter_get_input_port_count
;
1451 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1452 bt_component_filter_borrow_input_port_by_index_const
;
1454 if (!downstream_comp
) {
1455 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1456 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1457 port_count_fn
= (input_port_count_func_t
)
1458 bt_component_sink_get_input_port_count
;
1459 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1460 bt_component_sink_borrow_input_port_by_index_const
;
1463 if (!downstream_comp
) {
1464 BT_CLI_LOGE_APPEND_CAUSE("Cannot find downstream component: "
1465 "comp-name=\"%s\", conn-arg=\"%s\"",
1466 cfg_conn
->downstream_comp_name
->str
,
1467 cfg_conn
->arg
->str
);
1471 downstream_port_count
= port_count_fn(downstream_comp
);
1473 for (i
= 0; i
< downstream_port_count
; i
++) {
1474 const bt_port_input
*in_downstream_port
=
1475 port_by_index_fn(downstream_comp
, i
);
1476 const bt_port
*downstream_port
=
1477 bt_port_input_as_port_const(in_downstream_port
);
1478 const char *upstream_port_name
;
1479 const char *downstream_port_name
;
1481 BT_ASSERT(downstream_port
);
1483 /* Skip port if it's already connected. */
1484 if (bt_port_is_connected(downstream_port
)) {
1485 BT_LOGI("Skipping downstream port: already connected: "
1486 "port-addr=%p, port-name=\"%s\"",
1488 bt_port_get_name(downstream_port
));
1492 downstream_port_name
= bt_port_get_name(downstream_port
);
1493 BT_ASSERT(downstream_port_name
);
1494 upstream_port_name
= bt_port_get_name(upstream_port
);
1495 BT_ASSERT(upstream_port_name
);
1497 if (!bt_common_star_glob_match(
1498 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1499 downstream_port_name
, SIZE_MAX
)) {
1503 if (insert_trimmer
) {
1505 * In order to insert the trimmer between the
1506 * two components that were being connected, we
1507 * create a connection configuration entry which
1508 * describes a connection from the trimmer's
1509 * output to the original input that was being
1512 * Hence, the creation of the trimmer will cause
1513 * the graph "new port" listener to establish
1514 * all downstream connections as its output port
1515 * is connected. We will then establish the
1516 * connection between the original upstream
1517 * source and the trimmer.
1519 char *trimmer_name
= NULL
;
1520 bt_graph_add_component_status add_comp_status
;
1522 ret
= asprintf(&trimmer_name
,
1523 "stream-intersection-trimmer-%s",
1524 upstream_port_name
);
1530 ctx
->connect_ports
= false;
1531 add_comp_status
= bt_graph_add_filter_component(
1532 ctx
->graph
, trimmer_class
, trimmer_name
,
1533 trimmer_params
, ctx
->cfg
->log_level
,
1536 if (add_comp_status
!=
1537 BT_GRAPH_ADD_COMPONENT_STATUS_OK
) {
1543 bt_component_filter_borrow_input_port_by_index_const(
1545 if (!trimmer_input
) {
1549 bt_component_filter_borrow_output_port_by_index_const(
1551 if (!trimmer_output
) {
1556 * Replace the current downstream port by the trimmer's
1559 in_downstream_port
= trimmer_input
;
1561 bt_port_input_as_port_const(in_downstream_port
);
1562 downstream_port_name
= bt_port_get_name(
1564 BT_ASSERT(downstream_port_name
);
1567 /* We have a winner! */
1568 connect_ports_status
= bt_graph_connect_ports(ctx
->graph
,
1569 out_upstream_port
, in_downstream_port
, NULL
);
1570 downstream_port
= NULL
;
1571 switch (connect_ports_status
) {
1572 case BT_GRAPH_CONNECT_PORTS_STATUS_OK
:
1575 BT_CLI_LOGE_APPEND_CAUSE(
1576 "Cannot create connection: graph refuses to connect ports: "
1577 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1578 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1579 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1580 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1582 upstream_comp
, bt_component_get_name(upstream_comp
),
1583 upstream_port
, bt_port_get_name(upstream_port
),
1584 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1585 downstream_port
, downstream_port_name
,
1586 cfg_conn
->arg
->str
);
1590 BT_LOGI("Connected component ports: "
1591 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1592 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1593 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1594 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1596 upstream_comp
, bt_component_get_name(upstream_comp
),
1597 upstream_port
, bt_port_get_name(upstream_port
),
1598 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1599 downstream_port
, downstream_port_name
,
1600 cfg_conn
->arg
->str
);
1602 if (insert_trimmer
) {
1604 * The first connection, from the source to the trimmer,
1605 * has been done. We now connect the trimmer to the
1606 * original downstream port.
1608 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1610 bt_component_filter_as_component_const(trimmer
),
1611 trimmer_output
, cfg_conn
);
1615 ctx
->connect_ports
= true;
1619 * We found a matching downstream port: the search is
1625 /* No downstream port found */
1626 BT_CLI_LOGE_APPEND_CAUSE(
1627 "Cannot create connection: cannot find a matching downstream port for upstream port: "
1628 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1629 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1630 upstream_port
, bt_port_get_name(upstream_port
),
1631 cfg_conn
->downstream_comp_name
->str
,
1632 cfg_conn
->arg
->str
);
1638 free(intersection_begin
);
1639 free(intersection_end
);
1640 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1641 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1642 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1647 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1648 const bt_port_output
*upstream_port
)
1651 const char *upstream_port_name
;
1652 const char *upstream_comp_name
;
1653 const bt_component
*upstream_comp
= NULL
;
1657 BT_ASSERT(upstream_port
);
1658 upstream_port_name
= bt_port_get_name(
1659 bt_port_output_as_port_const(upstream_port
));
1660 BT_ASSERT(upstream_port_name
);
1661 upstream_comp
= bt_port_borrow_component_const(
1662 bt_port_output_as_port_const(upstream_port
));
1663 BT_ASSERT(upstream_comp
);
1664 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1665 BT_ASSERT(upstream_comp_name
);
1666 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1667 "port-addr=%p, port-name=\"%s\"",
1668 upstream_comp
, upstream_comp_name
,
1669 upstream_port
, upstream_port_name
);
1671 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1672 struct bt_config_connection
*cfg_conn
=
1674 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1676 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1677 upstream_comp_name
)) {
1681 if (!bt_common_star_glob_match(
1682 cfg_conn
->upstream_port_glob
->str
,
1683 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1687 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1688 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1690 BT_CLI_LOGE_APPEND_CAUSE(
1691 "Cannot connect upstream port: "
1692 "port-addr=%p, port-name=\"%s\"",
1694 upstream_port_name
);
1700 BT_CLI_LOGE_APPEND_CAUSE(
1701 "Cannot connect upstream port: port does not match any connection argument: "
1702 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1703 upstream_port_name
);
1713 bt_graph_listener_func_status
1714 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1715 const bt_port_output
*out_port
)
1717 const bt_component
*comp
;
1718 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1719 bt_graph_listener_func_status ret
=
1720 BT_GRAPH_LISTENER_FUNC_STATUS_OK
;
1722 comp
= bt_port_borrow_component_const(port
);
1723 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1724 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1725 comp
, comp
? bt_component_get_name(comp
) : "",
1726 port
, bt_port_get_name(port
));
1728 if (!ctx
->connect_ports
) {
1734 if (bt_port_is_connected(port
)) {
1735 BT_LOGW_STR("Port is already connected.");
1739 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
1740 BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port.");
1741 ret
= BT_GRAPH_LISTENER_FUNC_STATUS_ERROR
;
1750 bt_graph_listener_func_status
graph_source_output_port_added_listener(
1751 const bt_component_source
*component
,
1752 const bt_port_output
*port
, void *data
)
1754 return graph_output_port_added_listener(data
, port
);
1758 bt_graph_listener_func_status
graph_filter_output_port_added_listener(
1759 const bt_component_filter
*component
,
1760 const bt_port_output
*port
, void *data
)
1762 return graph_output_port_added_listener(data
, port
);
1766 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1772 if (ctx
->src_components
) {
1773 g_hash_table_destroy(ctx
->src_components
);
1774 ctx
->src_components
= NULL
;
1777 if (ctx
->flt_components
) {
1778 g_hash_table_destroy(ctx
->flt_components
);
1779 ctx
->flt_components
= NULL
;
1782 if (ctx
->sink_components
) {
1783 g_hash_table_destroy(ctx
->sink_components
);
1784 ctx
->sink_components
= NULL
;
1787 if (ctx
->intersections
) {
1788 g_hash_table_destroy(ctx
->intersections
);
1789 ctx
->intersections
= NULL
;
1792 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
1797 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1800 bt_graph_add_listener_status add_listener_status
;
1803 ctx
->connect_ports
= false;
1804 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
1805 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1806 if (!ctx
->src_components
) {
1810 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
1811 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1812 if (!ctx
->flt_components
) {
1816 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
1817 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1818 if (!ctx
->sink_components
) {
1822 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1823 ctx
->stream_intersection_mode
= true;
1824 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1825 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1826 if (!ctx
->intersections
) {
1831 ctx
->graph
= bt_graph_create();
1836 bt_graph_add_interrupter(ctx
->graph
, the_interrupter
);
1837 add_listener_status
= bt_graph_add_source_component_output_port_added_listener(
1838 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
1840 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1841 BT_CLI_LOGE_APPEND_CAUSE(
1842 "Cannot add \"port added\" listener to graph.");
1846 add_listener_status
= bt_graph_add_filter_component_output_port_added_listener(
1847 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
1849 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1850 BT_CLI_LOGE_APPEND_CAUSE(
1851 "Cannot add \"port added\" listener to graph.");
1858 cmd_run_ctx_destroy(ctx
);
1866 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
1867 struct bt_config_component
*cfg_comp
,
1868 const bt_component_class_source
*src_comp_cls
)
1872 int64_t trace_count
;
1873 const char *path
= NULL
;
1874 const bt_value
*query_result
= NULL
;
1875 const bt_value
*trace_info
= NULL
;
1876 const bt_value
*intersection_range
= NULL
;
1877 const bt_value
*intersection_begin
= NULL
;
1878 const bt_value
*intersection_end
= NULL
;
1879 const bt_value
*stream_infos
= NULL
;
1880 const bt_value
*stream_info
= NULL
;
1881 struct port_id
*port_id
= NULL
;
1882 struct trace_range
*trace_range
= NULL
;
1883 const char *fail_reason
= NULL
;
1884 const bt_component_class
*comp_cls
=
1885 bt_component_class_source_as_component_class_const(src_comp_cls
);
1887 ret
= query(ctx
->cfg
, comp_cls
, "trace-info",
1888 cfg_comp
->params
, &query_result
,
1891 BT_LOGD("Component class does not support the `trace-info` query: %s: "
1892 "comp-class-name=\"%s\"", fail_reason
,
1893 bt_component_class_get_name(comp_cls
));
1898 BT_ASSERT(query_result
);
1900 if (!bt_value_is_array(query_result
)) {
1901 BT_LOGD("Unexpected format of \'trace-info\' query result: "
1902 "component-class-name=%s",
1903 bt_component_class_get_name(comp_cls
));
1908 trace_count
= bt_value_array_get_size(query_result
);
1909 if (trace_count
< 0) {
1914 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
1916 uint64_t stream_idx
;
1917 int64_t stream_count
;
1919 trace_info
= bt_value_array_borrow_element_by_index_const(
1920 query_result
, trace_idx
);
1921 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
1923 BT_LOGD_STR("Cannot retrieve trace from query result.");
1927 intersection_range
= bt_value_map_borrow_entry_value_const(
1928 trace_info
, "intersection-range-ns");
1929 if (!intersection_range
) {
1931 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
1935 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
1937 if (!intersection_begin
) {
1939 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
1943 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
1945 if (!intersection_end
) {
1947 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
1951 begin
= bt_value_integer_signed_get(intersection_begin
);
1952 end
= bt_value_integer_signed_get(intersection_end
);
1954 if (begin
< 0 || end
< 0 || end
< begin
) {
1955 BT_CLI_LOGE_APPEND_CAUSE(
1956 "Invalid trace stream intersection values: "
1957 "intersection-range-ns:begin=%" PRId64
1958 ", intersection-range-ns:end=%" PRId64
,
1964 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
1966 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
1968 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
1972 stream_count
= bt_value_array_get_size(stream_infos
);
1973 if (stream_count
< 0) {
1978 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
1979 const bt_value
*port_name
;
1981 port_id
= g_new0(struct port_id
, 1);
1984 BT_CLI_LOGE_APPEND_CAUSE(
1985 "Cannot allocate memory for port_id structure.");
1988 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
1989 if (!port_id
->instance_name
) {
1991 BT_CLI_LOGE_APPEND_CAUSE(
1992 "Cannot allocate memory for port_id component instance name.");
1996 trace_range
= g_new0(struct trace_range
, 1);
1999 BT_CLI_LOGE_APPEND_CAUSE(
2000 "Cannot allocate memory for trace_range structure.");
2003 trace_range
->intersection_range_begin_ns
= begin
;
2004 trace_range
->intersection_range_end_ns
= end
;
2006 stream_info
= bt_value_array_borrow_element_by_index_const(
2007 stream_infos
, stream_idx
);
2008 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2010 BT_CLI_LOGE_APPEND_CAUSE(
2011 "Cannot retrieve stream informations from trace in query result.");
2015 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2016 if (!port_name
|| !bt_value_is_string(port_name
)) {
2018 BT_CLI_LOGE_APPEND_CAUSE(
2019 "Cannot retrieve port name in query result.");
2023 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2024 if (!port_id
->port_name
) {
2026 BT_CLI_LOGE_APPEND_CAUSE(
2027 "Cannot allocate memory for port_id port_name.");
2031 BT_LOGD("Inserting stream intersection ");
2033 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2043 BT_CLI_LOGE_APPEND_CAUSE(
2044 "Cannot determine stream intersection of trace: path=\"%s\"",
2045 path
? path
: "(unknown)");
2048 bt_value_put_ref(query_result
);
2050 g_free(trace_range
);
2055 int cmd_run_ctx_create_components_from_config_components(
2056 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2059 const void *comp_cls
= NULL
;
2060 const void *comp
= NULL
;
2063 for (i
= 0; i
< cfg_components
->len
; i
++) {
2064 struct bt_config_component
*cfg_comp
=
2065 g_ptr_array_index(cfg_components
, i
);
2068 switch (cfg_comp
->type
) {
2069 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2070 comp_cls
= find_source_component_class(
2071 cfg_comp
->plugin_name
->str
,
2072 cfg_comp
->comp_cls_name
->str
);
2074 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2075 comp_cls
= find_filter_component_class(
2076 cfg_comp
->plugin_name
->str
,
2077 cfg_comp
->comp_cls_name
->str
);
2079 case BT_COMPONENT_CLASS_TYPE_SINK
:
2080 comp_cls
= find_sink_component_class(
2081 cfg_comp
->plugin_name
->str
,
2082 cfg_comp
->comp_cls_name
->str
);
2089 BT_CLI_LOGE_APPEND_CAUSE(
2090 "Cannot find component class: plugin-name=\"%s\", "
2091 "comp-cls-name=\"%s\", comp-cls-type=%d",
2092 cfg_comp
->plugin_name
->str
,
2093 cfg_comp
->comp_cls_name
->str
,
2098 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_TRACE
);
2100 switch (cfg_comp
->type
) {
2101 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2102 ret
= bt_graph_add_source_component(ctx
->graph
,
2103 comp_cls
, cfg_comp
->instance_name
->str
,
2104 cfg_comp
->params
, cfg_comp
->log_level
,
2107 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2108 ret
= bt_graph_add_filter_component(ctx
->graph
,
2109 comp_cls
, cfg_comp
->instance_name
->str
,
2110 cfg_comp
->params
, cfg_comp
->log_level
,
2113 case BT_COMPONENT_CLASS_TYPE_SINK
:
2114 ret
= bt_graph_add_sink_component(ctx
->graph
,
2115 comp_cls
, cfg_comp
->instance_name
->str
,
2116 cfg_comp
->params
, cfg_comp
->log_level
,
2124 BT_CLI_LOGE_APPEND_CAUSE(
2125 "Cannot create component: plugin-name=\"%s\", "
2126 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2128 cfg_comp
->plugin_name
->str
,
2129 cfg_comp
->comp_cls_name
->str
,
2130 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2134 if (ctx
->stream_intersection_mode
&&
2135 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2136 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2142 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2143 comp
, cfg_comp
->instance_name
->str
);
2144 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2145 BT_ASSERT(quark
> 0);
2147 switch (cfg_comp
->type
) {
2148 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2149 g_hash_table_insert(ctx
->src_components
,
2150 GUINT_TO_POINTER(quark
), (void *) comp
);
2152 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2153 g_hash_table_insert(ctx
->flt_components
,
2154 GUINT_TO_POINTER(quark
), (void *) comp
);
2156 case BT_COMPONENT_CLASS_TYPE_SINK
:
2157 g_hash_table_insert(ctx
->sink_components
,
2158 GUINT_TO_POINTER(quark
), (void *) comp
);
2165 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2174 bt_object_put_ref(comp
);
2175 bt_object_put_ref(comp_cls
);
2180 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2185 * Make sure that, during this phase, our graph's "port added"
2186 * listener does not connect ports while we are creating the
2187 * components because we have a special, initial phase for
2190 ctx
->connect_ports
= false;
2192 ret
= cmd_run_ctx_create_components_from_config_components(
2193 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2199 ret
= cmd_run_ctx_create_components_from_config_components(
2200 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2206 ret
= cmd_run_ctx_create_components_from_config_components(
2207 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2217 typedef uint64_t (*output_port_count_func_t
)(const void *);
2218 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2219 const void *, uint64_t);
2222 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2223 void *comp
, output_port_count_func_t port_count_fn
,
2224 borrow_output_port_by_index_func_t port_by_index_fn
)
2230 count
= port_count_fn(comp
);
2232 for (i
= 0; i
< count
; i
++) {
2233 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2235 BT_ASSERT(upstream_port
);
2236 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2247 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2250 GHashTableIter iter
;
2251 gpointer g_name_quark
, g_comp
;
2253 ctx
->connect_ports
= true;
2254 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2256 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2257 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2258 (output_port_count_func_t
)
2259 bt_component_source_get_output_port_count
,
2260 (borrow_output_port_by_index_func_t
)
2261 bt_component_source_borrow_output_port_by_index_const
);
2267 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2269 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2270 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2271 (output_port_count_func_t
)
2272 bt_component_filter_get_output_port_count
,
2273 (borrow_output_port_by_index_func_t
)
2274 bt_component_filter_borrow_output_port_by_index_const
);
2285 int cmd_run(struct bt_config
*cfg
)
2288 struct cmd_run_ctx ctx
= { 0 };
2290 /* Initialize the command's context and the graph object */
2291 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2292 BT_CLI_LOGE_APPEND_CAUSE(
2293 "Cannot initialize the command's context.");
2297 if (bt_interrupter_is_set(the_interrupter
)) {
2298 BT_CLI_LOGW_APPEND_CAUSE(
2299 "Interrupted by user before creating components.");
2303 BT_LOGI_STR("Creating components.");
2305 /* Create the requested component instances */
2306 if (cmd_run_ctx_create_components(&ctx
)) {
2307 BT_CLI_LOGE_APPEND_CAUSE("Cannot create components.");
2311 if (bt_interrupter_is_set(the_interrupter
)) {
2312 BT_CLI_LOGW_APPEND_CAUSE(
2313 "Interrupted by user before connecting components.");
2317 BT_LOGI_STR("Connecting components.");
2319 /* Connect the initially visible component ports */
2320 if (cmd_run_ctx_connect_ports(&ctx
)) {
2321 BT_CLI_LOGE_APPEND_CAUSE(
2322 "Cannot connect initial component ports.");
2326 BT_LOGI_STR("Running the graph.");
2330 bt_graph_run_status run_status
= bt_graph_run(ctx
.graph
);
2333 * Reset console in case something messed with console
2334 * codes during the graph's execution.
2336 printf("%s", bt_common_color_reset());
2338 fprintf(stderr
, "%s", bt_common_color_reset());
2339 BT_LOGT("bt_graph_run() returned: status=%s",
2340 bt_common_func_status_string(run_status
));
2342 switch (run_status
) {
2343 case BT_GRAPH_RUN_STATUS_OK
:
2345 case BT_GRAPH_RUN_STATUS_AGAIN
:
2346 if (bt_interrupter_is_set(the_interrupter
)) {
2347 BT_CLI_LOGW_APPEND_CAUSE(
2348 "Graph was interrupted by user.");
2352 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2353 BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: "
2355 cfg
->cmd_data
.run
.retry_duration_us
);
2357 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2358 if (bt_interrupter_is_set(the_interrupter
)) {
2359 BT_CLI_LOGW_APPEND_CAUSE(
2360 "Graph was interrupted by user.");
2366 case BT_GRAPH_RUN_STATUS_END
:
2369 if (bt_interrupter_is_set(the_interrupter
)) {
2370 BT_CLI_LOGW_APPEND_CAUSE(
2371 "Graph was interrupted by user and failed: "
2373 bt_common_func_status_string(run_status
));
2377 BT_CLI_LOGE_APPEND_CAUSE(
2378 "Graph failed to complete successfully");
2391 cmd_run_ctx_destroy(&ctx
);
2396 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2398 const char *env_clash
;
2400 if (!cfg
->command_name
) {
2404 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2405 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2409 if (g_file_test(cfg
->command_name
,
2410 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2411 _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION
, __FILE__
, __LINE__
,
2412 BT_LOG_WARNING
, BT_LOG_TAG
,
2413 "The `%s` command was executed. "
2414 "If you meant to convert a trace located in "
2415 "the local `%s` directory, please use:\n\n"
2416 " babeltrace2 convert %s [OPTIONS]",
2417 cfg
->command_name
, cfg
->command_name
,
2423 void init_log_level(void)
2425 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2429 void set_auto_log_levels(struct bt_config
*cfg
)
2431 const char **env_var_name
;
2434 * Override the configuration's default log level if
2435 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2436 * are found for backward compatibility with legacy Babetrace 1.
2438 if (getenv("BABELTRACE_DEBUG") &&
2439 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2440 cfg
->log_level
= BT_LOG_TRACE
;
2441 } else if (getenv("BABELTRACE_VERBOSE") &&
2442 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2443 cfg
->log_level
= BT_LOG_INFO
;
2447 * Set log levels according to --debug or --verbose. For
2448 * backward compatibility, --debug is more verbose than
2451 * --verbose: INFO log level
2452 * --debug: TRACE log level (includes DEBUG, which is
2453 * is less verbose than TRACE in the internal
2454 * logging framework)
2456 if (!getenv("LIBBABELTRACE2_INIT_LOG_LEVEL")) {
2458 bt_logging_set_global_level(BT_LOG_INFO
);
2459 } else if (cfg
->debug
) {
2460 bt_logging_set_global_level(BT_LOG_TRACE
);
2463 * Set library's default log level if not
2464 * explicitly specified.
2466 bt_logging_set_global_level(cfg
->log_level
);
2470 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2472 bt_cli_log_level
= BT_LOG_INFO
;
2473 } else if (cfg
->debug
) {
2474 bt_cli_log_level
= BT_LOG_TRACE
;
2477 * Set CLI's default log level if not explicitly
2480 bt_cli_log_level
= cfg
->log_level
;
2484 env_var_name
= log_level_env_var_names
;
2486 while (*env_var_name
) {
2487 if (!getenv(*env_var_name
)) {
2489 g_setenv(*env_var_name
, "INFO", 1);
2490 } else if (cfg
->debug
) {
2491 g_setenv(*env_var_name
, "TRACE", 1);
2493 char val
[2] = { 0 };
2496 * Set module's default log level if not
2497 * explicitly specified.
2499 val
[0] = bt_log_get_letter_from_level(
2501 g_setenv(*env_var_name
, val
, 1);
2510 void print_error_causes(void)
2512 const bt_error
*error
= bt_current_thread_take_error();
2514 GString
*folded
= NULL
;
2515 unsigned int columns
;
2517 if (!error
|| bt_error_get_cause_count(error
) == 0) {
2518 fprintf(stderr
, "%s%sUnknown command-line error.%s\n",
2519 bt_common_color_bold(), bt_common_color_fg_red(),
2520 bt_common_color_reset());
2524 /* Try to get terminal width to fold the error cause messages */
2525 if (bt_common_get_term_size(&columns
, NULL
) < 0) {
2526 /* Width not found: default to 80 */
2531 * This helps visually separate the error causes from the last
2532 * logging statement.
2534 fprintf(stderr
, "\n");
2536 /* Reverse order: deepest (root) cause printed at the end */
2537 for (i
= bt_error_get_cause_count(error
) - 1; i
>= 0; i
--) {
2538 const bt_error_cause
*cause
=
2539 bt_error_borrow_cause_by_index(error
, (uint64_t) i
);
2540 const char *prefix_fmt
=
2541 i
== bt_error_get_cause_count(error
) - 1 ?
2542 "%s%sERROR%s: " : "%s%sCAUSED BY%s ";
2545 fprintf(stderr
, prefix_fmt
,
2546 bt_common_color_bold(), bt_common_color_fg_red(),
2547 bt_common_color_reset());
2549 /* Print actor name */
2550 fprintf(stderr
, "[");
2551 switch (bt_error_cause_get_actor_type(cause
)) {
2552 case BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN
:
2553 fprintf(stderr
, "%s%s%s",
2554 bt_common_color_bold(),
2555 bt_error_cause_get_module_name(cause
),
2556 bt_common_color_reset());
2558 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
:
2559 fprintf(stderr
, "%s%s%s: ",
2560 bt_common_color_bold(),
2561 bt_error_cause_component_actor_get_component_name(cause
),
2562 bt_common_color_reset());
2563 print_plugin_comp_cls_opt(stderr
,
2564 bt_error_cause_component_actor_get_plugin_name(cause
),
2565 bt_error_cause_component_actor_get_component_class_name(cause
),
2566 bt_error_cause_component_actor_get_component_class_type(cause
));
2568 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
:
2569 print_plugin_comp_cls_opt(stderr
,
2570 bt_error_cause_component_class_actor_get_plugin_name(cause
),
2571 bt_error_cause_component_class_actor_get_component_class_name(cause
),
2572 bt_error_cause_component_class_actor_get_component_class_type(cause
));
2574 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
:
2575 fprintf(stderr
, "%s%s%s (%s%s%s): ",
2576 bt_common_color_bold(),
2577 bt_error_cause_message_iterator_actor_get_component_name(cause
),
2578 bt_common_color_reset(),
2579 bt_common_color_bold(),
2580 bt_error_cause_message_iterator_actor_get_component_output_port_name(cause
),
2581 bt_common_color_reset());
2582 print_plugin_comp_cls_opt(stderr
,
2583 bt_error_cause_message_iterator_actor_get_plugin_name(cause
),
2584 bt_error_cause_message_iterator_actor_get_component_class_name(cause
),
2585 bt_error_cause_message_iterator_actor_get_component_class_type(cause
));
2591 /* Print file name and line number */
2592 fprintf(stderr
, "] (%s%s%s%s:%s%" PRIu64
"%s)\n",
2593 bt_common_color_bold(),
2594 bt_common_color_fg_magenta(),
2595 bt_error_cause_get_file_name(cause
),
2596 bt_common_color_reset(),
2597 bt_common_color_fg_green(),
2598 bt_error_cause_get_line_number(cause
),
2599 bt_common_color_reset());
2602 folded
= bt_common_fold(bt_error_cause_get_message(cause
),
2605 BT_LOGE_STR("Could not fold string.");
2606 fprintf(stderr
, "%s\n",
2607 bt_error_cause_get_message(cause
));
2611 fprintf(stderr
, "%s\n", folded
->str
);
2612 g_string_free(folded
, TRUE
);
2618 g_string_free(folded
, TRUE
);
2622 bt_error_release(error
);
2626 int main(int argc
, const char **argv
)
2630 struct bt_config
*cfg
;
2633 set_signal_handler();
2634 init_loaded_plugins();
2635 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2638 /* Quit without errors; typically usage/version */
2640 BT_LOGI_STR("Quitting without errors.");
2645 BT_CLI_LOGE_APPEND_CAUSE(
2646 "Command-line error: retcode=%d", retcode
);
2651 BT_CLI_LOGE_APPEND_CAUSE(
2652 "Failed to create a valid Babeltrace configuration.");
2657 set_auto_log_levels(cfg
);
2660 if (cfg
->command_needs_plugins
) {
2661 ret
= require_loaded_plugins(cfg
->plugin_paths
);
2663 BT_CLI_LOGE_APPEND_CAUSE(
2664 "Failed to load plugins: ret=%d", ret
);
2670 BT_ASSERT(!the_interrupter
);
2671 the_interrupter
= bt_interrupter_create();
2672 if (!the_interrupter
) {
2673 BT_CLI_LOGE_APPEND_CAUSE("Failed to create an interrupter object.");
2678 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2679 cfg
->command
, cfg
->command_name
);
2681 switch (cfg
->command
) {
2682 case BT_CONFIG_COMMAND_RUN
:
2685 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2686 ret
= cmd_list_plugins(cfg
);
2688 case BT_CONFIG_COMMAND_HELP
:
2689 ret
= cmd_help(cfg
);
2691 case BT_CONFIG_COMMAND_QUERY
:
2692 ret
= cmd_query(cfg
);
2694 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2695 ret
= cmd_print_ctf_metadata(cfg
);
2697 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2698 ret
= cmd_print_lttng_live_sessions(cfg
);
2701 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2705 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2706 cfg
->command
, cfg
->command_name
, ret
);
2707 warn_command_name_and_directory_clash(cfg
);
2708 retcode
= ret
? 1 : 0;
2711 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2712 fini_loaded_plugins();
2713 bt_interrupter_put_ref(the_interrupter
);
2716 print_error_causes();
2720 * Clear current thread's error in case there is one to avoid a
2723 bt_current_thread_clear_error();