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);
281 void print_map_value(const char *key
, const bt_value
*object
, FILE *fp
,
284 print_indent(fp
, indent
);
285 fprintf(fp
, "%s: ", key
);
288 if (bt_value_is_array(object
) &&
289 bt_value_array_is_empty(object
)) {
290 fprintf(fp
, "[ ]\n");
294 if (bt_value_is_map(object
) &&
295 bt_value_map_is_empty(object
)) {
296 fprintf(fp
, "{ }\n");
300 if (bt_value_is_array(object
) ||
301 bt_value_is_map(object
)) {
305 print_value_rec(fp
, object
, indent
+ 2);
312 bt_bool
collect_map_keys(const char *key
, const bt_value
*object
, void *data
)
314 GPtrArray
*map_keys
= data
;
316 g_ptr_array_add(map_keys
, (gpointer
*) key
);
322 gint
g_ptr_array_sort_strings(gconstpointer a
, gconstpointer b
) {
323 const char *s1
= *((const char **) a
);
324 const char *s2
= *((const char **) b
);
326 return g_strcmp0(s1
, s2
);
330 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
339 GPtrArray
*map_keys
= NULL
;
345 switch (bt_value_get_type(value
)) {
346 case BT_VALUE_TYPE_NULL
:
347 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
348 bt_common_color_reset());
350 case BT_VALUE_TYPE_BOOL
:
351 bool_val
= bt_value_bool_get(value
);
352 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
353 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
354 bt_common_color_reset());
356 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
357 uint_val
= bt_value_integer_unsigned_get(value
);
358 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
359 bt_common_color_fg_red(), uint_val
,
360 bt_common_color_reset());
362 case BT_VALUE_TYPE_SIGNED_INTEGER
:
363 int_val
= bt_value_integer_signed_get(value
);
364 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
365 bt_common_color_fg_red(), int_val
,
366 bt_common_color_reset());
368 case BT_VALUE_TYPE_REAL
:
369 dbl_val
= bt_value_real_get(value
);
370 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
371 bt_common_color_fg_red(), dbl_val
,
372 bt_common_color_reset());
374 case BT_VALUE_TYPE_STRING
:
375 str_val
= bt_value_string_get(value
);
376 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
377 bt_common_color_fg_green(), str_val
,
378 bt_common_color_reset());
380 case BT_VALUE_TYPE_ARRAY
:
381 size
= bt_value_array_get_length(value
);
387 print_indent(fp
, indent
);
388 fprintf(fp
, "[ ]\n");
392 for (i
= 0; i
< size
; i
++) {
393 const bt_value
*element
=
394 bt_value_array_borrow_element_by_index_const(
400 print_indent(fp
, indent
);
403 if (bt_value_is_array(element
) &&
404 bt_value_array_is_empty(element
)) {
405 fprintf(fp
, "[ ]\n");
409 if (bt_value_is_map(element
) &&
410 bt_value_map_is_empty(element
)) {
411 fprintf(fp
, "{ }\n");
415 if (bt_value_is_array(element
) ||
416 bt_value_is_map(element
)) {
420 print_value_rec(fp
, element
, indent
+ 2);
423 case BT_VALUE_TYPE_MAP
:
426 bt_value_map_foreach_entry_const_status foreach_status
;
428 if (bt_value_map_is_empty(value
)) {
429 print_indent(fp
, indent
);
430 fprintf(fp
, "{ }\n");
434 map_keys
= g_ptr_array_new();
436 BT_CLI_LOGE_APPEND_CAUSE("Failed to allocated on GPtrArray.");
441 * We want to print the map entries in a stable order. Collect
442 * all the map's keys in a GPtrArray, sort it, then print the
443 * entries in that order.
445 foreach_status
= bt_value_map_foreach_entry_const(value
,
446 collect_map_keys
, map_keys
);
447 if (foreach_status
!= BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK
) {
448 BT_CLI_LOGE_APPEND_CAUSE("Failed to iterator on map value.");
452 g_ptr_array_sort(map_keys
, g_ptr_array_sort_strings
);
454 for (i
= 0; i
< map_keys
->len
; i
++) {
455 const char *map_key
= g_ptr_array_index(map_keys
, i
);
456 const bt_value
*map_value
;
458 map_value
= bt_value_map_borrow_entry_value_const(value
, map_key
);
459 BT_ASSERT(map_value
);
461 print_map_value(map_key
, map_value
, fp
, indent
);
473 BT_LOGE("Error printing value of type %s.",
474 bt_common_value_type_string(bt_value_get_type(value
)));
478 g_ptr_array_free(map_keys
, TRUE
);
483 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
485 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
486 print_indent(fp
, indent
);
489 print_value_rec(fp
, value
, indent
);
493 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
495 fprintf(stderr
, " ");
496 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
497 bt_config_component
->comp_cls_name
->str
,
498 bt_config_component
->type
);
499 fprintf(stderr
, ":\n");
501 if (bt_config_component
->instance_name
->len
> 0) {
502 fprintf(stderr
, " Name: %s\n",
503 bt_config_component
->instance_name
->str
);
506 fprintf(stderr
, " Parameters:\n");
507 print_value(stderr
, bt_config_component
->params
, 8);
511 void print_bt_config_components(GPtrArray
*array
)
515 for (i
= 0; i
< array
->len
; i
++) {
516 struct bt_config_component
*cfg_component
=
517 bt_config_get_component(array
, i
);
518 print_bt_config_component(cfg_component
);
519 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
524 void print_plugin_paths(const bt_value
*plugin_paths
)
526 fprintf(stderr
, " Plugin paths:\n");
527 print_value(stderr
, plugin_paths
, 4);
531 void print_cfg_run(struct bt_config
*cfg
)
535 print_plugin_paths(cfg
->plugin_paths
);
536 fprintf(stderr
, " Source component instances:\n");
537 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
539 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
540 fprintf(stderr
, " Filter component instances:\n");
541 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
544 fprintf(stderr
, " Sink component instances:\n");
545 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
546 fprintf(stderr
, " Connections:\n");
548 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
549 struct bt_config_connection
*cfg_connection
=
550 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
553 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
554 cfg_connection
->upstream_comp_name
->str
,
555 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
556 cfg_connection
->upstream_port_glob
->str
,
557 cfg_connection
->downstream_comp_name
->str
,
558 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
559 cfg_connection
->downstream_port_glob
->str
);
564 void print_cfg_list_plugins(struct bt_config
*cfg
)
566 print_plugin_paths(cfg
->plugin_paths
);
570 void print_cfg_help(struct bt_config
*cfg
)
572 print_plugin_paths(cfg
->plugin_paths
);
576 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
578 print_plugin_paths(cfg
->plugin_paths
);
579 fprintf(stderr
, " Path: %s\n",
580 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
584 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
586 print_plugin_paths(cfg
->plugin_paths
);
587 fprintf(stderr
, " URL: %s\n",
588 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
592 void print_cfg_query(struct bt_config
*cfg
)
594 print_plugin_paths(cfg
->plugin_paths
);
595 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
596 fprintf(stderr
, " Component class:\n");
597 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
601 void print_cfg(struct bt_config
*cfg
)
603 if (!BT_LOG_ON_INFO
) {
607 BT_LOGI_STR("CLI configuration:");
609 switch (cfg
->command
) {
610 case BT_CONFIG_COMMAND_RUN
:
613 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
614 print_cfg_list_plugins(cfg
);
616 case BT_CONFIG_COMMAND_HELP
:
619 case BT_CONFIG_COMMAND_QUERY
:
620 print_cfg_query(cfg
);
622 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
623 print_cfg_print_ctf_metadata(cfg
);
625 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
626 print_cfg_print_lttng_live_sessions(cfg
);
634 void print_plugin_info(const bt_plugin
*plugin
)
636 unsigned int major
, minor
, patch
;
638 bt_property_availability version_avail
;
639 const char *plugin_name
;
643 const char *plugin_description
;
645 plugin_name
= bt_plugin_get_name(plugin
);
646 path
= bt_plugin_get_path(plugin
);
647 author
= bt_plugin_get_author(plugin
);
648 license
= bt_plugin_get_license(plugin
);
649 plugin_description
= bt_plugin_get_description(plugin
);
650 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
652 printf("%s%s%s%s:\n", bt_common_color_bold(),
653 bt_common_color_fg_blue(), plugin_name
,
654 bt_common_color_reset());
656 printf(" %sPath%s: %s\n", bt_common_color_bold(),
657 bt_common_color_reset(), path
);
662 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
663 printf(" %sVersion%s: %u.%u.%u",
664 bt_common_color_bold(), bt_common_color_reset(),
665 major
, minor
, patch
);
674 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
675 bt_common_color_reset(),
676 plugin_description
? plugin_description
: "(None)");
677 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
678 bt_common_color_reset(), author
? author
: "(Unknown)");
679 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
680 bt_common_color_reset(),
681 license
? license
: "(Unknown)");
685 int cmd_query(struct bt_config
*cfg
)
688 const bt_component_class
*comp_cls
= NULL
;
689 const bt_value
*results
= NULL
;
690 const char *fail_reason
= NULL
;
692 comp_cls
= find_component_class(
693 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
694 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
695 cfg
->cmd_data
.query
.cfg_component
->type
);
697 BT_CLI_LOGE_APPEND_CAUSE(
698 "Cannot find component class: plugin-name=\"%s\", "
699 "comp-cls-name=\"%s\", comp-cls-type=%d",
700 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
701 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
702 cfg
->cmd_data
.query
.cfg_component
->type
);
707 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
708 cfg
->cmd_data
.query
.cfg_component
->params
,
709 &results
, &fail_reason
);
714 print_value(stdout
, results
, 0);
718 BT_CLI_LOGE_APPEND_CAUSE(
719 "Failed to query component class: %s: plugin-name=\"%s\", "
720 "comp-cls-name=\"%s\", comp-cls-type=%d "
721 "object=\"%s\"", fail_reason
,
722 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
723 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
724 cfg
->cmd_data
.query
.cfg_component
->type
,
725 cfg
->cmd_data
.query
.object
->str
);
729 bt_component_class_put_ref(comp_cls
);
730 bt_value_put_ref(results
);
735 void print_component_class_help(const char *plugin_name
,
736 const bt_component_class
*comp_cls
)
738 const char *comp_class_name
=
739 bt_component_class_get_name(comp_cls
);
740 const char *comp_class_description
=
741 bt_component_class_get_description(comp_cls
);
742 const char *comp_class_help
=
743 bt_component_class_get_help(comp_cls
);
744 bt_component_class_type type
=
745 bt_component_class_get_type(comp_cls
);
747 print_plugin_comp_cls_opt(stdout
, plugin_name
, comp_class_name
, type
);
749 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
750 bt_common_color_reset(),
751 comp_class_description
? comp_class_description
: "(None)");
753 if (comp_class_help
) {
754 printf("\n%s\n", comp_class_help
);
759 int cmd_help(struct bt_config
*cfg
)
762 const bt_plugin
*plugin
= NULL
;
763 const bt_component_class
*needed_comp_cls
= NULL
;
765 plugin
= find_loaded_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
767 BT_CLI_LOGE_APPEND_CAUSE(
768 "Cannot find plugin: plugin-name=\"%s\"",
769 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
774 print_plugin_info(plugin
);
775 printf(" %sSource component classes%s: %d\n",
776 bt_common_color_bold(),
777 bt_common_color_reset(),
778 (int) bt_plugin_get_source_component_class_count(plugin
));
779 printf(" %sFilter component classes%s: %d\n",
780 bt_common_color_bold(),
781 bt_common_color_reset(),
782 (int) bt_plugin_get_filter_component_class_count(plugin
));
783 printf(" %sSink component classes%s: %d\n",
784 bt_common_color_bold(),
785 bt_common_color_reset(),
786 (int) bt_plugin_get_sink_component_class_count(plugin
));
788 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
789 /* Plugin help only */
793 needed_comp_cls
= find_component_class(
794 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
795 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
796 cfg
->cmd_data
.help
.cfg_component
->type
);
797 if (!needed_comp_cls
) {
798 BT_CLI_LOGE_APPEND_CAUSE(
799 "Cannot find component class: plugin-name=\"%s\", "
800 "comp-cls-name=\"%s\", comp-cls-type=%d",
801 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
802 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
803 cfg
->cmd_data
.help
.cfg_component
->type
);
809 print_component_class_help(
810 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
814 bt_component_class_put_ref(needed_comp_cls
);
815 bt_plugin_put_ref(plugin
);
819 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
821 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
824 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
825 const char *cc_type_name
, uint64_t count
,
826 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
827 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
832 printf(" %s%s component classes%s: (none)\n",
833 bt_common_color_bold(),
835 bt_common_color_reset());
838 printf(" %s%s component classes%s:\n",
839 bt_common_color_bold(),
841 bt_common_color_reset());
844 for (i
= 0; i
< count
; i
++) {
845 const bt_component_class
*comp_class
=
846 spec_comp_cls_borrow_comp_cls_func(
847 borrow_comp_cls_by_index_func(plugin
, i
));
848 const char *comp_class_name
=
849 bt_component_class_get_name(comp_class
);
850 const char *comp_class_description
=
851 bt_component_class_get_description(comp_class
);
852 bt_component_class_type type
=
853 bt_component_class_get_type(comp_class
);
856 print_plugin_comp_cls_opt(stdout
,
857 bt_plugin_get_name(plugin
), comp_class_name
,
860 if (comp_class_description
) {
861 printf(": %s", comp_class_description
);
872 int cmd_list_plugins(struct bt_config
*cfg
)
875 int plugins_count
, component_classes_count
= 0, i
;
877 printf("From the following plugin paths:\n\n");
878 print_value(stdout
, cfg
->plugin_paths
, 2);
880 plugins_count
= get_loaded_plugins_count();
881 if (plugins_count
== 0) {
882 printf("No plugins found.\n");
886 for (i
= 0; i
< plugins_count
; i
++) {
887 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
889 component_classes_count
+=
890 bt_plugin_get_source_component_class_count(plugin
) +
891 bt_plugin_get_filter_component_class_count(plugin
) +
892 bt_plugin_get_sink_component_class_count(plugin
);
895 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
896 bt_common_color_bold(),
897 component_classes_count
,
898 bt_common_color_reset(),
899 bt_common_color_bold(),
901 bt_common_color_reset());
903 for (i
= 0; i
< plugins_count
; i
++) {
904 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
907 print_plugin_info(plugin
);
908 cmd_list_plugins_print_component_classes(plugin
, "Source",
909 bt_plugin_get_source_component_class_count(plugin
),
910 (plugin_borrow_comp_cls_by_index_func_t
)
911 bt_plugin_borrow_source_component_class_by_index_const
,
912 (spec_comp_cls_borrow_comp_cls_func_t
)
913 bt_component_class_source_as_component_class
);
914 cmd_list_plugins_print_component_classes(plugin
, "Filter",
915 bt_plugin_get_filter_component_class_count(plugin
),
916 (plugin_borrow_comp_cls_by_index_func_t
)
917 bt_plugin_borrow_filter_component_class_by_index_const
,
918 (spec_comp_cls_borrow_comp_cls_func_t
)
919 bt_component_class_filter_as_component_class
);
920 cmd_list_plugins_print_component_classes(plugin
, "Sink",
921 bt_plugin_get_sink_component_class_count(plugin
),
922 (plugin_borrow_comp_cls_by_index_func_t
)
923 bt_plugin_borrow_sink_component_class_by_index_const
,
924 (spec_comp_cls_borrow_comp_cls_func_t
)
925 bt_component_class_sink_as_component_class
);
933 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
936 const bt_component_class
*comp_cls
= NULL
;
937 const bt_value
*results
= NULL
;
938 bt_value
*params
= NULL
;
939 const bt_value
*map
= NULL
;
940 const bt_value
*v
= NULL
;
941 static const char * const plugin_name
= "ctf";
942 static const char * const comp_cls_name
= "lttng-live";
943 static const bt_component_class_type comp_cls_type
=
944 BT_COMPONENT_CLASS_TYPE_SOURCE
;
945 int64_t array_size
, i
;
946 const char *fail_reason
= NULL
;
947 FILE *out_stream
= stdout
;
949 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
950 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
953 BT_CLI_LOGE_APPEND_CAUSE(
954 "Cannot find component class: plugin-name=\"%s\", "
955 "comp-cls-name=\"%s\", comp-cls-type=%d",
956 plugin_name
, comp_cls_name
,
957 BT_COMPONENT_CLASS_TYPE_SOURCE
);
961 params
= bt_value_map_create();
966 ret
= bt_value_map_insert_string_entry(params
, "url",
967 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
972 ret
= query(cfg
, comp_cls
, "sessions", params
,
973 &results
, &fail_reason
);
980 if (!bt_value_is_array(results
)) {
981 BT_CLI_LOGE_APPEND_CAUSE(
982 "Expecting an array for LTTng live `sessions` query.");
986 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
988 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
992 BT_LOGE_ERRNO("Cannot open file for writing",
994 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
995 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
997 "Cannot open file for writing: path=\"%s\"",
998 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1003 array_size
= bt_value_array_get_length(results
);
1004 for (i
= 0; i
< array_size
; i
++) {
1005 const char *url_text
;
1006 int64_t timer_us
, streams
, clients
;
1008 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
1010 BT_CLI_LOGE_APPEND_CAUSE("Unexpected empty array entry.");
1013 if (!bt_value_is_map(map
)) {
1014 BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type.");
1018 v
= bt_value_map_borrow_entry_value_const(map
, "url");
1020 BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry.");
1023 url_text
= bt_value_string_get(v
);
1024 fprintf(out_stream
, "%s", url_text
);
1025 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
1027 BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry.");
1030 timer_us
= bt_value_integer_unsigned_get(v
);
1031 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1032 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1034 BT_CLI_LOGE_APPEND_CAUSE(
1035 "Missing `stream-count` entry.");
1038 streams
= bt_value_integer_unsigned_get(v
);
1039 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1040 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1042 BT_CLI_LOGE_APPEND_CAUSE(
1043 "Missing `client-count` entry.");
1046 clients
= bt_value_integer_unsigned_get(v
);
1047 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1053 BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s",
1060 bt_value_put_ref(results
);
1061 bt_value_put_ref(params
);
1062 bt_component_class_put_ref(comp_cls
);
1064 if (out_stream
&& out_stream
!= stdout
) {
1065 int fclose_ret
= fclose(out_stream
);
1068 BT_LOGE_ERRNO("Cannot close file stream",
1070 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1078 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1081 const bt_component_class
*comp_cls
= NULL
;
1082 const bt_value
*results
= NULL
;
1083 bt_value
*params
= NULL
;
1084 const bt_value
*metadata_text_value
= NULL
;
1085 const char *metadata_text
= NULL
;
1086 static const char * const plugin_name
= "ctf";
1087 static const char * const comp_cls_name
= "fs";
1088 static const bt_component_class_type comp_cls_type
=
1089 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1090 const char *fail_reason
= NULL
;
1091 FILE *out_stream
= stdout
;
1093 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1094 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1097 BT_CLI_LOGE_APPEND_CAUSE(
1098 "Cannot find component class: plugin-name=\"%s\", "
1099 "comp-cls-name=\"%s\", comp-cls-type=%d",
1100 plugin_name
, comp_cls_name
,
1101 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1106 params
= bt_value_map_create();
1112 ret
= bt_value_map_insert_string_entry(params
, "path",
1113 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1119 ret
= query(cfg
, comp_cls
, "metadata-info",
1120 params
, &results
, &fail_reason
);
1125 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1127 if (!metadata_text_value
) {
1128 BT_CLI_LOGE_APPEND_CAUSE(
1129 "Cannot find `text` string value in the resulting metadata info object.");
1134 metadata_text
= bt_value_string_get(metadata_text_value
);
1136 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1138 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1141 BT_LOGE_ERRNO("Cannot open file for writing",
1143 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1144 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1146 "Cannot open file for writing: path=\"%s\"",
1147 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1153 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1155 BT_CLI_LOGE_APPEND_CAUSE(
1156 "Cannot write whole metadata text to output stream: "
1166 BT_CLI_LOGE_APPEND_CAUSE(
1167 "Failed to query `metadata-info` object: %s", fail_reason
);
1170 bt_value_put_ref(results
);
1171 bt_value_put_ref(params
);
1172 bt_component_class_put_ref(comp_cls
);
1174 if (out_stream
&& out_stream
!= stdout
) {
1175 int fclose_ret
= fclose(out_stream
);
1178 BT_LOGE_ERRNO("Cannot close file stream",
1180 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1188 char *instance_name
;
1192 struct trace_range
{
1193 uint64_t intersection_range_begin_ns
;
1194 uint64_t intersection_range_end_ns
;
1198 guint
port_id_hash(gconstpointer v
)
1200 const struct port_id
*id
= v
;
1202 BT_ASSERT(id
->instance_name
);
1203 BT_ASSERT(id
->port_name
);
1205 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1209 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1211 const struct port_id
*id1
= v1
;
1212 const struct port_id
*id2
= v2
;
1214 return strcmp(id1
->instance_name
, id2
->instance_name
) == 0 &&
1215 strcmp(id1
->port_name
, id2
->port_name
) == 0;
1219 void port_id_destroy(gpointer data
)
1221 struct port_id
*id
= data
;
1223 free(id
->instance_name
);
1224 free(id
->port_name
);
1229 void trace_range_destroy(gpointer data
)
1234 struct cmd_run_ctx
{
1236 GHashTable
*src_components
;
1239 GHashTable
*flt_components
;
1242 GHashTable
*sink_components
;
1248 struct bt_config
*cfg
;
1252 bool stream_intersection_mode
;
1255 * Association of struct port_id -> struct trace_range.
1257 GHashTable
*intersections
;
1260 /* Returns a timestamp of the form "(-)s.ns" */
1262 char *s_from_ns(int64_t ns
)
1267 int64_t ts_sec_abs
, ts_nsec_abs
;
1268 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1269 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1271 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1272 is_negative
= false;
1273 ts_sec_abs
= ts_sec
;
1274 ts_nsec_abs
= ts_nsec
;
1275 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1276 is_negative
= false;
1277 ts_sec_abs
= ts_sec
- 1;
1278 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1279 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1281 ts_sec_abs
= ts_sec
;
1282 ts_nsec_abs
= -ts_nsec
;
1283 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1285 ts_sec_abs
= -(ts_sec
+ 1);
1286 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1287 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1289 ts_sec_abs
= -ts_sec
;
1290 ts_nsec_abs
= ts_nsec
;
1291 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1293 ts_sec_abs
= -ts_sec
;
1294 ts_nsec_abs
= -ts_nsec
;
1297 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1298 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1306 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1307 struct cmd_run_ctx
*ctx
,
1308 const bt_component
*upstream_comp
,
1309 const bt_port_output
*out_upstream_port
,
1310 struct bt_config_connection
*cfg_conn
)
1312 typedef uint64_t (*input_port_count_func_t
)(void *);
1313 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1314 const void *, uint64_t);
1315 const bt_port
*upstream_port
=
1316 bt_port_output_as_port_const(out_upstream_port
);
1319 GQuark downstreamp_comp_name_quark
;
1320 void *downstream_comp
;
1321 uint64_t downstream_port_count
;
1323 input_port_count_func_t port_count_fn
;
1324 borrow_input_port_by_index_func_t port_by_index_fn
;
1325 bt_graph_connect_ports_status connect_ports_status
=
1326 BT_GRAPH_CONNECT_PORTS_STATUS_OK
;
1327 bool insert_trimmer
= false;
1328 bt_value
*trimmer_params
= NULL
;
1329 char *intersection_begin
= NULL
;
1330 char *intersection_end
= NULL
;
1331 const bt_component_filter
*trimmer
= NULL
;
1332 const bt_component_class_filter
*trimmer_class
= NULL
;
1333 const bt_port_input
*trimmer_input
= NULL
;
1334 const bt_port_output
*trimmer_output
= NULL
;
1336 if (ctx
->intersections
&&
1337 bt_component_get_class_type(upstream_comp
) ==
1338 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1339 struct trace_range
*range
;
1340 struct port_id port_id
= {
1341 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1342 .port_name
= (char *) bt_port_get_name(upstream_port
)
1345 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1349 range
= (struct trace_range
*) g_hash_table_lookup(
1350 ctx
->intersections
, &port_id
);
1352 bt_value_map_insert_entry_status insert_status
;
1354 intersection_begin
= s_from_ns(
1355 range
->intersection_range_begin_ns
);
1356 intersection_end
= s_from_ns(
1357 range
->intersection_range_end_ns
);
1358 if (!intersection_begin
|| !intersection_end
) {
1359 BT_CLI_LOGE_APPEND_CAUSE(
1360 "Cannot create trimmer argument timestamp string.");
1364 insert_trimmer
= true;
1365 trimmer_params
= bt_value_map_create();
1366 if (!trimmer_params
) {
1370 insert_status
= bt_value_map_insert_string_entry(
1371 trimmer_params
, "begin", intersection_begin
);
1372 if (insert_status
< 0) {
1375 insert_status
= bt_value_map_insert_string_entry(
1377 "end", intersection_end
);
1378 if (insert_status
< 0) {
1383 trimmer_class
= find_filter_component_class("utils", "trimmer");
1384 if (!trimmer_class
) {
1389 BT_LOGI("Connecting upstream port to the next available downstream port: "
1390 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1391 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1392 upstream_port
, bt_port_get_name(upstream_port
),
1393 cfg_conn
->downstream_comp_name
->str
,
1394 cfg_conn
->arg
->str
);
1395 downstreamp_comp_name_quark
= g_quark_from_string(
1396 cfg_conn
->downstream_comp_name
->str
);
1397 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1398 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1399 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1400 port_count_fn
= (input_port_count_func_t
)
1401 bt_component_filter_get_input_port_count
;
1402 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1403 bt_component_filter_borrow_input_port_by_index_const
;
1405 if (!downstream_comp
) {
1406 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1407 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1408 port_count_fn
= (input_port_count_func_t
)
1409 bt_component_sink_get_input_port_count
;
1410 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1411 bt_component_sink_borrow_input_port_by_index_const
;
1414 if (!downstream_comp
) {
1415 BT_CLI_LOGE_APPEND_CAUSE("Cannot find downstream component: "
1416 "comp-name=\"%s\", conn-arg=\"%s\"",
1417 cfg_conn
->downstream_comp_name
->str
,
1418 cfg_conn
->arg
->str
);
1422 downstream_port_count
= port_count_fn(downstream_comp
);
1424 for (i
= 0; i
< downstream_port_count
; i
++) {
1425 const bt_port_input
*in_downstream_port
=
1426 port_by_index_fn(downstream_comp
, i
);
1427 const bt_port
*downstream_port
=
1428 bt_port_input_as_port_const(in_downstream_port
);
1429 const char *upstream_port_name
;
1430 const char *downstream_port_name
;
1432 BT_ASSERT(downstream_port
);
1434 /* Skip port if it's already connected. */
1435 if (bt_port_is_connected(downstream_port
)) {
1436 BT_LOGI("Skipping downstream port: already connected: "
1437 "port-addr=%p, port-name=\"%s\"",
1439 bt_port_get_name(downstream_port
));
1443 downstream_port_name
= bt_port_get_name(downstream_port
);
1444 BT_ASSERT(downstream_port_name
);
1445 upstream_port_name
= bt_port_get_name(upstream_port
);
1446 BT_ASSERT(upstream_port_name
);
1448 if (!bt_common_star_glob_match(
1449 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1450 downstream_port_name
, SIZE_MAX
)) {
1454 if (insert_trimmer
) {
1456 * In order to insert the trimmer between the
1457 * two components that were being connected, we
1458 * create a connection configuration entry which
1459 * describes a connection from the trimmer's
1460 * output to the original input that was being
1463 * Hence, the creation of the trimmer will cause
1464 * the graph "new port" listener to establish
1465 * all downstream connections as its output port
1466 * is connected. We will then establish the
1467 * connection between the original upstream
1468 * source and the trimmer.
1470 char *trimmer_name
= NULL
;
1471 bt_graph_add_component_status add_comp_status
;
1473 ret
= asprintf(&trimmer_name
,
1474 "stream-intersection-trimmer-%s",
1475 upstream_port_name
);
1481 ctx
->connect_ports
= false;
1482 add_comp_status
= bt_graph_add_filter_component(
1483 ctx
->graph
, trimmer_class
, trimmer_name
,
1484 trimmer_params
, ctx
->cfg
->log_level
,
1487 if (add_comp_status
!=
1488 BT_GRAPH_ADD_COMPONENT_STATUS_OK
) {
1494 bt_component_filter_borrow_input_port_by_index_const(
1496 if (!trimmer_input
) {
1500 bt_component_filter_borrow_output_port_by_index_const(
1502 if (!trimmer_output
) {
1507 * Replace the current downstream port by the trimmer's
1510 in_downstream_port
= trimmer_input
;
1512 bt_port_input_as_port_const(in_downstream_port
);
1513 downstream_port_name
= bt_port_get_name(
1515 BT_ASSERT(downstream_port_name
);
1518 /* We have a winner! */
1519 connect_ports_status
= bt_graph_connect_ports(ctx
->graph
,
1520 out_upstream_port
, in_downstream_port
, NULL
);
1521 downstream_port
= NULL
;
1522 switch (connect_ports_status
) {
1523 case BT_GRAPH_CONNECT_PORTS_STATUS_OK
:
1526 BT_CLI_LOGE_APPEND_CAUSE(
1527 "Cannot create connection: graph refuses to connect ports: "
1528 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1529 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1530 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1531 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1533 upstream_comp
, bt_component_get_name(upstream_comp
),
1534 upstream_port
, bt_port_get_name(upstream_port
),
1535 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1536 downstream_port
, downstream_port_name
,
1537 cfg_conn
->arg
->str
);
1541 BT_LOGI("Connected component ports: "
1542 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1543 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1544 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1545 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1547 upstream_comp
, bt_component_get_name(upstream_comp
),
1548 upstream_port
, bt_port_get_name(upstream_port
),
1549 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1550 downstream_port
, downstream_port_name
,
1551 cfg_conn
->arg
->str
);
1553 if (insert_trimmer
) {
1555 * The first connection, from the source to the trimmer,
1556 * has been done. We now connect the trimmer to the
1557 * original downstream port.
1559 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1561 bt_component_filter_as_component_const(trimmer
),
1562 trimmer_output
, cfg_conn
);
1566 ctx
->connect_ports
= true;
1570 * We found a matching downstream port: the search is
1576 /* No downstream port found */
1577 BT_CLI_LOGE_APPEND_CAUSE(
1578 "Cannot create connection: cannot find a matching downstream port for upstream port: "
1579 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1580 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1581 upstream_port
, bt_port_get_name(upstream_port
),
1582 cfg_conn
->downstream_comp_name
->str
,
1583 cfg_conn
->arg
->str
);
1589 free(intersection_begin
);
1590 free(intersection_end
);
1591 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1592 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1593 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1598 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1599 const bt_port_output
*upstream_port
)
1602 const char *upstream_port_name
;
1603 const char *upstream_comp_name
;
1604 const bt_component
*upstream_comp
= NULL
;
1608 BT_ASSERT(upstream_port
);
1609 upstream_port_name
= bt_port_get_name(
1610 bt_port_output_as_port_const(upstream_port
));
1611 BT_ASSERT(upstream_port_name
);
1612 upstream_comp
= bt_port_borrow_component_const(
1613 bt_port_output_as_port_const(upstream_port
));
1614 BT_ASSERT(upstream_comp
);
1615 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1616 BT_ASSERT(upstream_comp_name
);
1617 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1618 "port-addr=%p, port-name=\"%s\"",
1619 upstream_comp
, upstream_comp_name
,
1620 upstream_port
, upstream_port_name
);
1622 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1623 struct bt_config_connection
*cfg_conn
=
1625 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1627 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1628 upstream_comp_name
)) {
1632 if (!bt_common_star_glob_match(
1633 cfg_conn
->upstream_port_glob
->str
,
1634 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1638 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1639 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1641 BT_CLI_LOGE_APPEND_CAUSE(
1642 "Cannot connect upstream port: "
1643 "port-addr=%p, port-name=\"%s\"",
1645 upstream_port_name
);
1651 BT_CLI_LOGE_APPEND_CAUSE(
1652 "Cannot connect upstream port: port does not match any connection argument: "
1653 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1654 upstream_port_name
);
1664 bt_graph_listener_func_status
1665 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1666 const bt_port_output
*out_port
)
1668 const bt_component
*comp
;
1669 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1670 bt_graph_listener_func_status ret
=
1671 BT_GRAPH_LISTENER_FUNC_STATUS_OK
;
1673 comp
= bt_port_borrow_component_const(port
);
1674 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1675 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1676 comp
, comp
? bt_component_get_name(comp
) : "",
1677 port
, bt_port_get_name(port
));
1679 if (!ctx
->connect_ports
) {
1685 if (bt_port_is_connected(port
)) {
1686 BT_LOGW_STR("Port is already connected.");
1690 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
1691 BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port.");
1692 ret
= BT_GRAPH_LISTENER_FUNC_STATUS_ERROR
;
1701 bt_graph_listener_func_status
graph_source_output_port_added_listener(
1702 const bt_component_source
*component
,
1703 const bt_port_output
*port
, void *data
)
1705 return graph_output_port_added_listener(data
, port
);
1709 bt_graph_listener_func_status
graph_filter_output_port_added_listener(
1710 const bt_component_filter
*component
,
1711 const bt_port_output
*port
, void *data
)
1713 return graph_output_port_added_listener(data
, port
);
1717 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1723 if (ctx
->src_components
) {
1724 g_hash_table_destroy(ctx
->src_components
);
1725 ctx
->src_components
= NULL
;
1728 if (ctx
->flt_components
) {
1729 g_hash_table_destroy(ctx
->flt_components
);
1730 ctx
->flt_components
= NULL
;
1733 if (ctx
->sink_components
) {
1734 g_hash_table_destroy(ctx
->sink_components
);
1735 ctx
->sink_components
= NULL
;
1738 if (ctx
->intersections
) {
1739 g_hash_table_destroy(ctx
->intersections
);
1740 ctx
->intersections
= NULL
;
1743 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
1748 int add_descriptor_to_component_descriptor_set(
1749 bt_component_descriptor_set
*comp_descr_set
,
1750 const char *plugin_name
, const char *comp_cls_name
,
1751 bt_component_class_type comp_cls_type
,
1752 const bt_value
*params
)
1754 const bt_component_class
*comp_cls
;
1757 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1760 BT_CLI_LOGE_APPEND_CAUSE(
1761 "Cannot find component class: plugin-name=\"%s\", "
1762 "comp-cls-name=\"%s\", comp-cls-type=%d",
1763 plugin_name
, comp_cls_name
, comp_cls_type
);
1768 status
= bt_component_descriptor_set_add_descriptor(
1769 comp_descr_set
, comp_cls
, params
);
1770 if (status
!= BT_COMPONENT_DESCRIPTOR_SET_ADD_DESCRIPTOR_STATUS_OK
) {
1771 BT_CLI_LOGE_APPEND_CAUSE(
1772 "Cannot append descriptor to component descriptor set: "
1773 "status=%s", bt_common_func_status_string(status
));
1778 bt_component_class_put_ref(comp_cls
);
1783 int append_descriptors_from_bt_config_component_array(
1784 bt_component_descriptor_set
*comp_descr_set
,
1785 GPtrArray
*component_configs
)
1790 for (i
= 0; i
< component_configs
->len
; i
++) {
1791 struct bt_config_component
*cfg_comp
=
1792 component_configs
->pdata
[i
];
1794 ret
= add_descriptor_to_component_descriptor_set(
1796 cfg_comp
->plugin_name
->str
,
1797 cfg_comp
->comp_cls_name
->str
,
1798 cfg_comp
->type
, cfg_comp
->params
);
1809 bt_get_greatest_operative_mip_version_status
get_greatest_operative_mip_version(
1810 struct bt_config
*cfg
, uint64_t *mip_version
)
1812 bt_get_greatest_operative_mip_version_status status
=
1813 BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK
;
1814 bt_component_descriptor_set
*comp_descr_set
= NULL
;
1818 BT_ASSERT(mip_version
);
1819 comp_descr_set
= bt_component_descriptor_set_create();
1820 if (!comp_descr_set
) {
1821 BT_CLI_LOGE_APPEND_CAUSE(
1822 "Failed to create a component descriptor set object.");
1823 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_MEMORY_ERROR
;
1827 ret
= append_descriptors_from_bt_config_component_array(
1828 comp_descr_set
, cfg
->cmd_data
.run
.sources
);
1830 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1834 ret
= append_descriptors_from_bt_config_component_array(
1835 comp_descr_set
, cfg
->cmd_data
.run
.filters
);
1837 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1841 ret
= append_descriptors_from_bt_config_component_array(
1842 comp_descr_set
, cfg
->cmd_data
.run
.sinks
);
1844 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1848 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1850 * Stream intersection mode adds `flt.utils.trimmer`
1851 * components; we need to include this type of component
1852 * in the component descriptor set to get the real
1853 * greatest operative MIP version.
1855 ret
= add_descriptor_to_component_descriptor_set(
1856 comp_descr_set
, "utils", "trimmer",
1857 BT_COMPONENT_CLASS_TYPE_FILTER
, NULL
);
1859 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1864 status
= bt_get_greatest_operative_mip_version(comp_descr_set
,
1865 bt_cli_log_level
, mip_version
);
1868 bt_component_descriptor_set_put_ref(comp_descr_set
);
1873 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1876 bt_graph_add_listener_status add_listener_status
;
1877 bt_get_greatest_operative_mip_version_status mip_version_status
;
1878 uint64_t mip_version
= UINT64_C(-1);
1881 ctx
->connect_ports
= false;
1882 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
1883 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1884 if (!ctx
->src_components
) {
1888 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
1889 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1890 if (!ctx
->flt_components
) {
1894 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
1895 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1896 if (!ctx
->sink_components
) {
1900 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1901 ctx
->stream_intersection_mode
= true;
1902 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1903 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1904 if (!ctx
->intersections
) {
1910 * Get the greatest operative MIP version to use to configure
1911 * the graph to create.
1913 mip_version_status
= get_greatest_operative_mip_version(
1915 if (mip_version_status
== BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_NO_MATCH
) {
1916 BT_CLI_LOGE_APPEND_CAUSE(
1917 "Failed to find an operative message interchange "
1918 "protocol version to use to create the `run` command's "
1919 "graph (components are not interoperable).");
1921 } else if (mip_version_status
< 0) {
1922 BT_CLI_LOGE_APPEND_CAUSE(
1923 "Cannot find an operative message interchange "
1924 "protocol version to use to create the `run` command's "
1926 bt_common_func_status_string(mip_version_status
));
1930 BT_ASSERT(mip_version_status
== BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK
);
1931 BT_LOGI("Found operative message interchange protocol version to "
1932 "configure the `run` command's graph: mip-version=%" PRIu64
,
1934 ctx
->graph
= bt_graph_create(mip_version
);
1939 bt_graph_add_interrupter(ctx
->graph
, the_interrupter
);
1940 add_listener_status
= bt_graph_add_source_component_output_port_added_listener(
1941 ctx
->graph
, graph_source_output_port_added_listener
, NULL
, ctx
,
1943 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1944 BT_CLI_LOGE_APPEND_CAUSE(
1945 "Cannot add \"port added\" listener to graph.");
1949 add_listener_status
= bt_graph_add_filter_component_output_port_added_listener(
1950 ctx
->graph
, graph_filter_output_port_added_listener
, NULL
, ctx
,
1952 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1953 BT_CLI_LOGE_APPEND_CAUSE(
1954 "Cannot add \"port added\" listener to graph.");
1961 cmd_run_ctx_destroy(ctx
);
1969 * Compute the intersection of all streams in the array `streams`, write it
1974 int compute_stream_intersection(const bt_value
*streams
,
1975 struct trace_range
*range
)
1978 unsigned int stream_count
;
1981 BT_ASSERT(bt_value_get_type(streams
) == BT_VALUE_TYPE_ARRAY
);
1983 stream_count
= bt_value_array_get_length(streams
);
1985 BT_ASSERT(stream_count
> 0);
1987 range
->intersection_range_begin_ns
= 0;
1988 range
->intersection_range_end_ns
= UINT64_MAX
;
1990 for (i
= 0; i
< stream_count
; i
++) {
1991 int64_t begin_ns
, end_ns
;
1992 uint64_t begin_ns_u
, end_ns_u
;
1993 const bt_value
*stream_value
;
1994 const bt_value
*range_ns_value
;
1995 const bt_value
*begin_value
;
1996 const bt_value
*end_value
;
1998 stream_value
= bt_value_array_borrow_element_by_index_const(streams
, i
);
1999 if (bt_value_get_type(stream_value
) != BT_VALUE_TYPE_MAP
) {
2000 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
2001 "expected streams array element to be a map, got %s.",
2002 bt_common_value_type_string(bt_value_get_type(stream_value
)));
2006 range_ns_value
= bt_value_map_borrow_entry_value_const(
2007 stream_value
, "range-ns");
2008 if (!range_ns_value
) {
2009 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
2010 "missing expected `range-ns` key in stream map.");
2014 if (bt_value_get_type(range_ns_value
) != BT_VALUE_TYPE_MAP
) {
2015 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
2016 "expected `range-ns` entry value of stream map to be a map, got %s.",
2017 bt_common_value_type_string(bt_value_get_type(range_ns_value
)));
2021 begin_value
= bt_value_map_borrow_entry_value_const(range_ns_value
, "begin");
2023 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
2024 "missing expected `begin` key in range-ns map.");
2028 if (bt_value_get_type(begin_value
) != BT_VALUE_TYPE_SIGNED_INTEGER
) {
2029 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
2030 "expected `begin` entry value of range-ns map to be a signed integer, got %s.",
2031 bt_common_value_type_string(bt_value_get_type(range_ns_value
)));
2035 end_value
= bt_value_map_borrow_entry_value_const(range_ns_value
, "end");
2037 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
2038 "missing expected `end` key in range-ns map.");
2042 if (bt_value_get_type(end_value
) != BT_VALUE_TYPE_SIGNED_INTEGER
) {
2043 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
2044 "expected `end` entry value of range-ns map to be a signed integer, got %s.",
2045 bt_common_value_type_string(bt_value_get_type(range_ns_value
)));
2049 begin_ns
= bt_value_integer_signed_get(begin_value
);
2050 end_ns
= bt_value_integer_signed_get(end_value
);
2052 if (begin_ns
< 0 || end_ns
< 0 || end_ns
< begin_ns
) {
2053 BT_CLI_LOGE_APPEND_CAUSE(
2054 "Invalid stream range values: "
2055 "range-ns:begin=%" PRId64
", "
2056 "range-ns:end=%" PRId64
,
2062 begin_ns_u
= begin_ns
;
2065 range
->intersection_range_begin_ns
=
2066 MAX(range
->intersection_range_begin_ns
, begin_ns_u
);
2067 range
->intersection_range_end_ns
=
2068 MIN(range
->intersection_range_end_ns
, end_ns_u
);
2081 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2082 struct bt_config_component
*cfg_comp
,
2083 const bt_component_class_source
*src_comp_cls
)
2087 int64_t trace_count
;
2088 const bt_value
*query_result
= NULL
;
2089 const bt_value
*trace_info
= NULL
;
2090 const bt_value
*stream_infos
= NULL
;
2091 const bt_value
*stream_info
= NULL
;
2092 struct port_id
*port_id
= NULL
;
2093 struct trace_range
*stream_intersection
= NULL
;
2094 const char *fail_reason
= NULL
;
2095 const bt_component_class
*comp_cls
=
2096 bt_component_class_source_as_component_class_const(src_comp_cls
);
2098 ret
= query(ctx
->cfg
, comp_cls
, "babeltrace.trace-infos",
2099 cfg_comp
->params
, &query_result
,
2102 BT_CLI_LOGE_APPEND_CAUSE("Failed to execute `babeltrace.trace-infos` query: %s: "
2103 "comp-class-name=\"%s\"", fail_reason
,
2104 bt_component_class_get_name(comp_cls
));
2109 BT_ASSERT(query_result
);
2111 if (!bt_value_is_array(query_result
)) {
2112 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting result to be an array: "
2113 "component-class-name=%s, actual-type=%s",
2114 bt_component_class_get_name(comp_cls
),
2115 bt_common_value_type_string(bt_value_get_type(query_result
)));
2120 trace_count
= bt_value_array_get_length(query_result
);
2121 if (trace_count
< 0) {
2122 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: result is empty: "
2123 "component-class-name=%s", bt_component_class_get_name(comp_cls
));
2128 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2129 uint64_t stream_idx
;
2130 int64_t stream_count
;
2131 struct trace_range trace_intersection
;
2133 trace_info
= bt_value_array_borrow_element_by_index_const(
2134 query_result
, trace_idx
);
2135 BT_ASSERT(trace_info
);
2136 if (!bt_value_is_map(trace_info
)) {
2138 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting element to be a map: "
2139 "component-class-name=%s, actual-type=%s",
2140 bt_component_class_get_name(comp_cls
),
2141 bt_common_value_type_string(bt_value_get_type(trace_info
)));
2145 stream_infos
= bt_value_map_borrow_entry_value_const(
2146 trace_info
, "stream-infos");
2147 if (!stream_infos
) {
2149 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: missing `streams` key in trace info map: "
2150 "component-class-name=%s",
2151 bt_component_class_get_name(comp_cls
));
2155 if (!bt_value_is_array(stream_infos
)) {
2157 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting `streams` entry of trace info map to be an array: "
2158 "component-class-name=%s, actual-type=%s",
2159 bt_component_class_get_name(comp_cls
),
2160 bt_common_value_type_string(bt_value_get_type(stream_infos
)));
2164 stream_count
= bt_value_array_get_length(stream_infos
);
2165 if (stream_count
< 0) {
2167 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: list of streams is empty: "
2168 "component-class-name=%s",
2169 bt_component_class_get_name(comp_cls
));
2173 ret
= compute_stream_intersection(stream_infos
, &trace_intersection
);
2175 BT_CLI_LOGE_APPEND_CAUSE("Failed to compute trace streams intersection.");
2179 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2180 const bt_value
*port_name
;
2182 port_id
= g_new0(struct port_id
, 1);
2185 BT_CLI_LOGE_APPEND_CAUSE(
2186 "Cannot allocate memory for port_id structure.");
2189 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2190 if (!port_id
->instance_name
) {
2192 BT_CLI_LOGE_APPEND_CAUSE(
2193 "Cannot allocate memory for port_id component instance name.");
2197 stream_intersection
= g_new0(struct trace_range
, 1);
2198 if (!stream_intersection
) {
2200 BT_CLI_LOGE_APPEND_CAUSE(
2201 "Cannot allocate memory for trace_range structure.");
2205 *stream_intersection
= trace_intersection
;
2207 stream_info
= bt_value_array_borrow_element_by_index_const(
2208 stream_infos
, stream_idx
);
2209 BT_ASSERT(stream_info
);
2210 if (!bt_value_is_map(stream_info
)) {
2212 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
2213 "expecting element of stream list to be a map: "
2214 "component-class-name=%s, actual-type=%s",
2215 bt_component_class_get_name(comp_cls
),
2216 bt_common_value_type_string(bt_value_get_type(stream_info
)));
2220 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2223 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
2224 "missing `port-name` key in stream info map: "
2225 "component-class-name=%s",
2226 bt_component_class_get_name(comp_cls
));
2230 if (!bt_value_is_string(port_name
)) {
2232 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
2233 "expecting `port-name` entry of stream info map to be a string: "
2234 "component-class-name=%s, actual-type=%s",
2235 bt_component_class_get_name(comp_cls
),
2236 bt_common_value_type_string(bt_value_get_type(port_name
)));
2240 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2241 if (!port_id
->port_name
) {
2243 BT_CLI_LOGE_APPEND_CAUSE(
2244 "Cannot allocate memory for port_id port_name.");
2248 BT_LOGD("Inserting stream intersection ");
2250 g_hash_table_insert(ctx
->intersections
, port_id
, stream_intersection
);
2253 stream_intersection
= NULL
;
2260 bt_value_put_ref(query_result
);
2262 g_free(stream_intersection
);
2267 int cmd_run_ctx_create_components_from_config_components(
2268 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2271 const void *comp_cls
= NULL
;
2272 const void *comp
= NULL
;
2275 for (i
= 0; i
< cfg_components
->len
; i
++) {
2276 struct bt_config_component
*cfg_comp
=
2277 g_ptr_array_index(cfg_components
, i
);
2280 switch (cfg_comp
->type
) {
2281 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2282 comp_cls
= find_source_component_class(
2283 cfg_comp
->plugin_name
->str
,
2284 cfg_comp
->comp_cls_name
->str
);
2286 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2287 comp_cls
= find_filter_component_class(
2288 cfg_comp
->plugin_name
->str
,
2289 cfg_comp
->comp_cls_name
->str
);
2291 case BT_COMPONENT_CLASS_TYPE_SINK
:
2292 comp_cls
= find_sink_component_class(
2293 cfg_comp
->plugin_name
->str
,
2294 cfg_comp
->comp_cls_name
->str
);
2301 BT_CLI_LOGE_APPEND_CAUSE(
2302 "Cannot find component class: plugin-name=\"%s\", "
2303 "comp-cls-name=\"%s\", comp-cls-type=%d",
2304 cfg_comp
->plugin_name
->str
,
2305 cfg_comp
->comp_cls_name
->str
,
2310 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_TRACE
);
2312 switch (cfg_comp
->type
) {
2313 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2314 ret
= bt_graph_add_source_component(ctx
->graph
,
2315 comp_cls
, cfg_comp
->instance_name
->str
,
2316 cfg_comp
->params
, cfg_comp
->log_level
,
2319 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2320 ret
= bt_graph_add_filter_component(ctx
->graph
,
2321 comp_cls
, cfg_comp
->instance_name
->str
,
2322 cfg_comp
->params
, cfg_comp
->log_level
,
2325 case BT_COMPONENT_CLASS_TYPE_SINK
:
2326 ret
= bt_graph_add_sink_component(ctx
->graph
,
2327 comp_cls
, cfg_comp
->instance_name
->str
,
2328 cfg_comp
->params
, cfg_comp
->log_level
,
2336 BT_CLI_LOGE_APPEND_CAUSE(
2337 "Cannot create component: plugin-name=\"%s\", "
2338 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2340 cfg_comp
->plugin_name
->str
,
2341 cfg_comp
->comp_cls_name
->str
,
2342 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2346 if (ctx
->stream_intersection_mode
&&
2347 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2348 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2350 BT_CLI_LOGE_APPEND_CAUSE(
2351 "Cannot determine stream intersection of trace.");
2356 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2357 comp
, cfg_comp
->instance_name
->str
);
2358 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2359 BT_ASSERT(quark
> 0);
2361 switch (cfg_comp
->type
) {
2362 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2363 g_hash_table_insert(ctx
->src_components
,
2364 GUINT_TO_POINTER(quark
), (void *) comp
);
2366 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2367 g_hash_table_insert(ctx
->flt_components
,
2368 GUINT_TO_POINTER(quark
), (void *) comp
);
2370 case BT_COMPONENT_CLASS_TYPE_SINK
:
2371 g_hash_table_insert(ctx
->sink_components
,
2372 GUINT_TO_POINTER(quark
), (void *) comp
);
2379 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2388 bt_object_put_ref(comp
);
2389 bt_object_put_ref(comp_cls
);
2394 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2399 * Make sure that, during this phase, our graph's "port added"
2400 * listener does not connect ports while we are creating the
2401 * components because we have a special, initial phase for
2404 ctx
->connect_ports
= false;
2406 ret
= cmd_run_ctx_create_components_from_config_components(
2407 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2413 ret
= cmd_run_ctx_create_components_from_config_components(
2414 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2420 ret
= cmd_run_ctx_create_components_from_config_components(
2421 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2431 typedef uint64_t (*output_port_count_func_t
)(const void *);
2432 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2433 const void *, uint64_t);
2436 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2437 void *comp
, output_port_count_func_t port_count_fn
,
2438 borrow_output_port_by_index_func_t port_by_index_fn
)
2444 count
= port_count_fn(comp
);
2446 for (i
= 0; i
< count
; i
++) {
2447 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2449 BT_ASSERT(upstream_port
);
2450 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2461 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2464 GHashTableIter iter
;
2465 gpointer g_name_quark
, g_comp
;
2467 ctx
->connect_ports
= true;
2468 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2470 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2471 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2472 (output_port_count_func_t
)
2473 bt_component_source_get_output_port_count
,
2474 (borrow_output_port_by_index_func_t
)
2475 bt_component_source_borrow_output_port_by_index_const
);
2481 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2483 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2484 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2485 (output_port_count_func_t
)
2486 bt_component_filter_get_output_port_count
,
2487 (borrow_output_port_by_index_func_t
)
2488 bt_component_filter_borrow_output_port_by_index_const
);
2499 int cmd_run(struct bt_config
*cfg
)
2502 struct cmd_run_ctx ctx
= { 0 };
2504 /* Initialize the command's context and the graph object */
2505 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2506 BT_CLI_LOGE_APPEND_CAUSE(
2507 "Cannot initialize the command's context.");
2511 if (bt_interrupter_is_set(the_interrupter
)) {
2512 BT_CLI_LOGW_APPEND_CAUSE(
2513 "Interrupted by user before creating components.");
2517 BT_LOGI_STR("Creating components.");
2519 /* Create the requested component instances */
2520 if (cmd_run_ctx_create_components(&ctx
)) {
2521 BT_CLI_LOGE_APPEND_CAUSE("Cannot create components.");
2525 if (bt_interrupter_is_set(the_interrupter
)) {
2526 BT_CLI_LOGW_APPEND_CAUSE(
2527 "Interrupted by user before connecting components.");
2531 BT_LOGI_STR("Connecting components.");
2533 /* Connect the initially visible component ports */
2534 if (cmd_run_ctx_connect_ports(&ctx
)) {
2535 BT_CLI_LOGE_APPEND_CAUSE(
2536 "Cannot connect initial component ports.");
2540 BT_LOGI_STR("Running the graph.");
2544 bt_graph_run_status run_status
= bt_graph_run(ctx
.graph
);
2547 * Reset console in case something messed with console
2548 * codes during the graph's execution.
2550 printf("%s", bt_common_color_reset());
2552 fprintf(stderr
, "%s", bt_common_color_reset());
2553 BT_LOGT("bt_graph_run() returned: status=%s",
2554 bt_common_func_status_string(run_status
));
2556 switch (run_status
) {
2557 case BT_GRAPH_RUN_STATUS_OK
:
2559 case BT_GRAPH_RUN_STATUS_AGAIN
:
2560 if (bt_interrupter_is_set(the_interrupter
)) {
2561 BT_CLI_LOGW_APPEND_CAUSE(
2562 "Graph was interrupted by user.");
2566 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2567 BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: "
2569 cfg
->cmd_data
.run
.retry_duration_us
);
2571 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2572 if (bt_interrupter_is_set(the_interrupter
)) {
2573 BT_CLI_LOGW_APPEND_CAUSE(
2574 "Graph was interrupted by user.");
2580 case BT_GRAPH_RUN_STATUS_END
:
2583 if (bt_interrupter_is_set(the_interrupter
)) {
2584 BT_CLI_LOGW_APPEND_CAUSE(
2585 "Graph was interrupted by user and failed: "
2587 bt_common_func_status_string(run_status
));
2591 BT_CLI_LOGE_APPEND_CAUSE(
2592 "Graph failed to complete successfully");
2605 cmd_run_ctx_destroy(&ctx
);
2610 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2612 const char *env_clash
;
2614 if (!cfg
->command_name
) {
2618 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2619 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2623 if (g_file_test(cfg
->command_name
,
2624 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2625 _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION
, __FILE__
, __LINE__
,
2626 BT_LOG_WARNING
, BT_LOG_TAG
,
2627 "The `%s` command was executed. "
2628 "If you meant to convert a trace located in "
2629 "the local `%s` directory, please use:\n\n"
2630 " babeltrace2 convert %s [OPTIONS]",
2631 cfg
->command_name
, cfg
->command_name
,
2637 void init_log_level(void)
2639 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2643 void print_error_causes(void)
2645 const bt_error
*error
= bt_current_thread_take_error();
2647 GString
*folded
= NULL
;
2648 unsigned int columns
;
2650 if (!error
|| bt_error_get_cause_count(error
) == 0) {
2651 fprintf(stderr
, "%s%sUnknown command-line error.%s\n",
2652 bt_common_color_bold(), bt_common_color_fg_red(),
2653 bt_common_color_reset());
2657 /* Try to get terminal width to fold the error cause messages */
2658 if (bt_common_get_term_size(&columns
, NULL
) < 0) {
2659 /* Width not found: default to 80 */
2664 * This helps visually separate the error causes from the last
2665 * logging statement.
2667 fprintf(stderr
, "\n");
2669 /* Reverse order: deepest (root) cause printed at the end */
2670 for (i
= bt_error_get_cause_count(error
) - 1; i
>= 0; i
--) {
2671 const bt_error_cause
*cause
=
2672 bt_error_borrow_cause_by_index(error
, (uint64_t) i
);
2673 const char *prefix_fmt
=
2674 i
== bt_error_get_cause_count(error
) - 1 ?
2675 "%s%sERROR%s: " : "%s%sCAUSED BY%s ";
2678 fprintf(stderr
, prefix_fmt
,
2679 bt_common_color_bold(), bt_common_color_fg_red(),
2680 bt_common_color_reset());
2682 /* Print actor name */
2683 fprintf(stderr
, "[");
2684 switch (bt_error_cause_get_actor_type(cause
)) {
2685 case BT_ERROR_CAUSE_ACTOR_TYPE_UNKNOWN
:
2686 fprintf(stderr
, "%s%s%s",
2687 bt_common_color_bold(),
2688 bt_error_cause_get_module_name(cause
),
2689 bt_common_color_reset());
2691 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT
:
2692 fprintf(stderr
, "%s%s%s: ",
2693 bt_common_color_bold(),
2694 bt_error_cause_component_actor_get_component_name(cause
),
2695 bt_common_color_reset());
2696 print_plugin_comp_cls_opt(stderr
,
2697 bt_error_cause_component_actor_get_plugin_name(cause
),
2698 bt_error_cause_component_actor_get_component_class_name(cause
),
2699 bt_error_cause_component_actor_get_component_class_type(cause
));
2701 case BT_ERROR_CAUSE_ACTOR_TYPE_COMPONENT_CLASS
:
2702 print_plugin_comp_cls_opt(stderr
,
2703 bt_error_cause_component_class_actor_get_plugin_name(cause
),
2704 bt_error_cause_component_class_actor_get_component_class_name(cause
),
2705 bt_error_cause_component_class_actor_get_component_class_type(cause
));
2707 case BT_ERROR_CAUSE_ACTOR_TYPE_MESSAGE_ITERATOR
:
2708 fprintf(stderr
, "%s%s%s (%s%s%s): ",
2709 bt_common_color_bold(),
2710 bt_error_cause_message_iterator_actor_get_component_name(cause
),
2711 bt_common_color_reset(),
2712 bt_common_color_bold(),
2713 bt_error_cause_message_iterator_actor_get_component_output_port_name(cause
),
2714 bt_common_color_reset());
2715 print_plugin_comp_cls_opt(stderr
,
2716 bt_error_cause_message_iterator_actor_get_plugin_name(cause
),
2717 bt_error_cause_message_iterator_actor_get_component_class_name(cause
),
2718 bt_error_cause_message_iterator_actor_get_component_class_type(cause
));
2724 /* Print file name and line number */
2725 fprintf(stderr
, "] (%s%s%s%s:%s%" PRIu64
"%s)\n",
2726 bt_common_color_bold(),
2727 bt_common_color_fg_magenta(),
2728 bt_error_cause_get_file_name(cause
),
2729 bt_common_color_reset(),
2730 bt_common_color_fg_green(),
2731 bt_error_cause_get_line_number(cause
),
2732 bt_common_color_reset());
2735 folded
= bt_common_fold(bt_error_cause_get_message(cause
),
2738 BT_LOGE_STR("Could not fold string.");
2739 fprintf(stderr
, "%s\n",
2740 bt_error_cause_get_message(cause
));
2744 fprintf(stderr
, "%s\n", folded
->str
);
2745 g_string_free(folded
, TRUE
);
2753 bt_error_release(error
);
2757 int main(int argc
, const char **argv
)
2761 struct bt_config
*cfg
= NULL
;
2764 set_signal_handler();
2765 init_loaded_plugins();
2767 BT_ASSERT(!the_interrupter
);
2768 the_interrupter
= bt_interrupter_create();
2769 if (!the_interrupter
) {
2770 BT_CLI_LOGE_APPEND_CAUSE("Failed to create an interrupter object.");
2775 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
,
2779 /* Quit without errors; typically usage/version */
2781 BT_LOGI_STR("Quitting without errors.");
2786 BT_CLI_LOGE_APPEND_CAUSE(
2787 "Command-line error: retcode=%d", retcode
);
2792 BT_CLI_LOGE_APPEND_CAUSE(
2793 "Failed to create a valid Babeltrace CLI configuration.");
2800 if (cfg
->command_needs_plugins
) {
2801 ret
= require_loaded_plugins(cfg
->plugin_paths
);
2803 BT_CLI_LOGE_APPEND_CAUSE(
2804 "Failed to load plugins: ret=%d", ret
);
2810 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2811 cfg
->command
, cfg
->command_name
);
2813 switch (cfg
->command
) {
2814 case BT_CONFIG_COMMAND_RUN
:
2817 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2818 ret
= cmd_list_plugins(cfg
);
2820 case BT_CONFIG_COMMAND_HELP
:
2821 ret
= cmd_help(cfg
);
2823 case BT_CONFIG_COMMAND_QUERY
:
2824 ret
= cmd_query(cfg
);
2826 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2827 ret
= cmd_print_ctf_metadata(cfg
);
2829 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2830 ret
= cmd_print_lttng_live_sessions(cfg
);
2833 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2837 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2838 cfg
->command
, cfg
->command_name
, ret
);
2839 warn_command_name_and_directory_clash(cfg
);
2840 retcode
= ret
? 1 : 0;
2843 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2844 fini_loaded_plugins();
2845 bt_interrupter_put_ref(the_interrupter
);
2848 print_error_causes();
2852 * Clear current thread's error in case there is one to avoid a
2855 bt_current_thread_clear_error();