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"
38 #include "babeltrace2-cfg.h"
39 #include "babeltrace2-cfg-cli-args.h"
40 #include "babeltrace2-cfg-cli-args-default.h"
41 #include "babeltrace2-plugins.h"
42 #include "babeltrace2-query.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 return cli_query(comp_cls
, obj
, params
, cfg
->log_level
,
119 the_interrupter
, user_result
, fail_reason
);
122 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
123 const bt_plugin
*, const char *);
126 const void *find_component_class_from_plugin(const char *plugin_name
,
127 const char *comp_class_name
,
128 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
130 const void *comp_class
= NULL
;
131 const bt_plugin
*plugin
;
133 BT_LOGI("Finding component class: plugin-name=\"%s\", "
134 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
136 plugin
= find_loaded_plugin(plugin_name
);
141 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
142 bt_object_get_ref(comp_class
);
143 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
147 BT_LOGI("Found component class: plugin-name=\"%s\", "
148 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
150 BT_LOGI("Cannot find source component class: "
151 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
152 plugin_name
, comp_class_name
);
159 const bt_component_class_source
*find_source_component_class(
160 const char *plugin_name
, const char *comp_class_name
)
162 return (const void *) find_component_class_from_plugin(
163 plugin_name
, comp_class_name
,
164 (plugin_borrow_comp_cls_func_t
)
165 bt_plugin_borrow_source_component_class_by_name_const
);
169 const bt_component_class_filter
*find_filter_component_class(
170 const char *plugin_name
, const char *comp_class_name
)
172 return (const void *) find_component_class_from_plugin(
173 plugin_name
, comp_class_name
,
174 (plugin_borrow_comp_cls_func_t
)
175 bt_plugin_borrow_filter_component_class_by_name_const
);
179 const bt_component_class_sink
*find_sink_component_class(
180 const char *plugin_name
, const char *comp_class_name
)
182 return (const void *) find_component_class_from_plugin(plugin_name
,
184 (plugin_borrow_comp_cls_func_t
)
185 bt_plugin_borrow_sink_component_class_by_name_const
);
189 const bt_component_class
*find_component_class(const char *plugin_name
,
190 const char *comp_class_name
,
191 bt_component_class_type comp_class_type
)
193 const bt_component_class
*comp_cls
= NULL
;
195 switch (comp_class_type
) {
196 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
197 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
199 case BT_COMPONENT_CLASS_TYPE_FILTER
:
200 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
202 case BT_COMPONENT_CLASS_TYPE_SINK
:
203 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
213 void print_indent(FILE *fp
, size_t indent
)
217 for (i
= 0; i
< indent
; i
++) {
223 const char *component_type_str(bt_component_class_type type
)
226 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
228 case BT_COMPONENT_CLASS_TYPE_SINK
:
230 case BT_COMPONENT_CLASS_TYPE_FILTER
:
238 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
239 const char *comp_cls_name
, bt_component_class_type type
)
241 GString
*shell_plugin_name
= NULL
;
242 GString
*shell_comp_cls_name
= NULL
;
245 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
246 if (!shell_plugin_name
) {
251 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
252 if (!shell_comp_cls_name
) {
256 fprintf(fh
, "'%s%s%s%s",
257 bt_common_color_bold(),
258 bt_common_color_fg_cyan(),
259 component_type_str(type
),
260 bt_common_color_fg_default());
262 if (shell_plugin_name
) {
263 fprintf(fh
, ".%s%s%s",
264 bt_common_color_fg_blue(),
265 shell_plugin_name
->str
,
266 bt_common_color_fg_default());
269 fprintf(fh
, ".%s%s%s'",
270 bt_common_color_fg_yellow(),
271 shell_comp_cls_name
->str
,
272 bt_common_color_reset());
275 if (shell_plugin_name
) {
276 g_string_free(shell_plugin_name
, TRUE
);
279 if (shell_comp_cls_name
) {
280 g_string_free(shell_comp_cls_name
, TRUE
);
285 void print_value(FILE *, const bt_value
*, size_t);
288 void print_value_rec(FILE *, const bt_value
*, size_t);
290 struct print_map_value_data
{
296 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
299 struct print_map_value_data
*print_map_value_data
= data
;
301 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
302 fprintf(print_map_value_data
->fp
, "%s: ", key
);
305 if (bt_value_is_array(object
) &&
306 bt_value_array_is_empty(object
)) {
307 fprintf(print_map_value_data
->fp
, "[ ]\n");
311 if (bt_value_is_map(object
) &&
312 bt_value_map_is_empty(object
)) {
313 fprintf(print_map_value_data
->fp
, "{ }\n");
317 if (bt_value_is_array(object
) ||
318 bt_value_is_map(object
)) {
319 fprintf(print_map_value_data
->fp
, "\n");
322 print_value_rec(print_map_value_data
->fp
, object
,
323 print_map_value_data
->indent
+ 2);
328 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
342 switch (bt_value_get_type(value
)) {
343 case BT_VALUE_TYPE_NULL
:
344 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
345 bt_common_color_reset());
347 case BT_VALUE_TYPE_BOOL
:
348 bool_val
= bt_value_bool_get(value
);
349 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
350 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
351 bt_common_color_reset());
353 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
354 uint_val
= bt_value_integer_unsigned_get(value
);
355 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
356 bt_common_color_fg_red(), uint_val
,
357 bt_common_color_reset());
359 case BT_VALUE_TYPE_SIGNED_INTEGER
:
360 int_val
= bt_value_integer_signed_get(value
);
361 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
362 bt_common_color_fg_red(), int_val
,
363 bt_common_color_reset());
365 case BT_VALUE_TYPE_REAL
:
366 dbl_val
= bt_value_real_get(value
);
367 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
368 bt_common_color_fg_red(), dbl_val
,
369 bt_common_color_reset());
371 case BT_VALUE_TYPE_STRING
:
372 str_val
= bt_value_string_get(value
);
373 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
374 bt_common_color_fg_green(), str_val
,
375 bt_common_color_reset());
377 case BT_VALUE_TYPE_ARRAY
:
378 size
= bt_value_array_get_size(value
);
384 print_indent(fp
, indent
);
385 fprintf(fp
, "[ ]\n");
389 for (i
= 0; i
< size
; i
++) {
390 const bt_value
*element
=
391 bt_value_array_borrow_element_by_index_const(
397 print_indent(fp
, indent
);
400 if (bt_value_is_array(element
) &&
401 bt_value_array_is_empty(element
)) {
402 fprintf(fp
, "[ ]\n");
406 if (bt_value_is_map(element
) &&
407 bt_value_map_is_empty(element
)) {
408 fprintf(fp
, "{ }\n");
412 if (bt_value_is_array(element
) ||
413 bt_value_is_map(element
)) {
417 print_value_rec(fp
, element
, indent
+ 2);
420 case BT_VALUE_TYPE_MAP
:
422 struct print_map_value_data data
= {
427 if (bt_value_map_is_empty(value
)) {
428 print_indent(fp
, indent
);
429 fprintf(fp
, "{ }\n");
433 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
442 BT_LOGE("Error printing value of type %s.",
443 bt_common_value_type_string(bt_value_get_type(value
)));
447 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
449 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
450 print_indent(fp
, indent
);
453 print_value_rec(fp
, value
, indent
);
457 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
459 fprintf(stderr
, " ");
460 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
461 bt_config_component
->comp_cls_name
->str
,
462 bt_config_component
->type
);
463 fprintf(stderr
, ":\n");
465 if (bt_config_component
->instance_name
->len
> 0) {
466 fprintf(stderr
, " Name: %s\n",
467 bt_config_component
->instance_name
->str
);
470 fprintf(stderr
, " Parameters:\n");
471 print_value(stderr
, bt_config_component
->params
, 8);
475 void print_bt_config_components(GPtrArray
*array
)
479 for (i
= 0; i
< array
->len
; i
++) {
480 struct bt_config_component
*cfg_component
=
481 bt_config_get_component(array
, i
);
482 print_bt_config_component(cfg_component
);
483 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
488 void print_plugin_paths(const bt_value
*plugin_paths
)
490 fprintf(stderr
, " Plugin paths:\n");
491 print_value(stderr
, plugin_paths
, 4);
495 void print_cfg_run(struct bt_config
*cfg
)
499 print_plugin_paths(cfg
->plugin_paths
);
500 fprintf(stderr
, " Source component instances:\n");
501 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
503 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
504 fprintf(stderr
, " Filter component instances:\n");
505 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
508 fprintf(stderr
, " Sink component instances:\n");
509 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
510 fprintf(stderr
, " Connections:\n");
512 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
513 struct bt_config_connection
*cfg_connection
=
514 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
517 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
518 cfg_connection
->upstream_comp_name
->str
,
519 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
520 cfg_connection
->upstream_port_glob
->str
,
521 cfg_connection
->downstream_comp_name
->str
,
522 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
523 cfg_connection
->downstream_port_glob
->str
);
528 void print_cfg_list_plugins(struct bt_config
*cfg
)
530 print_plugin_paths(cfg
->plugin_paths
);
534 void print_cfg_help(struct bt_config
*cfg
)
536 print_plugin_paths(cfg
->plugin_paths
);
540 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
542 print_plugin_paths(cfg
->plugin_paths
);
543 fprintf(stderr
, " Path: %s\n",
544 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
548 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
550 print_plugin_paths(cfg
->plugin_paths
);
551 fprintf(stderr
, " URL: %s\n",
552 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
556 void print_cfg_query(struct bt_config
*cfg
)
558 print_plugin_paths(cfg
->plugin_paths
);
559 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
560 fprintf(stderr
, " Component class:\n");
561 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
565 void print_cfg(struct bt_config
*cfg
)
567 if (!BT_LOG_ON_INFO
) {
571 BT_LOGI_STR("CLI configuration:");
572 BT_LOGI(" Debug mode: %s\n", cfg
->debug
? "yes" : "no");
573 BT_LOGI(" Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
575 switch (cfg
->command
) {
576 case BT_CONFIG_COMMAND_RUN
:
579 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
580 print_cfg_list_plugins(cfg
);
582 case BT_CONFIG_COMMAND_HELP
:
585 case BT_CONFIG_COMMAND_QUERY
:
586 print_cfg_query(cfg
);
588 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
589 print_cfg_print_ctf_metadata(cfg
);
591 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
592 print_cfg_print_lttng_live_sessions(cfg
);
600 void print_plugin_info(const bt_plugin
*plugin
)
602 unsigned int major
, minor
, patch
;
604 bt_property_availability version_avail
;
605 const char *plugin_name
;
609 const char *plugin_description
;
611 plugin_name
= bt_plugin_get_name(plugin
);
612 path
= bt_plugin_get_path(plugin
);
613 author
= bt_plugin_get_author(plugin
);
614 license
= bt_plugin_get_license(plugin
);
615 plugin_description
= bt_plugin_get_description(plugin
);
616 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
618 printf("%s%s%s%s:\n", bt_common_color_bold(),
619 bt_common_color_fg_blue(), plugin_name
,
620 bt_common_color_reset());
622 printf(" %sPath%s: %s\n", bt_common_color_bold(),
623 bt_common_color_reset(), path
);
628 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
629 printf(" %sVersion%s: %u.%u.%u",
630 bt_common_color_bold(), bt_common_color_reset(),
631 major
, minor
, patch
);
640 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
641 bt_common_color_reset(),
642 plugin_description
? plugin_description
: "(None)");
643 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
644 bt_common_color_reset(), author
? author
: "(Unknown)");
645 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
646 bt_common_color_reset(),
647 license
? license
: "(Unknown)");
651 int cmd_query(struct bt_config
*cfg
)
654 const bt_component_class
*comp_cls
= NULL
;
655 const bt_value
*results
= NULL
;
656 const char *fail_reason
= NULL
;
658 comp_cls
= find_component_class(
659 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
660 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
661 cfg
->cmd_data
.query
.cfg_component
->type
);
663 BT_CLI_LOGE_APPEND_CAUSE(
664 "Cannot find component class: plugin-name=\"%s\", "
665 "comp-cls-name=\"%s\", comp-cls-type=%d",
666 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
667 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
668 cfg
->cmd_data
.query
.cfg_component
->type
);
673 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
674 cfg
->cmd_data
.query
.cfg_component
->params
,
675 &results
, &fail_reason
);
680 print_value(stdout
, results
, 0);
684 BT_CLI_LOGE_APPEND_CAUSE(
685 "Failed to query component class: %s: plugin-name=\"%s\", "
686 "comp-cls-name=\"%s\", comp-cls-type=%d "
687 "object=\"%s\"", fail_reason
,
688 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
689 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
690 cfg
->cmd_data
.query
.cfg_component
->type
,
691 cfg
->cmd_data
.query
.object
->str
);
695 bt_component_class_put_ref(comp_cls
);
696 bt_value_put_ref(results
);
701 void print_component_class_help(const char *plugin_name
,
702 const bt_component_class
*comp_cls
)
704 const char *comp_class_name
=
705 bt_component_class_get_name(comp_cls
);
706 const char *comp_class_description
=
707 bt_component_class_get_description(comp_cls
);
708 const char *comp_class_help
=
709 bt_component_class_get_help(comp_cls
);
710 bt_component_class_type type
=
711 bt_component_class_get_type(comp_cls
);
713 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
715 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
716 bt_common_color_reset(),
717 comp_class_description
? comp_class_description
: "(None)");
719 if (comp_class_help
) {
720 printf("\n%s\n", comp_class_help
);
725 int cmd_help(struct bt_config
*cfg
)
728 const bt_plugin
*plugin
= NULL
;
729 const bt_component_class
*needed_comp_cls
= NULL
;
731 plugin
= find_loaded_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
733 BT_CLI_LOGE_APPEND_CAUSE(
734 "Cannot find plugin: plugin-name=\"%s\"",
735 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
740 print_plugin_info(plugin
);
741 printf(" %sSource component classes%s: %d\n",
742 bt_common_color_bold(),
743 bt_common_color_reset(),
744 (int) bt_plugin_get_source_component_class_count(plugin
));
745 printf(" %sFilter component classes%s: %d\n",
746 bt_common_color_bold(),
747 bt_common_color_reset(),
748 (int) bt_plugin_get_filter_component_class_count(plugin
));
749 printf(" %sSink component classes%s: %d\n",
750 bt_common_color_bold(),
751 bt_common_color_reset(),
752 (int) bt_plugin_get_sink_component_class_count(plugin
));
754 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
755 /* Plugin help only */
759 needed_comp_cls
= find_component_class(
760 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
761 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
762 cfg
->cmd_data
.help
.cfg_component
->type
);
763 if (!needed_comp_cls
) {
764 BT_CLI_LOGE_APPEND_CAUSE(
765 "Cannot find component class: plugin-name=\"%s\", "
766 "comp-cls-name=\"%s\", comp-cls-type=%d",
767 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
768 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
769 cfg
->cmd_data
.help
.cfg_component
->type
);
775 print_component_class_help(
776 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
780 bt_component_class_put_ref(needed_comp_cls
);
781 bt_plugin_put_ref(plugin
);
785 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
787 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
790 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
791 const char *cc_type_name
, uint64_t count
,
792 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
793 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
798 printf(" %s%s component classes%s: (none)\n",
799 bt_common_color_bold(),
801 bt_common_color_reset());
804 printf(" %s%s component classes%s:\n",
805 bt_common_color_bold(),
807 bt_common_color_reset());
810 for (i
= 0; i
< count
; i
++) {
811 const bt_component_class
*comp_class
=
812 spec_comp_cls_borrow_comp_cls_func(
813 borrow_comp_cls_by_index_func(plugin
, i
));
814 const char *comp_class_name
=
815 bt_component_class_get_name(comp_class
);
816 const char *comp_class_description
=
817 bt_component_class_get_description(comp_class
);
818 bt_component_class_type type
=
819 bt_component_class_get_type(comp_class
);
822 print_plugin_comp_cls_opt(stdout
,
823 bt_plugin_get_name(plugin
), comp_class_name
,
826 if (comp_class_description
) {
827 printf(": %s", comp_class_description
);
838 int cmd_list_plugins(struct bt_config
*cfg
)
841 int plugins_count
, component_classes_count
= 0, i
;
843 printf("From the following plugin paths:\n\n");
844 print_value(stdout
, cfg
->plugin_paths
, 2);
846 plugins_count
= get_loaded_plugins_count();
847 if (plugins_count
== 0) {
848 printf("No plugins found.\n");
852 for (i
= 0; i
< plugins_count
; i
++) {
853 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
855 component_classes_count
+=
856 bt_plugin_get_source_component_class_count(plugin
) +
857 bt_plugin_get_filter_component_class_count(plugin
) +
858 bt_plugin_get_sink_component_class_count(plugin
);
861 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
862 bt_common_color_bold(),
863 component_classes_count
,
864 bt_common_color_reset(),
865 bt_common_color_bold(),
867 bt_common_color_reset());
869 for (i
= 0; i
< plugins_count
; i
++) {
870 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
873 print_plugin_info(plugin
);
874 cmd_list_plugins_print_component_classes(plugin
, "Source",
875 bt_plugin_get_source_component_class_count(plugin
),
876 (plugin_borrow_comp_cls_by_index_func_t
)
877 bt_plugin_borrow_source_component_class_by_index_const
,
878 (spec_comp_cls_borrow_comp_cls_func_t
)
879 bt_component_class_source_as_component_class
);
880 cmd_list_plugins_print_component_classes(plugin
, "Filter",
881 bt_plugin_get_filter_component_class_count(plugin
),
882 (plugin_borrow_comp_cls_by_index_func_t
)
883 bt_plugin_borrow_filter_component_class_by_index_const
,
884 (spec_comp_cls_borrow_comp_cls_func_t
)
885 bt_component_class_filter_as_component_class
);
886 cmd_list_plugins_print_component_classes(plugin
, "Sink",
887 bt_plugin_get_sink_component_class_count(plugin
),
888 (plugin_borrow_comp_cls_by_index_func_t
)
889 bt_plugin_borrow_sink_component_class_by_index_const
,
890 (spec_comp_cls_borrow_comp_cls_func_t
)
891 bt_component_class_sink_as_component_class
);
899 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
902 const bt_component_class
*comp_cls
= NULL
;
903 const bt_value
*results
= NULL
;
904 bt_value
*params
= NULL
;
905 const bt_value
*map
= NULL
;
906 const bt_value
*v
= NULL
;
907 static const char * const plugin_name
= "ctf";
908 static const char * const comp_cls_name
= "lttng-live";
909 static const bt_component_class_type comp_cls_type
=
910 BT_COMPONENT_CLASS_TYPE_SOURCE
;
911 int64_t array_size
, i
;
912 const char *fail_reason
= NULL
;
913 FILE *out_stream
= stdout
;
915 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
916 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
919 BT_CLI_LOGE_APPEND_CAUSE(
920 "Cannot find component class: plugin-name=\"%s\", "
921 "comp-cls-name=\"%s\", comp-cls-type=%d",
922 plugin_name
, comp_cls_name
,
923 BT_COMPONENT_CLASS_TYPE_SOURCE
);
927 params
= bt_value_map_create();
932 ret
= bt_value_map_insert_string_entry(params
, "url",
933 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
938 ret
= query(cfg
, comp_cls
, "sessions", params
,
939 &results
, &fail_reason
);
946 if (!bt_value_is_array(results
)) {
947 BT_CLI_LOGE_APPEND_CAUSE(
948 "Expecting an array for LTTng live `sessions` query.");
952 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
954 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
958 BT_LOGE_ERRNO("Cannot open file for writing",
960 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
961 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
963 "Cannot open file for writing: path=\"%s\"",
964 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
969 array_size
= bt_value_array_get_size(results
);
970 for (i
= 0; i
< array_size
; i
++) {
971 const char *url_text
;
972 int64_t timer_us
, streams
, clients
;
974 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
976 BT_CLI_LOGE_APPEND_CAUSE("Unexpected empty array entry.");
979 if (!bt_value_is_map(map
)) {
980 BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type.");
984 v
= bt_value_map_borrow_entry_value_const(map
, "url");
986 BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry.");
989 url_text
= bt_value_string_get(v
);
990 fprintf(out_stream
, "%s", url_text
);
991 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
993 BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry.");
996 timer_us
= bt_value_integer_signed_get(v
);
997 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
998 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1000 BT_CLI_LOGE_APPEND_CAUSE(
1001 "Missing `stream-count` entry.");
1004 streams
= bt_value_integer_signed_get(v
);
1005 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1006 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1008 BT_CLI_LOGE_APPEND_CAUSE(
1009 "Missing `client-count` entry.");
1012 clients
= bt_value_integer_signed_get(v
);
1013 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1019 BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s",
1026 bt_value_put_ref(results
);
1027 bt_value_put_ref(params
);
1028 bt_component_class_put_ref(comp_cls
);
1030 if (out_stream
&& out_stream
!= stdout
) {
1031 int fclose_ret
= fclose(out_stream
);
1034 BT_LOGE_ERRNO("Cannot close file stream",
1036 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1044 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1047 const bt_component_class
*comp_cls
= NULL
;
1048 const bt_value
*results
= NULL
;
1049 bt_value
*params
= NULL
;
1050 const bt_value
*metadata_text_value
= NULL
;
1051 const char *metadata_text
= NULL
;
1052 static const char * const plugin_name
= "ctf";
1053 static const char * const comp_cls_name
= "fs";
1054 static const bt_component_class_type comp_cls_type
=
1055 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1056 const char *fail_reason
= NULL
;
1057 FILE *out_stream
= stdout
;
1059 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1060 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1063 BT_CLI_LOGE_APPEND_CAUSE(
1064 "Cannot find component class: plugin-name=\"%s\", "
1065 "comp-cls-name=\"%s\", comp-cls-type=%d",
1066 plugin_name
, comp_cls_name
,
1067 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1072 params
= bt_value_map_create();
1078 ret
= bt_value_map_insert_string_entry(params
, "path",
1079 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1085 ret
= query(cfg
, comp_cls
, "metadata-info",
1086 params
, &results
, &fail_reason
);
1091 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1093 if (!metadata_text_value
) {
1094 BT_CLI_LOGE_APPEND_CAUSE(
1095 "Cannot find `text` string value in the resulting metadata info object.");
1100 metadata_text
= bt_value_string_get(metadata_text_value
);
1102 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1104 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1107 BT_LOGE_ERRNO("Cannot open file for writing",
1109 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1110 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1112 "Cannot open file for writing: path=\"%s\"",
1113 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1119 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1121 BT_CLI_LOGE_APPEND_CAUSE(
1122 "Cannot write whole metadata text to output stream: "
1132 BT_CLI_LOGE_APPEND_CAUSE(
1133 "Failed to query `metadata-info` object: %s", fail_reason
);
1136 bt_value_put_ref(results
);
1137 bt_value_put_ref(params
);
1138 bt_component_class_put_ref(comp_cls
);
1140 if (out_stream
&& out_stream
!= stdout
) {
1141 int fclose_ret
= fclose(out_stream
);
1144 BT_LOGE_ERRNO("Cannot close file stream",
1146 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1154 char *instance_name
;
1158 struct trace_range
{
1159 uint64_t intersection_range_begin_ns
;
1160 uint64_t intersection_range_end_ns
;
1164 guint
port_id_hash(gconstpointer v
)
1166 const struct port_id
*id
= v
;
1168 BT_ASSERT(id
->instance_name
);
1169 BT_ASSERT(id
->port_name
);
1171 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1175 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1177 const struct port_id
*id1
= v1
;
1178 const struct port_id
*id2
= v2
;
1180 return strcmp(id1
->instance_name
, id2
->instance_name
) == 0 &&
1181 strcmp(id1
->port_name
, id2
->port_name
) == 0;
1185 void port_id_destroy(gpointer data
)
1187 struct port_id
*id
= data
;
1189 free(id
->instance_name
);
1190 free(id
->port_name
);
1195 void trace_range_destroy(gpointer data
)
1200 struct cmd_run_ctx
{
1202 GHashTable
*src_components
;
1205 GHashTable
*flt_components
;
1208 GHashTable
*sink_components
;
1214 struct bt_config
*cfg
;
1218 bool stream_intersection_mode
;
1221 * Association of struct port_id -> struct trace_range.
1223 GHashTable
*intersections
;
1226 /* Returns a timestamp of the form "(-)s.ns" */
1228 char *s_from_ns(int64_t ns
)
1233 int64_t ts_sec_abs
, ts_nsec_abs
;
1234 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1235 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1237 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1238 is_negative
= false;
1239 ts_sec_abs
= ts_sec
;
1240 ts_nsec_abs
= ts_nsec
;
1241 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1242 is_negative
= false;
1243 ts_sec_abs
= ts_sec
- 1;
1244 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1245 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1247 ts_sec_abs
= ts_sec
;
1248 ts_nsec_abs
= -ts_nsec
;
1249 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1251 ts_sec_abs
= -(ts_sec
+ 1);
1252 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1253 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1255 ts_sec_abs
= -ts_sec
;
1256 ts_nsec_abs
= ts_nsec
;
1257 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1259 ts_sec_abs
= -ts_sec
;
1260 ts_nsec_abs
= -ts_nsec
;
1263 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1264 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1272 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1273 struct cmd_run_ctx
*ctx
,
1274 const bt_component
*upstream_comp
,
1275 const bt_port_output
*out_upstream_port
,
1276 struct bt_config_connection
*cfg_conn
)
1278 typedef uint64_t (*input_port_count_func_t
)(void *);
1279 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1280 const void *, uint64_t);
1281 const bt_port
*upstream_port
=
1282 bt_port_output_as_port_const(out_upstream_port
);
1285 GQuark downstreamp_comp_name_quark
;
1286 void *downstream_comp
;
1287 uint64_t downstream_port_count
;
1289 input_port_count_func_t port_count_fn
;
1290 borrow_input_port_by_index_func_t port_by_index_fn
;
1291 bt_graph_connect_ports_status connect_ports_status
=
1292 BT_GRAPH_CONNECT_PORTS_STATUS_OK
;
1293 bool insert_trimmer
= false;
1294 bt_value
*trimmer_params
= NULL
;
1295 char *intersection_begin
= NULL
;
1296 char *intersection_end
= NULL
;
1297 const bt_component_filter
*trimmer
= NULL
;
1298 const bt_component_class_filter
*trimmer_class
= NULL
;
1299 const bt_port_input
*trimmer_input
= NULL
;
1300 const bt_port_output
*trimmer_output
= NULL
;
1302 if (ctx
->intersections
&&
1303 bt_component_get_class_type(upstream_comp
) ==
1304 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1305 struct trace_range
*range
;
1306 struct port_id port_id
= {
1307 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1308 .port_name
= (char *) bt_port_get_name(upstream_port
)
1311 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1315 range
= (struct trace_range
*) g_hash_table_lookup(
1316 ctx
->intersections
, &port_id
);
1318 bt_value_map_insert_entry_status insert_status
;
1320 intersection_begin
= s_from_ns(
1321 range
->intersection_range_begin_ns
);
1322 intersection_end
= s_from_ns(
1323 range
->intersection_range_end_ns
);
1324 if (!intersection_begin
|| !intersection_end
) {
1325 BT_CLI_LOGE_APPEND_CAUSE(
1326 "Cannot create trimmer argument timestamp string.");
1330 insert_trimmer
= true;
1331 trimmer_params
= bt_value_map_create();
1332 if (!trimmer_params
) {
1336 insert_status
= bt_value_map_insert_string_entry(
1337 trimmer_params
, "begin", intersection_begin
);
1338 if (insert_status
< 0) {
1341 insert_status
= bt_value_map_insert_string_entry(
1343 "end", intersection_end
);
1344 if (insert_status
< 0) {
1349 trimmer_class
= find_filter_component_class("utils", "trimmer");
1350 if (!trimmer_class
) {
1355 BT_LOGI("Connecting upstream port to the next available downstream port: "
1356 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1357 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1358 upstream_port
, bt_port_get_name(upstream_port
),
1359 cfg_conn
->downstream_comp_name
->str
,
1360 cfg_conn
->arg
->str
);
1361 downstreamp_comp_name_quark
= g_quark_from_string(
1362 cfg_conn
->downstream_comp_name
->str
);
1363 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1364 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1365 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1366 port_count_fn
= (input_port_count_func_t
)
1367 bt_component_filter_get_input_port_count
;
1368 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1369 bt_component_filter_borrow_input_port_by_index_const
;
1371 if (!downstream_comp
) {
1372 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1373 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1374 port_count_fn
= (input_port_count_func_t
)
1375 bt_component_sink_get_input_port_count
;
1376 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1377 bt_component_sink_borrow_input_port_by_index_const
;
1380 if (!downstream_comp
) {
1381 BT_CLI_LOGE_APPEND_CAUSE("Cannot find downstream component: "
1382 "comp-name=\"%s\", conn-arg=\"%s\"",
1383 cfg_conn
->downstream_comp_name
->str
,
1384 cfg_conn
->arg
->str
);
1388 downstream_port_count
= port_count_fn(downstream_comp
);
1390 for (i
= 0; i
< downstream_port_count
; i
++) {
1391 const bt_port_input
*in_downstream_port
=
1392 port_by_index_fn(downstream_comp
, i
);
1393 const bt_port
*downstream_port
=
1394 bt_port_input_as_port_const(in_downstream_port
);
1395 const char *upstream_port_name
;
1396 const char *downstream_port_name
;
1398 BT_ASSERT(downstream_port
);
1400 /* Skip port if it's already connected. */
1401 if (bt_port_is_connected(downstream_port
)) {
1402 BT_LOGI("Skipping downstream port: already connected: "
1403 "port-addr=%p, port-name=\"%s\"",
1405 bt_port_get_name(downstream_port
));
1409 downstream_port_name
= bt_port_get_name(downstream_port
);
1410 BT_ASSERT(downstream_port_name
);
1411 upstream_port_name
= bt_port_get_name(upstream_port
);
1412 BT_ASSERT(upstream_port_name
);
1414 if (!bt_common_star_glob_match(
1415 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1416 downstream_port_name
, SIZE_MAX
)) {
1420 if (insert_trimmer
) {
1422 * In order to insert the trimmer between the
1423 * two components that were being connected, we
1424 * create a connection configuration entry which
1425 * describes a connection from the trimmer's
1426 * output to the original input that was being
1429 * Hence, the creation of the trimmer will cause
1430 * the graph "new port" listener to establish
1431 * all downstream connections as its output port
1432 * is connected. We will then establish the
1433 * connection between the original upstream
1434 * source and the trimmer.
1436 char *trimmer_name
= NULL
;
1437 bt_graph_add_component_status add_comp_status
;
1439 ret
= asprintf(&trimmer_name
,
1440 "stream-intersection-trimmer-%s",
1441 upstream_port_name
);
1447 ctx
->connect_ports
= false;
1448 add_comp_status
= bt_graph_add_filter_component(
1449 ctx
->graph
, trimmer_class
, trimmer_name
,
1450 trimmer_params
, ctx
->cfg
->log_level
,
1453 if (add_comp_status
!=
1454 BT_GRAPH_ADD_COMPONENT_STATUS_OK
) {
1460 bt_component_filter_borrow_input_port_by_index_const(
1462 if (!trimmer_input
) {
1466 bt_component_filter_borrow_output_port_by_index_const(
1468 if (!trimmer_output
) {
1473 * Replace the current downstream port by the trimmer's
1476 in_downstream_port
= trimmer_input
;
1478 bt_port_input_as_port_const(in_downstream_port
);
1479 downstream_port_name
= bt_port_get_name(
1481 BT_ASSERT(downstream_port_name
);
1484 /* We have a winner! */
1485 connect_ports_status
= bt_graph_connect_ports(ctx
->graph
,
1486 out_upstream_port
, in_downstream_port
, NULL
);
1487 downstream_port
= NULL
;
1488 switch (connect_ports_status
) {
1489 case BT_GRAPH_CONNECT_PORTS_STATUS_OK
:
1492 BT_CLI_LOGE_APPEND_CAUSE(
1493 "Cannot create connection: graph refuses to connect ports: "
1494 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1495 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1496 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1497 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1499 upstream_comp
, bt_component_get_name(upstream_comp
),
1500 upstream_port
, bt_port_get_name(upstream_port
),
1501 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1502 downstream_port
, downstream_port_name
,
1503 cfg_conn
->arg
->str
);
1507 BT_LOGI("Connected component ports: "
1508 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1509 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1510 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1511 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1513 upstream_comp
, bt_component_get_name(upstream_comp
),
1514 upstream_port
, bt_port_get_name(upstream_port
),
1515 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1516 downstream_port
, downstream_port_name
,
1517 cfg_conn
->arg
->str
);
1519 if (insert_trimmer
) {
1521 * The first connection, from the source to the trimmer,
1522 * has been done. We now connect the trimmer to the
1523 * original downstream port.
1525 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1527 bt_component_filter_as_component_const(trimmer
),
1528 trimmer_output
, cfg_conn
);
1532 ctx
->connect_ports
= true;
1536 * We found a matching downstream port: the search is
1542 /* No downstream port found */
1543 BT_CLI_LOGE_APPEND_CAUSE(
1544 "Cannot create connection: cannot find a matching downstream port for upstream port: "
1545 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1546 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1547 upstream_port
, bt_port_get_name(upstream_port
),
1548 cfg_conn
->downstream_comp_name
->str
,
1549 cfg_conn
->arg
->str
);
1555 free(intersection_begin
);
1556 free(intersection_end
);
1557 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1558 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1559 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1564 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1565 const bt_port_output
*upstream_port
)
1568 const char *upstream_port_name
;
1569 const char *upstream_comp_name
;
1570 const bt_component
*upstream_comp
= NULL
;
1574 BT_ASSERT(upstream_port
);
1575 upstream_port_name
= bt_port_get_name(
1576 bt_port_output_as_port_const(upstream_port
));
1577 BT_ASSERT(upstream_port_name
);
1578 upstream_comp
= bt_port_borrow_component_const(
1579 bt_port_output_as_port_const(upstream_port
));
1580 BT_ASSERT(upstream_comp
);
1581 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1582 BT_ASSERT(upstream_comp_name
);
1583 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1584 "port-addr=%p, port-name=\"%s\"",
1585 upstream_comp
, upstream_comp_name
,
1586 upstream_port
, upstream_port_name
);
1588 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1589 struct bt_config_connection
*cfg_conn
=
1591 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1593 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1594 upstream_comp_name
)) {
1598 if (!bt_common_star_glob_match(
1599 cfg_conn
->upstream_port_glob
->str
,
1600 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1604 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1605 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1607 BT_CLI_LOGE_APPEND_CAUSE(
1608 "Cannot connect upstream port: "
1609 "port-addr=%p, port-name=\"%s\"",
1611 upstream_port_name
);
1617 BT_CLI_LOGE_APPEND_CAUSE(
1618 "Cannot connect upstream port: port does not match any connection argument: "
1619 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1620 upstream_port_name
);
1630 bt_graph_listener_func_status
1631 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1632 const bt_port_output
*out_port
)
1634 const bt_component
*comp
;
1635 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1636 bt_graph_listener_func_status ret
=
1637 BT_GRAPH_LISTENER_FUNC_STATUS_OK
;
1639 comp
= bt_port_borrow_component_const(port
);
1640 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1641 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1642 comp
, comp
? bt_component_get_name(comp
) : "",
1643 port
, bt_port_get_name(port
));
1645 if (!ctx
->connect_ports
) {
1651 if (bt_port_is_connected(port
)) {
1652 BT_LOGW_STR("Port is already connected.");
1656 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
1657 BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port.");
1658 ret
= BT_GRAPH_LISTENER_FUNC_STATUS_ERROR
;
1667 bt_graph_listener_func_status
graph_source_output_port_added_listener(
1668 const bt_component_source
*component
,
1669 const bt_port_output
*port
, void *data
)
1671 return graph_output_port_added_listener(data
, port
);
1675 bt_graph_listener_func_status
graph_filter_output_port_added_listener(
1676 const bt_component_filter
*component
,
1677 const bt_port_output
*port
, void *data
)
1679 return graph_output_port_added_listener(data
, port
);
1683 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1689 if (ctx
->src_components
) {
1690 g_hash_table_destroy(ctx
->src_components
);
1691 ctx
->src_components
= NULL
;
1694 if (ctx
->flt_components
) {
1695 g_hash_table_destroy(ctx
->flt_components
);
1696 ctx
->flt_components
= NULL
;
1699 if (ctx
->sink_components
) {
1700 g_hash_table_destroy(ctx
->sink_components
);
1701 ctx
->sink_components
= NULL
;
1704 if (ctx
->intersections
) {
1705 g_hash_table_destroy(ctx
->intersections
);
1706 ctx
->intersections
= NULL
;
1709 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
1714 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1717 bt_graph_add_listener_status add_listener_status
;
1720 ctx
->connect_ports
= false;
1721 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
1722 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1723 if (!ctx
->src_components
) {
1727 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
1728 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1729 if (!ctx
->flt_components
) {
1733 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
1734 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1735 if (!ctx
->sink_components
) {
1739 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1740 ctx
->stream_intersection_mode
= true;
1741 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1742 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1743 if (!ctx
->intersections
) {
1748 ctx
->graph
= bt_graph_create(0);
1753 bt_graph_add_interrupter(ctx
->graph
, the_interrupter
);
1754 add_listener_status
= bt_graph_add_source_component_output_port_added_listener(
1755 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
1757 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1758 BT_CLI_LOGE_APPEND_CAUSE(
1759 "Cannot add \"port added\" listener to graph.");
1763 add_listener_status
= bt_graph_add_filter_component_output_port_added_listener(
1764 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
1766 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1767 BT_CLI_LOGE_APPEND_CAUSE(
1768 "Cannot add \"port added\" listener to graph.");
1775 cmd_run_ctx_destroy(ctx
);
1783 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
1784 struct bt_config_component
*cfg_comp
,
1785 const bt_component_class_source
*src_comp_cls
)
1789 int64_t trace_count
;
1790 const char *path
= NULL
;
1791 const bt_value
*query_result
= NULL
;
1792 const bt_value
*trace_info
= NULL
;
1793 const bt_value
*intersection_range
= NULL
;
1794 const bt_value
*intersection_begin
= NULL
;
1795 const bt_value
*intersection_end
= NULL
;
1796 const bt_value
*stream_infos
= NULL
;
1797 const bt_value
*stream_info
= NULL
;
1798 struct port_id
*port_id
= NULL
;
1799 struct trace_range
*trace_range
= NULL
;
1800 const char *fail_reason
= NULL
;
1801 const bt_component_class
*comp_cls
=
1802 bt_component_class_source_as_component_class_const(src_comp_cls
);
1804 ret
= query(ctx
->cfg
, comp_cls
, "babeltrace.trace-info",
1805 cfg_comp
->params
, &query_result
,
1808 BT_LOGD("Component class does not support the `babeltrace.trace-info` query: %s: "
1809 "comp-class-name=\"%s\"", fail_reason
,
1810 bt_component_class_get_name(comp_cls
));
1815 BT_ASSERT(query_result
);
1817 if (!bt_value_is_array(query_result
)) {
1818 BT_LOGD("Unexpected format of `babeltrace.trace-info` query result: "
1819 "component-class-name=%s",
1820 bt_component_class_get_name(comp_cls
));
1825 trace_count
= bt_value_array_get_size(query_result
);
1826 if (trace_count
< 0) {
1831 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
1833 uint64_t stream_idx
;
1834 int64_t stream_count
;
1836 trace_info
= bt_value_array_borrow_element_by_index_const(
1837 query_result
, trace_idx
);
1838 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
1840 BT_LOGD_STR("Cannot retrieve trace from query result.");
1844 intersection_range
= bt_value_map_borrow_entry_value_const(
1845 trace_info
, "intersection-range-ns");
1846 if (!intersection_range
) {
1848 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
1852 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
1854 if (!intersection_begin
) {
1856 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
1860 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
1862 if (!intersection_end
) {
1864 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
1868 begin
= bt_value_integer_signed_get(intersection_begin
);
1869 end
= bt_value_integer_signed_get(intersection_end
);
1871 if (begin
< 0 || end
< 0 || end
< begin
) {
1872 BT_CLI_LOGE_APPEND_CAUSE(
1873 "Invalid trace stream intersection values: "
1874 "intersection-range-ns:begin=%" PRId64
1875 ", intersection-range-ns:end=%" PRId64
,
1881 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
1883 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
1885 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
1889 stream_count
= bt_value_array_get_size(stream_infos
);
1890 if (stream_count
< 0) {
1895 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
1896 const bt_value
*port_name
;
1898 port_id
= g_new0(struct port_id
, 1);
1901 BT_CLI_LOGE_APPEND_CAUSE(
1902 "Cannot allocate memory for port_id structure.");
1905 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
1906 if (!port_id
->instance_name
) {
1908 BT_CLI_LOGE_APPEND_CAUSE(
1909 "Cannot allocate memory for port_id component instance name.");
1913 trace_range
= g_new0(struct trace_range
, 1);
1916 BT_CLI_LOGE_APPEND_CAUSE(
1917 "Cannot allocate memory for trace_range structure.");
1920 trace_range
->intersection_range_begin_ns
= begin
;
1921 trace_range
->intersection_range_end_ns
= end
;
1923 stream_info
= bt_value_array_borrow_element_by_index_const(
1924 stream_infos
, stream_idx
);
1925 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
1927 BT_CLI_LOGE_APPEND_CAUSE(
1928 "Cannot retrieve stream informations from trace in query result.");
1932 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
1933 if (!port_name
|| !bt_value_is_string(port_name
)) {
1935 BT_CLI_LOGE_APPEND_CAUSE(
1936 "Cannot retrieve port name in query result.");
1940 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
1941 if (!port_id
->port_name
) {
1943 BT_CLI_LOGE_APPEND_CAUSE(
1944 "Cannot allocate memory for port_id port_name.");
1948 BT_LOGD("Inserting stream intersection ");
1950 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
1960 BT_CLI_LOGE_APPEND_CAUSE(
1961 "Cannot determine stream intersection of trace: path=\"%s\"",
1962 path
? path
: "(unknown)");
1965 bt_value_put_ref(query_result
);
1967 g_free(trace_range
);
1972 int cmd_run_ctx_create_components_from_config_components(
1973 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
1976 const void *comp_cls
= NULL
;
1977 const void *comp
= NULL
;
1980 for (i
= 0; i
< cfg_components
->len
; i
++) {
1981 struct bt_config_component
*cfg_comp
=
1982 g_ptr_array_index(cfg_components
, i
);
1985 switch (cfg_comp
->type
) {
1986 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1987 comp_cls
= find_source_component_class(
1988 cfg_comp
->plugin_name
->str
,
1989 cfg_comp
->comp_cls_name
->str
);
1991 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1992 comp_cls
= find_filter_component_class(
1993 cfg_comp
->plugin_name
->str
,
1994 cfg_comp
->comp_cls_name
->str
);
1996 case BT_COMPONENT_CLASS_TYPE_SINK
:
1997 comp_cls
= find_sink_component_class(
1998 cfg_comp
->plugin_name
->str
,
1999 cfg_comp
->comp_cls_name
->str
);
2006 BT_CLI_LOGE_APPEND_CAUSE(
2007 "Cannot find component class: plugin-name=\"%s\", "
2008 "comp-cls-name=\"%s\", comp-cls-type=%d",
2009 cfg_comp
->plugin_name
->str
,
2010 cfg_comp
->comp_cls_name
->str
,
2015 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_TRACE
);
2017 switch (cfg_comp
->type
) {
2018 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2019 ret
= bt_graph_add_source_component(ctx
->graph
,
2020 comp_cls
, cfg_comp
->instance_name
->str
,
2021 cfg_comp
->params
, cfg_comp
->log_level
,
2024 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2025 ret
= bt_graph_add_filter_component(ctx
->graph
,
2026 comp_cls
, cfg_comp
->instance_name
->str
,
2027 cfg_comp
->params
, cfg_comp
->log_level
,
2030 case BT_COMPONENT_CLASS_TYPE_SINK
:
2031 ret
= bt_graph_add_sink_component(ctx
->graph
,
2032 comp_cls
, cfg_comp
->instance_name
->str
,
2033 cfg_comp
->params
, cfg_comp
->log_level
,
2041 BT_CLI_LOGE_APPEND_CAUSE(
2042 "Cannot create component: plugin-name=\"%s\", "
2043 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2045 cfg_comp
->plugin_name
->str
,
2046 cfg_comp
->comp_cls_name
->str
,
2047 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2051 if (ctx
->stream_intersection_mode
&&
2052 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2053 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2059 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2060 comp
, cfg_comp
->instance_name
->str
);
2061 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2062 BT_ASSERT(quark
> 0);
2064 switch (cfg_comp
->type
) {
2065 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2066 g_hash_table_insert(ctx
->src_components
,
2067 GUINT_TO_POINTER(quark
), (void *) comp
);
2069 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2070 g_hash_table_insert(ctx
->flt_components
,
2071 GUINT_TO_POINTER(quark
), (void *) comp
);
2073 case BT_COMPONENT_CLASS_TYPE_SINK
:
2074 g_hash_table_insert(ctx
->sink_components
,
2075 GUINT_TO_POINTER(quark
), (void *) comp
);
2082 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2091 bt_object_put_ref(comp
);
2092 bt_object_put_ref(comp_cls
);
2097 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2102 * Make sure that, during this phase, our graph's "port added"
2103 * listener does not connect ports while we are creating the
2104 * components because we have a special, initial phase for
2107 ctx
->connect_ports
= false;
2109 ret
= cmd_run_ctx_create_components_from_config_components(
2110 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2116 ret
= cmd_run_ctx_create_components_from_config_components(
2117 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2123 ret
= cmd_run_ctx_create_components_from_config_components(
2124 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2134 typedef uint64_t (*output_port_count_func_t
)(const void *);
2135 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2136 const void *, uint64_t);
2139 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2140 void *comp
, output_port_count_func_t port_count_fn
,
2141 borrow_output_port_by_index_func_t port_by_index_fn
)
2147 count
= port_count_fn(comp
);
2149 for (i
= 0; i
< count
; i
++) {
2150 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2152 BT_ASSERT(upstream_port
);
2153 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2164 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2167 GHashTableIter iter
;
2168 gpointer g_name_quark
, g_comp
;
2170 ctx
->connect_ports
= true;
2171 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2173 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2174 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2175 (output_port_count_func_t
)
2176 bt_component_source_get_output_port_count
,
2177 (borrow_output_port_by_index_func_t
)
2178 bt_component_source_borrow_output_port_by_index_const
);
2184 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2186 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2187 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2188 (output_port_count_func_t
)
2189 bt_component_filter_get_output_port_count
,
2190 (borrow_output_port_by_index_func_t
)
2191 bt_component_filter_borrow_output_port_by_index_const
);
2202 int cmd_run(struct bt_config
*cfg
)
2205 struct cmd_run_ctx ctx
= { 0 };
2207 /* Initialize the command's context and the graph object */
2208 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2209 BT_CLI_LOGE_APPEND_CAUSE(
2210 "Cannot initialize the command's context.");
2214 if (bt_interrupter_is_set(the_interrupter
)) {
2215 BT_CLI_LOGW_APPEND_CAUSE(
2216 "Interrupted by user before creating components.");
2220 BT_LOGI_STR("Creating components.");
2222 /* Create the requested component instances */
2223 if (cmd_run_ctx_create_components(&ctx
)) {
2224 BT_CLI_LOGE_APPEND_CAUSE("Cannot create components.");
2228 if (bt_interrupter_is_set(the_interrupter
)) {
2229 BT_CLI_LOGW_APPEND_CAUSE(
2230 "Interrupted by user before connecting components.");
2234 BT_LOGI_STR("Connecting components.");
2236 /* Connect the initially visible component ports */
2237 if (cmd_run_ctx_connect_ports(&ctx
)) {
2238 BT_CLI_LOGE_APPEND_CAUSE(
2239 "Cannot connect initial component ports.");
2243 BT_LOGI_STR("Running the graph.");
2247 bt_graph_run_status run_status
= bt_graph_run(ctx
.graph
);
2250 * Reset console in case something messed with console
2251 * codes during the graph's execution.
2253 printf("%s", bt_common_color_reset());
2255 fprintf(stderr
, "%s", bt_common_color_reset());
2256 BT_LOGT("bt_graph_run() returned: status=%s",
2257 bt_common_func_status_string(run_status
));
2259 switch (run_status
) {
2260 case BT_GRAPH_RUN_STATUS_OK
:
2262 case BT_GRAPH_RUN_STATUS_AGAIN
:
2263 if (bt_interrupter_is_set(the_interrupter
)) {
2264 BT_CLI_LOGW_APPEND_CAUSE(
2265 "Graph was interrupted by user.");
2269 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2270 BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: "
2272 cfg
->cmd_data
.run
.retry_duration_us
);
2274 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2275 if (bt_interrupter_is_set(the_interrupter
)) {
2276 BT_CLI_LOGW_APPEND_CAUSE(
2277 "Graph was interrupted by user.");
2283 case BT_GRAPH_RUN_STATUS_END
:
2286 if (bt_interrupter_is_set(the_interrupter
)) {
2287 BT_CLI_LOGW_APPEND_CAUSE(
2288 "Graph was interrupted by user and failed: "
2290 bt_common_func_status_string(run_status
));
2294 BT_CLI_LOGE_APPEND_CAUSE(
2295 "Graph failed to complete successfully");
2308 cmd_run_ctx_destroy(&ctx
);
2313 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2315 const char *env_clash
;
2317 if (!cfg
->command_name
) {
2321 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2322 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2326 if (g_file_test(cfg
->command_name
,
2327 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2328 _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION
, __FILE__
, __LINE__
,
2329 BT_LOG_WARNING
, BT_LOG_TAG
,
2330 "The `%s` command was executed. "
2331 "If you meant to convert a trace located in "
2332 "the local `%s` directory, please use:\n\n"
2333 " babeltrace2 convert %s [OPTIONS]",
2334 cfg
->command_name
, cfg
->command_name
,
2340 void init_log_level(void)
2342 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2346 void set_auto_log_levels(struct bt_config
*cfg
)
2348 const char **env_var_name
;
2351 * Override the configuration's default log level if
2352 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2353 * are found for backward compatibility with legacy Babetrace 1.
2355 if (getenv("BABELTRACE_DEBUG") &&
2356 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2357 cfg
->log_level
= BT_LOG_TRACE
;
2358 } else if (getenv("BABELTRACE_VERBOSE") &&
2359 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2360 cfg
->log_level
= BT_LOG_INFO
;
2364 * Set log levels according to --debug or --verbose. For
2365 * backward compatibility, --debug is more verbose than
2368 * --verbose: INFO log level
2369 * --debug: TRACE log level (includes DEBUG, which is
2370 * is less verbose than TRACE in the internal
2371 * logging framework)
2373 if (!getenv("LIBBABELTRACE2_INIT_LOG_LEVEL")) {
2375 bt_logging_set_global_level(BT_LOG_INFO
);
2376 } else if (cfg
->debug
) {
2377 bt_logging_set_global_level(BT_LOG_TRACE
);
2380 * Set library's default log level if not
2381 * explicitly specified.
2383 bt_logging_set_global_level(cfg
->log_level
);
2387 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2389 bt_cli_log_level
= BT_LOG_INFO
;
2390 } else if (cfg
->debug
) {
2391 bt_cli_log_level
= BT_LOG_TRACE
;
2394 * Set CLI's default log level if not explicitly
2397 bt_cli_log_level
= cfg
->log_level
;
2401 env_var_name
= log_level_env_var_names
;
2403 while (*env_var_name
) {
2404 if (!getenv(*env_var_name
)) {
2406 g_setenv(*env_var_name
, "INFO", 1);
2407 } else if (cfg
->debug
) {
2408 g_setenv(*env_var_name
, "TRACE", 1);
2410 char val
[2] = { 0 };
2413 * Set module's default log level if not
2414 * explicitly specified.
2416 val
[0] = bt_log_get_letter_from_level(
2418 g_setenv(*env_var_name
, val
, 1);
2427 void print_error_causes(void)
2429 const bt_error
*error
= bt_current_thread_take_error();
2431 GString
*folded
= NULL
;
2432 unsigned int columns
;
2434 if (!error
|| bt_error_get_cause_count(error
) == 0) {
2435 fprintf(stderr
, "%s%sUnknown command-line error.%s\n",
2436 bt_common_color_bold(), bt_common_color_fg_red(),
2437 bt_common_color_reset());
2441 /* Try to get terminal width to fold the error cause messages */
2442 if (bt_common_get_term_size(&columns
, NULL
) < 0) {
2443 /* Width not found: default to 80 */
2448 * This helps visually separate the error causes from the last
2449 * logging statement.
2451 fprintf(stderr
, "\n");
2453 /* Reverse order: deepest (root) cause printed at the end */
2454 for (i
= bt_error_get_cause_count(error
) - 1; i
>= 0; i
--) {
2455 const bt_error_cause
*cause
=
2456 bt_error_borrow_cause_by_index(error
, (uint64_t) i
);
2457 const char *prefix_fmt
=
2458 i
== bt_error_get_cause_count(error
) - 1 ?
2459 "%s%sERROR%s: " : "%s%sCAUSED BY%s ";
2462 fprintf(stderr
, prefix_fmt
,
2463 bt_common_color_bold(), bt_common_color_fg_red(),
2464 bt_common_color_reset());
2466 /* Print actor name */
2467 fprintf(stderr
, "[");
2468 switch (bt_error_cause_get_actor_type(cause
)) {
2469 case BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN
:
2470 fprintf(stderr
, "%s%s%s",
2471 bt_common_color_bold(),
2472 bt_error_cause_get_module_name(cause
),
2473 bt_common_color_reset());
2475 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
:
2476 fprintf(stderr
, "%s%s%s: ",
2477 bt_common_color_bold(),
2478 bt_error_cause_component_actor_get_component_name(cause
),
2479 bt_common_color_reset());
2480 print_plugin_comp_cls_opt(stderr
,
2481 bt_error_cause_component_actor_get_plugin_name(cause
),
2482 bt_error_cause_component_actor_get_component_class_name(cause
),
2483 bt_error_cause_component_actor_get_component_class_type(cause
));
2485 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
:
2486 print_plugin_comp_cls_opt(stderr
,
2487 bt_error_cause_component_class_actor_get_plugin_name(cause
),
2488 bt_error_cause_component_class_actor_get_component_class_name(cause
),
2489 bt_error_cause_component_class_actor_get_component_class_type(cause
));
2491 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
:
2492 fprintf(stderr
, "%s%s%s (%s%s%s): ",
2493 bt_common_color_bold(),
2494 bt_error_cause_message_iterator_actor_get_component_name(cause
),
2495 bt_common_color_reset(),
2496 bt_common_color_bold(),
2497 bt_error_cause_message_iterator_actor_get_component_output_port_name(cause
),
2498 bt_common_color_reset());
2499 print_plugin_comp_cls_opt(stderr
,
2500 bt_error_cause_message_iterator_actor_get_plugin_name(cause
),
2501 bt_error_cause_message_iterator_actor_get_component_class_name(cause
),
2502 bt_error_cause_message_iterator_actor_get_component_class_type(cause
));
2508 /* Print file name and line number */
2509 fprintf(stderr
, "] (%s%s%s%s:%s%" PRIu64
"%s)\n",
2510 bt_common_color_bold(),
2511 bt_common_color_fg_magenta(),
2512 bt_error_cause_get_file_name(cause
),
2513 bt_common_color_reset(),
2514 bt_common_color_fg_green(),
2515 bt_error_cause_get_line_number(cause
),
2516 bt_common_color_reset());
2519 folded
= bt_common_fold(bt_error_cause_get_message(cause
),
2522 BT_LOGE_STR("Could not fold string.");
2523 fprintf(stderr
, "%s\n",
2524 bt_error_cause_get_message(cause
));
2528 fprintf(stderr
, "%s\n", folded
->str
);
2529 g_string_free(folded
, TRUE
);
2537 bt_error_release(error
);
2541 int main(int argc
, const char **argv
)
2545 struct bt_config
*cfg
;
2548 set_signal_handler();
2549 init_loaded_plugins();
2550 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2553 /* Quit without errors; typically usage/version */
2555 BT_LOGI_STR("Quitting without errors.");
2560 BT_CLI_LOGE_APPEND_CAUSE(
2561 "Command-line error: retcode=%d", retcode
);
2566 BT_CLI_LOGE_APPEND_CAUSE(
2567 "Failed to create a valid Babeltrace configuration.");
2572 set_auto_log_levels(cfg
);
2575 if (cfg
->command_needs_plugins
) {
2576 ret
= require_loaded_plugins(cfg
->plugin_paths
);
2578 BT_CLI_LOGE_APPEND_CAUSE(
2579 "Failed to load plugins: ret=%d", ret
);
2585 BT_ASSERT(!the_interrupter
);
2586 the_interrupter
= bt_interrupter_create();
2587 if (!the_interrupter
) {
2588 BT_CLI_LOGE_APPEND_CAUSE("Failed to create an interrupter object.");
2593 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2594 cfg
->command
, cfg
->command_name
);
2596 switch (cfg
->command
) {
2597 case BT_CONFIG_COMMAND_RUN
:
2600 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2601 ret
= cmd_list_plugins(cfg
);
2603 case BT_CONFIG_COMMAND_HELP
:
2604 ret
= cmd_help(cfg
);
2606 case BT_CONFIG_COMMAND_QUERY
:
2607 ret
= cmd_query(cfg
);
2609 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2610 ret
= cmd_print_ctf_metadata(cfg
);
2612 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2613 ret
= cmd_print_lttng_live_sessions(cfg
);
2616 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2620 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2621 cfg
->command
, cfg
->command_name
, ret
);
2622 warn_command_name_and_directory_clash(cfg
);
2623 retcode
= ret
? 1 : 0;
2626 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2627 fini_loaded_plugins();
2628 bt_interrupter_put_ref(the_interrupter
);
2631 print_error_causes();
2635 * Clear current thread's error in case there is one to avoid a
2638 bt_current_thread_clear_error();