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"
30 #include "string-format/format-error.h"
31 #include "string-format/format-plugin-comp-cls-name.h"
40 #include "babeltrace2-cfg.h"
41 #include "babeltrace2-cfg-cli-args.h"
42 #include "babeltrace2-cfg-cli-args-default.h"
43 #include "babeltrace2-log-level.h"
44 #include "babeltrace2-plugins.h"
45 #include "babeltrace2-query.h"
47 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
48 #define NSEC_PER_SEC 1000000000LL
52 BT_CMD_STATUS_ERROR
= -1,
53 BT_CMD_STATUS_INTERRUPTED
= -2,
57 const char *bt_cmd_status_string(enum bt_cmd_status cmd_status
)
60 case BT_CMD_STATUS_OK
:
62 case BT_CMD_STATUS_ERROR
:
64 case BT_CMD_STATUS_INTERRUPTED
:
71 /* Application's interrupter (owned by this) */
72 static bt_interrupter
*the_interrupter
;
79 BOOL WINAPI
signal_handler(DWORD signal
) {
80 if (the_interrupter
) {
81 bt_interrupter_set(the_interrupter
);
88 void set_signal_handler(void)
90 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
91 BT_LOGE("Failed to set the Ctrl+C handler.");
95 #else /* __MINGW32__ */
98 void signal_handler(int signum
)
100 if (signum
!= SIGINT
) {
104 if (the_interrupter
) {
105 bt_interrupter_set(the_interrupter
);
110 void set_signal_handler(void)
112 struct sigaction new_action
, old_action
;
114 new_action
.sa_handler
= signal_handler
;
115 sigemptyset(&new_action
.sa_mask
);
116 new_action
.sa_flags
= 0;
117 sigaction(SIGINT
, NULL
, &old_action
);
119 if (old_action
.sa_handler
!= SIG_IGN
) {
120 sigaction(SIGINT
, &new_action
, NULL
);
124 #endif /* __MINGW32__ */
127 int query(struct bt_config
*cfg
, const bt_component_class
*comp_cls
,
128 const char *obj
, const bt_value
*params
,
129 const bt_value
**user_result
, const char **fail_reason
)
131 return cli_query(comp_cls
, obj
, params
, cfg
->log_level
,
132 the_interrupter
, user_result
, fail_reason
);
135 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
136 const bt_plugin
*, const char *);
139 const void *find_component_class_from_plugin(const char *plugin_name
,
140 const char *comp_class_name
,
141 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
143 const void *comp_class
= NULL
;
144 const bt_plugin
*plugin
;
146 BT_LOGI("Finding component class: plugin-name=\"%s\", "
147 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
149 plugin
= find_loaded_plugin(plugin_name
);
154 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
155 bt_object_get_ref(comp_class
);
156 BT_PLUGIN_PUT_REF_AND_RESET(plugin
);
160 BT_LOGI("Found component class: plugin-name=\"%s\", "
161 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
163 BT_LOGI("Cannot find source component class: "
164 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
165 plugin_name
, comp_class_name
);
172 const bt_component_class_source
*find_source_component_class(
173 const char *plugin_name
, const char *comp_class_name
)
175 return (const void *) find_component_class_from_plugin(
176 plugin_name
, comp_class_name
,
177 (plugin_borrow_comp_cls_func_t
)
178 bt_plugin_borrow_source_component_class_by_name_const
);
182 const bt_component_class_filter
*find_filter_component_class(
183 const char *plugin_name
, const char *comp_class_name
)
185 return (const void *) find_component_class_from_plugin(
186 plugin_name
, comp_class_name
,
187 (plugin_borrow_comp_cls_func_t
)
188 bt_plugin_borrow_filter_component_class_by_name_const
);
192 const bt_component_class_sink
*find_sink_component_class(
193 const char *plugin_name
, const char *comp_class_name
)
195 return (const void *) find_component_class_from_plugin(plugin_name
,
197 (plugin_borrow_comp_cls_func_t
)
198 bt_plugin_borrow_sink_component_class_by_name_const
);
202 const bt_component_class
*find_component_class(const char *plugin_name
,
203 const char *comp_class_name
,
204 bt_component_class_type comp_class_type
)
206 const bt_component_class
*comp_cls
= NULL
;
208 switch (comp_class_type
) {
209 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
210 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
212 case BT_COMPONENT_CLASS_TYPE_FILTER
:
213 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
215 case BT_COMPONENT_CLASS_TYPE_SINK
:
216 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
226 void print_indent(FILE *fp
, size_t indent
)
230 for (i
= 0; i
< indent
; i
++) {
236 void print_value(FILE *, const bt_value
*, size_t);
239 void print_value_rec(FILE *, const bt_value
*, size_t);
242 void print_map_value(const char *key
, const bt_value
*object
, FILE *fp
,
245 print_indent(fp
, indent
);
246 fprintf(fp
, "%s: ", key
);
249 if (bt_value_is_array(object
) &&
250 bt_value_array_is_empty(object
)) {
251 fprintf(fp
, "[ ]\n");
255 if (bt_value_is_map(object
) &&
256 bt_value_map_is_empty(object
)) {
257 fprintf(fp
, "{ }\n");
261 if (bt_value_is_array(object
) ||
262 bt_value_is_map(object
)) {
266 print_value_rec(fp
, object
, indent
+ 2);
273 bt_value_map_foreach_entry_const_func_status
collect_map_keys(
274 const char *key
, const bt_value
*object
, void *data
)
276 GPtrArray
*map_keys
= data
;
278 g_ptr_array_add(map_keys
, (gpointer
*) key
);
280 return BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK
;
284 gint
g_ptr_array_sort_strings(gconstpointer a
, gconstpointer b
) {
285 const char *s1
= *((const char **) a
);
286 const char *s2
= *((const char **) b
);
288 return g_strcmp0(s1
, s2
);
292 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
299 GPtrArray
*map_keys
= NULL
;
303 switch (bt_value_get_type(value
)) {
304 case BT_VALUE_TYPE_NULL
:
305 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
306 bt_common_color_reset());
308 case BT_VALUE_TYPE_BOOL
:
309 bool_val
= bt_value_bool_get(value
);
310 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
311 bt_common_color_fg_bright_cyan(), bool_val
? "yes" : "no",
312 bt_common_color_reset());
314 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
315 uint_val
= bt_value_integer_unsigned_get(value
);
316 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
317 bt_common_color_fg_bright_red(), uint_val
,
318 bt_common_color_reset());
320 case BT_VALUE_TYPE_SIGNED_INTEGER
:
321 int_val
= bt_value_integer_signed_get(value
);
322 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
323 bt_common_color_fg_bright_red(), int_val
,
324 bt_common_color_reset());
326 case BT_VALUE_TYPE_REAL
:
327 dbl_val
= bt_value_real_get(value
);
328 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
329 bt_common_color_fg_bright_red(), dbl_val
,
330 bt_common_color_reset());
332 case BT_VALUE_TYPE_STRING
:
333 str_val
= bt_value_string_get(value
);
334 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
335 bt_common_color_fg_bright_green(), str_val
,
336 bt_common_color_reset());
338 case BT_VALUE_TYPE_ARRAY
:
341 size
= bt_value_array_get_length(value
);
343 print_indent(fp
, indent
);
344 fprintf(fp
, "[ ]\n");
348 for (i
= 0; i
< size
; i
++) {
349 const bt_value
*element
=
350 bt_value_array_borrow_element_by_index_const(
353 print_indent(fp
, indent
);
356 if (bt_value_is_array(element
) &&
357 bt_value_array_is_empty(element
)) {
358 fprintf(fp
, "[ ]\n");
362 if (bt_value_is_map(element
) &&
363 bt_value_map_is_empty(element
)) {
364 fprintf(fp
, "{ }\n");
368 if (bt_value_is_array(element
) ||
369 bt_value_is_map(element
)) {
373 print_value_rec(fp
, element
, indent
+ 2);
377 case BT_VALUE_TYPE_MAP
:
380 bt_value_map_foreach_entry_const_status foreach_status
;
382 if (bt_value_map_is_empty(value
)) {
383 print_indent(fp
, indent
);
384 fprintf(fp
, "{ }\n");
388 map_keys
= g_ptr_array_new();
390 BT_CLI_LOGE_APPEND_CAUSE("Failed to allocated on GPtrArray.");
395 * We want to print the map entries in a stable order. Collect
396 * all the map's keys in a GPtrArray, sort it, then print the
397 * entries in that order.
399 foreach_status
= bt_value_map_foreach_entry_const(value
,
400 collect_map_keys
, map_keys
);
401 if (foreach_status
!= BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK
) {
402 BT_CLI_LOGE_APPEND_CAUSE("Failed to iterator on map value.");
406 g_ptr_array_sort(map_keys
, g_ptr_array_sort_strings
);
408 for (i
= 0; i
< map_keys
->len
; i
++) {
409 const char *map_key
= g_ptr_array_index(map_keys
, i
);
410 const bt_value
*map_value
;
412 map_value
= bt_value_map_borrow_entry_value_const(value
, map_key
);
413 BT_ASSERT(map_value
);
415 print_map_value(map_key
, map_value
, fp
, indent
);
428 g_ptr_array_free(map_keys
, TRUE
);
433 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
435 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
436 print_indent(fp
, indent
);
439 print_value_rec(fp
, value
, indent
);
443 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
447 comp_cls_str
= format_plugin_comp_cls_opt(
448 bt_config_component
->plugin_name
->str
,
449 bt_config_component
->comp_cls_name
->str
,
450 bt_config_component
->type
,
451 BT_COMMON_COLOR_WHEN_AUTO
);
452 BT_ASSERT(comp_cls_str
);
454 fprintf(stderr
, " %s:\n", comp_cls_str
);
456 if (bt_config_component
->instance_name
->len
> 0) {
457 fprintf(stderr
, " Name: %s\n",
458 bt_config_component
->instance_name
->str
);
461 fprintf(stderr
, " Parameters:\n");
462 print_value(stderr
, bt_config_component
->params
, 8);
464 g_free(comp_cls_str
);
468 void print_bt_config_components(GPtrArray
*array
)
472 for (i
= 0; i
< array
->len
; i
++) {
473 struct bt_config_component
*cfg_component
=
474 bt_config_get_component(array
, i
);
475 print_bt_config_component(cfg_component
);
476 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
481 void print_plugin_paths(const bt_value
*plugin_paths
)
483 fprintf(stderr
, " Plugin paths:\n");
484 print_value(stderr
, plugin_paths
, 4);
488 void print_cfg_run(struct bt_config
*cfg
)
492 print_plugin_paths(cfg
->plugin_paths
);
493 fprintf(stderr
, " Source component instances:\n");
494 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
496 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
497 fprintf(stderr
, " Filter component instances:\n");
498 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
501 fprintf(stderr
, " Sink component instances:\n");
502 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
503 fprintf(stderr
, " Connections:\n");
505 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
506 struct bt_config_connection
*cfg_connection
=
507 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
510 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
511 cfg_connection
->upstream_comp_name
->str
,
512 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
513 cfg_connection
->upstream_port_glob
->str
,
514 cfg_connection
->downstream_comp_name
->str
,
515 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
516 cfg_connection
->downstream_port_glob
->str
);
521 void print_cfg_list_plugins(struct bt_config
*cfg
)
523 print_plugin_paths(cfg
->plugin_paths
);
527 void print_cfg_help(struct bt_config
*cfg
)
529 print_plugin_paths(cfg
->plugin_paths
);
533 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
535 print_plugin_paths(cfg
->plugin_paths
);
536 fprintf(stderr
, " Path: %s\n",
537 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
541 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
543 print_plugin_paths(cfg
->plugin_paths
);
544 fprintf(stderr
, " URL: %s\n",
545 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
549 void print_cfg_query(struct bt_config
*cfg
)
551 print_plugin_paths(cfg
->plugin_paths
);
552 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
553 fprintf(stderr
, " Component class:\n");
554 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
558 void print_cfg(struct bt_config
*cfg
)
560 if (!BT_LOG_ON_INFO
) {
564 BT_LOGI_STR("CLI configuration:");
566 switch (cfg
->command
) {
567 case BT_CONFIG_COMMAND_RUN
:
570 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
571 print_cfg_list_plugins(cfg
);
573 case BT_CONFIG_COMMAND_HELP
:
576 case BT_CONFIG_COMMAND_QUERY
:
577 print_cfg_query(cfg
);
579 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
580 print_cfg_print_ctf_metadata(cfg
);
582 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
583 print_cfg_print_lttng_live_sessions(cfg
);
591 void print_plugin_info(const bt_plugin
*plugin
)
593 unsigned int major
, minor
, patch
;
595 bt_property_availability version_avail
;
596 const char *plugin_name
;
600 const char *plugin_description
;
602 plugin_name
= bt_plugin_get_name(plugin
);
603 path
= bt_plugin_get_path(plugin
);
604 author
= bt_plugin_get_author(plugin
);
605 license
= bt_plugin_get_license(plugin
);
606 plugin_description
= bt_plugin_get_description(plugin
);
607 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
609 printf("%s%s%s%s:\n", bt_common_color_bold(),
610 bt_common_color_fg_bright_blue(), plugin_name
,
611 bt_common_color_reset());
613 printf(" %sPath%s: %s\n", bt_common_color_bold(),
614 bt_common_color_reset(), path
);
619 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
620 printf(" %sVersion%s: %u.%u.%u",
621 bt_common_color_bold(), bt_common_color_reset(),
622 major
, minor
, patch
);
631 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
632 bt_common_color_reset(),
633 plugin_description
? plugin_description
: "(None)");
634 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
635 bt_common_color_reset(), author
? author
: "(Unknown)");
636 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
637 bt_common_color_reset(),
638 license
? license
: "(Unknown)");
642 enum bt_cmd_status
cmd_query(struct bt_config
*cfg
)
645 enum bt_cmd_status cmd_status
;
646 const bt_component_class
*comp_cls
= NULL
;
647 const bt_value
*results
= NULL
;
648 const char *fail_reason
= NULL
;
650 comp_cls
= find_component_class(
651 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
652 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
653 cfg
->cmd_data
.query
.cfg_component
->type
);
655 BT_CLI_LOGE_APPEND_CAUSE(
656 "Cannot find component class: plugin-name=\"%s\", "
657 "comp-cls-name=\"%s\", comp-cls-type=%d",
658 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
659 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
660 cfg
->cmd_data
.query
.cfg_component
->type
);
664 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
665 cfg
->cmd_data
.query
.cfg_component
->params
,
666 &results
, &fail_reason
);
668 BT_CLI_LOGE_APPEND_CAUSE(
669 "Failed to query component class: %s: plugin-name=\"%s\", "
670 "comp-cls-name=\"%s\", comp-cls-type=%d "
671 "object=\"%s\"", fail_reason
,
672 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
673 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
674 cfg
->cmd_data
.query
.cfg_component
->type
,
675 cfg
->cmd_data
.query
.object
->str
);
679 print_value(stdout
, results
, 0);
680 cmd_status
= BT_CMD_STATUS_OK
;
684 cmd_status
= BT_CMD_STATUS_ERROR
;
687 bt_component_class_put_ref(comp_cls
);
688 bt_value_put_ref(results
);
693 void print_component_class_help(const char *plugin_name
,
694 const bt_component_class
*comp_cls
)
696 const char *comp_class_name
=
697 bt_component_class_get_name(comp_cls
);
698 const char *comp_class_description
=
699 bt_component_class_get_description(comp_cls
);
700 const char *comp_class_help
=
701 bt_component_class_get_help(comp_cls
);
702 bt_component_class_type type
=
703 bt_component_class_get_type(comp_cls
);
706 comp_cls_str
= format_plugin_comp_cls_opt(plugin_name
, comp_class_name
,
707 type
, BT_COMMON_COLOR_WHEN_AUTO
);
708 BT_ASSERT(comp_cls_str
);
710 printf("%s\n", comp_cls_str
);
711 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
712 bt_common_color_reset(),
713 comp_class_description
? comp_class_description
: "(None)");
715 if (comp_class_help
) {
716 printf("\n%s\n", comp_class_help
);
719 g_free(comp_cls_str
);
723 enum bt_cmd_status
cmd_help(struct bt_config
*cfg
)
725 enum bt_cmd_status cmd_status
;
726 const bt_plugin
*plugin
= NULL
;
727 const bt_component_class
*needed_comp_cls
= NULL
;
729 plugin
= find_loaded_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
731 BT_CLI_LOGE_APPEND_CAUSE(
732 "Cannot find plugin: plugin-name=\"%s\"",
733 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
737 print_plugin_info(plugin
);
738 printf(" %sSource component classes%s: %d\n",
739 bt_common_color_bold(),
740 bt_common_color_reset(),
741 (int) bt_plugin_get_source_component_class_count(plugin
));
742 printf(" %sFilter component classes%s: %d\n",
743 bt_common_color_bold(),
744 bt_common_color_reset(),
745 (int) bt_plugin_get_filter_component_class_count(plugin
));
746 printf(" %sSink component classes%s: %d\n",
747 bt_common_color_bold(),
748 bt_common_color_reset(),
749 (int) bt_plugin_get_sink_component_class_count(plugin
));
751 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
752 /* Plugin help only */
753 cmd_status
= BT_CMD_STATUS_OK
;
757 needed_comp_cls
= find_component_class(
758 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
759 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
760 cfg
->cmd_data
.help
.cfg_component
->type
);
761 if (!needed_comp_cls
) {
762 BT_CLI_LOGE_APPEND_CAUSE(
763 "Cannot find component class: plugin-name=\"%s\", "
764 "comp-cls-name=\"%s\", comp-cls-type=%d",
765 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
766 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
767 cfg
->cmd_data
.help
.cfg_component
->type
);
772 print_component_class_help(
773 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
775 cmd_status
= BT_CMD_STATUS_OK
;
779 cmd_status
= BT_CMD_STATUS_ERROR
;
782 bt_component_class_put_ref(needed_comp_cls
);
783 bt_plugin_put_ref(plugin
);
787 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
789 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
793 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
794 const char *cc_type_name
, uint64_t count
,
795 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
796 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
799 gchar
*comp_cls_str
= NULL
;
802 printf(" %s%s component classes%s: (none)\n",
803 bt_common_color_bold(),
805 bt_common_color_reset());
808 printf(" %s%s component classes%s:\n",
809 bt_common_color_bold(),
811 bt_common_color_reset());
814 for (i
= 0; i
< count
; i
++) {
815 const bt_component_class
*comp_class
=
816 spec_comp_cls_borrow_comp_cls_func(
817 borrow_comp_cls_by_index_func(plugin
, i
));
818 const char *comp_class_name
=
819 bt_component_class_get_name(comp_class
);
820 const char *comp_class_description
=
821 bt_component_class_get_description(comp_class
);
822 bt_component_class_type type
=
823 bt_component_class_get_type(comp_class
);
825 g_free(comp_cls_str
);
826 comp_cls_str
= format_plugin_comp_cls_opt(
827 bt_plugin_get_name(plugin
), comp_class_name
, type
,
828 BT_COMMON_COLOR_WHEN_AUTO
);
829 printf(" %s", comp_cls_str
);
831 if (comp_class_description
) {
832 printf(": %s", comp_class_description
);
839 g_free(comp_cls_str
);
843 enum bt_cmd_status
cmd_list_plugins(struct bt_config
*cfg
)
845 int plugins_count
, component_classes_count
= 0, i
;
847 printf("From the following plugin paths:\n\n");
848 print_value(stdout
, cfg
->plugin_paths
, 2);
850 plugins_count
= get_loaded_plugins_count();
851 if (plugins_count
== 0) {
852 printf("No plugins found.\n");
856 for (i
= 0; i
< plugins_count
; i
++) {
857 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
859 component_classes_count
+=
860 bt_plugin_get_source_component_class_count(plugin
) +
861 bt_plugin_get_filter_component_class_count(plugin
) +
862 bt_plugin_get_sink_component_class_count(plugin
);
865 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
866 bt_common_color_bold(),
867 component_classes_count
,
868 bt_common_color_reset(),
869 bt_common_color_bold(),
871 bt_common_color_reset());
873 for (i
= 0; i
< plugins_count
; i
++) {
874 const bt_plugin
*plugin
= borrow_loaded_plugin(i
);
877 print_plugin_info(plugin
);
878 cmd_list_plugins_print_component_classes(plugin
, "Source",
879 bt_plugin_get_source_component_class_count(plugin
),
880 (plugin_borrow_comp_cls_by_index_func_t
)
881 bt_plugin_borrow_source_component_class_by_index_const
,
882 (spec_comp_cls_borrow_comp_cls_func_t
)
883 bt_component_class_source_as_component_class
);
884 cmd_list_plugins_print_component_classes(plugin
, "Filter",
885 bt_plugin_get_filter_component_class_count(plugin
),
886 (plugin_borrow_comp_cls_by_index_func_t
)
887 bt_plugin_borrow_filter_component_class_by_index_const
,
888 (spec_comp_cls_borrow_comp_cls_func_t
)
889 bt_component_class_filter_as_component_class
);
890 cmd_list_plugins_print_component_classes(plugin
, "Sink",
891 bt_plugin_get_sink_component_class_count(plugin
),
892 (plugin_borrow_comp_cls_by_index_func_t
)
893 bt_plugin_borrow_sink_component_class_by_index_const
,
894 (spec_comp_cls_borrow_comp_cls_func_t
)
895 bt_component_class_sink_as_component_class
);
899 return BT_CMD_STATUS_OK
;
903 enum bt_cmd_status
cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
906 enum bt_cmd_status cmd_status
;
907 const bt_component_class
*comp_cls
= NULL
;
908 const bt_value
*results
= NULL
;
909 bt_value
*params
= NULL
;
910 const bt_value
*map
= NULL
;
911 const bt_value
*v
= NULL
;
912 static const char * const plugin_name
= "ctf";
913 static const char * const comp_cls_name
= "lttng-live";
914 static const bt_component_class_type comp_cls_type
=
915 BT_COMPONENT_CLASS_TYPE_SOURCE
;
916 uint64_t array_size
, i
;
917 const char *fail_reason
= NULL
;
918 FILE *out_stream
= stdout
;
920 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
921 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
924 BT_CLI_LOGE_APPEND_CAUSE(
925 "Cannot find component class: plugin-name=\"%s\", "
926 "comp-cls-name=\"%s\", comp-cls-type=%d",
927 plugin_name
, comp_cls_name
,
928 BT_COMPONENT_CLASS_TYPE_SOURCE
);
932 params
= bt_value_map_create();
937 ret
= bt_value_map_insert_string_entry(params
, "url",
938 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
943 ret
= query(cfg
, comp_cls
, "sessions", params
,
944 &results
, &fail_reason
);
946 BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s",
953 if (!bt_value_is_array(results
)) {
954 BT_CLI_LOGE_APPEND_CAUSE(
955 "Expecting an array for LTTng live `sessions` query.");
959 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
961 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
964 BT_LOGE_ERRNO("Cannot open file for writing",
966 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
967 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
969 "Cannot open file for writing: path=\"%s\"",
970 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
975 array_size
= bt_value_array_get_length(results
);
976 for (i
= 0; i
< array_size
; i
++) {
977 const char *url_text
;
978 int64_t timer_us
, streams
, clients
;
980 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
981 if (!bt_value_is_map(map
)) {
982 BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type.");
986 v
= bt_value_map_borrow_entry_value_const(map
, "url");
988 BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry.");
991 url_text
= bt_value_string_get(v
);
992 fprintf(out_stream
, "%s", url_text
);
993 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
995 BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry.");
998 timer_us
= bt_value_integer_unsigned_get(v
);
999 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
1000 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
1002 BT_CLI_LOGE_APPEND_CAUSE(
1003 "Missing `stream-count` entry.");
1006 streams
= bt_value_integer_unsigned_get(v
);
1007 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
1008 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
1010 BT_CLI_LOGE_APPEND_CAUSE(
1011 "Missing `client-count` entry.");
1014 clients
= bt_value_integer_unsigned_get(v
);
1015 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1018 cmd_status
= BT_CMD_STATUS_OK
;
1022 cmd_status
= BT_CMD_STATUS_ERROR
;
1025 bt_value_put_ref(results
);
1026 bt_value_put_ref(params
);
1027 bt_component_class_put_ref(comp_cls
);
1029 if (out_stream
&& out_stream
!= stdout
) {
1030 int fclose_ret
= fclose(out_stream
);
1033 BT_LOGE_ERRNO("Cannot close file stream",
1035 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1043 enum bt_cmd_status
cmd_print_ctf_metadata(struct bt_config
*cfg
)
1046 enum bt_cmd_status cmd_status
;
1047 const bt_component_class
*comp_cls
= NULL
;
1048 const bt_value
*results
= NULL
;
1049 bt_value
*params
= NULL
;
1050 const bt_value
*metadata_text_value
= NULL
;
1051 const char *metadata_text
= NULL
;
1052 static const char * const plugin_name
= "ctf";
1053 static const char * const comp_cls_name
= "fs";
1054 static const bt_component_class_type comp_cls_type
=
1055 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1056 const char *fail_reason
= NULL
;
1057 FILE *out_stream
= stdout
;
1059 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1060 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1063 BT_CLI_LOGE_APPEND_CAUSE(
1064 "Cannot find component class: plugin-name=\"%s\", "
1065 "comp-cls-name=\"%s\", comp-cls-type=%d",
1066 plugin_name
, comp_cls_name
,
1067 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1071 params
= bt_value_map_create();
1076 ret
= bt_value_map_insert_string_entry(params
, "path",
1077 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1082 ret
= query(cfg
, comp_cls
, "metadata-info",
1083 params
, &results
, &fail_reason
);
1085 BT_CLI_LOGE_APPEND_CAUSE(
1086 "Failed to query `metadata-info` object: %s", fail_reason
);
1090 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1092 if (!metadata_text_value
) {
1093 BT_CLI_LOGE_APPEND_CAUSE(
1094 "Cannot find `text` string value in the resulting metadata info object.");
1098 metadata_text
= bt_value_string_get(metadata_text_value
);
1100 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1102 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1105 BT_LOGE_ERRNO("Cannot open file for writing",
1107 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1108 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1110 "Cannot open file for writing: path=\"%s\"",
1111 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1116 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1118 BT_CLI_LOGE_APPEND_CAUSE(
1119 "Cannot write whole metadata text to output stream: "
1124 cmd_status
= BT_CMD_STATUS_OK
;
1128 cmd_status
= BT_CMD_STATUS_ERROR
;
1131 bt_value_put_ref(results
);
1132 bt_value_put_ref(params
);
1133 bt_component_class_put_ref(comp_cls
);
1135 if (out_stream
&& out_stream
!= stdout
) {
1136 int fclose_ret
= fclose(out_stream
);
1139 BT_LOGE_ERRNO("Cannot close file stream",
1141 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1149 char *instance_name
;
1153 struct trace_range
{
1154 uint64_t intersection_range_begin_ns
;
1155 uint64_t intersection_range_end_ns
;
1159 guint
port_id_hash(gconstpointer v
)
1161 const struct port_id
*id
= v
;
1163 BT_ASSERT(id
->instance_name
);
1164 BT_ASSERT(id
->port_name
);
1166 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1170 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1172 const struct port_id
*id1
= v1
;
1173 const struct port_id
*id2
= v2
;
1175 return strcmp(id1
->instance_name
, id2
->instance_name
) == 0 &&
1176 strcmp(id1
->port_name
, id2
->port_name
) == 0;
1180 void port_id_destroy(gpointer data
)
1182 struct port_id
*id
= data
;
1184 free(id
->instance_name
);
1185 free(id
->port_name
);
1190 void trace_range_destroy(gpointer data
)
1195 struct cmd_run_ctx
{
1197 GHashTable
*src_components
;
1200 GHashTable
*flt_components
;
1203 GHashTable
*sink_components
;
1209 struct bt_config
*cfg
;
1213 bool stream_intersection_mode
;
1216 * Association of struct port_id -> struct trace_range.
1218 GHashTable
*intersections
;
1221 /* Returns a timestamp of the form "(-)s.ns" */
1223 char *s_from_ns(int64_t ns
)
1228 int64_t ts_sec_abs
, ts_nsec_abs
;
1229 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1230 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1232 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1233 is_negative
= false;
1234 ts_sec_abs
= ts_sec
;
1235 ts_nsec_abs
= ts_nsec
;
1236 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1237 is_negative
= false;
1238 ts_sec_abs
= ts_sec
- 1;
1239 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1240 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1242 ts_sec_abs
= ts_sec
;
1243 ts_nsec_abs
= -ts_nsec
;
1244 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1246 ts_sec_abs
= -(ts_sec
+ 1);
1247 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1248 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1250 ts_sec_abs
= -ts_sec
;
1251 ts_nsec_abs
= ts_nsec
;
1252 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1254 ts_sec_abs
= -ts_sec
;
1255 ts_nsec_abs
= -ts_nsec
;
1258 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1259 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1267 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1268 struct cmd_run_ctx
*ctx
,
1269 const bt_component
*upstream_comp
,
1270 const bt_port_output
*out_upstream_port
,
1271 struct bt_config_connection
*cfg_conn
)
1273 typedef uint64_t (*input_port_count_func_t
)(void *);
1274 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1275 const void *, uint64_t);
1276 const bt_port
*upstream_port
=
1277 bt_port_output_as_port_const(out_upstream_port
);
1280 GQuark downstreamp_comp_name_quark
;
1281 void *downstream_comp
;
1282 uint64_t downstream_port_count
;
1284 input_port_count_func_t port_count_fn
;
1285 borrow_input_port_by_index_func_t port_by_index_fn
;
1286 bt_graph_connect_ports_status connect_ports_status
=
1287 BT_GRAPH_CONNECT_PORTS_STATUS_OK
;
1288 bool insert_trimmer
= false;
1289 bt_value
*trimmer_params
= NULL
;
1290 char *intersection_begin
= NULL
;
1291 char *intersection_end
= NULL
;
1292 const bt_component_filter
*trimmer
= NULL
;
1293 const bt_component_class_filter
*trimmer_class
= NULL
;
1294 const bt_port_input
*trimmer_input
= NULL
;
1295 const bt_port_output
*trimmer_output
= NULL
;
1297 if (ctx
->intersections
&&
1298 bt_component_get_class_type(upstream_comp
) ==
1299 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1300 struct trace_range
*range
;
1301 struct port_id port_id
= {
1302 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1303 .port_name
= (char *) bt_port_get_name(upstream_port
)
1306 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1310 range
= (struct trace_range
*) g_hash_table_lookup(
1311 ctx
->intersections
, &port_id
);
1313 bt_value_map_insert_entry_status insert_status
;
1315 intersection_begin
= s_from_ns(
1316 range
->intersection_range_begin_ns
);
1317 intersection_end
= s_from_ns(
1318 range
->intersection_range_end_ns
);
1319 if (!intersection_begin
|| !intersection_end
) {
1320 BT_CLI_LOGE_APPEND_CAUSE(
1321 "Cannot create trimmer argument timestamp string.");
1325 insert_trimmer
= true;
1326 trimmer_params
= bt_value_map_create();
1327 if (!trimmer_params
) {
1331 insert_status
= bt_value_map_insert_string_entry(
1332 trimmer_params
, "begin", intersection_begin
);
1333 if (insert_status
< 0) {
1336 insert_status
= bt_value_map_insert_string_entry(
1338 "end", intersection_end
);
1339 if (insert_status
< 0) {
1344 trimmer_class
= find_filter_component_class("utils", "trimmer");
1345 if (!trimmer_class
) {
1350 BT_LOGI("Connecting upstream port to the next available downstream port: "
1351 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1352 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1353 upstream_port
, bt_port_get_name(upstream_port
),
1354 cfg_conn
->downstream_comp_name
->str
,
1355 cfg_conn
->arg
->str
);
1356 downstreamp_comp_name_quark
= g_quark_from_string(
1357 cfg_conn
->downstream_comp_name
->str
);
1358 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1359 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1360 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1361 port_count_fn
= (input_port_count_func_t
)
1362 bt_component_filter_get_input_port_count
;
1363 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1364 bt_component_filter_borrow_input_port_by_index_const
;
1366 if (!downstream_comp
) {
1367 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1368 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1369 port_count_fn
= (input_port_count_func_t
)
1370 bt_component_sink_get_input_port_count
;
1371 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1372 bt_component_sink_borrow_input_port_by_index_const
;
1375 if (!downstream_comp
) {
1376 BT_CLI_LOGE_APPEND_CAUSE("Cannot find downstream component: "
1377 "comp-name=\"%s\", conn-arg=\"%s\"",
1378 cfg_conn
->downstream_comp_name
->str
,
1379 cfg_conn
->arg
->str
);
1383 downstream_port_count
= port_count_fn(downstream_comp
);
1385 for (i
= 0; i
< downstream_port_count
; i
++) {
1386 const bt_port_input
*in_downstream_port
=
1387 port_by_index_fn(downstream_comp
, i
);
1388 const bt_port
*downstream_port
=
1389 bt_port_input_as_port_const(in_downstream_port
);
1390 const char *upstream_port_name
;
1391 const char *downstream_port_name
;
1393 BT_ASSERT(downstream_port
);
1395 /* Skip port if it's already connected. */
1396 if (bt_port_is_connected(downstream_port
)) {
1397 BT_LOGI("Skipping downstream port: already connected: "
1398 "port-addr=%p, port-name=\"%s\"",
1400 bt_port_get_name(downstream_port
));
1404 downstream_port_name
= bt_port_get_name(downstream_port
);
1405 BT_ASSERT(downstream_port_name
);
1406 upstream_port_name
= bt_port_get_name(upstream_port
);
1407 BT_ASSERT(upstream_port_name
);
1409 if (!bt_common_star_glob_match(
1410 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1411 downstream_port_name
, SIZE_MAX
)) {
1415 if (insert_trimmer
) {
1417 * In order to insert the trimmer between the
1418 * two components that were being connected, we
1419 * create a connection configuration entry which
1420 * describes a connection from the trimmer's
1421 * output to the original input that was being
1424 * Hence, the creation of the trimmer will cause
1425 * the graph "new port" listener to establish
1426 * all downstream connections as its output port
1427 * is connected. We will then establish the
1428 * connection between the original upstream
1429 * source and the trimmer.
1431 char *trimmer_name
= NULL
;
1432 bt_graph_add_component_status add_comp_status
;
1434 ret
= asprintf(&trimmer_name
,
1435 "stream-intersection-trimmer-%s",
1436 upstream_port_name
);
1442 ctx
->connect_ports
= false;
1443 add_comp_status
= bt_graph_add_filter_component(
1444 ctx
->graph
, trimmer_class
, trimmer_name
,
1445 trimmer_params
, ctx
->cfg
->log_level
,
1448 if (add_comp_status
!=
1449 BT_GRAPH_ADD_COMPONENT_STATUS_OK
) {
1455 bt_component_filter_borrow_input_port_by_index_const(
1457 if (!trimmer_input
) {
1461 bt_component_filter_borrow_output_port_by_index_const(
1463 if (!trimmer_output
) {
1468 * Replace the current downstream port by the trimmer's
1471 in_downstream_port
= trimmer_input
;
1473 bt_port_input_as_port_const(in_downstream_port
);
1474 downstream_port_name
= bt_port_get_name(
1476 BT_ASSERT(downstream_port_name
);
1479 /* We have a winner! */
1480 connect_ports_status
= bt_graph_connect_ports(ctx
->graph
,
1481 out_upstream_port
, in_downstream_port
, NULL
);
1482 downstream_port
= NULL
;
1483 switch (connect_ports_status
) {
1484 case BT_GRAPH_CONNECT_PORTS_STATUS_OK
:
1487 BT_CLI_LOGE_APPEND_CAUSE(
1488 "Cannot create connection: graph refuses to connect ports: "
1489 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1490 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1491 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1492 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1494 upstream_comp
, bt_component_get_name(upstream_comp
),
1495 upstream_port
, bt_port_get_name(upstream_port
),
1496 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1497 downstream_port
, downstream_port_name
,
1498 cfg_conn
->arg
->str
);
1502 BT_LOGI("Connected component ports: "
1503 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1504 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1505 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1506 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1508 upstream_comp
, bt_component_get_name(upstream_comp
),
1509 upstream_port
, bt_port_get_name(upstream_port
),
1510 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1511 downstream_port
, downstream_port_name
,
1512 cfg_conn
->arg
->str
);
1514 if (insert_trimmer
) {
1516 * The first connection, from the source to the trimmer,
1517 * has been done. We now connect the trimmer to the
1518 * original downstream port.
1520 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1522 bt_component_filter_as_component_const(trimmer
),
1523 trimmer_output
, cfg_conn
);
1527 ctx
->connect_ports
= true;
1531 * We found a matching downstream port: the search is
1537 /* No downstream port found */
1538 BT_CLI_LOGE_APPEND_CAUSE(
1539 "Cannot create connection: cannot find a matching downstream port for upstream port: "
1540 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1541 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1542 upstream_port
, bt_port_get_name(upstream_port
),
1543 cfg_conn
->downstream_comp_name
->str
,
1544 cfg_conn
->arg
->str
);
1550 free(intersection_begin
);
1551 free(intersection_end
);
1552 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1553 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1554 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1559 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1560 const bt_port_output
*upstream_port
)
1563 const char *upstream_port_name
;
1564 const char *upstream_comp_name
;
1565 const bt_component
*upstream_comp
= NULL
;
1569 BT_ASSERT(upstream_port
);
1570 upstream_port_name
= bt_port_get_name(
1571 bt_port_output_as_port_const(upstream_port
));
1572 BT_ASSERT(upstream_port_name
);
1573 upstream_comp
= bt_port_borrow_component_const(
1574 bt_port_output_as_port_const(upstream_port
));
1575 BT_ASSERT(upstream_comp
);
1576 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1577 BT_ASSERT(upstream_comp_name
);
1578 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1579 "port-addr=%p, port-name=\"%s\"",
1580 upstream_comp
, upstream_comp_name
,
1581 upstream_port
, upstream_port_name
);
1583 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1584 struct bt_config_connection
*cfg_conn
=
1586 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1588 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1589 upstream_comp_name
)) {
1593 if (!bt_common_star_glob_match(
1594 cfg_conn
->upstream_port_glob
->str
,
1595 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1599 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1600 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1602 BT_CLI_LOGE_APPEND_CAUSE(
1603 "Cannot connect upstream port: "
1604 "port-addr=%p, port-name=\"%s\"",
1606 upstream_port_name
);
1612 BT_CLI_LOGE_APPEND_CAUSE(
1613 "Cannot connect upstream port: port does not match any connection argument: "
1614 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1615 upstream_port_name
);
1625 bt_graph_listener_func_status
1626 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1627 const bt_port_output
*out_port
)
1629 const bt_component
*comp
;
1630 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1631 bt_graph_listener_func_status ret
=
1632 BT_GRAPH_LISTENER_FUNC_STATUS_OK
;
1634 comp
= bt_port_borrow_component_const(port
);
1635 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1636 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1637 comp
, comp
? bt_component_get_name(comp
) : "",
1638 port
, bt_port_get_name(port
));
1640 if (!ctx
->connect_ports
) {
1646 if (bt_port_is_connected(port
)) {
1647 BT_LOGW_STR("Port is already connected.");
1651 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
1652 BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port.");
1653 ret
= BT_GRAPH_LISTENER_FUNC_STATUS_ERROR
;
1662 bt_graph_listener_func_status
graph_source_output_port_added_listener(
1663 const bt_component_source
*component
,
1664 const bt_port_output
*port
, void *data
)
1666 return graph_output_port_added_listener(data
, port
);
1670 bt_graph_listener_func_status
graph_filter_output_port_added_listener(
1671 const bt_component_filter
*component
,
1672 const bt_port_output
*port
, void *data
)
1674 return graph_output_port_added_listener(data
, port
);
1678 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1684 if (ctx
->src_components
) {
1685 g_hash_table_destroy(ctx
->src_components
);
1686 ctx
->src_components
= NULL
;
1689 if (ctx
->flt_components
) {
1690 g_hash_table_destroy(ctx
->flt_components
);
1691 ctx
->flt_components
= NULL
;
1694 if (ctx
->sink_components
) {
1695 g_hash_table_destroy(ctx
->sink_components
);
1696 ctx
->sink_components
= NULL
;
1699 if (ctx
->intersections
) {
1700 g_hash_table_destroy(ctx
->intersections
);
1701 ctx
->intersections
= NULL
;
1704 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
1709 int add_descriptor_to_component_descriptor_set(
1710 bt_component_descriptor_set
*comp_descr_set
,
1711 const char *plugin_name
, const char *comp_cls_name
,
1712 bt_component_class_type comp_cls_type
,
1713 const bt_value
*params
)
1715 const bt_component_class
*comp_cls
;
1718 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1721 BT_CLI_LOGE_APPEND_CAUSE(
1722 "Cannot find component class: plugin-name=\"%s\", "
1723 "comp-cls-name=\"%s\", comp-cls-type=%d",
1724 plugin_name
, comp_cls_name
, comp_cls_type
);
1729 status
= bt_component_descriptor_set_add_descriptor(
1730 comp_descr_set
, comp_cls
, params
);
1731 if (status
!= BT_COMPONENT_DESCRIPTOR_SET_ADD_DESCRIPTOR_STATUS_OK
) {
1732 BT_CLI_LOGE_APPEND_CAUSE(
1733 "Cannot append descriptor to component descriptor set: "
1734 "status=%s", bt_common_func_status_string(status
));
1739 bt_component_class_put_ref(comp_cls
);
1744 int append_descriptors_from_bt_config_component_array(
1745 bt_component_descriptor_set
*comp_descr_set
,
1746 GPtrArray
*component_configs
)
1751 for (i
= 0; i
< component_configs
->len
; i
++) {
1752 struct bt_config_component
*cfg_comp
=
1753 component_configs
->pdata
[i
];
1755 ret
= add_descriptor_to_component_descriptor_set(
1757 cfg_comp
->plugin_name
->str
,
1758 cfg_comp
->comp_cls_name
->str
,
1759 cfg_comp
->type
, cfg_comp
->params
);
1770 bt_get_greatest_operative_mip_version_status
get_greatest_operative_mip_version(
1771 struct bt_config
*cfg
, uint64_t *mip_version
)
1773 bt_get_greatest_operative_mip_version_status status
=
1774 BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK
;
1775 bt_component_descriptor_set
*comp_descr_set
= NULL
;
1779 BT_ASSERT(mip_version
);
1780 comp_descr_set
= bt_component_descriptor_set_create();
1781 if (!comp_descr_set
) {
1782 BT_CLI_LOGE_APPEND_CAUSE(
1783 "Failed to create a component descriptor set object.");
1784 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_MEMORY_ERROR
;
1788 ret
= append_descriptors_from_bt_config_component_array(
1789 comp_descr_set
, cfg
->cmd_data
.run
.sources
);
1791 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1795 ret
= append_descriptors_from_bt_config_component_array(
1796 comp_descr_set
, cfg
->cmd_data
.run
.filters
);
1798 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1802 ret
= append_descriptors_from_bt_config_component_array(
1803 comp_descr_set
, cfg
->cmd_data
.run
.sinks
);
1805 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1809 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1811 * Stream intersection mode adds `flt.utils.trimmer`
1812 * components; we need to include this type of component
1813 * in the component descriptor set to get the real
1814 * greatest operative MIP version.
1816 ret
= add_descriptor_to_component_descriptor_set(
1817 comp_descr_set
, "utils", "trimmer",
1818 BT_COMPONENT_CLASS_TYPE_FILTER
, NULL
);
1820 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1825 status
= bt_get_greatest_operative_mip_version(comp_descr_set
,
1826 bt_cli_log_level
, mip_version
);
1829 bt_component_descriptor_set_put_ref(comp_descr_set
);
1834 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1837 bt_graph_add_listener_status add_listener_status
;
1838 bt_get_greatest_operative_mip_version_status mip_version_status
;
1839 uint64_t mip_version
= UINT64_C(-1);
1842 ctx
->connect_ports
= false;
1843 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
1844 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1845 if (!ctx
->src_components
) {
1849 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
1850 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1851 if (!ctx
->flt_components
) {
1855 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
1856 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1857 if (!ctx
->sink_components
) {
1861 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1862 ctx
->stream_intersection_mode
= true;
1863 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1864 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1865 if (!ctx
->intersections
) {
1871 * Get the greatest operative MIP version to use to configure
1872 * the graph to create.
1874 mip_version_status
= get_greatest_operative_mip_version(
1876 if (mip_version_status
== BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_NO_MATCH
) {
1877 BT_CLI_LOGE_APPEND_CAUSE(
1878 "Failed to find an operative message interchange "
1879 "protocol version to use to create the `run` command's "
1880 "graph (components are not interoperable).");
1882 } else if (mip_version_status
< 0) {
1883 BT_CLI_LOGE_APPEND_CAUSE(
1884 "Cannot find an operative message interchange "
1885 "protocol version to use to create the `run` command's "
1887 bt_common_func_status_string(mip_version_status
));
1891 BT_ASSERT(mip_version_status
== BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK
);
1892 BT_LOGI("Found operative message interchange protocol version to "
1893 "configure the `run` command's graph: mip-version=%" PRIu64
,
1895 ctx
->graph
= bt_graph_create(mip_version
);
1900 bt_graph_add_interrupter(ctx
->graph
, the_interrupter
);
1901 add_listener_status
= bt_graph_add_source_component_output_port_added_listener(
1902 ctx
->graph
, graph_source_output_port_added_listener
, ctx
,
1904 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1905 BT_CLI_LOGE_APPEND_CAUSE(
1906 "Cannot add \"port added\" listener to graph.");
1910 add_listener_status
= bt_graph_add_filter_component_output_port_added_listener(
1911 ctx
->graph
, graph_filter_output_port_added_listener
, ctx
,
1913 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1914 BT_CLI_LOGE_APPEND_CAUSE(
1915 "Cannot add \"port added\" listener to graph.");
1922 cmd_run_ctx_destroy(ctx
);
1930 * Compute the intersection of all streams in the array `streams`, write it
1935 int compute_stream_intersection(const bt_value
*streams
,
1936 struct trace_range
*range
)
1938 uint64_t i
, stream_count
;
1941 BT_ASSERT(bt_value_get_type(streams
) == BT_VALUE_TYPE_ARRAY
);
1943 stream_count
= bt_value_array_get_length(streams
);
1945 BT_ASSERT(stream_count
> 0);
1947 range
->intersection_range_begin_ns
= 0;
1948 range
->intersection_range_end_ns
= UINT64_MAX
;
1950 for (i
= 0; i
< stream_count
; i
++) {
1951 int64_t begin_ns
, end_ns
;
1952 uint64_t begin_ns_u
, end_ns_u
;
1953 const bt_value
*stream_value
;
1954 const bt_value
*range_ns_value
;
1955 const bt_value
*begin_value
;
1956 const bt_value
*end_value
;
1958 stream_value
= bt_value_array_borrow_element_by_index_const(streams
, i
);
1959 if (bt_value_get_type(stream_value
) != BT_VALUE_TYPE_MAP
) {
1960 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1961 "expected streams array element to be a map, got %s.",
1962 bt_common_value_type_string(bt_value_get_type(stream_value
)));
1966 range_ns_value
= bt_value_map_borrow_entry_value_const(
1967 stream_value
, "range-ns");
1968 if (!range_ns_value
) {
1969 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1970 "missing expected `range-ns` key in stream map.");
1974 if (bt_value_get_type(range_ns_value
) != BT_VALUE_TYPE_MAP
) {
1975 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1976 "expected `range-ns` entry value of stream map to be a map, got %s.",
1977 bt_common_value_type_string(bt_value_get_type(range_ns_value
)));
1981 begin_value
= bt_value_map_borrow_entry_value_const(range_ns_value
, "begin");
1983 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1984 "missing expected `begin` key in range-ns map.");
1988 if (bt_value_get_type(begin_value
) != BT_VALUE_TYPE_SIGNED_INTEGER
) {
1989 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1990 "expected `begin` entry value of range-ns map to be a signed integer, got %s.",
1991 bt_common_value_type_string(bt_value_get_type(range_ns_value
)));
1995 end_value
= bt_value_map_borrow_entry_value_const(range_ns_value
, "end");
1997 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1998 "missing expected `end` key in range-ns map.");
2002 if (bt_value_get_type(end_value
) != BT_VALUE_TYPE_SIGNED_INTEGER
) {
2003 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
2004 "expected `end` entry value of range-ns map to be a signed integer, got %s.",
2005 bt_common_value_type_string(bt_value_get_type(range_ns_value
)));
2009 begin_ns
= bt_value_integer_signed_get(begin_value
);
2010 end_ns
= bt_value_integer_signed_get(end_value
);
2012 if (begin_ns
< 0 || end_ns
< 0 || end_ns
< begin_ns
) {
2013 BT_CLI_LOGE_APPEND_CAUSE(
2014 "Invalid stream range values: "
2015 "range-ns:begin=%" PRId64
", "
2016 "range-ns:end=%" PRId64
,
2021 begin_ns_u
= begin_ns
;
2024 range
->intersection_range_begin_ns
=
2025 MAX(range
->intersection_range_begin_ns
, begin_ns_u
);
2026 range
->intersection_range_end_ns
=
2027 MIN(range
->intersection_range_end_ns
, end_ns_u
);
2040 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2041 struct bt_config_component
*cfg_comp
,
2042 const bt_component_class_source
*src_comp_cls
)
2046 uint64_t trace_count
;
2047 const bt_value
*query_result
= NULL
;
2048 const bt_value
*trace_info
= NULL
;
2049 const bt_value
*stream_infos
= NULL
;
2050 const bt_value
*stream_info
= NULL
;
2051 struct port_id
*port_id
= NULL
;
2052 struct trace_range
*stream_intersection
= NULL
;
2053 const char *fail_reason
= NULL
;
2054 const bt_component_class
*comp_cls
=
2055 bt_component_class_source_as_component_class_const(src_comp_cls
);
2057 ret
= query(ctx
->cfg
, comp_cls
, "babeltrace.trace-infos",
2058 cfg_comp
->params
, &query_result
,
2061 BT_CLI_LOGE_APPEND_CAUSE("Failed to execute `babeltrace.trace-infos` query: %s: "
2062 "comp-class-name=\"%s\"", fail_reason
,
2063 bt_component_class_get_name(comp_cls
));
2068 BT_ASSERT(query_result
);
2070 if (!bt_value_is_array(query_result
)) {
2071 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting result to be an array: "
2072 "component-class-name=%s, actual-type=%s",
2073 bt_component_class_get_name(comp_cls
),
2074 bt_common_value_type_string(bt_value_get_type(query_result
)));
2079 trace_count
= bt_value_array_get_length(query_result
);
2080 if (trace_count
== 0) {
2081 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: result is empty: "
2082 "component-class-name=%s", bt_component_class_get_name(comp_cls
));
2087 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2088 uint64_t stream_idx
;
2089 int64_t stream_count
;
2090 struct trace_range trace_intersection
;
2092 trace_info
= bt_value_array_borrow_element_by_index_const(
2093 query_result
, trace_idx
);
2094 if (!bt_value_is_map(trace_info
)) {
2096 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting element to be a map: "
2097 "component-class-name=%s, actual-type=%s",
2098 bt_component_class_get_name(comp_cls
),
2099 bt_common_value_type_string(bt_value_get_type(trace_info
)));
2103 stream_infos
= bt_value_map_borrow_entry_value_const(
2104 trace_info
, "stream-infos");
2105 if (!stream_infos
) {
2107 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: missing `streams` key in trace info map: "
2108 "component-class-name=%s",
2109 bt_component_class_get_name(comp_cls
));
2113 if (!bt_value_is_array(stream_infos
)) {
2115 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting `streams` entry of trace info map to be an array: "
2116 "component-class-name=%s, actual-type=%s",
2117 bt_component_class_get_name(comp_cls
),
2118 bt_common_value_type_string(bt_value_get_type(stream_infos
)));
2122 stream_count
= bt_value_array_get_length(stream_infos
);
2123 if (stream_count
== 0) {
2125 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: list of streams is empty: "
2126 "component-class-name=%s",
2127 bt_component_class_get_name(comp_cls
));
2131 ret
= compute_stream_intersection(stream_infos
, &trace_intersection
);
2133 BT_CLI_LOGE_APPEND_CAUSE("Failed to compute trace streams intersection.");
2137 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2138 const bt_value
*port_name
;
2140 port_id
= g_new0(struct port_id
, 1);
2143 BT_CLI_LOGE_APPEND_CAUSE(
2144 "Cannot allocate memory for port_id structure.");
2147 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2148 if (!port_id
->instance_name
) {
2150 BT_CLI_LOGE_APPEND_CAUSE(
2151 "Cannot allocate memory for port_id component instance name.");
2155 stream_intersection
= g_new0(struct trace_range
, 1);
2156 if (!stream_intersection
) {
2158 BT_CLI_LOGE_APPEND_CAUSE(
2159 "Cannot allocate memory for trace_range structure.");
2163 *stream_intersection
= trace_intersection
;
2165 stream_info
= bt_value_array_borrow_element_by_index_const(
2166 stream_infos
, stream_idx
);
2167 if (!bt_value_is_map(stream_info
)) {
2169 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
2170 "expecting element of stream list to be a map: "
2171 "component-class-name=%s, actual-type=%s",
2172 bt_component_class_get_name(comp_cls
),
2173 bt_common_value_type_string(bt_value_get_type(stream_info
)));
2177 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2180 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
2181 "missing `port-name` key in stream info map: "
2182 "component-class-name=%s",
2183 bt_component_class_get_name(comp_cls
));
2187 if (!bt_value_is_string(port_name
)) {
2189 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
2190 "expecting `port-name` entry of stream info map to be a string: "
2191 "component-class-name=%s, actual-type=%s",
2192 bt_component_class_get_name(comp_cls
),
2193 bt_common_value_type_string(bt_value_get_type(port_name
)));
2197 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2198 if (!port_id
->port_name
) {
2200 BT_CLI_LOGE_APPEND_CAUSE(
2201 "Cannot allocate memory for port_id port_name.");
2205 BT_LOGD("Inserting stream intersection ");
2207 g_hash_table_insert(ctx
->intersections
, port_id
, stream_intersection
);
2210 stream_intersection
= NULL
;
2217 bt_value_put_ref(query_result
);
2219 g_free(stream_intersection
);
2224 int cmd_run_ctx_create_components_from_config_components(
2225 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2228 const void *comp_cls
= NULL
;
2229 const void *comp
= NULL
;
2232 for (i
= 0; i
< cfg_components
->len
; i
++) {
2233 struct bt_config_component
*cfg_comp
=
2234 g_ptr_array_index(cfg_components
, i
);
2237 switch (cfg_comp
->type
) {
2238 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2239 comp_cls
= find_source_component_class(
2240 cfg_comp
->plugin_name
->str
,
2241 cfg_comp
->comp_cls_name
->str
);
2243 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2244 comp_cls
= find_filter_component_class(
2245 cfg_comp
->plugin_name
->str
,
2246 cfg_comp
->comp_cls_name
->str
);
2248 case BT_COMPONENT_CLASS_TYPE_SINK
:
2249 comp_cls
= find_sink_component_class(
2250 cfg_comp
->plugin_name
->str
,
2251 cfg_comp
->comp_cls_name
->str
);
2258 BT_CLI_LOGE_APPEND_CAUSE(
2259 "Cannot find component class: plugin-name=\"%s\", "
2260 "comp-cls-name=\"%s\", comp-cls-type=%d",
2261 cfg_comp
->plugin_name
->str
,
2262 cfg_comp
->comp_cls_name
->str
,
2267 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_TRACE
);
2269 switch (cfg_comp
->type
) {
2270 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2271 ret
= bt_graph_add_source_component(ctx
->graph
,
2272 comp_cls
, cfg_comp
->instance_name
->str
,
2273 cfg_comp
->params
, cfg_comp
->log_level
,
2276 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2277 ret
= bt_graph_add_filter_component(ctx
->graph
,
2278 comp_cls
, cfg_comp
->instance_name
->str
,
2279 cfg_comp
->params
, cfg_comp
->log_level
,
2282 case BT_COMPONENT_CLASS_TYPE_SINK
:
2283 ret
= bt_graph_add_sink_component(ctx
->graph
,
2284 comp_cls
, cfg_comp
->instance_name
->str
,
2285 cfg_comp
->params
, cfg_comp
->log_level
,
2293 BT_CLI_LOGE_APPEND_CAUSE(
2294 "Cannot create component: plugin-name=\"%s\", "
2295 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2297 cfg_comp
->plugin_name
->str
,
2298 cfg_comp
->comp_cls_name
->str
,
2299 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2303 if (ctx
->stream_intersection_mode
&&
2304 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2305 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2307 BT_CLI_LOGE_APPEND_CAUSE(
2308 "Cannot determine stream intersection of trace.");
2313 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2314 comp
, cfg_comp
->instance_name
->str
);
2315 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2316 BT_ASSERT(quark
> 0);
2318 switch (cfg_comp
->type
) {
2319 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2320 g_hash_table_insert(ctx
->src_components
,
2321 GUINT_TO_POINTER(quark
), (void *) comp
);
2323 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2324 g_hash_table_insert(ctx
->flt_components
,
2325 GUINT_TO_POINTER(quark
), (void *) comp
);
2327 case BT_COMPONENT_CLASS_TYPE_SINK
:
2328 g_hash_table_insert(ctx
->sink_components
,
2329 GUINT_TO_POINTER(quark
), (void *) comp
);
2336 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2345 bt_object_put_ref(comp
);
2346 bt_object_put_ref(comp_cls
);
2351 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2356 * Make sure that, during this phase, our graph's "port added"
2357 * listener does not connect ports while we are creating the
2358 * components because we have a special, initial phase for
2361 ctx
->connect_ports
= false;
2363 ret
= cmd_run_ctx_create_components_from_config_components(
2364 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2370 ret
= cmd_run_ctx_create_components_from_config_components(
2371 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2377 ret
= cmd_run_ctx_create_components_from_config_components(
2378 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2388 typedef uint64_t (*output_port_count_func_t
)(const void *);
2389 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2390 const void *, uint64_t);
2393 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2394 void *comp
, output_port_count_func_t port_count_fn
,
2395 borrow_output_port_by_index_func_t port_by_index_fn
)
2401 count
= port_count_fn(comp
);
2403 for (i
= 0; i
< count
; i
++) {
2404 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2406 BT_ASSERT(upstream_port
);
2407 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2418 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2421 GHashTableIter iter
;
2422 gpointer g_name_quark
, g_comp
;
2424 ctx
->connect_ports
= true;
2425 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2427 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2428 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2429 (output_port_count_func_t
)
2430 bt_component_source_get_output_port_count
,
2431 (borrow_output_port_by_index_func_t
)
2432 bt_component_source_borrow_output_port_by_index_const
);
2438 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2440 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2441 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2442 (output_port_count_func_t
)
2443 bt_component_filter_get_output_port_count
,
2444 (borrow_output_port_by_index_func_t
)
2445 bt_component_filter_borrow_output_port_by_index_const
);
2456 enum bt_cmd_status
cmd_run(struct bt_config
*cfg
)
2458 enum bt_cmd_status cmd_status
;
2459 struct cmd_run_ctx ctx
= { 0 };
2461 /* Initialize the command's context and the graph object */
2462 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2463 BT_CLI_LOGE_APPEND_CAUSE(
2464 "Cannot initialize the command's context.");
2468 if (bt_interrupter_is_set(the_interrupter
)) {
2469 BT_CLI_LOGW_APPEND_CAUSE(
2470 "Interrupted by user before creating components.");
2474 BT_LOGI_STR("Creating components.");
2476 /* Create the requested component instances */
2477 if (cmd_run_ctx_create_components(&ctx
)) {
2478 BT_CLI_LOGE_APPEND_CAUSE("Cannot create components.");
2482 if (bt_interrupter_is_set(the_interrupter
)) {
2483 BT_CLI_LOGW_APPEND_CAUSE(
2484 "Interrupted by user before connecting components.");
2488 BT_LOGI_STR("Connecting components.");
2490 /* Connect the initially visible component ports */
2491 if (cmd_run_ctx_connect_ports(&ctx
)) {
2492 BT_CLI_LOGE_APPEND_CAUSE(
2493 "Cannot connect initial component ports.");
2497 BT_LOGI_STR("Running the graph.");
2501 bt_graph_run_status run_status
= bt_graph_run(ctx
.graph
);
2504 * Reset console in case something messed with console
2505 * codes during the graph's execution.
2507 printf("%s", bt_common_color_reset());
2509 fprintf(stderr
, "%s", bt_common_color_reset());
2510 BT_LOGT("bt_graph_run() returned: status=%s",
2511 bt_common_func_status_string(run_status
));
2513 switch (run_status
) {
2514 case BT_GRAPH_RUN_STATUS_OK
:
2515 cmd_status
= BT_CMD_STATUS_OK
;
2517 case BT_GRAPH_RUN_STATUS_AGAIN
:
2518 if (bt_interrupter_is_set(the_interrupter
)) {
2520 * The graph was interrupted by a SIGINT.
2522 cmd_status
= BT_CMD_STATUS_INTERRUPTED
;
2526 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2527 BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: "
2529 cfg
->cmd_data
.run
.retry_duration_us
);
2531 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2532 if (bt_interrupter_is_set(the_interrupter
)) {
2533 cmd_status
= BT_CMD_STATUS_INTERRUPTED
;
2540 if (bt_interrupter_is_set(the_interrupter
)) {
2541 cmd_status
= BT_CMD_STATUS_INTERRUPTED
;
2545 BT_CLI_LOGE_APPEND_CAUSE(
2546 "Graph failed to complete successfully");
2551 cmd_status
= BT_CMD_STATUS_ERROR
;
2554 cmd_run_ctx_destroy(&ctx
);
2559 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2561 const char *env_clash
;
2563 if (!cfg
->command_name
) {
2567 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2568 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2572 if (g_file_test(cfg
->command_name
,
2573 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2574 _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION
, __FILE__
, __LINE__
,
2575 BT_LOG_WARNING
, BT_LOG_TAG
,
2576 "The `%s` command was executed. "
2577 "If you meant to convert a trace located in "
2578 "the local `%s` directory, please use:\n\n"
2579 " babeltrace2 convert %s [OPTIONS]",
2580 cfg
->command_name
, cfg
->command_name
,
2586 void init_log_level(void)
2588 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2592 void print_error_causes(void)
2594 const bt_error
*error
= bt_current_thread_take_error();
2595 unsigned int columns
;
2596 gchar
*error_str
= NULL
;
2598 if (!error
|| bt_error_get_cause_count(error
) == 0) {
2599 fprintf(stderr
, "%s%sUnknown command-line error.%s\n",
2600 bt_common_color_bold(), bt_common_color_fg_bright_red(),
2601 bt_common_color_reset());
2605 /* Try to get terminal width to fold the error cause messages */
2606 if (bt_common_get_term_size(&columns
, NULL
) < 0) {
2607 /* Width not found: default to 80 */
2612 * This helps visually separate the error causes from the last
2613 * logging statement.
2615 fputc('\n', stderr
);
2617 error_str
= format_bt_error(error
, columns
, bt_cli_log_level
,
2618 BT_COMMON_COLOR_WHEN_AUTO
);
2619 BT_ASSERT(error_str
);
2621 fprintf(stderr
, "%s\n", error_str
);
2625 bt_error_release(error
);
2631 int main(int argc
, const char **argv
)
2634 enum bt_cmd_status cmd_status
;
2635 struct bt_config
*cfg
= NULL
;
2638 set_signal_handler();
2639 init_loaded_plugins();
2641 BT_ASSERT(!the_interrupter
);
2642 the_interrupter
= bt_interrupter_create();
2643 if (!the_interrupter
) {
2644 BT_CLI_LOGE_APPEND_CAUSE("Failed to create an interrupter object.");
2649 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
,
2653 /* Quit without errors; typically usage/version */
2655 BT_LOGI_STR("Quitting without errors.");
2660 BT_CLI_LOGE_APPEND_CAUSE(
2661 "Command-line error: retcode=%d", retcode
);
2666 BT_CLI_LOGE_APPEND_CAUSE(
2667 "Failed to create a valid Babeltrace CLI configuration.");
2674 if (cfg
->command_needs_plugins
) {
2675 ret
= require_loaded_plugins(cfg
->plugin_paths
);
2677 BT_CLI_LOGE_APPEND_CAUSE(
2678 "Failed to load plugins: ret=%d", ret
);
2684 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2685 cfg
->command
, cfg
->command_name
);
2687 switch (cfg
->command
) {
2688 case BT_CONFIG_COMMAND_RUN
:
2689 cmd_status
= cmd_run(cfg
);
2691 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2692 cmd_status
= cmd_list_plugins(cfg
);
2694 case BT_CONFIG_COMMAND_HELP
:
2695 cmd_status
= cmd_help(cfg
);
2697 case BT_CONFIG_COMMAND_QUERY
:
2698 cmd_status
= cmd_query(cfg
);
2700 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2701 cmd_status
= cmd_print_ctf_metadata(cfg
);
2703 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2704 cmd_status
= cmd_print_lttng_live_sessions(cfg
);
2707 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2711 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", command-status=\"%s\"",
2712 cfg
->command
, cfg
->command_name
, bt_cmd_status_string(cmd_status
));
2713 warn_command_name_and_directory_clash(cfg
);
2715 switch (cmd_status
) {
2716 case BT_CMD_STATUS_OK
:
2719 case BT_CMD_STATUS_ERROR
:
2722 case BT_CMD_STATUS_INTERRUPTED
:
2726 BT_LOGF("Invalid command status: cmd-status=%d", cmd_status
);
2732 print_error_causes();
2735 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2736 fini_loaded_plugins();
2737 bt_interrupter_put_ref(the_interrupter
);
2740 * Clear current thread's error in case there is one to avoid a
2743 bt_current_thread_clear_error();