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
50 BT_CMD_STATUS_ERROR
= -1,
51 BT_CMD_STATUS_INTERRUPTED
= -2,
55 const char *bt_cmd_status_string(enum bt_cmd_status cmd_status
)
58 case BT_CMD_STATUS_OK
:
60 case BT_CMD_STATUS_ERROR
:
62 case BT_CMD_STATUS_INTERRUPTED
:
69 /* Application's interrupter (owned by this) */
70 static bt_interrupter
*the_interrupter
;
77 BOOL WINAPI
signal_handler(DWORD signal
) {
78 if (the_interrupter
) {
79 bt_interrupter_set(the_interrupter
);
86 void set_signal_handler(void)
88 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
89 BT_LOGE("Failed to set the Ctrl+C handler.");
93 #else /* __MINGW32__ */
96 void signal_handler(int signum
)
98 if (signum
!= SIGINT
) {
102 if (the_interrupter
) {
103 bt_interrupter_set(the_interrupter
);
108 void set_signal_handler(void)
110 struct sigaction new_action
, old_action
;
112 new_action
.sa_handler
= signal_handler
;
113 sigemptyset(&new_action
.sa_mask
);
114 new_action
.sa_flags
= 0;
115 sigaction(SIGINT
, NULL
, &old_action
);
117 if (old_action
.sa_handler
!= SIG_IGN
) {
118 sigaction(SIGINT
, &new_action
, NULL
);
122 #endif /* __MINGW32__ */
125 int query(struct bt_config
*cfg
, const bt_component_class
*comp_cls
,
126 const char *obj
, const bt_value
*params
,
127 const bt_value
**user_result
, const char **fail_reason
)
129 return cli_query(comp_cls
, obj
, params
, cfg
->log_level
,
130 the_interrupter
, user_result
, fail_reason
);
133 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
134 const bt_plugin
*, const char *);
137 const void *find_component_class_from_plugin(const char *plugin_name
,
138 const char *comp_class_name
,
139 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
141 const void *comp_class
= NULL
;
142 const bt_plugin
*plugin
;
144 BT_LOGI("Finding component class: plugin-name=\"%s\", "
145 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
147 plugin
= find_loaded_plugin(plugin_name
);
152 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
153 bt_object_get_ref(comp_class
);
154 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
158 BT_LOGI("Found component class: plugin-name=\"%s\", "
159 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
161 BT_LOGI("Cannot find source component class: "
162 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
163 plugin_name
, comp_class_name
);
170 const bt_component_class_source
*find_source_component_class(
171 const char *plugin_name
, const char *comp_class_name
)
173 return (const void *) find_component_class_from_plugin(
174 plugin_name
, comp_class_name
,
175 (plugin_borrow_comp_cls_func_t
)
176 bt_plugin_borrow_source_component_class_by_name_const
);
180 const bt_component_class_filter
*find_filter_component_class(
181 const char *plugin_name
, const char *comp_class_name
)
183 return (const void *) find_component_class_from_plugin(
184 plugin_name
, comp_class_name
,
185 (plugin_borrow_comp_cls_func_t
)
186 bt_plugin_borrow_filter_component_class_by_name_const
);
190 const bt_component_class_sink
*find_sink_component_class(
191 const char *plugin_name
, const char *comp_class_name
)
193 return (const void *) find_component_class_from_plugin(plugin_name
,
195 (plugin_borrow_comp_cls_func_t
)
196 bt_plugin_borrow_sink_component_class_by_name_const
);
200 const bt_component_class
*find_component_class(const char *plugin_name
,
201 const char *comp_class_name
,
202 bt_component_class_type comp_class_type
)
204 const bt_component_class
*comp_cls
= NULL
;
206 switch (comp_class_type
) {
207 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
208 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
210 case BT_COMPONENT_CLASS_TYPE_FILTER
:
211 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
213 case BT_COMPONENT_CLASS_TYPE_SINK
:
214 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
224 void print_indent(FILE *fp
, size_t indent
)
228 for (i
= 0; i
< indent
; i
++) {
234 const char *component_type_str(bt_component_class_type type
)
237 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
239 case BT_COMPONENT_CLASS_TYPE_SINK
:
241 case BT_COMPONENT_CLASS_TYPE_FILTER
:
249 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
250 const char *comp_cls_name
, bt_component_class_type type
)
252 GString
*shell_plugin_name
= NULL
;
253 GString
*shell_comp_cls_name
= NULL
;
256 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
257 if (!shell_plugin_name
) {
262 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
263 if (!shell_comp_cls_name
) {
267 fprintf(fh
, "'%s%s%s%s",
268 bt_common_color_bold(),
269 bt_common_color_fg_bright_cyan(),
270 component_type_str(type
),
271 bt_common_color_fg_default());
273 if (shell_plugin_name
) {
274 fprintf(fh
, ".%s%s%s",
275 bt_common_color_fg_blue(),
276 shell_plugin_name
->str
,
277 bt_common_color_fg_default());
280 fprintf(fh
, ".%s%s%s'",
281 bt_common_color_fg_yellow(),
282 shell_comp_cls_name
->str
,
283 bt_common_color_reset());
286 if (shell_plugin_name
) {
287 g_string_free(shell_plugin_name
, TRUE
);
290 if (shell_comp_cls_name
) {
291 g_string_free(shell_comp_cls_name
, TRUE
);
296 void print_value(FILE *, const bt_value
*, size_t);
299 void print_value_rec(FILE *, const bt_value
*, size_t);
302 void print_map_value(const char *key
, const bt_value
*object
, FILE *fp
,
305 print_indent(fp
, indent
);
306 fprintf(fp
, "%s: ", key
);
309 if (bt_value_is_array(object
) &&
310 bt_value_array_is_empty(object
)) {
311 fprintf(fp
, "[ ]\n");
315 if (bt_value_is_map(object
) &&
316 bt_value_map_is_empty(object
)) {
317 fprintf(fp
, "{ }\n");
321 if (bt_value_is_array(object
) ||
322 bt_value_is_map(object
)) {
326 print_value_rec(fp
, object
, indent
+ 2);
333 bt_value_map_foreach_entry_const_func_status
collect_map_keys(
334 const char *key
, const bt_value
*object
, void *data
)
336 GPtrArray
*map_keys
= data
;
338 g_ptr_array_add(map_keys
, (gpointer
*) key
);
340 return BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK
;
344 gint
g_ptr_array_sort_strings(gconstpointer a
, gconstpointer b
) {
345 const char *s1
= *((const char **) a
);
346 const char *s2
= *((const char **) b
);
348 return g_strcmp0(s1
, s2
);
352 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
359 GPtrArray
*map_keys
= NULL
;
363 switch (bt_value_get_type(value
)) {
364 case BT_VALUE_TYPE_NULL
:
365 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
366 bt_common_color_reset());
368 case BT_VALUE_TYPE_BOOL
:
369 bool_val
= bt_value_bool_get(value
);
370 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
371 bt_common_color_fg_bright_cyan(), bool_val
? "yes" : "no",
372 bt_common_color_reset());
374 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
375 uint_val
= bt_value_integer_unsigned_get(value
);
376 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
377 bt_common_color_fg_bright_red(), uint_val
,
378 bt_common_color_reset());
380 case BT_VALUE_TYPE_SIGNED_INTEGER
:
381 int_val
= bt_value_integer_signed_get(value
);
382 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
383 bt_common_color_fg_bright_red(), int_val
,
384 bt_common_color_reset());
386 case BT_VALUE_TYPE_REAL
:
387 dbl_val
= bt_value_real_get(value
);
388 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
389 bt_common_color_fg_bright_red(), dbl_val
,
390 bt_common_color_reset());
392 case BT_VALUE_TYPE_STRING
:
393 str_val
= bt_value_string_get(value
);
394 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
395 bt_common_color_fg_bright_green(), str_val
,
396 bt_common_color_reset());
398 case BT_VALUE_TYPE_ARRAY
:
401 size
= bt_value_array_get_length(value
);
403 print_indent(fp
, indent
);
404 fprintf(fp
, "[ ]\n");
408 for (i
= 0; i
< size
; i
++) {
409 const bt_value
*element
=
410 bt_value_array_borrow_element_by_index_const(
413 print_indent(fp
, indent
);
416 if (bt_value_is_array(element
) &&
417 bt_value_array_is_empty(element
)) {
418 fprintf(fp
, "[ ]\n");
422 if (bt_value_is_map(element
) &&
423 bt_value_map_is_empty(element
)) {
424 fprintf(fp
, "{ }\n");
428 if (bt_value_is_array(element
) ||
429 bt_value_is_map(element
)) {
433 print_value_rec(fp
, element
, indent
+ 2);
437 case BT_VALUE_TYPE_MAP
:
440 bt_value_map_foreach_entry_const_status foreach_status
;
442 if (bt_value_map_is_empty(value
)) {
443 print_indent(fp
, indent
);
444 fprintf(fp
, "{ }\n");
448 map_keys
= g_ptr_array_new();
450 BT_CLI_LOGE_APPEND_CAUSE("Failed to allocated on GPtrArray.");
455 * We want to print the map entries in a stable order. Collect
456 * all the map's keys in a GPtrArray, sort it, then print the
457 * entries in that order.
459 foreach_status
= bt_value_map_foreach_entry_const(value
,
460 collect_map_keys
, map_keys
);
461 if (foreach_status
!= BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK
) {
462 BT_CLI_LOGE_APPEND_CAUSE("Failed to iterator on map value.");
466 g_ptr_array_sort(map_keys
, g_ptr_array_sort_strings
);
468 for (i
= 0; i
< map_keys
->len
; i
++) {
469 const char *map_key
= g_ptr_array_index(map_keys
, i
);
470 const bt_value
*map_value
;
472 map_value
= bt_value_map_borrow_entry_value_const(value
, map_key
);
473 BT_ASSERT(map_value
);
475 print_map_value(map_key
, map_value
, fp
, indent
);
488 g_ptr_array_free(map_keys
, TRUE
);
493 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
495 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
496 print_indent(fp
, indent
);
499 print_value_rec(fp
, value
, indent
);
503 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
505 fprintf(stderr
, " ");
506 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
507 bt_config_component
->comp_cls_name
->str
,
508 bt_config_component
->type
);
509 fprintf(stderr
, ":\n");
511 if (bt_config_component
->instance_name
->len
> 0) {
512 fprintf(stderr
, " Name: %s\n",
513 bt_config_component
->instance_name
->str
);
516 fprintf(stderr
, " Parameters:\n");
517 print_value(stderr
, bt_config_component
->params
, 8);
521 void print_bt_config_components(GPtrArray
*array
)
525 for (i
= 0; i
< array
->len
; i
++) {
526 struct bt_config_component
*cfg_component
=
527 bt_config_get_component(array
, i
);
528 print_bt_config_component(cfg_component
);
529 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
534 void print_plugin_paths(const bt_value
*plugin_paths
)
536 fprintf(stderr
, " Plugin paths:\n");
537 print_value(stderr
, plugin_paths
, 4);
541 void print_cfg_run(struct bt_config
*cfg
)
545 print_plugin_paths(cfg
->plugin_paths
);
546 fprintf(stderr
, " Source component instances:\n");
547 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
549 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
550 fprintf(stderr
, " Filter component instances:\n");
551 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
554 fprintf(stderr
, " Sink component instances:\n");
555 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
556 fprintf(stderr
, " Connections:\n");
558 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
559 struct bt_config_connection
*cfg_connection
=
560 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
563 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
564 cfg_connection
->upstream_comp_name
->str
,
565 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
566 cfg_connection
->upstream_port_glob
->str
,
567 cfg_connection
->downstream_comp_name
->str
,
568 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
569 cfg_connection
->downstream_port_glob
->str
);
574 void print_cfg_list_plugins(struct bt_config
*cfg
)
576 print_plugin_paths(cfg
->plugin_paths
);
580 void print_cfg_help(struct bt_config
*cfg
)
582 print_plugin_paths(cfg
->plugin_paths
);
586 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
588 print_plugin_paths(cfg
->plugin_paths
);
589 fprintf(stderr
, " Path: %s\n",
590 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
594 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
596 print_plugin_paths(cfg
->plugin_paths
);
597 fprintf(stderr
, " URL: %s\n",
598 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
602 void print_cfg_query(struct bt_config
*cfg
)
604 print_plugin_paths(cfg
->plugin_paths
);
605 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
606 fprintf(stderr
, " Component class:\n");
607 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
611 void print_cfg(struct bt_config
*cfg
)
613 if (!BT_LOG_ON_INFO
) {
617 BT_LOGI_STR("CLI configuration:");
619 switch (cfg
->command
) {
620 case BT_CONFIG_COMMAND_RUN
:
623 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
624 print_cfg_list_plugins(cfg
);
626 case BT_CONFIG_COMMAND_HELP
:
629 case BT_CONFIG_COMMAND_QUERY
:
630 print_cfg_query(cfg
);
632 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
633 print_cfg_print_ctf_metadata(cfg
);
635 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
636 print_cfg_print_lttng_live_sessions(cfg
);
644 void print_plugin_info(const bt_plugin
*plugin
)
646 unsigned int major
, minor
, patch
;
648 bt_property_availability version_avail
;
649 const char *plugin_name
;
653 const char *plugin_description
;
655 plugin_name
= bt_plugin_get_name(plugin
);
656 path
= bt_plugin_get_path(plugin
);
657 author
= bt_plugin_get_author(plugin
);
658 license
= bt_plugin_get_license(plugin
);
659 plugin_description
= bt_plugin_get_description(plugin
);
660 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
662 printf("%s%s%s%s:\n", bt_common_color_bold(),
663 bt_common_color_fg_bright_blue(), plugin_name
,
664 bt_common_color_reset());
666 printf(" %sPath%s: %s\n", bt_common_color_bold(),
667 bt_common_color_reset(), path
);
672 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
673 printf(" %sVersion%s: %u.%u.%u",
674 bt_common_color_bold(), bt_common_color_reset(),
675 major
, minor
, patch
);
684 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
685 bt_common_color_reset(),
686 plugin_description
? plugin_description
: "(None)");
687 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
688 bt_common_color_reset(), author
? author
: "(Unknown)");
689 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
690 bt_common_color_reset(),
691 license
? license
: "(Unknown)");
695 enum bt_cmd_status
cmd_query(struct bt_config
*cfg
)
698 enum bt_cmd_status cmd_status
;
699 const bt_component_class
*comp_cls
= NULL
;
700 const bt_value
*results
= NULL
;
701 const char *fail_reason
= NULL
;
703 comp_cls
= find_component_class(
704 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
705 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
706 cfg
->cmd_data
.query
.cfg_component
->type
);
708 BT_CLI_LOGE_APPEND_CAUSE(
709 "Cannot find component class: plugin-name=\"%s\", "
710 "comp-cls-name=\"%s\", comp-cls-type=%d",
711 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
712 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
713 cfg
->cmd_data
.query
.cfg_component
->type
);
717 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
718 cfg
->cmd_data
.query
.cfg_component
->params
,
719 &results
, &fail_reason
);
721 BT_CLI_LOGE_APPEND_CAUSE(
722 "Failed to query component class: %s: plugin-name=\"%s\", "
723 "comp-cls-name=\"%s\", comp-cls-type=%d "
724 "object=\"%s\"", fail_reason
,
725 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
726 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
727 cfg
->cmd_data
.query
.cfg_component
->type
,
728 cfg
->cmd_data
.query
.object
->str
);
732 print_value(stdout
, results
, 0);
733 cmd_status
= BT_CMD_STATUS_OK
;
737 cmd_status
= BT_CMD_STATUS_ERROR
;
740 bt_component_class_put_ref(comp_cls
);
741 bt_value_put_ref(results
);
746 void print_component_class_help(const char *plugin_name
,
747 const bt_component_class
*comp_cls
)
749 const char *comp_class_name
=
750 bt_component_class_get_name(comp_cls
);
751 const char *comp_class_description
=
752 bt_component_class_get_description(comp_cls
);
753 const char *comp_class_help
=
754 bt_component_class_get_help(comp_cls
);
755 bt_component_class_type type
=
756 bt_component_class_get_type(comp_cls
);
758 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
760 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
761 bt_common_color_reset(),
762 comp_class_description
? comp_class_description
: "(None)");
764 if (comp_class_help
) {
765 printf("\n%s\n", comp_class_help
);
770 enum bt_cmd_status
cmd_help(struct bt_config
*cfg
)
772 enum bt_cmd_status cmd_status
;
773 const bt_plugin
*plugin
= NULL
;
774 const bt_component_class
*needed_comp_cls
= NULL
;
776 plugin
= find_loaded_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
778 BT_CLI_LOGE_APPEND_CAUSE(
779 "Cannot find plugin: plugin-name=\"%s\"",
780 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
784 print_plugin_info(plugin
);
785 printf(" %sSource component classes%s: %d\n",
786 bt_common_color_bold(),
787 bt_common_color_reset(),
788 (int) bt_plugin_get_source_component_class_count(plugin
));
789 printf(" %sFilter component classes%s: %d\n",
790 bt_common_color_bold(),
791 bt_common_color_reset(),
792 (int) bt_plugin_get_filter_component_class_count(plugin
));
793 printf(" %sSink component classes%s: %d\n",
794 bt_common_color_bold(),
795 bt_common_color_reset(),
796 (int) bt_plugin_get_sink_component_class_count(plugin
));
798 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
799 /* Plugin help only */
800 cmd_status
= BT_CMD_STATUS_OK
;
804 needed_comp_cls
= find_component_class(
805 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
806 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
807 cfg
->cmd_data
.help
.cfg_component
->type
);
808 if (!needed_comp_cls
) {
809 BT_CLI_LOGE_APPEND_CAUSE(
810 "Cannot find component class: plugin-name=\"%s\", "
811 "comp-cls-name=\"%s\", comp-cls-type=%d",
812 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
813 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
814 cfg
->cmd_data
.help
.cfg_component
->type
);
819 print_component_class_help(
820 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
822 cmd_status
= BT_CMD_STATUS_OK
;
826 cmd_status
= BT_CMD_STATUS_ERROR
;
829 bt_component_class_put_ref(needed_comp_cls
);
830 bt_plugin_put_ref(plugin
);
834 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
836 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
840 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
841 const char *cc_type_name
, uint64_t count
,
842 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
843 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
848 printf(" %s%s component classes%s: (none)\n",
849 bt_common_color_bold(),
851 bt_common_color_reset());
854 printf(" %s%s component classes%s:\n",
855 bt_common_color_bold(),
857 bt_common_color_reset());
860 for (i
= 0; i
< count
; i
++) {
861 const bt_component_class
*comp_class
=
862 spec_comp_cls_borrow_comp_cls_func(
863 borrow_comp_cls_by_index_func(plugin
, i
));
864 const char *comp_class_name
=
865 bt_component_class_get_name(comp_class
);
866 const char *comp_class_description
=
867 bt_component_class_get_description(comp_class
);
868 bt_component_class_type type
=
869 bt_component_class_get_type(comp_class
);
872 print_plugin_comp_cls_opt(stdout
,
873 bt_plugin_get_name(plugin
), comp_class_name
,
876 if (comp_class_description
) {
877 printf(": %s", comp_class_description
);
888 enum bt_cmd_status
cmd_list_plugins(struct bt_config
*cfg
)
890 int plugins_count
, component_classes_count
= 0, i
;
892 printf("From the following plugin paths:\n\n");
893 print_value(stdout
, cfg
->plugin_paths
, 2);
895 plugins_count
= get_loaded_plugins_count();
896 if (plugins_count
== 0) {
897 printf("No plugins found.\n");
901 for (i
= 0; i
< plugins_count
; i
++) {
902 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
904 component_classes_count
+=
905 bt_plugin_get_source_component_class_count(plugin
) +
906 bt_plugin_get_filter_component_class_count(plugin
) +
907 bt_plugin_get_sink_component_class_count(plugin
);
910 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
911 bt_common_color_bold(),
912 component_classes_count
,
913 bt_common_color_reset(),
914 bt_common_color_bold(),
916 bt_common_color_reset());
918 for (i
= 0; i
< plugins_count
; i
++) {
919 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
922 print_plugin_info(plugin
);
923 cmd_list_plugins_print_component_classes(plugin
, "Source",
924 bt_plugin_get_source_component_class_count(plugin
),
925 (plugin_borrow_comp_cls_by_index_func_t
)
926 bt_plugin_borrow_source_component_class_by_index_const
,
927 (spec_comp_cls_borrow_comp_cls_func_t
)
928 bt_component_class_source_as_component_class
);
929 cmd_list_plugins_print_component_classes(plugin
, "Filter",
930 bt_plugin_get_filter_component_class_count(plugin
),
931 (plugin_borrow_comp_cls_by_index_func_t
)
932 bt_plugin_borrow_filter_component_class_by_index_const
,
933 (spec_comp_cls_borrow_comp_cls_func_t
)
934 bt_component_class_filter_as_component_class
);
935 cmd_list_plugins_print_component_classes(plugin
, "Sink",
936 bt_plugin_get_sink_component_class_count(plugin
),
937 (plugin_borrow_comp_cls_by_index_func_t
)
938 bt_plugin_borrow_sink_component_class_by_index_const
,
939 (spec_comp_cls_borrow_comp_cls_func_t
)
940 bt_component_class_sink_as_component_class
);
944 return BT_CMD_STATUS_OK
;
948 enum bt_cmd_status
cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
951 enum bt_cmd_status cmd_status
;
952 const bt_component_class
*comp_cls
= NULL
;
953 const bt_value
*results
= NULL
;
954 bt_value
*params
= NULL
;
955 const bt_value
*map
= NULL
;
956 const bt_value
*v
= NULL
;
957 static const char * const plugin_name
= "ctf";
958 static const char * const comp_cls_name
= "lttng-live";
959 static const bt_component_class_type comp_cls_type
=
960 BT_COMPONENT_CLASS_TYPE_SOURCE
;
961 uint64_t array_size
, i
;
962 const char *fail_reason
= NULL
;
963 FILE *out_stream
= stdout
;
965 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
966 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
969 BT_CLI_LOGE_APPEND_CAUSE(
970 "Cannot find component class: plugin-name=\"%s\", "
971 "comp-cls-name=\"%s\", comp-cls-type=%d",
972 plugin_name
, comp_cls_name
,
973 BT_COMPONENT_CLASS_TYPE_SOURCE
);
977 params
= bt_value_map_create();
982 ret
= bt_value_map_insert_string_entry(params
, "url",
983 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
988 ret
= query(cfg
, comp_cls
, "sessions", params
,
989 &results
, &fail_reason
);
991 BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s",
998 if (!bt_value_is_array(results
)) {
999 BT_CLI_LOGE_APPEND_CAUSE(
1000 "Expecting an array for LTTng live `sessions` query.");
1004 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
1006 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
1009 BT_LOGE_ERRNO("Cannot open file for writing",
1011 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1012 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1014 "Cannot open file for writing: path=\"%s\"",
1015 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1020 array_size
= bt_value_array_get_length(results
);
1021 for (i
= 0; i
< array_size
; i
++) {
1022 const char *url_text
;
1023 int64_t timer_us
, streams
, clients
;
1025 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1026 if (!bt_value_is_map(map
)) {
1027 BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type.");
1031 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1033 BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry.");
1036 url_text
= bt_value_string_get(v
);
1037 fprintf(out_stream
, "%s", url_text
);
1038 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1040 BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry.");
1043 timer_us
= bt_value_integer_unsigned_get(v
);
1044 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1045 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1047 BT_CLI_LOGE_APPEND_CAUSE(
1048 "Missing `stream-count` entry.");
1051 streams
= bt_value_integer_unsigned_get(v
);
1052 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1053 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1055 BT_CLI_LOGE_APPEND_CAUSE(
1056 "Missing `client-count` entry.");
1059 clients
= bt_value_integer_unsigned_get(v
);
1060 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1063 cmd_status
= BT_CMD_STATUS_OK
;
1067 cmd_status
= BT_CMD_STATUS_ERROR
;
1070 bt_value_put_ref(results
);
1071 bt_value_put_ref(params
);
1072 bt_component_class_put_ref(comp_cls
);
1074 if (out_stream
&& out_stream
!= stdout
) {
1075 int fclose_ret
= fclose(out_stream
);
1078 BT_LOGE_ERRNO("Cannot close file stream",
1080 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1088 enum bt_cmd_status
cmd_print_ctf_metadata(struct bt_config
*cfg
)
1091 enum bt_cmd_status cmd_status
;
1092 const bt_component_class
*comp_cls
= NULL
;
1093 const bt_value
*results
= NULL
;
1094 bt_value
*params
= NULL
;
1095 const bt_value
*metadata_text_value
= NULL
;
1096 const char *metadata_text
= NULL
;
1097 static const char * const plugin_name
= "ctf";
1098 static const char * const comp_cls_name
= "fs";
1099 static const bt_component_class_type comp_cls_type
=
1100 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1101 const char *fail_reason
= NULL
;
1102 FILE *out_stream
= stdout
;
1104 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1105 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1108 BT_CLI_LOGE_APPEND_CAUSE(
1109 "Cannot find component class: plugin-name=\"%s\", "
1110 "comp-cls-name=\"%s\", comp-cls-type=%d",
1111 plugin_name
, comp_cls_name
,
1112 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1116 params
= bt_value_map_create();
1121 ret
= bt_value_map_insert_string_entry(params
, "path",
1122 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1127 ret
= query(cfg
, comp_cls
, "metadata-info",
1128 params
, &results
, &fail_reason
);
1130 BT_CLI_LOGE_APPEND_CAUSE(
1131 "Failed to query `metadata-info` object: %s", fail_reason
);
1135 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1137 if (!metadata_text_value
) {
1138 BT_CLI_LOGE_APPEND_CAUSE(
1139 "Cannot find `text` string value in the resulting metadata info object.");
1143 metadata_text
= bt_value_string_get(metadata_text_value
);
1145 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1147 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1150 BT_LOGE_ERRNO("Cannot open file for writing",
1152 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1153 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1155 "Cannot open file for writing: path=\"%s\"",
1156 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1161 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1163 BT_CLI_LOGE_APPEND_CAUSE(
1164 "Cannot write whole metadata text to output stream: "
1169 cmd_status
= BT_CMD_STATUS_OK
;
1173 cmd_status
= BT_CMD_STATUS_ERROR
;
1176 bt_value_put_ref(results
);
1177 bt_value_put_ref(params
);
1178 bt_component_class_put_ref(comp_cls
);
1180 if (out_stream
&& out_stream
!= stdout
) {
1181 int fclose_ret
= fclose(out_stream
);
1184 BT_LOGE_ERRNO("Cannot close file stream",
1186 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1194 char *instance_name
;
1198 struct trace_range
{
1199 uint64_t intersection_range_begin_ns
;
1200 uint64_t intersection_range_end_ns
;
1204 guint
port_id_hash(gconstpointer v
)
1206 const struct port_id
*id
= v
;
1208 BT_ASSERT(id
->instance_name
);
1209 BT_ASSERT(id
->port_name
);
1211 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1215 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1217 const struct port_id
*id1
= v1
;
1218 const struct port_id
*id2
= v2
;
1220 return strcmp(id1
->instance_name
, id2
->instance_name
) == 0 &&
1221 strcmp(id1
->port_name
, id2
->port_name
) == 0;
1225 void port_id_destroy(gpointer data
)
1227 struct port_id
*id
= data
;
1229 free(id
->instance_name
);
1230 free(id
->port_name
);
1235 void trace_range_destroy(gpointer data
)
1240 struct cmd_run_ctx
{
1242 GHashTable
*src_components
;
1245 GHashTable
*flt_components
;
1248 GHashTable
*sink_components
;
1254 struct bt_config
*cfg
;
1258 bool stream_intersection_mode
;
1261 * Association of struct port_id -> struct trace_range.
1263 GHashTable
*intersections
;
1266 /* Returns a timestamp of the form "(-)s.ns" */
1268 char *s_from_ns(int64_t ns
)
1273 int64_t ts_sec_abs
, ts_nsec_abs
;
1274 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1275 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1277 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1278 is_negative
= false;
1279 ts_sec_abs
= ts_sec
;
1280 ts_nsec_abs
= ts_nsec
;
1281 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1282 is_negative
= false;
1283 ts_sec_abs
= ts_sec
- 1;
1284 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1285 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1287 ts_sec_abs
= ts_sec
;
1288 ts_nsec_abs
= -ts_nsec
;
1289 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1291 ts_sec_abs
= -(ts_sec
+ 1);
1292 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1293 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1295 ts_sec_abs
= -ts_sec
;
1296 ts_nsec_abs
= ts_nsec
;
1297 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1299 ts_sec_abs
= -ts_sec
;
1300 ts_nsec_abs
= -ts_nsec
;
1303 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1304 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1312 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1313 struct cmd_run_ctx
*ctx
,
1314 const bt_component
*upstream_comp
,
1315 const bt_port_output
*out_upstream_port
,
1316 struct bt_config_connection
*cfg_conn
)
1318 typedef uint64_t (*input_port_count_func_t
)(void *);
1319 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1320 const void *, uint64_t);
1321 const bt_port
*upstream_port
=
1322 bt_port_output_as_port_const(out_upstream_port
);
1325 GQuark downstreamp_comp_name_quark
;
1326 void *downstream_comp
;
1327 uint64_t downstream_port_count
;
1329 input_port_count_func_t port_count_fn
;
1330 borrow_input_port_by_index_func_t port_by_index_fn
;
1331 bt_graph_connect_ports_status connect_ports_status
=
1332 BT_GRAPH_CONNECT_PORTS_STATUS_OK
;
1333 bool insert_trimmer
= false;
1334 bt_value
*trimmer_params
= NULL
;
1335 char *intersection_begin
= NULL
;
1336 char *intersection_end
= NULL
;
1337 const bt_component_filter
*trimmer
= NULL
;
1338 const bt_component_class_filter
*trimmer_class
= NULL
;
1339 const bt_port_input
*trimmer_input
= NULL
;
1340 const bt_port_output
*trimmer_output
= NULL
;
1342 if (ctx
->intersections
&&
1343 bt_component_get_class_type(upstream_comp
) ==
1344 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1345 struct trace_range
*range
;
1346 struct port_id port_id
= {
1347 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1348 .port_name
= (char *) bt_port_get_name(upstream_port
)
1351 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1355 range
= (struct trace_range
*) g_hash_table_lookup(
1356 ctx
->intersections
, &port_id
);
1358 bt_value_map_insert_entry_status insert_status
;
1360 intersection_begin
= s_from_ns(
1361 range
->intersection_range_begin_ns
);
1362 intersection_end
= s_from_ns(
1363 range
->intersection_range_end_ns
);
1364 if (!intersection_begin
|| !intersection_end
) {
1365 BT_CLI_LOGE_APPEND_CAUSE(
1366 "Cannot create trimmer argument timestamp string.");
1370 insert_trimmer
= true;
1371 trimmer_params
= bt_value_map_create();
1372 if (!trimmer_params
) {
1376 insert_status
= bt_value_map_insert_string_entry(
1377 trimmer_params
, "begin", intersection_begin
);
1378 if (insert_status
< 0) {
1381 insert_status
= bt_value_map_insert_string_entry(
1383 "end", intersection_end
);
1384 if (insert_status
< 0) {
1389 trimmer_class
= find_filter_component_class("utils", "trimmer");
1390 if (!trimmer_class
) {
1395 BT_LOGI("Connecting upstream port to the next available downstream port: "
1396 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1397 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1398 upstream_port
, bt_port_get_name(upstream_port
),
1399 cfg_conn
->downstream_comp_name
->str
,
1400 cfg_conn
->arg
->str
);
1401 downstreamp_comp_name_quark
= g_quark_from_string(
1402 cfg_conn
->downstream_comp_name
->str
);
1403 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1404 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1405 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1406 port_count_fn
= (input_port_count_func_t
)
1407 bt_component_filter_get_input_port_count
;
1408 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1409 bt_component_filter_borrow_input_port_by_index_const
;
1411 if (!downstream_comp
) {
1412 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1413 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1414 port_count_fn
= (input_port_count_func_t
)
1415 bt_component_sink_get_input_port_count
;
1416 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1417 bt_component_sink_borrow_input_port_by_index_const
;
1420 if (!downstream_comp
) {
1421 BT_CLI_LOGE_APPEND_CAUSE("Cannot find downstream component: "
1422 "comp-name=\"%s\", conn-arg=\"%s\"",
1423 cfg_conn
->downstream_comp_name
->str
,
1424 cfg_conn
->arg
->str
);
1428 downstream_port_count
= port_count_fn(downstream_comp
);
1430 for (i
= 0; i
< downstream_port_count
; i
++) {
1431 const bt_port_input
*in_downstream_port
=
1432 port_by_index_fn(downstream_comp
, i
);
1433 const bt_port
*downstream_port
=
1434 bt_port_input_as_port_const(in_downstream_port
);
1435 const char *upstream_port_name
;
1436 const char *downstream_port_name
;
1438 BT_ASSERT(downstream_port
);
1440 /* Skip port if it's already connected. */
1441 if (bt_port_is_connected(downstream_port
)) {
1442 BT_LOGI("Skipping downstream port: already connected: "
1443 "port-addr=%p, port-name=\"%s\"",
1445 bt_port_get_name(downstream_port
));
1449 downstream_port_name
= bt_port_get_name(downstream_port
);
1450 BT_ASSERT(downstream_port_name
);
1451 upstream_port_name
= bt_port_get_name(upstream_port
);
1452 BT_ASSERT(upstream_port_name
);
1454 if (!bt_common_star_glob_match(
1455 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1456 downstream_port_name
, SIZE_MAX
)) {
1460 if (insert_trimmer
) {
1462 * In order to insert the trimmer between the
1463 * two components that were being connected, we
1464 * create a connection configuration entry which
1465 * describes a connection from the trimmer's
1466 * output to the original input that was being
1469 * Hence, the creation of the trimmer will cause
1470 * the graph "new port" listener to establish
1471 * all downstream connections as its output port
1472 * is connected. We will then establish the
1473 * connection between the original upstream
1474 * source and the trimmer.
1476 char *trimmer_name
= NULL
;
1477 bt_graph_add_component_status add_comp_status
;
1479 ret
= asprintf(&trimmer_name
,
1480 "stream-intersection-trimmer-%s",
1481 upstream_port_name
);
1487 ctx
->connect_ports
= false;
1488 add_comp_status
= bt_graph_add_filter_component(
1489 ctx
->graph
, trimmer_class
, trimmer_name
,
1490 trimmer_params
, ctx
->cfg
->log_level
,
1493 if (add_comp_status
!=
1494 BT_GRAPH_ADD_COMPONENT_STATUS_OK
) {
1500 bt_component_filter_borrow_input_port_by_index_const(
1502 if (!trimmer_input
) {
1506 bt_component_filter_borrow_output_port_by_index_const(
1508 if (!trimmer_output
) {
1513 * Replace the current downstream port by the trimmer's
1516 in_downstream_port
= trimmer_input
;
1518 bt_port_input_as_port_const(in_downstream_port
);
1519 downstream_port_name
= bt_port_get_name(
1521 BT_ASSERT(downstream_port_name
);
1524 /* We have a winner! */
1525 connect_ports_status
= bt_graph_connect_ports(ctx
->graph
,
1526 out_upstream_port
, in_downstream_port
, NULL
);
1527 downstream_port
= NULL
;
1528 switch (connect_ports_status
) {
1529 case BT_GRAPH_CONNECT_PORTS_STATUS_OK
:
1532 BT_CLI_LOGE_APPEND_CAUSE(
1533 "Cannot create connection: graph refuses to connect ports: "
1534 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1535 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1536 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1537 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1539 upstream_comp
, bt_component_get_name(upstream_comp
),
1540 upstream_port
, bt_port_get_name(upstream_port
),
1541 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1542 downstream_port
, downstream_port_name
,
1543 cfg_conn
->arg
->str
);
1547 BT_LOGI("Connected component ports: "
1548 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1549 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1550 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1551 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1553 upstream_comp
, bt_component_get_name(upstream_comp
),
1554 upstream_port
, bt_port_get_name(upstream_port
),
1555 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1556 downstream_port
, downstream_port_name
,
1557 cfg_conn
->arg
->str
);
1559 if (insert_trimmer
) {
1561 * The first connection, from the source to the trimmer,
1562 * has been done. We now connect the trimmer to the
1563 * original downstream port.
1565 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1567 bt_component_filter_as_component_const(trimmer
),
1568 trimmer_output
, cfg_conn
);
1572 ctx
->connect_ports
= true;
1576 * We found a matching downstream port: the search is
1582 /* No downstream port found */
1583 BT_CLI_LOGE_APPEND_CAUSE(
1584 "Cannot create connection: cannot find a matching downstream port for upstream port: "
1585 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1586 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1587 upstream_port
, bt_port_get_name(upstream_port
),
1588 cfg_conn
->downstream_comp_name
->str
,
1589 cfg_conn
->arg
->str
);
1595 free(intersection_begin
);
1596 free(intersection_end
);
1597 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1598 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1599 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1604 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1605 const bt_port_output
*upstream_port
)
1608 const char *upstream_port_name
;
1609 const char *upstream_comp_name
;
1610 const bt_component
*upstream_comp
= NULL
;
1614 BT_ASSERT(upstream_port
);
1615 upstream_port_name
= bt_port_get_name(
1616 bt_port_output_as_port_const(upstream_port
));
1617 BT_ASSERT(upstream_port_name
);
1618 upstream_comp
= bt_port_borrow_component_const(
1619 bt_port_output_as_port_const(upstream_port
));
1620 BT_ASSERT(upstream_comp
);
1621 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1622 BT_ASSERT(upstream_comp_name
);
1623 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1624 "port-addr=%p, port-name=\"%s\"",
1625 upstream_comp
, upstream_comp_name
,
1626 upstream_port
, upstream_port_name
);
1628 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1629 struct bt_config_connection
*cfg_conn
=
1631 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1633 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1634 upstream_comp_name
)) {
1638 if (!bt_common_star_glob_match(
1639 cfg_conn
->upstream_port_glob
->str
,
1640 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1644 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1645 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1647 BT_CLI_LOGE_APPEND_CAUSE(
1648 "Cannot connect upstream port: "
1649 "port-addr=%p, port-name=\"%s\"",
1651 upstream_port_name
);
1657 BT_CLI_LOGE_APPEND_CAUSE(
1658 "Cannot connect upstream port: port does not match any connection argument: "
1659 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1660 upstream_port_name
);
1670 bt_graph_listener_func_status
1671 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1672 const bt_port_output
*out_port
)
1674 const bt_component
*comp
;
1675 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1676 bt_graph_listener_func_status ret
=
1677 BT_GRAPH_LISTENER_FUNC_STATUS_OK
;
1679 comp
= bt_port_borrow_component_const(port
);
1680 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1681 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1682 comp
, comp
? bt_component_get_name(comp
) : "",
1683 port
, bt_port_get_name(port
));
1685 if (!ctx
->connect_ports
) {
1691 if (bt_port_is_connected(port
)) {
1692 BT_LOGW_STR("Port is already connected.");
1696 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
1697 BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port.");
1698 ret
= BT_GRAPH_LISTENER_FUNC_STATUS_ERROR
;
1707 bt_graph_listener_func_status
graph_source_output_port_added_listener(
1708 const bt_component_source
*component
,
1709 const bt_port_output
*port
, void *data
)
1711 return graph_output_port_added_listener(data
, port
);
1715 bt_graph_listener_func_status
graph_filter_output_port_added_listener(
1716 const bt_component_filter
*component
,
1717 const bt_port_output
*port
, void *data
)
1719 return graph_output_port_added_listener(data
, port
);
1723 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1729 if (ctx
->src_components
) {
1730 g_hash_table_destroy(ctx
->src_components
);
1731 ctx
->src_components
= NULL
;
1734 if (ctx
->flt_components
) {
1735 g_hash_table_destroy(ctx
->flt_components
);
1736 ctx
->flt_components
= NULL
;
1739 if (ctx
->sink_components
) {
1740 g_hash_table_destroy(ctx
->sink_components
);
1741 ctx
->sink_components
= NULL
;
1744 if (ctx
->intersections
) {
1745 g_hash_table_destroy(ctx
->intersections
);
1746 ctx
->intersections
= NULL
;
1749 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
1754 int add_descriptor_to_component_descriptor_set(
1755 bt_component_descriptor_set
*comp_descr_set
,
1756 const char *plugin_name
, const char *comp_cls_name
,
1757 bt_component_class_type comp_cls_type
,
1758 const bt_value
*params
)
1760 const bt_component_class
*comp_cls
;
1763 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1766 BT_CLI_LOGE_APPEND_CAUSE(
1767 "Cannot find component class: plugin-name=\"%s\", "
1768 "comp-cls-name=\"%s\", comp-cls-type=%d",
1769 plugin_name
, comp_cls_name
, comp_cls_type
);
1774 status
= bt_component_descriptor_set_add_descriptor(
1775 comp_descr_set
, comp_cls
, params
);
1776 if (status
!= BT_COMPONENT_DESCRIPTOR_SET_ADD_DESCRIPTOR_STATUS_OK
) {
1777 BT_CLI_LOGE_APPEND_CAUSE(
1778 "Cannot append descriptor to component descriptor set: "
1779 "status=%s", bt_common_func_status_string(status
));
1784 bt_component_class_put_ref(comp_cls
);
1789 int append_descriptors_from_bt_config_component_array(
1790 bt_component_descriptor_set
*comp_descr_set
,
1791 GPtrArray
*component_configs
)
1796 for (i
= 0; i
< component_configs
->len
; i
++) {
1797 struct bt_config_component
*cfg_comp
=
1798 component_configs
->pdata
[i
];
1800 ret
= add_descriptor_to_component_descriptor_set(
1802 cfg_comp
->plugin_name
->str
,
1803 cfg_comp
->comp_cls_name
->str
,
1804 cfg_comp
->type
, cfg_comp
->params
);
1815 bt_get_greatest_operative_mip_version_status
get_greatest_operative_mip_version(
1816 struct bt_config
*cfg
, uint64_t *mip_version
)
1818 bt_get_greatest_operative_mip_version_status status
=
1819 BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK
;
1820 bt_component_descriptor_set
*comp_descr_set
= NULL
;
1824 BT_ASSERT(mip_version
);
1825 comp_descr_set
= bt_component_descriptor_set_create();
1826 if (!comp_descr_set
) {
1827 BT_CLI_LOGE_APPEND_CAUSE(
1828 "Failed to create a component descriptor set object.");
1829 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_MEMORY_ERROR
;
1833 ret
= append_descriptors_from_bt_config_component_array(
1834 comp_descr_set
, cfg
->cmd_data
.run
.sources
);
1836 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1840 ret
= append_descriptors_from_bt_config_component_array(
1841 comp_descr_set
, cfg
->cmd_data
.run
.filters
);
1843 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1847 ret
= append_descriptors_from_bt_config_component_array(
1848 comp_descr_set
, cfg
->cmd_data
.run
.sinks
);
1850 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1854 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1856 * Stream intersection mode adds `flt.utils.trimmer`
1857 * components; we need to include this type of component
1858 * in the component descriptor set to get the real
1859 * greatest operative MIP version.
1861 ret
= add_descriptor_to_component_descriptor_set(
1862 comp_descr_set
, "utils", "trimmer",
1863 BT_COMPONENT_CLASS_TYPE_FILTER
, NULL
);
1865 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1870 status
= bt_get_greatest_operative_mip_version(comp_descr_set
,
1871 bt_cli_log_level
, mip_version
);
1874 bt_component_descriptor_set_put_ref(comp_descr_set
);
1879 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1882 bt_graph_add_listener_status add_listener_status
;
1883 bt_get_greatest_operative_mip_version_status mip_version_status
;
1884 uint64_t mip_version
= UINT64_C(-1);
1887 ctx
->connect_ports
= false;
1888 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
1889 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1890 if (!ctx
->src_components
) {
1894 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
1895 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1896 if (!ctx
->flt_components
) {
1900 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
1901 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1902 if (!ctx
->sink_components
) {
1906 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1907 ctx
->stream_intersection_mode
= true;
1908 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1909 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1910 if (!ctx
->intersections
) {
1916 * Get the greatest operative MIP version to use to configure
1917 * the graph to create.
1919 mip_version_status
= get_greatest_operative_mip_version(
1921 if (mip_version_status
== BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_NO_MATCH
) {
1922 BT_CLI_LOGE_APPEND_CAUSE(
1923 "Failed to find an operative message interchange "
1924 "protocol version to use to create the `run` command's "
1925 "graph (components are not interoperable).");
1927 } else if (mip_version_status
< 0) {
1928 BT_CLI_LOGE_APPEND_CAUSE(
1929 "Cannot find an operative message interchange "
1930 "protocol version to use to create the `run` command's "
1932 bt_common_func_status_string(mip_version_status
));
1936 BT_ASSERT(mip_version_status
== BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK
);
1937 BT_LOGI("Found operative message interchange protocol version to "
1938 "configure the `run` command's graph: mip-version=%" PRIu64
,
1940 ctx
->graph
= bt_graph_create(mip_version
);
1945 bt_graph_add_interrupter(ctx
->graph
, the_interrupter
);
1946 add_listener_status
= bt_graph_add_source_component_output_port_added_listener(
1947 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
1949 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1950 BT_CLI_LOGE_APPEND_CAUSE(
1951 "Cannot add \"port added\" listener to graph.");
1955 add_listener_status
= bt_graph_add_filter_component_output_port_added_listener(
1956 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
1958 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1959 BT_CLI_LOGE_APPEND_CAUSE(
1960 "Cannot add \"port added\" listener to graph.");
1967 cmd_run_ctx_destroy(ctx
);
1975 * Compute the intersection of all streams in the array `streams`, write it
1980 int compute_stream_intersection(const bt_value
*streams
,
1981 struct trace_range
*range
)
1983 uint64_t i
, stream_count
;
1986 BT_ASSERT(bt_value_get_type(streams
) == BT_VALUE_TYPE_ARRAY
);
1988 stream_count
= bt_value_array_get_length(streams
);
1990 BT_ASSERT(stream_count
> 0);
1992 range
->intersection_range_begin_ns
= 0;
1993 range
->intersection_range_end_ns
= UINT64_MAX
;
1995 for (i
= 0; i
< stream_count
; i
++) {
1996 int64_t begin_ns
, end_ns
;
1997 uint64_t begin_ns_u
, end_ns_u
;
1998 const bt_value
*stream_value
;
1999 const bt_value
*range_ns_value
;
2000 const bt_value
*begin_value
;
2001 const bt_value
*end_value
;
2003 stream_value
= bt_value_array_borrow_element_by_index_const(streams
, i
);
2004 if (bt_value_get_type(stream_value
) != BT_VALUE_TYPE_MAP
) {
2005 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
2006 "expected streams array element to be a map, got %s.",
2007 bt_common_value_type_string(bt_value_get_type(stream_value
)));
2011 range_ns_value
= bt_value_map_borrow_entry_value_const(
2012 stream_value
, "range-ns");
2013 if (!range_ns_value
) {
2014 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
2015 "missing expected `range-ns` key in stream map.");
2019 if (bt_value_get_type(range_ns_value
) != BT_VALUE_TYPE_MAP
) {
2020 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
2021 "expected `range-ns` entry value of stream map to be a map, got %s.",
2022 bt_common_value_type_string(bt_value_get_type(range_ns_value
)));
2026 begin_value
= bt_value_map_borrow_entry_value_const(range_ns_value
, "begin");
2028 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
2029 "missing expected `begin` key in range-ns map.");
2033 if (bt_value_get_type(begin_value
) != BT_VALUE_TYPE_SIGNED_INTEGER
) {
2034 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
2035 "expected `begin` entry value of range-ns map to be a signed integer, got %s.",
2036 bt_common_value_type_string(bt_value_get_type(range_ns_value
)));
2040 end_value
= bt_value_map_borrow_entry_value_const(range_ns_value
, "end");
2042 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
2043 "missing expected `end` key in range-ns map.");
2047 if (bt_value_get_type(end_value
) != BT_VALUE_TYPE_SIGNED_INTEGER
) {
2048 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
2049 "expected `end` entry value of range-ns map to be a signed integer, got %s.",
2050 bt_common_value_type_string(bt_value_get_type(range_ns_value
)));
2054 begin_ns
= bt_value_integer_signed_get(begin_value
);
2055 end_ns
= bt_value_integer_signed_get(end_value
);
2057 if (begin_ns
< 0 || end_ns
< 0 || end_ns
< begin_ns
) {
2058 BT_CLI_LOGE_APPEND_CAUSE(
2059 "Invalid stream range values: "
2060 "range-ns:begin=%" PRId64
", "
2061 "range-ns:end=%" PRId64
,
2066 begin_ns_u
= begin_ns
;
2069 range
->intersection_range_begin_ns
=
2070 MAX(range
->intersection_range_begin_ns
, begin_ns_u
);
2071 range
->intersection_range_end_ns
=
2072 MIN(range
->intersection_range_end_ns
, end_ns_u
);
2085 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2086 struct bt_config_component
*cfg_comp
,
2087 const bt_component_class_source
*src_comp_cls
)
2091 uint64_t trace_count
;
2092 const bt_value
*query_result
= NULL
;
2093 const bt_value
*trace_info
= NULL
;
2094 const bt_value
*stream_infos
= NULL
;
2095 const bt_value
*stream_info
= NULL
;
2096 struct port_id
*port_id
= NULL
;
2097 struct trace_range
*stream_intersection
= NULL
;
2098 const char *fail_reason
= NULL
;
2099 const bt_component_class
*comp_cls
=
2100 bt_component_class_source_as_component_class_const(src_comp_cls
);
2102 ret
= query(ctx
->cfg
, comp_cls
, "babeltrace.trace-infos",
2103 cfg_comp
->params
, &query_result
,
2106 BT_CLI_LOGE_APPEND_CAUSE("Failed to execute `babeltrace.trace-infos` query: %s: "
2107 "comp-class-name=\"%s\"", fail_reason
,
2108 bt_component_class_get_name(comp_cls
));
2113 BT_ASSERT(query_result
);
2115 if (!bt_value_is_array(query_result
)) {
2116 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting result to be an array: "
2117 "component-class-name=%s, actual-type=%s",
2118 bt_component_class_get_name(comp_cls
),
2119 bt_common_value_type_string(bt_value_get_type(query_result
)));
2124 trace_count
= bt_value_array_get_length(query_result
);
2125 if (trace_count
== 0) {
2126 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: result is empty: "
2127 "component-class-name=%s", bt_component_class_get_name(comp_cls
));
2132 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2133 uint64_t stream_idx
;
2134 int64_t stream_count
;
2135 struct trace_range trace_intersection
;
2137 trace_info
= bt_value_array_borrow_element_by_index_const(
2138 query_result
, trace_idx
);
2139 if (!bt_value_is_map(trace_info
)) {
2141 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting element to be a map: "
2142 "component-class-name=%s, actual-type=%s",
2143 bt_component_class_get_name(comp_cls
),
2144 bt_common_value_type_string(bt_value_get_type(trace_info
)));
2148 stream_infos
= bt_value_map_borrow_entry_value_const(
2149 trace_info
, "stream-infos");
2150 if (!stream_infos
) {
2152 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: missing `streams` key in trace info map: "
2153 "component-class-name=%s",
2154 bt_component_class_get_name(comp_cls
));
2158 if (!bt_value_is_array(stream_infos
)) {
2160 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting `streams` entry of trace info map to be an array: "
2161 "component-class-name=%s, actual-type=%s",
2162 bt_component_class_get_name(comp_cls
),
2163 bt_common_value_type_string(bt_value_get_type(stream_infos
)));
2167 stream_count
= bt_value_array_get_length(stream_infos
);
2168 if (stream_count
== 0) {
2170 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: list of streams is empty: "
2171 "component-class-name=%s",
2172 bt_component_class_get_name(comp_cls
));
2176 ret
= compute_stream_intersection(stream_infos
, &trace_intersection
);
2178 BT_CLI_LOGE_APPEND_CAUSE("Failed to compute trace streams intersection.");
2182 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2183 const bt_value
*port_name
;
2185 port_id
= g_new0(struct port_id
, 1);
2188 BT_CLI_LOGE_APPEND_CAUSE(
2189 "Cannot allocate memory for port_id structure.");
2192 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2193 if (!port_id
->instance_name
) {
2195 BT_CLI_LOGE_APPEND_CAUSE(
2196 "Cannot allocate memory for port_id component instance name.");
2200 stream_intersection
= g_new0(struct trace_range
, 1);
2201 if (!stream_intersection
) {
2203 BT_CLI_LOGE_APPEND_CAUSE(
2204 "Cannot allocate memory for trace_range structure.");
2208 *stream_intersection
= trace_intersection
;
2210 stream_info
= bt_value_array_borrow_element_by_index_const(
2211 stream_infos
, stream_idx
);
2212 if (!bt_value_is_map(stream_info
)) {
2214 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
2215 "expecting element of stream list to be a map: "
2216 "component-class-name=%s, actual-type=%s",
2217 bt_component_class_get_name(comp_cls
),
2218 bt_common_value_type_string(bt_value_get_type(stream_info
)));
2222 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2225 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
2226 "missing `port-name` key in stream info map: "
2227 "component-class-name=%s",
2228 bt_component_class_get_name(comp_cls
));
2232 if (!bt_value_is_string(port_name
)) {
2234 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
2235 "expecting `port-name` entry of stream info map to be a string: "
2236 "component-class-name=%s, actual-type=%s",
2237 bt_component_class_get_name(comp_cls
),
2238 bt_common_value_type_string(bt_value_get_type(port_name
)));
2242 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2243 if (!port_id
->port_name
) {
2245 BT_CLI_LOGE_APPEND_CAUSE(
2246 "Cannot allocate memory for port_id port_name.");
2250 BT_LOGD("Inserting stream intersection ");
2252 g_hash_table_insert(ctx
->intersections
, port_id
, stream_intersection
);
2255 stream_intersection
= NULL
;
2262 bt_value_put_ref(query_result
);
2264 g_free(stream_intersection
);
2269 int cmd_run_ctx_create_components_from_config_components(
2270 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2273 const void *comp_cls
= NULL
;
2274 const void *comp
= NULL
;
2277 for (i
= 0; i
< cfg_components
->len
; i
++) {
2278 struct bt_config_component
*cfg_comp
=
2279 g_ptr_array_index(cfg_components
, i
);
2282 switch (cfg_comp
->type
) {
2283 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2284 comp_cls
= find_source_component_class(
2285 cfg_comp
->plugin_name
->str
,
2286 cfg_comp
->comp_cls_name
->str
);
2288 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2289 comp_cls
= find_filter_component_class(
2290 cfg_comp
->plugin_name
->str
,
2291 cfg_comp
->comp_cls_name
->str
);
2293 case BT_COMPONENT_CLASS_TYPE_SINK
:
2294 comp_cls
= find_sink_component_class(
2295 cfg_comp
->plugin_name
->str
,
2296 cfg_comp
->comp_cls_name
->str
);
2303 BT_CLI_LOGE_APPEND_CAUSE(
2304 "Cannot find component class: plugin-name=\"%s\", "
2305 "comp-cls-name=\"%s\", comp-cls-type=%d",
2306 cfg_comp
->plugin_name
->str
,
2307 cfg_comp
->comp_cls_name
->str
,
2312 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_TRACE
);
2314 switch (cfg_comp
->type
) {
2315 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2316 ret
= bt_graph_add_source_component(ctx
->graph
,
2317 comp_cls
, cfg_comp
->instance_name
->str
,
2318 cfg_comp
->params
, cfg_comp
->log_level
,
2321 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2322 ret
= bt_graph_add_filter_component(ctx
->graph
,
2323 comp_cls
, cfg_comp
->instance_name
->str
,
2324 cfg_comp
->params
, cfg_comp
->log_level
,
2327 case BT_COMPONENT_CLASS_TYPE_SINK
:
2328 ret
= bt_graph_add_sink_component(ctx
->graph
,
2329 comp_cls
, cfg_comp
->instance_name
->str
,
2330 cfg_comp
->params
, cfg_comp
->log_level
,
2338 BT_CLI_LOGE_APPEND_CAUSE(
2339 "Cannot create component: plugin-name=\"%s\", "
2340 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2342 cfg_comp
->plugin_name
->str
,
2343 cfg_comp
->comp_cls_name
->str
,
2344 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2348 if (ctx
->stream_intersection_mode
&&
2349 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2350 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2352 BT_CLI_LOGE_APPEND_CAUSE(
2353 "Cannot determine stream intersection of trace.");
2358 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2359 comp
, cfg_comp
->instance_name
->str
);
2360 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2361 BT_ASSERT(quark
> 0);
2363 switch (cfg_comp
->type
) {
2364 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2365 g_hash_table_insert(ctx
->src_components
,
2366 GUINT_TO_POINTER(quark
), (void *) comp
);
2368 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2369 g_hash_table_insert(ctx
->flt_components
,
2370 GUINT_TO_POINTER(quark
), (void *) comp
);
2372 case BT_COMPONENT_CLASS_TYPE_SINK
:
2373 g_hash_table_insert(ctx
->sink_components
,
2374 GUINT_TO_POINTER(quark
), (void *) comp
);
2381 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2390 bt_object_put_ref(comp
);
2391 bt_object_put_ref(comp_cls
);
2396 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2401 * Make sure that, during this phase, our graph's "port added"
2402 * listener does not connect ports while we are creating the
2403 * components because we have a special, initial phase for
2406 ctx
->connect_ports
= false;
2408 ret
= cmd_run_ctx_create_components_from_config_components(
2409 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2415 ret
= cmd_run_ctx_create_components_from_config_components(
2416 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2422 ret
= cmd_run_ctx_create_components_from_config_components(
2423 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2433 typedef uint64_t (*output_port_count_func_t
)(const void *);
2434 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2435 const void *, uint64_t);
2438 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2439 void *comp
, output_port_count_func_t port_count_fn
,
2440 borrow_output_port_by_index_func_t port_by_index_fn
)
2446 count
= port_count_fn(comp
);
2448 for (i
= 0; i
< count
; i
++) {
2449 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2451 BT_ASSERT(upstream_port
);
2452 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2463 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2466 GHashTableIter iter
;
2467 gpointer g_name_quark
, g_comp
;
2469 ctx
->connect_ports
= true;
2470 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2472 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2473 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2474 (output_port_count_func_t
)
2475 bt_component_source_get_output_port_count
,
2476 (borrow_output_port_by_index_func_t
)
2477 bt_component_source_borrow_output_port_by_index_const
);
2483 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2485 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2486 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2487 (output_port_count_func_t
)
2488 bt_component_filter_get_output_port_count
,
2489 (borrow_output_port_by_index_func_t
)
2490 bt_component_filter_borrow_output_port_by_index_const
);
2501 enum bt_cmd_status
cmd_run(struct bt_config
*cfg
)
2503 enum bt_cmd_status cmd_status
;
2504 struct cmd_run_ctx ctx
= { 0 };
2506 /* Initialize the command's context and the graph object */
2507 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2508 BT_CLI_LOGE_APPEND_CAUSE(
2509 "Cannot initialize the command's context.");
2513 if (bt_interrupter_is_set(the_interrupter
)) {
2514 BT_CLI_LOGW_APPEND_CAUSE(
2515 "Interrupted by user before creating components.");
2519 BT_LOGI_STR("Creating components.");
2521 /* Create the requested component instances */
2522 if (cmd_run_ctx_create_components(&ctx
)) {
2523 BT_CLI_LOGE_APPEND_CAUSE("Cannot create components.");
2527 if (bt_interrupter_is_set(the_interrupter
)) {
2528 BT_CLI_LOGW_APPEND_CAUSE(
2529 "Interrupted by user before connecting components.");
2533 BT_LOGI_STR("Connecting components.");
2535 /* Connect the initially visible component ports */
2536 if (cmd_run_ctx_connect_ports(&ctx
)) {
2537 BT_CLI_LOGE_APPEND_CAUSE(
2538 "Cannot connect initial component ports.");
2542 BT_LOGI_STR("Running the graph.");
2546 bt_graph_run_status run_status
= bt_graph_run(ctx
.graph
);
2549 * Reset console in case something messed with console
2550 * codes during the graph's execution.
2552 printf("%s", bt_common_color_reset());
2554 fprintf(stderr
, "%s", bt_common_color_reset());
2555 BT_LOGT("bt_graph_run() returned: status=%s",
2556 bt_common_func_status_string(run_status
));
2558 switch (run_status
) {
2559 case BT_GRAPH_RUN_STATUS_OK
:
2560 cmd_status
= BT_CMD_STATUS_OK
;
2562 case BT_GRAPH_RUN_STATUS_AGAIN
:
2563 if (bt_interrupter_is_set(the_interrupter
)) {
2565 * The graph was interrupted by a SIGINT.
2567 cmd_status
= BT_CMD_STATUS_INTERRUPTED
;
2571 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2572 BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: "
2574 cfg
->cmd_data
.run
.retry_duration_us
);
2576 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2577 if (bt_interrupter_is_set(the_interrupter
)) {
2578 cmd_status
= BT_CMD_STATUS_INTERRUPTED
;
2585 if (bt_interrupter_is_set(the_interrupter
)) {
2586 cmd_status
= BT_CMD_STATUS_INTERRUPTED
;
2590 BT_CLI_LOGE_APPEND_CAUSE(
2591 "Graph failed to complete successfully");
2596 cmd_status
= BT_CMD_STATUS_ERROR
;
2599 cmd_run_ctx_destroy(&ctx
);
2604 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2606 const char *env_clash
;
2608 if (!cfg
->command_name
) {
2612 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2613 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2617 if (g_file_test(cfg
->command_name
,
2618 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2619 _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION
, __FILE__
, __LINE__
,
2620 BT_LOG_WARNING
, BT_LOG_TAG
,
2621 "The `%s` command was executed. "
2622 "If you meant to convert a trace located in "
2623 "the local `%s` directory, please use:\n\n"
2624 " babeltrace2 convert %s [OPTIONS]",
2625 cfg
->command_name
, cfg
->command_name
,
2631 void init_log_level(void)
2633 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2637 void print_error_causes(void)
2639 const bt_error
*error
= bt_current_thread_take_error();
2641 GString
*folded
= NULL
;
2642 unsigned int columns
;
2644 if (!error
|| bt_error_get_cause_count(error
) == 0) {
2645 fprintf(stderr
, "%s%sUnknown command-line error.%s\n",
2646 bt_common_color_bold(), bt_common_color_fg_bright_red(),
2647 bt_common_color_reset());
2651 /* Try to get terminal width to fold the error cause messages */
2652 if (bt_common_get_term_size(&columns
, NULL
) < 0) {
2653 /* Width not found: default to 80 */
2658 * This helps visually separate the error causes from the last
2659 * logging statement.
2661 fprintf(stderr
, "\n");
2663 /* Reverse order: deepest (root) cause printed at the end */
2664 for (i
= bt_error_get_cause_count(error
) - 1; i
>= 0; i
--) {
2665 const bt_error_cause
*cause
=
2666 bt_error_borrow_cause_by_index(error
, (uint64_t) i
);
2667 const char *prefix_fmt
=
2668 i
== bt_error_get_cause_count(error
) - 1 ?
2669 "%s%sERROR%s: " : "%s%sCAUSED BY%s ";
2672 fprintf(stderr
, prefix_fmt
,
2673 bt_common_color_bold(), bt_common_color_fg_bright_red(),
2674 bt_common_color_reset());
2676 /* Print actor name */
2677 fprintf(stderr
, "[");
2678 switch (bt_error_cause_get_actor_type(cause
)) {
2679 case BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN
:
2680 fprintf(stderr
, "%s%s%s",
2681 bt_common_color_bold(),
2682 bt_error_cause_get_module_name(cause
),
2683 bt_common_color_reset());
2685 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
:
2686 fprintf(stderr
, "%s%s%s: ",
2687 bt_common_color_bold(),
2688 bt_error_cause_component_actor_get_component_name(cause
),
2689 bt_common_color_reset());
2690 print_plugin_comp_cls_opt(stderr
,
2691 bt_error_cause_component_actor_get_plugin_name(cause
),
2692 bt_error_cause_component_actor_get_component_class_name(cause
),
2693 bt_error_cause_component_actor_get_component_class_type(cause
));
2695 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
:
2696 print_plugin_comp_cls_opt(stderr
,
2697 bt_error_cause_component_class_actor_get_plugin_name(cause
),
2698 bt_error_cause_component_class_actor_get_component_class_name(cause
),
2699 bt_error_cause_component_class_actor_get_component_class_type(cause
));
2701 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
:
2702 fprintf(stderr
, "%s%s%s (%s%s%s): ",
2703 bt_common_color_bold(),
2704 bt_error_cause_message_iterator_actor_get_component_name(cause
),
2705 bt_common_color_reset(),
2706 bt_common_color_bold(),
2707 bt_error_cause_message_iterator_actor_get_component_output_port_name(cause
),
2708 bt_common_color_reset());
2709 print_plugin_comp_cls_opt(stderr
,
2710 bt_error_cause_message_iterator_actor_get_plugin_name(cause
),
2711 bt_error_cause_message_iterator_actor_get_component_class_name(cause
),
2712 bt_error_cause_message_iterator_actor_get_component_class_type(cause
));
2718 /* Print file name and line number */
2719 fprintf(stderr
, "] (%s%s%s%s:%s%" PRIu64
"%s)\n",
2720 bt_common_color_bold(),
2721 bt_common_color_fg_bright_magenta(),
2722 bt_error_cause_get_file_name(cause
),
2723 bt_common_color_reset(),
2724 bt_common_color_fg_green(),
2725 bt_error_cause_get_line_number(cause
),
2726 bt_common_color_reset());
2729 folded
= bt_common_fold(bt_error_cause_get_message(cause
),
2732 BT_LOGE_STR("Could not fold string.");
2733 fprintf(stderr
, "%s\n",
2734 bt_error_cause_get_message(cause
));
2738 fprintf(stderr
, "%s\n", folded
->str
);
2739 g_string_free(folded
, TRUE
);
2747 bt_error_release(error
);
2751 int main(int argc
, const char **argv
)
2754 enum bt_cmd_status cmd_status
;
2755 struct bt_config
*cfg
= NULL
;
2758 set_signal_handler();
2759 init_loaded_plugins();
2761 BT_ASSERT(!the_interrupter
);
2762 the_interrupter
= bt_interrupter_create();
2763 if (!the_interrupter
) {
2764 BT_CLI_LOGE_APPEND_CAUSE("Failed to create an interrupter object.");
2769 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
,
2773 /* Quit without errors; typically usage/version */
2775 BT_LOGI_STR("Quitting without errors.");
2780 BT_CLI_LOGE_APPEND_CAUSE(
2781 "Command-line error: retcode=%d", retcode
);
2786 BT_CLI_LOGE_APPEND_CAUSE(
2787 "Failed to create a valid Babeltrace CLI configuration.");
2794 if (cfg
->command_needs_plugins
) {
2795 ret
= require_loaded_plugins(cfg
->plugin_paths
);
2797 BT_CLI_LOGE_APPEND_CAUSE(
2798 "Failed to load plugins: ret=%d", ret
);
2804 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2805 cfg
->command
, cfg
->command_name
);
2807 switch (cfg
->command
) {
2808 case BT_CONFIG_COMMAND_RUN
:
2809 cmd_status
= cmd_run(cfg
);
2811 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2812 cmd_status
= cmd_list_plugins(cfg
);
2814 case BT_CONFIG_COMMAND_HELP
:
2815 cmd_status
= cmd_help(cfg
);
2817 case BT_CONFIG_COMMAND_QUERY
:
2818 cmd_status
= cmd_query(cfg
);
2820 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2821 cmd_status
= cmd_print_ctf_metadata(cfg
);
2823 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2824 cmd_status
= cmd_print_lttng_live_sessions(cfg
);
2827 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2831 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", command-status=\"%s\"",
2832 cfg
->command
, cfg
->command_name
, bt_cmd_status_string(cmd_status
));
2833 warn_command_name_and_directory_clash(cfg
);
2835 switch (cmd_status
) {
2836 case BT_CMD_STATUS_OK
:
2839 case BT_CMD_STATUS_ERROR
:
2841 print_error_causes();
2843 case BT_CMD_STATUS_INTERRUPTED
:
2847 BT_LOGF("Invalid command status: cmd-status=%d", cmd_status
);
2852 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2853 fini_loaded_plugins();
2854 bt_interrupter_put_ref(the_interrupter
);
2857 * Clear current thread's error in case there is one to avoid a
2860 bt_current_thread_clear_error();