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/logging.h>
56 #include "babeltrace-cfg.h"
57 #include "babeltrace-cfg-cli-args.h"
58 #include "babeltrace-cfg-cli-args-default.h"
60 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
61 #define ENV_BABELTRACE_CLI_LOG_LEVEL "BABELTRACE_CLI_LOG_LEVEL"
64 * Known environment variable names for the log levels of the project's
67 static const char* log_level_env_var_names
[] = {
68 "BABELTRACE_COMMON_LOG_LEVEL",
69 "BABELTRACE_PLUGIN_CTF_BTR_LOG_LEVEL",
70 "BABELTRACE_PLUGIN_CTF_FS_SRC_LOG_LEVEL",
71 "BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_SRC_LOG_LEVEL",
72 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
73 "BABELTRACE_PLUGIN_CTF_NOTIF_ITER_LOG_LEVEL",
74 "BABELTRACE_PLUGIN_LTTNG_UTILS_DEBUG_INFO_FLT_LOG_LEVEL",
75 "BABELTRACE_PLUGIN_UTILS_TRIMMER_FLT_LOG_LEVEL",
76 "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
80 /* Application's processing graph (weak) */
81 static struct bt_graph
*the_graph
;
82 static bool canceled
= false;
84 GPtrArray
*loaded_plugins
;
87 void sigint_handler(int signum
)
89 if (signum
!= SIGINT
) {
94 bt_graph_cancel(the_graph
);
101 void init_static_data(void)
103 loaded_plugins
= g_ptr_array_new_with_free_func(bt_put
);
107 void fini_static_data(void)
109 g_ptr_array_free(loaded_plugins
, TRUE
);
113 struct bt_plugin
*find_plugin(const char *name
)
116 struct bt_plugin
*plugin
= NULL
;
119 BT_LOGD("Finding plugin: name=\"%s\"", name
);
121 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
122 plugin
= g_ptr_array_index(loaded_plugins
, i
);
124 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
131 if (BT_LOG_ON_DEBUG
) {
133 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
135 BT_LOGD("Cannot find plugin.");
139 return bt_get(plugin
);
143 struct bt_component_class
*find_component_class(const char *plugin_name
,
144 const char *comp_class_name
,
145 enum bt_component_class_type comp_class_type
)
147 struct bt_component_class
*comp_class
= NULL
;
148 struct bt_plugin
*plugin
;
150 BT_LOGD("Finding component class: plugin-name=\"%s\", "
151 "comp-cls-name=\"%s\", comp-cls-type=%d",
152 plugin_name
, comp_class_name
, comp_class_type
);
154 plugin
= find_plugin(plugin_name
);
160 comp_class
= bt_plugin_get_component_class_by_name_and_type(plugin
,
161 comp_class_name
, comp_class_type
);
165 if (BT_LOG_ON_DEBUG
) {
167 BT_LOGD("Found component class: comp-cls-addr=%p",
170 BT_LOGD("Cannot find component class.");
178 void print_indent(FILE *fp
, size_t indent
)
182 for (i
= 0; i
< indent
; i
++) {
188 const char *component_type_str(enum bt_component_class_type type
)
191 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
193 case BT_COMPONENT_CLASS_TYPE_SINK
:
195 case BT_COMPONENT_CLASS_TYPE_FILTER
:
197 case BT_COMPONENT_CLASS_TYPE_UNKNOWN
:
204 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
205 const char *comp_cls_name
, enum bt_component_class_type type
)
207 GString
*shell_plugin_name
= NULL
;
208 GString
*shell_comp_cls_name
= NULL
;
210 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
211 if (!shell_plugin_name
) {
215 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
216 if (!shell_comp_cls_name
) {
220 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
221 bt_common_color_bold(),
222 bt_common_color_fg_cyan(),
223 component_type_str(type
),
224 bt_common_color_fg_default(),
225 bt_common_color_fg_blue(),
226 shell_plugin_name
->str
,
227 bt_common_color_fg_default(),
228 bt_common_color_fg_yellow(),
229 shell_comp_cls_name
->str
,
230 bt_common_color_reset());
233 if (shell_plugin_name
) {
234 g_string_free(shell_plugin_name
, TRUE
);
237 if (shell_comp_cls_name
) {
238 g_string_free(shell_comp_cls_name
, TRUE
);
243 void print_value(FILE *, struct bt_value
*, size_t);
246 void print_value_rec(FILE *, struct bt_value
*, size_t);
248 struct print_map_value_data
{
254 bt_bool
print_map_value(const char *key
, struct bt_value
*object
, void *data
)
256 struct print_map_value_data
*print_map_value_data
= data
;
258 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
259 fprintf(print_map_value_data
->fp
, "%s: ", key
);
261 if (bt_value_is_array(object
) &&
262 bt_value_array_is_empty(object
)) {
263 fprintf(print_map_value_data
->fp
, "[ ]\n");
267 if (bt_value_is_map(object
) &&
268 bt_value_map_is_empty(object
)) {
269 fprintf(print_map_value_data
->fp
, "{ }\n");
273 if (bt_value_is_array(object
) ||
274 bt_value_is_map(object
)) {
275 fprintf(print_map_value_data
->fp
, "\n");
278 print_value_rec(print_map_value_data
->fp
, object
,
279 print_map_value_data
->indent
+ 2);
284 void print_value_rec(FILE *fp
, struct bt_value
*value
, size_t indent
)
297 switch (bt_value_get_type(value
)) {
298 case BT_VALUE_TYPE_NULL
:
299 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
300 bt_common_color_reset());
302 case BT_VALUE_TYPE_BOOL
:
303 bt_value_bool_get(value
, &bool_val
);
304 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
305 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
306 bt_common_color_reset());
308 case BT_VALUE_TYPE_INTEGER
:
309 bt_value_integer_get(value
, &int_val
);
310 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
311 bt_common_color_fg_red(), int_val
,
312 bt_common_color_reset());
314 case BT_VALUE_TYPE_FLOAT
:
315 bt_value_float_get(value
, &dbl_val
);
316 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
317 bt_common_color_fg_red(), dbl_val
,
318 bt_common_color_reset());
320 case BT_VALUE_TYPE_STRING
:
321 bt_value_string_get(value
, &str_val
);
322 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
323 bt_common_color_fg_green(), str_val
,
324 bt_common_color_reset());
326 case BT_VALUE_TYPE_ARRAY
:
327 size
= bt_value_array_size(value
);
331 print_indent(fp
, indent
);
332 fprintf(fp
, "[ ]\n");
336 for (i
= 0; i
< size
; i
++) {
337 struct bt_value
*element
=
338 bt_value_array_get(value
, i
);
341 print_indent(fp
, indent
);
344 if (bt_value_is_array(element
) &&
345 bt_value_array_is_empty(element
)) {
346 fprintf(fp
, "[ ]\n");
350 if (bt_value_is_map(element
) &&
351 bt_value_map_is_empty(element
)) {
352 fprintf(fp
, "{ }\n");
356 if (bt_value_is_array(element
) ||
357 bt_value_is_map(element
)) {
361 print_value_rec(fp
, element
, indent
+ 2);
365 case BT_VALUE_TYPE_MAP
:
367 struct print_map_value_data data
= {
372 if (bt_value_map_is_empty(value
)) {
373 print_indent(fp
, indent
);
374 fprintf(fp
, "{ }\n");
378 bt_value_map_foreach(value
, print_map_value
, &data
);
387 void print_value(FILE *fp
, struct bt_value
*value
, size_t indent
)
389 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
390 print_indent(fp
, indent
);
393 print_value_rec(fp
, value
, indent
);
397 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
399 fprintf(stderr
, " ");
400 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
401 bt_config_component
->comp_cls_name
->str
,
402 bt_config_component
->type
);
403 fprintf(stderr
, ":\n");
405 if (bt_config_component
->instance_name
->len
> 0) {
406 fprintf(stderr
, " Name: %s\n",
407 bt_config_component
->instance_name
->str
);
410 fprintf(stderr
, " Parameters:\n");
411 print_value(stderr
, bt_config_component
->params
, 8);
415 void print_bt_config_components(GPtrArray
*array
)
419 for (i
= 0; i
< array
->len
; i
++) {
420 struct bt_config_component
*cfg_component
=
421 bt_config_get_component(array
, i
);
422 print_bt_config_component(cfg_component
);
423 BT_PUT(cfg_component
);
428 void print_plugin_paths(struct bt_value
*plugin_paths
)
430 fprintf(stderr
, " Plugin paths:\n");
431 print_value(stderr
, plugin_paths
, 4);
435 void print_cfg_run(struct bt_config
*cfg
)
439 print_plugin_paths(cfg
->plugin_paths
);
440 fprintf(stderr
, " Source component instances:\n");
441 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
443 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
444 fprintf(stderr
, " Filter component instances:\n");
445 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
448 fprintf(stderr
, " Sink component instances:\n");
449 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
450 fprintf(stderr
, " Connections:\n");
452 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
453 struct bt_config_connection
*cfg_connection
=
454 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
457 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
458 cfg_connection
->upstream_comp_name
->str
,
459 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
460 cfg_connection
->upstream_port_glob
->str
,
461 cfg_connection
->downstream_comp_name
->str
,
462 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
463 cfg_connection
->downstream_port_glob
->str
);
468 void print_cfg_list_plugins(struct bt_config
*cfg
)
470 print_plugin_paths(cfg
->plugin_paths
);
474 void print_cfg_help(struct bt_config
*cfg
)
476 print_plugin_paths(cfg
->plugin_paths
);
480 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
482 print_plugin_paths(cfg
->plugin_paths
);
483 fprintf(stderr
, " Path: %s\n",
484 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
488 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
490 print_plugin_paths(cfg
->plugin_paths
);
491 fprintf(stderr
, " URL: %s\n",
492 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
496 void print_cfg_query(struct bt_config
*cfg
)
498 print_plugin_paths(cfg
->plugin_paths
);
499 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
500 fprintf(stderr
, " Component class:\n");
501 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
505 void print_cfg(struct bt_config
*cfg
)
507 if (!BT_LOG_ON_INFO
) {
511 BT_LOGI_STR("Configuration:");
512 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
513 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
515 switch (cfg
->command
) {
516 case BT_CONFIG_COMMAND_RUN
:
519 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
520 print_cfg_list_plugins(cfg
);
522 case BT_CONFIG_COMMAND_HELP
:
525 case BT_CONFIG_COMMAND_QUERY
:
526 print_cfg_query(cfg
);
528 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
529 print_cfg_print_ctf_metadata(cfg
);
531 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
532 print_cfg_print_lttng_live_sessions(cfg
);
540 void add_to_loaded_plugins(struct bt_plugin_set
*plugin_set
)
545 count
= bt_plugin_set_get_plugin_count(plugin_set
);
548 for (i
= 0; i
< count
; i
++) {
549 struct bt_plugin
*plugin
=
550 bt_plugin_set_get_plugin(plugin_set
, i
);
551 struct bt_plugin
*loaded_plugin
=
552 find_plugin(bt_plugin_get_name(plugin
));
557 BT_LOGI("Not using plugin: another one already exists with the same name: "
558 "plugin-name=\"%s\", plugin-path=\"%s\", "
559 "existing-plugin-path=\"%s\"",
560 bt_plugin_get_name(plugin
),
561 bt_plugin_get_path(plugin
),
562 bt_plugin_get_path(loaded_plugin
));
563 bt_put(loaded_plugin
);
565 /* Add to global array. */
566 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
567 bt_plugin_get_name(plugin
));
568 g_ptr_array_add(loaded_plugins
, bt_get(plugin
));
576 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
578 int nr_paths
, i
, ret
= 0;
580 nr_paths
= bt_value_array_size(plugin_paths
);
582 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
587 BT_LOGI("Loading dynamic plugins.");
589 for (i
= 0; i
< nr_paths
; i
++) {
590 struct bt_value
*plugin_path_value
= NULL
;
591 const char *plugin_path
;
592 struct bt_plugin_set
*plugin_set
;
594 plugin_path_value
= bt_value_array_get(plugin_paths
, i
);
595 bt_value_string_get(plugin_path_value
, &plugin_path
);
599 * Skip this if the directory does not exist because
600 * bt_plugin_create_all_from_dir() expects an existing
603 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
604 BT_LOGV("Skipping nonexistent directory path: "
605 "path=\"%s\"", plugin_path
);
606 BT_PUT(plugin_path_value
);
610 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
612 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
614 BT_PUT(plugin_path_value
);
618 add_to_loaded_plugins(plugin_set
);
620 BT_PUT(plugin_path_value
);
627 int load_static_plugins(void)
630 struct bt_plugin_set
*plugin_set
;
632 BT_LOGI("Loading static plugins.");
633 plugin_set
= bt_plugin_create_all_from_static();
635 BT_LOGE("Unable to load static plugins.");
640 add_to_loaded_plugins(plugin_set
);
647 int load_all_plugins(struct bt_value
*plugin_paths
)
651 if (load_dynamic_plugins(plugin_paths
)) {
656 if (load_static_plugins()) {
661 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
668 void print_plugin_info(struct bt_plugin
*plugin
)
670 unsigned int major
, minor
, patch
;
672 enum bt_plugin_status version_status
;
673 const char *plugin_name
;
677 const char *plugin_description
;
679 plugin_name
= bt_plugin_get_name(plugin
);
680 path
= bt_plugin_get_path(plugin
);
681 author
= bt_plugin_get_author(plugin
);
682 license
= bt_plugin_get_license(plugin
);
683 plugin_description
= bt_plugin_get_description(plugin
);
684 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
686 printf("%s%s%s%s:\n", bt_common_color_bold(),
687 bt_common_color_fg_blue(), plugin_name
,
688 bt_common_color_reset());
689 printf(" %sPath%s: %s\n", bt_common_color_bold(),
690 bt_common_color_reset(), path
? path
: "(None)");
692 if (version_status
== BT_PLUGIN_STATUS_OK
) {
693 printf(" %sVersion%s: %u.%u.%u",
694 bt_common_color_bold(), bt_common_color_reset(),
695 major
, minor
, patch
);
704 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
705 bt_common_color_reset(),
706 plugin_description
? plugin_description
: "(None)");
707 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
708 bt_common_color_reset(), author
? author
: "(Unknown)");
709 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
710 bt_common_color_reset(),
711 license
? license
: "(Unknown)");
715 int cmd_query(struct bt_config
*cfg
)
718 struct bt_component_class
*comp_cls
= NULL
;
719 struct bt_value
*results
= NULL
;
721 comp_cls
= find_component_class(cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
722 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
723 cfg
->cmd_data
.query
.cfg_component
->type
);
725 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
726 "comp-cls-name=\"%s\", comp-cls-type=%d",
727 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
728 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
729 cfg
->cmd_data
.query
.cfg_component
->type
);
730 fprintf(stderr
, "%s%sCannot find component class %s",
731 bt_common_color_bold(),
732 bt_common_color_fg_red(),
733 bt_common_color_reset());
734 print_plugin_comp_cls_opt(stderr
,
735 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
736 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
737 cfg
->cmd_data
.query
.cfg_component
->type
);
738 fprintf(stderr
, "\n");
743 results
= bt_component_class_query(comp_cls
,
744 cfg
->cmd_data
.query
.object
->str
,
745 cfg
->cmd_data
.query
.cfg_component
->params
);
747 BT_LOGE("Failed to query component class: plugin-name=\"%s\", "
748 "comp-cls-name=\"%s\", comp-cls-type=%d "
750 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
751 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
752 cfg
->cmd_data
.query
.cfg_component
->type
,
753 cfg
->cmd_data
.query
.object
->str
);
754 fprintf(stderr
, "%s%sFailed to query info to %s",
755 bt_common_color_bold(),
756 bt_common_color_fg_red(),
757 bt_common_color_reset());
758 print_plugin_comp_cls_opt(stderr
,
759 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
760 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
761 cfg
->cmd_data
.query
.cfg_component
->type
);
762 fprintf(stderr
, "%s%s with object `%s`%s\n",
763 bt_common_color_bold(),
764 bt_common_color_fg_red(),
765 cfg
->cmd_data
.query
.object
->str
,
766 bt_common_color_reset());
771 print_value(stdout
, results
, 0);
780 int cmd_help(struct bt_config
*cfg
)
783 struct bt_plugin
*plugin
= NULL
;
786 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
788 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
789 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
790 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
791 bt_common_color_bold(), bt_common_color_fg_red(),
792 bt_common_color_fg_blue(),
793 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
794 bt_common_color_reset());
799 print_plugin_info(plugin
);
800 printf(" %sComponent classes%s: %d\n",
801 bt_common_color_bold(),
802 bt_common_color_reset(),
803 (int) bt_plugin_get_component_class_count(plugin
));
806 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
807 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
808 struct bt_component_class
*needed_comp_cls
=
809 find_component_class(
810 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
811 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
812 cfg
->cmd_data
.help
.cfg_component
->type
);
814 if (!needed_comp_cls
) {
815 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
816 "comp-cls-name=\"%s\", comp-cls-type=%d",
817 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
818 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
819 cfg
->cmd_data
.help
.cfg_component
->type
);
820 fprintf(stderr
, "\n%s%sCannot find component class %s",
821 bt_common_color_bold(),
822 bt_common_color_fg_red(),
823 bt_common_color_reset());
824 print_plugin_comp_cls_opt(stderr
,
825 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
826 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
827 cfg
->cmd_data
.help
.cfg_component
->type
);
828 fprintf(stderr
, "\n");
833 bt_put(needed_comp_cls
);
836 for (i
= 0; i
< bt_plugin_get_component_class_count(plugin
); i
++) {
837 struct bt_component_class
*comp_cls
=
838 bt_plugin_get_component_class_by_index(plugin
, i
);
839 const char *comp_class_name
=
840 bt_component_class_get_name(comp_cls
);
841 const char *comp_class_description
=
842 bt_component_class_get_description(comp_cls
);
843 const char *comp_class_help
=
844 bt_component_class_get_help(comp_cls
);
845 enum bt_component_class_type type
=
846 bt_component_class_get_type(comp_cls
);
850 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
851 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
852 if (strcmp(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
853 comp_class_name
) != 0 &&
855 cfg
->cmd_data
.help
.cfg_component
->type
) {
862 print_plugin_comp_cls_opt(stdout
,
863 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
867 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
868 bt_common_color_reset(),
869 comp_class_description
? comp_class_description
: "(None)");
871 if (comp_class_help
) {
872 printf("\n%s\n", comp_class_help
);
884 int cmd_list_plugins(struct bt_config
*cfg
)
887 int plugins_count
, component_classes_count
= 0, i
;
889 printf("From the following plugin paths:\n\n");
890 print_value(stdout
, cfg
->plugin_paths
, 2);
892 plugins_count
= loaded_plugins
->len
;
893 if (plugins_count
== 0) {
894 printf("No plugins found.\n");
898 for (i
= 0; i
< plugins_count
; i
++) {
899 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
901 component_classes_count
+= bt_plugin_get_component_class_count(plugin
);
904 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
905 bt_common_color_bold(),
906 component_classes_count
,
907 bt_common_color_reset(),
908 bt_common_color_bold(),
910 bt_common_color_reset());
912 for (i
= 0; i
< plugins_count
; i
++) {
914 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
916 component_classes_count
=
917 bt_plugin_get_component_class_count(plugin
);
919 print_plugin_info(plugin
);
921 if (component_classes_count
== 0) {
922 printf(" %sComponent classes%s: (none)\n",
923 bt_common_color_bold(),
924 bt_common_color_reset());
926 printf(" %sComponent classes%s:\n",
927 bt_common_color_bold(),
928 bt_common_color_reset());
931 for (j
= 0; j
< component_classes_count
; j
++) {
932 struct bt_component_class
*comp_class
=
933 bt_plugin_get_component_class_by_index(
935 const char *comp_class_name
=
936 bt_component_class_get_name(comp_class
);
937 const char *comp_class_description
=
938 bt_component_class_get_description(comp_class
);
939 enum bt_component_class_type type
=
940 bt_component_class_get_type(comp_class
);
943 print_plugin_comp_cls_opt(stdout
,
944 bt_plugin_get_name(plugin
), comp_class_name
,
947 if (comp_class_description
) {
948 printf(": %s", comp_class_description
);
961 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
964 struct bt_component_class
*comp_cls
= NULL
;
965 struct bt_value
*results
= NULL
;
966 struct bt_value
*params
= NULL
;
967 struct bt_value
*map
= NULL
;
968 struct bt_value
*v
= NULL
;
969 static const char * const plugin_name
= "ctf";
970 static const char * const comp_cls_name
= "lttng-live";
971 static const enum bt_component_class_type comp_cls_type
=
972 BT_COMPONENT_CLASS_TYPE_SOURCE
;
973 int64_t array_size
, i
;
975 assert(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
976 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
979 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
980 "comp-cls-name=\"%s\", comp-cls-type=%d",
981 plugin_name
, comp_cls_name
,
982 BT_COMPONENT_CLASS_TYPE_SOURCE
);
983 fprintf(stderr
, "%s%sCannot find component class %s",
984 bt_common_color_bold(),
985 bt_common_color_fg_red(),
986 bt_common_color_reset());
987 print_plugin_comp_cls_opt(stderr
, plugin_name
,
988 comp_cls_name
, comp_cls_type
);
989 fprintf(stderr
, "\n");
993 params
= bt_value_map_create();
998 ret
= bt_value_map_insert_string(params
, "url",
999 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1004 results
= bt_component_class_query(comp_cls
, "sessions",
1007 BT_LOGE_STR("Failed to query for sessions.");
1008 fprintf(stderr
, "%s%sFailed to request sessions%s\n",
1009 bt_common_color_bold(),
1010 bt_common_color_fg_red(),
1011 bt_common_color_reset());
1015 if (!bt_value_is_array(results
)) {
1016 BT_LOGE_STR("Expecting an array for sessions query.");
1017 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1018 bt_common_color_bold(),
1019 bt_common_color_fg_red(),
1020 bt_common_color_reset());
1024 array_size
= bt_value_array_size(results
);
1025 for (i
= 0; i
< array_size
; i
++) {
1026 const char *url_text
;
1027 int64_t timer_us
, streams
, clients
;
1029 map
= bt_value_array_get(results
, i
);
1031 BT_LOGE_STR("Unexpected empty array entry.");
1034 if (!bt_value_is_map(map
)) {
1035 BT_LOGE_STR("Unexpected entry type.");
1039 v
= bt_value_map_get(map
, "url");
1041 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1044 ret
= bt_value_string_get(v
, &url_text
);
1046 printf("%s", url_text
);
1049 v
= bt_value_map_get(map
, "timer-us");
1051 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1054 ret
= bt_value_integer_get(v
, &timer_us
);
1056 printf(" (timer = %" PRIu64
", ", timer_us
);
1059 v
= bt_value_map_get(map
, "stream-count");
1061 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1064 ret
= bt_value_integer_get(v
, &streams
);
1066 printf("%" PRIu64
" stream(s), ", streams
);
1069 v
= bt_value_map_get(map
, "client-count");
1071 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1074 ret
= bt_value_integer_get(v
, &clients
);
1076 printf("%" PRIu64
" client(s) connected)\n", clients
);
1095 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1098 struct bt_component_class
*comp_cls
= NULL
;
1099 struct bt_value
*results
= NULL
;
1100 struct bt_value
*params
= NULL
;
1101 struct bt_value
*metadata_text_value
= NULL
;
1102 const char *metadata_text
= NULL
;
1103 static const char * const plugin_name
= "ctf";
1104 static const char * const comp_cls_name
= "fs";
1105 static const enum bt_component_class_type comp_cls_type
=
1106 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1108 assert(cfg
->cmd_data
.print_ctf_metadata
.path
);
1109 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1112 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1113 "comp-cls-name=\"%s\", comp-cls-type=%d",
1114 plugin_name
, comp_cls_name
,
1115 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1116 fprintf(stderr
, "%s%sCannot find component class %s",
1117 bt_common_color_bold(),
1118 bt_common_color_fg_red(),
1119 bt_common_color_reset());
1120 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1121 comp_cls_name
, comp_cls_type
);
1122 fprintf(stderr
, "\n");
1127 params
= bt_value_map_create();
1133 ret
= bt_value_map_insert_string(params
, "path",
1134 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1140 results
= bt_component_class_query(comp_cls
, "metadata-info",
1144 BT_LOGE_STR("Failed to query for metadata info.");
1145 fprintf(stderr
, "%s%sFailed to request metadata info%s\n",
1146 bt_common_color_bold(),
1147 bt_common_color_fg_red(),
1148 bt_common_color_reset());
1152 metadata_text_value
= bt_value_map_get(results
, "text");
1153 if (!metadata_text_value
) {
1154 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1159 ret
= bt_value_string_get(metadata_text_value
, &metadata_text
);
1161 printf("%s\n", metadata_text
);
1166 bt_put(metadata_text_value
);
1171 struct cmd_run_ctx
{
1173 GHashTable
*components
;
1176 struct bt_graph
*graph
;
1179 struct bt_config
*cfg
;
1185 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1186 struct cmd_run_ctx
*ctx
, struct bt_component
*upstream_comp
,
1187 struct bt_port
*upstream_port
,
1188 struct bt_config_connection
*cfg_conn
)
1191 GQuark downstreamp_comp_name_quark
;
1192 struct bt_component
*downstream_comp
;
1193 int64_t downstream_port_count
;
1195 int64_t (*port_count_fn
)(struct bt_component
*);
1196 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
1197 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1199 BT_LOGI("Connecting upstream port to the next available downstream port: "
1200 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1201 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1202 upstream_port
, bt_port_get_name(upstream_port
),
1203 cfg_conn
->downstream_comp_name
->str
,
1204 cfg_conn
->arg
->str
);
1205 downstreamp_comp_name_quark
= g_quark_from_string(
1206 cfg_conn
->downstream_comp_name
->str
);
1207 assert(downstreamp_comp_name_quark
> 0);
1208 downstream_comp
= g_hash_table_lookup(ctx
->components
,
1209 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1210 if (!downstream_comp
) {
1211 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1212 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1213 cfg_conn
->arg
->str
);
1214 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1215 cfg_conn
->arg
->str
);
1219 if (bt_component_is_filter(downstream_comp
)) {
1220 port_count_fn
= bt_component_filter_get_input_port_count
;
1221 port_by_index_fn
= bt_component_filter_get_input_port_by_index
;
1222 } else if (bt_component_is_sink(downstream_comp
)) {
1223 port_count_fn
= bt_component_sink_get_input_port_count
;
1224 port_by_index_fn
= bt_component_sink_get_input_port_by_index
;
1227 * Should never happen because the connections are
1228 * validated before we get here.
1230 BT_LOGF("Invalid connection: downstream component is a source: "
1231 "conn-arg=\"%s\"", cfg_conn
->arg
->str
);
1235 downstream_port_count
= port_count_fn(downstream_comp
);
1236 assert(downstream_port_count
>= 0);
1238 for (i
= 0; i
< downstream_port_count
; i
++) {
1239 struct bt_port
*downstream_port
=
1240 port_by_index_fn(downstream_comp
, i
);
1241 const char *downstream_port_name
;
1243 assert(downstream_port
);
1245 /* Skip port if it's already connected */
1246 if (bt_port_is_connected(downstream_port
)) {
1247 bt_put(downstream_port
);
1248 BT_LOGD("Skipping downstream port: already connected: "
1249 "port-addr=%p, port-name=\"%s\"",
1251 bt_port_get_name(downstream_port
));
1255 downstream_port_name
= bt_port_get_name(downstream_port
);
1256 assert(downstream_port_name
);
1258 if (bt_common_star_glob_match(
1259 cfg_conn
->downstream_port_glob
->str
, -1ULL,
1260 downstream_port_name
, -1ULL)) {
1261 /* We have a winner! */
1262 status
= bt_graph_connect_ports(ctx
->graph
,
1263 upstream_port
, downstream_port
, NULL
);
1264 bt_put(downstream_port
);
1266 case BT_GRAPH_STATUS_OK
:
1268 case BT_GRAPH_STATUS_CANCELED
:
1269 BT_LOGI_STR("Graph was canceled by user.");
1270 status
= BT_GRAPH_STATUS_OK
;
1272 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1273 BT_LOGE("A component refused a connection to one of its ports: "
1274 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1275 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1276 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1277 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1279 upstream_comp
, bt_component_get_name(upstream_comp
),
1280 upstream_port
, bt_port_get_name(upstream_port
),
1281 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1282 downstream_port
, downstream_port_name
,
1283 cfg_conn
->arg
->str
);
1285 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1286 bt_port_get_name(upstream_port
),
1287 downstream_port_name
,
1288 cfg_conn
->arg
->str
);
1291 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1292 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1293 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1294 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1295 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1297 upstream_comp
, bt_component_get_name(upstream_comp
),
1298 upstream_port
, bt_port_get_name(upstream_port
),
1299 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1300 downstream_port
, downstream_port_name
,
1301 cfg_conn
->arg
->str
);
1303 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1304 bt_port_get_name(upstream_port
),
1305 downstream_port_name
,
1306 cfg_conn
->arg
->str
);
1310 BT_LOGI("Connected component ports: "
1311 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1312 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1313 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1314 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1316 upstream_comp
, bt_component_get_name(upstream_comp
),
1317 upstream_port
, bt_port_get_name(upstream_port
),
1318 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1319 downstream_port
, downstream_port_name
,
1320 cfg_conn
->arg
->str
);
1325 bt_put(downstream_port
);
1328 if (status
!= BT_GRAPH_STATUS_OK
) {
1329 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1330 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1331 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1332 upstream_port
, bt_port_get_name(upstream_port
),
1333 cfg_conn
->downstream_comp_name
->str
,
1334 cfg_conn
->arg
->str
);
1336 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1337 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1351 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1352 struct bt_port
*upstream_port
)
1355 const char *upstream_port_name
;
1356 const char *upstream_comp_name
;
1357 struct bt_component
*upstream_comp
= NULL
;
1361 assert(upstream_port
);
1362 upstream_port_name
= bt_port_get_name(upstream_port
);
1363 assert(upstream_port_name
);
1364 upstream_comp
= bt_port_get_component(upstream_port
);
1365 if (!upstream_comp
) {
1366 BT_LOGW("Upstream port to connect is not part of a component: "
1367 "port-addr=%p, port-name=\"%s\"",
1368 upstream_port
, upstream_port_name
);
1373 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1374 assert(upstream_comp_name
);
1375 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1376 "port-addr=%p, port-name=\"%s\"",
1377 upstream_comp
, upstream_comp_name
,
1378 upstream_port
, upstream_port_name
);
1380 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1381 struct bt_config_connection
*cfg_conn
=
1383 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1385 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1386 upstream_comp_name
) == 0) {
1387 if (bt_common_star_glob_match(
1388 cfg_conn
->upstream_port_glob
->str
,
1389 -1ULL, upstream_port_name
, -1ULL)) {
1390 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1391 ctx
, upstream_comp
, upstream_port
,
1394 BT_LOGE("Cannot connect upstream port: "
1395 "port-addr=%p, port-name=\"%s\"",
1397 upstream_port_name
);
1399 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1402 cfg_conn
->arg
->str
);
1411 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1412 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1413 upstream_port_name
);
1415 "Cannot create connection: upstream port `%s` does not match any connection\n",
1416 upstream_port_name
);
1422 bt_put(upstream_comp
);
1427 void graph_port_added_listener(struct bt_port
*port
, void *data
)
1429 struct bt_component
*comp
= NULL
;
1430 struct cmd_run_ctx
*ctx
= data
;
1432 comp
= bt_port_get_component(port
);
1433 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1434 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1435 comp
, comp
? bt_component_get_name(comp
) : "",
1436 port
, bt_port_get_name(port
));
1438 if (!ctx
->connect_ports
) {
1443 BT_LOGW_STR("Port has no component.");
1447 if (bt_port_is_connected(port
)) {
1448 BT_LOGW_STR("Port is already connected.");
1452 if (!bt_port_is_output(port
)) {
1453 BT_LOGI_STR("Skipping input port.");
1457 if (cmd_run_ctx_connect_upstream_port(ctx
, port
)) {
1458 BT_LOGF_STR("Cannot connect upstream port.");
1459 fprintf(stderr
, "Added port could not be connected: aborting\n");
1469 void graph_port_removed_listener(struct bt_component
*component
,
1470 struct bt_port
*port
, void *data
)
1472 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1473 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1474 component
, bt_component_get_name(component
),
1475 port
, bt_port_get_name(port
));
1479 void graph_ports_connected_listener(struct bt_port
*upstream_port
,
1480 struct bt_port
*downstream_port
, void *data
)
1482 struct bt_component
*upstream_comp
= bt_port_get_component(upstream_port
);
1483 struct bt_component
*downstream_comp
= bt_port_get_component(downstream_port
);
1485 assert(upstream_comp
);
1486 assert(downstream_comp
);
1487 BT_LOGI("Graph's component ports connected: "
1488 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1489 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1490 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1491 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1492 upstream_comp
, bt_component_get_name(upstream_comp
),
1493 upstream_port
, bt_port_get_name(upstream_port
),
1494 downstream_comp
, bt_component_get_name(downstream_comp
),
1495 downstream_port
, bt_port_get_name(downstream_port
));
1496 bt_put(upstream_comp
);
1497 bt_put(downstream_comp
);
1501 void graph_ports_disconnected_listener(
1502 struct bt_component
*upstream_component
,
1503 struct bt_component
*downstream_component
,
1504 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
1507 BT_LOGI("Graph's component ports disconnected: "
1508 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1509 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1510 upstream_port
, bt_port_get_name(upstream_port
),
1511 downstream_port
, bt_port_get_name(downstream_port
));
1515 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1521 if (ctx
->components
) {
1522 g_hash_table_destroy(ctx
->components
);
1523 ctx
->components
= NULL
;
1532 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1537 ctx
->connect_ports
= false;
1538 ctx
->components
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
1540 if (!ctx
->components
) {
1544 ctx
->graph
= bt_graph_create();
1549 the_graph
= ctx
->graph
;
1550 ret
= bt_graph_add_port_added_listener(ctx
->graph
,
1551 graph_port_added_listener
, ctx
);
1553 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
1557 ret
= bt_graph_add_port_removed_listener(ctx
->graph
,
1558 graph_port_removed_listener
, ctx
);
1560 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
1564 ret
= bt_graph_add_ports_connected_listener(ctx
->graph
,
1565 graph_ports_connected_listener
, ctx
);
1567 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
1571 ret
= bt_graph_add_ports_disconnected_listener(ctx
->graph
,
1572 graph_ports_disconnected_listener
, ctx
);
1574 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
1581 cmd_run_ctx_destroy(ctx
);
1589 int cmd_run_ctx_create_components_from_config_components(
1590 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
1593 struct bt_component_class
*comp_cls
= NULL
;
1594 struct bt_component
*comp
= NULL
;
1597 for (i
= 0; i
< cfg_components
->len
; i
++) {
1598 struct bt_config_component
*cfg_comp
=
1599 g_ptr_array_index(cfg_components
, i
);
1602 comp_cls
= find_component_class(cfg_comp
->plugin_name
->str
,
1603 cfg_comp
->comp_cls_name
->str
, cfg_comp
->type
);
1605 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1606 "comp-cls-name=\"%s\", comp-cls-type=%d",
1607 cfg_comp
->plugin_name
->str
,
1608 cfg_comp
->comp_cls_name
->str
,
1610 fprintf(stderr
, "%s%sCannot find component class %s",
1611 bt_common_color_bold(),
1612 bt_common_color_fg_red(),
1613 bt_common_color_reset());
1614 print_plugin_comp_cls_opt(stderr
,
1615 cfg_comp
->plugin_name
->str
,
1616 cfg_comp
->comp_cls_name
->str
,
1618 fprintf(stderr
, "\n");
1622 ret
= bt_graph_add_component(ctx
->graph
, comp_cls
,
1623 cfg_comp
->instance_name
->str
, cfg_comp
->params
, &comp
);
1625 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
1626 "comp-cls-name=\"%s\", comp-cls-type=%d, "
1628 cfg_comp
->plugin_name
->str
,
1629 cfg_comp
->comp_cls_name
->str
,
1630 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
1631 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
1632 bt_common_color_bold(),
1633 bt_common_color_fg_red(),
1634 cfg_comp
->instance_name
->str
,
1635 bt_common_color_reset());
1639 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
1640 comp
, cfg_comp
->instance_name
->str
);
1641 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
1643 g_hash_table_insert(ctx
->components
,
1644 GUINT_TO_POINTER(quark
), comp
);
1661 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
1666 * Make sure that, during this phase, our graph's "port added"
1667 * listener does not connect ports while we are creating the
1668 * components because we have a special, initial phase for
1671 ctx
->connect_ports
= false;
1673 ret
= cmd_run_ctx_create_components_from_config_components(
1674 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
1680 ret
= cmd_run_ctx_create_components_from_config_components(
1681 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
1687 ret
= cmd_run_ctx_create_components_from_config_components(
1688 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
1699 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
1700 struct bt_component
*comp
,
1701 int64_t (*port_count_fn
)(struct bt_component
*),
1702 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t))
1708 count
= port_count_fn(comp
);
1711 for (i
= 0; i
< count
; i
++) {
1712 struct bt_port
*upstream_port
= port_by_index_fn(comp
, i
);
1714 assert(upstream_port
);
1715 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
1716 bt_put(upstream_port
);
1727 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
1730 GHashTableIter iter
;
1731 gpointer g_name_quark
, g_comp
;
1733 ctx
->connect_ports
= true;
1734 g_hash_table_iter_init(&iter
, ctx
->components
);
1736 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
1737 if (bt_component_is_source(g_comp
)) {
1738 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
1739 g_comp
, bt_component_source_get_output_port_count
,
1740 bt_component_source_get_output_port_by_index
);
1741 } else if (bt_component_is_filter(g_comp
)) {
1742 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
1743 g_comp
, bt_component_filter_get_output_port_count
,
1744 bt_component_filter_get_output_port_by_index
);
1757 const char *bt_graph_status_str(enum bt_graph_status status
)
1760 case BT_GRAPH_STATUS_CANCELED
:
1761 return "BT_GRAPH_STATUS_CANCELED";
1762 case BT_GRAPH_STATUS_AGAIN
:
1763 return "BT_GRAPH_STATUS_AGAIN";
1764 case BT_GRAPH_STATUS_END
:
1765 return "BT_GRAPH_STATUS_END";
1766 case BT_GRAPH_STATUS_OK
:
1767 return "BT_GRAPH_STATUS_OK";
1768 case BT_GRAPH_STATUS_INVALID
:
1769 return "BT_GRAPH_STATUS_INVALID";
1770 case BT_GRAPH_STATUS_NO_SINK
:
1771 return "BT_GRAPH_STATUS_NO_SINK";
1772 case BT_GRAPH_STATUS_ERROR
:
1773 return "BT_GRAPH_STATUS_ERROR";
1780 int cmd_run(struct bt_config
*cfg
)
1783 struct cmd_run_ctx ctx
= { 0 };
1785 /* Initialize the command's context and the graph object */
1786 if (cmd_run_ctx_init(&ctx
, cfg
)) {
1787 BT_LOGE_STR("Cannot initialize the command's context.");
1788 fprintf(stderr
, "Cannot initialize the command's context\n");
1793 BT_LOGI_STR("Canceled by user before creating components.");
1797 BT_LOGI_STR("Creating components.");
1799 /* Create the requested component instances */
1800 if (cmd_run_ctx_create_components(&ctx
)) {
1801 BT_LOGE_STR("Cannot create components.");
1802 fprintf(stderr
, "Cannot create components\n");
1807 BT_LOGI_STR("Canceled by user before connecting components.");
1811 BT_LOGI_STR("Connecting components.");
1813 /* Connect the initially visible component ports */
1814 if (cmd_run_ctx_connect_ports(&ctx
)) {
1815 BT_LOGE_STR("Cannot connect initial component ports.");
1816 fprintf(stderr
, "Cannot connect initial component ports\n");
1821 BT_LOGI_STR("Canceled by user before running the graph.");
1825 BT_LOGI_STR("Running the graph.");
1829 enum bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
1832 * Reset console in case something messed with console
1833 * codes during the graph's execution.
1835 printf("%s", bt_common_color_reset());
1837 fprintf(stderr
, "%s", bt_common_color_reset());
1838 BT_LOGV("bt_graph_run() returned: status=%s",
1839 bt_graph_status_str(graph_status
));
1841 switch (graph_status
) {
1842 case BT_GRAPH_STATUS_OK
:
1844 case BT_GRAPH_STATUS_CANCELED
:
1845 BT_LOGI_STR("Graph was canceled by user.");
1847 case BT_GRAPH_STATUS_AGAIN
:
1848 if (bt_graph_is_canceled(ctx
.graph
)) {
1849 BT_LOGI_STR("Graph was canceled by user.");
1853 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
1854 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
1856 cfg
->cmd_data
.run
.retry_duration_us
);
1858 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
1859 if (bt_graph_is_canceled(ctx
.graph
)) {
1860 BT_LOGI_STR("Graph was canceled by user.");
1866 case BT_COMPONENT_STATUS_END
:
1869 BT_LOGE_STR("Graph failed to complete successfully");
1870 fprintf(stderr
, "Graph failed to complete successfully\n");
1883 cmd_run_ctx_destroy(&ctx
);
1888 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
1890 const char *env_clash
;
1892 if (!cfg
->command_name
) {
1896 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
1897 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
1901 if (g_file_test(cfg
->command_name
,
1902 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
1903 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
1905 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
1907 fprintf(stderr
, "\n");
1908 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
1914 void init_log_level(void)
1916 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
1920 void set_auto_log_levels(struct bt_config
*cfg
)
1922 const char **env_var_name
;
1925 * Override the configuration's default log level if
1926 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
1927 * are found for backward compatibility with legacy Babetrace 1.
1929 if (getenv("BABELTRACE_DEBUG") &&
1930 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
1931 cfg
->log_level
= 'V';
1932 } else if (getenv("BABELTRACE_VERBOSE") &&
1933 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
1934 cfg
->log_level
= 'I';
1938 * Set log levels according to --debug or --verbose. For
1939 * backward compatibility, --debug is more verbose than
1942 * --verbose: INFO log level
1943 * --debug: VERBOSE log level (includes DEBUG, which is
1944 * is less verbose than VERBOSE in the internal
1945 * logging framework)
1947 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
1949 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
1950 } else if (cfg
->debug
) {
1951 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
1954 * Set library's default log level if not
1955 * explicitly specified.
1957 switch (cfg
->log_level
) {
1959 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE
);
1962 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
1965 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
1968 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
1971 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN
);
1974 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR
);
1977 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL
);
1985 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
1987 bt_cli_log_level
= BT_LOG_INFO
;
1988 } else if (cfg
->debug
) {
1989 bt_cli_log_level
= BT_LOG_VERBOSE
;
1992 * Set CLI's default log level if not explicitly
1995 switch (cfg
->log_level
) {
1997 bt_cli_log_level
= BT_LOG_NONE
;
2000 bt_cli_log_level
= BT_LOG_VERBOSE
;
2003 bt_cli_log_level
= BT_LOG_DEBUG
;
2006 bt_cli_log_level
= BT_LOG_INFO
;
2009 bt_cli_log_level
= BT_LOG_WARN
;
2012 bt_cli_log_level
= BT_LOG_ERROR
;
2015 bt_cli_log_level
= BT_LOG_FATAL
;
2023 env_var_name
= log_level_env_var_names
;
2025 while (*env_var_name
) {
2026 if (!getenv(*env_var_name
)) {
2028 setenv(*env_var_name
, "I", 1);
2029 } else if (cfg
->debug
) {
2030 setenv(*env_var_name
, "V", 1);
2032 char val
[2] = { 0 };
2035 * Set module's default log level if not
2036 * explicitly specified.
2038 val
[0] = cfg
->log_level
;
2039 setenv(*env_var_name
, val
, 1);
2048 void set_sigint_handler(void)
2050 struct sigaction new_action
, old_action
;
2052 new_action
.sa_handler
= sigint_handler
;
2053 sigemptyset(&new_action
.sa_mask
);
2054 new_action
.sa_flags
= 0;
2055 sigaction(SIGINT
, NULL
, &old_action
);
2057 if (old_action
.sa_handler
!= SIG_IGN
) {
2058 sigaction(SIGINT
, &new_action
, NULL
);
2062 int main(int argc
, const char **argv
)
2066 struct bt_config
*cfg
;
2069 set_sigint_handler();
2071 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2074 /* Quit without errors; typically usage/version */
2076 BT_LOGI_STR("Quitting without errors.");
2081 BT_LOGE("Command-line error: retcode=%d", retcode
);
2086 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2087 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2092 set_auto_log_levels(cfg
);
2095 if (cfg
->command_needs_plugins
) {
2096 ret
= load_all_plugins(cfg
->plugin_paths
);
2098 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2104 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2105 cfg
->command
, cfg
->command_name
);
2107 switch (cfg
->command
) {
2108 case BT_CONFIG_COMMAND_RUN
:
2111 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2112 ret
= cmd_list_plugins(cfg
);
2114 case BT_CONFIG_COMMAND_HELP
:
2115 ret
= cmd_help(cfg
);
2117 case BT_CONFIG_COMMAND_QUERY
:
2118 ret
= cmd_query(cfg
);
2120 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2121 ret
= cmd_print_ctf_metadata(cfg
);
2123 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2124 ret
= cmd_print_lttng_live_sessions(cfg
);
2127 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2131 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2132 cfg
->command
, cfg
->command_name
, ret
);
2133 warn_command_name_and_directory_clash(cfg
);
2134 retcode
= ret
? 1 : 0;