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_PLUGIN_CTF_BTR_LOG_LEVEL",
69 "BABELTRACE_PLUGIN_CTF_FS_SRC_LOG_LEVEL",
70 "BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_SRC_LOG_LEVEL",
71 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
72 "BABELTRACE_PLUGIN_CTF_NOTIF_ITER_LOG_LEVEL",
73 "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
77 /* Application's processing graph (weak) */
78 static struct bt_graph
*the_graph
;
79 static bool canceled
= false;
81 GPtrArray
*loaded_plugins
;
84 void sigint_handler(int signum
)
86 if (signum
!= SIGINT
) {
91 bt_graph_cancel(the_graph
);
98 void init_static_data(void)
100 loaded_plugins
= g_ptr_array_new_with_free_func(bt_put
);
104 void fini_static_data(void)
106 g_ptr_array_free(loaded_plugins
, TRUE
);
110 struct bt_plugin
*find_plugin(const char *name
)
113 struct bt_plugin
*plugin
= NULL
;
116 BT_LOGD("Finding plugin: name=\"%s\"", name
);
118 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
119 plugin
= g_ptr_array_index(loaded_plugins
, i
);
121 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
128 if (BT_LOG_ON_DEBUG
) {
130 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
132 BT_LOGD("Cannot find plugin.");
136 return bt_get(plugin
);
140 struct bt_component_class
*find_component_class(const char *plugin_name
,
141 const char *comp_class_name
,
142 enum bt_component_class_type comp_class_type
)
144 struct bt_component_class
*comp_class
= NULL
;
145 struct bt_plugin
*plugin
;
147 BT_LOGD("Finding component class: plugin-name=\"%s\", "
148 "comp-cls-name=\"%s\", comp-cls-type=%d",
149 plugin_name
, comp_class_name
, comp_class_type
);
151 plugin
= find_plugin(plugin_name
);
157 comp_class
= bt_plugin_get_component_class_by_name_and_type(plugin
,
158 comp_class_name
, comp_class_type
);
162 if (BT_LOG_ON_DEBUG
) {
164 BT_LOGD("Found component class: comp-cls-addr=%p",
167 BT_LOGD("Cannot find component class.");
175 void print_indent(FILE *fp
, size_t indent
)
179 for (i
= 0; i
< indent
; i
++) {
185 const char *component_type_str(enum bt_component_class_type type
)
188 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
190 case BT_COMPONENT_CLASS_TYPE_SINK
:
192 case BT_COMPONENT_CLASS_TYPE_FILTER
:
194 case BT_COMPONENT_CLASS_TYPE_UNKNOWN
:
201 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
202 const char *comp_cls_name
, enum bt_component_class_type type
)
204 GString
*shell_plugin_name
= NULL
;
205 GString
*shell_comp_cls_name
= NULL
;
207 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
208 if (!shell_plugin_name
) {
212 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
213 if (!shell_comp_cls_name
) {
217 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
218 bt_common_color_bold(),
219 bt_common_color_fg_cyan(),
220 component_type_str(type
),
221 bt_common_color_fg_default(),
222 bt_common_color_fg_blue(),
223 shell_plugin_name
->str
,
224 bt_common_color_fg_default(),
225 bt_common_color_fg_yellow(),
226 shell_comp_cls_name
->str
,
227 bt_common_color_reset());
230 if (shell_plugin_name
) {
231 g_string_free(shell_plugin_name
, TRUE
);
234 if (shell_comp_cls_name
) {
235 g_string_free(shell_comp_cls_name
, TRUE
);
240 void print_value(FILE *, struct bt_value
*, size_t);
243 void print_value_rec(FILE *, struct bt_value
*, size_t);
245 struct print_map_value_data
{
251 bt_bool
print_map_value(const char *key
, struct bt_value
*object
, void *data
)
253 struct print_map_value_data
*print_map_value_data
= data
;
255 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
256 fprintf(print_map_value_data
->fp
, "%s: ", key
);
258 if (bt_value_is_array(object
) &&
259 bt_value_array_is_empty(object
)) {
260 fprintf(print_map_value_data
->fp
, "[ ]\n");
264 if (bt_value_is_map(object
) &&
265 bt_value_map_is_empty(object
)) {
266 fprintf(print_map_value_data
->fp
, "{ }\n");
270 if (bt_value_is_array(object
) ||
271 bt_value_is_map(object
)) {
272 fprintf(print_map_value_data
->fp
, "\n");
275 print_value_rec(print_map_value_data
->fp
, object
,
276 print_map_value_data
->indent
+ 2);
281 void print_value_rec(FILE *fp
, struct bt_value
*value
, size_t indent
)
294 switch (bt_value_get_type(value
)) {
295 case BT_VALUE_TYPE_NULL
:
296 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
297 bt_common_color_reset());
299 case BT_VALUE_TYPE_BOOL
:
300 bt_value_bool_get(value
, &bool_val
);
301 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
302 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
303 bt_common_color_reset());
305 case BT_VALUE_TYPE_INTEGER
:
306 bt_value_integer_get(value
, &int_val
);
307 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
308 bt_common_color_fg_red(), int_val
,
309 bt_common_color_reset());
311 case BT_VALUE_TYPE_FLOAT
:
312 bt_value_float_get(value
, &dbl_val
);
313 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
314 bt_common_color_fg_red(), dbl_val
,
315 bt_common_color_reset());
317 case BT_VALUE_TYPE_STRING
:
318 bt_value_string_get(value
, &str_val
);
319 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
320 bt_common_color_fg_green(), str_val
,
321 bt_common_color_reset());
323 case BT_VALUE_TYPE_ARRAY
:
324 size
= bt_value_array_size(value
);
328 print_indent(fp
, indent
);
329 fprintf(fp
, "[ ]\n");
333 for (i
= 0; i
< size
; i
++) {
334 struct bt_value
*element
=
335 bt_value_array_get(value
, i
);
338 print_indent(fp
, indent
);
341 if (bt_value_is_array(element
) &&
342 bt_value_array_is_empty(element
)) {
343 fprintf(fp
, "[ ]\n");
347 if (bt_value_is_map(element
) &&
348 bt_value_map_is_empty(element
)) {
349 fprintf(fp
, "{ }\n");
353 if (bt_value_is_array(element
) ||
354 bt_value_is_map(element
)) {
358 print_value_rec(fp
, element
, indent
+ 2);
362 case BT_VALUE_TYPE_MAP
:
364 struct print_map_value_data data
= {
369 if (bt_value_map_is_empty(value
)) {
370 print_indent(fp
, indent
);
371 fprintf(fp
, "{ }\n");
375 bt_value_map_foreach(value
, print_map_value
, &data
);
384 void print_value(FILE *fp
, struct bt_value
*value
, size_t indent
)
386 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
387 print_indent(fp
, indent
);
390 print_value_rec(fp
, value
, indent
);
394 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
396 fprintf(stderr
, " ");
397 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
398 bt_config_component
->comp_cls_name
->str
,
399 bt_config_component
->type
);
400 fprintf(stderr
, ":\n");
402 if (bt_config_component
->instance_name
->len
> 0) {
403 fprintf(stderr
, " Name: %s\n",
404 bt_config_component
->instance_name
->str
);
407 fprintf(stderr
, " Parameters:\n");
408 print_value(stderr
, bt_config_component
->params
, 8);
412 void print_bt_config_components(GPtrArray
*array
)
416 for (i
= 0; i
< array
->len
; i
++) {
417 struct bt_config_component
*cfg_component
=
418 bt_config_get_component(array
, i
);
419 print_bt_config_component(cfg_component
);
420 BT_PUT(cfg_component
);
425 void print_plugin_paths(struct bt_value
*plugin_paths
)
427 fprintf(stderr
, " Plugin paths:\n");
428 print_value(stderr
, plugin_paths
, 4);
432 void print_cfg_run(struct bt_config
*cfg
)
436 print_plugin_paths(cfg
->plugin_paths
);
437 fprintf(stderr
, " Source component instances:\n");
438 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
440 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
441 fprintf(stderr
, " Filter component instances:\n");
442 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
445 fprintf(stderr
, " Sink component instances:\n");
446 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
447 fprintf(stderr
, " Connections:\n");
449 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
450 struct bt_config_connection
*cfg_connection
=
451 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
454 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
455 cfg_connection
->upstream_comp_name
->str
,
456 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
457 cfg_connection
->upstream_port_glob
->str
,
458 cfg_connection
->downstream_comp_name
->str
,
459 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
460 cfg_connection
->downstream_port_glob
->str
);
465 void print_cfg_list_plugins(struct bt_config
*cfg
)
467 print_plugin_paths(cfg
->plugin_paths
);
471 void print_cfg_help(struct bt_config
*cfg
)
473 print_plugin_paths(cfg
->plugin_paths
);
477 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
479 print_plugin_paths(cfg
->plugin_paths
);
480 fprintf(stderr
, " Path: %s\n",
481 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
485 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
487 print_plugin_paths(cfg
->plugin_paths
);
488 fprintf(stderr
, " URL: %s\n",
489 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
493 void print_cfg_query(struct bt_config
*cfg
)
495 print_plugin_paths(cfg
->plugin_paths
);
496 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
497 fprintf(stderr
, " Component class:\n");
498 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
502 void print_cfg(struct bt_config
*cfg
)
504 if (!BT_LOG_ON_INFO
) {
508 BT_LOGI_STR("Configuration:");
509 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
510 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
512 switch (cfg
->command
) {
513 case BT_CONFIG_COMMAND_RUN
:
516 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
517 print_cfg_list_plugins(cfg
);
519 case BT_CONFIG_COMMAND_HELP
:
522 case BT_CONFIG_COMMAND_QUERY
:
523 print_cfg_query(cfg
);
525 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
526 print_cfg_print_ctf_metadata(cfg
);
528 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
529 print_cfg_print_lttng_live_sessions(cfg
);
537 void add_to_loaded_plugins(struct bt_plugin_set
*plugin_set
)
542 count
= bt_plugin_set_get_plugin_count(plugin_set
);
545 for (i
= 0; i
< count
; i
++) {
546 struct bt_plugin
*plugin
=
547 bt_plugin_set_get_plugin(plugin_set
, i
);
548 struct bt_plugin
*loaded_plugin
=
549 find_plugin(bt_plugin_get_name(plugin
));
554 BT_LOGI("Not using plugin: another one already exists with the same name: "
555 "plugin-name=\"%s\", plugin-path=\"%s\", "
556 "existing-plugin-path=\"%s\"",
557 bt_plugin_get_name(plugin
),
558 bt_plugin_get_path(plugin
),
559 bt_plugin_get_path(loaded_plugin
));
560 bt_put(loaded_plugin
);
562 /* Add to global array. */
563 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
564 bt_plugin_get_name(plugin
));
565 g_ptr_array_add(loaded_plugins
, bt_get(plugin
));
573 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
575 int nr_paths
, i
, ret
= 0;
577 nr_paths
= bt_value_array_size(plugin_paths
);
579 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
584 BT_LOGI("Loading dynamic plugins.");
586 for (i
= 0; i
< nr_paths
; i
++) {
587 struct bt_value
*plugin_path_value
= NULL
;
588 const char *plugin_path
;
589 struct bt_plugin_set
*plugin_set
;
591 plugin_path_value
= bt_value_array_get(plugin_paths
, i
);
592 bt_value_string_get(plugin_path_value
, &plugin_path
);
596 * Skip this if the directory does not exist because
597 * bt_plugin_create_all_from_dir() expects an existing
600 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
601 BT_LOGV("Skipping nonexistent directory path: "
602 "path=\"%s\"", plugin_path
);
603 BT_PUT(plugin_path_value
);
607 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
609 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
611 BT_PUT(plugin_path_value
);
615 add_to_loaded_plugins(plugin_set
);
617 BT_PUT(plugin_path_value
);
624 int load_static_plugins(void)
627 struct bt_plugin_set
*plugin_set
;
629 BT_LOGI("Loading static plugins.");
630 plugin_set
= bt_plugin_create_all_from_static();
632 BT_LOGE("Unable to load static plugins.");
637 add_to_loaded_plugins(plugin_set
);
644 int load_all_plugins(struct bt_value
*plugin_paths
)
648 if (load_dynamic_plugins(plugin_paths
)) {
653 if (load_static_plugins()) {
658 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
665 void print_plugin_info(struct bt_plugin
*plugin
)
667 unsigned int major
, minor
, patch
;
669 enum bt_plugin_status version_status
;
670 const char *plugin_name
;
674 const char *plugin_description
;
676 plugin_name
= bt_plugin_get_name(plugin
);
677 path
= bt_plugin_get_path(plugin
);
678 author
= bt_plugin_get_author(plugin
);
679 license
= bt_plugin_get_license(plugin
);
680 plugin_description
= bt_plugin_get_description(plugin
);
681 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
683 printf("%s%s%s%s:\n", bt_common_color_bold(),
684 bt_common_color_fg_blue(), plugin_name
,
685 bt_common_color_reset());
686 printf(" %sPath%s: %s\n", bt_common_color_bold(),
687 bt_common_color_reset(), path
? path
: "(None)");
689 if (version_status
== BT_PLUGIN_STATUS_OK
) {
690 printf(" %sVersion%s: %u.%u.%u",
691 bt_common_color_bold(), bt_common_color_reset(),
692 major
, minor
, patch
);
701 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
702 bt_common_color_reset(),
703 plugin_description
? plugin_description
: "(None)");
704 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
705 bt_common_color_reset(), author
? author
: "(Unknown)");
706 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
707 bt_common_color_reset(),
708 license
? license
: "(Unknown)");
712 int cmd_query(struct bt_config
*cfg
)
715 struct bt_component_class
*comp_cls
= NULL
;
716 struct bt_value
*results
= NULL
;
718 comp_cls
= find_component_class(cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
719 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
720 cfg
->cmd_data
.query
.cfg_component
->type
);
722 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
723 "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 fprintf(stderr
, "%s%sCannot find component class %s",
728 bt_common_color_bold(),
729 bt_common_color_fg_red(),
730 bt_common_color_reset());
731 print_plugin_comp_cls_opt(stderr
,
732 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
733 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
734 cfg
->cmd_data
.query
.cfg_component
->type
);
735 fprintf(stderr
, "\n");
740 results
= bt_component_class_query(comp_cls
,
741 cfg
->cmd_data
.query
.object
->str
,
742 cfg
->cmd_data
.query
.cfg_component
->params
);
744 BT_LOGE("Failed to query component class: plugin-name=\"%s\", "
745 "comp-cls-name=\"%s\", comp-cls-type=%d "
747 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
748 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
749 cfg
->cmd_data
.query
.cfg_component
->type
,
750 cfg
->cmd_data
.query
.object
->str
);
751 fprintf(stderr
, "%s%sFailed to query info to %s",
752 bt_common_color_bold(),
753 bt_common_color_fg_red(),
754 bt_common_color_reset());
755 print_plugin_comp_cls_opt(stderr
,
756 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
757 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
758 cfg
->cmd_data
.query
.cfg_component
->type
);
759 fprintf(stderr
, "%s%s with object `%s`%s\n",
760 bt_common_color_bold(),
761 bt_common_color_fg_red(),
762 cfg
->cmd_data
.query
.object
->str
,
763 bt_common_color_reset());
768 print_value(stdout
, results
, 0);
777 int cmd_help(struct bt_config
*cfg
)
780 struct bt_plugin
*plugin
= NULL
;
783 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
785 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
786 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
787 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
788 bt_common_color_bold(), bt_common_color_fg_red(),
789 bt_common_color_fg_blue(),
790 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
791 bt_common_color_reset());
796 print_plugin_info(plugin
);
797 printf(" %sComponent classes%s: %d\n",
798 bt_common_color_bold(),
799 bt_common_color_reset(),
800 (int) bt_plugin_get_component_class_count(plugin
));
803 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
804 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
805 struct bt_component_class
*needed_comp_cls
=
806 find_component_class(
807 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
808 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
809 cfg
->cmd_data
.help
.cfg_component
->type
);
811 if (!needed_comp_cls
) {
812 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
813 "comp-cls-name=\"%s\", comp-cls-type=%d",
814 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
815 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
816 cfg
->cmd_data
.help
.cfg_component
->type
);
817 fprintf(stderr
, "\n%s%sCannot find component class %s",
818 bt_common_color_bold(),
819 bt_common_color_fg_red(),
820 bt_common_color_reset());
821 print_plugin_comp_cls_opt(stderr
,
822 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
823 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
824 cfg
->cmd_data
.help
.cfg_component
->type
);
825 fprintf(stderr
, "\n");
830 bt_put(needed_comp_cls
);
833 for (i
= 0; i
< bt_plugin_get_component_class_count(plugin
); i
++) {
834 struct bt_component_class
*comp_cls
=
835 bt_plugin_get_component_class_by_index(plugin
, i
);
836 const char *comp_class_name
=
837 bt_component_class_get_name(comp_cls
);
838 const char *comp_class_description
=
839 bt_component_class_get_description(comp_cls
);
840 const char *comp_class_help
=
841 bt_component_class_get_help(comp_cls
);
842 enum bt_component_class_type type
=
843 bt_component_class_get_type(comp_cls
);
847 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
848 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
849 if (strcmp(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
850 comp_class_name
) != 0 &&
852 cfg
->cmd_data
.help
.cfg_component
->type
) {
859 print_plugin_comp_cls_opt(stdout
,
860 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
864 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
865 bt_common_color_reset(),
866 comp_class_description
? comp_class_description
: "(None)");
868 if (comp_class_help
) {
869 printf("\n%s\n", comp_class_help
);
881 int cmd_list_plugins(struct bt_config
*cfg
)
884 int plugins_count
, component_classes_count
= 0, i
;
886 printf("From the following plugin paths:\n\n");
887 print_value(stdout
, cfg
->plugin_paths
, 2);
889 plugins_count
= loaded_plugins
->len
;
890 if (plugins_count
== 0) {
891 printf("No plugins found.\n");
895 for (i
= 0; i
< plugins_count
; i
++) {
896 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
898 component_classes_count
+= bt_plugin_get_component_class_count(plugin
);
901 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
902 bt_common_color_bold(),
903 component_classes_count
,
904 bt_common_color_reset(),
905 bt_common_color_bold(),
907 bt_common_color_reset());
909 for (i
= 0; i
< plugins_count
; i
++) {
911 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
913 component_classes_count
=
914 bt_plugin_get_component_class_count(plugin
);
916 print_plugin_info(plugin
);
918 if (component_classes_count
== 0) {
919 printf(" %sComponent classes%s: (none)\n",
920 bt_common_color_bold(),
921 bt_common_color_reset());
923 printf(" %sComponent classes%s:\n",
924 bt_common_color_bold(),
925 bt_common_color_reset());
928 for (j
= 0; j
< component_classes_count
; j
++) {
929 struct bt_component_class
*comp_class
=
930 bt_plugin_get_component_class_by_index(
932 const char *comp_class_name
=
933 bt_component_class_get_name(comp_class
);
934 const char *comp_class_description
=
935 bt_component_class_get_description(comp_class
);
936 enum bt_component_class_type type
=
937 bt_component_class_get_type(comp_class
);
940 print_plugin_comp_cls_opt(stdout
,
941 bt_plugin_get_name(plugin
), comp_class_name
,
944 if (comp_class_description
) {
945 printf(": %s", comp_class_description
);
958 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
961 struct bt_component_class
*comp_cls
= NULL
;
962 struct bt_value
*results
= NULL
;
963 struct bt_value
*params
= NULL
;
964 struct bt_value
*map
= NULL
;
965 struct bt_value
*v
= NULL
;
966 static const char * const plugin_name
= "ctf";
967 static const char * const comp_cls_name
= "lttng-live";
968 static const enum bt_component_class_type comp_cls_type
=
969 BT_COMPONENT_CLASS_TYPE_SOURCE
;
970 int64_t array_size
, i
;
972 assert(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
973 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
976 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
977 "comp-cls-name=\"%s\", comp-cls-type=%d",
978 plugin_name
, comp_cls_name
,
979 BT_COMPONENT_CLASS_TYPE_SOURCE
);
980 fprintf(stderr
, "%s%sCannot find component class %s",
981 bt_common_color_bold(),
982 bt_common_color_fg_red(),
983 bt_common_color_reset());
984 print_plugin_comp_cls_opt(stderr
, plugin_name
,
985 comp_cls_name
, comp_cls_type
);
986 fprintf(stderr
, "\n");
990 params
= bt_value_map_create();
995 ret
= bt_value_map_insert_string(params
, "url",
996 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1001 results
= bt_component_class_query(comp_cls
, "sessions",
1004 BT_LOGE_STR("Failed to query for sessions.");
1005 fprintf(stderr
, "%s%sFailed to request sessions%s\n",
1006 bt_common_color_bold(),
1007 bt_common_color_fg_red(),
1008 bt_common_color_reset());
1012 if (!bt_value_is_array(results
)) {
1013 BT_LOGE_STR("Expecting an array for sessions query.");
1014 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1015 bt_common_color_bold(),
1016 bt_common_color_fg_red(),
1017 bt_common_color_reset());
1021 array_size
= bt_value_array_size(results
);
1022 for (i
= 0; i
< array_size
; i
++) {
1023 const char *url_text
;
1024 int64_t timer_us
, streams
, clients
;
1026 map
= bt_value_array_get(results
, i
);
1028 BT_LOGE_STR("Unexpected empty array entry.");
1031 if (!bt_value_is_map(map
)) {
1032 BT_LOGE_STR("Unexpected entry type.");
1036 v
= bt_value_map_get(map
, "url");
1038 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1041 ret
= bt_value_string_get(v
, &url_text
);
1043 printf("%s", url_text
);
1046 v
= bt_value_map_get(map
, "timer-us");
1048 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1051 ret
= bt_value_integer_get(v
, &timer_us
);
1053 printf(" (timer = %" PRIu64
", ", timer_us
);
1056 v
= bt_value_map_get(map
, "stream-count");
1058 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1061 ret
= bt_value_integer_get(v
, &streams
);
1063 printf("%" PRIu64
" stream(s), ", streams
);
1066 v
= bt_value_map_get(map
, "client-count");
1068 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1071 ret
= bt_value_integer_get(v
, &clients
);
1073 printf("%" PRIu64
" client(s) connected)\n", clients
);
1092 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1095 struct bt_component_class
*comp_cls
= NULL
;
1096 struct bt_value
*results
= NULL
;
1097 struct bt_value
*params
= NULL
;
1098 struct bt_value
*metadata_text_value
= NULL
;
1099 const char *metadata_text
= NULL
;
1100 static const char * const plugin_name
= "ctf";
1101 static const char * const comp_cls_name
= "fs";
1102 static const enum bt_component_class_type comp_cls_type
=
1103 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1105 assert(cfg
->cmd_data
.print_ctf_metadata
.path
);
1106 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1109 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1110 "comp-cls-name=\"%s\", comp-cls-type=%d",
1111 plugin_name
, comp_cls_name
,
1112 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1113 fprintf(stderr
, "%s%sCannot find component class %s",
1114 bt_common_color_bold(),
1115 bt_common_color_fg_red(),
1116 bt_common_color_reset());
1117 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1118 comp_cls_name
, comp_cls_type
);
1119 fprintf(stderr
, "\n");
1124 params
= bt_value_map_create();
1130 ret
= bt_value_map_insert_string(params
, "path",
1131 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1137 results
= bt_component_class_query(comp_cls
, "metadata-info",
1141 BT_LOGE_STR("Failed to query for metadata info.");
1142 fprintf(stderr
, "%s%sFailed to request metadata info%s\n",
1143 bt_common_color_bold(),
1144 bt_common_color_fg_red(),
1145 bt_common_color_reset());
1149 metadata_text_value
= bt_value_map_get(results
, "text");
1150 if (!metadata_text_value
) {
1151 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1156 ret
= bt_value_string_get(metadata_text_value
, &metadata_text
);
1158 printf("%s\n", metadata_text
);
1163 bt_put(metadata_text_value
);
1168 struct cmd_run_ctx
{
1170 GHashTable
*components
;
1173 struct bt_graph
*graph
;
1176 struct bt_config
*cfg
;
1182 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1183 struct cmd_run_ctx
*ctx
, struct bt_component
*upstream_comp
,
1184 struct bt_port
*upstream_port
,
1185 struct bt_config_connection
*cfg_conn
)
1188 GQuark downstreamp_comp_name_quark
;
1189 struct bt_component
*downstream_comp
;
1190 int64_t downstream_port_count
;
1192 int64_t (*port_count_fn
)(struct bt_component
*);
1193 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
1194 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1196 BT_LOGI("Connecting upstream port to the next available downstream port: "
1197 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1198 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1199 upstream_port
, bt_port_get_name(upstream_port
),
1200 cfg_conn
->downstream_comp_name
->str
,
1201 cfg_conn
->arg
->str
);
1202 downstreamp_comp_name_quark
= g_quark_from_string(
1203 cfg_conn
->downstream_comp_name
->str
);
1204 assert(downstreamp_comp_name_quark
> 0);
1205 downstream_comp
= g_hash_table_lookup(ctx
->components
,
1206 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1207 if (!downstream_comp
) {
1208 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1209 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1210 cfg_conn
->arg
->str
);
1211 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1212 cfg_conn
->arg
->str
);
1216 if (bt_component_is_filter(downstream_comp
)) {
1217 port_count_fn
= bt_component_filter_get_input_port_count
;
1218 port_by_index_fn
= bt_component_filter_get_input_port_by_index
;
1219 } else if (bt_component_is_sink(downstream_comp
)) {
1220 port_count_fn
= bt_component_sink_get_input_port_count
;
1221 port_by_index_fn
= bt_component_sink_get_input_port_by_index
;
1224 * Should never happen because the connections are
1225 * validated before we get here.
1227 BT_LOGF("Invalid connection: downstream component is a source: "
1228 "conn-arg=\"%s\"", cfg_conn
->arg
->str
);
1232 downstream_port_count
= port_count_fn(downstream_comp
);
1233 assert(downstream_port_count
>= 0);
1235 for (i
= 0; i
< downstream_port_count
; i
++) {
1236 struct bt_port
*downstream_port
=
1237 port_by_index_fn(downstream_comp
, i
);
1238 const char *downstream_port_name
;
1240 assert(downstream_port
);
1242 /* Skip port if it's already connected */
1243 if (bt_port_is_connected(downstream_port
)) {
1244 bt_put(downstream_port
);
1245 BT_LOGD("Skipping downstream port: already connected: "
1246 "port-addr=%p, port-name=\"%s\"",
1248 bt_port_get_name(downstream_port
));
1252 downstream_port_name
= bt_port_get_name(downstream_port
);
1253 assert(downstream_port_name
);
1255 if (bt_common_star_glob_match(
1256 cfg_conn
->downstream_port_glob
->str
, -1ULL,
1257 downstream_port_name
, -1ULL)) {
1258 /* We have a winner! */
1259 status
= bt_graph_connect_ports(ctx
->graph
,
1260 upstream_port
, downstream_port
, NULL
);
1261 bt_put(downstream_port
);
1263 case BT_GRAPH_STATUS_OK
:
1265 case BT_GRAPH_STATUS_CANCELED
:
1266 BT_LOGI_STR("Graph was canceled by user.");
1267 status
= BT_GRAPH_STATUS_OK
;
1269 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1270 BT_LOGE("A component refused a connection to one of its ports: "
1271 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1272 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1273 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1274 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1276 upstream_comp
, bt_component_get_name(upstream_comp
),
1277 upstream_port
, bt_port_get_name(upstream_port
),
1278 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1279 downstream_port
, downstream_port_name
,
1280 cfg_conn
->arg
->str
);
1282 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1283 bt_port_get_name(upstream_port
),
1284 downstream_port_name
,
1285 cfg_conn
->arg
->str
);
1288 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1289 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1290 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1291 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1292 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1294 upstream_comp
, bt_component_get_name(upstream_comp
),
1295 upstream_port
, bt_port_get_name(upstream_port
),
1296 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1297 downstream_port
, downstream_port_name
,
1298 cfg_conn
->arg
->str
);
1300 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1301 bt_port_get_name(upstream_port
),
1302 downstream_port_name
,
1303 cfg_conn
->arg
->str
);
1307 BT_LOGI("Connected component ports: "
1308 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1309 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1310 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1311 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1313 upstream_comp
, bt_component_get_name(upstream_comp
),
1314 upstream_port
, bt_port_get_name(upstream_port
),
1315 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1316 downstream_port
, downstream_port_name
,
1317 cfg_conn
->arg
->str
);
1322 bt_put(downstream_port
);
1325 if (status
!= BT_GRAPH_STATUS_OK
) {
1326 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1327 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1328 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1329 upstream_port
, bt_port_get_name(upstream_port
),
1330 cfg_conn
->downstream_comp_name
->str
,
1331 cfg_conn
->arg
->str
);
1333 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1334 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1348 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1349 struct bt_port
*upstream_port
)
1352 const char *upstream_port_name
;
1353 const char *upstream_comp_name
;
1354 struct bt_component
*upstream_comp
= NULL
;
1358 assert(upstream_port
);
1359 upstream_port_name
= bt_port_get_name(upstream_port
);
1360 assert(upstream_port_name
);
1361 upstream_comp
= bt_port_get_component(upstream_port
);
1362 if (!upstream_comp
) {
1363 BT_LOGW("Upstream port to connect is not part of a component: "
1364 "port-addr=%p, port-name=\"%s\"",
1365 upstream_port
, upstream_port_name
);
1370 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1371 assert(upstream_comp_name
);
1372 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1373 "port-addr=%p, port-name=\"%s\"",
1374 upstream_comp
, upstream_comp_name
,
1375 upstream_port
, upstream_port_name
);
1377 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1378 struct bt_config_connection
*cfg_conn
=
1380 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1382 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1383 upstream_comp_name
) == 0) {
1384 if (bt_common_star_glob_match(
1385 cfg_conn
->upstream_port_glob
->str
,
1386 -1ULL, upstream_port_name
, -1ULL)) {
1387 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1388 ctx
, upstream_comp
, upstream_port
,
1391 BT_LOGE("Cannot connect upstream port: "
1392 "port-addr=%p, port-name=\"%s\"",
1394 upstream_port_name
);
1396 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1399 cfg_conn
->arg
->str
);
1408 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1409 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1410 upstream_port_name
);
1412 "Cannot create connection: upstream port `%s` does not match any connection\n",
1413 upstream_port_name
);
1419 bt_put(upstream_comp
);
1424 void graph_port_added_listener(struct bt_port
*port
, void *data
)
1426 struct bt_component
*comp
= NULL
;
1427 struct cmd_run_ctx
*ctx
= data
;
1429 comp
= bt_port_get_component(port
);
1430 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1431 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1432 comp
, comp
? bt_component_get_name(comp
) : "",
1433 port
, bt_port_get_name(port
));
1435 if (!ctx
->connect_ports
) {
1440 BT_LOGW_STR("Port has no component.");
1444 if (bt_port_is_connected(port
)) {
1445 BT_LOGW_STR("Port is already connected.");
1449 if (!bt_port_is_output(port
)) {
1450 BT_LOGI_STR("Skipping input port.");
1454 if (cmd_run_ctx_connect_upstream_port(ctx
, port
)) {
1455 BT_LOGF_STR("Cannot connect upstream port.");
1456 fprintf(stderr
, "Added port could not be connected: aborting\n");
1466 void graph_port_removed_listener(struct bt_component
*component
,
1467 struct bt_port
*port
, void *data
)
1469 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1470 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1471 component
, bt_component_get_name(component
),
1472 port
, bt_port_get_name(port
));
1476 void graph_ports_connected_listener(struct bt_port
*upstream_port
,
1477 struct bt_port
*downstream_port
, void *data
)
1479 struct bt_component
*upstream_comp
= bt_port_get_component(upstream_port
);
1480 struct bt_component
*downstream_comp
= bt_port_get_component(downstream_port
);
1482 assert(upstream_comp
);
1483 assert(downstream_comp
);
1484 BT_LOGI("Graph's component ports connected: "
1485 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1486 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1487 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1488 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1489 upstream_comp
, bt_component_get_name(upstream_comp
),
1490 upstream_port
, bt_port_get_name(upstream_port
),
1491 downstream_comp
, bt_component_get_name(downstream_comp
),
1492 downstream_port
, bt_port_get_name(downstream_port
));
1493 bt_put(upstream_comp
);
1494 bt_put(downstream_comp
);
1498 void graph_ports_disconnected_listener(
1499 struct bt_component
*upstream_component
,
1500 struct bt_component
*downstream_component
,
1501 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
1504 BT_LOGI("Graph's component ports disconnected: "
1505 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1506 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1507 upstream_port
, bt_port_get_name(upstream_port
),
1508 downstream_port
, bt_port_get_name(downstream_port
));
1512 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1518 if (ctx
->components
) {
1519 g_hash_table_destroy(ctx
->components
);
1520 ctx
->components
= NULL
;
1529 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1534 ctx
->connect_ports
= false;
1535 ctx
->components
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
1537 if (!ctx
->components
) {
1541 ctx
->graph
= bt_graph_create();
1546 the_graph
= ctx
->graph
;
1547 ret
= bt_graph_add_port_added_listener(ctx
->graph
,
1548 graph_port_added_listener
, ctx
);
1550 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
1554 ret
= bt_graph_add_port_removed_listener(ctx
->graph
,
1555 graph_port_removed_listener
, ctx
);
1557 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
1561 ret
= bt_graph_add_ports_connected_listener(ctx
->graph
,
1562 graph_ports_connected_listener
, ctx
);
1564 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
1568 ret
= bt_graph_add_ports_disconnected_listener(ctx
->graph
,
1569 graph_ports_disconnected_listener
, ctx
);
1571 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
1578 cmd_run_ctx_destroy(ctx
);
1586 int cmd_run_ctx_create_components_from_config_components(
1587 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
1590 struct bt_component_class
*comp_cls
= NULL
;
1591 struct bt_component
*comp
= NULL
;
1594 for (i
= 0; i
< cfg_components
->len
; i
++) {
1595 struct bt_config_component
*cfg_comp
=
1596 g_ptr_array_index(cfg_components
, i
);
1599 comp_cls
= find_component_class(cfg_comp
->plugin_name
->str
,
1600 cfg_comp
->comp_cls_name
->str
, cfg_comp
->type
);
1602 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1603 "comp-cls-name=\"%s\", comp-cls-type=%d",
1604 cfg_comp
->plugin_name
->str
,
1605 cfg_comp
->comp_cls_name
->str
,
1607 fprintf(stderr
, "%s%sCannot find component class %s",
1608 bt_common_color_bold(),
1609 bt_common_color_fg_red(),
1610 bt_common_color_reset());
1611 print_plugin_comp_cls_opt(stderr
,
1612 cfg_comp
->plugin_name
->str
,
1613 cfg_comp
->comp_cls_name
->str
,
1615 fprintf(stderr
, "\n");
1619 ret
= bt_graph_add_component(ctx
->graph
, comp_cls
,
1620 cfg_comp
->instance_name
->str
, cfg_comp
->params
, &comp
);
1622 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
1623 "comp-cls-name=\"%s\", comp-cls-type=%d, "
1625 cfg_comp
->plugin_name
->str
,
1626 cfg_comp
->comp_cls_name
->str
,
1627 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
1628 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
1629 bt_common_color_bold(),
1630 bt_common_color_fg_red(),
1631 cfg_comp
->instance_name
->str
,
1632 bt_common_color_reset());
1636 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
1637 comp
, cfg_comp
->instance_name
->str
);
1638 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
1640 g_hash_table_insert(ctx
->components
,
1641 GUINT_TO_POINTER(quark
), comp
);
1658 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
1663 * Make sure that, during this phase, our graph's "port added"
1664 * listener does not connect ports while we are creating the
1665 * components because we have a special, initial phase for
1668 ctx
->connect_ports
= false;
1670 ret
= cmd_run_ctx_create_components_from_config_components(
1671 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
1677 ret
= cmd_run_ctx_create_components_from_config_components(
1678 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
1684 ret
= cmd_run_ctx_create_components_from_config_components(
1685 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
1696 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
1697 struct bt_component
*comp
,
1698 int64_t (*port_count_fn
)(struct bt_component
*),
1699 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t))
1705 count
= port_count_fn(comp
);
1708 for (i
= 0; i
< count
; i
++) {
1709 struct bt_port
*upstream_port
= port_by_index_fn(comp
, i
);
1711 assert(upstream_port
);
1712 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
1713 bt_put(upstream_port
);
1724 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
1727 GHashTableIter iter
;
1728 gpointer g_name_quark
, g_comp
;
1730 ctx
->connect_ports
= true;
1731 g_hash_table_iter_init(&iter
, ctx
->components
);
1733 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
1734 if (bt_component_is_source(g_comp
)) {
1735 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
1736 g_comp
, bt_component_source_get_output_port_count
,
1737 bt_component_source_get_output_port_by_index
);
1738 } else if (bt_component_is_filter(g_comp
)) {
1739 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
1740 g_comp
, bt_component_filter_get_output_port_count
,
1741 bt_component_filter_get_output_port_by_index
);
1754 const char *bt_graph_status_str(enum bt_graph_status status
)
1757 case BT_GRAPH_STATUS_CANCELED
:
1758 return "BT_GRAPH_STATUS_CANCELED";
1759 case BT_GRAPH_STATUS_AGAIN
:
1760 return "BT_GRAPH_STATUS_AGAIN";
1761 case BT_GRAPH_STATUS_END
:
1762 return "BT_GRAPH_STATUS_END";
1763 case BT_GRAPH_STATUS_OK
:
1764 return "BT_GRAPH_STATUS_OK";
1765 case BT_GRAPH_STATUS_INVALID
:
1766 return "BT_GRAPH_STATUS_INVALID";
1767 case BT_GRAPH_STATUS_NO_SINK
:
1768 return "BT_GRAPH_STATUS_NO_SINK";
1769 case BT_GRAPH_STATUS_ERROR
:
1770 return "BT_GRAPH_STATUS_ERROR";
1777 int cmd_run(struct bt_config
*cfg
)
1780 struct cmd_run_ctx ctx
= { 0 };
1782 /* Initialize the command's context and the graph object */
1783 if (cmd_run_ctx_init(&ctx
, cfg
)) {
1784 BT_LOGE_STR("Cannot initialize the command's context.");
1785 fprintf(stderr
, "Cannot initialize the command's context\n");
1790 BT_LOGI_STR("Canceled by user before creating components.");
1794 BT_LOGI_STR("Creating components.");
1796 /* Create the requested component instances */
1797 if (cmd_run_ctx_create_components(&ctx
)) {
1798 BT_LOGE_STR("Cannot create components.");
1799 fprintf(stderr
, "Cannot create components\n");
1804 BT_LOGI_STR("Canceled by user before connecting components.");
1808 BT_LOGI_STR("Connecting components.");
1810 /* Connect the initially visible component ports */
1811 if (cmd_run_ctx_connect_ports(&ctx
)) {
1812 BT_LOGE_STR("Cannot connect initial component ports.");
1813 fprintf(stderr
, "Cannot connect initial component ports\n");
1818 BT_LOGI_STR("Canceled by user before running the graph.");
1822 BT_LOGI_STR("Running the graph.");
1826 enum bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
1829 * Reset console in case something messed with console
1830 * codes during the graph's execution.
1832 printf("%s", bt_common_color_reset());
1834 fprintf(stderr
, "%s", bt_common_color_reset());
1835 BT_LOGV("bt_graph_run() returned: status=%s",
1836 bt_graph_status_str(graph_status
));
1838 switch (graph_status
) {
1839 case BT_GRAPH_STATUS_OK
:
1841 case BT_GRAPH_STATUS_CANCELED
:
1842 BT_LOGI_STR("Graph was canceled by user.");
1844 case BT_GRAPH_STATUS_AGAIN
:
1845 if (bt_graph_is_canceled(ctx
.graph
)) {
1846 BT_LOGI_STR("Graph was canceled by user.");
1850 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
1851 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
1853 cfg
->cmd_data
.run
.retry_duration_us
);
1855 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
1856 if (bt_graph_is_canceled(ctx
.graph
)) {
1857 BT_LOGI_STR("Graph was canceled by user.");
1863 case BT_COMPONENT_STATUS_END
:
1866 BT_LOGE_STR("Graph failed to complete successfully");
1867 fprintf(stderr
, "Graph failed to complete successfully\n");
1880 cmd_run_ctx_destroy(&ctx
);
1885 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
1887 const char *env_clash
;
1889 if (!cfg
->command_name
) {
1893 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
1894 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
1898 if (g_file_test(cfg
->command_name
,
1899 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
1900 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
1902 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
1904 fprintf(stderr
, "\n");
1905 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
1911 void init_log_level(void)
1913 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
1917 void set_auto_log_levels(struct bt_config
*cfg
)
1919 const char **env_var_name
;
1922 * Set log levels according to --debug or --verbose. For
1923 * backward compatibility, --debug is more verbose than
1926 * --verbose: INFO log level
1927 * --debug: VERBOSE log level (includes DEBUG, which is
1928 * is less verbose than VERBOSE in the internal
1929 * logging framework)
1931 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
1933 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
1934 } else if (cfg
->debug
) {
1935 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
1938 * Set library's default log level if not
1939 * explicitly specified.
1941 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN
);
1945 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
1947 bt_cli_log_level
= BT_LOG_INFO
;
1948 } else if (cfg
->debug
) {
1949 bt_cli_log_level
= BT_LOG_VERBOSE
;
1952 * Set CLI's default log level if not explicitly
1955 bt_cli_log_level
= BT_LOG_WARN
;
1959 env_var_name
= log_level_env_var_names
;
1961 while (*env_var_name
) {
1962 if (!getenv(*env_var_name
)) {
1964 setenv(*env_var_name
, "I", 1);
1965 } else if (cfg
->debug
) {
1966 setenv(*env_var_name
, "V", 1);
1969 * Set module's default log level if not
1970 * explicitly specified.
1972 setenv(*env_var_name
, "W", 1);
1979 babeltrace_debug
= cfg
->debug
;
1980 babeltrace_verbose
= cfg
->verbose
;
1984 void set_sigint_handler(void)
1986 struct sigaction new_action
, old_action
;
1988 new_action
.sa_handler
= sigint_handler
;
1989 sigemptyset(&new_action
.sa_mask
);
1990 new_action
.sa_flags
= 0;
1991 sigaction(SIGINT
, NULL
, &old_action
);
1993 if (old_action
.sa_handler
!= SIG_IGN
) {
1994 sigaction(SIGINT
, &new_action
, NULL
);
1998 int main(int argc
, const char **argv
)
2002 struct bt_config
*cfg
;
2005 set_sigint_handler();
2007 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2010 /* Quit without errors; typically usage/version */
2012 BT_LOGI_STR("Quitting without errors.");
2017 BT_LOGE("Command-line error: retcode=%d", retcode
);
2022 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2023 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2028 set_auto_log_levels(cfg
);
2031 if (cfg
->command_needs_plugins
) {
2032 ret
= load_all_plugins(cfg
->plugin_paths
);
2034 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2040 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2041 cfg
->command
, cfg
->command_name
);
2043 switch (cfg
->command
) {
2044 case BT_CONFIG_COMMAND_RUN
:
2047 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2048 ret
= cmd_list_plugins(cfg
);
2050 case BT_CONFIG_COMMAND_HELP
:
2051 ret
= cmd_help(cfg
);
2053 case BT_CONFIG_COMMAND_QUERY
:
2054 ret
= cmd_query(cfg
);
2056 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2057 ret
= cmd_print_ctf_metadata(cfg
);
2059 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2060 ret
= cmd_print_lttng_live_sessions(cfg
);
2063 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2067 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2068 cfg
->command
, cfg
->command_name
, ret
);
2069 warn_command_name_and_directory_clash(cfg
);
2070 retcode
= ret
? 1 : 0;