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 #include <babeltrace/babeltrace.h>
30 #include <babeltrace/plugin/plugin.h>
31 #include <babeltrace/common-internal.h>
32 #include <babeltrace/graph/component.h>
33 #include <babeltrace/graph/component-source.h>
34 #include <babeltrace/graph/component-sink.h>
35 #include <babeltrace/graph/component-filter.h>
36 #include <babeltrace/graph/component-class.h>
37 #include <babeltrace/graph/port.h>
38 #include <babeltrace/graph/graph.h>
39 #include <babeltrace/graph/connection.h>
40 #include <babeltrace/graph/notification-iterator.h>
41 #include <babeltrace/ref.h>
42 #include <babeltrace/values.h>
43 #include <babeltrace/logging.h>
53 #include "babeltrace-cfg.h"
54 #include "babeltrace-cfg-cli-args.h"
55 #include "babeltrace-cfg-cli-args-default.h"
57 #define BT_LOG_TAG "CLI"
60 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
62 /* Application's processing graph (weak) */
63 static struct bt_graph
*the_graph
;
64 static bool canceled
= false;
66 GPtrArray
*loaded_plugins
;
69 int bt_cli_log_level
= BT_LOG_NONE
;
72 void sigint_handler(int signum
)
74 if (signum
!= SIGINT
) {
79 bt_graph_cancel(the_graph
);
86 void init_static_data(void)
88 loaded_plugins
= g_ptr_array_new_with_free_func(bt_put
);
92 void fini_static_data(void)
94 g_ptr_array_free(loaded_plugins
, TRUE
);
98 struct bt_plugin
*find_plugin(const char *name
)
101 struct bt_plugin
*plugin
= NULL
;
104 BT_LOGD("Finding plugin: name=\"%s\"", name
);
106 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
107 plugin
= g_ptr_array_index(loaded_plugins
, i
);
109 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
116 if (BT_LOG_ON_DEBUG
) {
118 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
120 BT_LOGD("Cannot find plugin.");
124 return bt_get(plugin
);
128 struct bt_component_class
*find_component_class(const char *plugin_name
,
129 const char *comp_class_name
,
130 enum bt_component_class_type comp_class_type
)
132 struct bt_component_class
*comp_class
= NULL
;
133 struct bt_plugin
*plugin
;
135 BT_LOGD("Finding component class: plugin-name=\"%s\", "
136 "comp-cls-name=\"%s\", comp-cls-type=%d",
137 plugin_name
, comp_class_name
, comp_class_type
);
139 plugin
= find_plugin(plugin_name
);
145 comp_class
= bt_plugin_get_component_class_by_name_and_type(plugin
,
146 comp_class_name
, comp_class_type
);
150 if (BT_LOG_ON_DEBUG
) {
152 BT_LOGD("Found component class: comp-cls-addr=%p",
155 BT_LOGD("Cannot find component class.");
163 void print_indent(FILE *fp
, size_t indent
)
167 for (i
= 0; i
< indent
; i
++) {
173 const char *component_type_str(enum bt_component_class_type type
)
176 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
178 case BT_COMPONENT_CLASS_TYPE_SINK
:
180 case BT_COMPONENT_CLASS_TYPE_FILTER
:
182 case BT_COMPONENT_CLASS_TYPE_UNKNOWN
:
189 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
190 const char *comp_cls_name
, enum bt_component_class_type type
)
192 GString
*shell_plugin_name
= NULL
;
193 GString
*shell_comp_cls_name
= NULL
;
195 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
196 if (!shell_plugin_name
) {
200 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
201 if (!shell_comp_cls_name
) {
205 fprintf(fh
, "%s%s--%s%s %s'%s%s%s%s.%s%s%s'",
206 bt_common_color_bold(),
207 bt_common_color_fg_cyan(),
208 component_type_str(type
),
209 bt_common_color_reset(),
210 bt_common_color_fg_default(),
211 bt_common_color_bold(),
212 bt_common_color_fg_blue(),
213 shell_plugin_name
->str
,
214 bt_common_color_fg_default(),
215 bt_common_color_fg_yellow(),
216 shell_comp_cls_name
->str
,
217 bt_common_color_reset());
220 if (shell_plugin_name
) {
221 g_string_free(shell_plugin_name
, TRUE
);
224 if (shell_comp_cls_name
) {
225 g_string_free(shell_comp_cls_name
, TRUE
);
230 void print_value(FILE *, struct bt_value
*, size_t);
233 void print_value_rec(FILE *, struct bt_value
*, size_t);
235 struct print_map_value_data
{
241 bt_bool
print_map_value(const char *key
, struct bt_value
*object
, void *data
)
243 struct print_map_value_data
*print_map_value_data
= data
;
245 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
246 fprintf(print_map_value_data
->fp
, "%s: ", key
);
248 if (bt_value_is_array(object
) &&
249 bt_value_array_is_empty(object
)) {
250 fprintf(print_map_value_data
->fp
, "[ ]\n");
254 if (bt_value_is_map(object
) &&
255 bt_value_map_is_empty(object
)) {
256 fprintf(print_map_value_data
->fp
, "{ }\n");
260 if (bt_value_is_array(object
) ||
261 bt_value_is_map(object
)) {
262 fprintf(print_map_value_data
->fp
, "\n");
265 print_value_rec(print_map_value_data
->fp
, object
,
266 print_map_value_data
->indent
+ 2);
271 void print_value_rec(FILE *fp
, struct bt_value
*value
, size_t indent
)
284 switch (bt_value_get_type(value
)) {
285 case BT_VALUE_TYPE_NULL
:
286 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
287 bt_common_color_reset());
289 case BT_VALUE_TYPE_BOOL
:
290 bt_value_bool_get(value
, &bool_val
);
291 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
292 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
293 bt_common_color_reset());
295 case BT_VALUE_TYPE_INTEGER
:
296 bt_value_integer_get(value
, &int_val
);
297 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
298 bt_common_color_fg_red(), int_val
,
299 bt_common_color_reset());
301 case BT_VALUE_TYPE_FLOAT
:
302 bt_value_float_get(value
, &dbl_val
);
303 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
304 bt_common_color_fg_red(), dbl_val
,
305 bt_common_color_reset());
307 case BT_VALUE_TYPE_STRING
:
308 bt_value_string_get(value
, &str_val
);
309 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
310 bt_common_color_fg_green(), str_val
,
311 bt_common_color_reset());
313 case BT_VALUE_TYPE_ARRAY
:
314 size
= bt_value_array_size(value
);
318 print_indent(fp
, indent
);
319 fprintf(fp
, "[ ]\n");
323 for (i
= 0; i
< size
; i
++) {
324 struct bt_value
*element
=
325 bt_value_array_get(value
, i
);
328 print_indent(fp
, indent
);
331 if (bt_value_is_array(element
) &&
332 bt_value_array_is_empty(element
)) {
333 fprintf(fp
, "[ ]\n");
337 if (bt_value_is_map(element
) &&
338 bt_value_map_is_empty(element
)) {
339 fprintf(fp
, "{ }\n");
343 if (bt_value_is_array(element
) ||
344 bt_value_is_map(element
)) {
348 print_value_rec(fp
, element
, indent
+ 2);
352 case BT_VALUE_TYPE_MAP
:
354 struct print_map_value_data data
= {
359 if (bt_value_map_is_empty(value
)) {
360 print_indent(fp
, indent
);
361 fprintf(fp
, "{ }\n");
365 bt_value_map_foreach(value
, print_map_value
, &data
);
374 void print_value(FILE *fp
, struct bt_value
*value
, size_t indent
)
376 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
377 print_indent(fp
, indent
);
380 print_value_rec(fp
, value
, indent
);
384 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
386 fprintf(stderr
, " ");
387 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
388 bt_config_component
->comp_cls_name
->str
,
389 bt_config_component
->type
);
390 fprintf(stderr
, ":\n");
392 if (bt_config_component
->instance_name
->len
> 0) {
393 fprintf(stderr
, " Name: %s\n",
394 bt_config_component
->instance_name
->str
);
397 fprintf(stderr
, " Parameters:\n");
398 print_value(stderr
, bt_config_component
->params
, 8);
402 void print_bt_config_components(GPtrArray
*array
)
406 for (i
= 0; i
< array
->len
; i
++) {
407 struct bt_config_component
*cfg_component
=
408 bt_config_get_component(array
, i
);
409 print_bt_config_component(cfg_component
);
410 BT_PUT(cfg_component
);
415 void print_plugin_paths(struct bt_value
*plugin_paths
)
417 fprintf(stderr
, " Plugin paths:\n");
418 print_value(stderr
, plugin_paths
, 4);
422 void print_cfg_run(struct bt_config
*cfg
)
426 print_plugin_paths(cfg
->plugin_paths
);
427 fprintf(stderr
, " Source component instances:\n");
428 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
430 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
431 fprintf(stderr
, " Filter component instances:\n");
432 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
435 fprintf(stderr
, " Sink component instances:\n");
436 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
437 fprintf(stderr
, " Connections:\n");
439 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
440 struct bt_config_connection
*cfg_connection
=
441 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
444 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
445 cfg_connection
->upstream_comp_name
->str
,
446 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
447 cfg_connection
->upstream_port_glob
->str
,
448 cfg_connection
->downstream_comp_name
->str
,
449 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
450 cfg_connection
->downstream_port_glob
->str
);
455 void print_cfg_list_plugins(struct bt_config
*cfg
)
457 print_plugin_paths(cfg
->plugin_paths
);
461 void print_cfg_help(struct bt_config
*cfg
)
463 print_plugin_paths(cfg
->plugin_paths
);
467 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
469 print_plugin_paths(cfg
->plugin_paths
);
470 fprintf(stderr
, " Path: %s\n",
471 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
475 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
477 print_plugin_paths(cfg
->plugin_paths
);
478 fprintf(stderr
, " URL: %s\n",
479 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
483 void print_cfg_query(struct bt_config
*cfg
)
485 print_plugin_paths(cfg
->plugin_paths
);
486 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
487 fprintf(stderr
, " Component class:\n");
488 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
492 void print_cfg(struct bt_config
*cfg
)
494 if (!BT_LOG_ON_INFO
) {
498 BT_LOGI_STR("Configuration:");
499 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
500 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
502 switch (cfg
->command
) {
503 case BT_CONFIG_COMMAND_RUN
:
506 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
507 print_cfg_list_plugins(cfg
);
509 case BT_CONFIG_COMMAND_HELP
:
512 case BT_CONFIG_COMMAND_QUERY
:
513 print_cfg_query(cfg
);
515 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
516 print_cfg_print_ctf_metadata(cfg
);
518 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
519 print_cfg_print_lttng_live_sessions(cfg
);
527 void add_to_loaded_plugins(struct bt_plugin_set
*plugin_set
)
532 count
= bt_plugin_set_get_plugin_count(plugin_set
);
535 for (i
= 0; i
< count
; i
++) {
536 struct bt_plugin
*plugin
=
537 bt_plugin_set_get_plugin(plugin_set
, i
);
538 struct bt_plugin
*loaded_plugin
=
539 find_plugin(bt_plugin_get_name(plugin
));
544 BT_LOGI("Not using plugin: another one already exists with the same name: "
545 "plugin-name=\"%s\", plugin-path=\"%s\", "
546 "existing-plugin-path=\"%s\"",
547 bt_plugin_get_name(plugin
),
548 bt_plugin_get_path(plugin
),
549 bt_plugin_get_path(loaded_plugin
));
550 bt_put(loaded_plugin
);
552 /* Add to global array. */
553 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
554 bt_plugin_get_name(plugin
));
555 g_ptr_array_add(loaded_plugins
, bt_get(plugin
));
563 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
565 int nr_paths
, i
, ret
= 0;
567 nr_paths
= bt_value_array_size(plugin_paths
);
569 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
574 BT_LOGI("Loading dynamic plugins.");
576 for (i
= 0; i
< nr_paths
; i
++) {
577 struct bt_value
*plugin_path_value
= NULL
;
578 const char *plugin_path
;
579 struct bt_plugin_set
*plugin_set
;
581 plugin_path_value
= bt_value_array_get(plugin_paths
, i
);
582 bt_value_string_get(plugin_path_value
, &plugin_path
);
584 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
586 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
588 BT_PUT(plugin_path_value
);
592 add_to_loaded_plugins(plugin_set
);
594 BT_PUT(plugin_path_value
);
601 int load_static_plugins(void)
604 struct bt_plugin_set
*plugin_set
;
606 BT_LOGI("Loading static plugins.");
607 plugin_set
= bt_plugin_create_all_from_static();
609 BT_LOGE("Unable to load static plugins.");
614 add_to_loaded_plugins(plugin_set
);
621 int load_all_plugins(struct bt_value
*plugin_paths
)
625 if (load_dynamic_plugins(plugin_paths
)) {
630 if (load_static_plugins()) {
635 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
642 void print_plugin_info(struct bt_plugin
*plugin
)
644 unsigned int major
, minor
, patch
;
646 enum bt_plugin_status version_status
;
647 const char *plugin_name
;
651 const char *plugin_description
;
653 plugin_name
= bt_plugin_get_name(plugin
);
654 path
= bt_plugin_get_path(plugin
);
655 author
= bt_plugin_get_author(plugin
);
656 license
= bt_plugin_get_license(plugin
);
657 plugin_description
= bt_plugin_get_description(plugin
);
658 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
660 printf("%s%s%s%s:\n", bt_common_color_bold(),
661 bt_common_color_fg_blue(), plugin_name
,
662 bt_common_color_reset());
663 printf(" %sPath%s: %s\n", bt_common_color_bold(),
664 bt_common_color_reset(), path
? path
: "(None)");
666 if (version_status
== BT_PLUGIN_STATUS_OK
) {
667 printf(" %sVersion%s: %u.%u.%u",
668 bt_common_color_bold(), bt_common_color_reset(),
669 major
, minor
, patch
);
678 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
679 bt_common_color_reset(),
680 plugin_description
? plugin_description
: "(None)");
681 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
682 bt_common_color_reset(), author
? author
: "(Unknown)");
683 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
684 bt_common_color_reset(),
685 license
? license
: "(Unknown)");
689 int cmd_query(struct bt_config
*cfg
)
692 struct bt_component_class
*comp_cls
= NULL
;
693 struct bt_value
*results
= NULL
;
695 comp_cls
= find_component_class(cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
696 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
697 cfg
->cmd_data
.query
.cfg_component
->type
);
699 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
700 "comp-cls-name=\"%s\", comp-cls-type=%d",
701 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
702 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
703 cfg
->cmd_data
.query
.cfg_component
->type
);
704 fprintf(stderr
, "%s%sCannot find component class %s",
705 bt_common_color_bold(),
706 bt_common_color_fg_red(),
707 bt_common_color_reset());
708 print_plugin_comp_cls_opt(stderr
,
709 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
710 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
711 cfg
->cmd_data
.query
.cfg_component
->type
);
712 fprintf(stderr
, "\n");
717 results
= bt_component_class_query(comp_cls
,
718 cfg
->cmd_data
.query
.object
->str
,
719 cfg
->cmd_data
.query
.cfg_component
->params
);
721 BT_LOGE("Failed to query component class: plugin-name=\"%s\", "
722 "comp-cls-name=\"%s\", comp-cls-type=%d "
724 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
725 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
726 cfg
->cmd_data
.query
.cfg_component
->type
,
727 cfg
->cmd_data
.query
.object
->str
);
728 fprintf(stderr
, "%s%sFailed to query info to %s",
729 bt_common_color_bold(),
730 bt_common_color_fg_red(),
731 bt_common_color_reset());
732 print_plugin_comp_cls_opt(stderr
,
733 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
734 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
735 cfg
->cmd_data
.query
.cfg_component
->type
);
736 fprintf(stderr
, "%s%s with object `%s`%s\n",
737 bt_common_color_bold(),
738 bt_common_color_fg_red(),
739 cfg
->cmd_data
.query
.object
->str
,
740 bt_common_color_reset());
745 print_value(stdout
, results
, 0);
754 int cmd_help(struct bt_config
*cfg
)
757 struct bt_plugin
*plugin
= NULL
;
760 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
762 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
763 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
764 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
765 bt_common_color_bold(), bt_common_color_fg_red(),
766 bt_common_color_fg_blue(),
767 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
768 bt_common_color_reset());
773 print_plugin_info(plugin
);
774 printf(" %sComponent classes%s: %d\n",
775 bt_common_color_bold(),
776 bt_common_color_reset(),
777 (int) bt_plugin_get_component_class_count(plugin
));
780 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
781 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
782 struct bt_component_class
*needed_comp_cls
=
783 find_component_class(
784 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
785 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
786 cfg
->cmd_data
.help
.cfg_component
->type
);
788 if (!needed_comp_cls
) {
789 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
790 "comp-cls-name=\"%s\", comp-cls-type=%d",
791 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
792 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
793 cfg
->cmd_data
.help
.cfg_component
->type
);
794 fprintf(stderr
, "\n%s%sCannot find component class %s",
795 bt_common_color_bold(),
796 bt_common_color_fg_red(),
797 bt_common_color_reset());
798 print_plugin_comp_cls_opt(stderr
,
799 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
800 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
801 cfg
->cmd_data
.help
.cfg_component
->type
);
802 fprintf(stderr
, "\n");
807 bt_put(needed_comp_cls
);
810 for (i
= 0; i
< bt_plugin_get_component_class_count(plugin
); i
++) {
811 struct bt_component_class
*comp_cls
=
812 bt_plugin_get_component_class_by_index(plugin
, i
);
813 const char *comp_class_name
=
814 bt_component_class_get_name(comp_cls
);
815 const char *comp_class_description
=
816 bt_component_class_get_description(comp_cls
);
817 const char *comp_class_help
=
818 bt_component_class_get_help(comp_cls
);
819 enum bt_component_class_type type
=
820 bt_component_class_get_type(comp_cls
);
824 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
825 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
826 if (strcmp(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
827 comp_class_name
) != 0 &&
829 cfg
->cmd_data
.help
.cfg_component
->type
) {
836 print_plugin_comp_cls_opt(stdout
,
837 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
841 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
842 bt_common_color_reset(),
843 comp_class_description
? comp_class_description
: "(None)");
845 if (comp_class_help
) {
846 printf("\n%s\n", comp_class_help
);
858 int cmd_list_plugins(struct bt_config
*cfg
)
861 int plugins_count
, component_classes_count
= 0, i
;
863 printf("From the following plugin paths:\n\n");
864 print_value(stdout
, cfg
->plugin_paths
, 2);
866 plugins_count
= loaded_plugins
->len
;
867 if (plugins_count
== 0) {
868 printf("No plugins found.\n");
872 for (i
= 0; i
< plugins_count
; i
++) {
873 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
875 component_classes_count
+= bt_plugin_get_component_class_count(plugin
);
878 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
879 bt_common_color_bold(),
880 component_classes_count
,
881 bt_common_color_reset(),
882 bt_common_color_bold(),
884 bt_common_color_reset());
886 for (i
= 0; i
< plugins_count
; i
++) {
888 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
890 component_classes_count
=
891 bt_plugin_get_component_class_count(plugin
);
893 print_plugin_info(plugin
);
895 if (component_classes_count
== 0) {
896 printf(" %sComponent classes%s: (none)\n",
897 bt_common_color_bold(),
898 bt_common_color_reset());
900 printf(" %sComponent classes%s:\n",
901 bt_common_color_bold(),
902 bt_common_color_reset());
905 for (j
= 0; j
< component_classes_count
; j
++) {
906 struct bt_component_class
*comp_class
=
907 bt_plugin_get_component_class_by_index(
909 const char *comp_class_name
=
910 bt_component_class_get_name(comp_class
);
911 const char *comp_class_description
=
912 bt_component_class_get_description(comp_class
);
913 enum bt_component_class_type type
=
914 bt_component_class_get_type(comp_class
);
917 print_plugin_comp_cls_opt(stdout
,
918 bt_plugin_get_name(plugin
), comp_class_name
,
921 if (comp_class_description
) {
922 printf(": %s", comp_class_description
);
935 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
942 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
945 struct bt_component_class
*comp_cls
= NULL
;
946 struct bt_value
*results
= NULL
;
947 struct bt_value
*params
= NULL
;
948 struct bt_value
*metadata_text_value
= NULL
;
949 const char *metadata_text
= NULL
;
950 static const char * const plugin_name
= "ctf";
951 static const char * const comp_cls_name
= "fs";
952 static const enum bt_component_class_type comp_cls_type
=
953 BT_COMPONENT_CLASS_TYPE_SOURCE
;
955 assert(cfg
->cmd_data
.print_ctf_metadata
.path
);
956 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
959 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
960 "comp-cls-name=\"%s\", comp-cls-type=%d",
961 plugin_name
, comp_cls_name
,
962 BT_COMPONENT_CLASS_TYPE_SOURCE
);
963 fprintf(stderr
, "%s%sCannot find component class %s",
964 bt_common_color_bold(),
965 bt_common_color_fg_red(),
966 bt_common_color_reset());
967 print_plugin_comp_cls_opt(stderr
, plugin_name
,
968 comp_cls_name
, comp_cls_type
);
969 fprintf(stderr
, "\n");
974 params
= bt_value_map_create();
980 ret
= bt_value_map_insert_string(params
, "path",
981 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
987 results
= bt_component_class_query(comp_cls
, "metadata-info",
991 BT_LOGE_STR("Failed to query for metadata info.");
992 fprintf(stderr
, "%s%sFailed to request metadata info%s\n",
993 bt_common_color_bold(),
994 bt_common_color_fg_red(),
995 bt_common_color_reset());
999 metadata_text_value
= bt_value_map_get(results
, "text");
1000 if (!metadata_text_value
) {
1001 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1006 ret
= bt_value_string_get(metadata_text_value
, &metadata_text
);
1008 printf("%s\n", metadata_text
);
1013 bt_put(metadata_text_value
);
1018 struct cmd_run_ctx
{
1020 GHashTable
*components
;
1023 struct bt_graph
*graph
;
1026 struct bt_config
*cfg
;
1032 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1033 struct cmd_run_ctx
*ctx
, struct bt_component
*upstream_comp
,
1034 struct bt_port
*upstream_port
,
1035 struct bt_config_connection
*cfg_conn
)
1038 GQuark downstreamp_comp_name_quark
;
1039 struct bt_component
*downstream_comp
;
1040 int64_t downstream_port_count
;
1042 int64_t (*port_count_fn
)(struct bt_component
*);
1043 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
1046 BT_LOGI("Connecting upstream port to the next available downstream port: "
1047 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1048 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1049 upstream_port
, bt_port_get_name(upstream_port
),
1050 cfg_conn
->downstream_comp_name
->str
,
1051 cfg_conn
->arg
->str
);
1052 downstreamp_comp_name_quark
= g_quark_from_string(
1053 cfg_conn
->downstream_comp_name
->str
);
1054 assert(downstreamp_comp_name_quark
> 0);
1055 downstream_comp
= g_hash_table_lookup(ctx
->components
,
1056 (gpointer
) (long) downstreamp_comp_name_quark
);
1057 if (!downstream_comp
) {
1058 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1059 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1060 cfg_conn
->arg
->str
);
1061 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1062 cfg_conn
->arg
->str
);
1066 if (bt_component_is_filter(downstream_comp
)) {
1067 port_count_fn
= bt_component_filter_get_input_port_count
;
1068 port_by_index_fn
= bt_component_filter_get_input_port_by_index
;
1069 } else if (bt_component_is_sink(downstream_comp
)) {
1070 port_count_fn
= bt_component_sink_get_input_port_count
;
1071 port_by_index_fn
= bt_component_sink_get_input_port_by_index
;
1074 * Should never happen because the connections are
1075 * validated before we get here.
1077 BT_LOGF("Invalid connection: downstream component is a source: "
1078 "conn-arg=\"%s\"", cfg_conn
->arg
->str
);
1082 downstream_port_count
= port_count_fn(downstream_comp
);
1083 assert(downstream_port_count
>= 0);
1085 for (i
= 0; i
< downstream_port_count
; i
++) {
1086 struct bt_port
*downstream_port
=
1087 port_by_index_fn(downstream_comp
, i
);
1088 const char *downstream_port_name
;
1090 assert(downstream_port
);
1092 /* Skip port if it's already connected */
1093 if (bt_port_is_connected(downstream_port
)) {
1094 bt_put(downstream_port
);
1095 BT_LOGD("Skipping downstream port: already connected: "
1096 "port-addr=%p, port-name=\"%s\"",
1098 bt_port_get_name(downstream_port
));
1102 downstream_port_name
= bt_port_get_name(downstream_port
);
1103 assert(downstream_port_name
);
1105 if (bt_common_star_glob_match(
1106 cfg_conn
->downstream_port_glob
->str
, -1ULL,
1107 downstream_port_name
, -1ULL)) {
1108 /* We have a winner! */
1109 conn
= bt_graph_connect_ports(ctx
->graph
,
1110 upstream_port
, downstream_port
);
1111 bt_put(downstream_port
);
1113 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1114 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1115 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1116 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1117 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1119 upstream_comp
, bt_component_get_name(upstream_comp
),
1120 upstream_port
, bt_port_get_name(upstream_port
),
1121 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1122 downstream_port
, downstream_port_name
,
1123 cfg_conn
->arg
->str
);
1125 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1126 bt_port_get_name(upstream_port
),
1127 downstream_port_name
,
1128 cfg_conn
->arg
->str
);
1132 BT_LOGI("Connected component ports: "
1133 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1134 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1135 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1136 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1138 upstream_comp
, bt_component_get_name(upstream_comp
),
1139 upstream_port
, bt_port_get_name(upstream_port
),
1140 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1141 downstream_port
, downstream_port_name
,
1142 cfg_conn
->arg
->str
);
1147 bt_put(downstream_port
);
1151 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1152 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1153 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1154 upstream_port
, bt_port_get_name(upstream_port
),
1155 cfg_conn
->downstream_comp_name
->str
,
1156 cfg_conn
->arg
->str
);
1158 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1159 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1174 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1175 struct bt_port
*upstream_port
)
1178 const char *upstream_port_name
;
1179 const char *upstream_comp_name
;
1180 struct bt_component
*upstream_comp
= NULL
;
1184 assert(upstream_port
);
1185 upstream_port_name
= bt_port_get_name(upstream_port
);
1186 assert(upstream_port_name
);
1187 upstream_comp
= bt_port_get_component(upstream_port
);
1188 if (!upstream_comp
) {
1189 BT_LOGW("Upstream port to connect is not part of a component: "
1190 "port-addr=%p, port-name=\"%s\"",
1191 upstream_port
, upstream_port_name
);
1196 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1197 assert(upstream_comp_name
);
1198 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1199 "port-addr=%p, port-name=\"%s\"",
1200 upstream_comp
, upstream_comp_name
,
1201 upstream_port
, upstream_port_name
);
1203 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1204 struct bt_config_connection
*cfg_conn
=
1206 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1208 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1209 upstream_comp_name
) == 0) {
1210 if (bt_common_star_glob_match(
1211 cfg_conn
->upstream_port_glob
->str
,
1212 -1ULL, upstream_port_name
, -1ULL)) {
1213 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1214 ctx
, upstream_comp
, upstream_port
,
1217 BT_LOGE("Cannot connect upstream port: "
1218 "port-addr=%p, port-name=\"%s\"",
1220 upstream_port_name
);
1222 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1225 cfg_conn
->arg
->str
);
1234 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1235 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1236 upstream_port_name
);
1238 "Cannot create connection: upstream port `%s` does not match any connection\n",
1239 upstream_port_name
);
1245 bt_put(upstream_comp
);
1250 void graph_port_added_listener(struct bt_port
*port
, void *data
)
1252 struct bt_component
*comp
= NULL
;
1253 struct cmd_run_ctx
*ctx
= data
;
1255 comp
= bt_port_get_component(port
);
1256 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1257 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1258 comp
, comp
? bt_component_get_name(comp
) : "",
1259 port
, bt_port_get_name(port
));
1261 BT_LOGW_STR("Port has no component.");
1265 if (bt_port_is_connected(port
)) {
1266 BT_LOGW_STR("Port is already connected.");
1270 if (!bt_port_is_output(port
)) {
1271 BT_LOGI_STR("Skipping input port.");
1275 if (cmd_run_ctx_connect_upstream_port(ctx
, port
)) {
1276 BT_LOGF_STR("Cannot connect upstream port.");
1277 fprintf(stderr
, "Added port could not be connected: aborting\n");
1287 void graph_port_removed_listener(struct bt_component
*component
,
1288 struct bt_port
*port
, void *data
)
1290 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1291 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1292 component
, bt_component_get_name(component
),
1293 port
, bt_port_get_name(port
));
1297 void graph_ports_connected_listener(struct bt_port
*upstream_port
,
1298 struct bt_port
*downstream_port
, void *data
)
1300 struct bt_component
*upstream_comp
= bt_port_get_component(upstream_port
);
1301 struct bt_component
*downstream_comp
= bt_port_get_component(downstream_port
);
1303 assert(upstream_comp
);
1304 assert(downstream_comp
);
1305 BT_LOGI("Graph's component ports connected: "
1306 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1307 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1308 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1309 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1310 upstream_comp
, bt_component_get_name(upstream_comp
),
1311 upstream_port
, bt_port_get_name(upstream_port
),
1312 downstream_comp
, bt_component_get_name(downstream_comp
),
1313 downstream_port
, bt_port_get_name(downstream_port
));
1314 bt_put(upstream_comp
);
1315 bt_put(downstream_comp
);
1319 void graph_ports_disconnected_listener(
1320 struct bt_component
*upstream_component
,
1321 struct bt_component
*downstream_component
,
1322 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
1325 BT_LOGI("Graph's component ports disconnected: "
1326 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1327 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1328 upstream_port
, bt_port_get_name(upstream_port
),
1329 downstream_port
, bt_port_get_name(downstream_port
));
1333 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1339 if (ctx
->components
) {
1340 g_hash_table_destroy(ctx
->components
);
1341 ctx
->components
= NULL
;
1350 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1355 ctx
->connect_ports
= false;
1356 ctx
->components
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
1358 if (!ctx
->components
) {
1362 ctx
->graph
= bt_graph_create();
1367 the_graph
= ctx
->graph
;
1368 ret
= bt_graph_add_port_added_listener(ctx
->graph
,
1369 graph_port_added_listener
, ctx
);
1374 ret
= bt_graph_add_port_removed_listener(ctx
->graph
,
1375 graph_port_removed_listener
, ctx
);
1380 ret
= bt_graph_add_ports_connected_listener(ctx
->graph
,
1381 graph_ports_connected_listener
, ctx
);
1386 ret
= bt_graph_add_ports_disconnected_listener(ctx
->graph
,
1387 graph_ports_disconnected_listener
, ctx
);
1395 cmd_run_ctx_destroy(ctx
);
1403 int cmd_run_ctx_create_components_from_config_components(
1404 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
1407 struct bt_component_class
*comp_cls
= NULL
;
1408 struct bt_component
*comp
= NULL
;
1411 for (i
= 0; i
< cfg_components
->len
; i
++) {
1412 struct bt_config_component
*cfg_comp
=
1413 g_ptr_array_index(cfg_components
, i
);
1416 comp_cls
= find_component_class(cfg_comp
->plugin_name
->str
,
1417 cfg_comp
->comp_cls_name
->str
, cfg_comp
->type
);
1419 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1420 "comp-cls-name=\"%s\", comp-cls-type=%d",
1421 cfg_comp
->plugin_name
->str
,
1422 cfg_comp
->comp_cls_name
->str
,
1424 fprintf(stderr
, "%s%sCannot find component class %s",
1425 bt_common_color_bold(),
1426 bt_common_color_fg_red(),
1427 bt_common_color_reset());
1428 print_plugin_comp_cls_opt(stderr
,
1429 cfg_comp
->plugin_name
->str
,
1430 cfg_comp
->comp_cls_name
->str
,
1432 fprintf(stderr
, "\n");
1436 comp
= bt_component_create(comp_cls
,
1437 cfg_comp
->instance_name
->str
, cfg_comp
->params
);
1439 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
1440 "comp-cls-name=\"%s\", comp-cls-type=%d",
1442 cfg_comp
->plugin_name
->str
,
1443 cfg_comp
->comp_cls_name
->str
,
1444 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
1445 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
1446 bt_common_color_bold(),
1447 bt_common_color_fg_red(),
1448 cfg_comp
->instance_name
->str
,
1449 bt_common_color_reset());
1453 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
1454 comp
, cfg_comp
->instance_name
->str
);
1455 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
1457 g_hash_table_insert(ctx
->components
,
1458 (gpointer
) (long) quark
, comp
);
1475 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
1480 * Make sure that, during this phase, our graph's "port added"
1481 * listener does not connect ports while we are creating the
1482 * components because we have a special, initial phase for
1485 ctx
->connect_ports
= false;
1487 ret
= cmd_run_ctx_create_components_from_config_components(
1488 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
1494 ret
= cmd_run_ctx_create_components_from_config_components(
1495 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
1501 ret
= cmd_run_ctx_create_components_from_config_components(
1502 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
1513 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
1514 struct bt_component
*comp
,
1515 int64_t (*port_count_fn
)(struct bt_component
*),
1516 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t))
1522 count
= port_count_fn(comp
);
1525 for (i
= 0; i
< count
; i
++) {
1526 struct bt_port
*upstream_port
= port_by_index_fn(comp
, i
);
1528 assert(upstream_port
);
1529 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
1530 bt_put(upstream_port
);
1541 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
1544 GHashTableIter iter
;
1545 gpointer g_name_quark
, g_comp
;
1547 ctx
->connect_ports
= true;
1548 g_hash_table_iter_init(&iter
, ctx
->components
);
1550 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
1551 if (bt_component_is_source(g_comp
)) {
1552 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
1553 g_comp
, bt_component_source_get_output_port_count
,
1554 bt_component_source_get_output_port_by_index
);
1555 } else if (bt_component_is_filter(g_comp
)) {
1556 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
1557 g_comp
, bt_component_filter_get_output_port_count
,
1558 bt_component_filter_get_output_port_by_index
);
1571 const char *bt_graph_status_str(enum bt_graph_status status
)
1574 case BT_GRAPH_STATUS_CANCELED
:
1575 return "BT_GRAPH_STATUS_CANCELED";
1576 case BT_GRAPH_STATUS_AGAIN
:
1577 return "BT_GRAPH_STATUS_AGAIN";
1578 case BT_GRAPH_STATUS_END
:
1579 return "BT_GRAPH_STATUS_END";
1580 case BT_GRAPH_STATUS_OK
:
1581 return "BT_GRAPH_STATUS_OK";
1582 case BT_GRAPH_STATUS_ALREADY_IN_A_GRAPH
:
1583 return "BT_GRAPH_STATUS_ALREADY_IN_A_GRAPH";
1584 case BT_GRAPH_STATUS_INVALID
:
1585 return "BT_GRAPH_STATUS_INVALID";
1586 case BT_GRAPH_STATUS_NO_SINK
:
1587 return "BT_GRAPH_STATUS_NO_SINK";
1588 case BT_GRAPH_STATUS_ERROR
:
1589 return "BT_GRAPH_STATUS_ERROR";
1596 int cmd_run(struct bt_config
*cfg
)
1599 struct cmd_run_ctx ctx
= { 0 };
1601 /* Initialize the command's context and the graph object */
1602 if (cmd_run_ctx_init(&ctx
, cfg
)) {
1603 BT_LOGE_STR("Cannot initialize the command's context.");
1604 fprintf(stderr
, "Cannot initialize the command's context\n");
1608 /* Create the requested component instances */
1609 if (cmd_run_ctx_create_components(&ctx
)) {
1610 BT_LOGE_STR("Cannot create components.");
1611 fprintf(stderr
, "Cannot create components\n");
1615 /* Connect the initially visible component ports */
1616 if (cmd_run_ctx_connect_ports(&ctx
)) {
1617 BT_LOGE_STR("Cannot connect initial component ports.");
1618 fprintf(stderr
, "Cannot connect initial component ports\n");
1626 BT_LOGI_STR("Running the graph.");
1630 enum bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
1632 BT_LOGV("bt_graph_run() returned: status=%s",
1633 bt_graph_status_str(graph_status
));
1635 switch (graph_status
) {
1636 case BT_GRAPH_STATUS_OK
:
1638 case BT_GRAPH_STATUS_CANCELED
:
1639 BT_LOGI_STR("Graph was canceled by user.");
1641 case BT_GRAPH_STATUS_AGAIN
:
1642 if (bt_graph_is_canceled(ctx
.graph
)) {
1643 BT_LOGI_STR("Graph was canceled by user.");
1647 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
1648 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
1650 cfg
->cmd_data
.run
.retry_duration_us
);
1652 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
1653 if (bt_graph_is_canceled(ctx
.graph
)) {
1654 BT_LOGI_STR("Graph was canceled by user.");
1660 case BT_COMPONENT_STATUS_END
:
1663 BT_LOGE_STR("Graph failed to complete successfully");
1664 fprintf(stderr
, "Graph failed to complete successfully\n");
1677 cmd_run_ctx_destroy(&ctx
);
1682 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
1684 const char *env_clash
;
1686 if (!cfg
->command_name
) {
1690 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
1691 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
1695 if (g_file_test(cfg
->command_name
,
1696 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
1697 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
1699 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
1701 fprintf(stderr
, "\n");
1702 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
1708 void init_log_level(void)
1710 enum bt_logging_level log_level
= BT_LOG_NONE
;
1711 const char *log_level_env
= getenv("BABELTRACE_CLI_LOG_LEVEL");
1713 if (!log_level_env
) {
1717 if (strcmp(log_level_env
, "VERBOSE") == 0) {
1718 log_level
= BT_LOGGING_LEVEL_VERBOSE
;
1719 } else if (strcmp(log_level_env
, "DEBUG") == 0) {
1720 log_level
= BT_LOGGING_LEVEL_DEBUG
;
1721 } else if (strcmp(log_level_env
, "INFO") == 0) {
1722 log_level
= BT_LOGGING_LEVEL_INFO
;
1723 } else if (strcmp(log_level_env
, "WARN") == 0) {
1724 log_level
= BT_LOGGING_LEVEL_WARN
;
1725 } else if (strcmp(log_level_env
, "ERROR") == 0) {
1726 log_level
= BT_LOGGING_LEVEL_ERROR
;
1727 } else if (strcmp(log_level_env
, "FATAL") == 0) {
1728 log_level
= BT_LOGGING_LEVEL_FATAL
;
1729 } else if (strcmp(log_level_env
, "NONE") == 0) {
1730 log_level
= BT_LOGGING_LEVEL_NONE
;
1734 bt_cli_log_level
= log_level
;
1737 void set_sigint_handler(void)
1739 struct sigaction new_action
, old_action
;
1741 new_action
.sa_handler
= sigint_handler
;
1742 sigemptyset(&new_action
.sa_mask
);
1743 new_action
.sa_flags
= 0;
1744 sigaction(SIGINT
, NULL
, &old_action
);
1746 if (old_action
.sa_handler
!= SIG_IGN
) {
1747 sigaction(SIGINT
, &new_action
, NULL
);
1751 int main(int argc
, const char **argv
)
1755 struct bt_config
*cfg
;
1758 set_sigint_handler();
1760 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
1763 /* Quit without errors; typically usage/version */
1765 BT_LOGI_STR("Quitting without errors.");
1770 BT_LOGE("Command-line error: retcode=%d", retcode
);
1775 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
1776 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
1782 bt_cli_log_level
= BT_LOGGING_LEVEL_VERBOSE
;
1783 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
1784 // TODO: for backward compat., set the log level
1785 // environment variables of the known plugins
1787 } else if (cfg
->debug
) {
1788 bt_cli_log_level
= BT_LOGGING_LEVEL_DEBUG
;
1789 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
1790 // TODO: for backward compat., set the log level
1791 // environment variables of the known plugins
1795 babeltrace_debug
= cfg
->debug
;
1796 babeltrace_verbose
= cfg
->verbose
;
1799 if (cfg
->command_needs_plugins
) {
1800 ret
= load_all_plugins(cfg
->plugin_paths
);
1802 BT_LOGE("Failed to load plugins: ret=%d", ret
);
1808 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
1809 cfg
->command
, cfg
->command_name
);
1811 switch (cfg
->command
) {
1812 case BT_CONFIG_COMMAND_RUN
:
1815 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
1816 ret
= cmd_list_plugins(cfg
);
1818 case BT_CONFIG_COMMAND_HELP
:
1819 ret
= cmd_help(cfg
);
1821 case BT_CONFIG_COMMAND_QUERY
:
1822 ret
= cmd_query(cfg
);
1824 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
1825 ret
= cmd_print_ctf_metadata(cfg
);
1827 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
1828 ret
= cmd_print_lttng_live_sessions(cfg
);
1831 BT_LOGF("Invalid command: cmd=%d", cfg
->command
);
1835 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
1836 cfg
->command
, cfg
->command_name
, ret
);
1837 warn_command_name_and_directory_clash(cfg
);
1838 retcode
= ret
? 1 : 0;