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-log-level.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 NSEC_PER_SEC 1000000000LL
48 /* Application's interrupter (owned by this) */
49 static bt_interrupter
*the_interrupter
;
56 BOOL WINAPI
signal_handler(DWORD signal
) {
57 if (the_interrupter
) {
58 bt_interrupter_set(the_interrupter
);
65 void set_signal_handler(void)
67 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
68 BT_LOGE("Failed to set the Ctrl+C handler.");
72 #else /* __MINGW32__ */
75 void signal_handler(int signum
)
77 if (signum
!= SIGINT
) {
81 if (the_interrupter
) {
82 bt_interrupter_set(the_interrupter
);
87 void set_signal_handler(void)
89 struct sigaction new_action
, old_action
;
91 new_action
.sa_handler
= signal_handler
;
92 sigemptyset(&new_action
.sa_mask
);
93 new_action
.sa_flags
= 0;
94 sigaction(SIGINT
, NULL
, &old_action
);
96 if (old_action
.sa_handler
!= SIG_IGN
) {
97 sigaction(SIGINT
, &new_action
, NULL
);
101 #endif /* __MINGW32__ */
104 int query(struct bt_config
*cfg
, const bt_component_class
*comp_cls
,
105 const char *obj
, const bt_value
*params
,
106 const bt_value
**user_result
, const char **fail_reason
)
108 return cli_query(comp_cls
, obj
, params
, cfg
->log_level
,
109 the_interrupter
, user_result
, fail_reason
);
112 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
113 const bt_plugin
*, const char *);
116 const void *find_component_class_from_plugin(const char *plugin_name
,
117 const char *comp_class_name
,
118 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
120 const void *comp_class
= NULL
;
121 const bt_plugin
*plugin
;
123 BT_LOGI("Finding component class: plugin-name=\"%s\", "
124 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
126 plugin
= find_loaded_plugin(plugin_name
);
131 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
132 bt_object_get_ref(comp_class
);
133 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
137 BT_LOGI("Found component class: plugin-name=\"%s\", "
138 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
140 BT_LOGI("Cannot find source component class: "
141 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
142 plugin_name
, comp_class_name
);
149 const bt_component_class_source
*find_source_component_class(
150 const char *plugin_name
, const char *comp_class_name
)
152 return (const void *) find_component_class_from_plugin(
153 plugin_name
, comp_class_name
,
154 (plugin_borrow_comp_cls_func_t
)
155 bt_plugin_borrow_source_component_class_by_name_const
);
159 const bt_component_class_filter
*find_filter_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_filter_component_class_by_name_const
);
169 const bt_component_class_sink
*find_sink_component_class(
170 const char *plugin_name
, const char *comp_class_name
)
172 return (const void *) find_component_class_from_plugin(plugin_name
,
174 (plugin_borrow_comp_cls_func_t
)
175 bt_plugin_borrow_sink_component_class_by_name_const
);
179 const bt_component_class
*find_component_class(const char *plugin_name
,
180 const char *comp_class_name
,
181 bt_component_class_type comp_class_type
)
183 const bt_component_class
*comp_cls
= NULL
;
185 switch (comp_class_type
) {
186 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
187 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
189 case BT_COMPONENT_CLASS_TYPE_FILTER
:
190 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
192 case BT_COMPONENT_CLASS_TYPE_SINK
:
193 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
203 void print_indent(FILE *fp
, size_t indent
)
207 for (i
= 0; i
< indent
; i
++) {
213 const char *component_type_str(bt_component_class_type type
)
216 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
218 case BT_COMPONENT_CLASS_TYPE_SINK
:
220 case BT_COMPONENT_CLASS_TYPE_FILTER
:
228 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
229 const char *comp_cls_name
, bt_component_class_type type
)
231 GString
*shell_plugin_name
= NULL
;
232 GString
*shell_comp_cls_name
= NULL
;
235 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
236 if (!shell_plugin_name
) {
241 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
242 if (!shell_comp_cls_name
) {
246 fprintf(fh
, "'%s%s%s%s",
247 bt_common_color_bold(),
248 bt_common_color_fg_cyan(),
249 component_type_str(type
),
250 bt_common_color_fg_default());
252 if (shell_plugin_name
) {
253 fprintf(fh
, ".%s%s%s",
254 bt_common_color_fg_blue(),
255 shell_plugin_name
->str
,
256 bt_common_color_fg_default());
259 fprintf(fh
, ".%s%s%s'",
260 bt_common_color_fg_yellow(),
261 shell_comp_cls_name
->str
,
262 bt_common_color_reset());
265 if (shell_plugin_name
) {
266 g_string_free(shell_plugin_name
, TRUE
);
269 if (shell_comp_cls_name
) {
270 g_string_free(shell_comp_cls_name
, TRUE
);
275 void print_value(FILE *, const bt_value
*, size_t);
278 void print_value_rec(FILE *, const bt_value
*, size_t);
280 struct print_map_value_data
{
286 bt_bool
print_map_value(const char *key
, const bt_value
*object
,
289 struct print_map_value_data
*print_map_value_data
= data
;
291 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
292 fprintf(print_map_value_data
->fp
, "%s: ", key
);
295 if (bt_value_is_array(object
) &&
296 bt_value_array_is_empty(object
)) {
297 fprintf(print_map_value_data
->fp
, "[ ]\n");
301 if (bt_value_is_map(object
) &&
302 bt_value_map_is_empty(object
)) {
303 fprintf(print_map_value_data
->fp
, "{ }\n");
307 if (bt_value_is_array(object
) ||
308 bt_value_is_map(object
)) {
309 fprintf(print_map_value_data
->fp
, "\n");
312 print_value_rec(print_map_value_data
->fp
, object
,
313 print_map_value_data
->indent
+ 2);
318 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
332 switch (bt_value_get_type(value
)) {
333 case BT_VALUE_TYPE_NULL
:
334 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
335 bt_common_color_reset());
337 case BT_VALUE_TYPE_BOOL
:
338 bool_val
= bt_value_bool_get(value
);
339 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
340 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
341 bt_common_color_reset());
343 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
344 uint_val
= bt_value_integer_unsigned_get(value
);
345 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
346 bt_common_color_fg_red(), uint_val
,
347 bt_common_color_reset());
349 case BT_VALUE_TYPE_SIGNED_INTEGER
:
350 int_val
= bt_value_integer_signed_get(value
);
351 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
352 bt_common_color_fg_red(), int_val
,
353 bt_common_color_reset());
355 case BT_VALUE_TYPE_REAL
:
356 dbl_val
= bt_value_real_get(value
);
357 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
358 bt_common_color_fg_red(), dbl_val
,
359 bt_common_color_reset());
361 case BT_VALUE_TYPE_STRING
:
362 str_val
= bt_value_string_get(value
);
363 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
364 bt_common_color_fg_green(), str_val
,
365 bt_common_color_reset());
367 case BT_VALUE_TYPE_ARRAY
:
368 size
= bt_value_array_get_length(value
);
374 print_indent(fp
, indent
);
375 fprintf(fp
, "[ ]\n");
379 for (i
= 0; i
< size
; i
++) {
380 const bt_value
*element
=
381 bt_value_array_borrow_element_by_index_const(
387 print_indent(fp
, indent
);
390 if (bt_value_is_array(element
) &&
391 bt_value_array_is_empty(element
)) {
392 fprintf(fp
, "[ ]\n");
396 if (bt_value_is_map(element
) &&
397 bt_value_map_is_empty(element
)) {
398 fprintf(fp
, "{ }\n");
402 if (bt_value_is_array(element
) ||
403 bt_value_is_map(element
)) {
407 print_value_rec(fp
, element
, indent
+ 2);
410 case BT_VALUE_TYPE_MAP
:
412 struct print_map_value_data data
= {
417 if (bt_value_map_is_empty(value
)) {
418 print_indent(fp
, indent
);
419 fprintf(fp
, "{ }\n");
423 bt_value_map_foreach_entry_const(value
, print_map_value
, &data
);
432 BT_LOGE("Error printing value of type %s.",
433 bt_common_value_type_string(bt_value_get_type(value
)));
437 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
439 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
440 print_indent(fp
, indent
);
443 print_value_rec(fp
, value
, indent
);
447 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
449 fprintf(stderr
, " ");
450 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
451 bt_config_component
->comp_cls_name
->str
,
452 bt_config_component
->type
);
453 fprintf(stderr
, ":\n");
455 if (bt_config_component
->instance_name
->len
> 0) {
456 fprintf(stderr
, " Name: %s\n",
457 bt_config_component
->instance_name
->str
);
460 fprintf(stderr
, " Parameters:\n");
461 print_value(stderr
, bt_config_component
->params
, 8);
465 void print_bt_config_components(GPtrArray
*array
)
469 for (i
= 0; i
< array
->len
; i
++) {
470 struct bt_config_component
*cfg_component
=
471 bt_config_get_component(array
, i
);
472 print_bt_config_component(cfg_component
);
473 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
478 void print_plugin_paths(const bt_value
*plugin_paths
)
480 fprintf(stderr
, " Plugin paths:\n");
481 print_value(stderr
, plugin_paths
, 4);
485 void print_cfg_run(struct bt_config
*cfg
)
489 print_plugin_paths(cfg
->plugin_paths
);
490 fprintf(stderr
, " Source component instances:\n");
491 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
493 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
494 fprintf(stderr
, " Filter component instances:\n");
495 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
498 fprintf(stderr
, " Sink component instances:\n");
499 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
500 fprintf(stderr
, " Connections:\n");
502 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
503 struct bt_config_connection
*cfg_connection
=
504 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
507 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
508 cfg_connection
->upstream_comp_name
->str
,
509 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
510 cfg_connection
->upstream_port_glob
->str
,
511 cfg_connection
->downstream_comp_name
->str
,
512 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
513 cfg_connection
->downstream_port_glob
->str
);
518 void print_cfg_list_plugins(struct bt_config
*cfg
)
520 print_plugin_paths(cfg
->plugin_paths
);
524 void print_cfg_help(struct bt_config
*cfg
)
526 print_plugin_paths(cfg
->plugin_paths
);
530 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
532 print_plugin_paths(cfg
->plugin_paths
);
533 fprintf(stderr
, " Path: %s\n",
534 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
538 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
540 print_plugin_paths(cfg
->plugin_paths
);
541 fprintf(stderr
, " URL: %s\n",
542 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
546 void print_cfg_query(struct bt_config
*cfg
)
548 print_plugin_paths(cfg
->plugin_paths
);
549 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
550 fprintf(stderr
, " Component class:\n");
551 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
555 void print_cfg(struct bt_config
*cfg
)
557 if (!BT_LOG_ON_INFO
) {
561 BT_LOGI_STR("CLI configuration:");
563 switch (cfg
->command
) {
564 case BT_CONFIG_COMMAND_RUN
:
567 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
568 print_cfg_list_plugins(cfg
);
570 case BT_CONFIG_COMMAND_HELP
:
573 case BT_CONFIG_COMMAND_QUERY
:
574 print_cfg_query(cfg
);
576 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
577 print_cfg_print_ctf_metadata(cfg
);
579 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
580 print_cfg_print_lttng_live_sessions(cfg
);
588 void print_plugin_info(const bt_plugin
*plugin
)
590 unsigned int major
, minor
, patch
;
592 bt_property_availability version_avail
;
593 const char *plugin_name
;
597 const char *plugin_description
;
599 plugin_name
= bt_plugin_get_name(plugin
);
600 path
= bt_plugin_get_path(plugin
);
601 author
= bt_plugin_get_author(plugin
);
602 license
= bt_plugin_get_license(plugin
);
603 plugin_description
= bt_plugin_get_description(plugin
);
604 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
606 printf("%s%s%s%s:\n", bt_common_color_bold(),
607 bt_common_color_fg_blue(), plugin_name
,
608 bt_common_color_reset());
610 printf(" %sPath%s: %s\n", bt_common_color_bold(),
611 bt_common_color_reset(), path
);
616 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
617 printf(" %sVersion%s: %u.%u.%u",
618 bt_common_color_bold(), bt_common_color_reset(),
619 major
, minor
, patch
);
628 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
629 bt_common_color_reset(),
630 plugin_description
? plugin_description
: "(None)");
631 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
632 bt_common_color_reset(), author
? author
: "(Unknown)");
633 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
634 bt_common_color_reset(),
635 license
? license
: "(Unknown)");
639 int cmd_query(struct bt_config
*cfg
)
642 const bt_component_class
*comp_cls
= NULL
;
643 const bt_value
*results
= NULL
;
644 const char *fail_reason
= NULL
;
646 comp_cls
= find_component_class(
647 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
648 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
649 cfg
->cmd_data
.query
.cfg_component
->type
);
651 BT_CLI_LOGE_APPEND_CAUSE(
652 "Cannot find component class: plugin-name=\"%s\", "
653 "comp-cls-name=\"%s\", comp-cls-type=%d",
654 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
655 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
656 cfg
->cmd_data
.query
.cfg_component
->type
);
661 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
662 cfg
->cmd_data
.query
.cfg_component
->params
,
663 &results
, &fail_reason
);
668 print_value(stdout
, results
, 0);
672 BT_CLI_LOGE_APPEND_CAUSE(
673 "Failed to query component class: %s: plugin-name=\"%s\", "
674 "comp-cls-name=\"%s\", comp-cls-type=%d "
675 "object=\"%s\"", fail_reason
,
676 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
677 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
678 cfg
->cmd_data
.query
.cfg_component
->type
,
679 cfg
->cmd_data
.query
.object
->str
);
683 bt_component_class_put_ref(comp_cls
);
684 bt_value_put_ref(results
);
689 void print_component_class_help(const char *plugin_name
,
690 const bt_component_class
*comp_cls
)
692 const char *comp_class_name
=
693 bt_component_class_get_name(comp_cls
);
694 const char *comp_class_description
=
695 bt_component_class_get_description(comp_cls
);
696 const char *comp_class_help
=
697 bt_component_class_get_help(comp_cls
);
698 bt_component_class_type type
=
699 bt_component_class_get_type(comp_cls
);
701 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
703 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
704 bt_common_color_reset(),
705 comp_class_description
? comp_class_description
: "(None)");
707 if (comp_class_help
) {
708 printf("\n%s\n", comp_class_help
);
713 int cmd_help(struct bt_config
*cfg
)
716 const bt_plugin
*plugin
= NULL
;
717 const bt_component_class
*needed_comp_cls
= NULL
;
719 plugin
= find_loaded_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
721 BT_CLI_LOGE_APPEND_CAUSE(
722 "Cannot find plugin: plugin-name=\"%s\"",
723 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
728 print_plugin_info(plugin
);
729 printf(" %sSource component classes%s: %d\n",
730 bt_common_color_bold(),
731 bt_common_color_reset(),
732 (int) bt_plugin_get_source_component_class_count(plugin
));
733 printf(" %sFilter component classes%s: %d\n",
734 bt_common_color_bold(),
735 bt_common_color_reset(),
736 (int) bt_plugin_get_filter_component_class_count(plugin
));
737 printf(" %sSink component classes%s: %d\n",
738 bt_common_color_bold(),
739 bt_common_color_reset(),
740 (int) bt_plugin_get_sink_component_class_count(plugin
));
742 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
743 /* Plugin help only */
747 needed_comp_cls
= find_component_class(
748 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
749 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
750 cfg
->cmd_data
.help
.cfg_component
->type
);
751 if (!needed_comp_cls
) {
752 BT_CLI_LOGE_APPEND_CAUSE(
753 "Cannot find component class: plugin-name=\"%s\", "
754 "comp-cls-name=\"%s\", comp-cls-type=%d",
755 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
756 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
757 cfg
->cmd_data
.help
.cfg_component
->type
);
763 print_component_class_help(
764 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
768 bt_component_class_put_ref(needed_comp_cls
);
769 bt_plugin_put_ref(plugin
);
773 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
775 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
778 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
779 const char *cc_type_name
, uint64_t count
,
780 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
781 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
786 printf(" %s%s component classes%s: (none)\n",
787 bt_common_color_bold(),
789 bt_common_color_reset());
792 printf(" %s%s component classes%s:\n",
793 bt_common_color_bold(),
795 bt_common_color_reset());
798 for (i
= 0; i
< count
; i
++) {
799 const bt_component_class
*comp_class
=
800 spec_comp_cls_borrow_comp_cls_func(
801 borrow_comp_cls_by_index_func(plugin
, i
));
802 const char *comp_class_name
=
803 bt_component_class_get_name(comp_class
);
804 const char *comp_class_description
=
805 bt_component_class_get_description(comp_class
);
806 bt_component_class_type type
=
807 bt_component_class_get_type(comp_class
);
810 print_plugin_comp_cls_opt(stdout
,
811 bt_plugin_get_name(plugin
), comp_class_name
,
814 if (comp_class_description
) {
815 printf(": %s", comp_class_description
);
826 int cmd_list_plugins(struct bt_config
*cfg
)
829 int plugins_count
, component_classes_count
= 0, i
;
831 printf("From the following plugin paths:\n\n");
832 print_value(stdout
, cfg
->plugin_paths
, 2);
834 plugins_count
= get_loaded_plugins_count();
835 if (plugins_count
== 0) {
836 printf("No plugins found.\n");
840 for (i
= 0; i
< plugins_count
; i
++) {
841 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
843 component_classes_count
+=
844 bt_plugin_get_source_component_class_count(plugin
) +
845 bt_plugin_get_filter_component_class_count(plugin
) +
846 bt_plugin_get_sink_component_class_count(plugin
);
849 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
850 bt_common_color_bold(),
851 component_classes_count
,
852 bt_common_color_reset(),
853 bt_common_color_bold(),
855 bt_common_color_reset());
857 for (i
= 0; i
< plugins_count
; i
++) {
858 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
861 print_plugin_info(plugin
);
862 cmd_list_plugins_print_component_classes(plugin
, "Source",
863 bt_plugin_get_source_component_class_count(plugin
),
864 (plugin_borrow_comp_cls_by_index_func_t
)
865 bt_plugin_borrow_source_component_class_by_index_const
,
866 (spec_comp_cls_borrow_comp_cls_func_t
)
867 bt_component_class_source_as_component_class
);
868 cmd_list_plugins_print_component_classes(plugin
, "Filter",
869 bt_plugin_get_filter_component_class_count(plugin
),
870 (plugin_borrow_comp_cls_by_index_func_t
)
871 bt_plugin_borrow_filter_component_class_by_index_const
,
872 (spec_comp_cls_borrow_comp_cls_func_t
)
873 bt_component_class_filter_as_component_class
);
874 cmd_list_plugins_print_component_classes(plugin
, "Sink",
875 bt_plugin_get_sink_component_class_count(plugin
),
876 (plugin_borrow_comp_cls_by_index_func_t
)
877 bt_plugin_borrow_sink_component_class_by_index_const
,
878 (spec_comp_cls_borrow_comp_cls_func_t
)
879 bt_component_class_sink_as_component_class
);
887 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
890 const bt_component_class
*comp_cls
= NULL
;
891 const bt_value
*results
= NULL
;
892 bt_value
*params
= NULL
;
893 const bt_value
*map
= NULL
;
894 const bt_value
*v
= NULL
;
895 static const char * const plugin_name
= "ctf";
896 static const char * const comp_cls_name
= "lttng-live";
897 static const bt_component_class_type comp_cls_type
=
898 BT_COMPONENT_CLASS_TYPE_SOURCE
;
899 int64_t array_size
, i
;
900 const char *fail_reason
= NULL
;
901 FILE *out_stream
= stdout
;
903 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
904 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
907 BT_CLI_LOGE_APPEND_CAUSE(
908 "Cannot find component class: plugin-name=\"%s\", "
909 "comp-cls-name=\"%s\", comp-cls-type=%d",
910 plugin_name
, comp_cls_name
,
911 BT_COMPONENT_CLASS_TYPE_SOURCE
);
915 params
= bt_value_map_create();
920 ret
= bt_value_map_insert_string_entry(params
, "url",
921 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
926 ret
= query(cfg
, comp_cls
, "sessions", params
,
927 &results
, &fail_reason
);
934 if (!bt_value_is_array(results
)) {
935 BT_CLI_LOGE_APPEND_CAUSE(
936 "Expecting an array for LTTng live `sessions` query.");
940 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
942 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
946 BT_LOGE_ERRNO("Cannot open file for writing",
948 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
949 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
951 "Cannot open file for writing: path=\"%s\"",
952 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
957 array_size
= bt_value_array_get_length(results
);
958 for (i
= 0; i
< array_size
; i
++) {
959 const char *url_text
;
960 int64_t timer_us
, streams
, clients
;
962 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
964 BT_CLI_LOGE_APPEND_CAUSE("Unexpected empty array entry.");
967 if (!bt_value_is_map(map
)) {
968 BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type.");
972 v
= bt_value_map_borrow_entry_value_const(map
, "url");
974 BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry.");
977 url_text
= bt_value_string_get(v
);
978 fprintf(out_stream
, "%s", url_text
);
979 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
981 BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry.");
984 timer_us
= bt_value_integer_unsigned_get(v
);
985 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
986 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
988 BT_CLI_LOGE_APPEND_CAUSE(
989 "Missing `stream-count` entry.");
992 streams
= bt_value_integer_unsigned_get(v
);
993 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
994 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
996 BT_CLI_LOGE_APPEND_CAUSE(
997 "Missing `client-count` entry.");
1000 clients
= bt_value_integer_unsigned_get(v
);
1001 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1007 BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s",
1014 bt_value_put_ref(results
);
1015 bt_value_put_ref(params
);
1016 bt_component_class_put_ref(comp_cls
);
1018 if (out_stream
&& out_stream
!= stdout
) {
1019 int fclose_ret
= fclose(out_stream
);
1022 BT_LOGE_ERRNO("Cannot close file stream",
1024 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1032 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1035 const bt_component_class
*comp_cls
= NULL
;
1036 const bt_value
*results
= NULL
;
1037 bt_value
*params
= NULL
;
1038 const bt_value
*metadata_text_value
= NULL
;
1039 const char *metadata_text
= NULL
;
1040 static const char * const plugin_name
= "ctf";
1041 static const char * const comp_cls_name
= "fs";
1042 static const bt_component_class_type comp_cls_type
=
1043 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1044 const char *fail_reason
= NULL
;
1045 FILE *out_stream
= stdout
;
1047 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1048 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1051 BT_CLI_LOGE_APPEND_CAUSE(
1052 "Cannot find component class: plugin-name=\"%s\", "
1053 "comp-cls-name=\"%s\", comp-cls-type=%d",
1054 plugin_name
, comp_cls_name
,
1055 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1060 params
= bt_value_map_create();
1066 ret
= bt_value_map_insert_string_entry(params
, "path",
1067 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1073 ret
= query(cfg
, comp_cls
, "metadata-info",
1074 params
, &results
, &fail_reason
);
1079 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1081 if (!metadata_text_value
) {
1082 BT_CLI_LOGE_APPEND_CAUSE(
1083 "Cannot find `text` string value in the resulting metadata info object.");
1088 metadata_text
= bt_value_string_get(metadata_text_value
);
1090 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1092 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1095 BT_LOGE_ERRNO("Cannot open file for writing",
1097 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1098 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1100 "Cannot open file for writing: path=\"%s\"",
1101 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1107 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1109 BT_CLI_LOGE_APPEND_CAUSE(
1110 "Cannot write whole metadata text to output stream: "
1120 BT_CLI_LOGE_APPEND_CAUSE(
1121 "Failed to query `metadata-info` object: %s", fail_reason
);
1124 bt_value_put_ref(results
);
1125 bt_value_put_ref(params
);
1126 bt_component_class_put_ref(comp_cls
);
1128 if (out_stream
&& out_stream
!= stdout
) {
1129 int fclose_ret
= fclose(out_stream
);
1132 BT_LOGE_ERRNO("Cannot close file stream",
1134 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1142 char *instance_name
;
1146 struct trace_range
{
1147 uint64_t intersection_range_begin_ns
;
1148 uint64_t intersection_range_end_ns
;
1152 guint
port_id_hash(gconstpointer v
)
1154 const struct port_id
*id
= v
;
1156 BT_ASSERT(id
->instance_name
);
1157 BT_ASSERT(id
->port_name
);
1159 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1163 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1165 const struct port_id
*id1
= v1
;
1166 const struct port_id
*id2
= v2
;
1168 return strcmp(id1
->instance_name
, id2
->instance_name
) == 0 &&
1169 strcmp(id1
->port_name
, id2
->port_name
) == 0;
1173 void port_id_destroy(gpointer data
)
1175 struct port_id
*id
= data
;
1177 free(id
->instance_name
);
1178 free(id
->port_name
);
1183 void trace_range_destroy(gpointer data
)
1188 struct cmd_run_ctx
{
1190 GHashTable
*src_components
;
1193 GHashTable
*flt_components
;
1196 GHashTable
*sink_components
;
1202 struct bt_config
*cfg
;
1206 bool stream_intersection_mode
;
1209 * Association of struct port_id -> struct trace_range.
1211 GHashTable
*intersections
;
1214 /* Returns a timestamp of the form "(-)s.ns" */
1216 char *s_from_ns(int64_t ns
)
1221 int64_t ts_sec_abs
, ts_nsec_abs
;
1222 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1223 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1225 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1226 is_negative
= false;
1227 ts_sec_abs
= ts_sec
;
1228 ts_nsec_abs
= ts_nsec
;
1229 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1230 is_negative
= false;
1231 ts_sec_abs
= ts_sec
- 1;
1232 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1233 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1235 ts_sec_abs
= ts_sec
;
1236 ts_nsec_abs
= -ts_nsec
;
1237 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1239 ts_sec_abs
= -(ts_sec
+ 1);
1240 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1241 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1243 ts_sec_abs
= -ts_sec
;
1244 ts_nsec_abs
= ts_nsec
;
1245 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1247 ts_sec_abs
= -ts_sec
;
1248 ts_nsec_abs
= -ts_nsec
;
1251 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1252 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1260 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1261 struct cmd_run_ctx
*ctx
,
1262 const bt_component
*upstream_comp
,
1263 const bt_port_output
*out_upstream_port
,
1264 struct bt_config_connection
*cfg_conn
)
1266 typedef uint64_t (*input_port_count_func_t
)(void *);
1267 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1268 const void *, uint64_t);
1269 const bt_port
*upstream_port
=
1270 bt_port_output_as_port_const(out_upstream_port
);
1273 GQuark downstreamp_comp_name_quark
;
1274 void *downstream_comp
;
1275 uint64_t downstream_port_count
;
1277 input_port_count_func_t port_count_fn
;
1278 borrow_input_port_by_index_func_t port_by_index_fn
;
1279 bt_graph_connect_ports_status connect_ports_status
=
1280 BT_GRAPH_CONNECT_PORTS_STATUS_OK
;
1281 bool insert_trimmer
= false;
1282 bt_value
*trimmer_params
= NULL
;
1283 char *intersection_begin
= NULL
;
1284 char *intersection_end
= NULL
;
1285 const bt_component_filter
*trimmer
= NULL
;
1286 const bt_component_class_filter
*trimmer_class
= NULL
;
1287 const bt_port_input
*trimmer_input
= NULL
;
1288 const bt_port_output
*trimmer_output
= NULL
;
1290 if (ctx
->intersections
&&
1291 bt_component_get_class_type(upstream_comp
) ==
1292 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1293 struct trace_range
*range
;
1294 struct port_id port_id
= {
1295 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1296 .port_name
= (char *) bt_port_get_name(upstream_port
)
1299 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1303 range
= (struct trace_range
*) g_hash_table_lookup(
1304 ctx
->intersections
, &port_id
);
1306 bt_value_map_insert_entry_status insert_status
;
1308 intersection_begin
= s_from_ns(
1309 range
->intersection_range_begin_ns
);
1310 intersection_end
= s_from_ns(
1311 range
->intersection_range_end_ns
);
1312 if (!intersection_begin
|| !intersection_end
) {
1313 BT_CLI_LOGE_APPEND_CAUSE(
1314 "Cannot create trimmer argument timestamp string.");
1318 insert_trimmer
= true;
1319 trimmer_params
= bt_value_map_create();
1320 if (!trimmer_params
) {
1324 insert_status
= bt_value_map_insert_string_entry(
1325 trimmer_params
, "begin", intersection_begin
);
1326 if (insert_status
< 0) {
1329 insert_status
= bt_value_map_insert_string_entry(
1331 "end", intersection_end
);
1332 if (insert_status
< 0) {
1337 trimmer_class
= find_filter_component_class("utils", "trimmer");
1338 if (!trimmer_class
) {
1343 BT_LOGI("Connecting upstream port to the next available downstream port: "
1344 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1345 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1346 upstream_port
, bt_port_get_name(upstream_port
),
1347 cfg_conn
->downstream_comp_name
->str
,
1348 cfg_conn
->arg
->str
);
1349 downstreamp_comp_name_quark
= g_quark_from_string(
1350 cfg_conn
->downstream_comp_name
->str
);
1351 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1352 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1353 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1354 port_count_fn
= (input_port_count_func_t
)
1355 bt_component_filter_get_input_port_count
;
1356 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1357 bt_component_filter_borrow_input_port_by_index_const
;
1359 if (!downstream_comp
) {
1360 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1361 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1362 port_count_fn
= (input_port_count_func_t
)
1363 bt_component_sink_get_input_port_count
;
1364 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1365 bt_component_sink_borrow_input_port_by_index_const
;
1368 if (!downstream_comp
) {
1369 BT_CLI_LOGE_APPEND_CAUSE("Cannot find downstream component: "
1370 "comp-name=\"%s\", conn-arg=\"%s\"",
1371 cfg_conn
->downstream_comp_name
->str
,
1372 cfg_conn
->arg
->str
);
1376 downstream_port_count
= port_count_fn(downstream_comp
);
1378 for (i
= 0; i
< downstream_port_count
; i
++) {
1379 const bt_port_input
*in_downstream_port
=
1380 port_by_index_fn(downstream_comp
, i
);
1381 const bt_port
*downstream_port
=
1382 bt_port_input_as_port_const(in_downstream_port
);
1383 const char *upstream_port_name
;
1384 const char *downstream_port_name
;
1386 BT_ASSERT(downstream_port
);
1388 /* Skip port if it's already connected. */
1389 if (bt_port_is_connected(downstream_port
)) {
1390 BT_LOGI("Skipping downstream port: already connected: "
1391 "port-addr=%p, port-name=\"%s\"",
1393 bt_port_get_name(downstream_port
));
1397 downstream_port_name
= bt_port_get_name(downstream_port
);
1398 BT_ASSERT(downstream_port_name
);
1399 upstream_port_name
= bt_port_get_name(upstream_port
);
1400 BT_ASSERT(upstream_port_name
);
1402 if (!bt_common_star_glob_match(
1403 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1404 downstream_port_name
, SIZE_MAX
)) {
1408 if (insert_trimmer
) {
1410 * In order to insert the trimmer between the
1411 * two components that were being connected, we
1412 * create a connection configuration entry which
1413 * describes a connection from the trimmer's
1414 * output to the original input that was being
1417 * Hence, the creation of the trimmer will cause
1418 * the graph "new port" listener to establish
1419 * all downstream connections as its output port
1420 * is connected. We will then establish the
1421 * connection between the original upstream
1422 * source and the trimmer.
1424 char *trimmer_name
= NULL
;
1425 bt_graph_add_component_status add_comp_status
;
1427 ret
= asprintf(&trimmer_name
,
1428 "stream-intersection-trimmer-%s",
1429 upstream_port_name
);
1435 ctx
->connect_ports
= false;
1436 add_comp_status
= bt_graph_add_filter_component(
1437 ctx
->graph
, trimmer_class
, trimmer_name
,
1438 trimmer_params
, ctx
->cfg
->log_level
,
1441 if (add_comp_status
!=
1442 BT_GRAPH_ADD_COMPONENT_STATUS_OK
) {
1448 bt_component_filter_borrow_input_port_by_index_const(
1450 if (!trimmer_input
) {
1454 bt_component_filter_borrow_output_port_by_index_const(
1456 if (!trimmer_output
) {
1461 * Replace the current downstream port by the trimmer's
1464 in_downstream_port
= trimmer_input
;
1466 bt_port_input_as_port_const(in_downstream_port
);
1467 downstream_port_name
= bt_port_get_name(
1469 BT_ASSERT(downstream_port_name
);
1472 /* We have a winner! */
1473 connect_ports_status
= bt_graph_connect_ports(ctx
->graph
,
1474 out_upstream_port
, in_downstream_port
, NULL
);
1475 downstream_port
= NULL
;
1476 switch (connect_ports_status
) {
1477 case BT_GRAPH_CONNECT_PORTS_STATUS_OK
:
1480 BT_CLI_LOGE_APPEND_CAUSE(
1481 "Cannot create connection: graph refuses to connect ports: "
1482 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1483 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1484 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1485 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1487 upstream_comp
, bt_component_get_name(upstream_comp
),
1488 upstream_port
, bt_port_get_name(upstream_port
),
1489 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1490 downstream_port
, downstream_port_name
,
1491 cfg_conn
->arg
->str
);
1495 BT_LOGI("Connected component ports: "
1496 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1497 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1498 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1499 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1501 upstream_comp
, bt_component_get_name(upstream_comp
),
1502 upstream_port
, bt_port_get_name(upstream_port
),
1503 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1504 downstream_port
, downstream_port_name
,
1505 cfg_conn
->arg
->str
);
1507 if (insert_trimmer
) {
1509 * The first connection, from the source to the trimmer,
1510 * has been done. We now connect the trimmer to the
1511 * original downstream port.
1513 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1515 bt_component_filter_as_component_const(trimmer
),
1516 trimmer_output
, cfg_conn
);
1520 ctx
->connect_ports
= true;
1524 * We found a matching downstream port: the search is
1530 /* No downstream port found */
1531 BT_CLI_LOGE_APPEND_CAUSE(
1532 "Cannot create connection: cannot find a matching downstream port for upstream port: "
1533 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1534 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1535 upstream_port
, bt_port_get_name(upstream_port
),
1536 cfg_conn
->downstream_comp_name
->str
,
1537 cfg_conn
->arg
->str
);
1543 free(intersection_begin
);
1544 free(intersection_end
);
1545 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1546 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1547 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1552 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1553 const bt_port_output
*upstream_port
)
1556 const char *upstream_port_name
;
1557 const char *upstream_comp_name
;
1558 const bt_component
*upstream_comp
= NULL
;
1562 BT_ASSERT(upstream_port
);
1563 upstream_port_name
= bt_port_get_name(
1564 bt_port_output_as_port_const(upstream_port
));
1565 BT_ASSERT(upstream_port_name
);
1566 upstream_comp
= bt_port_borrow_component_const(
1567 bt_port_output_as_port_const(upstream_port
));
1568 BT_ASSERT(upstream_comp
);
1569 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1570 BT_ASSERT(upstream_comp_name
);
1571 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1572 "port-addr=%p, port-name=\"%s\"",
1573 upstream_comp
, upstream_comp_name
,
1574 upstream_port
, upstream_port_name
);
1576 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1577 struct bt_config_connection
*cfg_conn
=
1579 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1581 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1582 upstream_comp_name
)) {
1586 if (!bt_common_star_glob_match(
1587 cfg_conn
->upstream_port_glob
->str
,
1588 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1592 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1593 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1595 BT_CLI_LOGE_APPEND_CAUSE(
1596 "Cannot connect upstream port: "
1597 "port-addr=%p, port-name=\"%s\"",
1599 upstream_port_name
);
1605 BT_CLI_LOGE_APPEND_CAUSE(
1606 "Cannot connect upstream port: port does not match any connection argument: "
1607 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1608 upstream_port_name
);
1618 bt_graph_listener_func_status
1619 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1620 const bt_port_output
*out_port
)
1622 const bt_component
*comp
;
1623 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1624 bt_graph_listener_func_status ret
=
1625 BT_GRAPH_LISTENER_FUNC_STATUS_OK
;
1627 comp
= bt_port_borrow_component_const(port
);
1628 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1629 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1630 comp
, comp
? bt_component_get_name(comp
) : "",
1631 port
, bt_port_get_name(port
));
1633 if (!ctx
->connect_ports
) {
1639 if (bt_port_is_connected(port
)) {
1640 BT_LOGW_STR("Port is already connected.");
1644 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
1645 BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port.");
1646 ret
= BT_GRAPH_LISTENER_FUNC_STATUS_ERROR
;
1655 bt_graph_listener_func_status
graph_source_output_port_added_listener(
1656 const bt_component_source
*component
,
1657 const bt_port_output
*port
, void *data
)
1659 return graph_output_port_added_listener(data
, port
);
1663 bt_graph_listener_func_status
graph_filter_output_port_added_listener(
1664 const bt_component_filter
*component
,
1665 const bt_port_output
*port
, void *data
)
1667 return graph_output_port_added_listener(data
, port
);
1671 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1677 if (ctx
->src_components
) {
1678 g_hash_table_destroy(ctx
->src_components
);
1679 ctx
->src_components
= NULL
;
1682 if (ctx
->flt_components
) {
1683 g_hash_table_destroy(ctx
->flt_components
);
1684 ctx
->flt_components
= NULL
;
1687 if (ctx
->sink_components
) {
1688 g_hash_table_destroy(ctx
->sink_components
);
1689 ctx
->sink_components
= NULL
;
1692 if (ctx
->intersections
) {
1693 g_hash_table_destroy(ctx
->intersections
);
1694 ctx
->intersections
= NULL
;
1697 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
1702 int add_descriptor_to_component_descriptor_set(
1703 bt_component_descriptor_set
*comp_descr_set
,
1704 const char *plugin_name
, const char *comp_cls_name
,
1705 bt_component_class_type comp_cls_type
,
1706 const bt_value
*params
)
1708 const bt_component_class
*comp_cls
;
1711 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1714 BT_CLI_LOGE_APPEND_CAUSE(
1715 "Cannot find component class: plugin-name=\"%s\", "
1716 "comp-cls-name=\"%s\", comp-cls-type=%d",
1717 plugin_name
, comp_cls_name
, comp_cls_type
);
1722 status
= bt_component_descriptor_set_add_descriptor(
1723 comp_descr_set
, comp_cls
, params
);
1724 if (status
!= BT_COMPONENT_DESCRIPTOR_SET_ADD_DESCRIPTOR_STATUS_OK
) {
1725 BT_CLI_LOGE_APPEND_CAUSE(
1726 "Cannot append descriptor to component descriptor set: "
1727 "status=%s", bt_common_func_status_string(status
));
1732 bt_component_class_put_ref(comp_cls
);
1737 int append_descriptors_from_bt_config_component_array(
1738 bt_component_descriptor_set
*comp_descr_set
,
1739 GPtrArray
*component_configs
)
1744 for (i
= 0; i
< component_configs
->len
; i
++) {
1745 struct bt_config_component
*cfg_comp
=
1746 component_configs
->pdata
[i
];
1748 ret
= add_descriptor_to_component_descriptor_set(
1750 cfg_comp
->plugin_name
->str
,
1751 cfg_comp
->comp_cls_name
->str
,
1752 cfg_comp
->type
, cfg_comp
->params
);
1763 bt_get_greatest_operative_mip_version_status
get_greatest_operative_mip_version(
1764 struct bt_config
*cfg
, uint64_t *mip_version
)
1766 bt_get_greatest_operative_mip_version_status status
=
1767 BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK
;
1768 bt_component_descriptor_set
*comp_descr_set
= NULL
;
1772 BT_ASSERT(mip_version
);
1773 comp_descr_set
= bt_component_descriptor_set_create();
1774 if (!comp_descr_set
) {
1775 BT_CLI_LOGE_APPEND_CAUSE(
1776 "Failed to create a component descriptor set object.");
1777 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_MEMORY_ERROR
;
1781 ret
= append_descriptors_from_bt_config_component_array(
1782 comp_descr_set
, cfg
->cmd_data
.run
.sources
);
1784 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1788 ret
= append_descriptors_from_bt_config_component_array(
1789 comp_descr_set
, cfg
->cmd_data
.run
.filters
);
1791 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1795 ret
= append_descriptors_from_bt_config_component_array(
1796 comp_descr_set
, cfg
->cmd_data
.run
.sinks
);
1798 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1802 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1804 * Stream intersection mode adds `flt.utils.trimmer`
1805 * components; we need to include this type of component
1806 * in the component descriptor set to get the real
1807 * greatest operative MIP version.
1809 ret
= add_descriptor_to_component_descriptor_set(
1810 comp_descr_set
, "utils", "trimmer",
1811 BT_COMPONENT_CLASS_TYPE_FILTER
, NULL
);
1813 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1818 status
= bt_get_greatest_operative_mip_version(comp_descr_set
,
1819 bt_cli_log_level
, mip_version
);
1822 bt_component_descriptor_set_put_ref(comp_descr_set
);
1827 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1830 bt_graph_add_listener_status add_listener_status
;
1831 bt_get_greatest_operative_mip_version_status mip_version_status
;
1832 uint64_t mip_version
= UINT64_C(-1);
1835 ctx
->connect_ports
= false;
1836 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
1837 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1838 if (!ctx
->src_components
) {
1842 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
1843 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1844 if (!ctx
->flt_components
) {
1848 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
1849 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1850 if (!ctx
->sink_components
) {
1854 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1855 ctx
->stream_intersection_mode
= true;
1856 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1857 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1858 if (!ctx
->intersections
) {
1864 * Get the greatest operative MIP version to use to configure
1865 * the graph to create.
1867 mip_version_status
= get_greatest_operative_mip_version(
1869 if (mip_version_status
== BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_NO_MATCH
) {
1870 BT_CLI_LOGE_APPEND_CAUSE(
1871 "Failed to find an operative message interchange "
1872 "protocol version to use to create the `run` command's "
1873 "graph (components are not interoperable).");
1875 } else if (mip_version_status
< 0) {
1876 BT_CLI_LOGE_APPEND_CAUSE(
1877 "Cannot find an operative message interchange "
1878 "protocol version to use to create the `run` command's "
1880 bt_common_func_status_string(mip_version_status
));
1884 BT_ASSERT(mip_version_status
== BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK
);
1885 BT_LOGI("Found operative message interchange protocol version to "
1886 "configure the `run` command's graph: mip-version=%" PRIu64
,
1888 ctx
->graph
= bt_graph_create(mip_version
);
1893 bt_graph_add_interrupter(ctx
->graph
, the_interrupter
);
1894 add_listener_status
= bt_graph_add_source_component_output_port_added_listener(
1895 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
1897 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1898 BT_CLI_LOGE_APPEND_CAUSE(
1899 "Cannot add \"port added\" listener to graph.");
1903 add_listener_status
= bt_graph_add_filter_component_output_port_added_listener(
1904 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
1906 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1907 BT_CLI_LOGE_APPEND_CAUSE(
1908 "Cannot add \"port added\" listener to graph.");
1915 cmd_run_ctx_destroy(ctx
);
1923 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
1924 struct bt_config_component
*cfg_comp
,
1925 const bt_component_class_source
*src_comp_cls
)
1929 int64_t trace_count
;
1930 const char *path
= NULL
;
1931 const bt_value
*query_result
= NULL
;
1932 const bt_value
*trace_info
= NULL
;
1933 const bt_value
*intersection_range
= NULL
;
1934 const bt_value
*intersection_begin
= NULL
;
1935 const bt_value
*intersection_end
= NULL
;
1936 const bt_value
*stream_infos
= NULL
;
1937 const bt_value
*stream_info
= NULL
;
1938 struct port_id
*port_id
= NULL
;
1939 struct trace_range
*trace_range
= NULL
;
1940 const char *fail_reason
= NULL
;
1941 const bt_component_class
*comp_cls
=
1942 bt_component_class_source_as_component_class_const(src_comp_cls
);
1944 ret
= query(ctx
->cfg
, comp_cls
, "babeltrace.trace-info",
1945 cfg_comp
->params
, &query_result
,
1948 BT_LOGD("Component class does not support the `babeltrace.trace-info` query: %s: "
1949 "comp-class-name=\"%s\"", fail_reason
,
1950 bt_component_class_get_name(comp_cls
));
1955 BT_ASSERT(query_result
);
1957 if (!bt_value_is_array(query_result
)) {
1958 BT_LOGD("Unexpected format of `babeltrace.trace-info` query result: "
1959 "component-class-name=%s",
1960 bt_component_class_get_name(comp_cls
));
1965 trace_count
= bt_value_array_get_length(query_result
);
1966 if (trace_count
< 0) {
1971 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
1973 uint64_t stream_idx
;
1974 int64_t stream_count
;
1976 trace_info
= bt_value_array_borrow_element_by_index_const(
1977 query_result
, trace_idx
);
1978 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
1980 BT_LOGD_STR("Cannot retrieve trace from query result.");
1984 intersection_range
= bt_value_map_borrow_entry_value_const(
1985 trace_info
, "intersection-range-ns");
1986 if (!intersection_range
) {
1988 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
1992 intersection_begin
= bt_value_map_borrow_entry_value_const(intersection_range
,
1994 if (!intersection_begin
) {
1996 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
2000 intersection_end
= bt_value_map_borrow_entry_value_const(intersection_range
,
2002 if (!intersection_end
) {
2004 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
2008 begin
= bt_value_integer_signed_get(intersection_begin
);
2009 end
= bt_value_integer_signed_get(intersection_end
);
2011 if (begin
< 0 || end
< 0 || end
< begin
) {
2012 BT_CLI_LOGE_APPEND_CAUSE(
2013 "Invalid trace stream intersection values: "
2014 "intersection-range-ns:begin=%" PRId64
2015 ", intersection-range-ns:end=%" PRId64
,
2021 stream_infos
= bt_value_map_borrow_entry_value_const(trace_info
,
2023 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2025 BT_LOGD_STR("Cannot retrieve stream information from trace in query result.");
2029 stream_count
= bt_value_array_get_length(stream_infos
);
2030 if (stream_count
< 0) {
2035 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2036 const bt_value
*port_name
;
2038 port_id
= g_new0(struct port_id
, 1);
2041 BT_CLI_LOGE_APPEND_CAUSE(
2042 "Cannot allocate memory for port_id structure.");
2045 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2046 if (!port_id
->instance_name
) {
2048 BT_CLI_LOGE_APPEND_CAUSE(
2049 "Cannot allocate memory for port_id component instance name.");
2053 trace_range
= g_new0(struct trace_range
, 1);
2056 BT_CLI_LOGE_APPEND_CAUSE(
2057 "Cannot allocate memory for trace_range structure.");
2060 trace_range
->intersection_range_begin_ns
= begin
;
2061 trace_range
->intersection_range_end_ns
= end
;
2063 stream_info
= bt_value_array_borrow_element_by_index_const(
2064 stream_infos
, stream_idx
);
2065 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2067 BT_CLI_LOGE_APPEND_CAUSE(
2068 "Cannot retrieve stream informations from trace in query result.");
2072 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2073 if (!port_name
|| !bt_value_is_string(port_name
)) {
2075 BT_CLI_LOGE_APPEND_CAUSE(
2076 "Cannot retrieve port name in query result.");
2080 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2081 if (!port_id
->port_name
) {
2083 BT_CLI_LOGE_APPEND_CAUSE(
2084 "Cannot allocate memory for port_id port_name.");
2088 BT_LOGD("Inserting stream intersection ");
2090 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2100 BT_CLI_LOGE_APPEND_CAUSE(
2101 "Cannot determine stream intersection of trace: path=\"%s\"",
2102 path
? path
: "(unknown)");
2105 bt_value_put_ref(query_result
);
2107 g_free(trace_range
);
2112 int cmd_run_ctx_create_components_from_config_components(
2113 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2116 const void *comp_cls
= NULL
;
2117 const void *comp
= NULL
;
2120 for (i
= 0; i
< cfg_components
->len
; i
++) {
2121 struct bt_config_component
*cfg_comp
=
2122 g_ptr_array_index(cfg_components
, i
);
2125 switch (cfg_comp
->type
) {
2126 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2127 comp_cls
= find_source_component_class(
2128 cfg_comp
->plugin_name
->str
,
2129 cfg_comp
->comp_cls_name
->str
);
2131 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2132 comp_cls
= find_filter_component_class(
2133 cfg_comp
->plugin_name
->str
,
2134 cfg_comp
->comp_cls_name
->str
);
2136 case BT_COMPONENT_CLASS_TYPE_SINK
:
2137 comp_cls
= find_sink_component_class(
2138 cfg_comp
->plugin_name
->str
,
2139 cfg_comp
->comp_cls_name
->str
);
2146 BT_CLI_LOGE_APPEND_CAUSE(
2147 "Cannot find component class: plugin-name=\"%s\", "
2148 "comp-cls-name=\"%s\", comp-cls-type=%d",
2149 cfg_comp
->plugin_name
->str
,
2150 cfg_comp
->comp_cls_name
->str
,
2155 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_TRACE
);
2157 switch (cfg_comp
->type
) {
2158 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2159 ret
= bt_graph_add_source_component(ctx
->graph
,
2160 comp_cls
, cfg_comp
->instance_name
->str
,
2161 cfg_comp
->params
, cfg_comp
->log_level
,
2164 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2165 ret
= bt_graph_add_filter_component(ctx
->graph
,
2166 comp_cls
, cfg_comp
->instance_name
->str
,
2167 cfg_comp
->params
, cfg_comp
->log_level
,
2170 case BT_COMPONENT_CLASS_TYPE_SINK
:
2171 ret
= bt_graph_add_sink_component(ctx
->graph
,
2172 comp_cls
, cfg_comp
->instance_name
->str
,
2173 cfg_comp
->params
, cfg_comp
->log_level
,
2181 BT_CLI_LOGE_APPEND_CAUSE(
2182 "Cannot create component: plugin-name=\"%s\", "
2183 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2185 cfg_comp
->plugin_name
->str
,
2186 cfg_comp
->comp_cls_name
->str
,
2187 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2191 if (ctx
->stream_intersection_mode
&&
2192 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2193 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2199 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2200 comp
, cfg_comp
->instance_name
->str
);
2201 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2202 BT_ASSERT(quark
> 0);
2204 switch (cfg_comp
->type
) {
2205 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2206 g_hash_table_insert(ctx
->src_components
,
2207 GUINT_TO_POINTER(quark
), (void *) comp
);
2209 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2210 g_hash_table_insert(ctx
->flt_components
,
2211 GUINT_TO_POINTER(quark
), (void *) comp
);
2213 case BT_COMPONENT_CLASS_TYPE_SINK
:
2214 g_hash_table_insert(ctx
->sink_components
,
2215 GUINT_TO_POINTER(quark
), (void *) comp
);
2222 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2231 bt_object_put_ref(comp
);
2232 bt_object_put_ref(comp_cls
);
2237 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2242 * Make sure that, during this phase, our graph's "port added"
2243 * listener does not connect ports while we are creating the
2244 * components because we have a special, initial phase for
2247 ctx
->connect_ports
= false;
2249 ret
= cmd_run_ctx_create_components_from_config_components(
2250 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2256 ret
= cmd_run_ctx_create_components_from_config_components(
2257 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2263 ret
= cmd_run_ctx_create_components_from_config_components(
2264 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2274 typedef uint64_t (*output_port_count_func_t
)(const void *);
2275 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2276 const void *, uint64_t);
2279 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2280 void *comp
, output_port_count_func_t port_count_fn
,
2281 borrow_output_port_by_index_func_t port_by_index_fn
)
2287 count
= port_count_fn(comp
);
2289 for (i
= 0; i
< count
; i
++) {
2290 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2292 BT_ASSERT(upstream_port
);
2293 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2304 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2307 GHashTableIter iter
;
2308 gpointer g_name_quark
, g_comp
;
2310 ctx
->connect_ports
= true;
2311 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2313 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2314 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2315 (output_port_count_func_t
)
2316 bt_component_source_get_output_port_count
,
2317 (borrow_output_port_by_index_func_t
)
2318 bt_component_source_borrow_output_port_by_index_const
);
2324 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2326 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2327 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2328 (output_port_count_func_t
)
2329 bt_component_filter_get_output_port_count
,
2330 (borrow_output_port_by_index_func_t
)
2331 bt_component_filter_borrow_output_port_by_index_const
);
2342 int cmd_run(struct bt_config
*cfg
)
2345 struct cmd_run_ctx ctx
= { 0 };
2347 /* Initialize the command's context and the graph object */
2348 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2349 BT_CLI_LOGE_APPEND_CAUSE(
2350 "Cannot initialize the command's context.");
2354 if (bt_interrupter_is_set(the_interrupter
)) {
2355 BT_CLI_LOGW_APPEND_CAUSE(
2356 "Interrupted by user before creating components.");
2360 BT_LOGI_STR("Creating components.");
2362 /* Create the requested component instances */
2363 if (cmd_run_ctx_create_components(&ctx
)) {
2364 BT_CLI_LOGE_APPEND_CAUSE("Cannot create components.");
2368 if (bt_interrupter_is_set(the_interrupter
)) {
2369 BT_CLI_LOGW_APPEND_CAUSE(
2370 "Interrupted by user before connecting components.");
2374 BT_LOGI_STR("Connecting components.");
2376 /* Connect the initially visible component ports */
2377 if (cmd_run_ctx_connect_ports(&ctx
)) {
2378 BT_CLI_LOGE_APPEND_CAUSE(
2379 "Cannot connect initial component ports.");
2383 BT_LOGI_STR("Running the graph.");
2387 bt_graph_run_status run_status
= bt_graph_run(ctx
.graph
);
2390 * Reset console in case something messed with console
2391 * codes during the graph's execution.
2393 printf("%s", bt_common_color_reset());
2395 fprintf(stderr
, "%s", bt_common_color_reset());
2396 BT_LOGT("bt_graph_run() returned: status=%s",
2397 bt_common_func_status_string(run_status
));
2399 switch (run_status
) {
2400 case BT_GRAPH_RUN_STATUS_OK
:
2402 case BT_GRAPH_RUN_STATUS_AGAIN
:
2403 if (bt_interrupter_is_set(the_interrupter
)) {
2404 BT_CLI_LOGW_APPEND_CAUSE(
2405 "Graph was interrupted by user.");
2409 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2410 BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: "
2412 cfg
->cmd_data
.run
.retry_duration_us
);
2414 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2415 if (bt_interrupter_is_set(the_interrupter
)) {
2416 BT_CLI_LOGW_APPEND_CAUSE(
2417 "Graph was interrupted by user.");
2423 case BT_GRAPH_RUN_STATUS_END
:
2426 if (bt_interrupter_is_set(the_interrupter
)) {
2427 BT_CLI_LOGW_APPEND_CAUSE(
2428 "Graph was interrupted by user and failed: "
2430 bt_common_func_status_string(run_status
));
2434 BT_CLI_LOGE_APPEND_CAUSE(
2435 "Graph failed to complete successfully");
2448 cmd_run_ctx_destroy(&ctx
);
2453 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2455 const char *env_clash
;
2457 if (!cfg
->command_name
) {
2461 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2462 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2466 if (g_file_test(cfg
->command_name
,
2467 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2468 _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION
, __FILE__
, __LINE__
,
2469 BT_LOG_WARNING
, BT_LOG_TAG
,
2470 "The `%s` command was executed. "
2471 "If you meant to convert a trace located in "
2472 "the local `%s` directory, please use:\n\n"
2473 " babeltrace2 convert %s [OPTIONS]",
2474 cfg
->command_name
, cfg
->command_name
,
2480 void init_log_level(void)
2482 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2486 void print_error_causes(void)
2488 const bt_error
*error
= bt_current_thread_take_error();
2490 GString
*folded
= NULL
;
2491 unsigned int columns
;
2493 if (!error
|| bt_error_get_cause_count(error
) == 0) {
2494 fprintf(stderr
, "%s%sUnknown command-line error.%s\n",
2495 bt_common_color_bold(), bt_common_color_fg_red(),
2496 bt_common_color_reset());
2500 /* Try to get terminal width to fold the error cause messages */
2501 if (bt_common_get_term_size(&columns
, NULL
) < 0) {
2502 /* Width not found: default to 80 */
2507 * This helps visually separate the error causes from the last
2508 * logging statement.
2510 fprintf(stderr
, "\n");
2512 /* Reverse order: deepest (root) cause printed at the end */
2513 for (i
= bt_error_get_cause_count(error
) - 1; i
>= 0; i
--) {
2514 const bt_error_cause
*cause
=
2515 bt_error_borrow_cause_by_index(error
, (uint64_t) i
);
2516 const char *prefix_fmt
=
2517 i
== bt_error_get_cause_count(error
) - 1 ?
2518 "%s%sERROR%s: " : "%s%sCAUSED BY%s ";
2521 fprintf(stderr
, prefix_fmt
,
2522 bt_common_color_bold(), bt_common_color_fg_red(),
2523 bt_common_color_reset());
2525 /* Print actor name */
2526 fprintf(stderr
, "[");
2527 switch (bt_error_cause_get_actor_type(cause
)) {
2528 case BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN
:
2529 fprintf(stderr
, "%s%s%s",
2530 bt_common_color_bold(),
2531 bt_error_cause_get_module_name(cause
),
2532 bt_common_color_reset());
2534 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
:
2535 fprintf(stderr
, "%s%s%s: ",
2536 bt_common_color_bold(),
2537 bt_error_cause_component_actor_get_component_name(cause
),
2538 bt_common_color_reset());
2539 print_plugin_comp_cls_opt(stderr
,
2540 bt_error_cause_component_actor_get_plugin_name(cause
),
2541 bt_error_cause_component_actor_get_component_class_name(cause
),
2542 bt_error_cause_component_actor_get_component_class_type(cause
));
2544 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
:
2545 print_plugin_comp_cls_opt(stderr
,
2546 bt_error_cause_component_class_actor_get_plugin_name(cause
),
2547 bt_error_cause_component_class_actor_get_component_class_name(cause
),
2548 bt_error_cause_component_class_actor_get_component_class_type(cause
));
2550 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
:
2551 fprintf(stderr
, "%s%s%s (%s%s%s): ",
2552 bt_common_color_bold(),
2553 bt_error_cause_message_iterator_actor_get_component_name(cause
),
2554 bt_common_color_reset(),
2555 bt_common_color_bold(),
2556 bt_error_cause_message_iterator_actor_get_component_output_port_name(cause
),
2557 bt_common_color_reset());
2558 print_plugin_comp_cls_opt(stderr
,
2559 bt_error_cause_message_iterator_actor_get_plugin_name(cause
),
2560 bt_error_cause_message_iterator_actor_get_component_class_name(cause
),
2561 bt_error_cause_message_iterator_actor_get_component_class_type(cause
));
2567 /* Print file name and line number */
2568 fprintf(stderr
, "] (%s%s%s%s:%s%" PRIu64
"%s)\n",
2569 bt_common_color_bold(),
2570 bt_common_color_fg_magenta(),
2571 bt_error_cause_get_file_name(cause
),
2572 bt_common_color_reset(),
2573 bt_common_color_fg_green(),
2574 bt_error_cause_get_line_number(cause
),
2575 bt_common_color_reset());
2578 folded
= bt_common_fold(bt_error_cause_get_message(cause
),
2581 BT_LOGE_STR("Could not fold string.");
2582 fprintf(stderr
, "%s\n",
2583 bt_error_cause_get_message(cause
));
2587 fprintf(stderr
, "%s\n", folded
->str
);
2588 g_string_free(folded
, TRUE
);
2596 bt_error_release(error
);
2600 int main(int argc
, const char **argv
)
2604 struct bt_config
*cfg
= NULL
;
2607 set_signal_handler();
2608 init_loaded_plugins();
2610 BT_ASSERT(!the_interrupter
);
2611 the_interrupter
= bt_interrupter_create();
2612 if (!the_interrupter
) {
2613 BT_CLI_LOGE_APPEND_CAUSE("Failed to create an interrupter object.");
2618 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
,
2622 /* Quit without errors; typically usage/version */
2624 BT_LOGI_STR("Quitting without errors.");
2629 BT_CLI_LOGE_APPEND_CAUSE(
2630 "Command-line error: retcode=%d", retcode
);
2635 BT_CLI_LOGE_APPEND_CAUSE(
2636 "Failed to create a valid Babeltrace CLI configuration.");
2643 if (cfg
->command_needs_plugins
) {
2644 ret
= require_loaded_plugins(cfg
->plugin_paths
);
2646 BT_CLI_LOGE_APPEND_CAUSE(
2647 "Failed to load plugins: ret=%d", ret
);
2653 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2654 cfg
->command
, cfg
->command_name
);
2656 switch (cfg
->command
) {
2657 case BT_CONFIG_COMMAND_RUN
:
2660 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2661 ret
= cmd_list_plugins(cfg
);
2663 case BT_CONFIG_COMMAND_HELP
:
2664 ret
= cmd_help(cfg
);
2666 case BT_CONFIG_COMMAND_QUERY
:
2667 ret
= cmd_query(cfg
);
2669 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2670 ret
= cmd_print_ctf_metadata(cfg
);
2672 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2673 ret
= cmd_print_lttng_live_sessions(cfg
);
2676 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2680 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2681 cfg
->command
, cfg
->command_name
, ret
);
2682 warn_command_name_and_directory_clash(cfg
);
2683 retcode
= ret
? 1 : 0;
2686 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2687 fini_loaded_plugins();
2688 bt_interrupter_put_ref(the_interrupter
);
2691 print_error_causes();
2695 * Clear current thread's error in case there is one to avoid a
2698 bt_current_thread_clear_error();