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
31 #define EXIT_INTERRUPTED 2
35 BT_CMD_STATUS_ERROR
= -1,
36 BT_CMD_STATUS_INTERRUPTED
= -2,
40 const char *bt_cmd_status_string(enum bt_cmd_status cmd_status
)
43 case BT_CMD_STATUS_OK
:
45 case BT_CMD_STATUS_ERROR
:
47 case BT_CMD_STATUS_INTERRUPTED
:
54 /* Application's interrupter (owned by this) */
55 static bt_interrupter
*the_interrupter
;
62 BOOL WINAPI
signal_handler(DWORD signal
__attribute__((unused
))) {
63 if (the_interrupter
) {
64 bt_interrupter_set(the_interrupter
);
71 void set_signal_handler(void)
73 if (!SetConsoleCtrlHandler(signal_handler
, TRUE
)) {
74 BT_LOGE("Failed to set the Ctrl+C handler.");
78 #else /* __MINGW32__ */
81 void signal_handler(int signum
)
83 if (signum
!= SIGINT
) {
87 if (the_interrupter
) {
88 bt_interrupter_set(the_interrupter
);
93 void set_signal_handler(void)
95 struct sigaction new_action
, old_action
;
97 new_action
.sa_handler
= signal_handler
;
98 sigemptyset(&new_action
.sa_mask
);
99 new_action
.sa_flags
= 0;
100 sigaction(SIGINT
, NULL
, &old_action
);
102 if (old_action
.sa_handler
!= SIG_IGN
) {
103 sigaction(SIGINT
, &new_action
, NULL
);
107 #endif /* __MINGW32__ */
110 int query(struct bt_config
*cfg
, const bt_component_class
*comp_cls
,
111 const char *obj
, const bt_value
*params
,
112 const bt_value
**user_result
, const char **fail_reason
)
114 return cli_query(comp_cls
, obj
, params
, cfg
->log_level
,
115 the_interrupter
, user_result
, fail_reason
);
118 typedef const void *(*plugin_borrow_comp_cls_func_t
)(
119 const bt_plugin
*, const char *);
122 const void *find_component_class_from_plugin(const char *plugin_name
,
123 const char *comp_class_name
,
124 plugin_borrow_comp_cls_func_t plugin_borrow_comp_cls_func
)
126 const void *comp_class
= NULL
;
127 const bt_plugin
*plugin
;
129 BT_LOGI("Finding component class: plugin-name=\"%s\", "
130 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
132 plugin
= borrow_loaded_plugin_by_name(plugin_name
);
137 comp_class
= plugin_borrow_comp_cls_func(plugin
, comp_class_name
);
138 bt_object_get_ref(comp_class
);
142 BT_LOGI("Found component class: plugin-name=\"%s\", "
143 "comp-cls-name=\"%s\"", plugin_name
, comp_class_name
);
145 BT_LOGI("Cannot find source component class: "
146 "plugin-name=\"%s\", comp-cls-name=\"%s\"",
147 plugin_name
, comp_class_name
);
154 const bt_component_class_source
*find_source_component_class(
155 const char *plugin_name
, const char *comp_class_name
)
157 return (const void *) find_component_class_from_plugin(
158 plugin_name
, comp_class_name
,
159 (plugin_borrow_comp_cls_func_t
)
160 bt_plugin_borrow_source_component_class_by_name_const
);
164 const bt_component_class_filter
*find_filter_component_class(
165 const char *plugin_name
, const char *comp_class_name
)
167 return (const void *) find_component_class_from_plugin(
168 plugin_name
, comp_class_name
,
169 (plugin_borrow_comp_cls_func_t
)
170 bt_plugin_borrow_filter_component_class_by_name_const
);
174 const bt_component_class_sink
*find_sink_component_class(
175 const char *plugin_name
, const char *comp_class_name
)
177 return (const void *) find_component_class_from_plugin(plugin_name
,
179 (plugin_borrow_comp_cls_func_t
)
180 bt_plugin_borrow_sink_component_class_by_name_const
);
184 const bt_component_class
*find_component_class(const char *plugin_name
,
185 const char *comp_class_name
,
186 bt_component_class_type comp_class_type
)
188 const bt_component_class
*comp_cls
= NULL
;
190 switch (comp_class_type
) {
191 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
192 comp_cls
= bt_component_class_source_as_component_class_const(find_source_component_class(plugin_name
, comp_class_name
));
194 case BT_COMPONENT_CLASS_TYPE_FILTER
:
195 comp_cls
= bt_component_class_filter_as_component_class_const(find_filter_component_class(plugin_name
, comp_class_name
));
197 case BT_COMPONENT_CLASS_TYPE_SINK
:
198 comp_cls
= bt_component_class_sink_as_component_class_const(find_sink_component_class(plugin_name
, comp_class_name
));
208 void print_indent(FILE *fp
, size_t indent
)
212 for (i
= 0; i
< indent
; i
++) {
218 void print_value(FILE *, const bt_value
*, size_t);
221 void print_value_rec(FILE *, const bt_value
*, size_t);
224 void print_map_value(const char *key
, const bt_value
*object
, FILE *fp
,
227 print_indent(fp
, indent
);
228 fprintf(fp
, "%s: ", key
);
231 if (bt_value_is_array(object
) &&
232 bt_value_array_is_empty(object
)) {
233 fprintf(fp
, "[ ]\n");
237 if (bt_value_is_map(object
) &&
238 bt_value_map_is_empty(object
)) {
239 fprintf(fp
, "{ }\n");
243 if (bt_value_is_array(object
) ||
244 bt_value_is_map(object
)) {
248 print_value_rec(fp
, object
, indent
+ 2);
255 bt_value_map_foreach_entry_const_func_status
collect_map_keys(
257 const bt_value
*object
__attribute__((unused
)),
260 GPtrArray
*map_keys
= data
;
262 g_ptr_array_add(map_keys
, (gpointer
*) key
);
264 return BT_VALUE_MAP_FOREACH_ENTRY_CONST_FUNC_STATUS_OK
;
268 gint
g_ptr_array_sort_strings(gconstpointer a
, gconstpointer b
) {
269 const char *s1
= *((const char **) a
);
270 const char *s2
= *((const char **) b
);
272 return g_strcmp0(s1
, s2
);
276 void print_value_rec(FILE *fp
, const bt_value
*value
, size_t indent
)
283 GPtrArray
*map_keys
= NULL
;
287 switch (bt_value_get_type(value
)) {
288 case BT_VALUE_TYPE_NULL
:
289 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
290 bt_common_color_reset());
292 case BT_VALUE_TYPE_BOOL
:
293 bool_val
= bt_value_bool_get(value
);
294 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
295 bt_common_color_fg_bright_cyan(), bool_val
? "yes" : "no",
296 bt_common_color_reset());
298 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
299 uint_val
= bt_value_integer_unsigned_get(value
);
300 fprintf(fp
, "%s%s%" PRIu64
"%s\n", bt_common_color_bold(),
301 bt_common_color_fg_bright_red(), uint_val
,
302 bt_common_color_reset());
304 case BT_VALUE_TYPE_SIGNED_INTEGER
:
305 int_val
= bt_value_integer_signed_get(value
);
306 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
307 bt_common_color_fg_bright_red(), int_val
,
308 bt_common_color_reset());
310 case BT_VALUE_TYPE_REAL
:
311 dbl_val
= bt_value_real_get(value
);
312 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
313 bt_common_color_fg_bright_red(), dbl_val
,
314 bt_common_color_reset());
316 case BT_VALUE_TYPE_STRING
:
317 str_val
= bt_value_string_get(value
);
318 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
319 bt_common_color_fg_bright_green(), str_val
,
320 bt_common_color_reset());
322 case BT_VALUE_TYPE_ARRAY
:
325 size
= bt_value_array_get_length(value
);
327 print_indent(fp
, indent
);
328 fprintf(fp
, "[ ]\n");
332 for (i
= 0; i
< size
; i
++) {
333 const bt_value
*element
=
334 bt_value_array_borrow_element_by_index_const(
337 print_indent(fp
, indent
);
340 if (bt_value_is_array(element
) &&
341 bt_value_array_is_empty(element
)) {
342 fprintf(fp
, "[ ]\n");
346 if (bt_value_is_map(element
) &&
347 bt_value_map_is_empty(element
)) {
348 fprintf(fp
, "{ }\n");
352 if (bt_value_is_array(element
) ||
353 bt_value_is_map(element
)) {
357 print_value_rec(fp
, element
, indent
+ 2);
361 case BT_VALUE_TYPE_MAP
:
364 bt_value_map_foreach_entry_const_status foreach_status
;
366 if (bt_value_map_is_empty(value
)) {
367 print_indent(fp
, indent
);
368 fprintf(fp
, "{ }\n");
372 map_keys
= g_ptr_array_new();
374 BT_CLI_LOGE_APPEND_CAUSE("Failed to allocated on GPtrArray.");
379 * We want to print the map entries in a stable order. Collect
380 * all the map's keys in a GPtrArray, sort it, then print the
381 * entries in that order.
383 foreach_status
= bt_value_map_foreach_entry_const(value
,
384 collect_map_keys
, map_keys
);
385 if (foreach_status
!= BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_OK
) {
386 BT_CLI_LOGE_APPEND_CAUSE("Failed to iterator on map value.");
390 g_ptr_array_sort(map_keys
, g_ptr_array_sort_strings
);
392 for (i
= 0; i
< map_keys
->len
; i
++) {
393 const char *map_key
= g_ptr_array_index(map_keys
, i
);
394 const bt_value
*map_value
;
396 map_value
= bt_value_map_borrow_entry_value_const(value
, map_key
);
397 BT_ASSERT(map_value
);
399 print_map_value(map_key
, map_value
, fp
, indent
);
412 g_ptr_array_free(map_keys
, TRUE
);
417 void print_value(FILE *fp
, const bt_value
*value
, size_t indent
)
419 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
420 print_indent(fp
, indent
);
423 print_value_rec(fp
, value
, indent
);
427 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
431 comp_cls_str
= format_plugin_comp_cls_opt(
432 bt_config_component
->plugin_name
->str
,
433 bt_config_component
->comp_cls_name
->str
,
434 bt_config_component
->type
,
435 BT_COMMON_COLOR_WHEN_AUTO
);
436 BT_ASSERT(comp_cls_str
);
438 fprintf(stderr
, " %s:\n", comp_cls_str
);
440 if (bt_config_component
->instance_name
->len
> 0) {
441 fprintf(stderr
, " Name: %s\n",
442 bt_config_component
->instance_name
->str
);
445 fprintf(stderr
, " Parameters:\n");
446 print_value(stderr
, bt_config_component
->params
, 8);
448 g_free(comp_cls_str
);
452 void print_bt_config_components(GPtrArray
*array
)
456 for (i
= 0; i
< array
->len
; i
++) {
457 struct bt_config_component
*cfg_component
=
458 bt_config_get_component(array
, i
);
459 print_bt_config_component(cfg_component
);
460 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
465 void print_plugin_paths(const bt_value
*plugin_paths
)
467 fprintf(stderr
, " Plugin paths:\n");
468 print_value(stderr
, plugin_paths
, 4);
472 void print_cfg_run(struct bt_config
*cfg
)
476 print_plugin_paths(cfg
->plugin_paths
);
477 fprintf(stderr
, " Source component instances:\n");
478 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
480 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
481 fprintf(stderr
, " Filter component instances:\n");
482 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
485 fprintf(stderr
, " Sink component instances:\n");
486 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
487 fprintf(stderr
, " Connections:\n");
489 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
490 struct bt_config_connection
*cfg_connection
=
491 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
494 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
495 cfg_connection
->upstream_comp_name
->str
,
496 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
497 cfg_connection
->upstream_port_glob
->str
,
498 cfg_connection
->downstream_comp_name
->str
,
499 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
500 cfg_connection
->downstream_port_glob
->str
);
505 void print_cfg_list_plugins(struct bt_config
*cfg
)
507 print_plugin_paths(cfg
->plugin_paths
);
511 void print_cfg_help(struct bt_config
*cfg
)
513 print_plugin_paths(cfg
->plugin_paths
);
517 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
519 print_plugin_paths(cfg
->plugin_paths
);
520 fprintf(stderr
, " Path: %s\n",
521 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
525 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
527 print_plugin_paths(cfg
->plugin_paths
);
528 fprintf(stderr
, " URL: %s\n",
529 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
533 void print_cfg_query(struct bt_config
*cfg
)
535 print_plugin_paths(cfg
->plugin_paths
);
536 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
537 fprintf(stderr
, " Component class:\n");
538 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
542 void print_cfg(struct bt_config
*cfg
)
544 if (!BT_LOG_ON_INFO
) {
548 BT_LOGI_STR("CLI configuration:");
550 switch (cfg
->command
) {
551 case BT_CONFIG_COMMAND_RUN
:
554 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
555 print_cfg_list_plugins(cfg
);
557 case BT_CONFIG_COMMAND_HELP
:
560 case BT_CONFIG_COMMAND_QUERY
:
561 print_cfg_query(cfg
);
563 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
564 print_cfg_print_ctf_metadata(cfg
);
566 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
567 print_cfg_print_lttng_live_sessions(cfg
);
575 void print_plugin_info(const bt_plugin
*plugin
)
577 unsigned int major
, minor
, patch
;
579 bt_property_availability version_avail
;
580 const char *plugin_name
;
584 const char *plugin_description
;
586 plugin_name
= bt_plugin_get_name(plugin
);
587 path
= bt_plugin_get_path(plugin
);
588 author
= bt_plugin_get_author(plugin
);
589 license
= bt_plugin_get_license(plugin
);
590 plugin_description
= bt_plugin_get_description(plugin
);
591 version_avail
= bt_plugin_get_version(plugin
, &major
, &minor
,
593 printf("%s%s%s%s:\n", bt_common_color_bold(),
594 bt_common_color_fg_bright_blue(), plugin_name
,
595 bt_common_color_reset());
597 printf(" %sPath%s: %s\n", bt_common_color_bold(),
598 bt_common_color_reset(), path
);
603 if (version_avail
== BT_PROPERTY_AVAILABILITY_AVAILABLE
) {
604 printf(" %sVersion%s: %u.%u.%u",
605 bt_common_color_bold(), bt_common_color_reset(),
606 major
, minor
, patch
);
615 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
616 bt_common_color_reset(),
617 plugin_description
? plugin_description
: "(None)");
618 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
619 bt_common_color_reset(), author
? author
: "(Unknown)");
620 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
621 bt_common_color_reset(),
622 license
? license
: "(Unknown)");
626 enum bt_cmd_status
cmd_query(struct bt_config
*cfg
)
629 enum bt_cmd_status cmd_status
;
630 const bt_component_class
*comp_cls
= NULL
;
631 const bt_value
*results
= NULL
;
632 const char *fail_reason
= NULL
;
634 comp_cls
= find_component_class(
635 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
636 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
637 cfg
->cmd_data
.query
.cfg_component
->type
);
639 BT_CLI_LOGE_APPEND_CAUSE(
640 "Cannot find component class: plugin-name=\"%s\", "
641 "comp-cls-name=\"%s\", comp-cls-type=%s",
642 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
643 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
644 bt_common_component_class_type_string(
645 cfg
->cmd_data
.query
.cfg_component
->type
));
649 ret
= query(cfg
, comp_cls
, cfg
->cmd_data
.query
.object
->str
,
650 cfg
->cmd_data
.query
.cfg_component
->params
,
651 &results
, &fail_reason
);
653 BT_CLI_LOGE_APPEND_CAUSE(
654 "Failed to query component class: %s: plugin-name=\"%s\", "
655 "comp-cls-name=\"%s\", comp-cls-type=%s "
656 "object=\"%s\"", fail_reason
,
657 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
658 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
659 bt_common_component_class_type_string(
660 cfg
->cmd_data
.query
.cfg_component
->type
),
661 cfg
->cmd_data
.query
.object
->str
);
665 print_value(stdout
, results
, 0);
666 cmd_status
= BT_CMD_STATUS_OK
;
670 cmd_status
= BT_CMD_STATUS_ERROR
;
673 bt_component_class_put_ref(comp_cls
);
674 bt_value_put_ref(results
);
679 void print_component_class_help(const char *plugin_name
,
680 const bt_component_class
*comp_cls
)
682 const char *comp_class_name
=
683 bt_component_class_get_name(comp_cls
);
684 const char *comp_class_description
=
685 bt_component_class_get_description(comp_cls
);
686 const char *comp_class_help
=
687 bt_component_class_get_help(comp_cls
);
688 bt_component_class_type type
=
689 bt_component_class_get_type(comp_cls
);
692 comp_cls_str
= format_plugin_comp_cls_opt(plugin_name
, comp_class_name
,
693 type
, BT_COMMON_COLOR_WHEN_AUTO
);
694 BT_ASSERT(comp_cls_str
);
696 printf("%s\n", comp_cls_str
);
697 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
698 bt_common_color_reset(),
699 comp_class_description
? comp_class_description
: "(None)");
701 if (comp_class_help
) {
702 printf("\n%s\n", comp_class_help
);
705 g_free(comp_cls_str
);
709 enum bt_cmd_status
cmd_help(struct bt_config
*cfg
)
711 enum bt_cmd_status cmd_status
;
712 const bt_plugin
*plugin
= NULL
;
713 const bt_component_class
*needed_comp_cls
= NULL
;
715 plugin
= borrow_loaded_plugin_by_name(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
717 BT_CLI_LOGE_APPEND_CAUSE(
718 "Cannot find plugin: plugin-name=\"%s\"",
719 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
723 print_plugin_info(plugin
);
724 printf(" %sSource component classes%s: %d\n",
725 bt_common_color_bold(),
726 bt_common_color_reset(),
727 (int) bt_plugin_get_source_component_class_count(plugin
));
728 printf(" %sFilter component classes%s: %d\n",
729 bt_common_color_bold(),
730 bt_common_color_reset(),
731 (int) bt_plugin_get_filter_component_class_count(plugin
));
732 printf(" %sSink component classes%s: %d\n",
733 bt_common_color_bold(),
734 bt_common_color_reset(),
735 (int) bt_plugin_get_sink_component_class_count(plugin
));
737 if (strlen(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
) == 0) {
738 /* Plugin help only */
739 cmd_status
= BT_CMD_STATUS_OK
;
743 needed_comp_cls
= find_component_class(
744 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
745 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
746 cfg
->cmd_data
.help
.cfg_component
->type
);
747 if (!needed_comp_cls
) {
748 BT_CLI_LOGE_APPEND_CAUSE(
749 "Cannot find component class: plugin-name=\"%s\", "
750 "comp-cls-name=\"%s\", comp-cls-type=%s",
751 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
752 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
753 bt_common_component_class_type_string(
754 cfg
->cmd_data
.help
.cfg_component
->type
));
759 print_component_class_help(
760 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
762 cmd_status
= BT_CMD_STATUS_OK
;
766 cmd_status
= BT_CMD_STATUS_ERROR
;
769 bt_component_class_put_ref(needed_comp_cls
);
773 typedef void *(* plugin_borrow_comp_cls_by_index_func_t
)(const bt_plugin
*,
775 typedef const bt_component_class
*(* spec_comp_cls_borrow_comp_cls_func_t
)(
779 void cmd_list_plugins_print_component_classes(const bt_plugin
*plugin
,
780 const char *cc_type_name
, uint64_t count
,
781 plugin_borrow_comp_cls_by_index_func_t borrow_comp_cls_by_index_func
,
782 spec_comp_cls_borrow_comp_cls_func_t spec_comp_cls_borrow_comp_cls_func
)
785 gchar
*comp_cls_str
= NULL
;
788 printf(" %s%s component classes%s: (none)\n",
789 bt_common_color_bold(),
791 bt_common_color_reset());
794 printf(" %s%s component classes%s:\n",
795 bt_common_color_bold(),
797 bt_common_color_reset());
800 for (i
= 0; i
< count
; i
++) {
801 const bt_component_class
*comp_class
=
802 spec_comp_cls_borrow_comp_cls_func(
803 borrow_comp_cls_by_index_func(plugin
, i
));
804 const char *comp_class_name
=
805 bt_component_class_get_name(comp_class
);
806 const char *comp_class_description
=
807 bt_component_class_get_description(comp_class
);
808 bt_component_class_type type
=
809 bt_component_class_get_type(comp_class
);
811 g_free(comp_cls_str
);
812 comp_cls_str
= format_plugin_comp_cls_opt(
813 bt_plugin_get_name(plugin
), comp_class_name
, type
,
814 BT_COMMON_COLOR_WHEN_AUTO
);
815 printf(" %s", comp_cls_str
);
817 if (comp_class_description
) {
818 printf(": %s", comp_class_description
);
825 g_free(comp_cls_str
);
829 enum bt_cmd_status
cmd_list_plugins(struct bt_config
*cfg
)
831 int plugins_count
, component_classes_count
= 0, i
;
833 printf("From the following plugin paths:\n\n");
834 print_value(stdout
, cfg
->plugin_paths
, 2);
836 plugins_count
= get_loaded_plugins_count();
837 if (plugins_count
== 0) {
838 printf("No plugins found.\n");
842 for (i
= 0; i
< plugins_count
; i
++) {
843 const bt_plugin
*plugin
= borrow_loaded_plugin_by_index(i
);
845 component_classes_count
+=
846 bt_plugin_get_source_component_class_count(plugin
) +
847 bt_plugin_get_filter_component_class_count(plugin
) +
848 bt_plugin_get_sink_component_class_count(plugin
);
851 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
852 bt_common_color_bold(),
853 component_classes_count
,
854 bt_common_color_reset(),
855 bt_common_color_bold(),
857 bt_common_color_reset());
859 for (i
= 0; i
< plugins_count
; i
++) {
860 const bt_plugin
*plugin
= borrow_loaded_plugin_by_index(i
);
863 print_plugin_info(plugin
);
864 cmd_list_plugins_print_component_classes(plugin
, "Source",
865 bt_plugin_get_source_component_class_count(plugin
),
866 (plugin_borrow_comp_cls_by_index_func_t
)
867 bt_plugin_borrow_source_component_class_by_index_const
,
868 (spec_comp_cls_borrow_comp_cls_func_t
)
869 bt_component_class_source_as_component_class
);
870 cmd_list_plugins_print_component_classes(plugin
, "Filter",
871 bt_plugin_get_filter_component_class_count(plugin
),
872 (plugin_borrow_comp_cls_by_index_func_t
)
873 bt_plugin_borrow_filter_component_class_by_index_const
,
874 (spec_comp_cls_borrow_comp_cls_func_t
)
875 bt_component_class_filter_as_component_class
);
876 cmd_list_plugins_print_component_classes(plugin
, "Sink",
877 bt_plugin_get_sink_component_class_count(plugin
),
878 (plugin_borrow_comp_cls_by_index_func_t
)
879 bt_plugin_borrow_sink_component_class_by_index_const
,
880 (spec_comp_cls_borrow_comp_cls_func_t
)
881 bt_component_class_sink_as_component_class
);
885 return BT_CMD_STATUS_OK
;
889 enum bt_cmd_status
cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
892 enum bt_cmd_status cmd_status
;
893 const bt_component_class
*comp_cls
= NULL
;
894 const bt_value
*results
= NULL
;
895 bt_value
*params
= NULL
;
896 const bt_value
*map
= NULL
;
897 const bt_value
*v
= NULL
;
898 static const char * const plugin_name
= "ctf";
899 static const char * const comp_cls_name
= "lttng-live";
900 static const bt_component_class_type comp_cls_type
=
901 BT_COMPONENT_CLASS_TYPE_SOURCE
;
902 uint64_t array_size
, i
;
903 const char *fail_reason
= NULL
;
904 FILE *out_stream
= stdout
;
906 BT_ASSERT(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
907 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
910 BT_CLI_LOGE_APPEND_CAUSE(
911 "Cannot find component class: plugin-name=\"%s\", "
912 "comp-cls-name=\"%s\", comp-cls-type=%s",
913 plugin_name
, comp_cls_name
,
914 bt_common_component_class_type_string(
915 BT_COMPONENT_CLASS_TYPE_SOURCE
));
919 params
= bt_value_map_create();
924 ret
= bt_value_map_insert_string_entry(params
, "url",
925 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
930 ret
= query(cfg
, comp_cls
, "sessions", params
,
931 &results
, &fail_reason
);
933 BT_CLI_LOGE_APPEND_CAUSE("Failed to query `sessions` object: %s",
940 if (!bt_value_is_array(results
)) {
941 BT_CLI_LOGE_APPEND_CAUSE(
942 "Expecting an array for LTTng live `sessions` query.");
946 if (cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->len
> 0) {
948 fopen(cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
,
951 BT_LOGE_ERRNO("Cannot open file for writing",
953 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
954 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
956 "Cannot open file for writing: path=\"%s\"",
957 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
962 array_size
= bt_value_array_get_length(results
);
963 for (i
= 0; i
< array_size
; i
++) {
964 const char *url_text
;
965 int64_t timer_us
, streams
, clients
;
967 map
= bt_value_array_borrow_element_by_index_const(results
, i
);
968 if (!bt_value_is_map(map
)) {
969 BT_CLI_LOGE_APPEND_CAUSE("Unexpected entry type.");
973 v
= bt_value_map_borrow_entry_value_const(map
, "url");
975 BT_CLI_LOGE_APPEND_CAUSE("Missing `url` entry.");
978 url_text
= bt_value_string_get(v
);
979 fprintf(out_stream
, "%s", url_text
);
980 v
= bt_value_map_borrow_entry_value_const(map
, "timer-us");
982 BT_CLI_LOGE_APPEND_CAUSE("Missing `timer-us` entry.");
985 timer_us
= bt_value_integer_unsigned_get(v
);
986 fprintf(out_stream
, " (timer = %" PRIu64
", ", timer_us
);
987 v
= bt_value_map_borrow_entry_value_const(map
, "stream-count");
989 BT_CLI_LOGE_APPEND_CAUSE(
990 "Missing `stream-count` entry.");
993 streams
= bt_value_integer_unsigned_get(v
);
994 fprintf(out_stream
, "%" PRIu64
" stream(s), ", streams
);
995 v
= bt_value_map_borrow_entry_value_const(map
, "client-count");
997 BT_CLI_LOGE_APPEND_CAUSE(
998 "Missing `client-count` entry.");
1001 clients
= bt_value_integer_unsigned_get(v
);
1002 fprintf(out_stream
, "%" PRIu64
" client(s) connected)\n", clients
);
1005 cmd_status
= BT_CMD_STATUS_OK
;
1009 cmd_status
= BT_CMD_STATUS_ERROR
;
1012 bt_value_put_ref(results
);
1013 bt_value_put_ref(params
);
1014 bt_component_class_put_ref(comp_cls
);
1016 if (out_stream
&& out_stream
!= stdout
) {
1017 int fclose_ret
= fclose(out_stream
);
1020 BT_LOGE_ERRNO("Cannot close file stream",
1022 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
->str
);
1030 enum bt_cmd_status
cmd_print_ctf_metadata(struct bt_config
*cfg
)
1033 enum bt_cmd_status cmd_status
;
1034 const bt_component_class
*comp_cls
= NULL
;
1035 const bt_value
*results
= NULL
;
1036 bt_value
*params
= NULL
;
1037 const bt_value
*metadata_text_value
= NULL
;
1038 const char *metadata_text
= NULL
;
1039 static const char * const plugin_name
= "ctf";
1040 static const char * const comp_cls_name
= "fs";
1041 static const bt_component_class_type comp_cls_type
=
1042 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1043 const char *fail_reason
= NULL
;
1044 FILE *out_stream
= stdout
;
1046 BT_ASSERT(cfg
->cmd_data
.print_ctf_metadata
.path
);
1047 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1050 BT_CLI_LOGE_APPEND_CAUSE(
1051 "Cannot find component class: plugin-name=\"%s\", "
1052 "comp-cls-name=\"%s\", comp-cls-type=%s",
1053 plugin_name
, comp_cls_name
,
1054 bt_common_component_class_type_string(
1055 BT_COMPONENT_CLASS_TYPE_SOURCE
));
1059 params
= bt_value_map_create();
1064 ret
= bt_value_map_insert_string_entry(params
, "path",
1065 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1070 ret
= query(cfg
, comp_cls
, "metadata-info",
1071 params
, &results
, &fail_reason
);
1073 BT_CLI_LOGE_APPEND_CAUSE(
1074 "Failed to query `metadata-info` object: %s", fail_reason
);
1078 metadata_text_value
= bt_value_map_borrow_entry_value_const(results
,
1080 if (!metadata_text_value
) {
1081 BT_CLI_LOGE_APPEND_CAUSE(
1082 "Cannot find `text` string value in the resulting metadata info object.");
1086 metadata_text
= bt_value_string_get(metadata_text_value
);
1088 if (cfg
->cmd_data
.print_ctf_metadata
.output_path
->len
> 0) {
1090 fopen(cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
,
1093 BT_LOGE_ERRNO("Cannot open file for writing",
1095 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1096 (void) BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
1098 "Cannot open file for writing: path=\"%s\"",
1099 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1104 ret
= fprintf(out_stream
, "%s\n", metadata_text
);
1106 BT_CLI_LOGE_APPEND_CAUSE(
1107 "Cannot write whole metadata text to output stream: "
1112 cmd_status
= BT_CMD_STATUS_OK
;
1116 cmd_status
= BT_CMD_STATUS_ERROR
;
1119 bt_value_put_ref(results
);
1120 bt_value_put_ref(params
);
1121 bt_component_class_put_ref(comp_cls
);
1123 if (out_stream
&& out_stream
!= stdout
) {
1124 int fclose_ret
= fclose(out_stream
);
1127 BT_LOGE_ERRNO("Cannot close file stream",
1129 cfg
->cmd_data
.print_ctf_metadata
.output_path
->str
);
1137 char *instance_name
;
1141 struct trace_range
{
1142 uint64_t intersection_range_begin_ns
;
1143 uint64_t intersection_range_end_ns
;
1147 guint
port_id_hash(gconstpointer v
)
1149 const struct port_id
*id
= v
;
1151 BT_ASSERT(id
->instance_name
);
1152 BT_ASSERT(id
->port_name
);
1154 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1158 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1160 const struct port_id
*id1
= v1
;
1161 const struct port_id
*id2
= v2
;
1163 return strcmp(id1
->instance_name
, id2
->instance_name
) == 0 &&
1164 strcmp(id1
->port_name
, id2
->port_name
) == 0;
1168 void port_id_destroy(gpointer data
)
1170 struct port_id
*id
= data
;
1172 free(id
->instance_name
);
1173 free(id
->port_name
);
1178 void trace_range_destroy(gpointer data
)
1183 struct cmd_run_ctx
{
1185 GHashTable
*src_components
;
1188 GHashTable
*flt_components
;
1191 GHashTable
*sink_components
;
1197 struct bt_config
*cfg
;
1201 bool stream_intersection_mode
;
1204 * Association of struct port_id -> struct trace_range.
1206 GHashTable
*intersections
;
1209 /* Returns a timestamp of the form "(-)s.ns" */
1211 char *s_from_ns(int64_t ns
)
1216 int64_t ts_sec_abs
, ts_nsec_abs
;
1217 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1218 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1220 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1221 is_negative
= false;
1222 ts_sec_abs
= ts_sec
;
1223 ts_nsec_abs
= ts_nsec
;
1224 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1225 is_negative
= false;
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 if (ts_sec
< 0 && ts_nsec
> 0) {
1234 ts_sec_abs
= -(ts_sec
+ 1);
1235 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1236 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1238 ts_sec_abs
= -ts_sec
;
1239 ts_nsec_abs
= ts_nsec
;
1240 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1242 ts_sec_abs
= -ts_sec
;
1243 ts_nsec_abs
= -ts_nsec
;
1246 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1247 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1255 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1256 struct cmd_run_ctx
*ctx
,
1257 const bt_component
*upstream_comp
,
1258 const bt_port_output
*out_upstream_port
,
1259 struct bt_config_connection
*cfg_conn
)
1261 typedef uint64_t (*input_port_count_func_t
)(void *);
1262 typedef const bt_port_input
*(*borrow_input_port_by_index_func_t
)(
1263 const void *, uint64_t);
1264 const bt_port
*upstream_port
=
1265 bt_port_output_as_port_const(out_upstream_port
);
1268 GQuark downstreamp_comp_name_quark
;
1269 void *downstream_comp
;
1270 uint64_t downstream_port_count
;
1272 input_port_count_func_t port_count_fn
;
1273 borrow_input_port_by_index_func_t port_by_index_fn
;
1274 bt_graph_connect_ports_status connect_ports_status
=
1275 BT_GRAPH_CONNECT_PORTS_STATUS_OK
;
1276 bool insert_trimmer
= false;
1277 bt_value
*trimmer_params
= NULL
;
1278 char *intersection_begin
= NULL
;
1279 char *intersection_end
= NULL
;
1280 const bt_component_filter
*trimmer
= NULL
;
1281 const bt_component_class_filter
*trimmer_class
= NULL
;
1282 const bt_port_input
*trimmer_input
= NULL
;
1283 const bt_port_output
*trimmer_output
= NULL
;
1285 if (ctx
->intersections
&&
1286 bt_component_get_class_type(upstream_comp
) ==
1287 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1288 struct trace_range
*range
;
1289 struct port_id port_id
= {
1290 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1291 .port_name
= (char *) bt_port_get_name(upstream_port
)
1294 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1298 range
= (struct trace_range
*) g_hash_table_lookup(
1299 ctx
->intersections
, &port_id
);
1301 bt_value_map_insert_entry_status insert_status
;
1303 intersection_begin
= s_from_ns(
1304 range
->intersection_range_begin_ns
);
1305 intersection_end
= s_from_ns(
1306 range
->intersection_range_end_ns
);
1307 if (!intersection_begin
|| !intersection_end
) {
1308 BT_CLI_LOGE_APPEND_CAUSE(
1309 "Cannot create trimmer argument timestamp string.");
1313 insert_trimmer
= true;
1314 trimmer_params
= bt_value_map_create();
1315 if (!trimmer_params
) {
1319 insert_status
= bt_value_map_insert_string_entry(
1320 trimmer_params
, "begin", intersection_begin
);
1321 if (insert_status
< 0) {
1324 insert_status
= bt_value_map_insert_string_entry(
1326 "end", intersection_end
);
1327 if (insert_status
< 0) {
1332 trimmer_class
= find_filter_component_class("utils", "trimmer");
1333 if (!trimmer_class
) {
1338 BT_LOGI("Connecting upstream port to the next available downstream port: "
1339 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1340 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1341 upstream_port
, bt_port_get_name(upstream_port
),
1342 cfg_conn
->downstream_comp_name
->str
,
1343 cfg_conn
->arg
->str
);
1344 downstreamp_comp_name_quark
= g_quark_from_string(
1345 cfg_conn
->downstream_comp_name
->str
);
1346 BT_ASSERT(downstreamp_comp_name_quark
> 0);
1347 downstream_comp
= g_hash_table_lookup(ctx
->flt_components
,
1348 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1349 port_count_fn
= (input_port_count_func_t
)
1350 bt_component_filter_get_input_port_count
;
1351 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1352 bt_component_filter_borrow_input_port_by_index_const
;
1354 if (!downstream_comp
) {
1355 downstream_comp
= g_hash_table_lookup(ctx
->sink_components
,
1356 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1357 port_count_fn
= (input_port_count_func_t
)
1358 bt_component_sink_get_input_port_count
;
1359 port_by_index_fn
= (borrow_input_port_by_index_func_t
)
1360 bt_component_sink_borrow_input_port_by_index_const
;
1363 if (!downstream_comp
) {
1364 BT_CLI_LOGE_APPEND_CAUSE("Cannot find downstream component: "
1365 "comp-name=\"%s\", conn-arg=\"%s\"",
1366 cfg_conn
->downstream_comp_name
->str
,
1367 cfg_conn
->arg
->str
);
1371 downstream_port_count
= port_count_fn(downstream_comp
);
1373 for (i
= 0; i
< downstream_port_count
; i
++) {
1374 const bt_port_input
*in_downstream_port
=
1375 port_by_index_fn(downstream_comp
, i
);
1376 const bt_port
*downstream_port
=
1377 bt_port_input_as_port_const(in_downstream_port
);
1378 const char *upstream_port_name
;
1379 const char *downstream_port_name
;
1381 BT_ASSERT(downstream_port
);
1383 /* Skip port if it's already connected. */
1384 if (bt_port_is_connected(downstream_port
)) {
1385 BT_LOGI("Skipping downstream port: already connected: "
1386 "port-addr=%p, port-name=\"%s\"",
1388 bt_port_get_name(downstream_port
));
1392 downstream_port_name
= bt_port_get_name(downstream_port
);
1393 BT_ASSERT(downstream_port_name
);
1394 upstream_port_name
= bt_port_get_name(upstream_port
);
1395 BT_ASSERT(upstream_port_name
);
1397 if (!bt_common_star_glob_match(
1398 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1399 downstream_port_name
, SIZE_MAX
)) {
1403 if (insert_trimmer
) {
1405 * In order to insert the trimmer between the
1406 * two components that were being connected, we
1407 * create a connection configuration entry which
1408 * describes a connection from the trimmer's
1409 * output to the original input that was being
1412 * Hence, the creation of the trimmer will cause
1413 * the graph "new port" listener to establish
1414 * all downstream connections as its output port
1415 * is connected. We will then establish the
1416 * connection between the original upstream
1417 * source and the trimmer.
1419 char *trimmer_name
= NULL
;
1420 bt_graph_add_component_status add_comp_status
;
1422 ret
= asprintf(&trimmer_name
,
1423 "stream-intersection-trimmer-%s",
1424 upstream_port_name
);
1430 ctx
->connect_ports
= false;
1431 add_comp_status
= bt_graph_add_filter_component(
1432 ctx
->graph
, trimmer_class
, trimmer_name
,
1433 trimmer_params
, ctx
->cfg
->log_level
,
1435 bt_component_filter_get_ref(trimmer
);
1437 if (add_comp_status
!=
1438 BT_GRAPH_ADD_COMPONENT_STATUS_OK
) {
1444 bt_component_filter_borrow_input_port_by_index_const(
1446 if (!trimmer_input
) {
1450 bt_component_filter_borrow_output_port_by_index_const(
1452 if (!trimmer_output
) {
1457 * Replace the current downstream port by the trimmer's
1460 in_downstream_port
= trimmer_input
;
1462 bt_port_input_as_port_const(in_downstream_port
);
1463 downstream_port_name
= bt_port_get_name(
1465 BT_ASSERT(downstream_port_name
);
1468 /* We have a winner! */
1469 connect_ports_status
= bt_graph_connect_ports(ctx
->graph
,
1470 out_upstream_port
, in_downstream_port
, NULL
);
1471 downstream_port
= NULL
;
1472 switch (connect_ports_status
) {
1473 case BT_GRAPH_CONNECT_PORTS_STATUS_OK
:
1476 BT_CLI_LOGE_APPEND_CAUSE(
1477 "Cannot create connection: graph refuses to connect ports: "
1478 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1479 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1480 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1481 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1483 upstream_comp
, bt_component_get_name(upstream_comp
),
1484 upstream_port
, bt_port_get_name(upstream_port
),
1485 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1486 downstream_port
, downstream_port_name
,
1487 cfg_conn
->arg
->str
);
1491 BT_LOGI("Connected component ports: "
1492 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1493 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1494 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1495 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1497 upstream_comp
, bt_component_get_name(upstream_comp
),
1498 upstream_port
, bt_port_get_name(upstream_port
),
1499 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1500 downstream_port
, downstream_port_name
,
1501 cfg_conn
->arg
->str
);
1503 if (insert_trimmer
) {
1505 * The first connection, from the source to the trimmer,
1506 * has been done. We now connect the trimmer to the
1507 * original downstream port.
1509 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1511 bt_component_filter_as_component_const(trimmer
),
1512 trimmer_output
, cfg_conn
);
1516 ctx
->connect_ports
= true;
1520 * We found a matching downstream port: the search is
1526 /* No downstream port found */
1527 BT_CLI_LOGE_APPEND_CAUSE(
1528 "Cannot create connection: cannot find a matching downstream port for upstream port: "
1529 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1530 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1531 upstream_port
, bt_port_get_name(upstream_port
),
1532 cfg_conn
->downstream_comp_name
->str
,
1533 cfg_conn
->arg
->str
);
1539 free(intersection_begin
);
1540 free(intersection_end
);
1541 BT_VALUE_PUT_REF_AND_RESET(trimmer_params
);
1542 BT_COMPONENT_CLASS_FILTER_PUT_REF_AND_RESET(trimmer_class
);
1543 BT_COMPONENT_FILTER_PUT_REF_AND_RESET(trimmer
);
1548 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1549 const bt_port_output
*upstream_port
)
1552 const char *upstream_port_name
;
1553 const char *upstream_comp_name
;
1554 const bt_component
*upstream_comp
= NULL
;
1558 BT_ASSERT(upstream_port
);
1559 upstream_port_name
= bt_port_get_name(
1560 bt_port_output_as_port_const(upstream_port
));
1561 BT_ASSERT(upstream_port_name
);
1562 upstream_comp
= bt_port_borrow_component_const(
1563 bt_port_output_as_port_const(upstream_port
));
1564 BT_ASSERT(upstream_comp
);
1565 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1566 BT_ASSERT(upstream_comp_name
);
1567 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1568 "port-addr=%p, port-name=\"%s\"",
1569 upstream_comp
, upstream_comp_name
,
1570 upstream_port
, upstream_port_name
);
1572 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1573 struct bt_config_connection
*cfg_conn
=
1575 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1577 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1578 upstream_comp_name
)) {
1582 if (!bt_common_star_glob_match(
1583 cfg_conn
->upstream_port_glob
->str
,
1584 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1588 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1589 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1591 BT_CLI_LOGE_APPEND_CAUSE(
1592 "Cannot connect upstream port: "
1593 "port-addr=%p, port-name=\"%s\"",
1595 upstream_port_name
);
1601 BT_CLI_LOGE_APPEND_CAUSE(
1602 "Cannot connect upstream port: port does not match any connection argument: "
1603 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1604 upstream_port_name
);
1614 bt_graph_listener_func_status
1615 graph_output_port_added_listener(struct cmd_run_ctx
*ctx
,
1616 const bt_port_output
*out_port
)
1618 const bt_component
*comp
;
1619 const bt_port
*port
= bt_port_output_as_port_const(out_port
);
1620 bt_graph_listener_func_status ret
=
1621 BT_GRAPH_LISTENER_FUNC_STATUS_OK
;
1623 comp
= bt_port_borrow_component_const(port
);
1624 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1625 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1626 comp
, comp
? bt_component_get_name(comp
) : "",
1627 port
, bt_port_get_name(port
));
1629 if (!ctx
->connect_ports
) {
1635 if (bt_port_is_connected(port
)) {
1636 BT_LOGW_STR("Port is already connected.");
1640 if (cmd_run_ctx_connect_upstream_port(ctx
, out_port
)) {
1641 BT_CLI_LOGE_APPEND_CAUSE("Cannot connect upstream port.");
1642 ret
= BT_GRAPH_LISTENER_FUNC_STATUS_ERROR
;
1651 bt_graph_listener_func_status
graph_source_output_port_added_listener(
1652 const bt_component_source
*component
__attribute__((unused
)),
1653 const bt_port_output
*port
, void *data
)
1655 return graph_output_port_added_listener(data
, port
);
1659 bt_graph_listener_func_status
graph_filter_output_port_added_listener(
1660 const bt_component_filter
*component
__attribute__((unused
)),
1661 const bt_port_output
*port
, void *data
)
1663 return graph_output_port_added_listener(data
, port
);
1667 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1673 if (ctx
->src_components
) {
1674 g_hash_table_destroy(ctx
->src_components
);
1675 ctx
->src_components
= NULL
;
1678 if (ctx
->flt_components
) {
1679 g_hash_table_destroy(ctx
->flt_components
);
1680 ctx
->flt_components
= NULL
;
1683 if (ctx
->sink_components
) {
1684 g_hash_table_destroy(ctx
->sink_components
);
1685 ctx
->sink_components
= NULL
;
1688 if (ctx
->intersections
) {
1689 g_hash_table_destroy(ctx
->intersections
);
1690 ctx
->intersections
= NULL
;
1693 BT_GRAPH_PUT_REF_AND_RESET(ctx
->graph
);
1698 int add_descriptor_to_component_descriptor_set(
1699 bt_component_descriptor_set
*comp_descr_set
,
1700 const char *plugin_name
, const char *comp_cls_name
,
1701 bt_component_class_type comp_cls_type
,
1702 const bt_value
*params
)
1704 const bt_component_class
*comp_cls
;
1707 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1710 BT_CLI_LOGE_APPEND_CAUSE(
1711 "Cannot find component class: plugin-name=\"%s\", "
1712 "comp-cls-name=\"%s\", comp-cls-type=%s",
1713 plugin_name
, comp_cls_name
,
1714 bt_common_component_class_type_string(comp_cls_type
));
1719 status
= bt_component_descriptor_set_add_descriptor(
1720 comp_descr_set
, comp_cls
, params
);
1721 if (status
!= BT_COMPONENT_DESCRIPTOR_SET_ADD_DESCRIPTOR_STATUS_OK
) {
1722 BT_CLI_LOGE_APPEND_CAUSE(
1723 "Cannot append descriptor to component descriptor set: "
1724 "status=%s", bt_common_func_status_string(status
));
1729 bt_component_class_put_ref(comp_cls
);
1734 int append_descriptors_from_bt_config_component_array(
1735 bt_component_descriptor_set
*comp_descr_set
,
1736 GPtrArray
*component_configs
)
1741 for (i
= 0; i
< component_configs
->len
; i
++) {
1742 struct bt_config_component
*cfg_comp
=
1743 component_configs
->pdata
[i
];
1745 ret
= add_descriptor_to_component_descriptor_set(
1747 cfg_comp
->plugin_name
->str
,
1748 cfg_comp
->comp_cls_name
->str
,
1749 cfg_comp
->type
, cfg_comp
->params
);
1760 bt_get_greatest_operative_mip_version_status
get_greatest_operative_mip_version(
1761 struct bt_config
*cfg
, uint64_t *mip_version
)
1763 bt_get_greatest_operative_mip_version_status status
=
1764 BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK
;
1765 bt_component_descriptor_set
*comp_descr_set
= NULL
;
1769 BT_ASSERT(mip_version
);
1770 comp_descr_set
= bt_component_descriptor_set_create();
1771 if (!comp_descr_set
) {
1772 BT_CLI_LOGE_APPEND_CAUSE(
1773 "Failed to create a component descriptor set object.");
1774 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_MEMORY_ERROR
;
1778 ret
= append_descriptors_from_bt_config_component_array(
1779 comp_descr_set
, cfg
->cmd_data
.run
.sources
);
1781 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1785 ret
= append_descriptors_from_bt_config_component_array(
1786 comp_descr_set
, cfg
->cmd_data
.run
.filters
);
1788 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1792 ret
= append_descriptors_from_bt_config_component_array(
1793 comp_descr_set
, cfg
->cmd_data
.run
.sinks
);
1795 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1799 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1801 * Stream intersection mode adds `flt.utils.trimmer`
1802 * components; we need to include this type of component
1803 * in the component descriptor set to get the real
1804 * greatest operative MIP version.
1806 ret
= add_descriptor_to_component_descriptor_set(
1807 comp_descr_set
, "utils", "trimmer",
1808 BT_COMPONENT_CLASS_TYPE_FILTER
, NULL
);
1810 status
= BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR
;
1815 status
= bt_get_greatest_operative_mip_version(comp_descr_set
,
1816 (bt_logging_level
) bt_cli_log_level
, mip_version
);
1819 bt_component_descriptor_set_put_ref(comp_descr_set
);
1824 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1827 bt_graph_add_listener_status add_listener_status
;
1828 bt_get_greatest_operative_mip_version_status mip_version_status
;
1829 uint64_t mip_version
= UINT64_C(-1);
1832 ctx
->connect_ports
= false;
1833 ctx
->src_components
= g_hash_table_new_full(g_direct_hash
,
1834 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1835 if (!ctx
->src_components
) {
1839 ctx
->flt_components
= g_hash_table_new_full(g_direct_hash
,
1840 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1841 if (!ctx
->flt_components
) {
1845 ctx
->sink_components
= g_hash_table_new_full(g_direct_hash
,
1846 g_direct_equal
, NULL
, (GDestroyNotify
) bt_object_put_ref
);
1847 if (!ctx
->sink_components
) {
1851 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1852 ctx
->stream_intersection_mode
= true;
1853 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1854 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1855 if (!ctx
->intersections
) {
1861 * Get the greatest operative MIP version to use to configure
1862 * the graph to create.
1864 mip_version_status
= get_greatest_operative_mip_version(
1866 if (mip_version_status
== BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_NO_MATCH
) {
1867 BT_CLI_LOGE_APPEND_CAUSE(
1868 "Failed to find an operative message interchange "
1869 "protocol version to use to create the `run` command's "
1870 "graph (components are not interoperable).");
1872 } else if (mip_version_status
< 0) {
1873 BT_CLI_LOGE_APPEND_CAUSE(
1874 "Cannot find an operative message interchange "
1875 "protocol version to use to create the `run` command's "
1877 bt_common_func_status_string(mip_version_status
));
1881 BT_ASSERT(mip_version_status
== BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK
);
1882 BT_LOGI("Found operative message interchange protocol version to "
1883 "configure the `run` command's graph: mip-version=%" PRIu64
,
1885 ctx
->graph
= bt_graph_create(mip_version
);
1890 bt_graph_add_interrupter(ctx
->graph
, the_interrupter
);
1891 add_listener_status
= bt_graph_add_source_component_output_port_added_listener(
1892 ctx
->graph
, graph_source_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.");
1900 add_listener_status
= bt_graph_add_filter_component_output_port_added_listener(
1901 ctx
->graph
, graph_filter_output_port_added_listener
, ctx
,
1903 if (add_listener_status
!= BT_GRAPH_ADD_LISTENER_STATUS_OK
) {
1904 BT_CLI_LOGE_APPEND_CAUSE(
1905 "Cannot add \"port added\" listener to graph.");
1912 cmd_run_ctx_destroy(ctx
);
1920 * Compute the intersection of all streams in the array `streams`, write it
1925 int compute_stream_intersection(const bt_value
*streams
,
1926 struct trace_range
*range
)
1928 uint64_t i
, stream_count
;
1931 BT_ASSERT(bt_value_get_type(streams
) == BT_VALUE_TYPE_ARRAY
);
1933 stream_count
= bt_value_array_get_length(streams
);
1935 BT_ASSERT(stream_count
> 0);
1937 range
->intersection_range_begin_ns
= 0;
1938 range
->intersection_range_end_ns
= UINT64_MAX
;
1940 for (i
= 0; i
< stream_count
; i
++) {
1941 int64_t begin_ns
, end_ns
;
1942 uint64_t begin_ns_u
, end_ns_u
;
1943 const bt_value
*stream_value
;
1944 const bt_value
*range_ns_value
;
1945 const bt_value
*begin_value
;
1946 const bt_value
*end_value
;
1948 stream_value
= bt_value_array_borrow_element_by_index_const(streams
, i
);
1949 if (bt_value_get_type(stream_value
) != BT_VALUE_TYPE_MAP
) {
1950 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1951 "expected streams array element to be a map, got %s.",
1952 bt_common_value_type_string(bt_value_get_type(stream_value
)));
1956 range_ns_value
= bt_value_map_borrow_entry_value_const(
1957 stream_value
, "range-ns");
1958 if (!range_ns_value
) {
1959 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1960 "missing expected `range-ns` key in stream map.");
1964 if (bt_value_get_type(range_ns_value
) != BT_VALUE_TYPE_MAP
) {
1965 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1966 "expected `range-ns` entry value of stream map to be a map, got %s.",
1967 bt_common_value_type_string(bt_value_get_type(range_ns_value
)));
1971 begin_value
= bt_value_map_borrow_entry_value_const(range_ns_value
, "begin");
1973 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1974 "missing expected `begin` key in range-ns map.");
1978 if (bt_value_get_type(begin_value
) != BT_VALUE_TYPE_SIGNED_INTEGER
) {
1979 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1980 "expected `begin` entry value of range-ns map to be a signed integer, got %s.",
1981 bt_common_value_type_string(bt_value_get_type(range_ns_value
)));
1985 end_value
= bt_value_map_borrow_entry_value_const(range_ns_value
, "end");
1987 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1988 "missing expected `end` key in range-ns map.");
1992 if (bt_value_get_type(end_value
) != BT_VALUE_TYPE_SIGNED_INTEGER
) {
1993 BT_CLI_LOGE_APPEND_CAUSE("Unexpected format of `babeltrace.trace-infos` query result: "
1994 "expected `end` entry value of range-ns map to be a signed integer, got %s.",
1995 bt_common_value_type_string(bt_value_get_type(range_ns_value
)));
1999 begin_ns
= bt_value_integer_signed_get(begin_value
);
2000 end_ns
= bt_value_integer_signed_get(end_value
);
2002 if (begin_ns
< 0 || end_ns
< 0 || end_ns
< begin_ns
) {
2003 BT_CLI_LOGE_APPEND_CAUSE(
2004 "Invalid stream range values: "
2005 "range-ns:begin=%" PRId64
", "
2006 "range-ns:end=%" PRId64
,
2011 begin_ns_u
= begin_ns
;
2014 range
->intersection_range_begin_ns
=
2015 MAX(range
->intersection_range_begin_ns
, begin_ns_u
);
2016 range
->intersection_range_end_ns
=
2017 MIN(range
->intersection_range_end_ns
, end_ns_u
);
2030 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
2031 struct bt_config_component
*cfg_comp
,
2032 const bt_component_class_source
*src_comp_cls
)
2036 uint64_t trace_count
;
2037 const bt_value
*query_result
= NULL
;
2038 const bt_value
*trace_info
= NULL
;
2039 const bt_value
*stream_infos
= NULL
;
2040 const bt_value
*stream_info
= NULL
;
2041 struct port_id
*port_id
= NULL
;
2042 struct trace_range
*stream_intersection
= NULL
;
2043 const char *fail_reason
= NULL
;
2044 const bt_component_class
*comp_cls
=
2045 bt_component_class_source_as_component_class_const(src_comp_cls
);
2047 ret
= query(ctx
->cfg
, comp_cls
, "babeltrace.trace-infos",
2048 cfg_comp
->params
, &query_result
,
2051 BT_CLI_LOGE_APPEND_CAUSE("Failed to execute `babeltrace.trace-infos` query: %s: "
2052 "comp-class-name=\"%s\"", fail_reason
,
2053 bt_component_class_get_name(comp_cls
));
2058 BT_ASSERT(query_result
);
2060 if (!bt_value_is_array(query_result
)) {
2061 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting result to be an array: "
2062 "component-class-name=%s, actual-type=%s",
2063 bt_component_class_get_name(comp_cls
),
2064 bt_common_value_type_string(bt_value_get_type(query_result
)));
2069 trace_count
= bt_value_array_get_length(query_result
);
2070 if (trace_count
== 0) {
2071 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: result is empty: "
2072 "component-class-name=%s", bt_component_class_get_name(comp_cls
));
2077 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
2078 uint64_t stream_idx
;
2079 int64_t stream_count
;
2080 struct trace_range trace_intersection
;
2082 trace_info
= bt_value_array_borrow_element_by_index_const(
2083 query_result
, trace_idx
);
2084 if (!bt_value_is_map(trace_info
)) {
2086 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting element to be a map: "
2087 "component-class-name=%s, actual-type=%s",
2088 bt_component_class_get_name(comp_cls
),
2089 bt_common_value_type_string(bt_value_get_type(trace_info
)));
2093 stream_infos
= bt_value_map_borrow_entry_value_const(
2094 trace_info
, "stream-infos");
2095 if (!stream_infos
) {
2097 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: missing `streams` key in trace info map: "
2098 "component-class-name=%s",
2099 bt_component_class_get_name(comp_cls
));
2103 if (!bt_value_is_array(stream_infos
)) {
2105 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: expecting `streams` entry of trace info map to be an array: "
2106 "component-class-name=%s, actual-type=%s",
2107 bt_component_class_get_name(comp_cls
),
2108 bt_common_value_type_string(bt_value_get_type(stream_infos
)));
2112 stream_count
= bt_value_array_get_length(stream_infos
);
2113 if (stream_count
== 0) {
2115 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: list of streams is empty: "
2116 "component-class-name=%s",
2117 bt_component_class_get_name(comp_cls
));
2121 ret
= compute_stream_intersection(stream_infos
, &trace_intersection
);
2123 BT_CLI_LOGE_APPEND_CAUSE("Failed to compute trace streams intersection.");
2127 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2128 const bt_value
*port_name
;
2130 port_id
= g_new0(struct port_id
, 1);
2133 BT_CLI_LOGE_APPEND_CAUSE(
2134 "Cannot allocate memory for port_id structure.");
2137 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2138 if (!port_id
->instance_name
) {
2140 BT_CLI_LOGE_APPEND_CAUSE(
2141 "Cannot allocate memory for port_id component instance name.");
2145 stream_intersection
= g_new0(struct trace_range
, 1);
2146 if (!stream_intersection
) {
2148 BT_CLI_LOGE_APPEND_CAUSE(
2149 "Cannot allocate memory for trace_range structure.");
2153 *stream_intersection
= trace_intersection
;
2155 stream_info
= bt_value_array_borrow_element_by_index_const(
2156 stream_infos
, stream_idx
);
2157 if (!bt_value_is_map(stream_info
)) {
2159 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
2160 "expecting element of stream list to be a map: "
2161 "component-class-name=%s, actual-type=%s",
2162 bt_component_class_get_name(comp_cls
),
2163 bt_common_value_type_string(bt_value_get_type(stream_info
)));
2167 port_name
= bt_value_map_borrow_entry_value_const(stream_info
, "port-name");
2170 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
2171 "missing `port-name` key in stream info map: "
2172 "component-class-name=%s",
2173 bt_component_class_get_name(comp_cls
));
2177 if (!bt_value_is_string(port_name
)) {
2179 BT_CLI_LOGE_APPEND_CAUSE("`babeltrace.trace-infos` query: "
2180 "expecting `port-name` entry of stream info map to be a string: "
2181 "component-class-name=%s, actual-type=%s",
2182 bt_component_class_get_name(comp_cls
),
2183 bt_common_value_type_string(bt_value_get_type(port_name
)));
2187 port_id
->port_name
= g_strdup(bt_value_string_get(port_name
));
2188 if (!port_id
->port_name
) {
2190 BT_CLI_LOGE_APPEND_CAUSE(
2191 "Cannot allocate memory for port_id port_name.");
2195 BT_LOGD("Inserting stream intersection ");
2197 g_hash_table_insert(ctx
->intersections
, port_id
, stream_intersection
);
2200 stream_intersection
= NULL
;
2207 bt_value_put_ref(query_result
);
2209 g_free(stream_intersection
);
2214 int cmd_run_ctx_create_components_from_config_components(
2215 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2218 const void *comp_cls
= NULL
;
2219 const void *comp
= NULL
;
2222 for (i
= 0; i
< cfg_components
->len
; i
++) {
2223 struct bt_config_component
*cfg_comp
=
2224 g_ptr_array_index(cfg_components
, i
);
2227 switch (cfg_comp
->type
) {
2228 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2229 comp_cls
= find_source_component_class(
2230 cfg_comp
->plugin_name
->str
,
2231 cfg_comp
->comp_cls_name
->str
);
2233 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2234 comp_cls
= find_filter_component_class(
2235 cfg_comp
->plugin_name
->str
,
2236 cfg_comp
->comp_cls_name
->str
);
2238 case BT_COMPONENT_CLASS_TYPE_SINK
:
2239 comp_cls
= find_sink_component_class(
2240 cfg_comp
->plugin_name
->str
,
2241 cfg_comp
->comp_cls_name
->str
);
2248 BT_CLI_LOGE_APPEND_CAUSE(
2249 "Cannot find component class: plugin-name=\"%s\", "
2250 "comp-cls-name=\"%s\", comp-cls-type=%s",
2251 cfg_comp
->plugin_name
->str
,
2252 cfg_comp
->comp_cls_name
->str
,
2253 bt_common_component_class_type_string(cfg_comp
->type
));
2257 BT_ASSERT(cfg_comp
->log_level
>= BT_LOG_TRACE
);
2259 switch (cfg_comp
->type
) {
2260 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2261 ret
= bt_graph_add_source_component(ctx
->graph
,
2262 comp_cls
, cfg_comp
->instance_name
->str
,
2263 cfg_comp
->params
, cfg_comp
->log_level
,
2265 bt_component_source_get_ref(comp
);
2267 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2268 ret
= bt_graph_add_filter_component(ctx
->graph
,
2269 comp_cls
, cfg_comp
->instance_name
->str
,
2270 cfg_comp
->params
, cfg_comp
->log_level
,
2272 bt_component_filter_get_ref(comp
);
2274 case BT_COMPONENT_CLASS_TYPE_SINK
:
2275 ret
= bt_graph_add_sink_component(ctx
->graph
,
2276 comp_cls
, cfg_comp
->instance_name
->str
,
2277 cfg_comp
->params
, cfg_comp
->log_level
,
2279 bt_component_sink_get_ref(comp
);
2286 BT_CLI_LOGE_APPEND_CAUSE(
2287 "Cannot create component: plugin-name=\"%s\", "
2288 "comp-cls-name=\"%s\", comp-cls-type=%s, "
2290 cfg_comp
->plugin_name
->str
,
2291 cfg_comp
->comp_cls_name
->str
,
2292 bt_common_component_class_type_string(cfg_comp
->type
),
2293 cfg_comp
->instance_name
->str
);
2297 if (ctx
->stream_intersection_mode
&&
2298 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2299 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2301 BT_CLI_LOGE_APPEND_CAUSE(
2302 "Cannot determine stream intersection of trace.");
2307 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2308 comp
, cfg_comp
->instance_name
->str
);
2309 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2310 BT_ASSERT(quark
> 0);
2312 switch (cfg_comp
->type
) {
2313 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2314 g_hash_table_insert(ctx
->src_components
,
2315 GUINT_TO_POINTER(quark
), (void *) comp
);
2317 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2318 g_hash_table_insert(ctx
->flt_components
,
2319 GUINT_TO_POINTER(quark
), (void *) comp
);
2321 case BT_COMPONENT_CLASS_TYPE_SINK
:
2322 g_hash_table_insert(ctx
->sink_components
,
2323 GUINT_TO_POINTER(quark
), (void *) comp
);
2330 BT_OBJECT_PUT_REF_AND_RESET(comp_cls
);
2339 bt_object_put_ref(comp
);
2340 bt_object_put_ref(comp_cls
);
2345 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2350 * Make sure that, during this phase, our graph's "port added"
2351 * listener does not connect ports while we are creating the
2352 * components because we have a special, initial phase for
2355 ctx
->connect_ports
= false;
2357 ret
= cmd_run_ctx_create_components_from_config_components(
2358 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2364 ret
= cmd_run_ctx_create_components_from_config_components(
2365 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2371 ret
= cmd_run_ctx_create_components_from_config_components(
2372 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2382 typedef uint64_t (*output_port_count_func_t
)(const void *);
2383 typedef const bt_port_output
*(*borrow_output_port_by_index_func_t
)(
2384 const void *, uint64_t);
2387 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2388 void *comp
, output_port_count_func_t port_count_fn
,
2389 borrow_output_port_by_index_func_t port_by_index_fn
)
2395 count
= port_count_fn(comp
);
2397 for (i
= 0; i
< count
; i
++) {
2398 const bt_port_output
*upstream_port
= port_by_index_fn(comp
, i
);
2400 BT_ASSERT(upstream_port
);
2401 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2412 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2415 GHashTableIter iter
;
2416 gpointer g_name_quark
, g_comp
;
2418 ctx
->connect_ports
= true;
2419 g_hash_table_iter_init(&iter
, ctx
->src_components
);
2421 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2422 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2423 (output_port_count_func_t
)
2424 bt_component_source_get_output_port_count
,
2425 (borrow_output_port_by_index_func_t
)
2426 bt_component_source_borrow_output_port_by_index_const
);
2432 g_hash_table_iter_init(&iter
, ctx
->flt_components
);
2434 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2435 ret
= cmd_run_ctx_connect_comp_ports(ctx
, g_comp
,
2436 (output_port_count_func_t
)
2437 bt_component_filter_get_output_port_count
,
2438 (borrow_output_port_by_index_func_t
)
2439 bt_component_filter_borrow_output_port_by_index_const
);
2450 enum bt_cmd_status
cmd_run(struct bt_config
*cfg
)
2452 enum bt_cmd_status cmd_status
;
2453 struct cmd_run_ctx ctx
= { 0 };
2455 /* Initialize the command's context and the graph object */
2456 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2457 BT_CLI_LOGE_APPEND_CAUSE(
2458 "Cannot initialize the command's context.");
2462 if (bt_interrupter_is_set(the_interrupter
)) {
2463 BT_CLI_LOGW_APPEND_CAUSE(
2464 "Interrupted by user before creating components.");
2468 BT_LOGI_STR("Creating components.");
2470 /* Create the requested component instances */
2471 if (cmd_run_ctx_create_components(&ctx
)) {
2472 BT_CLI_LOGE_APPEND_CAUSE("Cannot create components.");
2476 if (bt_interrupter_is_set(the_interrupter
)) {
2477 BT_CLI_LOGW_APPEND_CAUSE(
2478 "Interrupted by user before connecting components.");
2482 BT_LOGI_STR("Connecting components.");
2484 /* Connect the initially visible component ports */
2485 if (cmd_run_ctx_connect_ports(&ctx
)) {
2486 BT_CLI_LOGE_APPEND_CAUSE(
2487 "Cannot connect initial component ports.");
2491 BT_LOGI_STR("Running the graph.");
2495 bt_graph_run_status run_status
= bt_graph_run(ctx
.graph
);
2498 * Reset console in case something messed with console
2499 * codes during the graph's execution.
2501 printf("%s", bt_common_color_reset());
2503 fprintf(stderr
, "%s", bt_common_color_reset());
2504 BT_LOGT("bt_graph_run() returned: status=%s",
2505 bt_common_func_status_string(run_status
));
2507 switch (run_status
) {
2508 case BT_GRAPH_RUN_STATUS_OK
:
2509 cmd_status
= BT_CMD_STATUS_OK
;
2511 case BT_GRAPH_RUN_STATUS_AGAIN
:
2512 if (bt_interrupter_is_set(the_interrupter
)) {
2514 * The graph was interrupted by a SIGINT.
2516 cmd_status
= BT_CMD_STATUS_INTERRUPTED
;
2520 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2521 BT_LOGT("Got BT_GRAPH_RUN_STATUS_AGAIN: sleeping: "
2523 cfg
->cmd_data
.run
.retry_duration_us
);
2525 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2526 if (bt_interrupter_is_set(the_interrupter
)) {
2527 cmd_status
= BT_CMD_STATUS_INTERRUPTED
;
2534 if (bt_interrupter_is_set(the_interrupter
)) {
2535 cmd_status
= BT_CMD_STATUS_INTERRUPTED
;
2539 BT_CLI_LOGE_APPEND_CAUSE(
2540 "Graph failed to complete successfully");
2545 cmd_status
= BT_CMD_STATUS_ERROR
;
2548 cmd_run_ctx_destroy(&ctx
);
2553 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2555 const char *env_clash
;
2557 if (!cfg
->command_name
) {
2561 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2562 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2566 if (g_file_test(cfg
->command_name
,
2567 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2568 bt_log_write_printf(__FILE__
, __func__
, __LINE__
,
2569 BT_LOG_WARNING
, BT_LOG_TAG
,
2570 "The `%s` command was executed. "
2571 "If you meant to convert a trace located in "
2572 "the local `%s` directory, please use:\n\n"
2573 " babeltrace2 convert %s [OPTIONS]",
2574 cfg
->command_name
, cfg
->command_name
,
2580 void init_log_level(void)
2582 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2586 void print_error_causes(void)
2588 const bt_error
*error
= bt_current_thread_take_error();
2589 unsigned int columns
;
2590 gchar
*error_str
= NULL
;
2592 if (!error
|| bt_error_get_cause_count(error
) == 0) {
2593 fprintf(stderr
, "%s%sUnknown command-line error.%s\n",
2594 bt_common_color_bold(), bt_common_color_fg_bright_red(),
2595 bt_common_color_reset());
2599 /* Try to get terminal width to fold the error cause messages */
2600 if (bt_common_get_term_size(&columns
, NULL
) < 0) {
2601 /* Width not found: default to 80 */
2606 * This helps visually separate the error causes from the last
2607 * logging statement.
2609 fputc('\n', stderr
);
2611 error_str
= format_bt_error(error
, columns
,
2612 (bt_logging_level
) bt_cli_log_level
, BT_COMMON_COLOR_WHEN_AUTO
);
2613 BT_ASSERT(error_str
);
2615 fprintf(stderr
, "%s\n", error_str
);
2619 bt_error_release(error
);
2625 int main(int argc
, const char **argv
)
2628 enum bt_config_cli_args_status cli_args_status
;
2629 enum bt_cmd_status cmd_status
;
2630 struct bt_config
*cfg
= NULL
;
2633 set_signal_handler();
2634 init_loaded_plugins();
2636 BT_ASSERT(!the_interrupter
);
2637 the_interrupter
= bt_interrupter_create();
2638 if (!the_interrupter
) {
2639 BT_CLI_LOGE_APPEND_CAUSE("Failed to create an interrupter object.");
2640 retcode
= EXIT_FAILURE
;
2644 cli_args_status
= bt_config_cli_args_create_with_default(argc
, argv
, &cfg
,
2646 if (cli_args_status
== BT_CONFIG_CLI_ARGS_STATUS_INFO_ONLY
) {
2647 /* Quit without errors; typically usage/version */
2648 retcode
= EXIT_SUCCESS
;
2649 BT_LOGI_STR("Quitting without errors.");
2653 if (cli_args_status
== BT_CONFIG_CLI_ARGS_STATUS_ERROR
) {
2654 retcode
= EXIT_FAILURE
;
2655 BT_CLI_LOGE_APPEND_CAUSE(
2656 "Command-line error: retcode=%d", retcode
);
2660 BT_ASSERT(cli_args_status
== BT_CONFIG_CLI_ARGS_STATUS_OK
);
2665 if (cfg
->command_needs_plugins
) {
2666 int ret
= require_loaded_plugins(cfg
->plugin_paths
);
2669 BT_CLI_LOGE_APPEND_CAUSE(
2670 "Failed to load plugins: ret=%d", ret
);
2671 retcode
= EXIT_FAILURE
;
2676 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2677 cfg
->command
, cfg
->command_name
);
2679 switch (cfg
->command
) {
2680 case BT_CONFIG_COMMAND_RUN
:
2681 cmd_status
= cmd_run(cfg
);
2683 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2684 cmd_status
= cmd_list_plugins(cfg
);
2686 case BT_CONFIG_COMMAND_HELP
:
2687 cmd_status
= cmd_help(cfg
);
2689 case BT_CONFIG_COMMAND_QUERY
:
2690 cmd_status
= cmd_query(cfg
);
2692 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2693 cmd_status
= cmd_print_ctf_metadata(cfg
);
2695 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2696 cmd_status
= cmd_print_lttng_live_sessions(cfg
);
2699 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2703 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", command-status=\"%s\"",
2704 cfg
->command
, cfg
->command_name
, bt_cmd_status_string(cmd_status
));
2705 warn_command_name_and_directory_clash(cfg
);
2707 switch (cmd_status
) {
2708 case BT_CMD_STATUS_OK
:
2709 retcode
= EXIT_SUCCESS
;
2711 case BT_CMD_STATUS_ERROR
:
2712 retcode
= EXIT_FAILURE
;
2714 case BT_CMD_STATUS_INTERRUPTED
:
2715 retcode
= EXIT_INTERRUPTED
;
2718 BT_LOGF("Invalid command status: cmd-status=%d", cmd_status
);
2723 if (retcode
== EXIT_FAILURE
) {
2724 print_error_causes();
2727 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2728 fini_loaded_plugins();
2729 bt_interrupter_put_ref(the_interrupter
);
2732 * Clear current thread's error in case there is one to avoid a
2735 bt_current_thread_clear_error();