4 * Babeltrace Trace Converter
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define BT_LOG_TAG "CLI"
32 #include <babeltrace/babeltrace.h>
33 #include <babeltrace/plugin/plugin.h>
34 #include <babeltrace/common-internal.h>
35 #include <babeltrace/graph/component.h>
36 #include <babeltrace/graph/component-source.h>
37 #include <babeltrace/graph/component-sink.h>
38 #include <babeltrace/graph/component-filter.h>
39 #include <babeltrace/graph/component-class.h>
40 #include <babeltrace/graph/port.h>
41 #include <babeltrace/graph/graph.h>
42 #include <babeltrace/graph/connection.h>
43 #include <babeltrace/graph/notification-iterator.h>
44 #include <babeltrace/ref.h>
45 #include <babeltrace/values.h>
46 #include <babeltrace/values-internal.h>
47 #include <babeltrace/logging.h>
57 #include "babeltrace-cfg.h"
58 #include "babeltrace-cfg-cli-args.h"
59 #include "babeltrace-cfg-cli-args-default.h"
61 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
62 #define ENV_BABELTRACE_CLI_LOG_LEVEL "BABELTRACE_CLI_LOG_LEVEL"
63 #define NSEC_PER_SEC 1000000000LL
66 * Known environment variable names for the log levels of the project's
69 static const char* log_level_env_var_names
[] = {
70 "BABELTRACE_COMMON_LOG_LEVEL",
71 "BABELTRACE_PLUGIN_CTF_BTR_LOG_LEVEL",
72 "BABELTRACE_PLUGIN_CTF_FS_SRC_LOG_LEVEL",
73 "BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_SRC_LOG_LEVEL",
74 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
75 "BABELTRACE_PLUGIN_CTF_NOTIF_ITER_LOG_LEVEL",
76 "BABELTRACE_PLUGIN_LTTNG_UTILS_DEBUG_INFO_FLT_LOG_LEVEL",
77 "BABELTRACE_PLUGIN_TEXT_DMESG_SRC_LOG_LEVEL",
78 "BABELTRACE_PLUGIN_UTILS_MUXER_FLT_LOG_LEVEL",
79 "BABELTRACE_PLUGIN_UTILS_TRIMMER_FLT_LOG_LEVEL",
80 "BABELTRACE_PLUGIN_CTFCOPYTRACE_LIB_LOG_LEVEL",
81 "BABELTRACE_PLUGIN_CTF_FS_SINK_LOG_LEVEL",
82 "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
86 /* Application's processing graph (weak) */
87 static struct bt_graph
*the_graph
;
88 static bool canceled
= false;
90 GPtrArray
*loaded_plugins
;
93 void sigint_handler(int signum
)
95 if (signum
!= SIGINT
) {
100 bt_graph_cancel(the_graph
);
107 void init_static_data(void)
109 loaded_plugins
= g_ptr_array_new_with_free_func(bt_put
);
113 void fini_static_data(void)
115 g_ptr_array_free(loaded_plugins
, TRUE
);
119 struct bt_plugin
*find_plugin(const char *name
)
122 struct bt_plugin
*plugin
= NULL
;
125 BT_LOGD("Finding plugin: name=\"%s\"", name
);
127 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
128 plugin
= g_ptr_array_index(loaded_plugins
, i
);
130 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
137 if (BT_LOG_ON_DEBUG
) {
139 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
141 BT_LOGD("Cannot find plugin.");
145 return bt_get(plugin
);
149 struct bt_component_class
*find_component_class(const char *plugin_name
,
150 const char *comp_class_name
,
151 enum bt_component_class_type comp_class_type
)
153 struct bt_component_class
*comp_class
= NULL
;
154 struct bt_plugin
*plugin
;
156 BT_LOGD("Finding component class: plugin-name=\"%s\", "
157 "comp-cls-name=\"%s\", comp-cls-type=%d",
158 plugin_name
, comp_class_name
, comp_class_type
);
160 plugin
= find_plugin(plugin_name
);
166 comp_class
= bt_plugin_get_component_class_by_name_and_type(plugin
,
167 comp_class_name
, comp_class_type
);
171 if (BT_LOG_ON_DEBUG
) {
173 BT_LOGD("Found component class: comp-cls-addr=%p",
176 BT_LOGD("Cannot find component class.");
184 void print_indent(FILE *fp
, size_t indent
)
188 for (i
= 0; i
< indent
; i
++) {
194 const char *component_type_str(enum bt_component_class_type type
)
197 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
199 case BT_COMPONENT_CLASS_TYPE_SINK
:
201 case BT_COMPONENT_CLASS_TYPE_FILTER
:
203 case BT_COMPONENT_CLASS_TYPE_UNKNOWN
:
210 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
211 const char *comp_cls_name
, enum bt_component_class_type type
)
213 GString
*shell_plugin_name
= NULL
;
214 GString
*shell_comp_cls_name
= NULL
;
216 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
217 if (!shell_plugin_name
) {
221 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
222 if (!shell_comp_cls_name
) {
226 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
227 bt_common_color_bold(),
228 bt_common_color_fg_cyan(),
229 component_type_str(type
),
230 bt_common_color_fg_default(),
231 bt_common_color_fg_blue(),
232 shell_plugin_name
->str
,
233 bt_common_color_fg_default(),
234 bt_common_color_fg_yellow(),
235 shell_comp_cls_name
->str
,
236 bt_common_color_reset());
239 if (shell_plugin_name
) {
240 g_string_free(shell_plugin_name
, TRUE
);
243 if (shell_comp_cls_name
) {
244 g_string_free(shell_comp_cls_name
, TRUE
);
249 void print_value(FILE *, struct bt_value
*, size_t);
252 void print_value_rec(FILE *, struct bt_value
*, size_t);
254 struct print_map_value_data
{
260 bt_bool
print_map_value(const char *key
, struct bt_value
*object
, void *data
)
262 struct print_map_value_data
*print_map_value_data
= data
;
264 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
265 fprintf(print_map_value_data
->fp
, "%s: ", key
);
267 if (bt_value_is_array(object
) &&
268 bt_value_array_is_empty(object
)) {
269 fprintf(print_map_value_data
->fp
, "[ ]\n");
273 if (bt_value_is_map(object
) &&
274 bt_value_map_is_empty(object
)) {
275 fprintf(print_map_value_data
->fp
, "{ }\n");
279 if (bt_value_is_array(object
) ||
280 bt_value_is_map(object
)) {
281 fprintf(print_map_value_data
->fp
, "\n");
284 print_value_rec(print_map_value_data
->fp
, object
,
285 print_map_value_data
->indent
+ 2);
290 void print_value_rec(FILE *fp
, struct bt_value
*value
, size_t indent
)
298 enum bt_value_status status
;
304 switch (bt_value_get_type(value
)) {
305 case BT_VALUE_TYPE_NULL
:
306 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
307 bt_common_color_reset());
309 case BT_VALUE_TYPE_BOOL
:
310 status
= bt_value_bool_get(value
, &bool_val
);
311 if (status
!= BT_VALUE_STATUS_OK
) {
314 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
315 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
316 bt_common_color_reset());
318 case BT_VALUE_TYPE_INTEGER
:
319 status
= bt_value_integer_get(value
, &int_val
);
320 if (status
!= BT_VALUE_STATUS_OK
) {
323 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
324 bt_common_color_fg_red(), int_val
,
325 bt_common_color_reset());
327 case BT_VALUE_TYPE_FLOAT
:
328 status
= bt_value_float_get(value
, &dbl_val
);
329 if (status
!= BT_VALUE_STATUS_OK
) {
332 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
333 bt_common_color_fg_red(), dbl_val
,
334 bt_common_color_reset());
336 case BT_VALUE_TYPE_STRING
:
337 status
= bt_value_string_get(value
, &str_val
);
338 if (status
!= BT_VALUE_STATUS_OK
) {
341 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
342 bt_common_color_fg_green(), str_val
,
343 bt_common_color_reset());
345 case BT_VALUE_TYPE_ARRAY
:
346 size
= bt_value_array_size(value
);
352 print_indent(fp
, indent
);
353 fprintf(fp
, "[ ]\n");
357 for (i
= 0; i
< size
; i
++) {
358 struct bt_value
*element
=
359 bt_value_array_get(value
, i
);
364 print_indent(fp
, indent
);
367 if (bt_value_is_array(element
) &&
368 bt_value_array_is_empty(element
)) {
369 fprintf(fp
, "[ ]\n");
373 if (bt_value_is_map(element
) &&
374 bt_value_map_is_empty(element
)) {
375 fprintf(fp
, "{ }\n");
379 if (bt_value_is_array(element
) ||
380 bt_value_is_map(element
)) {
384 print_value_rec(fp
, element
, indent
+ 2);
388 case BT_VALUE_TYPE_MAP
:
390 struct print_map_value_data data
= {
395 if (bt_value_map_is_empty(value
)) {
396 print_indent(fp
, indent
);
397 fprintf(fp
, "{ }\n");
401 bt_value_map_foreach(value
, print_map_value
, &data
);
410 BT_LOGE("Error printing value of type %s.",
411 bt_value_type_string(bt_value_get_type(value
)));
415 void print_value(FILE *fp
, struct bt_value
*value
, size_t indent
)
417 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
418 print_indent(fp
, indent
);
421 print_value_rec(fp
, value
, indent
);
425 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
427 fprintf(stderr
, " ");
428 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
429 bt_config_component
->comp_cls_name
->str
,
430 bt_config_component
->type
);
431 fprintf(stderr
, ":\n");
433 if (bt_config_component
->instance_name
->len
> 0) {
434 fprintf(stderr
, " Name: %s\n",
435 bt_config_component
->instance_name
->str
);
438 fprintf(stderr
, " Parameters:\n");
439 print_value(stderr
, bt_config_component
->params
, 8);
443 void print_bt_config_components(GPtrArray
*array
)
447 for (i
= 0; i
< array
->len
; i
++) {
448 struct bt_config_component
*cfg_component
=
449 bt_config_get_component(array
, i
);
450 print_bt_config_component(cfg_component
);
451 BT_PUT(cfg_component
);
456 void print_plugin_paths(struct bt_value
*plugin_paths
)
458 fprintf(stderr
, " Plugin paths:\n");
459 print_value(stderr
, plugin_paths
, 4);
463 void print_cfg_run(struct bt_config
*cfg
)
467 print_plugin_paths(cfg
->plugin_paths
);
468 fprintf(stderr
, " Source component instances:\n");
469 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
471 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
472 fprintf(stderr
, " Filter component instances:\n");
473 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
476 fprintf(stderr
, " Sink component instances:\n");
477 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
478 fprintf(stderr
, " Connections:\n");
480 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
481 struct bt_config_connection
*cfg_connection
=
482 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
485 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
486 cfg_connection
->upstream_comp_name
->str
,
487 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
488 cfg_connection
->upstream_port_glob
->str
,
489 cfg_connection
->downstream_comp_name
->str
,
490 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
491 cfg_connection
->downstream_port_glob
->str
);
496 void print_cfg_list_plugins(struct bt_config
*cfg
)
498 print_plugin_paths(cfg
->plugin_paths
);
502 void print_cfg_help(struct bt_config
*cfg
)
504 print_plugin_paths(cfg
->plugin_paths
);
508 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
510 print_plugin_paths(cfg
->plugin_paths
);
511 fprintf(stderr
, " Path: %s\n",
512 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
516 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
518 print_plugin_paths(cfg
->plugin_paths
);
519 fprintf(stderr
, " URL: %s\n",
520 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
524 void print_cfg_query(struct bt_config
*cfg
)
526 print_plugin_paths(cfg
->plugin_paths
);
527 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
528 fprintf(stderr
, " Component class:\n");
529 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
533 void print_cfg(struct bt_config
*cfg
)
535 if (!BT_LOG_ON_INFO
) {
539 BT_LOGI_STR("Configuration:");
540 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
541 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
543 switch (cfg
->command
) {
544 case BT_CONFIG_COMMAND_RUN
:
547 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
548 print_cfg_list_plugins(cfg
);
550 case BT_CONFIG_COMMAND_HELP
:
553 case BT_CONFIG_COMMAND_QUERY
:
554 print_cfg_query(cfg
);
556 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
557 print_cfg_print_ctf_metadata(cfg
);
559 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
560 print_cfg_print_lttng_live_sessions(cfg
);
568 void add_to_loaded_plugins(struct bt_plugin_set
*plugin_set
)
573 count
= bt_plugin_set_get_plugin_count(plugin_set
);
576 for (i
= 0; i
< count
; i
++) {
577 struct bt_plugin
*plugin
=
578 bt_plugin_set_get_plugin(plugin_set
, i
);
579 struct bt_plugin
*loaded_plugin
=
580 find_plugin(bt_plugin_get_name(plugin
));
585 BT_LOGI("Not using plugin: another one already exists with the same name: "
586 "plugin-name=\"%s\", plugin-path=\"%s\", "
587 "existing-plugin-path=\"%s\"",
588 bt_plugin_get_name(plugin
),
589 bt_plugin_get_path(plugin
),
590 bt_plugin_get_path(loaded_plugin
));
591 bt_put(loaded_plugin
);
593 /* Add to global array. */
594 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
595 bt_plugin_get_name(plugin
));
596 g_ptr_array_add(loaded_plugins
, bt_get(plugin
));
604 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
606 int nr_paths
, i
, ret
= 0;
608 nr_paths
= bt_value_array_size(plugin_paths
);
610 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
615 BT_LOGI("Loading dynamic plugins.");
617 for (i
= 0; i
< nr_paths
; i
++) {
618 struct bt_value
*plugin_path_value
= NULL
;
619 const char *plugin_path
;
620 struct bt_plugin_set
*plugin_set
;
621 enum bt_value_status status
;
623 plugin_path_value
= bt_value_array_get(plugin_paths
, i
);
624 status
= bt_value_string_get(plugin_path_value
, &plugin_path
);
625 if (status
!= BT_VALUE_STATUS_OK
) {
626 BT_LOGD_STR("Cannot get plugin path string.");
627 BT_PUT(plugin_path_value
);
632 * Skip this if the directory does not exist because
633 * bt_plugin_create_all_from_dir() expects an existing
636 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
637 BT_LOGV("Skipping nonexistent directory path: "
638 "path=\"%s\"", plugin_path
);
639 BT_PUT(plugin_path_value
);
643 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
645 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
647 BT_PUT(plugin_path_value
);
651 add_to_loaded_plugins(plugin_set
);
653 BT_PUT(plugin_path_value
);
660 int load_static_plugins(void)
663 struct bt_plugin_set
*plugin_set
;
665 BT_LOGI("Loading static plugins.");
666 plugin_set
= bt_plugin_create_all_from_static();
668 BT_LOGE("Unable to load static plugins.");
673 add_to_loaded_plugins(plugin_set
);
680 int load_all_plugins(struct bt_value
*plugin_paths
)
684 if (load_dynamic_plugins(plugin_paths
)) {
689 if (load_static_plugins()) {
694 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
701 void print_plugin_info(struct bt_plugin
*plugin
)
703 unsigned int major
, minor
, patch
;
705 enum bt_plugin_status version_status
;
706 const char *plugin_name
;
710 const char *plugin_description
;
712 plugin_name
= bt_plugin_get_name(plugin
);
713 path
= bt_plugin_get_path(plugin
);
714 author
= bt_plugin_get_author(plugin
);
715 license
= bt_plugin_get_license(plugin
);
716 plugin_description
= bt_plugin_get_description(plugin
);
717 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
719 printf("%s%s%s%s:\n", bt_common_color_bold(),
720 bt_common_color_fg_blue(), plugin_name
,
721 bt_common_color_reset());
722 printf(" %sPath%s: %s\n", bt_common_color_bold(),
723 bt_common_color_reset(), path
? path
: "(None)");
725 if (version_status
== BT_PLUGIN_STATUS_OK
) {
726 printf(" %sVersion%s: %u.%u.%u",
727 bt_common_color_bold(), bt_common_color_reset(),
728 major
, minor
, patch
);
737 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
738 bt_common_color_reset(),
739 plugin_description
? plugin_description
: "(None)");
740 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
741 bt_common_color_reset(), author
? author
: "(Unknown)");
742 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
743 bt_common_color_reset(),
744 license
? license
: "(Unknown)");
748 int cmd_query(struct bt_config
*cfg
)
751 struct bt_component_class
*comp_cls
= NULL
;
752 struct bt_value
*results
= NULL
;
754 comp_cls
= find_component_class(cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
755 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
756 cfg
->cmd_data
.query
.cfg_component
->type
);
758 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
759 "comp-cls-name=\"%s\", comp-cls-type=%d",
760 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
761 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
762 cfg
->cmd_data
.query
.cfg_component
->type
);
763 fprintf(stderr
, "%s%sCannot find component class %s",
764 bt_common_color_bold(),
765 bt_common_color_fg_red(),
766 bt_common_color_reset());
767 print_plugin_comp_cls_opt(stderr
,
768 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
769 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
770 cfg
->cmd_data
.query
.cfg_component
->type
);
771 fprintf(stderr
, "\n");
776 results
= bt_component_class_query(comp_cls
,
777 cfg
->cmd_data
.query
.object
->str
,
778 cfg
->cmd_data
.query
.cfg_component
->params
);
780 BT_LOGE("Failed to query component class: plugin-name=\"%s\", "
781 "comp-cls-name=\"%s\", comp-cls-type=%d "
783 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
784 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
785 cfg
->cmd_data
.query
.cfg_component
->type
,
786 cfg
->cmd_data
.query
.object
->str
);
787 fprintf(stderr
, "%s%sFailed to query info to %s",
788 bt_common_color_bold(),
789 bt_common_color_fg_red(),
790 bt_common_color_reset());
791 print_plugin_comp_cls_opt(stderr
,
792 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
793 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
794 cfg
->cmd_data
.query
.cfg_component
->type
);
795 fprintf(stderr
, "%s%s with object `%s`%s\n",
796 bt_common_color_bold(),
797 bt_common_color_fg_red(),
798 cfg
->cmd_data
.query
.object
->str
,
799 bt_common_color_reset());
804 print_value(stdout
, results
, 0);
813 int cmd_help(struct bt_config
*cfg
)
816 struct bt_plugin
*plugin
= NULL
;
819 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
821 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
822 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
823 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
824 bt_common_color_bold(), bt_common_color_fg_red(),
825 bt_common_color_fg_blue(),
826 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
827 bt_common_color_reset());
832 print_plugin_info(plugin
);
833 printf(" %sComponent classes%s: %d\n",
834 bt_common_color_bold(),
835 bt_common_color_reset(),
836 (int) bt_plugin_get_component_class_count(plugin
));
839 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
840 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
841 struct bt_component_class
*needed_comp_cls
=
842 find_component_class(
843 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
844 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
845 cfg
->cmd_data
.help
.cfg_component
->type
);
847 if (!needed_comp_cls
) {
848 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
849 "comp-cls-name=\"%s\", comp-cls-type=%d",
850 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
851 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
852 cfg
->cmd_data
.help
.cfg_component
->type
);
853 fprintf(stderr
, "\n%s%sCannot find component class %s",
854 bt_common_color_bold(),
855 bt_common_color_fg_red(),
856 bt_common_color_reset());
857 print_plugin_comp_cls_opt(stderr
,
858 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
859 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
860 cfg
->cmd_data
.help
.cfg_component
->type
);
861 fprintf(stderr
, "\n");
866 bt_put(needed_comp_cls
);
869 for (i
= 0; i
< bt_plugin_get_component_class_count(plugin
); i
++) {
870 struct bt_component_class
*comp_cls
=
871 bt_plugin_get_component_class_by_index(plugin
, i
);
872 const char *comp_class_name
=
873 bt_component_class_get_name(comp_cls
);
874 const char *comp_class_description
=
875 bt_component_class_get_description(comp_cls
);
876 const char *comp_class_help
=
877 bt_component_class_get_help(comp_cls
);
878 enum bt_component_class_type type
=
879 bt_component_class_get_type(comp_cls
);
883 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
884 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
885 if (strcmp(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
886 comp_class_name
) != 0 &&
888 cfg
->cmd_data
.help
.cfg_component
->type
) {
895 print_plugin_comp_cls_opt(stdout
,
896 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
900 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
901 bt_common_color_reset(),
902 comp_class_description
? comp_class_description
: "(None)");
904 if (comp_class_help
) {
905 printf("\n%s\n", comp_class_help
);
917 int cmd_list_plugins(struct bt_config
*cfg
)
920 int plugins_count
, component_classes_count
= 0, i
;
922 printf("From the following plugin paths:\n\n");
923 print_value(stdout
, cfg
->plugin_paths
, 2);
925 plugins_count
= loaded_plugins
->len
;
926 if (plugins_count
== 0) {
927 printf("No plugins found.\n");
931 for (i
= 0; i
< plugins_count
; i
++) {
932 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
934 component_classes_count
+= bt_plugin_get_component_class_count(plugin
);
937 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
938 bt_common_color_bold(),
939 component_classes_count
,
940 bt_common_color_reset(),
941 bt_common_color_bold(),
943 bt_common_color_reset());
945 for (i
= 0; i
< plugins_count
; i
++) {
947 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
949 component_classes_count
=
950 bt_plugin_get_component_class_count(plugin
);
952 print_plugin_info(plugin
);
954 if (component_classes_count
== 0) {
955 printf(" %sComponent classes%s: (none)\n",
956 bt_common_color_bold(),
957 bt_common_color_reset());
959 printf(" %sComponent classes%s:\n",
960 bt_common_color_bold(),
961 bt_common_color_reset());
964 for (j
= 0; j
< component_classes_count
; j
++) {
965 struct bt_component_class
*comp_class
=
966 bt_plugin_get_component_class_by_index(
968 const char *comp_class_name
=
969 bt_component_class_get_name(comp_class
);
970 const char *comp_class_description
=
971 bt_component_class_get_description(comp_class
);
972 enum bt_component_class_type type
=
973 bt_component_class_get_type(comp_class
);
976 print_plugin_comp_cls_opt(stdout
,
977 bt_plugin_get_name(plugin
), comp_class_name
,
980 if (comp_class_description
) {
981 printf(": %s", comp_class_description
);
994 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
997 struct bt_component_class
*comp_cls
= NULL
;
998 struct bt_value
*results
= NULL
;
999 struct bt_value
*params
= NULL
;
1000 struct bt_value
*map
= NULL
;
1001 struct bt_value
*v
= NULL
;
1002 static const char * const plugin_name
= "ctf";
1003 static const char * const comp_cls_name
= "lttng-live";
1004 static const enum bt_component_class_type comp_cls_type
=
1005 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1006 int64_t array_size
, i
;
1008 assert(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
1009 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1012 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1013 "comp-cls-name=\"%s\", comp-cls-type=%d",
1014 plugin_name
, comp_cls_name
,
1015 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1016 fprintf(stderr
, "%s%sCannot find component class %s",
1017 bt_common_color_bold(),
1018 bt_common_color_fg_red(),
1019 bt_common_color_reset());
1020 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1021 comp_cls_name
, comp_cls_type
);
1022 fprintf(stderr
, "\n");
1026 params
= bt_value_map_create();
1031 ret
= bt_value_map_insert_string(params
, "url",
1032 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1037 results
= bt_component_class_query(comp_cls
, "sessions",
1040 BT_LOGE_STR("Failed to query for sessions.");
1041 fprintf(stderr
, "%s%sFailed to request sessions%s\n",
1042 bt_common_color_bold(),
1043 bt_common_color_fg_red(),
1044 bt_common_color_reset());
1048 if (!bt_value_is_array(results
)) {
1049 BT_LOGE_STR("Expecting an array for sessions query.");
1050 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1051 bt_common_color_bold(),
1052 bt_common_color_fg_red(),
1053 bt_common_color_reset());
1057 array_size
= bt_value_array_size(results
);
1058 for (i
= 0; i
< array_size
; i
++) {
1059 const char *url_text
;
1060 int64_t timer_us
, streams
, clients
;
1062 map
= bt_value_array_get(results
, i
);
1064 BT_LOGE_STR("Unexpected empty array entry.");
1067 if (!bt_value_is_map(map
)) {
1068 BT_LOGE_STR("Unexpected entry type.");
1072 v
= bt_value_map_get(map
, "url");
1074 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1077 ret
= bt_value_string_get(v
, &url_text
);
1079 printf("%s", url_text
);
1082 v
= bt_value_map_get(map
, "timer-us");
1084 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1087 ret
= bt_value_integer_get(v
, &timer_us
);
1089 printf(" (timer = %" PRIu64
", ", timer_us
);
1092 v
= bt_value_map_get(map
, "stream-count");
1094 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1097 ret
= bt_value_integer_get(v
, &streams
);
1099 printf("%" PRIu64
" stream(s), ", streams
);
1102 v
= bt_value_map_get(map
, "client-count");
1104 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1107 ret
= bt_value_integer_get(v
, &clients
);
1109 printf("%" PRIu64
" client(s) connected)\n", clients
);
1128 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1131 struct bt_component_class
*comp_cls
= NULL
;
1132 struct bt_value
*results
= NULL
;
1133 struct bt_value
*params
= NULL
;
1134 struct bt_value
*metadata_text_value
= NULL
;
1135 const char *metadata_text
= NULL
;
1136 static const char * const plugin_name
= "ctf";
1137 static const char * const comp_cls_name
= "fs";
1138 static const enum bt_component_class_type comp_cls_type
=
1139 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1141 assert(cfg
->cmd_data
.print_ctf_metadata
.path
);
1142 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1145 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1146 "comp-cls-name=\"%s\", comp-cls-type=%d",
1147 plugin_name
, comp_cls_name
,
1148 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1149 fprintf(stderr
, "%s%sCannot find component class %s",
1150 bt_common_color_bold(),
1151 bt_common_color_fg_red(),
1152 bt_common_color_reset());
1153 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1154 comp_cls_name
, comp_cls_type
);
1155 fprintf(stderr
, "\n");
1160 params
= bt_value_map_create();
1166 ret
= bt_value_map_insert_string(params
, "path",
1167 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1173 results
= bt_component_class_query(comp_cls
, "metadata-info",
1177 BT_LOGE_STR("Failed to query for metadata info.");
1178 fprintf(stderr
, "%s%sFailed to request metadata info%s\n",
1179 bt_common_color_bold(),
1180 bt_common_color_fg_red(),
1181 bt_common_color_reset());
1185 metadata_text_value
= bt_value_map_get(results
, "text");
1186 if (!metadata_text_value
) {
1187 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1192 ret
= bt_value_string_get(metadata_text_value
, &metadata_text
);
1194 printf("%s\n", metadata_text
);
1199 bt_put(metadata_text_value
);
1205 char *instance_name
;
1209 struct trace_range
{
1210 uint64_t intersection_range_begin_ns
;
1211 uint64_t intersection_range_end_ns
;
1215 guint
port_id_hash(gconstpointer v
)
1217 const struct port_id
*id
= v
;
1219 assert(id
->instance_name
);
1220 assert(id
->port_name
);
1222 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1226 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1228 const struct port_id
*id1
= v1
;
1229 const struct port_id
*id2
= v2
;
1231 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1232 !strcmp(id1
->port_name
, id2
->port_name
);
1236 void port_id_destroy(gpointer data
)
1238 struct port_id
*id
= data
;
1240 free(id
->instance_name
);
1241 free(id
->port_name
);
1246 void trace_range_destroy(gpointer data
)
1251 struct cmd_run_ctx
{
1253 GHashTable
*components
;
1256 struct bt_graph
*graph
;
1259 struct bt_config
*cfg
;
1263 bool stream_intersection_mode
;
1266 * Association of struct port_id -> struct trace_range.
1268 GHashTable
*intersections
;
1271 /* Returns a timestamp of the form "(-)s.ns" */
1273 char *s_from_ns(int64_t ns
)
1278 int64_t ts_sec_abs
, ts_nsec_abs
;
1279 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1280 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1282 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1283 is_negative
= false;
1284 ts_sec_abs
= ts_sec
;
1285 ts_nsec_abs
= ts_nsec
;
1286 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1287 is_negative
= false;
1288 ts_sec_abs
= ts_sec
- 1;
1289 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1290 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1292 ts_sec_abs
= ts_sec
;
1293 ts_nsec_abs
= -ts_nsec
;
1294 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1296 ts_sec_abs
= -(ts_sec
+ 1);
1297 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1298 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1300 ts_sec_abs
= -ts_sec
;
1301 ts_nsec_abs
= ts_nsec
;
1302 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1304 ts_sec_abs
= -ts_sec
;
1305 ts_nsec_abs
= -ts_nsec
;
1308 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1309 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1317 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1318 struct cmd_run_ctx
*ctx
, struct bt_component
*upstream_comp
,
1319 struct bt_port
*upstream_port
,
1320 struct bt_config_connection
*cfg_conn
)
1323 GQuark downstreamp_comp_name_quark
;
1324 struct bt_component
*downstream_comp
;
1325 int64_t downstream_port_count
;
1327 int64_t (*port_count_fn
)(struct bt_component
*);
1328 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
1329 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1330 bool insert_trimmer
= false;
1331 struct bt_value
*trimmer_params
= NULL
;
1332 char *intersection_begin
= NULL
;
1333 char *intersection_end
= NULL
;
1334 struct bt_component
*trimmer
= NULL
;
1335 struct bt_component_class
*trimmer_class
= NULL
;
1336 struct bt_port
*trimmer_input
= NULL
;
1337 struct bt_port
*trimmer_output
= NULL
;
1339 if (ctx
->intersections
&&
1340 bt_component_get_class_type(upstream_comp
) ==
1341 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1342 struct trace_range
*range
;
1343 struct port_id port_id
= {
1344 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1345 .port_name
= (char *) bt_port_get_name(upstream_port
)
1348 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1352 range
= (struct trace_range
*) g_hash_table_lookup(
1353 ctx
->intersections
, &port_id
);
1355 enum bt_value_status status
;
1357 intersection_begin
= s_from_ns(
1358 range
->intersection_range_begin_ns
);
1359 intersection_end
= s_from_ns(
1360 range
->intersection_range_end_ns
);
1361 if (!intersection_begin
|| !intersection_end
) {
1362 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1366 insert_trimmer
= true;
1367 trimmer_params
= bt_value_map_create();
1368 if (!trimmer_params
) {
1372 status
= bt_value_map_insert_string(trimmer_params
,
1373 "begin", intersection_begin
);
1374 if (status
!= BT_VALUE_STATUS_OK
) {
1377 status
= bt_value_map_insert_string(trimmer_params
,
1378 "end", intersection_end
);
1379 if (status
!= BT_VALUE_STATUS_OK
) {
1384 trimmer_class
= find_component_class("utils", "trimmer",
1385 BT_COMPONENT_CLASS_TYPE_FILTER
);
1386 if (!trimmer_class
) {
1391 BT_LOGI("Connecting upstream port to the next available downstream port: "
1392 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1393 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1394 upstream_port
, bt_port_get_name(upstream_port
),
1395 cfg_conn
->downstream_comp_name
->str
,
1396 cfg_conn
->arg
->str
);
1397 downstreamp_comp_name_quark
= g_quark_from_string(
1398 cfg_conn
->downstream_comp_name
->str
);
1399 assert(downstreamp_comp_name_quark
> 0);
1400 downstream_comp
= g_hash_table_lookup(ctx
->components
,
1401 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1402 if (!downstream_comp
) {
1403 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1404 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1405 cfg_conn
->arg
->str
);
1406 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1407 cfg_conn
->arg
->str
);
1411 if (bt_component_is_filter(downstream_comp
)) {
1412 port_count_fn
= bt_component_filter_get_input_port_count
;
1413 port_by_index_fn
= bt_component_filter_get_input_port_by_index
;
1414 } else if (bt_component_is_sink(downstream_comp
)) {
1415 port_count_fn
= bt_component_sink_get_input_port_count
;
1416 port_by_index_fn
= bt_component_sink_get_input_port_by_index
;
1419 * Should never happen because the connections are
1420 * validated before we get here.
1422 BT_LOGF("Invalid connection: downstream component is a source: "
1423 "conn-arg=\"%s\"", cfg_conn
->arg
->str
);
1427 downstream_port_count
= port_count_fn(downstream_comp
);
1428 assert(downstream_port_count
>= 0);
1430 for (i
= 0; i
< downstream_port_count
; i
++) {
1431 struct bt_port
*downstream_port
=
1432 port_by_index_fn(downstream_comp
, i
);
1433 const char *upstream_port_name
;
1434 const char *downstream_port_name
;
1436 assert(downstream_port
);
1438 /* Skip port if it's already connected. */
1439 if (bt_port_is_connected(downstream_port
)) {
1440 bt_put(downstream_port
);
1441 BT_LOGD("Skipping downstream port: already connected: "
1442 "port-addr=%p, port-name=\"%s\"",
1444 bt_port_get_name(downstream_port
));
1448 downstream_port_name
= bt_port_get_name(downstream_port
);
1449 assert(downstream_port_name
);
1450 upstream_port_name
= bt_port_get_name(upstream_port
);
1451 assert(upstream_port_name
);
1453 if (!bt_common_star_glob_match(
1454 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1455 downstream_port_name
, SIZE_MAX
)) {
1456 bt_put(downstream_port
);
1460 if (insert_trimmer
) {
1462 * In order to insert the trimmer between the two
1463 * components that were being connected, we create
1464 * a connection configuration entry which describes
1465 * a connection from the trimmer's output to the
1466 * original input that was being connected.
1468 * Hence, the creation of the trimmer will cause the
1469 * graph "new port" listener to establish all downstream
1470 * connections as its output port is connected. We will
1471 * then establish the connection between the original
1472 * upstream source and the trimmer.
1474 char *trimmer_name
= NULL
;
1475 enum bt_graph_status graph_status
;
1477 ret
= asprintf(&trimmer_name
, "%s-%s",
1478 "stream-intersection-trimmer",
1479 upstream_port_name
);
1485 ctx
->connect_ports
= false;
1486 graph_status
= bt_graph_add_component(ctx
->graph
,
1487 trimmer_class
, trimmer_name
, trimmer_params
,
1490 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1496 bt_component_filter_get_input_port_by_index(
1498 if (!trimmer_input
) {
1502 bt_component_filter_get_output_port_by_index(
1504 if (!trimmer_output
) {
1509 * Replace the current downstream port by the trimmer's
1512 BT_MOVE(downstream_port
, trimmer_input
);
1513 downstream_port_name
= bt_port_get_name(
1515 if (!downstream_port_name
) {
1520 /* We have a winner! */
1521 status
= bt_graph_connect_ports(ctx
->graph
,
1522 upstream_port
, downstream_port
, NULL
);
1523 BT_PUT(downstream_port
);
1525 case BT_GRAPH_STATUS_OK
:
1527 case BT_GRAPH_STATUS_CANCELED
:
1528 BT_LOGI_STR("Graph was canceled by user.");
1529 status
= BT_GRAPH_STATUS_OK
;
1531 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1532 BT_LOGE("A component refused a connection to one of its ports: "
1533 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1534 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1535 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1536 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1538 upstream_comp
, bt_component_get_name(upstream_comp
),
1539 upstream_port
, bt_port_get_name(upstream_port
),
1540 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1541 downstream_port
, downstream_port_name
,
1542 cfg_conn
->arg
->str
);
1544 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1545 bt_port_get_name(upstream_port
),
1546 downstream_port_name
,
1547 cfg_conn
->arg
->str
);
1550 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1551 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1552 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1553 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1554 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1556 upstream_comp
, bt_component_get_name(upstream_comp
),
1557 upstream_port
, bt_port_get_name(upstream_port
),
1558 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1559 downstream_port
, downstream_port_name
,
1560 cfg_conn
->arg
->str
);
1562 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1563 bt_port_get_name(upstream_port
),
1564 downstream_port_name
,
1565 cfg_conn
->arg
->str
);
1569 BT_LOGI("Connected component ports: "
1570 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1571 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1572 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1573 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1575 upstream_comp
, bt_component_get_name(upstream_comp
),
1576 upstream_port
, bt_port_get_name(upstream_port
),
1577 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1578 downstream_port
, downstream_port_name
,
1579 cfg_conn
->arg
->str
);
1581 if (insert_trimmer
) {
1583 * The first connection, from the source to the trimmer,
1584 * has been done. We now connect the trimmer to the
1585 * original downstream port.
1587 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1588 ctx
, trimmer
, trimmer_output
, cfg_conn
);
1592 ctx
->connect_ports
= true;
1596 * We found a matching downstream port: the search is
1602 /* No downstream port found */
1603 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1604 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1605 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1606 upstream_port
, bt_port_get_name(upstream_port
),
1607 cfg_conn
->downstream_comp_name
->str
,
1608 cfg_conn
->arg
->str
);
1610 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1611 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1617 free(intersection_begin
);
1618 free(intersection_end
);
1619 BT_PUT(trimmer_params
);
1620 BT_PUT(trimmer_class
);
1622 BT_PUT(trimmer_input
);
1623 BT_PUT(trimmer_output
);
1628 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1629 struct bt_port
*upstream_port
)
1632 const char *upstream_port_name
;
1633 const char *upstream_comp_name
;
1634 struct bt_component
*upstream_comp
= NULL
;
1638 assert(upstream_port
);
1639 upstream_port_name
= bt_port_get_name(upstream_port
);
1640 assert(upstream_port_name
);
1641 upstream_comp
= bt_port_get_component(upstream_port
);
1642 if (!upstream_comp
) {
1643 BT_LOGW("Upstream port to connect is not part of a component: "
1644 "port-addr=%p, port-name=\"%s\"",
1645 upstream_port
, upstream_port_name
);
1650 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1651 assert(upstream_comp_name
);
1652 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1653 "port-addr=%p, port-name=\"%s\"",
1654 upstream_comp
, upstream_comp_name
,
1655 upstream_port
, upstream_port_name
);
1657 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1658 struct bt_config_connection
*cfg_conn
=
1660 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1662 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1663 upstream_comp_name
)) {
1667 if (!bt_common_star_glob_match(
1668 cfg_conn
->upstream_port_glob
->str
,
1669 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1673 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1674 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1676 BT_LOGE("Cannot connect upstream port: "
1677 "port-addr=%p, port-name=\"%s\"",
1679 upstream_port_name
);
1681 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1684 cfg_conn
->arg
->str
);
1690 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1691 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1692 upstream_port_name
);
1694 "Cannot create connection: upstream port `%s` does not match any connection\n",
1695 upstream_port_name
);
1701 bt_put(upstream_comp
);
1706 void graph_port_added_listener(struct bt_port
*port
, void *data
)
1708 struct bt_component
*comp
= NULL
;
1709 struct cmd_run_ctx
*ctx
= data
;
1711 comp
= bt_port_get_component(port
);
1712 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1713 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1714 comp
, comp
? bt_component_get_name(comp
) : "",
1715 port
, bt_port_get_name(port
));
1717 if (!ctx
->connect_ports
) {
1722 BT_LOGW_STR("Port has no component.");
1726 if (bt_port_is_connected(port
)) {
1727 BT_LOGW_STR("Port is already connected.");
1731 if (!bt_port_is_output(port
)) {
1732 BT_LOGI_STR("Skipping input port.");
1736 if (cmd_run_ctx_connect_upstream_port(ctx
, port
)) {
1737 BT_LOGF_STR("Cannot connect upstream port.");
1738 fprintf(stderr
, "Added port could not be connected: aborting\n");
1748 void graph_port_removed_listener(struct bt_component
*component
,
1749 struct bt_port
*port
, void *data
)
1751 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1752 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1753 component
, bt_component_get_name(component
),
1754 port
, bt_port_get_name(port
));
1758 void graph_ports_connected_listener(struct bt_port
*upstream_port
,
1759 struct bt_port
*downstream_port
, void *data
)
1761 struct bt_component
*upstream_comp
= bt_port_get_component(upstream_port
);
1762 struct bt_component
*downstream_comp
= bt_port_get_component(downstream_port
);
1764 assert(upstream_comp
);
1765 assert(downstream_comp
);
1766 BT_LOGI("Graph's component ports connected: "
1767 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1768 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1769 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1770 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1771 upstream_comp
, bt_component_get_name(upstream_comp
),
1772 upstream_port
, bt_port_get_name(upstream_port
),
1773 downstream_comp
, bt_component_get_name(downstream_comp
),
1774 downstream_port
, bt_port_get_name(downstream_port
));
1775 bt_put(upstream_comp
);
1776 bt_put(downstream_comp
);
1780 void graph_ports_disconnected_listener(
1781 struct bt_component
*upstream_component
,
1782 struct bt_component
*downstream_component
,
1783 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
1786 BT_LOGI("Graph's component ports disconnected: "
1787 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1788 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1789 upstream_port
, bt_port_get_name(upstream_port
),
1790 downstream_port
, bt_port_get_name(downstream_port
));
1794 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1800 if (ctx
->components
) {
1801 g_hash_table_destroy(ctx
->components
);
1802 ctx
->components
= NULL
;
1805 if (ctx
->intersections
) {
1806 g_hash_table_destroy(ctx
->intersections
);
1807 ctx
->intersections
= NULL
;
1816 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1821 ctx
->connect_ports
= false;
1822 ctx
->components
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
1824 if (!ctx
->components
) {
1828 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1829 ctx
->stream_intersection_mode
= true;
1830 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1831 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1832 if (!ctx
->intersections
) {
1837 ctx
->graph
= bt_graph_create();
1842 the_graph
= ctx
->graph
;
1843 ret
= bt_graph_add_port_added_listener(ctx
->graph
,
1844 graph_port_added_listener
, NULL
, ctx
);
1846 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
1850 ret
= bt_graph_add_port_removed_listener(ctx
->graph
,
1851 graph_port_removed_listener
, NULL
, ctx
);
1853 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
1857 ret
= bt_graph_add_ports_connected_listener(ctx
->graph
,
1858 graph_ports_connected_listener
, NULL
, ctx
);
1860 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
1864 ret
= bt_graph_add_ports_disconnected_listener(ctx
->graph
,
1865 graph_ports_disconnected_listener
, NULL
, ctx
);
1867 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
1874 cmd_run_ctx_destroy(ctx
);
1882 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
1883 struct bt_config_component
*cfg_comp
,
1884 struct bt_component_class
*comp_cls
)
1888 int64_t trace_count
;
1889 enum bt_value_status value_status
;
1890 const char *path
= NULL
;
1891 struct bt_value
*component_path_value
= NULL
;
1892 struct bt_value
*query_params
= NULL
;
1893 struct bt_value
*query_result
= NULL
;
1894 struct bt_value
*trace_info
= NULL
;
1895 struct bt_value
*intersection_range
= NULL
;
1896 struct bt_value
*intersection_begin
= NULL
;
1897 struct bt_value
*intersection_end
= NULL
;
1898 struct bt_value
*stream_path_value
= NULL
;
1899 struct bt_value
*stream_paths
= NULL
;
1900 struct bt_value
*stream_infos
= NULL
;
1901 struct bt_value
*stream_info
= NULL
;
1902 struct port_id
*port_id
= NULL
;
1903 struct trace_range
*trace_range
= NULL
;
1905 component_path_value
= bt_value_map_get(cfg_comp
->params
, "path");
1906 if (!bt_value_is_string(component_path_value
)) {
1907 BT_LOGD("Cannot get path parameter: component-name=%s",
1908 cfg_comp
->instance_name
->str
);
1913 value_status
= bt_value_string_get(component_path_value
, &path
);
1914 if (value_status
!= BT_VALUE_STATUS_OK
) {
1915 BT_LOGD("Cannot get path string value: component-name=%s",
1916 cfg_comp
->instance_name
->str
);
1921 query_params
= bt_value_map_create();
1922 if (!query_params
) {
1923 BT_LOGE_STR("Cannot create query parameters.");
1928 value_status
= bt_value_map_insert(query_params
, "path", component_path_value
);
1929 if (value_status
!= BT_VALUE_STATUS_OK
) {
1930 BT_LOGE_STR("Cannot insert path parameter in query paramater map.");
1935 query_result
= bt_component_class_query(comp_cls
, "trace-info",
1937 if (!query_result
) {
1938 BT_LOGD("Component class \'%s\' does not support the \'trace-info\' query.",
1939 bt_component_class_get_name(comp_cls
));
1944 if (!bt_value_is_array(query_result
)) {
1945 BT_LOGD("Unexpected format of \'trace-info\' query result: "
1946 "component-class-name=%s",
1947 bt_component_class_get_name(comp_cls
));
1952 trace_count
= bt_value_array_size(query_result
);
1953 if (trace_count
< 0) {
1958 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
1960 uint64_t stream_idx
;
1961 int64_t stream_count
;
1963 trace_info
= bt_value_array_get(query_result
, trace_idx
);
1964 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
1966 BT_LOGD_STR("Cannot retrieve trace from query result.");
1970 intersection_range
= bt_value_map_get(trace_info
,
1971 "intersection-range-ns");
1972 if (!intersection_range
) {
1974 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
1978 intersection_begin
= bt_value_map_get(intersection_range
,
1980 if (!intersection_begin
) {
1982 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
1986 intersection_end
= bt_value_map_get(intersection_range
,
1988 if (!intersection_end
) {
1990 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
1994 value_status
= bt_value_integer_get(intersection_begin
, &begin
);
1995 if (value_status
!= BT_VALUE_STATUS_OK
) {
1997 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'begin\' field from query result.");
2001 value_status
= bt_value_integer_get(intersection_end
, &end
);
2002 if (value_status
!= BT_VALUE_STATUS_OK
) {
2004 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'end\' field from query result.");
2008 if (begin
< 0 || end
< 0 || end
< begin
) {
2009 BT_LOGW("Invalid trace stream intersection values: "
2010 "intersection-range-ns:begin=%" PRId64
2011 ", intersection-range-ns:end=%" PRId64
,
2017 stream_infos
= bt_value_map_get(trace_info
, "streams");
2018 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
2020 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2024 stream_count
= bt_value_array_size(stream_infos
);
2025 if (stream_count
< 0) {
2033 * The first path of a stream's "paths" is currently used to
2034 * associate streams/ports to a given trace intersection.
2036 * This is a fragile hack as it relies on the port names
2037 * being set to the various streams path.
2039 * A stream name should be introduced as part of the trace-info
2042 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2043 const char *stream_path
;
2045 port_id
= g_new0(struct port_id
, 1);
2048 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2051 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2052 if (!port_id
->instance_name
) {
2054 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2058 trace_range
= g_new0(struct trace_range
, 1);
2061 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2064 trace_range
->intersection_range_begin_ns
= begin
;
2065 trace_range
->intersection_range_end_ns
= end
;
2067 stream_info
= bt_value_array_get(stream_infos
,
2069 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2071 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2075 stream_paths
= bt_value_map_get(stream_info
, "paths");
2076 if (!stream_paths
|| !bt_value_is_array(stream_paths
)) {
2078 BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
2082 stream_path_value
= bt_value_array_get(stream_paths
, 0);
2083 if (!stream_path_value
||
2084 !bt_value_is_string(stream_path_value
)) {
2086 BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
2090 value_status
= bt_value_string_get(stream_path_value
,
2092 if (value_status
!= BT_VALUE_STATUS_OK
) {
2097 port_id
->port_name
= strdup(stream_path
);
2098 if (!port_id
->port_name
) {
2100 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2104 BT_LOGD("Inserting stream intersection ");
2106 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2110 BT_PUT(stream_info
);
2111 BT_PUT(stream_paths
);
2112 BT_PUT(stream_path_value
);
2116 BT_PUT(stream_paths
);
2117 BT_PUT(stream_path_value
);
2118 BT_PUT(intersection_range
);
2119 BT_PUT(intersection_begin
);
2120 BT_PUT(intersection_end
);
2121 BT_PUT(stream_paths
);
2122 BT_PUT(stream_path_value
);
2128 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2129 bt_common_color_bold(),
2130 bt_common_color_fg_yellow(),
2131 path
? path
: "(unknown)",
2132 bt_common_color_reset());
2134 bt_put(component_path_value
);
2135 bt_put(query_params
);
2136 bt_put(query_result
);
2138 bt_put(intersection_range
);
2139 bt_put(intersection_begin
);
2140 bt_put(intersection_end
);
2141 bt_put(stream_infos
);
2142 bt_put(stream_info
);
2143 bt_put(stream_paths
);
2144 bt_put(stream_path_value
);
2146 g_free(trace_range
);
2151 int cmd_run_ctx_create_components_from_config_components(
2152 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2155 struct bt_component_class
*comp_cls
= NULL
;
2156 struct bt_component
*comp
= NULL
;
2159 for (i
= 0; i
< cfg_components
->len
; i
++) {
2160 struct bt_config_component
*cfg_comp
=
2161 g_ptr_array_index(cfg_components
, i
);
2164 comp_cls
= find_component_class(cfg_comp
->plugin_name
->str
,
2165 cfg_comp
->comp_cls_name
->str
, cfg_comp
->type
);
2167 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2168 "comp-cls-name=\"%s\", comp-cls-type=%d",
2169 cfg_comp
->plugin_name
->str
,
2170 cfg_comp
->comp_cls_name
->str
,
2172 fprintf(stderr
, "%s%sCannot find component class %s",
2173 bt_common_color_bold(),
2174 bt_common_color_fg_red(),
2175 bt_common_color_reset());
2176 print_plugin_comp_cls_opt(stderr
,
2177 cfg_comp
->plugin_name
->str
,
2178 cfg_comp
->comp_cls_name
->str
,
2180 fprintf(stderr
, "\n");
2184 ret
= bt_graph_add_component(ctx
->graph
, comp_cls
,
2185 cfg_comp
->instance_name
->str
, cfg_comp
->params
, &comp
);
2187 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2188 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2190 cfg_comp
->plugin_name
->str
,
2191 cfg_comp
->comp_cls_name
->str
,
2192 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2193 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2194 bt_common_color_bold(),
2195 bt_common_color_fg_red(),
2196 cfg_comp
->instance_name
->str
,
2197 bt_common_color_reset());
2201 if (ctx
->stream_intersection_mode
&&
2202 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2203 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2209 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2210 comp
, cfg_comp
->instance_name
->str
);
2211 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2213 g_hash_table_insert(ctx
->components
,
2214 GUINT_TO_POINTER(quark
), comp
);
2231 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2236 * Make sure that, during this phase, our graph's "port added"
2237 * listener does not connect ports while we are creating the
2238 * components because we have a special, initial phase for
2241 ctx
->connect_ports
= false;
2243 ret
= cmd_run_ctx_create_components_from_config_components(
2244 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2250 ret
= cmd_run_ctx_create_components_from_config_components(
2251 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2257 ret
= cmd_run_ctx_create_components_from_config_components(
2258 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2269 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2270 struct bt_component
*comp
,
2271 int64_t (*port_count_fn
)(struct bt_component
*),
2272 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t))
2278 count
= port_count_fn(comp
);
2281 for (i
= 0; i
< count
; i
++) {
2282 struct bt_port
*upstream_port
= port_by_index_fn(comp
, i
);
2284 assert(upstream_port
);
2285 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2286 bt_put(upstream_port
);
2297 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2300 GHashTableIter iter
;
2301 gpointer g_name_quark
, g_comp
;
2303 ctx
->connect_ports
= true;
2304 g_hash_table_iter_init(&iter
, ctx
->components
);
2306 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2307 int64_t (*port_count_fn
)(struct bt_component
*);
2308 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
2310 if (bt_component_is_source(g_comp
)) {
2312 bt_component_source_get_output_port_count
;
2314 bt_component_source_get_output_port_by_index
;
2315 } else if (bt_component_is_filter(g_comp
)) {
2317 bt_component_filter_get_output_port_count
;
2319 bt_component_filter_get_output_port_by_index
;
2324 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
2325 g_comp
, port_count_fn
, port_by_index_fn
);
2336 const char *bt_graph_status_str(enum bt_graph_status status
)
2339 case BT_GRAPH_STATUS_CANCELED
:
2340 return "BT_GRAPH_STATUS_CANCELED";
2341 case BT_GRAPH_STATUS_AGAIN
:
2342 return "BT_GRAPH_STATUS_AGAIN";
2343 case BT_GRAPH_STATUS_END
:
2344 return "BT_GRAPH_STATUS_END";
2345 case BT_GRAPH_STATUS_OK
:
2346 return "BT_GRAPH_STATUS_OK";
2347 case BT_GRAPH_STATUS_INVALID
:
2348 return "BT_GRAPH_STATUS_INVALID";
2349 case BT_GRAPH_STATUS_NO_SINK
:
2350 return "BT_GRAPH_STATUS_NO_SINK";
2351 case BT_GRAPH_STATUS_ERROR
:
2352 return "BT_GRAPH_STATUS_ERROR";
2359 int cmd_run(struct bt_config
*cfg
)
2362 struct cmd_run_ctx ctx
= { 0 };
2364 /* Initialize the command's context and the graph object */
2365 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2366 BT_LOGE_STR("Cannot initialize the command's context.");
2367 fprintf(stderr
, "Cannot initialize the command's context\n");
2372 BT_LOGI_STR("Canceled by user before creating components.");
2376 BT_LOGI_STR("Creating components.");
2378 /* Create the requested component instances */
2379 if (cmd_run_ctx_create_components(&ctx
)) {
2380 BT_LOGE_STR("Cannot create components.");
2381 fprintf(stderr
, "Cannot create components\n");
2386 BT_LOGI_STR("Canceled by user before connecting components.");
2390 BT_LOGI_STR("Connecting components.");
2392 /* Connect the initially visible component ports */
2393 if (cmd_run_ctx_connect_ports(&ctx
)) {
2394 BT_LOGE_STR("Cannot connect initial component ports.");
2395 fprintf(stderr
, "Cannot connect initial component ports\n");
2400 BT_LOGI_STR("Canceled by user before running the graph.");
2404 BT_LOGI_STR("Running the graph.");
2408 enum bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2411 * Reset console in case something messed with console
2412 * codes during the graph's execution.
2414 printf("%s", bt_common_color_reset());
2416 fprintf(stderr
, "%s", bt_common_color_reset());
2417 BT_LOGV("bt_graph_run() returned: status=%s",
2418 bt_graph_status_str(graph_status
));
2420 switch (graph_status
) {
2421 case BT_GRAPH_STATUS_OK
:
2423 case BT_GRAPH_STATUS_CANCELED
:
2424 BT_LOGI_STR("Graph was canceled by user.");
2426 case BT_GRAPH_STATUS_AGAIN
:
2427 if (bt_graph_is_canceled(ctx
.graph
)) {
2428 BT_LOGI_STR("Graph was canceled by user.");
2432 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2433 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2435 cfg
->cmd_data
.run
.retry_duration_us
);
2437 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2438 if (bt_graph_is_canceled(ctx
.graph
)) {
2439 BT_LOGI_STR("Graph was canceled by user.");
2445 case BT_COMPONENT_STATUS_END
:
2448 BT_LOGE_STR("Graph failed to complete successfully");
2449 fprintf(stderr
, "Graph failed to complete successfully\n");
2462 cmd_run_ctx_destroy(&ctx
);
2467 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2469 const char *env_clash
;
2471 if (!cfg
->command_name
) {
2475 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2476 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2480 if (g_file_test(cfg
->command_name
,
2481 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2482 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2484 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2486 fprintf(stderr
, "\n");
2487 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
2493 void init_log_level(void)
2495 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2499 void set_auto_log_levels(struct bt_config
*cfg
)
2501 const char **env_var_name
;
2504 * Override the configuration's default log level if
2505 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2506 * are found for backward compatibility with legacy Babetrace 1.
2508 if (getenv("BABELTRACE_DEBUG") &&
2509 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2510 cfg
->log_level
= 'V';
2511 } else if (getenv("BABELTRACE_VERBOSE") &&
2512 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2513 cfg
->log_level
= 'I';
2517 * Set log levels according to --debug or --verbose. For
2518 * backward compatibility, --debug is more verbose than
2521 * --verbose: INFO log level
2522 * --debug: VERBOSE log level (includes DEBUG, which is
2523 * is less verbose than VERBOSE in the internal
2524 * logging framework)
2526 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2528 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2529 } else if (cfg
->debug
) {
2530 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2533 * Set library's default log level if not
2534 * explicitly specified.
2536 switch (cfg
->log_level
) {
2538 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE
);
2541 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2544 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
2547 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2550 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN
);
2553 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR
);
2556 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL
);
2564 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2566 bt_cli_log_level
= BT_LOG_INFO
;
2567 } else if (cfg
->debug
) {
2568 bt_cli_log_level
= BT_LOG_VERBOSE
;
2571 * Set CLI's default log level if not explicitly
2574 switch (cfg
->log_level
) {
2576 bt_cli_log_level
= BT_LOG_NONE
;
2579 bt_cli_log_level
= BT_LOG_VERBOSE
;
2582 bt_cli_log_level
= BT_LOG_DEBUG
;
2585 bt_cli_log_level
= BT_LOG_INFO
;
2588 bt_cli_log_level
= BT_LOG_WARN
;
2591 bt_cli_log_level
= BT_LOG_ERROR
;
2594 bt_cli_log_level
= BT_LOG_FATAL
;
2602 env_var_name
= log_level_env_var_names
;
2604 while (*env_var_name
) {
2605 if (!getenv(*env_var_name
)) {
2607 g_setenv(*env_var_name
, "I", 1);
2608 } else if (cfg
->debug
) {
2609 g_setenv(*env_var_name
, "V", 1);
2611 char val
[2] = { 0 };
2614 * Set module's default log level if not
2615 * explicitly specified.
2617 val
[0] = cfg
->log_level
;
2618 g_setenv(*env_var_name
, val
, 1);
2627 void set_sigint_handler(void)
2629 struct sigaction new_action
, old_action
;
2631 new_action
.sa_handler
= sigint_handler
;
2632 sigemptyset(&new_action
.sa_mask
);
2633 new_action
.sa_flags
= 0;
2634 sigaction(SIGINT
, NULL
, &old_action
);
2636 if (old_action
.sa_handler
!= SIG_IGN
) {
2637 sigaction(SIGINT
, &new_action
, NULL
);
2641 int main(int argc
, const char **argv
)
2645 struct bt_config
*cfg
;
2648 set_sigint_handler();
2650 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2653 /* Quit without errors; typically usage/version */
2655 BT_LOGI_STR("Quitting without errors.");
2660 BT_LOGE("Command-line error: retcode=%d", retcode
);
2665 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2666 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2671 set_auto_log_levels(cfg
);
2674 if (cfg
->command_needs_plugins
) {
2675 ret
= load_all_plugins(cfg
->plugin_paths
);
2677 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2683 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2684 cfg
->command
, cfg
->command_name
);
2686 switch (cfg
->command
) {
2687 case BT_CONFIG_COMMAND_RUN
:
2690 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2691 ret
= cmd_list_plugins(cfg
);
2693 case BT_CONFIG_COMMAND_HELP
:
2694 ret
= cmd_help(cfg
);
2696 case BT_CONFIG_COMMAND_QUERY
:
2697 ret
= cmd_query(cfg
);
2699 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2700 ret
= cmd_print_ctf_metadata(cfg
);
2702 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2703 ret
= cmd_print_lttng_live_sessions(cfg
);
2706 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2710 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2711 cfg
->command
, cfg
->command_name
, ret
);
2712 warn_command_name_and_directory_clash(cfg
);
2713 retcode
= ret
? 1 : 0;