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"
43 #include "babeltrace2-query.h"
45 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
46 #define ENV_BABELTRACE_CLI_LOG_LEVEL "BABELTRACE_CLI_LOG_LEVEL"
47 #define NSEC_PER_SEC 1000000000LL
50 * Known environment variable names for the log levels of the project's
53 static const char* log_level_env_var_names
[] = {
54 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
55 "BABELTRACE_PYTHON_BT2_LOG_LEVEL",
59 /* Application's interrupter (owned by this) */
60 static bt_interrupter
*the_interrupter
;
67 BOOL WINAPI
signal_handler(DWORD signal
) {
68 if (the_interrupter
) {
69 bt_interrupter_set(the_interrupter
);
76 void set_signal_handler(void)
78 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
79 BT_LOGE("Failed to set the Ctrl+C handler.");
83 #else /* __MINGW32__ */
86 void signal_handler(int signum
)
88 if (signum
!= SIGINT
) {
92 if (the_interrupter
) {
93 bt_interrupter_set(the_interrupter
);
98 void set_signal_handler(void)
100 struct sigaction new_action
, old_action
;
102 new_action
.sa_handler
= signal_handler
;
103 sigemptyset(&new_action
.sa_mask
);
104 new_action
.sa_flags
= 0;
105 sigaction(SIGINT
, NULL
, &old_action
);
107 if (old_action
.sa_handler
!= SIG_IGN
) {
108 sigaction(SIGINT
, &new_action
, NULL
);
112 #endif /* __MINGW32__ */
115 int query(struct bt_config
*cfg
, const bt_component_class
*comp_cls
,
116 const char *obj
, const bt_value
*params
,
117 const bt_value
**user_result
, const char **fail_reason
)
119 return cli_query(comp_cls
, obj
, params
, cfg
->log_level
,
120 the_interrupter
, user_result
, fail_reason
);
123 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
124 const bt_plugin
*, const char *);
127 const void *find_component_class_from_plugin(const char *plugin_name
,
128 const char *comp_class_name
,
129 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
131 const void *comp_class
= NULL
;
132 const bt_plugin
*plugin
;
134 BT_LOGI("Finding component class: plugin-name=\"%s\", "
135 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
137 plugin
= find_loaded_plugin(plugin_name
);
142 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
143 bt_object_get_ref(comp_class
);
144 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
148 BT_LOGI("Found component class: plugin-name=\"%s\", "
149 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
151 BT_LOGI("Cannot find source component class: "
152 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
153 plugin_name
, comp_class_name
);
160 const bt_component_class_source
*find_source_component_class(
161 const char *plugin_name
, const char *comp_class_name
)
163 return (const void *) find_component_class_from_plugin(
164 plugin_name
, comp_class_name
,
165 (plugin_borrow_comp_cls_func_t
)
166 bt_plugin_borrow_source_component_class_by_name_const
);
170 const bt_component_class_filter
*find_filter_component_class(
171 const char *plugin_name
, const char *comp_class_name
)
173 return (const void *) find_component_class_from_plugin(
174 plugin_name
, comp_class_name
,
175 (plugin_borrow_comp_cls_func_t
)
176 bt_plugin_borrow_filter_component_class_by_name_const
);
180 const bt_component_class_sink
*find_sink_component_class(
181 const char *plugin_name
, const char *comp_class_name
)
183 return (const void *) find_component_class_from_plugin(plugin_name
,
185 (plugin_borrow_comp_cls_func_t
)
186 bt_plugin_borrow_sink_component_class_by_name_const
);
190 const bt_component_class
*find_component_class(const char *plugin_name
,
191 const char *comp_class_name
,
192 bt_component_class_type comp_class_type
)
194 const bt_component_class
*comp_cls
= NULL
;
196 switch (comp_class_type
) {
197 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
198 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
200 case BT_COMPONENT_CLASS_TYPE_FILTER
:
201 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
203 case BT_COMPONENT_CLASS_TYPE_SINK
:
204 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
214 void print_indent(FILE *fp
, size_t indent
)
218 for (i
= 0; i
< indent
; i
++) {
224 const char *component_type_str(bt_component_class_type type
)
227 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
229 case BT_COMPONENT_CLASS_TYPE_SINK
:
231 case BT_COMPONENT_CLASS_TYPE_FILTER
:
239 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
240 const char *comp_cls_name
, bt_component_class_type type
)
242 GString
*shell_plugin_name
= NULL
;
243 GString
*shell_comp_cls_name
= NULL
;
246 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
247 if (!shell_plugin_name
) {
252 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
253 if (!shell_comp_cls_name
) {
257 fprintf(fh
, "'%s%s%s%s",
258 bt_common_color_bold(),
259 bt_common_color_fg_cyan(),
260 component_type_str(type
),
261 bt_common_color_fg_default());
263 if (shell_plugin_name
) {
264 fprintf(fh
, ".%s%s%s",
265 bt_common_color_fg_blue(),
266 shell_plugin_name
->str
,
267 bt_common_color_fg_default());
270 fprintf(fh
, ".%s%s%s'",
271 bt_common_color_fg_yellow(),
272 shell_comp_cls_name
->str
,
273 bt_common_color_reset());
276 if (shell_plugin_name
) {
277 g_string_free(shell_plugin_name
, TRUE
);
280 if (shell_comp_cls_name
) {
281 g_string_free(shell_comp_cls_name
, TRUE
);
286 void print_value(FILE *, const bt_value
*, size_t);
289 void print_value_rec(FILE *, const bt_value
*, size_t);
291 struct print_map_value_data
{
297 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
300 struct print_map_value_data
*print_map_value_data
= data
;
302 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
303 fprintf(print_map_value_data
->fp
, "%s: ", key
);
306 if (bt_value_is_array(object
) &&
307 bt_value_array_is_empty(object
)) {
308 fprintf(print_map_value_data
->fp
, "[ ]\n");
312 if (bt_value_is_map(object
) &&
313 bt_value_map_is_empty(object
)) {
314 fprintf(print_map_value_data
->fp
, "{ }\n");
318 if (bt_value_is_array(object
) ||
319 bt_value_is_map(object
)) {
320 fprintf(print_map_value_data
->fp
, "\n");
323 print_value_rec(print_map_value_data
->fp
, object
,
324 print_map_value_data
->indent
+ 2);
329 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
343 switch (bt_value_get_type(value
)) {
344 case BT_VALUE_TYPE_NULL
:
345 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
346 bt_common_color_reset());
348 case BT_VALUE_TYPE_BOOL
:
349 bool_val
= bt_value_bool_get(value
);
350 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
351 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
352 bt_common_color_reset());
354 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
355 uint_val
= bt_value_integer_unsigned_get(value
);
356 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
357 bt_common_color_fg_red(), uint_val
,
358 bt_common_color_reset());
360 case BT_VALUE_TYPE_SIGNED_INTEGER
:
361 int_val
= bt_value_integer_signed_get(value
);
362 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
363 bt_common_color_fg_red(), int_val
,
364 bt_common_color_reset());
366 case BT_VALUE_TYPE_REAL
:
367 dbl_val
= bt_value_real_get(value
);
368 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
369 bt_common_color_fg_red(), dbl_val
,
370 bt_common_color_reset());
372 case BT_VALUE_TYPE_STRING
:
373 str_val
= bt_value_string_get(value
);
374 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
375 bt_common_color_fg_green(), str_val
,
376 bt_common_color_reset());
378 case BT_VALUE_TYPE_ARRAY
:
379 size
= bt_value_array_get_size(value
);
385 print_indent(fp
, indent
);
386 fprintf(fp
, "[ ]\n");
390 for (i
= 0; i
< size
; i
++) {
391 const bt_value
*element
=
392 bt_value_array_borrow_element_by_index_const(
398 print_indent(fp
, indent
);
401 if (bt_value_is_array(element
) &&
402 bt_value_array_is_empty(element
)) {
403 fprintf(fp
, "[ ]\n");
407 if (bt_value_is_map(element
) &&
408 bt_value_map_is_empty(element
)) {
409 fprintf(fp
, "{ }\n");
413 if (bt_value_is_array(element
) ||
414 bt_value_is_map(element
)) {
418 print_value_rec(fp
, element
, indent
+ 2);
421 case BT_VALUE_TYPE_MAP
:
423 struct print_map_value_data data
= {
428 if (bt_value_map_is_empty(value
)) {
429 print_indent(fp
, indent
);
430 fprintf(fp
, "{ }\n");
434 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
443 BT_LOGE("Error printing value of type %s.",
444 bt_common_value_type_string(bt_value_get_type(value
)));
448 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
450 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
451 print_indent(fp
, indent
);
454 print_value_rec(fp
, value
, indent
);
458 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
460 fprintf(stderr
, " ");
461 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
462 bt_config_component
->comp_cls_name
->str
,
463 bt_config_component
->type
);
464 fprintf(stderr
, ":\n");
466 if (bt_config_component
->instance_name
->len
> 0) {
467 fprintf(stderr
, " Name: %s\n",
468 bt_config_component
->instance_name
->str
);
471 fprintf(stderr
, " Parameters:\n");
472 print_value(stderr
, bt_config_component
->params
, 8);
476 void print_bt_config_components(GPtrArray
*array
)
480 for (i
= 0; i
< array
->len
; i
++) {
481 struct bt_config_component
*cfg_component
=
482 bt_config_get_component(array
, i
);
483 print_bt_config_component(cfg_component
);
484 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
489 void print_plugin_paths(const bt_value
*plugin_paths
)
491 fprintf(stderr
, " Plugin paths:\n");
492 print_value(stderr
, plugin_paths
, 4);
496 void print_cfg_run(struct bt_config
*cfg
)
500 print_plugin_paths(cfg
->plugin_paths
);
501 fprintf(stderr
, " Source component instances:\n");
502 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
504 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
505 fprintf(stderr
, " Filter component instances:\n");
506 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
509 fprintf(stderr
, " Sink component instances:\n");
510 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
511 fprintf(stderr
, " Connections:\n");
513 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
514 struct bt_config_connection
*cfg_connection
=
515 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
518 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
519 cfg_connection
->upstream_comp_name
->str
,
520 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
521 cfg_connection
->upstream_port_glob
->str
,
522 cfg_connection
->downstream_comp_name
->str
,
523 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
524 cfg_connection
->downstream_port_glob
->str
);
529 void print_cfg_list_plugins(struct bt_config
*cfg
)
531 print_plugin_paths(cfg
->plugin_paths
);
535 void print_cfg_help(struct bt_config
*cfg
)
537 print_plugin_paths(cfg
->plugin_paths
);
541 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
543 print_plugin_paths(cfg
->plugin_paths
);
544 fprintf(stderr
, " Path: %s\n",
545 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
549 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
551 print_plugin_paths(cfg
->plugin_paths
);
552 fprintf(stderr
, " URL: %s\n",
553 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
557 void print_cfg_query(struct bt_config
*cfg
)
559 print_plugin_paths(cfg
->plugin_paths
);
560 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
561 fprintf(stderr
, " Component class:\n");
562 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
566 void print_cfg(struct bt_config
*cfg
)
568 if (!BT_LOG_ON_INFO
) {
572 BT_LOGI_STR("CLI configuration:");
573 BT_LOGI(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
574 BT_LOGI(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
576 switch (cfg
->command
) {
577 case BT_CONFIG_COMMAND_RUN
:
580 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
581 print_cfg_list_plugins(cfg
);
583 case BT_CONFIG_COMMAND_HELP
:
586 case BT_CONFIG_COMMAND_QUERY
:
587 print_cfg_query(cfg
);
589 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
590 print_cfg_print_ctf_metadata(cfg
);
592 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
593 print_cfg_print_lttng_live_sessions(cfg
);
601 void print_plugin_info(const bt_plugin
*plugin
)
603 unsigned int major
, minor
, patch
;
605 bt_property_availability version_avail
;
606 const char *plugin_name
;
610 const char *plugin_description
;
612 plugin_name
= bt_plugin_get_name(plugin
);
613 path
= bt_plugin_get_path(plugin
);
614 author
= bt_plugin_get_author(plugin
);
615 license
= bt_plugin_get_license(plugin
);
616 plugin_description
= bt_plugin_get_description(plugin
);
617 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
619 printf("%s%s%s%s:\n", bt_common_color_bold(),
620 bt_common_color_fg_blue(), plugin_name
,
621 bt_common_color_reset());
623 printf(" %sPath%s: %s\n", bt_common_color_bold(),
624 bt_common_color_reset(), path
);
629 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
630 printf(" %sVersion%s: %u.%u.%u",
631 bt_common_color_bold(), bt_common_color_reset(),
632 major
, minor
, patch
);
641 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
642 bt_common_color_reset(),
643 plugin_description
? plugin_description
: "(None)");
644 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
645 bt_common_color_reset(), author
? author
: "(Unknown)");
646 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
647 bt_common_color_reset(),
648 license
? license
: "(Unknown)");
652 int cmd_query(struct bt_config
*cfg
)
655 const bt_component_class
*comp_cls
= NULL
;
656 const bt_value
*results
= NULL
;
657 const char *fail_reason
= NULL
;
659 comp_cls
= find_component_class(
660 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
661 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
662 cfg
->cmd_data
.query
.cfg_component
->type
);
664 BT_CLI_LOGE_APPEND_CAUSE(
665 "Cannot find component class: plugin-name=\"%s\", "
666 "comp-cls-name=\"%s\", comp-cls-type=%d",
667 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
668 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
669 cfg
->cmd_data
.query
.cfg_component
->type
);
674 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
675 cfg
->cmd_data
.query
.cfg_component
->params
,
676 &results
, &fail_reason
);
681 print_value(stdout
, results
, 0);
685 BT_CLI_LOGE_APPEND_CAUSE(
686 "Failed to query component class: %s: plugin-name=\"%s\", "
687 "comp-cls-name=\"%s\", comp-cls-type=%d "
688 "object=\"%s\"", fail_reason
,
689 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
690 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
691 cfg
->cmd_data
.query
.cfg_component
->type
,
692 cfg
->cmd_data
.query
.object
->str
);
696 bt_component_class_put_ref(comp_cls
);
697 bt_value_put_ref(results
);
702 void print_component_class_help(const char *plugin_name
,
703 const bt_component_class
*comp_cls
)
705 const char *comp_class_name
=
706 bt_component_class_get_name(comp_cls
);
707 const char *comp_class_description
=
708 bt_component_class_get_description(comp_cls
);
709 const char *comp_class_help
=
710 bt_component_class_get_help(comp_cls
);
711 bt_component_class_type type
=
712 bt_component_class_get_type(comp_cls
);
714 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
716 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
717 bt_common_color_reset(),
718 comp_class_description
? comp_class_description
: "(None)");
720 if (comp_class_help
) {
721 printf("\n%s\n", comp_class_help
);
726 int cmd_help(struct bt_config
*cfg
)
729 const bt_plugin
*plugin
= NULL
;
730 const bt_component_class
*needed_comp_cls
= NULL
;
732 plugin
= find_loaded_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
734 BT_CLI_LOGE_APPEND_CAUSE(
735 "Cannot find plugin: plugin-name=\"%s\"",
736 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
741 print_plugin_info(plugin
);
742 printf(" %sSource component classes%s: %d\n",
743 bt_common_color_bold(),
744 bt_common_color_reset(),
745 (int) bt_plugin_get_source_component_class_count(plugin
));
746 printf(" %sFilter component classes%s: %d\n",
747 bt_common_color_bold(),
748 bt_common_color_reset(),
749 (int) bt_plugin_get_filter_component_class_count(plugin
));
750 printf(" %sSink component classes%s: %d\n",
751 bt_common_color_bold(),
752 bt_common_color_reset(),
753 (int) bt_plugin_get_sink_component_class_count(plugin
));
755 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
756 /* Plugin help only */
760 needed_comp_cls
= find_component_class(
761 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
762 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
763 cfg
->cmd_data
.help
.cfg_component
->type
);
764 if (!needed_comp_cls
) {
765 BT_CLI_LOGE_APPEND_CAUSE(
766 "Cannot find component class: plugin-name=\"%s\", "
767 "comp-cls-name=\"%s\", comp-cls-type=%d",
768 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
769 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
770 cfg
->cmd_data
.help
.cfg_component
->type
);
776 print_component_class_help(
777 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
781 bt_component_class_put_ref(needed_comp_cls
);
782 bt_plugin_put_ref(plugin
);
786 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
788 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
791 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
792 const char *cc_type_name
, uint64_t count
,
793 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
794 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
799 printf(" %s%s component classes%s: (none)\n",
800 bt_common_color_bold(),
802 bt_common_color_reset());
805 printf(" %s%s component classes%s:\n",
806 bt_common_color_bold(),
808 bt_common_color_reset());
811 for (i
= 0; i
< count
; i
++) {
812 const bt_component_class
*comp_class
=
813 spec_comp_cls_borrow_comp_cls_func(
814 borrow_comp_cls_by_index_func(plugin
, i
));
815 const char *comp_class_name
=
816 bt_component_class_get_name(comp_class
);
817 const char *comp_class_description
=
818 bt_component_class_get_description(comp_class
);
819 bt_component_class_type type
=
820 bt_component_class_get_type(comp_class
);
823 print_plugin_comp_cls_opt(stdout
,
824 bt_plugin_get_name(plugin
), comp_class_name
,
827 if (comp_class_description
) {
828 printf(": %s", comp_class_description
);
839 int cmd_list_plugins(struct bt_config
*cfg
)
842 int plugins_count
, component_classes_count
= 0, i
;
844 printf("From the following plugin paths:\n\n");
845 print_value(stdout
, cfg
->plugin_paths
, 2);
847 plugins_count
= get_loaded_plugins_count();
848 if (plugins_count
== 0) {
849 printf("No plugins found.\n");
853 for (i
= 0; i
< plugins_count
; i
++) {
854 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
856 component_classes_count
+=
857 bt_plugin_get_source_component_class_count(plugin
) +
858 bt_plugin_get_filter_component_class_count(plugin
) +
859 bt_plugin_get_sink_component_class_count(plugin
);
862 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
863 bt_common_color_bold(),
864 component_classes_count
,
865 bt_common_color_reset(),
866 bt_common_color_bold(),
868 bt_common_color_reset());
870 for (i
= 0; i
< plugins_count
; i
++) {
871 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
874 print_plugin_info(plugin
);
875 cmd_list_plugins_print_component_classes(plugin
, "Source",
876 bt_plugin_get_source_component_class_count(plugin
),
877 (plugin_borrow_comp_cls_by_index_func_t
)
878 bt_plugin_borrow_source_component_class_by_index_const
,
879 (spec_comp_cls_borrow_comp_cls_func_t
)
880 bt_component_class_source_as_component_class
);
881 cmd_list_plugins_print_component_classes(plugin
, "Filter",
882 bt_plugin_get_filter_component_class_count(plugin
),
883 (plugin_borrow_comp_cls_by_index_func_t
)
884 bt_plugin_borrow_filter_component_class_by_index_const
,
885 (spec_comp_cls_borrow_comp_cls_func_t
)
886 bt_component_class_filter_as_component_class
);
887 cmd_list_plugins_print_component_classes(plugin
, "Sink",
888 bt_plugin_get_sink_component_class_count(plugin
),
889 (plugin_borrow_comp_cls_by_index_func_t
)
890 bt_plugin_borrow_sink_component_class_by_index_const
,
891 (spec_comp_cls_borrow_comp_cls_func_t
)
892 bt_component_class_sink_as_component_class
);
900 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
903 const bt_component_class
*comp_cls
= NULL
;
904 const bt_value
*results
= NULL
;
905 bt_value
*params
= NULL
;
906 const bt_value
*map
= NULL
;
907 const bt_value
*v
= NULL
;
908 static const char * const plugin_name
= "ctf";
909 static const char * const comp_cls_name
= "lttng-live";
910 static const bt_component_class_type comp_cls_type
=
911 BT_COMPONENT_CLASS_TYPE_SOURCE
;
912 int64_t array_size
, i
;
913 const char *fail_reason
= NULL
;
914 FILE *out_stream
= stdout
;
916 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
917 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
920 BT_CLI_LOGE_APPEND_CAUSE(
921 "Cannot find component class: plugin-name=\"%s\", "
922 "comp-cls-name=\"%s\", comp-cls-type=%d",
923 plugin_name
, comp_cls_name
,
924 BT_COMPONENT_CLASS_TYPE_SOURCE
);
928 params
= bt_value_map_create();
933 ret
= bt_value_map_insert_string_entry(params
, "url",
934 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
939 ret
= query(cfg
, comp_cls
, "sessions", params
,
940 &results
, &fail_reason
);
947 if (!bt_value_is_array(results
)) {
948 BT_CLI_LOGE_APPEND_CAUSE(
949 "Expecting an array for LTTng live `sessions` query.");
953 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
955 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
959 BT_LOGE_ERRNO("Cannot open file for writing",
961 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
962 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
964 "Cannot open file for writing: path=\"%s\"",
965 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
970 array_size
= bt_value_array_get_size(results
);
971 for (i
= 0; i
< array_size
; i
++) {
972 const char *url_text
;
973 int64_t timer_us
, streams
, clients
;
975 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
977 BT_CLI_LOGE_APPEND_CAUSE("Unexpected empty array entry.");
980 if (!bt_value_is_map(map
)) {
981 BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type.");
985 v
= bt_value_map_borrow_entry_value_const(map
, "url");
987 BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry.");
990 url_text
= bt_value_string_get(v
);
991 fprintf(out_stream
, "%s", url_text
);
992 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
994 BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry.");
997 timer_us
= bt_value_integer_signed_get(v
);
998 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
999 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1001 BT_CLI_LOGE_APPEND_CAUSE(
1002 "Missing `stream-count` entry.");
1005 streams
= bt_value_integer_signed_get(v
);
1006 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1007 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1009 BT_CLI_LOGE_APPEND_CAUSE(
1010 "Missing `client-count` entry.");
1013 clients
= bt_value_integer_signed_get(v
);
1014 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1020 BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s",
1027 bt_value_put_ref(results
);
1028 bt_value_put_ref(params
);
1029 bt_component_class_put_ref(comp_cls
);
1031 if (out_stream
&& out_stream
!= stdout
) {
1032 int fclose_ret
= fclose(out_stream
);
1035 BT_LOGE_ERRNO("Cannot close file stream",
1037 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1045 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1048 const bt_component_class
*comp_cls
= NULL
;
1049 const bt_value
*results
= NULL
;
1050 bt_value
*params
= NULL
;
1051 const bt_value
*metadata_text_value
= NULL
;
1052 const char *metadata_text
= NULL
;
1053 static const char * const plugin_name
= "ctf";
1054 static const char * const comp_cls_name
= "fs";
1055 static const bt_component_class_type comp_cls_type
=
1056 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1057 const char *fail_reason
= NULL
;
1058 FILE *out_stream
= stdout
;
1060 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1061 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1064 BT_CLI_LOGE_APPEND_CAUSE(
1065 "Cannot find component class: plugin-name=\"%s\", "
1066 "comp-cls-name=\"%s\", comp-cls-type=%d",
1067 plugin_name
, comp_cls_name
,
1068 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1073 params
= bt_value_map_create();
1079 ret
= bt_value_map_insert_string_entry(params
, "path",
1080 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1086 ret
= query(cfg
, comp_cls
, "metadata-info",
1087 params
, &results
, &fail_reason
);
1092 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1094 if (!metadata_text_value
) {
1095 BT_CLI_LOGE_APPEND_CAUSE(
1096 "Cannot find `text` string value in the resulting metadata info object.");
1101 metadata_text
= bt_value_string_get(metadata_text_value
);
1103 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1105 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1108 BT_LOGE_ERRNO("Cannot open file for writing",
1110 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1111 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1113 "Cannot open file for writing: path=\"%s\"",
1114 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1120 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1122 BT_CLI_LOGE_APPEND_CAUSE(
1123 "Cannot write whole metadata text to output stream: "
1133 BT_CLI_LOGE_APPEND_CAUSE(
1134 "Failed to query `metadata-info` object: %s", fail_reason
);
1137 bt_value_put_ref(results
);
1138 bt_value_put_ref(params
);
1139 bt_component_class_put_ref(comp_cls
);
1141 if (out_stream
&& out_stream
!= stdout
) {
1142 int fclose_ret
= fclose(out_stream
);
1145 BT_LOGE_ERRNO("Cannot close file stream",
1147 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1155 char *instance_name
;
1159 struct trace_range
{
1160 uint64_t intersection_range_begin_ns
;
1161 uint64_t intersection_range_end_ns
;
1165 guint
port_id_hash(gconstpointer v
)
1167 const struct port_id
*id
= v
;
1169 BT_ASSERT(id
->instance_name
);
1170 BT_ASSERT(id
->port_name
);
1172 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1176 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1178 const struct port_id
*id1
= v1
;
1179 const struct port_id
*id2
= v2
;
1181 return strcmp(id1
->instance_name
, id2
->instance_name
) == 0 &&
1182 strcmp(id1
->port_name
, id2
->port_name
) == 0;
1186 void port_id_destroy(gpointer data
)
1188 struct port_id
*id
= data
;
1190 free(id
->instance_name
);
1191 free(id
->port_name
);
1196 void trace_range_destroy(gpointer data
)
1201 struct cmd_run_ctx
{
1203 GHashTable
*src_components
;
1206 GHashTable
*flt_components
;
1209 GHashTable
*sink_components
;
1215 struct bt_config
*cfg
;
1219 bool stream_intersection_mode
;
1222 * Association of struct port_id -> struct trace_range.
1224 GHashTable
*intersections
;
1227 /* Returns a timestamp of the form "(-)s.ns" */
1229 char *s_from_ns(int64_t ns
)
1234 int64_t ts_sec_abs
, ts_nsec_abs
;
1235 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1236 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1238 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1239 is_negative
= false;
1240 ts_sec_abs
= ts_sec
;
1241 ts_nsec_abs
= ts_nsec
;
1242 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1243 is_negative
= false;
1244 ts_sec_abs
= ts_sec
- 1;
1245 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1246 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1248 ts_sec_abs
= ts_sec
;
1249 ts_nsec_abs
= -ts_nsec
;
1250 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1252 ts_sec_abs
= -(ts_sec
+ 1);
1253 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1254 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1256 ts_sec_abs
= -ts_sec
;
1257 ts_nsec_abs
= ts_nsec
;
1258 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1260 ts_sec_abs
= -ts_sec
;
1261 ts_nsec_abs
= -ts_nsec
;
1264 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1265 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1273 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1274 struct cmd_run_ctx
*ctx
,
1275 const bt_component
*upstream_comp
,
1276 const bt_port_output
*out_upstream_port
,
1277 struct bt_config_connection
*cfg_conn
)
1279 typedef uint64_t (*input_port_count_func_t
)(void *);
1280 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1281 const void *, uint64_t);
1282 const bt_port
*upstream_port
=
1283 bt_port_output_as_port_const(out_upstream_port
);
1286 GQuark downstreamp_comp_name_quark
;
1287 void *downstream_comp
;
1288 uint64_t downstream_port_count
;
1290 input_port_count_func_t port_count_fn
;
1291 borrow_input_port_by_index_func_t port_by_index_fn
;
1292 bt_graph_connect_ports_status connect_ports_status
=
1293 BT_GRAPH_CONNECT_PORTS_STATUS_OK
;
1294 bool insert_trimmer
= false;
1295 bt_value
*trimmer_params
= NULL
;
1296 char *intersection_begin
= NULL
;
1297 char *intersection_end
= NULL
;
1298 const bt_component_filter
*trimmer
= NULL
;
1299 const bt_component_class_filter
*trimmer_class
= NULL
;
1300 const bt_port_input
*trimmer_input
= NULL
;
1301 const bt_port_output
*trimmer_output
= NULL
;
1303 if (ctx
->intersections
&&
1304 bt_component_get_class_type(upstream_comp
) ==
1305 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1306 struct trace_range
*range
;
1307 struct port_id port_id
= {
1308 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1309 .port_name
= (char *) bt_port_get_name(upstream_port
)
1312 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1316 range
= (struct trace_range
*) g_hash_table_lookup(
1317 ctx
->intersections
, &port_id
);
1319 bt_value_map_insert_entry_status insert_status
;
1321 intersection_begin
= s_from_ns(
1322 range
->intersection_range_begin_ns
);
1323 intersection_end
= s_from_ns(
1324 range
->intersection_range_end_ns
);
1325 if (!intersection_begin
|| !intersection_end
) {
1326 BT_CLI_LOGE_APPEND_CAUSE(
1327 "Cannot create trimmer argument timestamp string.");
1331 insert_trimmer
= true;
1332 trimmer_params
= bt_value_map_create();
1333 if (!trimmer_params
) {
1337 insert_status
= bt_value_map_insert_string_entry(
1338 trimmer_params
, "begin", intersection_begin
);
1339 if (insert_status
< 0) {
1342 insert_status
= bt_value_map_insert_string_entry(
1344 "end", intersection_end
);
1345 if (insert_status
< 0) {
1350 trimmer_class
= find_filter_component_class("utils", "trimmer");
1351 if (!trimmer_class
) {
1356 BT_LOGI("Connecting upstream port to the next available downstream port: "
1357 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1358 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1359 upstream_port
, bt_port_get_name(upstream_port
),
1360 cfg_conn
->downstream_comp_name
->str
,
1361 cfg_conn
->arg
->str
);
1362 downstreamp_comp_name_quark
= g_quark_from_string(
1363 cfg_conn
->downstream_comp_name
->str
);
1364 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1365 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1366 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1367 port_count_fn
= (input_port_count_func_t
)
1368 bt_component_filter_get_input_port_count
;
1369 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1370 bt_component_filter_borrow_input_port_by_index_const
;
1372 if (!downstream_comp
) {
1373 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1374 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1375 port_count_fn
= (input_port_count_func_t
)
1376 bt_component_sink_get_input_port_count
;
1377 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1378 bt_component_sink_borrow_input_port_by_index_const
;
1381 if (!downstream_comp
) {
1382 BT_CLI_LOGE_APPEND_CAUSE("Cannot find downstream component: "
1383 "comp-name=\"%s\", conn-arg=\"%s\"",
1384 cfg_conn
->downstream_comp_name
->str
,
1385 cfg_conn
->arg
->str
);
1389 downstream_port_count
= port_count_fn(downstream_comp
);
1391 for (i
= 0; i
< downstream_port_count
; i
++) {
1392 const bt_port_input
*in_downstream_port
=
1393 port_by_index_fn(downstream_comp
, i
);
1394 const bt_port
*downstream_port
=
1395 bt_port_input_as_port_const(in_downstream_port
);
1396 const char *upstream_port_name
;
1397 const char *downstream_port_name
;
1399 BT_ASSERT(downstream_port
);
1401 /* Skip port if it's already connected. */
1402 if (bt_port_is_connected(downstream_port
)) {
1403 BT_LOGI("Skipping downstream port: already connected: "
1404 "port-addr=%p, port-name=\"%s\"",
1406 bt_port_get_name(downstream_port
));
1410 downstream_port_name
= bt_port_get_name(downstream_port
);
1411 BT_ASSERT(downstream_port_name
);
1412 upstream_port_name
= bt_port_get_name(upstream_port
);
1413 BT_ASSERT(upstream_port_name
);
1415 if (!bt_common_star_glob_match(
1416 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1417 downstream_port_name
, SIZE_MAX
)) {
1421 if (insert_trimmer
) {
1423 * In order to insert the trimmer between the
1424 * two components that were being connected, we
1425 * create a connection configuration entry which
1426 * describes a connection from the trimmer's
1427 * output to the original input that was being
1430 * Hence, the creation of the trimmer will cause
1431 * the graph "new port" listener to establish
1432 * all downstream connections as its output port
1433 * is connected. We will then establish the
1434 * connection between the original upstream
1435 * source and the trimmer.
1437 char *trimmer_name
= NULL
;
1438 bt_graph_add_component_status add_comp_status
;
1440 ret
= asprintf(&trimmer_name
,
1441 "stream-intersection-trimmer-%s",
1442 upstream_port_name
);
1448 ctx
->connect_ports
= false;
1449 add_comp_status
= bt_graph_add_filter_component(
1450 ctx
->graph
, trimmer_class
, trimmer_name
,
1451 trimmer_params
, ctx
->cfg
->log_level
,
1454 if (add_comp_status
!=
1455 BT_GRAPH_ADD_COMPONENT_STATUS_OK
) {
1461 bt_component_filter_borrow_input_port_by_index_const(
1463 if (!trimmer_input
) {
1467 bt_component_filter_borrow_output_port_by_index_const(
1469 if (!trimmer_output
) {
1474 * Replace the current downstream port by the trimmer's
1477 in_downstream_port
= trimmer_input
;
1479 bt_port_input_as_port_const(in_downstream_port
);
1480 downstream_port_name
= bt_port_get_name(
1482 BT_ASSERT(downstream_port_name
);
1485 /* We have a winner! */
1486 connect_ports_status
= bt_graph_connect_ports(ctx
->graph
,
1487 out_upstream_port
, in_downstream_port
, NULL
);
1488 downstream_port
= NULL
;
1489 switch (connect_ports_status
) {
1490 case BT_GRAPH_CONNECT_PORTS_STATUS_OK
:
1493 BT_CLI_LOGE_APPEND_CAUSE(
1494 "Cannot create connection: graph refuses to connect ports: "
1495 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1496 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1497 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1498 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1500 upstream_comp
, bt_component_get_name(upstream_comp
),
1501 upstream_port
, bt_port_get_name(upstream_port
),
1502 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1503 downstream_port
, downstream_port_name
,
1504 cfg_conn
->arg
->str
);
1508 BT_LOGI("Connected component ports: "
1509 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1510 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1511 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1512 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1514 upstream_comp
, bt_component_get_name(upstream_comp
),
1515 upstream_port
, bt_port_get_name(upstream_port
),
1516 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1517 downstream_port
, downstream_port_name
,
1518 cfg_conn
->arg
->str
);
1520 if (insert_trimmer
) {
1522 * The first connection, from the source to the trimmer,
1523 * has been done. We now connect the trimmer to the
1524 * original downstream port.
1526 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1528 bt_component_filter_as_component_const(trimmer
),
1529 trimmer_output
, cfg_conn
);
1533 ctx
->connect_ports
= true;
1537 * We found a matching downstream port: the search is
1543 /* No downstream port found */
1544 BT_CLI_LOGE_APPEND_CAUSE(
1545 "Cannot create connection: cannot find a matching downstream port for upstream port: "
1546 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1547 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1548 upstream_port
, bt_port_get_name(upstream_port
),
1549 cfg_conn
->downstream_comp_name
->str
,
1550 cfg_conn
->arg
->str
);
1556 free(intersection_begin
);
1557 free(intersection_end
);
1558 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1559 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1560 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1565 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1566 const bt_port_output
*upstream_port
)
1569 const char *upstream_port_name
;
1570 const char *upstream_comp_name
;
1571 const bt_component
*upstream_comp
= NULL
;
1575 BT_ASSERT(upstream_port
);
1576 upstream_port_name
= bt_port_get_name(
1577 bt_port_output_as_port_const(upstream_port
));
1578 BT_ASSERT(upstream_port_name
);
1579 upstream_comp
= bt_port_borrow_component_const(
1580 bt_port_output_as_port_const(upstream_port
));
1581 BT_ASSERT(upstream_comp
);
1582 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1583 BT_ASSERT(upstream_comp_name
);
1584 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1585 "port-addr=%p, port-name=\"%s\"",
1586 upstream_comp
, upstream_comp_name
,
1587 upstream_port
, upstream_port_name
);
1589 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1590 struct bt_config_connection
*cfg_conn
=
1592 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1594 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1595 upstream_comp_name
)) {
1599 if (!bt_common_star_glob_match(
1600 cfg_conn
->upstream_port_glob
->str
,
1601 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1605 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1606 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1608 BT_CLI_LOGE_APPEND_CAUSE(
1609 "Cannot connect upstream port: "
1610 "port-addr=%p, port-name=\"%s\"",
1612 upstream_port_name
);
1618 BT_CLI_LOGE_APPEND_CAUSE(
1619 "Cannot connect upstream port: port does not match any connection argument: "
1620 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1621 upstream_port_name
);
1631 bt_graph_listener_func_status
1632 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1633 const bt_port_output
*out_port
)
1635 const bt_component
*comp
;
1636 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1637 bt_graph_listener_func_status ret
=
1638 BT_GRAPH_LISTENER_FUNC_STATUS_OK
;
1640 comp
= bt_port_borrow_component_const(port
);
1641 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1642 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1643 comp
, comp
? bt_component_get_name(comp
) : "",
1644 port
, bt_port_get_name(port
));
1646 if (!ctx
->connect_ports
) {
1652 if (bt_port_is_connected(port
)) {
1653 BT_LOGW_STR("Port is already connected.");
1657 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
1658 BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port.");
1659 ret
= BT_GRAPH_LISTENER_FUNC_STATUS_ERROR
;
1668 bt_graph_listener_func_status
graph_source_output_port_added_listener(
1669 const bt_component_source
*component
,
1670 const bt_port_output
*port
, void *data
)
1672 return graph_output_port_added_listener(data
, port
);
1676 bt_graph_listener_func_status
graph_filter_output_port_added_listener(
1677 const bt_component_filter
*component
,
1678 const bt_port_output
*port
, void *data
)
1680 return graph_output_port_added_listener(data
, port
);
1684 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1690 if (ctx
->src_components
) {
1691 g_hash_table_destroy(ctx
->src_components
);
1692 ctx
->src_components
= NULL
;
1695 if (ctx
->flt_components
) {
1696 g_hash_table_destroy(ctx
->flt_components
);
1697 ctx
->flt_components
= NULL
;
1700 if (ctx
->sink_components
) {
1701 g_hash_table_destroy(ctx
->sink_components
);
1702 ctx
->sink_components
= NULL
;
1705 if (ctx
->intersections
) {
1706 g_hash_table_destroy(ctx
->intersections
);
1707 ctx
->intersections
= NULL
;
1710 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
1715 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1718 bt_graph_add_listener_status add_listener_status
;
1721 ctx
->connect_ports
= false;
1722 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
1723 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1724 if (!ctx
->src_components
) {
1728 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
1729 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1730 if (!ctx
->flt_components
) {
1734 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
1735 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1736 if (!ctx
->sink_components
) {
1740 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1741 ctx
->stream_intersection_mode
= true;
1742 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1743 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1744 if (!ctx
->intersections
) {
1749 ctx
->graph
= bt_graph_create();
1754 bt_graph_add_interrupter(ctx
->graph
, the_interrupter
);
1755 add_listener_status
= bt_graph_add_source_component_output_port_added_listener(
1756 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
1758 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1759 BT_CLI_LOGE_APPEND_CAUSE(
1760 "Cannot add \"port added\" listener to graph.");
1764 add_listener_status
= bt_graph_add_filter_component_output_port_added_listener(
1765 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
1767 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1768 BT_CLI_LOGE_APPEND_CAUSE(
1769 "Cannot add \"port added\" listener to graph.");
1776 cmd_run_ctx_destroy(ctx
);
1784 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
1785 struct bt_config_component
*cfg_comp
,
1786 const bt_component_class_source
*src_comp_cls
)
1790 int64_t trace_count
;
1791 const char *path
= NULL
;
1792 const bt_value
*query_result
= NULL
;
1793 const bt_value
*trace_info
= NULL
;
1794 const bt_value
*intersection_range
= NULL
;
1795 const bt_value
*intersection_begin
= NULL
;
1796 const bt_value
*intersection_end
= NULL
;
1797 const bt_value
*stream_infos
= NULL
;
1798 const bt_value
*stream_info
= NULL
;
1799 struct port_id
*port_id
= NULL
;
1800 struct trace_range
*trace_range
= NULL
;
1801 const char *fail_reason
= NULL
;
1802 const bt_component_class
*comp_cls
=
1803 bt_component_class_source_as_component_class_const(src_comp_cls
);
1805 ret
= query(ctx
->cfg
, comp_cls
, "babeltrace.trace-info",
1806 cfg_comp
->params
, &query_result
,
1809 BT_LOGD("Component class does not support the `babeltrace.trace-info` query: %s: "
1810 "comp-class-name=\"%s\"", fail_reason
,
1811 bt_component_class_get_name(comp_cls
));
1816 BT_ASSERT(query_result
);
1818 if (!bt_value_is_array(query_result
)) {
1819 BT_LOGD("Unexpected format of `babeltrace.trace-info` query result: "
1820 "component-class-name=%s",
1821 bt_component_class_get_name(comp_cls
));
1826 trace_count
= bt_value_array_get_size(query_result
);
1827 if (trace_count
< 0) {
1832 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
1834 uint64_t stream_idx
;
1835 int64_t stream_count
;
1837 trace_info
= bt_value_array_borrow_element_by_index_const(
1838 query_result
, trace_idx
);
1839 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
1841 BT_LOGD_STR("Cannot retrieve trace from query result.");
1845 intersection_range
= bt_value_map_borrow_entry_value_const(
1846 trace_info
, "intersection-range-ns");
1847 if (!intersection_range
) {
1849 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
1853 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
1855 if (!intersection_begin
) {
1857 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
1861 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
1863 if (!intersection_end
) {
1865 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
1869 begin
= bt_value_integer_signed_get(intersection_begin
);
1870 end
= bt_value_integer_signed_get(intersection_end
);
1872 if (begin
< 0 || end
< 0 || end
< begin
) {
1873 BT_CLI_LOGE_APPEND_CAUSE(
1874 "Invalid trace stream intersection values: "
1875 "intersection-range-ns:begin=%" PRId64
1876 ", intersection-range-ns:end=%" PRId64
,
1882 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
1884 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
1886 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
1890 stream_count
= bt_value_array_get_size(stream_infos
);
1891 if (stream_count
< 0) {
1896 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
1897 const bt_value
*port_name
;
1899 port_id
= g_new0(struct port_id
, 1);
1902 BT_CLI_LOGE_APPEND_CAUSE(
1903 "Cannot allocate memory for port_id structure.");
1906 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
1907 if (!port_id
->instance_name
) {
1909 BT_CLI_LOGE_APPEND_CAUSE(
1910 "Cannot allocate memory for port_id component instance name.");
1914 trace_range
= g_new0(struct trace_range
, 1);
1917 BT_CLI_LOGE_APPEND_CAUSE(
1918 "Cannot allocate memory for trace_range structure.");
1921 trace_range
->intersection_range_begin_ns
= begin
;
1922 trace_range
->intersection_range_end_ns
= end
;
1924 stream_info
= bt_value_array_borrow_element_by_index_const(
1925 stream_infos
, stream_idx
);
1926 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
1928 BT_CLI_LOGE_APPEND_CAUSE(
1929 "Cannot retrieve stream informations from trace in query result.");
1933 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
1934 if (!port_name
|| !bt_value_is_string(port_name
)) {
1936 BT_CLI_LOGE_APPEND_CAUSE(
1937 "Cannot retrieve port name in query result.");
1941 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
1942 if (!port_id
->port_name
) {
1944 BT_CLI_LOGE_APPEND_CAUSE(
1945 "Cannot allocate memory for port_id port_name.");
1949 BT_LOGD("Inserting stream intersection ");
1951 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
1961 BT_CLI_LOGE_APPEND_CAUSE(
1962 "Cannot determine stream intersection of trace: path=\"%s\"",
1963 path
? path
: "(unknown)");
1966 bt_value_put_ref(query_result
);
1968 g_free(trace_range
);
1973 int cmd_run_ctx_create_components_from_config_components(
1974 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
1977 const void *comp_cls
= NULL
;
1978 const void *comp
= NULL
;
1981 for (i
= 0; i
< cfg_components
->len
; i
++) {
1982 struct bt_config_component
*cfg_comp
=
1983 g_ptr_array_index(cfg_components
, i
);
1986 switch (cfg_comp
->type
) {
1987 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1988 comp_cls
= find_source_component_class(
1989 cfg_comp
->plugin_name
->str
,
1990 cfg_comp
->comp_cls_name
->str
);
1992 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1993 comp_cls
= find_filter_component_class(
1994 cfg_comp
->plugin_name
->str
,
1995 cfg_comp
->comp_cls_name
->str
);
1997 case BT_COMPONENT_CLASS_TYPE_SINK
:
1998 comp_cls
= find_sink_component_class(
1999 cfg_comp
->plugin_name
->str
,
2000 cfg_comp
->comp_cls_name
->str
);
2007 BT_CLI_LOGE_APPEND_CAUSE(
2008 "Cannot find component class: plugin-name=\"%s\", "
2009 "comp-cls-name=\"%s\", comp-cls-type=%d",
2010 cfg_comp
->plugin_name
->str
,
2011 cfg_comp
->comp_cls_name
->str
,
2016 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_TRACE
);
2018 switch (cfg_comp
->type
) {
2019 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2020 ret
= bt_graph_add_source_component(ctx
->graph
,
2021 comp_cls
, cfg_comp
->instance_name
->str
,
2022 cfg_comp
->params
, cfg_comp
->log_level
,
2025 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2026 ret
= bt_graph_add_filter_component(ctx
->graph
,
2027 comp_cls
, cfg_comp
->instance_name
->str
,
2028 cfg_comp
->params
, cfg_comp
->log_level
,
2031 case BT_COMPONENT_CLASS_TYPE_SINK
:
2032 ret
= bt_graph_add_sink_component(ctx
->graph
,
2033 comp_cls
, cfg_comp
->instance_name
->str
,
2034 cfg_comp
->params
, cfg_comp
->log_level
,
2042 BT_CLI_LOGE_APPEND_CAUSE(
2043 "Cannot create component: plugin-name=\"%s\", "
2044 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2046 cfg_comp
->plugin_name
->str
,
2047 cfg_comp
->comp_cls_name
->str
,
2048 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2052 if (ctx
->stream_intersection_mode
&&
2053 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2054 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2060 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2061 comp
, cfg_comp
->instance_name
->str
);
2062 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2063 BT_ASSERT(quark
> 0);
2065 switch (cfg_comp
->type
) {
2066 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2067 g_hash_table_insert(ctx
->src_components
,
2068 GUINT_TO_POINTER(quark
), (void *) comp
);
2070 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2071 g_hash_table_insert(ctx
->flt_components
,
2072 GUINT_TO_POINTER(quark
), (void *) comp
);
2074 case BT_COMPONENT_CLASS_TYPE_SINK
:
2075 g_hash_table_insert(ctx
->sink_components
,
2076 GUINT_TO_POINTER(quark
), (void *) comp
);
2083 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2092 bt_object_put_ref(comp
);
2093 bt_object_put_ref(comp_cls
);
2098 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2103 * Make sure that, during this phase, our graph's "port added"
2104 * listener does not connect ports while we are creating the
2105 * components because we have a special, initial phase for
2108 ctx
->connect_ports
= false;
2110 ret
= cmd_run_ctx_create_components_from_config_components(
2111 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2117 ret
= cmd_run_ctx_create_components_from_config_components(
2118 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2124 ret
= cmd_run_ctx_create_components_from_config_components(
2125 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2135 typedef uint64_t (*output_port_count_func_t
)(const void *);
2136 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2137 const void *, uint64_t);
2140 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2141 void *comp
, output_port_count_func_t port_count_fn
,
2142 borrow_output_port_by_index_func_t port_by_index_fn
)
2148 count
= port_count_fn(comp
);
2150 for (i
= 0; i
< count
; i
++) {
2151 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2153 BT_ASSERT(upstream_port
);
2154 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2165 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2168 GHashTableIter iter
;
2169 gpointer g_name_quark
, g_comp
;
2171 ctx
->connect_ports
= true;
2172 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2174 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2175 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2176 (output_port_count_func_t
)
2177 bt_component_source_get_output_port_count
,
2178 (borrow_output_port_by_index_func_t
)
2179 bt_component_source_borrow_output_port_by_index_const
);
2185 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2187 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2188 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2189 (output_port_count_func_t
)
2190 bt_component_filter_get_output_port_count
,
2191 (borrow_output_port_by_index_func_t
)
2192 bt_component_filter_borrow_output_port_by_index_const
);
2203 int cmd_run(struct bt_config
*cfg
)
2206 struct cmd_run_ctx ctx
= { 0 };
2208 /* Initialize the command's context and the graph object */
2209 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2210 BT_CLI_LOGE_APPEND_CAUSE(
2211 "Cannot initialize the command's context.");
2215 if (bt_interrupter_is_set(the_interrupter
)) {
2216 BT_CLI_LOGW_APPEND_CAUSE(
2217 "Interrupted by user before creating components.");
2221 BT_LOGI_STR("Creating components.");
2223 /* Create the requested component instances */
2224 if (cmd_run_ctx_create_components(&ctx
)) {
2225 BT_CLI_LOGE_APPEND_CAUSE("Cannot create components.");
2229 if (bt_interrupter_is_set(the_interrupter
)) {
2230 BT_CLI_LOGW_APPEND_CAUSE(
2231 "Interrupted by user before connecting components.");
2235 BT_LOGI_STR("Connecting components.");
2237 /* Connect the initially visible component ports */
2238 if (cmd_run_ctx_connect_ports(&ctx
)) {
2239 BT_CLI_LOGE_APPEND_CAUSE(
2240 "Cannot connect initial component ports.");
2244 BT_LOGI_STR("Running the graph.");
2248 bt_graph_run_status run_status
= bt_graph_run(ctx
.graph
);
2251 * Reset console in case something messed with console
2252 * codes during the graph's execution.
2254 printf("%s", bt_common_color_reset());
2256 fprintf(stderr
, "%s", bt_common_color_reset());
2257 BT_LOGT("bt_graph_run() returned: status=%s",
2258 bt_common_func_status_string(run_status
));
2260 switch (run_status
) {
2261 case BT_GRAPH_RUN_STATUS_OK
:
2263 case BT_GRAPH_RUN_STATUS_AGAIN
:
2264 if (bt_interrupter_is_set(the_interrupter
)) {
2265 BT_CLI_LOGW_APPEND_CAUSE(
2266 "Graph was interrupted by user.");
2270 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2271 BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: "
2273 cfg
->cmd_data
.run
.retry_duration_us
);
2275 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2276 if (bt_interrupter_is_set(the_interrupter
)) {
2277 BT_CLI_LOGW_APPEND_CAUSE(
2278 "Graph was interrupted by user.");
2284 case BT_GRAPH_RUN_STATUS_END
:
2287 if (bt_interrupter_is_set(the_interrupter
)) {
2288 BT_CLI_LOGW_APPEND_CAUSE(
2289 "Graph was interrupted by user and failed: "
2291 bt_common_func_status_string(run_status
));
2295 BT_CLI_LOGE_APPEND_CAUSE(
2296 "Graph failed to complete successfully");
2309 cmd_run_ctx_destroy(&ctx
);
2314 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2316 const char *env_clash
;
2318 if (!cfg
->command_name
) {
2322 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2323 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2327 if (g_file_test(cfg
->command_name
,
2328 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2329 _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION
, __FILE__
, __LINE__
,
2330 BT_LOG_WARNING
, BT_LOG_TAG
,
2331 "The `%s` command was executed. "
2332 "If you meant to convert a trace located in "
2333 "the local `%s` directory, please use:\n\n"
2334 " babeltrace2 convert %s [OPTIONS]",
2335 cfg
->command_name
, cfg
->command_name
,
2341 void init_log_level(void)
2343 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2347 void set_auto_log_levels(struct bt_config
*cfg
)
2349 const char **env_var_name
;
2352 * Override the configuration's default log level if
2353 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2354 * are found for backward compatibility with legacy Babetrace 1.
2356 if (getenv("BABELTRACE_DEBUG") &&
2357 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2358 cfg
->log_level
= BT_LOG_TRACE
;
2359 } else if (getenv("BABELTRACE_VERBOSE") &&
2360 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2361 cfg
->log_level
= BT_LOG_INFO
;
2365 * Set log levels according to --debug or --verbose. For
2366 * backward compatibility, --debug is more verbose than
2369 * --verbose: INFO log level
2370 * --debug: TRACE log level (includes DEBUG, which is
2371 * is less verbose than TRACE in the internal
2372 * logging framework)
2374 if (!getenv("LIBBABELTRACE2_INIT_LOG_LEVEL")) {
2376 bt_logging_set_global_level(BT_LOG_INFO
);
2377 } else if (cfg
->debug
) {
2378 bt_logging_set_global_level(BT_LOG_TRACE
);
2381 * Set library's default log level if not
2382 * explicitly specified.
2384 bt_logging_set_global_level(cfg
->log_level
);
2388 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2390 bt_cli_log_level
= BT_LOG_INFO
;
2391 } else if (cfg
->debug
) {
2392 bt_cli_log_level
= BT_LOG_TRACE
;
2395 * Set CLI's default log level if not explicitly
2398 bt_cli_log_level
= cfg
->log_level
;
2402 env_var_name
= log_level_env_var_names
;
2404 while (*env_var_name
) {
2405 if (!getenv(*env_var_name
)) {
2407 g_setenv(*env_var_name
, "INFO", 1);
2408 } else if (cfg
->debug
) {
2409 g_setenv(*env_var_name
, "TRACE", 1);
2411 char val
[2] = { 0 };
2414 * Set module's default log level if not
2415 * explicitly specified.
2417 val
[0] = bt_log_get_letter_from_level(
2419 g_setenv(*env_var_name
, val
, 1);
2428 void print_error_causes(void)
2430 const bt_error
*error
= bt_current_thread_take_error();
2432 GString
*folded
= NULL
;
2433 unsigned int columns
;
2435 if (!error
|| bt_error_get_cause_count(error
) == 0) {
2436 fprintf(stderr
, "%s%sUnknown command-line error.%s\n",
2437 bt_common_color_bold(), bt_common_color_fg_red(),
2438 bt_common_color_reset());
2442 /* Try to get terminal width to fold the error cause messages */
2443 if (bt_common_get_term_size(&columns
, NULL
) < 0) {
2444 /* Width not found: default to 80 */
2449 * This helps visually separate the error causes from the last
2450 * logging statement.
2452 fprintf(stderr
, "\n");
2454 /* Reverse order: deepest (root) cause printed at the end */
2455 for (i
= bt_error_get_cause_count(error
) - 1; i
>= 0; i
--) {
2456 const bt_error_cause
*cause
=
2457 bt_error_borrow_cause_by_index(error
, (uint64_t) i
);
2458 const char *prefix_fmt
=
2459 i
== bt_error_get_cause_count(error
) - 1 ?
2460 "%s%sERROR%s: " : "%s%sCAUSED BY%s ";
2463 fprintf(stderr
, prefix_fmt
,
2464 bt_common_color_bold(), bt_common_color_fg_red(),
2465 bt_common_color_reset());
2467 /* Print actor name */
2468 fprintf(stderr
, "[");
2469 switch (bt_error_cause_get_actor_type(cause
)) {
2470 case BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN
:
2471 fprintf(stderr
, "%s%s%s",
2472 bt_common_color_bold(),
2473 bt_error_cause_get_module_name(cause
),
2474 bt_common_color_reset());
2476 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
:
2477 fprintf(stderr
, "%s%s%s: ",
2478 bt_common_color_bold(),
2479 bt_error_cause_component_actor_get_component_name(cause
),
2480 bt_common_color_reset());
2481 print_plugin_comp_cls_opt(stderr
,
2482 bt_error_cause_component_actor_get_plugin_name(cause
),
2483 bt_error_cause_component_actor_get_component_class_name(cause
),
2484 bt_error_cause_component_actor_get_component_class_type(cause
));
2486 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
:
2487 print_plugin_comp_cls_opt(stderr
,
2488 bt_error_cause_component_class_actor_get_plugin_name(cause
),
2489 bt_error_cause_component_class_actor_get_component_class_name(cause
),
2490 bt_error_cause_component_class_actor_get_component_class_type(cause
));
2492 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
:
2493 fprintf(stderr
, "%s%s%s (%s%s%s): ",
2494 bt_common_color_bold(),
2495 bt_error_cause_message_iterator_actor_get_component_name(cause
),
2496 bt_common_color_reset(),
2497 bt_common_color_bold(),
2498 bt_error_cause_message_iterator_actor_get_component_output_port_name(cause
),
2499 bt_common_color_reset());
2500 print_plugin_comp_cls_opt(stderr
,
2501 bt_error_cause_message_iterator_actor_get_plugin_name(cause
),
2502 bt_error_cause_message_iterator_actor_get_component_class_name(cause
),
2503 bt_error_cause_message_iterator_actor_get_component_class_type(cause
));
2509 /* Print file name and line number */
2510 fprintf(stderr
, "] (%s%s%s%s:%s%" PRIu64
"%s)\n",
2511 bt_common_color_bold(),
2512 bt_common_color_fg_magenta(),
2513 bt_error_cause_get_file_name(cause
),
2514 bt_common_color_reset(),
2515 bt_common_color_fg_green(),
2516 bt_error_cause_get_line_number(cause
),
2517 bt_common_color_reset());
2520 folded
= bt_common_fold(bt_error_cause_get_message(cause
),
2523 BT_LOGE_STR("Could not fold string.");
2524 fprintf(stderr
, "%s\n",
2525 bt_error_cause_get_message(cause
));
2529 fprintf(stderr
, "%s\n", folded
->str
);
2530 g_string_free(folded
, TRUE
);
2536 g_string_free(folded
, TRUE
);
2540 bt_error_release(error
);
2544 int main(int argc
, const char **argv
)
2548 struct bt_config
*cfg
;
2551 set_signal_handler();
2552 init_loaded_plugins();
2553 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2556 /* Quit without errors; typically usage/version */
2558 BT_LOGI_STR("Quitting without errors.");
2563 BT_CLI_LOGE_APPEND_CAUSE(
2564 "Command-line error: retcode=%d", retcode
);
2569 BT_CLI_LOGE_APPEND_CAUSE(
2570 "Failed to create a valid Babeltrace configuration.");
2575 set_auto_log_levels(cfg
);
2578 if (cfg
->command_needs_plugins
) {
2579 ret
= require_loaded_plugins(cfg
->plugin_paths
);
2581 BT_CLI_LOGE_APPEND_CAUSE(
2582 "Failed to load plugins: ret=%d", ret
);
2588 BT_ASSERT(!the_interrupter
);
2589 the_interrupter
= bt_interrupter_create();
2590 if (!the_interrupter
) {
2591 BT_CLI_LOGE_APPEND_CAUSE("Failed to create an interrupter object.");
2596 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2597 cfg
->command
, cfg
->command_name
);
2599 switch (cfg
->command
) {
2600 case BT_CONFIG_COMMAND_RUN
:
2603 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2604 ret
= cmd_list_plugins(cfg
);
2606 case BT_CONFIG_COMMAND_HELP
:
2607 ret
= cmd_help(cfg
);
2609 case BT_CONFIG_COMMAND_QUERY
:
2610 ret
= cmd_query(cfg
);
2612 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2613 ret
= cmd_print_ctf_metadata(cfg
);
2615 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2616 ret
= cmd_print_lttng_live_sessions(cfg
);
2619 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2623 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2624 cfg
->command
, cfg
->command_name
, ret
);
2625 warn_command_name_and_directory_clash(cfg
);
2626 retcode
= ret
? 1 : 0;
2629 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2630 fini_loaded_plugins();
2631 bt_interrupter_put_ref(the_interrupter
);
2634 print_error_causes();
2638 * Clear current thread's error in case there is one to avoid a
2641 bt_current_thread_clear_error();