2 * SPDX-License-Identifier: MIT
4 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 #define BT_LOG_TAG "CLI"
10 #include <babeltrace2/babeltrace.h>
11 #include "common/common.h"
12 #include "string-format/format-error.h"
13 #include "string-format/format-plugin-comp-cls-name.h"
22 #include "babeltrace2-cfg.h"
23 #include "babeltrace2-cfg-cli-args.h"
24 #include "babeltrace2-cfg-cli-args-default.h"
25 #include "babeltrace2-log-level.h"
26 #include "babeltrace2-plugins.h"
27 #include "babeltrace2-query.h"
29 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
30 #define NSEC_PER_SEC 1000000000LL
34 BT_CMD_STATUS_ERROR
= -1,
35 BT_CMD_STATUS_INTERRUPTED
= -2,
39 const char *bt_cmd_status_string(enum bt_cmd_status cmd_status
)
42 case BT_CMD_STATUS_OK
:
44 case BT_CMD_STATUS_ERROR
:
46 case BT_CMD_STATUS_INTERRUPTED
:
53 /* Application's interrupter (owned by this) */
54 static bt_interrupter
*the_interrupter
;
61 BOOL WINAPI
signal_handler(DWORD signal
) {
62 if (the_interrupter
) {
63 bt_interrupter_set(the_interrupter
);
70 void set_signal_handler(void)
72 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
73 BT_LOGE("Failed to set the Ctrl+C handler.");
77 #else /* __MINGW32__ */
80 void signal_handler(int signum
)
82 if (signum
!= SIGINT
) {
86 if (the_interrupter
) {
87 bt_interrupter_set(the_interrupter
);
92 void set_signal_handler(void)
94 struct sigaction new_action
, old_action
;
96 new_action
.sa_handler
= signal_handler
;
97 sigemptyset(&new_action
.sa_mask
);
98 new_action
.sa_flags
= 0;
99 sigaction(SIGINT
, NULL
, &old_action
);
101 if (old_action
.sa_handler
!= SIG_IGN
) {
102 sigaction(SIGINT
, &new_action
, NULL
);
106 #endif /* __MINGW32__ */
109 int query(struct bt_config
*cfg
, const bt_component_class
*comp_cls
,
110 const char *obj
, const bt_value
*params
,
111 const bt_value
**user_result
, const char **fail_reason
)
113 return cli_query(comp_cls
, obj
, params
, cfg
->log_level
,
114 the_interrupter
, user_result
, fail_reason
);
117 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
118 const bt_plugin
*, const char *);
121 const void *find_component_class_from_plugin(const char *plugin_name
,
122 const char *comp_class_name
,
123 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
125 const void *comp_class
= NULL
;
126 const bt_plugin
*plugin
;
128 BT_LOGI("Finding component class: plugin-name=\"%s\", "
129 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
131 plugin
= borrow_loaded_plugin_by_name(plugin_name
);
136 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
137 bt_object_get_ref(comp_class
);
141 BT_LOGI("Found component class: plugin-name=\"%s\", "
142 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
144 BT_LOGI("Cannot find source component class: "
145 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
146 plugin_name
, comp_class_name
);
153 const bt_component_class_source
*find_source_component_class(
154 const char *plugin_name
, const char *comp_class_name
)
156 return (const void *) find_component_class_from_plugin(
157 plugin_name
, comp_class_name
,
158 (plugin_borrow_comp_cls_func_t
)
159 bt_plugin_borrow_source_component_class_by_name_const
);
163 const bt_component_class_filter
*find_filter_component_class(
164 const char *plugin_name
, const char *comp_class_name
)
166 return (const void *) find_component_class_from_plugin(
167 plugin_name
, comp_class_name
,
168 (plugin_borrow_comp_cls_func_t
)
169 bt_plugin_borrow_filter_component_class_by_name_const
);
173 const bt_component_class_sink
*find_sink_component_class(
174 const char *plugin_name
, const char *comp_class_name
)
176 return (const void *) find_component_class_from_plugin(plugin_name
,
178 (plugin_borrow_comp_cls_func_t
)
179 bt_plugin_borrow_sink_component_class_by_name_const
);
183 const bt_component_class
*find_component_class(const char *plugin_name
,
184 const char *comp_class_name
,
185 bt_component_class_type comp_class_type
)
187 const bt_component_class
*comp_cls
= NULL
;
189 switch (comp_class_type
) {
190 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
191 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
193 case BT_COMPONENT_CLASS_TYPE_FILTER
:
194 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
196 case BT_COMPONENT_CLASS_TYPE_SINK
:
197 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
207 void print_indent(FILE *fp
, size_t indent
)
211 for (i
= 0; i
< indent
; i
++) {
217 void print_value(FILE *, const bt_value
*, size_t);
220 void print_value_rec(FILE *, const bt_value
*, size_t);
223 void print_map_value(const char *key
, const bt_value
*object
, FILE *fp
,
226 print_indent(fp
, indent
);
227 fprintf(fp
, "%s: ", key
);
230 if (bt_value_is_array(object
) &&
231 bt_value_array_is_empty(object
)) {
232 fprintf(fp
, "[ ]\n");
236 if (bt_value_is_map(object
) &&
237 bt_value_map_is_empty(object
)) {
238 fprintf(fp
, "{ }\n");
242 if (bt_value_is_array(object
) ||
243 bt_value_is_map(object
)) {
247 print_value_rec(fp
, object
, indent
+ 2);
254 bt_value_map_foreach_entry_const_func_status
collect_map_keys(
255 const char *key
, const bt_value
*object
, void *data
)
257 GPtrArray
*map_keys
= data
;
259 g_ptr_array_add(map_keys
, (gpointer
*) key
);
261 return BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK
;
265 gint
g_ptr_array_sort_strings(gconstpointer a
, gconstpointer b
) {
266 const char *s1
= *((const char **) a
);
267 const char *s2
= *((const char **) b
);
269 return g_strcmp0(s1
, s2
);
273 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
280 GPtrArray
*map_keys
= NULL
;
284 switch (bt_value_get_type(value
)) {
285 case BT_VALUE_TYPE_NULL
:
286 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
287 bt_common_color_reset());
289 case BT_VALUE_TYPE_BOOL
:
290 bool_val
= bt_value_bool_get(value
);
291 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
292 bt_common_color_fg_bright_cyan(), bool_val
? "yes" : "no",
293 bt_common_color_reset());
295 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
296 uint_val
= bt_value_integer_unsigned_get(value
);
297 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
298 bt_common_color_fg_bright_red(), uint_val
,
299 bt_common_color_reset());
301 case BT_VALUE_TYPE_SIGNED_INTEGER
:
302 int_val
= bt_value_integer_signed_get(value
);
303 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
304 bt_common_color_fg_bright_red(), int_val
,
305 bt_common_color_reset());
307 case BT_VALUE_TYPE_REAL
:
308 dbl_val
= bt_value_real_get(value
);
309 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
310 bt_common_color_fg_bright_red(), dbl_val
,
311 bt_common_color_reset());
313 case BT_VALUE_TYPE_STRING
:
314 str_val
= bt_value_string_get(value
);
315 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
316 bt_common_color_fg_bright_green(), str_val
,
317 bt_common_color_reset());
319 case BT_VALUE_TYPE_ARRAY
:
322 size
= bt_value_array_get_length(value
);
324 print_indent(fp
, indent
);
325 fprintf(fp
, "[ ]\n");
329 for (i
= 0; i
< size
; i
++) {
330 const bt_value
*element
=
331 bt_value_array_borrow_element_by_index_const(
334 print_indent(fp
, indent
);
337 if (bt_value_is_array(element
) &&
338 bt_value_array_is_empty(element
)) {
339 fprintf(fp
, "[ ]\n");
343 if (bt_value_is_map(element
) &&
344 bt_value_map_is_empty(element
)) {
345 fprintf(fp
, "{ }\n");
349 if (bt_value_is_array(element
) ||
350 bt_value_is_map(element
)) {
354 print_value_rec(fp
, element
, indent
+ 2);
358 case BT_VALUE_TYPE_MAP
:
361 bt_value_map_foreach_entry_const_status foreach_status
;
363 if (bt_value_map_is_empty(value
)) {
364 print_indent(fp
, indent
);
365 fprintf(fp
, "{ }\n");
369 map_keys
= g_ptr_array_new();
371 BT_CLI_LOGE_APPEND_CAUSE("Failed to allocated on GPtrArray.");
376 * We want to print the map entries in a stable order. Collect
377 * all the map's keys in a GPtrArray, sort it, then print the
378 * entries in that order.
380 foreach_status
= bt_value_map_foreach_entry_const(value
,
381 collect_map_keys
, map_keys
);
382 if (foreach_status
!= BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK
) {
383 BT_CLI_LOGE_APPEND_CAUSE("Failed to iterator on map value.");
387 g_ptr_array_sort(map_keys
, g_ptr_array_sort_strings
);
389 for (i
= 0; i
< map_keys
->len
; i
++) {
390 const char *map_key
= g_ptr_array_index(map_keys
, i
);
391 const bt_value
*map_value
;
393 map_value
= bt_value_map_borrow_entry_value_const(value
, map_key
);
394 BT_ASSERT(map_value
);
396 print_map_value(map_key
, map_value
, fp
, indent
);
409 g_ptr_array_free(map_keys
, TRUE
);
414 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
416 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
417 print_indent(fp
, indent
);
420 print_value_rec(fp
, value
, indent
);
424 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
428 comp_cls_str
= format_plugin_comp_cls_opt(
429 bt_config_component
->plugin_name
->str
,
430 bt_config_component
->comp_cls_name
->str
,
431 bt_config_component
->type
,
432 BT_COMMON_COLOR_WHEN_AUTO
);
433 BT_ASSERT(comp_cls_str
);
435 fprintf(stderr
, " %s:\n", comp_cls_str
);
437 if (bt_config_component
->instance_name
->len
> 0) {
438 fprintf(stderr
, " Name: %s\n",
439 bt_config_component
->instance_name
->str
);
442 fprintf(stderr
, " Parameters:\n");
443 print_value(stderr
, bt_config_component
->params
, 8);
445 g_free(comp_cls_str
);
449 void print_bt_config_components(GPtrArray
*array
)
453 for (i
= 0; i
< array
->len
; i
++) {
454 struct bt_config_component
*cfg_component
=
455 bt_config_get_component(array
, i
);
456 print_bt_config_component(cfg_component
);
457 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
462 void print_plugin_paths(const bt_value
*plugin_paths
)
464 fprintf(stderr
, " Plugin paths:\n");
465 print_value(stderr
, plugin_paths
, 4);
469 void print_cfg_run(struct bt_config
*cfg
)
473 print_plugin_paths(cfg
->plugin_paths
);
474 fprintf(stderr
, " Source component instances:\n");
475 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
477 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
478 fprintf(stderr
, " Filter component instances:\n");
479 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
482 fprintf(stderr
, " Sink component instances:\n");
483 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
484 fprintf(stderr
, " Connections:\n");
486 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
487 struct bt_config_connection
*cfg_connection
=
488 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
491 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
492 cfg_connection
->upstream_comp_name
->str
,
493 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
494 cfg_connection
->upstream_port_glob
->str
,
495 cfg_connection
->downstream_comp_name
->str
,
496 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
497 cfg_connection
->downstream_port_glob
->str
);
502 void print_cfg_list_plugins(struct bt_config
*cfg
)
504 print_plugin_paths(cfg
->plugin_paths
);
508 void print_cfg_help(struct bt_config
*cfg
)
510 print_plugin_paths(cfg
->plugin_paths
);
514 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
516 print_plugin_paths(cfg
->plugin_paths
);
517 fprintf(stderr
, " Path: %s\n",
518 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
522 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
524 print_plugin_paths(cfg
->plugin_paths
);
525 fprintf(stderr
, " URL: %s\n",
526 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
530 void print_cfg_query(struct bt_config
*cfg
)
532 print_plugin_paths(cfg
->plugin_paths
);
533 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
534 fprintf(stderr
, " Component class:\n");
535 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
539 void print_cfg(struct bt_config
*cfg
)
541 if (!BT_LOG_ON_INFO
) {
545 BT_LOGI_STR("CLI configuration:");
547 switch (cfg
->command
) {
548 case BT_CONFIG_COMMAND_RUN
:
551 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
552 print_cfg_list_plugins(cfg
);
554 case BT_CONFIG_COMMAND_HELP
:
557 case BT_CONFIG_COMMAND_QUERY
:
558 print_cfg_query(cfg
);
560 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
561 print_cfg_print_ctf_metadata(cfg
);
563 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
564 print_cfg_print_lttng_live_sessions(cfg
);
572 void print_plugin_info(const bt_plugin
*plugin
)
574 unsigned int major
, minor
, patch
;
576 bt_property_availability version_avail
;
577 const char *plugin_name
;
581 const char *plugin_description
;
583 plugin_name
= bt_plugin_get_name(plugin
);
584 path
= bt_plugin_get_path(plugin
);
585 author
= bt_plugin_get_author(plugin
);
586 license
= bt_plugin_get_license(plugin
);
587 plugin_description
= bt_plugin_get_description(plugin
);
588 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
590 printf("%s%s%s%s:\n", bt_common_color_bold(),
591 bt_common_color_fg_bright_blue(), plugin_name
,
592 bt_common_color_reset());
594 printf(" %sPath%s: %s\n", bt_common_color_bold(),
595 bt_common_color_reset(), path
);
600 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
601 printf(" %sVersion%s: %u.%u.%u",
602 bt_common_color_bold(), bt_common_color_reset(),
603 major
, minor
, patch
);
612 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
613 bt_common_color_reset(),
614 plugin_description
? plugin_description
: "(None)");
615 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
616 bt_common_color_reset(), author
? author
: "(Unknown)");
617 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
618 bt_common_color_reset(),
619 license
? license
: "(Unknown)");
623 enum bt_cmd_status
cmd_query(struct bt_config
*cfg
)
626 enum bt_cmd_status cmd_status
;
627 const bt_component_class
*comp_cls
= NULL
;
628 const bt_value
*results
= NULL
;
629 const char *fail_reason
= NULL
;
631 comp_cls
= find_component_class(
632 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
633 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
634 cfg
->cmd_data
.query
.cfg_component
->type
);
636 BT_CLI_LOGE_APPEND_CAUSE(
637 "Cannot find component class: plugin-name=\"%s\", "
638 "comp-cls-name=\"%s\", comp-cls-type=%d",
639 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
640 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
641 cfg
->cmd_data
.query
.cfg_component
->type
);
645 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
646 cfg
->cmd_data
.query
.cfg_component
->params
,
647 &results
, &fail_reason
);
649 BT_CLI_LOGE_APPEND_CAUSE(
650 "Failed to query component class: %s: plugin-name=\"%s\", "
651 "comp-cls-name=\"%s\", comp-cls-type=%d "
652 "object=\"%s\"", fail_reason
,
653 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
654 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
655 cfg
->cmd_data
.query
.cfg_component
->type
,
656 cfg
->cmd_data
.query
.object
->str
);
660 print_value(stdout
, results
, 0);
661 cmd_status
= BT_CMD_STATUS_OK
;
665 cmd_status
= BT_CMD_STATUS_ERROR
;
668 bt_component_class_put_ref(comp_cls
);
669 bt_value_put_ref(results
);
674 void print_component_class_help(const char *plugin_name
,
675 const bt_component_class
*comp_cls
)
677 const char *comp_class_name
=
678 bt_component_class_get_name(comp_cls
);
679 const char *comp_class_description
=
680 bt_component_class_get_description(comp_cls
);
681 const char *comp_class_help
=
682 bt_component_class_get_help(comp_cls
);
683 bt_component_class_type type
=
684 bt_component_class_get_type(comp_cls
);
687 comp_cls_str
= format_plugin_comp_cls_opt(plugin_name
, comp_class_name
,
688 type
, BT_COMMON_COLOR_WHEN_AUTO
);
689 BT_ASSERT(comp_cls_str
);
691 printf("%s\n", comp_cls_str
);
692 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
693 bt_common_color_reset(),
694 comp_class_description
? comp_class_description
: "(None)");
696 if (comp_class_help
) {
697 printf("\n%s\n", comp_class_help
);
700 g_free(comp_cls_str
);
704 enum bt_cmd_status
cmd_help(struct bt_config
*cfg
)
706 enum bt_cmd_status cmd_status
;
707 const bt_plugin
*plugin
= NULL
;
708 const bt_component_class
*needed_comp_cls
= NULL
;
710 plugin
= borrow_loaded_plugin_by_name(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
712 BT_CLI_LOGE_APPEND_CAUSE(
713 "Cannot find plugin: plugin-name=\"%s\"",
714 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
718 print_plugin_info(plugin
);
719 printf(" %sSource component classes%s: %d\n",
720 bt_common_color_bold(),
721 bt_common_color_reset(),
722 (int) bt_plugin_get_source_component_class_count(plugin
));
723 printf(" %sFilter component classes%s: %d\n",
724 bt_common_color_bold(),
725 bt_common_color_reset(),
726 (int) bt_plugin_get_filter_component_class_count(plugin
));
727 printf(" %sSink component classes%s: %d\n",
728 bt_common_color_bold(),
729 bt_common_color_reset(),
730 (int) bt_plugin_get_sink_component_class_count(plugin
));
732 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
733 /* Plugin help only */
734 cmd_status
= BT_CMD_STATUS_OK
;
738 needed_comp_cls
= find_component_class(
739 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
740 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
741 cfg
->cmd_data
.help
.cfg_component
->type
);
742 if (!needed_comp_cls
) {
743 BT_CLI_LOGE_APPEND_CAUSE(
744 "Cannot find component class: plugin-name=\"%s\", "
745 "comp-cls-name=\"%s\", comp-cls-type=%d",
746 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
747 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
748 cfg
->cmd_data
.help
.cfg_component
->type
);
753 print_component_class_help(
754 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
756 cmd_status
= BT_CMD_STATUS_OK
;
760 cmd_status
= BT_CMD_STATUS_ERROR
;
763 bt_component_class_put_ref(needed_comp_cls
);
767 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
769 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
773 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
774 const char *cc_type_name
, uint64_t count
,
775 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
776 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
779 gchar
*comp_cls_str
= NULL
;
782 printf(" %s%s component classes%s: (none)\n",
783 bt_common_color_bold(),
785 bt_common_color_reset());
788 printf(" %s%s component classes%s:\n",
789 bt_common_color_bold(),
791 bt_common_color_reset());
794 for (i
= 0; i
< count
; i
++) {
795 const bt_component_class
*comp_class
=
796 spec_comp_cls_borrow_comp_cls_func(
797 borrow_comp_cls_by_index_func(plugin
, i
));
798 const char *comp_class_name
=
799 bt_component_class_get_name(comp_class
);
800 const char *comp_class_description
=
801 bt_component_class_get_description(comp_class
);
802 bt_component_class_type type
=
803 bt_component_class_get_type(comp_class
);
805 g_free(comp_cls_str
);
806 comp_cls_str
= format_plugin_comp_cls_opt(
807 bt_plugin_get_name(plugin
), comp_class_name
, type
,
808 BT_COMMON_COLOR_WHEN_AUTO
);
809 printf(" %s", comp_cls_str
);
811 if (comp_class_description
) {
812 printf(": %s", comp_class_description
);
819 g_free(comp_cls_str
);
823 enum bt_cmd_status
cmd_list_plugins(struct bt_config
*cfg
)
825 int plugins_count
, component_classes_count
= 0, i
;
827 printf("From the following plugin paths:\n\n");
828 print_value(stdout
, cfg
->plugin_paths
, 2);
830 plugins_count
= get_loaded_plugins_count();
831 if (plugins_count
== 0) {
832 printf("No plugins found.\n");
836 for (i
= 0; i
< plugins_count
; i
++) {
837 const bt_plugin
*plugin
= borrow_loaded_plugin_by_index(i
);
839 component_classes_count
+=
840 bt_plugin_get_source_component_class_count(plugin
) +
841 bt_plugin_get_filter_component_class_count(plugin
) +
842 bt_plugin_get_sink_component_class_count(plugin
);
845 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
846 bt_common_color_bold(),
847 component_classes_count
,
848 bt_common_color_reset(),
849 bt_common_color_bold(),
851 bt_common_color_reset());
853 for (i
= 0; i
< plugins_count
; i
++) {
854 const bt_plugin
*plugin
= borrow_loaded_plugin_by_index(i
);
857 print_plugin_info(plugin
);
858 cmd_list_plugins_print_component_classes(plugin
, "Source",
859 bt_plugin_get_source_component_class_count(plugin
),
860 (plugin_borrow_comp_cls_by_index_func_t
)
861 bt_plugin_borrow_source_component_class_by_index_const
,
862 (spec_comp_cls_borrow_comp_cls_func_t
)
863 bt_component_class_source_as_component_class
);
864 cmd_list_plugins_print_component_classes(plugin
, "Filter",
865 bt_plugin_get_filter_component_class_count(plugin
),
866 (plugin_borrow_comp_cls_by_index_func_t
)
867 bt_plugin_borrow_filter_component_class_by_index_const
,
868 (spec_comp_cls_borrow_comp_cls_func_t
)
869 bt_component_class_filter_as_component_class
);
870 cmd_list_plugins_print_component_classes(plugin
, "Sink",
871 bt_plugin_get_sink_component_class_count(plugin
),
872 (plugin_borrow_comp_cls_by_index_func_t
)
873 bt_plugin_borrow_sink_component_class_by_index_const
,
874 (spec_comp_cls_borrow_comp_cls_func_t
)
875 bt_component_class_sink_as_component_class
);
879 return BT_CMD_STATUS_OK
;
883 enum bt_cmd_status
cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
886 enum bt_cmd_status cmd_status
;
887 const bt_component_class
*comp_cls
= NULL
;
888 const bt_value
*results
= NULL
;
889 bt_value
*params
= NULL
;
890 const bt_value
*map
= NULL
;
891 const bt_value
*v
= NULL
;
892 static const char * const plugin_name
= "ctf";
893 static const char * const comp_cls_name
= "lttng-live";
894 static const bt_component_class_type comp_cls_type
=
895 BT_COMPONENT_CLASS_TYPE_SOURCE
;
896 uint64_t array_size
, i
;
897 const char *fail_reason
= NULL
;
898 FILE *out_stream
= stdout
;
900 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
901 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
904 BT_CLI_LOGE_APPEND_CAUSE(
905 "Cannot find component class: plugin-name=\"%s\", "
906 "comp-cls-name=\"%s\", comp-cls-type=%d",
907 plugin_name
, comp_cls_name
,
908 BT_COMPONENT_CLASS_TYPE_SOURCE
);
912 params
= bt_value_map_create();
917 ret
= bt_value_map_insert_string_entry(params
, "url",
918 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
923 ret
= query(cfg
, comp_cls
, "sessions", params
,
924 &results
, &fail_reason
);
926 BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s",
933 if (!bt_value_is_array(results
)) {
934 BT_CLI_LOGE_APPEND_CAUSE(
935 "Expecting an array for LTTng live `sessions` query.");
939 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
941 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
944 BT_LOGE_ERRNO("Cannot open file for writing",
946 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
947 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
949 "Cannot open file for writing: path=\"%s\"",
950 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
955 array_size
= bt_value_array_get_length(results
);
956 for (i
= 0; i
< array_size
; i
++) {
957 const char *url_text
;
958 int64_t timer_us
, streams
, clients
;
960 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
961 if (!bt_value_is_map(map
)) {
962 BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type.");
966 v
= bt_value_map_borrow_entry_value_const(map
, "url");
968 BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry.");
971 url_text
= bt_value_string_get(v
);
972 fprintf(out_stream
, "%s", url_text
);
973 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
975 BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry.");
978 timer_us
= bt_value_integer_unsigned_get(v
);
979 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
980 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
982 BT_CLI_LOGE_APPEND_CAUSE(
983 "Missing `stream-count` entry.");
986 streams
= bt_value_integer_unsigned_get(v
);
987 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
988 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
990 BT_CLI_LOGE_APPEND_CAUSE(
991 "Missing `client-count` entry.");
994 clients
= bt_value_integer_unsigned_get(v
);
995 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
998 cmd_status
= BT_CMD_STATUS_OK
;
1002 cmd_status
= BT_CMD_STATUS_ERROR
;
1005 bt_value_put_ref(results
);
1006 bt_value_put_ref(params
);
1007 bt_component_class_put_ref(comp_cls
);
1009 if (out_stream
&& out_stream
!= stdout
) {
1010 int fclose_ret
= fclose(out_stream
);
1013 BT_LOGE_ERRNO("Cannot close file stream",
1015 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1023 enum bt_cmd_status
cmd_print_ctf_metadata(struct bt_config
*cfg
)
1026 enum bt_cmd_status cmd_status
;
1027 const bt_component_class
*comp_cls
= NULL
;
1028 const bt_value
*results
= NULL
;
1029 bt_value
*params
= NULL
;
1030 const bt_value
*metadata_text_value
= NULL
;
1031 const char *metadata_text
= NULL
;
1032 static const char * const plugin_name
= "ctf";
1033 static const char * const comp_cls_name
= "fs";
1034 static const bt_component_class_type comp_cls_type
=
1035 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1036 const char *fail_reason
= NULL
;
1037 FILE *out_stream
= stdout
;
1039 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1040 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1043 BT_CLI_LOGE_APPEND_CAUSE(
1044 "Cannot find component class: plugin-name=\"%s\", "
1045 "comp-cls-name=\"%s\", comp-cls-type=%d",
1046 plugin_name
, comp_cls_name
,
1047 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1051 params
= bt_value_map_create();
1056 ret
= bt_value_map_insert_string_entry(params
, "path",
1057 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1062 ret
= query(cfg
, comp_cls
, "metadata-info",
1063 params
, &results
, &fail_reason
);
1065 BT_CLI_LOGE_APPEND_CAUSE(
1066 "Failed to query `metadata-info` object: %s", fail_reason
);
1070 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1072 if (!metadata_text_value
) {
1073 BT_CLI_LOGE_APPEND_CAUSE(
1074 "Cannot find `text` string value in the resulting metadata info object.");
1078 metadata_text
= bt_value_string_get(metadata_text_value
);
1080 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1082 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1085 BT_LOGE_ERRNO("Cannot open file for writing",
1087 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1088 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1090 "Cannot open file for writing: path=\"%s\"",
1091 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1096 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1098 BT_CLI_LOGE_APPEND_CAUSE(
1099 "Cannot write whole metadata text to output stream: "
1104 cmd_status
= BT_CMD_STATUS_OK
;
1108 cmd_status
= BT_CMD_STATUS_ERROR
;
1111 bt_value_put_ref(results
);
1112 bt_value_put_ref(params
);
1113 bt_component_class_put_ref(comp_cls
);
1115 if (out_stream
&& out_stream
!= stdout
) {
1116 int fclose_ret
= fclose(out_stream
);
1119 BT_LOGE_ERRNO("Cannot close file stream",
1121 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1129 char *instance_name
;
1133 struct trace_range
{
1134 uint64_t intersection_range_begin_ns
;
1135 uint64_t intersection_range_end_ns
;
1139 guint
port_id_hash(gconstpointer v
)
1141 const struct port_id
*id
= v
;
1143 BT_ASSERT(id
->instance_name
);
1144 BT_ASSERT(id
->port_name
);
1146 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1150 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1152 const struct port_id
*id1
= v1
;
1153 const struct port_id
*id2
= v2
;
1155 return strcmp(id1
->instance_name
, id2
->instance_name
) == 0 &&
1156 strcmp(id1
->port_name
, id2
->port_name
) == 0;
1160 void port_id_destroy(gpointer data
)
1162 struct port_id
*id
= data
;
1164 free(id
->instance_name
);
1165 free(id
->port_name
);
1170 void trace_range_destroy(gpointer data
)
1175 struct cmd_run_ctx
{
1177 GHashTable
*src_components
;
1180 GHashTable
*flt_components
;
1183 GHashTable
*sink_components
;
1189 struct bt_config
*cfg
;
1193 bool stream_intersection_mode
;
1196 * Association of struct port_id -> struct trace_range.
1198 GHashTable
*intersections
;
1201 /* Returns a timestamp of the form "(-)s.ns" */
1203 char *s_from_ns(int64_t ns
)
1208 int64_t ts_sec_abs
, ts_nsec_abs
;
1209 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1210 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1212 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1213 is_negative
= false;
1214 ts_sec_abs
= ts_sec
;
1215 ts_nsec_abs
= ts_nsec
;
1216 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1217 is_negative
= false;
1218 ts_sec_abs
= ts_sec
- 1;
1219 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1220 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1222 ts_sec_abs
= ts_sec
;
1223 ts_nsec_abs
= -ts_nsec
;
1224 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1226 ts_sec_abs
= -(ts_sec
+ 1);
1227 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1228 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1230 ts_sec_abs
= -ts_sec
;
1231 ts_nsec_abs
= ts_nsec
;
1232 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1234 ts_sec_abs
= -ts_sec
;
1235 ts_nsec_abs
= -ts_nsec
;
1238 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1239 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1247 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1248 struct cmd_run_ctx
*ctx
,
1249 const bt_component
*upstream_comp
,
1250 const bt_port_output
*out_upstream_port
,
1251 struct bt_config_connection
*cfg_conn
)
1253 typedef uint64_t (*input_port_count_func_t
)(void *);
1254 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1255 const void *, uint64_t);
1256 const bt_port
*upstream_port
=
1257 bt_port_output_as_port_const(out_upstream_port
);
1260 GQuark downstreamp_comp_name_quark
;
1261 void *downstream_comp
;
1262 uint64_t downstream_port_count
;
1264 input_port_count_func_t port_count_fn
;
1265 borrow_input_port_by_index_func_t port_by_index_fn
;
1266 bt_graph_connect_ports_status connect_ports_status
=
1267 BT_GRAPH_CONNECT_PORTS_STATUS_OK
;
1268 bool insert_trimmer
= false;
1269 bt_value
*trimmer_params
= NULL
;
1270 char *intersection_begin
= NULL
;
1271 char *intersection_end
= NULL
;
1272 const bt_component_filter
*trimmer
= NULL
;
1273 const bt_component_class_filter
*trimmer_class
= NULL
;
1274 const bt_port_input
*trimmer_input
= NULL
;
1275 const bt_port_output
*trimmer_output
= NULL
;
1277 if (ctx
->intersections
&&
1278 bt_component_get_class_type(upstream_comp
) ==
1279 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1280 struct trace_range
*range
;
1281 struct port_id port_id
= {
1282 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1283 .port_name
= (char *) bt_port_get_name(upstream_port
)
1286 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1290 range
= (struct trace_range
*) g_hash_table_lookup(
1291 ctx
->intersections
, &port_id
);
1293 bt_value_map_insert_entry_status insert_status
;
1295 intersection_begin
= s_from_ns(
1296 range
->intersection_range_begin_ns
);
1297 intersection_end
= s_from_ns(
1298 range
->intersection_range_end_ns
);
1299 if (!intersection_begin
|| !intersection_end
) {
1300 BT_CLI_LOGE_APPEND_CAUSE(
1301 "Cannot create trimmer argument timestamp string.");
1305 insert_trimmer
= true;
1306 trimmer_params
= bt_value_map_create();
1307 if (!trimmer_params
) {
1311 insert_status
= bt_value_map_insert_string_entry(
1312 trimmer_params
, "begin", intersection_begin
);
1313 if (insert_status
< 0) {
1316 insert_status
= bt_value_map_insert_string_entry(
1318 "end", intersection_end
);
1319 if (insert_status
< 0) {
1324 trimmer_class
= find_filter_component_class("utils", "trimmer");
1325 if (!trimmer_class
) {
1330 BT_LOGI("Connecting upstream port to the next available downstream port: "
1331 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1332 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1333 upstream_port
, bt_port_get_name(upstream_port
),
1334 cfg_conn
->downstream_comp_name
->str
,
1335 cfg_conn
->arg
->str
);
1336 downstreamp_comp_name_quark
= g_quark_from_string(
1337 cfg_conn
->downstream_comp_name
->str
);
1338 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1339 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1340 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1341 port_count_fn
= (input_port_count_func_t
)
1342 bt_component_filter_get_input_port_count
;
1343 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1344 bt_component_filter_borrow_input_port_by_index_const
;
1346 if (!downstream_comp
) {
1347 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1348 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1349 port_count_fn
= (input_port_count_func_t
)
1350 bt_component_sink_get_input_port_count
;
1351 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1352 bt_component_sink_borrow_input_port_by_index_const
;
1355 if (!downstream_comp
) {
1356 BT_CLI_LOGE_APPEND_CAUSE("Cannot find downstream component: "
1357 "comp-name=\"%s\", conn-arg=\"%s\"",
1358 cfg_conn
->downstream_comp_name
->str
,
1359 cfg_conn
->arg
->str
);
1363 downstream_port_count
= port_count_fn(downstream_comp
);
1365 for (i
= 0; i
< downstream_port_count
; i
++) {
1366 const bt_port_input
*in_downstream_port
=
1367 port_by_index_fn(downstream_comp
, i
);
1368 const bt_port
*downstream_port
=
1369 bt_port_input_as_port_const(in_downstream_port
);
1370 const char *upstream_port_name
;
1371 const char *downstream_port_name
;
1373 BT_ASSERT(downstream_port
);
1375 /* Skip port if it's already connected. */
1376 if (bt_port_is_connected(downstream_port
)) {
1377 BT_LOGI("Skipping downstream port: already connected: "
1378 "port-addr=%p, port-name=\"%s\"",
1380 bt_port_get_name(downstream_port
));
1384 downstream_port_name
= bt_port_get_name(downstream_port
);
1385 BT_ASSERT(downstream_port_name
);
1386 upstream_port_name
= bt_port_get_name(upstream_port
);
1387 BT_ASSERT(upstream_port_name
);
1389 if (!bt_common_star_glob_match(
1390 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1391 downstream_port_name
, SIZE_MAX
)) {
1395 if (insert_trimmer
) {
1397 * In order to insert the trimmer between the
1398 * two components that were being connected, we
1399 * create a connection configuration entry which
1400 * describes a connection from the trimmer's
1401 * output to the original input that was being
1404 * Hence, the creation of the trimmer will cause
1405 * the graph "new port" listener to establish
1406 * all downstream connections as its output port
1407 * is connected. We will then establish the
1408 * connection between the original upstream
1409 * source and the trimmer.
1411 char *trimmer_name
= NULL
;
1412 bt_graph_add_component_status add_comp_status
;
1414 ret
= asprintf(&trimmer_name
,
1415 "stream-intersection-trimmer-%s",
1416 upstream_port_name
);
1422 ctx
->connect_ports
= false;
1423 add_comp_status
= bt_graph_add_filter_component(
1424 ctx
->graph
, trimmer_class
, trimmer_name
,
1425 trimmer_params
, ctx
->cfg
->log_level
,
1427 bt_component_filter_get_ref(trimmer
);
1429 if (add_comp_status
!=
1430 BT_GRAPH_ADD_COMPONENT_STATUS_OK
) {
1436 bt_component_filter_borrow_input_port_by_index_const(
1438 if (!trimmer_input
) {
1442 bt_component_filter_borrow_output_port_by_index_const(
1444 if (!trimmer_output
) {
1449 * Replace the current downstream port by the trimmer's
1452 in_downstream_port
= trimmer_input
;
1454 bt_port_input_as_port_const(in_downstream_port
);
1455 downstream_port_name
= bt_port_get_name(
1457 BT_ASSERT(downstream_port_name
);
1460 /* We have a winner! */
1461 connect_ports_status
= bt_graph_connect_ports(ctx
->graph
,
1462 out_upstream_port
, in_downstream_port
, NULL
);
1463 downstream_port
= NULL
;
1464 switch (connect_ports_status
) {
1465 case BT_GRAPH_CONNECT_PORTS_STATUS_OK
:
1468 BT_CLI_LOGE_APPEND_CAUSE(
1469 "Cannot create connection: graph refuses to connect ports: "
1470 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1471 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1472 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1473 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1475 upstream_comp
, bt_component_get_name(upstream_comp
),
1476 upstream_port
, bt_port_get_name(upstream_port
),
1477 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1478 downstream_port
, downstream_port_name
,
1479 cfg_conn
->arg
->str
);
1483 BT_LOGI("Connected component ports: "
1484 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1485 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1486 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1487 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1489 upstream_comp
, bt_component_get_name(upstream_comp
),
1490 upstream_port
, bt_port_get_name(upstream_port
),
1491 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1492 downstream_port
, downstream_port_name
,
1493 cfg_conn
->arg
->str
);
1495 if (insert_trimmer
) {
1497 * The first connection, from the source to the trimmer,
1498 * has been done. We now connect the trimmer to the
1499 * original downstream port.
1501 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1503 bt_component_filter_as_component_const(trimmer
),
1504 trimmer_output
, cfg_conn
);
1508 ctx
->connect_ports
= true;
1512 * We found a matching downstream port: the search is
1518 /* No downstream port found */
1519 BT_CLI_LOGE_APPEND_CAUSE(
1520 "Cannot create connection: cannot find a matching downstream port for upstream port: "
1521 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1522 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1523 upstream_port
, bt_port_get_name(upstream_port
),
1524 cfg_conn
->downstream_comp_name
->str
,
1525 cfg_conn
->arg
->str
);
1531 free(intersection_begin
);
1532 free(intersection_end
);
1533 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1534 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1535 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1540 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1541 const bt_port_output
*upstream_port
)
1544 const char *upstream_port_name
;
1545 const char *upstream_comp_name
;
1546 const bt_component
*upstream_comp
= NULL
;
1550 BT_ASSERT(upstream_port
);
1551 upstream_port_name
= bt_port_get_name(
1552 bt_port_output_as_port_const(upstream_port
));
1553 BT_ASSERT(upstream_port_name
);
1554 upstream_comp
= bt_port_borrow_component_const(
1555 bt_port_output_as_port_const(upstream_port
));
1556 BT_ASSERT(upstream_comp
);
1557 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1558 BT_ASSERT(upstream_comp_name
);
1559 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1560 "port-addr=%p, port-name=\"%s\"",
1561 upstream_comp
, upstream_comp_name
,
1562 upstream_port
, upstream_port_name
);
1564 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1565 struct bt_config_connection
*cfg_conn
=
1567 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1569 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1570 upstream_comp_name
)) {
1574 if (!bt_common_star_glob_match(
1575 cfg_conn
->upstream_port_glob
->str
,
1576 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1580 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1581 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1583 BT_CLI_LOGE_APPEND_CAUSE(
1584 "Cannot connect upstream port: "
1585 "port-addr=%p, port-name=\"%s\"",
1587 upstream_port_name
);
1593 BT_CLI_LOGE_APPEND_CAUSE(
1594 "Cannot connect upstream port: port does not match any connection argument: "
1595 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1596 upstream_port_name
);
1606 bt_graph_listener_func_status
1607 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1608 const bt_port_output
*out_port
)
1610 const bt_component
*comp
;
1611 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1612 bt_graph_listener_func_status ret
=
1613 BT_GRAPH_LISTENER_FUNC_STATUS_OK
;
1615 comp
= bt_port_borrow_component_const(port
);
1616 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1617 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1618 comp
, comp
? bt_component_get_name(comp
) : "",
1619 port
, bt_port_get_name(port
));
1621 if (!ctx
->connect_ports
) {
1627 if (bt_port_is_connected(port
)) {
1628 BT_LOGW_STR("Port is already connected.");
1632 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
1633 BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port.");
1634 ret
= BT_GRAPH_LISTENER_FUNC_STATUS_ERROR
;
1643 bt_graph_listener_func_status
graph_source_output_port_added_listener(
1644 const bt_component_source
*component
,
1645 const bt_port_output
*port
, void *data
)
1647 return graph_output_port_added_listener(data
, port
);
1651 bt_graph_listener_func_status
graph_filter_output_port_added_listener(
1652 const bt_component_filter
*component
,
1653 const bt_port_output
*port
, void *data
)
1655 return graph_output_port_added_listener(data
, port
);
1659 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1665 if (ctx
->src_components
) {
1666 g_hash_table_destroy(ctx
->src_components
);
1667 ctx
->src_components
= NULL
;
1670 if (ctx
->flt_components
) {
1671 g_hash_table_destroy(ctx
->flt_components
);
1672 ctx
->flt_components
= NULL
;
1675 if (ctx
->sink_components
) {
1676 g_hash_table_destroy(ctx
->sink_components
);
1677 ctx
->sink_components
= NULL
;
1680 if (ctx
->intersections
) {
1681 g_hash_table_destroy(ctx
->intersections
);
1682 ctx
->intersections
= NULL
;
1685 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
1690 int add_descriptor_to_component_descriptor_set(
1691 bt_component_descriptor_set
*comp_descr_set
,
1692 const char *plugin_name
, const char *comp_cls_name
,
1693 bt_component_class_type comp_cls_type
,
1694 const bt_value
*params
)
1696 const bt_component_class
*comp_cls
;
1699 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1702 BT_CLI_LOGE_APPEND_CAUSE(
1703 "Cannot find component class: plugin-name=\"%s\", "
1704 "comp-cls-name=\"%s\", comp-cls-type=%d",
1705 plugin_name
, comp_cls_name
, comp_cls_type
);
1710 status
= bt_component_descriptor_set_add_descriptor(
1711 comp_descr_set
, comp_cls
, params
);
1712 if (status
!= BT_COMPONENT_DESCRIPTOR_SET_ADD_DESCRIPTOR_STATUS_OK
) {
1713 BT_CLI_LOGE_APPEND_CAUSE(
1714 "Cannot append descriptor to component descriptor set: "
1715 "status=%s", bt_common_func_status_string(status
));
1720 bt_component_class_put_ref(comp_cls
);
1725 int append_descriptors_from_bt_config_component_array(
1726 bt_component_descriptor_set
*comp_descr_set
,
1727 GPtrArray
*component_configs
)
1732 for (i
= 0; i
< component_configs
->len
; i
++) {
1733 struct bt_config_component
*cfg_comp
=
1734 component_configs
->pdata
[i
];
1736 ret
= add_descriptor_to_component_descriptor_set(
1738 cfg_comp
->plugin_name
->str
,
1739 cfg_comp
->comp_cls_name
->str
,
1740 cfg_comp
->type
, cfg_comp
->params
);
1751 bt_get_greatest_operative_mip_version_status
get_greatest_operative_mip_version(
1752 struct bt_config
*cfg
, uint64_t *mip_version
)
1754 bt_get_greatest_operative_mip_version_status status
=
1755 BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK
;
1756 bt_component_descriptor_set
*comp_descr_set
= NULL
;
1760 BT_ASSERT(mip_version
);
1761 comp_descr_set
= bt_component_descriptor_set_create();
1762 if (!comp_descr_set
) {
1763 BT_CLI_LOGE_APPEND_CAUSE(
1764 "Failed to create a component descriptor set object.");
1765 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_MEMORY_ERROR
;
1769 ret
= append_descriptors_from_bt_config_component_array(
1770 comp_descr_set
, cfg
->cmd_data
.run
.sources
);
1772 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1776 ret
= append_descriptors_from_bt_config_component_array(
1777 comp_descr_set
, cfg
->cmd_data
.run
.filters
);
1779 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1783 ret
= append_descriptors_from_bt_config_component_array(
1784 comp_descr_set
, cfg
->cmd_data
.run
.sinks
);
1786 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1790 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1792 * Stream intersection mode adds `flt.utils.trimmer`
1793 * components; we need to include this type of component
1794 * in the component descriptor set to get the real
1795 * greatest operative MIP version.
1797 ret
= add_descriptor_to_component_descriptor_set(
1798 comp_descr_set
, "utils", "trimmer",
1799 BT_COMPONENT_CLASS_TYPE_FILTER
, NULL
);
1801 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1806 status
= bt_get_greatest_operative_mip_version(comp_descr_set
,
1807 bt_cli_log_level
, mip_version
);
1810 bt_component_descriptor_set_put_ref(comp_descr_set
);
1815 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1818 bt_graph_add_listener_status add_listener_status
;
1819 bt_get_greatest_operative_mip_version_status mip_version_status
;
1820 uint64_t mip_version
= UINT64_C(-1);
1823 ctx
->connect_ports
= false;
1824 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
1825 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1826 if (!ctx
->src_components
) {
1830 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
1831 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1832 if (!ctx
->flt_components
) {
1836 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
1837 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1838 if (!ctx
->sink_components
) {
1842 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1843 ctx
->stream_intersection_mode
= true;
1844 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1845 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1846 if (!ctx
->intersections
) {
1852 * Get the greatest operative MIP version to use to configure
1853 * the graph to create.
1855 mip_version_status
= get_greatest_operative_mip_version(
1857 if (mip_version_status
== BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_NO_MATCH
) {
1858 BT_CLI_LOGE_APPEND_CAUSE(
1859 "Failed to find an operative message interchange "
1860 "protocol version to use to create the `run` command's "
1861 "graph (components are not interoperable).");
1863 } else if (mip_version_status
< 0) {
1864 BT_CLI_LOGE_APPEND_CAUSE(
1865 "Cannot find an operative message interchange "
1866 "protocol version to use to create the `run` command's "
1868 bt_common_func_status_string(mip_version_status
));
1872 BT_ASSERT(mip_version_status
== BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK
);
1873 BT_LOGI("Found operative message interchange protocol version to "
1874 "configure the `run` command's graph: mip-version=%" PRIu64
,
1876 ctx
->graph
= bt_graph_create(mip_version
);
1881 bt_graph_add_interrupter(ctx
->graph
, the_interrupter
);
1882 add_listener_status
= bt_graph_add_source_component_output_port_added_listener(
1883 ctx
->graph
, graph_source_output_port_added_listener
, ctx
,
1885 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1886 BT_CLI_LOGE_APPEND_CAUSE(
1887 "Cannot add \"port added\" listener to graph.");
1891 add_listener_status
= bt_graph_add_filter_component_output_port_added_listener(
1892 ctx
->graph
, graph_filter_output_port_added_listener
, ctx
,
1894 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1895 BT_CLI_LOGE_APPEND_CAUSE(
1896 "Cannot add \"port added\" listener to graph.");
1903 cmd_run_ctx_destroy(ctx
);
1911 * Compute the intersection of all streams in the array `streams`, write it
1916 int compute_stream_intersection(const bt_value
*streams
,
1917 struct trace_range
*range
)
1919 uint64_t i
, stream_count
;
1922 BT_ASSERT(bt_value_get_type(streams
) == BT_VALUE_TYPE_ARRAY
);
1924 stream_count
= bt_value_array_get_length(streams
);
1926 BT_ASSERT(stream_count
> 0);
1928 range
->intersection_range_begin_ns
= 0;
1929 range
->intersection_range_end_ns
= UINT64_MAX
;
1931 for (i
= 0; i
< stream_count
; i
++) {
1932 int64_t begin_ns
, end_ns
;
1933 uint64_t begin_ns_u
, end_ns_u
;
1934 const bt_value
*stream_value
;
1935 const bt_value
*range_ns_value
;
1936 const bt_value
*begin_value
;
1937 const bt_value
*end_value
;
1939 stream_value
= bt_value_array_borrow_element_by_index_const(streams
, i
);
1940 if (bt_value_get_type(stream_value
) != BT_VALUE_TYPE_MAP
) {
1941 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1942 "expected streams array element to be a map, got %s.",
1943 bt_common_value_type_string(bt_value_get_type(stream_value
)));
1947 range_ns_value
= bt_value_map_borrow_entry_value_const(
1948 stream_value
, "range-ns");
1949 if (!range_ns_value
) {
1950 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1951 "missing expected `range-ns` key in stream map.");
1955 if (bt_value_get_type(range_ns_value
) != BT_VALUE_TYPE_MAP
) {
1956 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1957 "expected `range-ns` entry value of stream map to be a map, got %s.",
1958 bt_common_value_type_string(bt_value_get_type(range_ns_value
)));
1962 begin_value
= bt_value_map_borrow_entry_value_const(range_ns_value
, "begin");
1964 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1965 "missing expected `begin` key in range-ns map.");
1969 if (bt_value_get_type(begin_value
) != BT_VALUE_TYPE_SIGNED_INTEGER
) {
1970 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1971 "expected `begin` entry value of range-ns map to be a signed integer, got %s.",
1972 bt_common_value_type_string(bt_value_get_type(range_ns_value
)));
1976 end_value
= bt_value_map_borrow_entry_value_const(range_ns_value
, "end");
1978 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1979 "missing expected `end` key in range-ns map.");
1983 if (bt_value_get_type(end_value
) != BT_VALUE_TYPE_SIGNED_INTEGER
) {
1984 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1985 "expected `end` entry value of range-ns map to be a signed integer, got %s.",
1986 bt_common_value_type_string(bt_value_get_type(range_ns_value
)));
1990 begin_ns
= bt_value_integer_signed_get(begin_value
);
1991 end_ns
= bt_value_integer_signed_get(end_value
);
1993 if (begin_ns
< 0 || end_ns
< 0 || end_ns
< begin_ns
) {
1994 BT_CLI_LOGE_APPEND_CAUSE(
1995 "Invalid stream range values: "
1996 "range-ns:begin=%" PRId64
", "
1997 "range-ns:end=%" PRId64
,
2002 begin_ns_u
= begin_ns
;
2005 range
->intersection_range_begin_ns
=
2006 MAX(range
->intersection_range_begin_ns
, begin_ns_u
);
2007 range
->intersection_range_end_ns
=
2008 MIN(range
->intersection_range_end_ns
, end_ns_u
);
2021 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2022 struct bt_config_component
*cfg_comp
,
2023 const bt_component_class_source
*src_comp_cls
)
2027 uint64_t trace_count
;
2028 const bt_value
*query_result
= NULL
;
2029 const bt_value
*trace_info
= NULL
;
2030 const bt_value
*stream_infos
= NULL
;
2031 const bt_value
*stream_info
= NULL
;
2032 struct port_id
*port_id
= NULL
;
2033 struct trace_range
*stream_intersection
= NULL
;
2034 const char *fail_reason
= NULL
;
2035 const bt_component_class
*comp_cls
=
2036 bt_component_class_source_as_component_class_const(src_comp_cls
);
2038 ret
= query(ctx
->cfg
, comp_cls
, "babeltrace.trace-infos",
2039 cfg_comp
->params
, &query_result
,
2042 BT_CLI_LOGE_APPEND_CAUSE("Failed to execute `babeltrace.trace-infos` query: %s: "
2043 "comp-class-name=\"%s\"", fail_reason
,
2044 bt_component_class_get_name(comp_cls
));
2049 BT_ASSERT(query_result
);
2051 if (!bt_value_is_array(query_result
)) {
2052 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting result to be an array: "
2053 "component-class-name=%s, actual-type=%s",
2054 bt_component_class_get_name(comp_cls
),
2055 bt_common_value_type_string(bt_value_get_type(query_result
)));
2060 trace_count
= bt_value_array_get_length(query_result
);
2061 if (trace_count
== 0) {
2062 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: result is empty: "
2063 "component-class-name=%s", bt_component_class_get_name(comp_cls
));
2068 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2069 uint64_t stream_idx
;
2070 int64_t stream_count
;
2071 struct trace_range trace_intersection
;
2073 trace_info
= bt_value_array_borrow_element_by_index_const(
2074 query_result
, trace_idx
);
2075 if (!bt_value_is_map(trace_info
)) {
2077 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting element to be a map: "
2078 "component-class-name=%s, actual-type=%s",
2079 bt_component_class_get_name(comp_cls
),
2080 bt_common_value_type_string(bt_value_get_type(trace_info
)));
2084 stream_infos
= bt_value_map_borrow_entry_value_const(
2085 trace_info
, "stream-infos");
2086 if (!stream_infos
) {
2088 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: missing `streams` key in trace info map: "
2089 "component-class-name=%s",
2090 bt_component_class_get_name(comp_cls
));
2094 if (!bt_value_is_array(stream_infos
)) {
2096 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting `streams` entry of trace info map to be an array: "
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(stream_infos
)));
2103 stream_count
= bt_value_array_get_length(stream_infos
);
2104 if (stream_count
== 0) {
2106 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: list of streams is empty: "
2107 "component-class-name=%s",
2108 bt_component_class_get_name(comp_cls
));
2112 ret
= compute_stream_intersection(stream_infos
, &trace_intersection
);
2114 BT_CLI_LOGE_APPEND_CAUSE("Failed to compute trace streams intersection.");
2118 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2119 const bt_value
*port_name
;
2121 port_id
= g_new0(struct port_id
, 1);
2124 BT_CLI_LOGE_APPEND_CAUSE(
2125 "Cannot allocate memory for port_id structure.");
2128 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2129 if (!port_id
->instance_name
) {
2131 BT_CLI_LOGE_APPEND_CAUSE(
2132 "Cannot allocate memory for port_id component instance name.");
2136 stream_intersection
= g_new0(struct trace_range
, 1);
2137 if (!stream_intersection
) {
2139 BT_CLI_LOGE_APPEND_CAUSE(
2140 "Cannot allocate memory for trace_range structure.");
2144 *stream_intersection
= trace_intersection
;
2146 stream_info
= bt_value_array_borrow_element_by_index_const(
2147 stream_infos
, stream_idx
);
2148 if (!bt_value_is_map(stream_info
)) {
2150 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
2151 "expecting element of stream list to be a map: "
2152 "component-class-name=%s, actual-type=%s",
2153 bt_component_class_get_name(comp_cls
),
2154 bt_common_value_type_string(bt_value_get_type(stream_info
)));
2158 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2161 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
2162 "missing `port-name` key in stream info map: "
2163 "component-class-name=%s",
2164 bt_component_class_get_name(comp_cls
));
2168 if (!bt_value_is_string(port_name
)) {
2170 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
2171 "expecting `port-name` entry of stream info map to be a string: "
2172 "component-class-name=%s, actual-type=%s",
2173 bt_component_class_get_name(comp_cls
),
2174 bt_common_value_type_string(bt_value_get_type(port_name
)));
2178 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2179 if (!port_id
->port_name
) {
2181 BT_CLI_LOGE_APPEND_CAUSE(
2182 "Cannot allocate memory for port_id port_name.");
2186 BT_LOGD("Inserting stream intersection ");
2188 g_hash_table_insert(ctx
->intersections
, port_id
, stream_intersection
);
2191 stream_intersection
= NULL
;
2198 bt_value_put_ref(query_result
);
2200 g_free(stream_intersection
);
2205 int cmd_run_ctx_create_components_from_config_components(
2206 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2209 const void *comp_cls
= NULL
;
2210 const void *comp
= NULL
;
2213 for (i
= 0; i
< cfg_components
->len
; i
++) {
2214 struct bt_config_component
*cfg_comp
=
2215 g_ptr_array_index(cfg_components
, i
);
2218 switch (cfg_comp
->type
) {
2219 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2220 comp_cls
= find_source_component_class(
2221 cfg_comp
->plugin_name
->str
,
2222 cfg_comp
->comp_cls_name
->str
);
2224 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2225 comp_cls
= find_filter_component_class(
2226 cfg_comp
->plugin_name
->str
,
2227 cfg_comp
->comp_cls_name
->str
);
2229 case BT_COMPONENT_CLASS_TYPE_SINK
:
2230 comp_cls
= find_sink_component_class(
2231 cfg_comp
->plugin_name
->str
,
2232 cfg_comp
->comp_cls_name
->str
);
2239 BT_CLI_LOGE_APPEND_CAUSE(
2240 "Cannot find component class: plugin-name=\"%s\", "
2241 "comp-cls-name=\"%s\", comp-cls-type=%d",
2242 cfg_comp
->plugin_name
->str
,
2243 cfg_comp
->comp_cls_name
->str
,
2248 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_TRACE
);
2250 switch (cfg_comp
->type
) {
2251 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2252 ret
= bt_graph_add_source_component(ctx
->graph
,
2253 comp_cls
, cfg_comp
->instance_name
->str
,
2254 cfg_comp
->params
, cfg_comp
->log_level
,
2256 bt_component_source_get_ref(comp
);
2258 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2259 ret
= bt_graph_add_filter_component(ctx
->graph
,
2260 comp_cls
, cfg_comp
->instance_name
->str
,
2261 cfg_comp
->params
, cfg_comp
->log_level
,
2263 bt_component_filter_get_ref(comp
);
2265 case BT_COMPONENT_CLASS_TYPE_SINK
:
2266 ret
= bt_graph_add_sink_component(ctx
->graph
,
2267 comp_cls
, cfg_comp
->instance_name
->str
,
2268 cfg_comp
->params
, cfg_comp
->log_level
,
2270 bt_component_sink_get_ref(comp
);
2277 BT_CLI_LOGE_APPEND_CAUSE(
2278 "Cannot create component: plugin-name=\"%s\", "
2279 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2281 cfg_comp
->plugin_name
->str
,
2282 cfg_comp
->comp_cls_name
->str
,
2283 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2287 if (ctx
->stream_intersection_mode
&&
2288 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2289 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2291 BT_CLI_LOGE_APPEND_CAUSE(
2292 "Cannot determine stream intersection of trace.");
2297 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2298 comp
, cfg_comp
->instance_name
->str
);
2299 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2300 BT_ASSERT(quark
> 0);
2302 switch (cfg_comp
->type
) {
2303 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2304 g_hash_table_insert(ctx
->src_components
,
2305 GUINT_TO_POINTER(quark
), (void *) comp
);
2307 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2308 g_hash_table_insert(ctx
->flt_components
,
2309 GUINT_TO_POINTER(quark
), (void *) comp
);
2311 case BT_COMPONENT_CLASS_TYPE_SINK
:
2312 g_hash_table_insert(ctx
->sink_components
,
2313 GUINT_TO_POINTER(quark
), (void *) comp
);
2320 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2329 bt_object_put_ref(comp
);
2330 bt_object_put_ref(comp_cls
);
2335 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2340 * Make sure that, during this phase, our graph's "port added"
2341 * listener does not connect ports while we are creating the
2342 * components because we have a special, initial phase for
2345 ctx
->connect_ports
= false;
2347 ret
= cmd_run_ctx_create_components_from_config_components(
2348 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2354 ret
= cmd_run_ctx_create_components_from_config_components(
2355 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2361 ret
= cmd_run_ctx_create_components_from_config_components(
2362 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2372 typedef uint64_t (*output_port_count_func_t
)(const void *);
2373 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2374 const void *, uint64_t);
2377 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2378 void *comp
, output_port_count_func_t port_count_fn
,
2379 borrow_output_port_by_index_func_t port_by_index_fn
)
2385 count
= port_count_fn(comp
);
2387 for (i
= 0; i
< count
; i
++) {
2388 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2390 BT_ASSERT(upstream_port
);
2391 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2402 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2405 GHashTableIter iter
;
2406 gpointer g_name_quark
, g_comp
;
2408 ctx
->connect_ports
= true;
2409 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2411 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2412 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2413 (output_port_count_func_t
)
2414 bt_component_source_get_output_port_count
,
2415 (borrow_output_port_by_index_func_t
)
2416 bt_component_source_borrow_output_port_by_index_const
);
2422 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2424 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2425 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2426 (output_port_count_func_t
)
2427 bt_component_filter_get_output_port_count
,
2428 (borrow_output_port_by_index_func_t
)
2429 bt_component_filter_borrow_output_port_by_index_const
);
2440 enum bt_cmd_status
cmd_run(struct bt_config
*cfg
)
2442 enum bt_cmd_status cmd_status
;
2443 struct cmd_run_ctx ctx
= { 0 };
2445 /* Initialize the command's context and the graph object */
2446 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2447 BT_CLI_LOGE_APPEND_CAUSE(
2448 "Cannot initialize the command's context.");
2452 if (bt_interrupter_is_set(the_interrupter
)) {
2453 BT_CLI_LOGW_APPEND_CAUSE(
2454 "Interrupted by user before creating components.");
2458 BT_LOGI_STR("Creating components.");
2460 /* Create the requested component instances */
2461 if (cmd_run_ctx_create_components(&ctx
)) {
2462 BT_CLI_LOGE_APPEND_CAUSE("Cannot create components.");
2466 if (bt_interrupter_is_set(the_interrupter
)) {
2467 BT_CLI_LOGW_APPEND_CAUSE(
2468 "Interrupted by user before connecting components.");
2472 BT_LOGI_STR("Connecting components.");
2474 /* Connect the initially visible component ports */
2475 if (cmd_run_ctx_connect_ports(&ctx
)) {
2476 BT_CLI_LOGE_APPEND_CAUSE(
2477 "Cannot connect initial component ports.");
2481 BT_LOGI_STR("Running the graph.");
2485 bt_graph_run_status run_status
= bt_graph_run(ctx
.graph
);
2488 * Reset console in case something messed with console
2489 * codes during the graph's execution.
2491 printf("%s", bt_common_color_reset());
2493 fprintf(stderr
, "%s", bt_common_color_reset());
2494 BT_LOGT("bt_graph_run() returned: status=%s",
2495 bt_common_func_status_string(run_status
));
2497 switch (run_status
) {
2498 case BT_GRAPH_RUN_STATUS_OK
:
2499 cmd_status
= BT_CMD_STATUS_OK
;
2501 case BT_GRAPH_RUN_STATUS_AGAIN
:
2502 if (bt_interrupter_is_set(the_interrupter
)) {
2504 * The graph was interrupted by a SIGINT.
2506 cmd_status
= BT_CMD_STATUS_INTERRUPTED
;
2510 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2511 BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: "
2513 cfg
->cmd_data
.run
.retry_duration_us
);
2515 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2516 if (bt_interrupter_is_set(the_interrupter
)) {
2517 cmd_status
= BT_CMD_STATUS_INTERRUPTED
;
2524 if (bt_interrupter_is_set(the_interrupter
)) {
2525 cmd_status
= BT_CMD_STATUS_INTERRUPTED
;
2529 BT_CLI_LOGE_APPEND_CAUSE(
2530 "Graph failed to complete successfully");
2535 cmd_status
= BT_CMD_STATUS_ERROR
;
2538 cmd_run_ctx_destroy(&ctx
);
2543 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2545 const char *env_clash
;
2547 if (!cfg
->command_name
) {
2551 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2552 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2556 if (g_file_test(cfg
->command_name
,
2557 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2558 _bt_log_write_d(_BT_LOG_SRCLOC_FUNCTION
, __FILE__
, __LINE__
,
2559 BT_LOG_WARNING
, BT_LOG_TAG
,
2560 "The `%s` command was executed. "
2561 "If you meant to convert a trace located in "
2562 "the local `%s` directory, please use:\n\n"
2563 " babeltrace2 convert %s [OPTIONS]",
2564 cfg
->command_name
, cfg
->command_name
,
2570 void init_log_level(void)
2572 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2576 void print_error_causes(void)
2578 const bt_error
*error
= bt_current_thread_take_error();
2579 unsigned int columns
;
2580 gchar
*error_str
= NULL
;
2582 if (!error
|| bt_error_get_cause_count(error
) == 0) {
2583 fprintf(stderr
, "%s%sUnknown command-line error.%s\n",
2584 bt_common_color_bold(), bt_common_color_fg_bright_red(),
2585 bt_common_color_reset());
2589 /* Try to get terminal width to fold the error cause messages */
2590 if (bt_common_get_term_size(&columns
, NULL
) < 0) {
2591 /* Width not found: default to 80 */
2596 * This helps visually separate the error causes from the last
2597 * logging statement.
2599 fputc('\n', stderr
);
2601 error_str
= format_bt_error(error
, columns
, bt_cli_log_level
,
2602 BT_COMMON_COLOR_WHEN_AUTO
);
2603 BT_ASSERT(error_str
);
2605 fprintf(stderr
, "%s\n", error_str
);
2609 bt_error_release(error
);
2615 int main(int argc
, const char **argv
)
2618 enum bt_cmd_status cmd_status
;
2619 struct bt_config
*cfg
= NULL
;
2622 set_signal_handler();
2623 init_loaded_plugins();
2625 BT_ASSERT(!the_interrupter
);
2626 the_interrupter
= bt_interrupter_create();
2627 if (!the_interrupter
) {
2628 BT_CLI_LOGE_APPEND_CAUSE("Failed to create an interrupter object.");
2633 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
,
2637 /* Quit without errors; typically usage/version */
2639 BT_LOGI_STR("Quitting without errors.");
2644 BT_CLI_LOGE_APPEND_CAUSE(
2645 "Command-line error: retcode=%d", retcode
);
2650 BT_CLI_LOGE_APPEND_CAUSE(
2651 "Failed to create a valid Babeltrace CLI configuration.");
2658 if (cfg
->command_needs_plugins
) {
2659 ret
= require_loaded_plugins(cfg
->plugin_paths
);
2661 BT_CLI_LOGE_APPEND_CAUSE(
2662 "Failed to load plugins: ret=%d", ret
);
2668 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2669 cfg
->command
, cfg
->command_name
);
2671 switch (cfg
->command
) {
2672 case BT_CONFIG_COMMAND_RUN
:
2673 cmd_status
= cmd_run(cfg
);
2675 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2676 cmd_status
= cmd_list_plugins(cfg
);
2678 case BT_CONFIG_COMMAND_HELP
:
2679 cmd_status
= cmd_help(cfg
);
2681 case BT_CONFIG_COMMAND_QUERY
:
2682 cmd_status
= cmd_query(cfg
);
2684 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2685 cmd_status
= cmd_print_ctf_metadata(cfg
);
2687 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2688 cmd_status
= cmd_print_lttng_live_sessions(cfg
);
2691 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2695 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", command-status=\"%s\"",
2696 cfg
->command
, cfg
->command_name
, bt_cmd_status_string(cmd_status
));
2697 warn_command_name_and_directory_clash(cfg
);
2699 switch (cmd_status
) {
2700 case BT_CMD_STATUS_OK
:
2703 case BT_CMD_STATUS_ERROR
:
2706 case BT_CMD_STATUS_INTERRUPTED
:
2710 BT_LOGF("Invalid command status: cmd-status=%d", cmd_status
);
2716 print_error_causes();
2719 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2720 fini_loaded_plugins();
2721 bt_interrupter_put_ref(the_interrupter
);
2724 * Clear current thread's error in case there is one to avoid a
2727 bt_current_thread_clear_error();