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"
63 * Known environment variable names for the log levels of the project's
66 static const char* log_level_env_var_names
[] = {
67 "BABELTRACE_PLUGIN_CTF_BTR_LOG_LEVEL",
68 "BABELTRACE_PLUGIN_CTF_FS_SRC_LOG_LEVEL",
69 "BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_SRC_LOG_LEVEL",
70 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
71 "BABELTRACE_PLUGIN_CTF_NOTIF_ITER_LOG_LEVEL",
72 "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
76 /* Application's processing graph (weak) */
77 static struct bt_graph
*the_graph
;
78 static bool canceled
= false;
80 GPtrArray
*loaded_plugins
;
83 void sigint_handler(int signum
)
85 if (signum
!= SIGINT
) {
90 bt_graph_cancel(the_graph
);
97 void init_static_data(void)
99 loaded_plugins
= g_ptr_array_new_with_free_func(bt_put
);
103 void fini_static_data(void)
105 g_ptr_array_free(loaded_plugins
, TRUE
);
109 struct bt_plugin
*find_plugin(const char *name
)
112 struct bt_plugin
*plugin
= NULL
;
115 BT_LOGD("Finding plugin: name=\"%s\"", name
);
117 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
118 plugin
= g_ptr_array_index(loaded_plugins
, i
);
120 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
127 if (BT_LOG_ON_DEBUG
) {
129 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
131 BT_LOGD("Cannot find plugin.");
135 return bt_get(plugin
);
139 struct bt_component_class
*find_component_class(const char *plugin_name
,
140 const char *comp_class_name
,
141 enum bt_component_class_type comp_class_type
)
143 struct bt_component_class
*comp_class
= NULL
;
144 struct bt_plugin
*plugin
;
146 BT_LOGD("Finding component class: plugin-name=\"%s\", "
147 "comp-cls-name=\"%s\", comp-cls-type=%d",
148 plugin_name
, comp_class_name
, comp_class_type
);
150 plugin
= find_plugin(plugin_name
);
156 comp_class
= bt_plugin_get_component_class_by_name_and_type(plugin
,
157 comp_class_name
, comp_class_type
);
161 if (BT_LOG_ON_DEBUG
) {
163 BT_LOGD("Found component class: comp-cls-addr=%p",
166 BT_LOGD("Cannot find component class.");
174 void print_indent(FILE *fp
, size_t indent
)
178 for (i
= 0; i
< indent
; i
++) {
184 const char *component_type_str(enum bt_component_class_type type
)
187 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
189 case BT_COMPONENT_CLASS_TYPE_SINK
:
191 case BT_COMPONENT_CLASS_TYPE_FILTER
:
193 case BT_COMPONENT_CLASS_TYPE_UNKNOWN
:
200 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
201 const char *comp_cls_name
, enum bt_component_class_type type
)
203 GString
*shell_plugin_name
= NULL
;
204 GString
*shell_comp_cls_name
= NULL
;
206 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
207 if (!shell_plugin_name
) {
211 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
212 if (!shell_comp_cls_name
) {
216 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
217 bt_common_color_bold(),
218 bt_common_color_fg_cyan(),
219 component_type_str(type
),
220 bt_common_color_fg_default(),
221 bt_common_color_fg_blue(),
222 shell_plugin_name
->str
,
223 bt_common_color_fg_default(),
224 bt_common_color_fg_yellow(),
225 shell_comp_cls_name
->str
,
226 bt_common_color_reset());
229 if (shell_plugin_name
) {
230 g_string_free(shell_plugin_name
, TRUE
);
233 if (shell_comp_cls_name
) {
234 g_string_free(shell_comp_cls_name
, TRUE
);
239 void print_value(FILE *, struct bt_value
*, size_t);
242 void print_value_rec(FILE *, struct bt_value
*, size_t);
244 struct print_map_value_data
{
250 bt_bool
print_map_value(const char *key
, struct bt_value
*object
, void *data
)
252 struct print_map_value_data
*print_map_value_data
= data
;
254 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
255 fprintf(print_map_value_data
->fp
, "%s: ", key
);
257 if (bt_value_is_array(object
) &&
258 bt_value_array_is_empty(object
)) {
259 fprintf(print_map_value_data
->fp
, "[ ]\n");
263 if (bt_value_is_map(object
) &&
264 bt_value_map_is_empty(object
)) {
265 fprintf(print_map_value_data
->fp
, "{ }\n");
269 if (bt_value_is_array(object
) ||
270 bt_value_is_map(object
)) {
271 fprintf(print_map_value_data
->fp
, "\n");
274 print_value_rec(print_map_value_data
->fp
, object
,
275 print_map_value_data
->indent
+ 2);
280 void print_value_rec(FILE *fp
, struct bt_value
*value
, size_t indent
)
293 switch (bt_value_get_type(value
)) {
294 case BT_VALUE_TYPE_NULL
:
295 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
296 bt_common_color_reset());
298 case BT_VALUE_TYPE_BOOL
:
299 bt_value_bool_get(value
, &bool_val
);
300 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
301 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
302 bt_common_color_reset());
304 case BT_VALUE_TYPE_INTEGER
:
305 bt_value_integer_get(value
, &int_val
);
306 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
307 bt_common_color_fg_red(), int_val
,
308 bt_common_color_reset());
310 case BT_VALUE_TYPE_FLOAT
:
311 bt_value_float_get(value
, &dbl_val
);
312 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
313 bt_common_color_fg_red(), dbl_val
,
314 bt_common_color_reset());
316 case BT_VALUE_TYPE_STRING
:
317 bt_value_string_get(value
, &str_val
);
318 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
319 bt_common_color_fg_green(), str_val
,
320 bt_common_color_reset());
322 case BT_VALUE_TYPE_ARRAY
:
323 size
= bt_value_array_size(value
);
327 print_indent(fp
, indent
);
328 fprintf(fp
, "[ ]\n");
332 for (i
= 0; i
< size
; i
++) {
333 struct bt_value
*element
=
334 bt_value_array_get(value
, i
);
337 print_indent(fp
, indent
);
340 if (bt_value_is_array(element
) &&
341 bt_value_array_is_empty(element
)) {
342 fprintf(fp
, "[ ]\n");
346 if (bt_value_is_map(element
) &&
347 bt_value_map_is_empty(element
)) {
348 fprintf(fp
, "{ }\n");
352 if (bt_value_is_array(element
) ||
353 bt_value_is_map(element
)) {
357 print_value_rec(fp
, element
, indent
+ 2);
361 case BT_VALUE_TYPE_MAP
:
363 struct print_map_value_data data
= {
368 if (bt_value_map_is_empty(value
)) {
369 print_indent(fp
, indent
);
370 fprintf(fp
, "{ }\n");
374 bt_value_map_foreach(value
, print_map_value
, &data
);
383 void print_value(FILE *fp
, struct bt_value
*value
, size_t indent
)
385 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
386 print_indent(fp
, indent
);
389 print_value_rec(fp
, value
, indent
);
393 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
395 fprintf(stderr
, " ");
396 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
397 bt_config_component
->comp_cls_name
->str
,
398 bt_config_component
->type
);
399 fprintf(stderr
, ":\n");
401 if (bt_config_component
->instance_name
->len
> 0) {
402 fprintf(stderr
, " Name: %s\n",
403 bt_config_component
->instance_name
->str
);
406 fprintf(stderr
, " Parameters:\n");
407 print_value(stderr
, bt_config_component
->params
, 8);
411 void print_bt_config_components(GPtrArray
*array
)
415 for (i
= 0; i
< array
->len
; i
++) {
416 struct bt_config_component
*cfg_component
=
417 bt_config_get_component(array
, i
);
418 print_bt_config_component(cfg_component
);
419 BT_PUT(cfg_component
);
424 void print_plugin_paths(struct bt_value
*plugin_paths
)
426 fprintf(stderr
, " Plugin paths:\n");
427 print_value(stderr
, plugin_paths
, 4);
431 void print_cfg_run(struct bt_config
*cfg
)
435 print_plugin_paths(cfg
->plugin_paths
);
436 fprintf(stderr
, " Source component instances:\n");
437 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
439 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
440 fprintf(stderr
, " Filter component instances:\n");
441 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
444 fprintf(stderr
, " Sink component instances:\n");
445 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
446 fprintf(stderr
, " Connections:\n");
448 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
449 struct bt_config_connection
*cfg_connection
=
450 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
453 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
454 cfg_connection
->upstream_comp_name
->str
,
455 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
456 cfg_connection
->upstream_port_glob
->str
,
457 cfg_connection
->downstream_comp_name
->str
,
458 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
459 cfg_connection
->downstream_port_glob
->str
);
464 void print_cfg_list_plugins(struct bt_config
*cfg
)
466 print_plugin_paths(cfg
->plugin_paths
);
470 void print_cfg_help(struct bt_config
*cfg
)
472 print_plugin_paths(cfg
->plugin_paths
);
476 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
478 print_plugin_paths(cfg
->plugin_paths
);
479 fprintf(stderr
, " Path: %s\n",
480 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
484 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
486 print_plugin_paths(cfg
->plugin_paths
);
487 fprintf(stderr
, " URL: %s\n",
488 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
492 void print_cfg_query(struct bt_config
*cfg
)
494 print_plugin_paths(cfg
->plugin_paths
);
495 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
496 fprintf(stderr
, " Component class:\n");
497 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
501 void print_cfg(struct bt_config
*cfg
)
503 if (!BT_LOG_ON_INFO
) {
507 BT_LOGI_STR("Configuration:");
508 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
509 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
511 switch (cfg
->command
) {
512 case BT_CONFIG_COMMAND_RUN
:
515 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
516 print_cfg_list_plugins(cfg
);
518 case BT_CONFIG_COMMAND_HELP
:
521 case BT_CONFIG_COMMAND_QUERY
:
522 print_cfg_query(cfg
);
524 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
525 print_cfg_print_ctf_metadata(cfg
);
527 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
528 print_cfg_print_lttng_live_sessions(cfg
);
536 void add_to_loaded_plugins(struct bt_plugin_set
*plugin_set
)
541 count
= bt_plugin_set_get_plugin_count(plugin_set
);
544 for (i
= 0; i
< count
; i
++) {
545 struct bt_plugin
*plugin
=
546 bt_plugin_set_get_plugin(plugin_set
, i
);
547 struct bt_plugin
*loaded_plugin
=
548 find_plugin(bt_plugin_get_name(plugin
));
553 BT_LOGI("Not using plugin: another one already exists with the same name: "
554 "plugin-name=\"%s\", plugin-path=\"%s\", "
555 "existing-plugin-path=\"%s\"",
556 bt_plugin_get_name(plugin
),
557 bt_plugin_get_path(plugin
),
558 bt_plugin_get_path(loaded_plugin
));
559 bt_put(loaded_plugin
);
561 /* Add to global array. */
562 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
563 bt_plugin_get_name(plugin
));
564 g_ptr_array_add(loaded_plugins
, bt_get(plugin
));
572 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
574 int nr_paths
, i
, ret
= 0;
576 nr_paths
= bt_value_array_size(plugin_paths
);
578 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
583 BT_LOGI("Loading dynamic plugins.");
585 for (i
= 0; i
< nr_paths
; i
++) {
586 struct bt_value
*plugin_path_value
= NULL
;
587 const char *plugin_path
;
588 struct bt_plugin_set
*plugin_set
;
590 plugin_path_value
= bt_value_array_get(plugin_paths
, i
);
591 bt_value_string_get(plugin_path_value
, &plugin_path
);
595 * Skip this if the directory does not exist because
596 * bt_plugin_create_all_from_dir() expects an existing
599 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
600 BT_LOGV("Skipping nonexistent directory path: "
601 "path=\"%s\"", plugin_path
);
602 BT_PUT(plugin_path_value
);
606 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
608 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
610 BT_PUT(plugin_path_value
);
614 add_to_loaded_plugins(plugin_set
);
616 BT_PUT(plugin_path_value
);
623 int load_static_plugins(void)
626 struct bt_plugin_set
*plugin_set
;
628 BT_LOGI("Loading static plugins.");
629 plugin_set
= bt_plugin_create_all_from_static();
631 BT_LOGE("Unable to load static plugins.");
636 add_to_loaded_plugins(plugin_set
);
643 int load_all_plugins(struct bt_value
*plugin_paths
)
647 if (load_dynamic_plugins(plugin_paths
)) {
652 if (load_static_plugins()) {
657 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
664 void print_plugin_info(struct bt_plugin
*plugin
)
666 unsigned int major
, minor
, patch
;
668 enum bt_plugin_status version_status
;
669 const char *plugin_name
;
673 const char *plugin_description
;
675 plugin_name
= bt_plugin_get_name(plugin
);
676 path
= bt_plugin_get_path(plugin
);
677 author
= bt_plugin_get_author(plugin
);
678 license
= bt_plugin_get_license(plugin
);
679 plugin_description
= bt_plugin_get_description(plugin
);
680 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
682 printf("%s%s%s%s:\n", bt_common_color_bold(),
683 bt_common_color_fg_blue(), plugin_name
,
684 bt_common_color_reset());
685 printf(" %sPath%s: %s\n", bt_common_color_bold(),
686 bt_common_color_reset(), path
? path
: "(None)");
688 if (version_status
== BT_PLUGIN_STATUS_OK
) {
689 printf(" %sVersion%s: %u.%u.%u",
690 bt_common_color_bold(), bt_common_color_reset(),
691 major
, minor
, patch
);
700 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
701 bt_common_color_reset(),
702 plugin_description
? plugin_description
: "(None)");
703 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
704 bt_common_color_reset(), author
? author
: "(Unknown)");
705 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
706 bt_common_color_reset(),
707 license
? license
: "(Unknown)");
711 int cmd_query(struct bt_config
*cfg
)
714 struct bt_component_class
*comp_cls
= NULL
;
715 struct bt_value
*results
= NULL
;
717 comp_cls
= find_component_class(cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
718 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
719 cfg
->cmd_data
.query
.cfg_component
->type
);
721 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
722 "comp-cls-name=\"%s\", comp-cls-type=%d",
723 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
724 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
725 cfg
->cmd_data
.query
.cfg_component
->type
);
726 fprintf(stderr
, "%s%sCannot find component class %s",
727 bt_common_color_bold(),
728 bt_common_color_fg_red(),
729 bt_common_color_reset());
730 print_plugin_comp_cls_opt(stderr
,
731 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
732 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
733 cfg
->cmd_data
.query
.cfg_component
->type
);
734 fprintf(stderr
, "\n");
739 results
= bt_component_class_query(comp_cls
,
740 cfg
->cmd_data
.query
.object
->str
,
741 cfg
->cmd_data
.query
.cfg_component
->params
);
743 BT_LOGE("Failed to query component class: plugin-name=\"%s\", "
744 "comp-cls-name=\"%s\", comp-cls-type=%d "
746 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
747 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
748 cfg
->cmd_data
.query
.cfg_component
->type
,
749 cfg
->cmd_data
.query
.object
->str
);
750 fprintf(stderr
, "%s%sFailed to query info to %s",
751 bt_common_color_bold(),
752 bt_common_color_fg_red(),
753 bt_common_color_reset());
754 print_plugin_comp_cls_opt(stderr
,
755 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
756 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
757 cfg
->cmd_data
.query
.cfg_component
->type
);
758 fprintf(stderr
, "%s%s with object `%s`%s\n",
759 bt_common_color_bold(),
760 bt_common_color_fg_red(),
761 cfg
->cmd_data
.query
.object
->str
,
762 bt_common_color_reset());
767 print_value(stdout
, results
, 0);
776 int cmd_help(struct bt_config
*cfg
)
779 struct bt_plugin
*plugin
= NULL
;
782 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
784 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
785 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
786 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
787 bt_common_color_bold(), bt_common_color_fg_red(),
788 bt_common_color_fg_blue(),
789 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
790 bt_common_color_reset());
795 print_plugin_info(plugin
);
796 printf(" %sComponent classes%s: %d\n",
797 bt_common_color_bold(),
798 bt_common_color_reset(),
799 (int) bt_plugin_get_component_class_count(plugin
));
802 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
803 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
804 struct bt_component_class
*needed_comp_cls
=
805 find_component_class(
806 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
807 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
808 cfg
->cmd_data
.help
.cfg_component
->type
);
810 if (!needed_comp_cls
) {
811 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
812 "comp-cls-name=\"%s\", comp-cls-type=%d",
813 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
814 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
815 cfg
->cmd_data
.help
.cfg_component
->type
);
816 fprintf(stderr
, "\n%s%sCannot find component class %s",
817 bt_common_color_bold(),
818 bt_common_color_fg_red(),
819 bt_common_color_reset());
820 print_plugin_comp_cls_opt(stderr
,
821 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
822 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
823 cfg
->cmd_data
.help
.cfg_component
->type
);
824 fprintf(stderr
, "\n");
829 bt_put(needed_comp_cls
);
832 for (i
= 0; i
< bt_plugin_get_component_class_count(plugin
); i
++) {
833 struct bt_component_class
*comp_cls
=
834 bt_plugin_get_component_class_by_index(plugin
, i
);
835 const char *comp_class_name
=
836 bt_component_class_get_name(comp_cls
);
837 const char *comp_class_description
=
838 bt_component_class_get_description(comp_cls
);
839 const char *comp_class_help
=
840 bt_component_class_get_help(comp_cls
);
841 enum bt_component_class_type type
=
842 bt_component_class_get_type(comp_cls
);
846 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
847 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
848 if (strcmp(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
849 comp_class_name
) != 0 &&
851 cfg
->cmd_data
.help
.cfg_component
->type
) {
858 print_plugin_comp_cls_opt(stdout
,
859 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
863 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
864 bt_common_color_reset(),
865 comp_class_description
? comp_class_description
: "(None)");
867 if (comp_class_help
) {
868 printf("\n%s\n", comp_class_help
);
880 int cmd_list_plugins(struct bt_config
*cfg
)
883 int plugins_count
, component_classes_count
= 0, i
;
885 printf("From the following plugin paths:\n\n");
886 print_value(stdout
, cfg
->plugin_paths
, 2);
888 plugins_count
= loaded_plugins
->len
;
889 if (plugins_count
== 0) {
890 printf("No plugins found.\n");
894 for (i
= 0; i
< plugins_count
; i
++) {
895 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
897 component_classes_count
+= bt_plugin_get_component_class_count(plugin
);
900 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
901 bt_common_color_bold(),
902 component_classes_count
,
903 bt_common_color_reset(),
904 bt_common_color_bold(),
906 bt_common_color_reset());
908 for (i
= 0; i
< plugins_count
; i
++) {
910 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
912 component_classes_count
=
913 bt_plugin_get_component_class_count(plugin
);
915 print_plugin_info(plugin
);
917 if (component_classes_count
== 0) {
918 printf(" %sComponent classes%s: (none)\n",
919 bt_common_color_bold(),
920 bt_common_color_reset());
922 printf(" %sComponent classes%s:\n",
923 bt_common_color_bold(),
924 bt_common_color_reset());
927 for (j
= 0; j
< component_classes_count
; j
++) {
928 struct bt_component_class
*comp_class
=
929 bt_plugin_get_component_class_by_index(
931 const char *comp_class_name
=
932 bt_component_class_get_name(comp_class
);
933 const char *comp_class_description
=
934 bt_component_class_get_description(comp_class
);
935 enum bt_component_class_type type
=
936 bt_component_class_get_type(comp_class
);
939 print_plugin_comp_cls_opt(stdout
,
940 bt_plugin_get_name(plugin
), comp_class_name
,
943 if (comp_class_description
) {
944 printf(": %s", comp_class_description
);
957 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
960 struct bt_component_class
*comp_cls
= NULL
;
961 struct bt_value
*results
= NULL
;
962 struct bt_value
*params
= NULL
;
963 struct bt_value
*map
= NULL
;
964 struct bt_value
*v
= NULL
;
965 static const char * const plugin_name
= "ctf";
966 static const char * const comp_cls_name
= "lttng-live";
967 static const enum bt_component_class_type comp_cls_type
=
968 BT_COMPONENT_CLASS_TYPE_SOURCE
;
969 int64_t array_size
, i
;
971 assert(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
972 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
975 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
976 "comp-cls-name=\"%s\", comp-cls-type=%d",
977 plugin_name
, comp_cls_name
,
978 BT_COMPONENT_CLASS_TYPE_SOURCE
);
979 fprintf(stderr
, "%s%sCannot find component class %s",
980 bt_common_color_bold(),
981 bt_common_color_fg_red(),
982 bt_common_color_reset());
983 print_plugin_comp_cls_opt(stderr
, plugin_name
,
984 comp_cls_name
, comp_cls_type
);
985 fprintf(stderr
, "\n");
989 params
= bt_value_map_create();
994 ret
= bt_value_map_insert_string(params
, "url",
995 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1000 results
= bt_component_class_query(comp_cls
, "sessions",
1003 BT_LOGE_STR("Failed to query for sessions.");
1004 fprintf(stderr
, "%s%sFailed to request sessions%s\n",
1005 bt_common_color_bold(),
1006 bt_common_color_fg_red(),
1007 bt_common_color_reset());
1011 if (!bt_value_is_array(results
)) {
1012 BT_LOGE_STR("Expecting an array for sessions query.");
1013 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1014 bt_common_color_bold(),
1015 bt_common_color_fg_red(),
1016 bt_common_color_reset());
1020 array_size
= bt_value_array_size(results
);
1021 for (i
= 0; i
< array_size
; i
++) {
1022 const char *url_text
;
1023 int64_t timer_us
, streams
, clients
;
1025 map
= bt_value_array_get(results
, i
);
1027 BT_LOGE_STR("Unexpected empty array entry.");
1030 if (!bt_value_is_map(map
)) {
1031 BT_LOGE_STR("Unexpected entry type.");
1035 v
= bt_value_map_get(map
, "url");
1037 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1040 ret
= bt_value_string_get(v
, &url_text
);
1042 printf("%s", url_text
);
1045 v
= bt_value_map_get(map
, "timer-us");
1047 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1050 ret
= bt_value_integer_get(v
, &timer_us
);
1052 printf(" (timer = %" PRIu64
", ", timer_us
);
1055 v
= bt_value_map_get(map
, "stream-count");
1057 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1060 ret
= bt_value_integer_get(v
, &streams
);
1062 printf("%" PRIu64
" stream(s), ", streams
);
1065 v
= bt_value_map_get(map
, "client-count");
1067 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1070 ret
= bt_value_integer_get(v
, &clients
);
1072 printf("%" PRIu64
" client(s) connected)\n", clients
);
1091 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1094 struct bt_component_class
*comp_cls
= NULL
;
1095 struct bt_value
*results
= NULL
;
1096 struct bt_value
*params
= NULL
;
1097 struct bt_value
*metadata_text_value
= NULL
;
1098 const char *metadata_text
= NULL
;
1099 static const char * const plugin_name
= "ctf";
1100 static const char * const comp_cls_name
= "fs";
1101 static const enum bt_component_class_type comp_cls_type
=
1102 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1104 assert(cfg
->cmd_data
.print_ctf_metadata
.path
);
1105 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1108 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1109 "comp-cls-name=\"%s\", comp-cls-type=%d",
1110 plugin_name
, comp_cls_name
,
1111 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1112 fprintf(stderr
, "%s%sCannot find component class %s",
1113 bt_common_color_bold(),
1114 bt_common_color_fg_red(),
1115 bt_common_color_reset());
1116 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1117 comp_cls_name
, comp_cls_type
);
1118 fprintf(stderr
, "\n");
1123 params
= bt_value_map_create();
1129 ret
= bt_value_map_insert_string(params
, "path",
1130 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1136 results
= bt_component_class_query(comp_cls
, "metadata-info",
1140 BT_LOGE_STR("Failed to query for metadata info.");
1141 fprintf(stderr
, "%s%sFailed to request metadata info%s\n",
1142 bt_common_color_bold(),
1143 bt_common_color_fg_red(),
1144 bt_common_color_reset());
1148 metadata_text_value
= bt_value_map_get(results
, "text");
1149 if (!metadata_text_value
) {
1150 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1155 ret
= bt_value_string_get(metadata_text_value
, &metadata_text
);
1157 printf("%s\n", metadata_text
);
1162 bt_put(metadata_text_value
);
1167 struct cmd_run_ctx
{
1169 GHashTable
*components
;
1172 struct bt_graph
*graph
;
1175 struct bt_config
*cfg
;
1181 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1182 struct cmd_run_ctx
*ctx
, struct bt_component
*upstream_comp
,
1183 struct bt_port
*upstream_port
,
1184 struct bt_config_connection
*cfg_conn
)
1187 GQuark downstreamp_comp_name_quark
;
1188 struct bt_component
*downstream_comp
;
1189 int64_t downstream_port_count
;
1191 int64_t (*port_count_fn
)(struct bt_component
*);
1192 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
1193 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1195 BT_LOGI("Connecting upstream port to the next available downstream port: "
1196 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1197 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1198 upstream_port
, bt_port_get_name(upstream_port
),
1199 cfg_conn
->downstream_comp_name
->str
,
1200 cfg_conn
->arg
->str
);
1201 downstreamp_comp_name_quark
= g_quark_from_string(
1202 cfg_conn
->downstream_comp_name
->str
);
1203 assert(downstreamp_comp_name_quark
> 0);
1204 downstream_comp
= g_hash_table_lookup(ctx
->components
,
1205 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1206 if (!downstream_comp
) {
1207 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1208 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1209 cfg_conn
->arg
->str
);
1210 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1211 cfg_conn
->arg
->str
);
1215 if (bt_component_is_filter(downstream_comp
)) {
1216 port_count_fn
= bt_component_filter_get_input_port_count
;
1217 port_by_index_fn
= bt_component_filter_get_input_port_by_index
;
1218 } else if (bt_component_is_sink(downstream_comp
)) {
1219 port_count_fn
= bt_component_sink_get_input_port_count
;
1220 port_by_index_fn
= bt_component_sink_get_input_port_by_index
;
1223 * Should never happen because the connections are
1224 * validated before we get here.
1226 BT_LOGF("Invalid connection: downstream component is a source: "
1227 "conn-arg=\"%s\"", cfg_conn
->arg
->str
);
1231 downstream_port_count
= port_count_fn(downstream_comp
);
1232 assert(downstream_port_count
>= 0);
1234 for (i
= 0; i
< downstream_port_count
; i
++) {
1235 struct bt_port
*downstream_port
=
1236 port_by_index_fn(downstream_comp
, i
);
1237 const char *downstream_port_name
;
1239 assert(downstream_port
);
1241 /* Skip port if it's already connected */
1242 if (bt_port_is_connected(downstream_port
)) {
1243 bt_put(downstream_port
);
1244 BT_LOGD("Skipping downstream port: already connected: "
1245 "port-addr=%p, port-name=\"%s\"",
1247 bt_port_get_name(downstream_port
));
1251 downstream_port_name
= bt_port_get_name(downstream_port
);
1252 assert(downstream_port_name
);
1254 if (bt_common_star_glob_match(
1255 cfg_conn
->downstream_port_glob
->str
, -1ULL,
1256 downstream_port_name
, -1ULL)) {
1257 /* We have a winner! */
1258 status
= bt_graph_connect_ports(ctx
->graph
,
1259 upstream_port
, downstream_port
, NULL
);
1260 bt_put(downstream_port
);
1262 case BT_GRAPH_STATUS_OK
:
1264 case BT_GRAPH_STATUS_CANCELED
:
1265 BT_LOGI_STR("Graph was canceled by user.");
1266 status
= BT_GRAPH_STATUS_OK
;
1268 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1269 BT_LOGE("A component refused a connection to one of its ports: "
1270 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1271 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1272 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1273 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1275 upstream_comp
, bt_component_get_name(upstream_comp
),
1276 upstream_port
, bt_port_get_name(upstream_port
),
1277 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1278 downstream_port
, downstream_port_name
,
1279 cfg_conn
->arg
->str
);
1281 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1282 bt_port_get_name(upstream_port
),
1283 downstream_port_name
,
1284 cfg_conn
->arg
->str
);
1287 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1288 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1289 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1290 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1291 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1293 upstream_comp
, bt_component_get_name(upstream_comp
),
1294 upstream_port
, bt_port_get_name(upstream_port
),
1295 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1296 downstream_port
, downstream_port_name
,
1297 cfg_conn
->arg
->str
);
1299 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1300 bt_port_get_name(upstream_port
),
1301 downstream_port_name
,
1302 cfg_conn
->arg
->str
);
1306 BT_LOGI("Connected component ports: "
1307 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1308 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1309 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1310 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1312 upstream_comp
, bt_component_get_name(upstream_comp
),
1313 upstream_port
, bt_port_get_name(upstream_port
),
1314 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1315 downstream_port
, downstream_port_name
,
1316 cfg_conn
->arg
->str
);
1321 bt_put(downstream_port
);
1324 if (status
!= BT_GRAPH_STATUS_OK
) {
1325 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1326 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1327 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1328 upstream_port
, bt_port_get_name(upstream_port
),
1329 cfg_conn
->downstream_comp_name
->str
,
1330 cfg_conn
->arg
->str
);
1332 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1333 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1347 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1348 struct bt_port
*upstream_port
)
1351 const char *upstream_port_name
;
1352 const char *upstream_comp_name
;
1353 struct bt_component
*upstream_comp
= NULL
;
1357 assert(upstream_port
);
1358 upstream_port_name
= bt_port_get_name(upstream_port
);
1359 assert(upstream_port_name
);
1360 upstream_comp
= bt_port_get_component(upstream_port
);
1361 if (!upstream_comp
) {
1362 BT_LOGW("Upstream port to connect is not part of a component: "
1363 "port-addr=%p, port-name=\"%s\"",
1364 upstream_port
, upstream_port_name
);
1369 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1370 assert(upstream_comp_name
);
1371 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1372 "port-addr=%p, port-name=\"%s\"",
1373 upstream_comp
, upstream_comp_name
,
1374 upstream_port
, upstream_port_name
);
1376 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1377 struct bt_config_connection
*cfg_conn
=
1379 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1381 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1382 upstream_comp_name
) == 0) {
1383 if (bt_common_star_glob_match(
1384 cfg_conn
->upstream_port_glob
->str
,
1385 -1ULL, upstream_port_name
, -1ULL)) {
1386 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1387 ctx
, upstream_comp
, upstream_port
,
1390 BT_LOGE("Cannot connect upstream port: "
1391 "port-addr=%p, port-name=\"%s\"",
1393 upstream_port_name
);
1395 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1398 cfg_conn
->arg
->str
);
1407 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1408 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1409 upstream_port_name
);
1411 "Cannot create connection: upstream port `%s` does not match any connection\n",
1412 upstream_port_name
);
1418 bt_put(upstream_comp
);
1423 void graph_port_added_listener(struct bt_port
*port
, void *data
)
1425 struct bt_component
*comp
= NULL
;
1426 struct cmd_run_ctx
*ctx
= data
;
1428 comp
= bt_port_get_component(port
);
1429 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1430 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1431 comp
, comp
? bt_component_get_name(comp
) : "",
1432 port
, bt_port_get_name(port
));
1434 if (!ctx
->connect_ports
) {
1439 BT_LOGW_STR("Port has no component.");
1443 if (bt_port_is_connected(port
)) {
1444 BT_LOGW_STR("Port is already connected.");
1448 if (!bt_port_is_output(port
)) {
1449 BT_LOGI_STR("Skipping input port.");
1453 if (cmd_run_ctx_connect_upstream_port(ctx
, port
)) {
1454 BT_LOGF_STR("Cannot connect upstream port.");
1455 fprintf(stderr
, "Added port could not be connected: aborting\n");
1465 void graph_port_removed_listener(struct bt_component
*component
,
1466 struct bt_port
*port
, void *data
)
1468 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1469 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1470 component
, bt_component_get_name(component
),
1471 port
, bt_port_get_name(port
));
1475 void graph_ports_connected_listener(struct bt_port
*upstream_port
,
1476 struct bt_port
*downstream_port
, void *data
)
1478 struct bt_component
*upstream_comp
= bt_port_get_component(upstream_port
);
1479 struct bt_component
*downstream_comp
= bt_port_get_component(downstream_port
);
1481 assert(upstream_comp
);
1482 assert(downstream_comp
);
1483 BT_LOGI("Graph's component ports connected: "
1484 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1485 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1486 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1487 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1488 upstream_comp
, bt_component_get_name(upstream_comp
),
1489 upstream_port
, bt_port_get_name(upstream_port
),
1490 downstream_comp
, bt_component_get_name(downstream_comp
),
1491 downstream_port
, bt_port_get_name(downstream_port
));
1492 bt_put(upstream_comp
);
1493 bt_put(downstream_comp
);
1497 void graph_ports_disconnected_listener(
1498 struct bt_component
*upstream_component
,
1499 struct bt_component
*downstream_component
,
1500 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
1503 BT_LOGI("Graph's component ports disconnected: "
1504 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1505 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1506 upstream_port
, bt_port_get_name(upstream_port
),
1507 downstream_port
, bt_port_get_name(downstream_port
));
1511 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1517 if (ctx
->components
) {
1518 g_hash_table_destroy(ctx
->components
);
1519 ctx
->components
= NULL
;
1528 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1533 ctx
->connect_ports
= false;
1534 ctx
->components
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
1536 if (!ctx
->components
) {
1540 ctx
->graph
= bt_graph_create();
1545 the_graph
= ctx
->graph
;
1546 ret
= bt_graph_add_port_added_listener(ctx
->graph
,
1547 graph_port_added_listener
, ctx
);
1549 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
1553 ret
= bt_graph_add_port_removed_listener(ctx
->graph
,
1554 graph_port_removed_listener
, ctx
);
1556 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
1560 ret
= bt_graph_add_ports_connected_listener(ctx
->graph
,
1561 graph_ports_connected_listener
, ctx
);
1563 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
1567 ret
= bt_graph_add_ports_disconnected_listener(ctx
->graph
,
1568 graph_ports_disconnected_listener
, ctx
);
1570 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
1577 cmd_run_ctx_destroy(ctx
);
1585 int cmd_run_ctx_create_components_from_config_components(
1586 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
1589 struct bt_component_class
*comp_cls
= NULL
;
1590 struct bt_component
*comp
= NULL
;
1593 for (i
= 0; i
< cfg_components
->len
; i
++) {
1594 struct bt_config_component
*cfg_comp
=
1595 g_ptr_array_index(cfg_components
, i
);
1598 comp_cls
= find_component_class(cfg_comp
->plugin_name
->str
,
1599 cfg_comp
->comp_cls_name
->str
, cfg_comp
->type
);
1601 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1602 "comp-cls-name=\"%s\", comp-cls-type=%d",
1603 cfg_comp
->plugin_name
->str
,
1604 cfg_comp
->comp_cls_name
->str
,
1606 fprintf(stderr
, "%s%sCannot find component class %s",
1607 bt_common_color_bold(),
1608 bt_common_color_fg_red(),
1609 bt_common_color_reset());
1610 print_plugin_comp_cls_opt(stderr
,
1611 cfg_comp
->plugin_name
->str
,
1612 cfg_comp
->comp_cls_name
->str
,
1614 fprintf(stderr
, "\n");
1618 ret
= bt_graph_add_component(ctx
->graph
, comp_cls
,
1619 cfg_comp
->instance_name
->str
, cfg_comp
->params
, &comp
);
1621 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
1622 "comp-cls-name=\"%s\", comp-cls-type=%d, "
1624 cfg_comp
->plugin_name
->str
,
1625 cfg_comp
->comp_cls_name
->str
,
1626 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
1627 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
1628 bt_common_color_bold(),
1629 bt_common_color_fg_red(),
1630 cfg_comp
->instance_name
->str
,
1631 bt_common_color_reset());
1635 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
1636 comp
, cfg_comp
->instance_name
->str
);
1637 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
1639 g_hash_table_insert(ctx
->components
,
1640 GUINT_TO_POINTER(quark
), comp
);
1657 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
1662 * Make sure that, during this phase, our graph's "port added"
1663 * listener does not connect ports while we are creating the
1664 * components because we have a special, initial phase for
1667 ctx
->connect_ports
= false;
1669 ret
= cmd_run_ctx_create_components_from_config_components(
1670 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
1676 ret
= cmd_run_ctx_create_components_from_config_components(
1677 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
1683 ret
= cmd_run_ctx_create_components_from_config_components(
1684 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
1695 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
1696 struct bt_component
*comp
,
1697 int64_t (*port_count_fn
)(struct bt_component
*),
1698 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t))
1704 count
= port_count_fn(comp
);
1707 for (i
= 0; i
< count
; i
++) {
1708 struct bt_port
*upstream_port
= port_by_index_fn(comp
, i
);
1710 assert(upstream_port
);
1711 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
1712 bt_put(upstream_port
);
1723 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
1726 GHashTableIter iter
;
1727 gpointer g_name_quark
, g_comp
;
1729 ctx
->connect_ports
= true;
1730 g_hash_table_iter_init(&iter
, ctx
->components
);
1732 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
1733 if (bt_component_is_source(g_comp
)) {
1734 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
1735 g_comp
, bt_component_source_get_output_port_count
,
1736 bt_component_source_get_output_port_by_index
);
1737 } else if (bt_component_is_filter(g_comp
)) {
1738 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
1739 g_comp
, bt_component_filter_get_output_port_count
,
1740 bt_component_filter_get_output_port_by_index
);
1753 const char *bt_graph_status_str(enum bt_graph_status status
)
1756 case BT_GRAPH_STATUS_CANCELED
:
1757 return "BT_GRAPH_STATUS_CANCELED";
1758 case BT_GRAPH_STATUS_AGAIN
:
1759 return "BT_GRAPH_STATUS_AGAIN";
1760 case BT_GRAPH_STATUS_END
:
1761 return "BT_GRAPH_STATUS_END";
1762 case BT_GRAPH_STATUS_OK
:
1763 return "BT_GRAPH_STATUS_OK";
1764 case BT_GRAPH_STATUS_INVALID
:
1765 return "BT_GRAPH_STATUS_INVALID";
1766 case BT_GRAPH_STATUS_NO_SINK
:
1767 return "BT_GRAPH_STATUS_NO_SINK";
1768 case BT_GRAPH_STATUS_ERROR
:
1769 return "BT_GRAPH_STATUS_ERROR";
1776 int cmd_run(struct bt_config
*cfg
)
1779 struct cmd_run_ctx ctx
= { 0 };
1781 /* Initialize the command's context and the graph object */
1782 if (cmd_run_ctx_init(&ctx
, cfg
)) {
1783 BT_LOGE_STR("Cannot initialize the command's context.");
1784 fprintf(stderr
, "Cannot initialize the command's context\n");
1789 BT_LOGI_STR("Canceled by user before creating components.");
1793 BT_LOGI_STR("Creating components.");
1795 /* Create the requested component instances */
1796 if (cmd_run_ctx_create_components(&ctx
)) {
1797 BT_LOGE_STR("Cannot create components.");
1798 fprintf(stderr
, "Cannot create components\n");
1803 BT_LOGI_STR("Canceled by user before connecting components.");
1807 BT_LOGI_STR("Connecting components.");
1809 /* Connect the initially visible component ports */
1810 if (cmd_run_ctx_connect_ports(&ctx
)) {
1811 BT_LOGE_STR("Cannot connect initial component ports.");
1812 fprintf(stderr
, "Cannot connect initial component ports\n");
1817 BT_LOGI_STR("Canceled by user before running the graph.");
1821 BT_LOGI_STR("Running the graph.");
1825 enum bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
1828 * Reset console in case something messed with console
1829 * codes during the graph's execution.
1831 printf("%s", bt_common_color_reset());
1833 fprintf(stderr
, "%s", bt_common_color_reset());
1834 BT_LOGV("bt_graph_run() returned: status=%s",
1835 bt_graph_status_str(graph_status
));
1837 switch (graph_status
) {
1838 case BT_GRAPH_STATUS_OK
:
1840 case BT_GRAPH_STATUS_CANCELED
:
1841 BT_LOGI_STR("Graph was canceled by user.");
1843 case BT_GRAPH_STATUS_AGAIN
:
1844 if (bt_graph_is_canceled(ctx
.graph
)) {
1845 BT_LOGI_STR("Graph was canceled by user.");
1849 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
1850 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
1852 cfg
->cmd_data
.run
.retry_duration_us
);
1854 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
1855 if (bt_graph_is_canceled(ctx
.graph
)) {
1856 BT_LOGI_STR("Graph was canceled by user.");
1862 case BT_COMPONENT_STATUS_END
:
1865 BT_LOGE_STR("Graph failed to complete successfully");
1866 fprintf(stderr
, "Graph failed to complete successfully\n");
1879 cmd_run_ctx_destroy(&ctx
);
1884 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
1886 const char *env_clash
;
1888 if (!cfg
->command_name
) {
1892 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
1893 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
1897 if (g_file_test(cfg
->command_name
,
1898 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
1899 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
1901 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
1903 fprintf(stderr
, "\n");
1904 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
1910 void init_log_level(void)
1912 bt_cli_log_level
= bt_log_get_level_from_env("BABELTRACE_CLI_LOG_LEVEL");
1915 void set_sigint_handler(void)
1917 struct sigaction new_action
, old_action
;
1919 new_action
.sa_handler
= sigint_handler
;
1920 sigemptyset(&new_action
.sa_mask
);
1921 new_action
.sa_flags
= 0;
1922 sigaction(SIGINT
, NULL
, &old_action
);
1924 if (old_action
.sa_handler
!= SIG_IGN
) {
1925 sigaction(SIGINT
, &new_action
, NULL
);
1929 int main(int argc
, const char **argv
)
1933 struct bt_config
*cfg
;
1934 const char **env_var_name
;
1937 set_sigint_handler();
1939 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
1942 /* Quit without errors; typically usage/version */
1944 BT_LOGI_STR("Quitting without errors.");
1949 BT_LOGE("Command-line error: retcode=%d", retcode
);
1954 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
1955 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
1961 * Set log levels according to --debug or --verbose. For
1962 * backward compatibility, --debug is more verbose than
1965 * --verbose: INFO log level
1966 * --debug: VERBOSE log level (includes DEBUG, which is
1967 * is less verbose than VERBOSE in the internal
1968 * logging framework)
1971 bt_cli_log_level
= BT_LOGGING_LEVEL_INFO
;
1972 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
1973 } else if (cfg
->debug
) {
1974 bt_cli_log_level
= BT_LOGGING_LEVEL_VERBOSE
;
1975 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
1978 env_var_name
= log_level_env_var_names
;
1980 while (*env_var_name
) {
1982 setenv(*env_var_name
, "I", 1);
1983 } else if (cfg
->debug
) {
1984 setenv(*env_var_name
, "V", 1);
1990 babeltrace_debug
= cfg
->debug
;
1991 babeltrace_verbose
= cfg
->verbose
;
1994 if (cfg
->command_needs_plugins
) {
1995 ret
= load_all_plugins(cfg
->plugin_paths
);
1997 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2003 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2004 cfg
->command
, cfg
->command_name
);
2006 switch (cfg
->command
) {
2007 case BT_CONFIG_COMMAND_RUN
:
2010 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2011 ret
= cmd_list_plugins(cfg
);
2013 case BT_CONFIG_COMMAND_HELP
:
2014 ret
= cmd_help(cfg
);
2016 case BT_CONFIG_COMMAND_QUERY
:
2017 ret
= cmd_query(cfg
);
2019 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2020 ret
= cmd_print_ctf_metadata(cfg
);
2022 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2023 ret
= cmd_print_lttng_live_sessions(cfg
);
2026 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2030 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2031 cfg
->command
, cfg
->command_name
, ret
);
2032 warn_command_name_and_directory_clash(cfg
);
2033 retcode
= ret
? 1 : 0;