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"
62 #define NSEC_PER_SEC 1000000000LL
65 * Known environment variable names for the log levels of the project's
68 static const char* log_level_env_var_names
[] = {
69 "BABELTRACE_COMMON_LOG_LEVEL",
70 "BABELTRACE_PLUGIN_CTF_BTR_LOG_LEVEL",
71 "BABELTRACE_PLUGIN_CTF_FS_SRC_LOG_LEVEL",
72 "BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_SRC_LOG_LEVEL",
73 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
74 "BABELTRACE_PLUGIN_CTF_NOTIF_ITER_LOG_LEVEL",
75 "BABELTRACE_PLUGIN_LTTNG_UTILS_DEBUG_INFO_FLT_LOG_LEVEL",
76 "BABELTRACE_PLUGIN_TEXT_DMESG_SRC_LOG_LEVEL",
77 "BABELTRACE_PLUGIN_UTILS_MUXER_FLT_LOG_LEVEL",
78 "BABELTRACE_PLUGIN_UTILS_TRIMMER_FLT_LOG_LEVEL",
79 "BABELTRACE_PLUGIN_CTFCOPYTRACE_LIB_LOG_LEVEL",
80 "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
84 /* Application's processing graph (weak) */
85 static struct bt_graph
*the_graph
;
86 static bool canceled
= false;
88 GPtrArray
*loaded_plugins
;
91 void sigint_handler(int signum
)
93 if (signum
!= SIGINT
) {
98 bt_graph_cancel(the_graph
);
105 void init_static_data(void)
107 loaded_plugins
= g_ptr_array_new_with_free_func(bt_put
);
111 void fini_static_data(void)
113 g_ptr_array_free(loaded_plugins
, TRUE
);
117 struct bt_plugin
*find_plugin(const char *name
)
120 struct bt_plugin
*plugin
= NULL
;
123 BT_LOGD("Finding plugin: name=\"%s\"", name
);
125 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
126 plugin
= g_ptr_array_index(loaded_plugins
, i
);
128 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
135 if (BT_LOG_ON_DEBUG
) {
137 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
139 BT_LOGD("Cannot find plugin.");
143 return bt_get(plugin
);
147 struct bt_component_class
*find_component_class(const char *plugin_name
,
148 const char *comp_class_name
,
149 enum bt_component_class_type comp_class_type
)
151 struct bt_component_class
*comp_class
= NULL
;
152 struct bt_plugin
*plugin
;
154 BT_LOGD("Finding component class: plugin-name=\"%s\", "
155 "comp-cls-name=\"%s\", comp-cls-type=%d",
156 plugin_name
, comp_class_name
, comp_class_type
);
158 plugin
= find_plugin(plugin_name
);
164 comp_class
= bt_plugin_get_component_class_by_name_and_type(plugin
,
165 comp_class_name
, comp_class_type
);
169 if (BT_LOG_ON_DEBUG
) {
171 BT_LOGD("Found component class: comp-cls-addr=%p",
174 BT_LOGD("Cannot find component class.");
182 void print_indent(FILE *fp
, size_t indent
)
186 for (i
= 0; i
< indent
; i
++) {
192 const char *component_type_str(enum bt_component_class_type type
)
195 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
197 case BT_COMPONENT_CLASS_TYPE_SINK
:
199 case BT_COMPONENT_CLASS_TYPE_FILTER
:
201 case BT_COMPONENT_CLASS_TYPE_UNKNOWN
:
208 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
209 const char *comp_cls_name
, enum bt_component_class_type type
)
211 GString
*shell_plugin_name
= NULL
;
212 GString
*shell_comp_cls_name
= NULL
;
214 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
215 if (!shell_plugin_name
) {
219 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
220 if (!shell_comp_cls_name
) {
224 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
225 bt_common_color_bold(),
226 bt_common_color_fg_cyan(),
227 component_type_str(type
),
228 bt_common_color_fg_default(),
229 bt_common_color_fg_blue(),
230 shell_plugin_name
->str
,
231 bt_common_color_fg_default(),
232 bt_common_color_fg_yellow(),
233 shell_comp_cls_name
->str
,
234 bt_common_color_reset());
237 if (shell_plugin_name
) {
238 g_string_free(shell_plugin_name
, TRUE
);
241 if (shell_comp_cls_name
) {
242 g_string_free(shell_comp_cls_name
, TRUE
);
247 void print_value(FILE *, struct bt_value
*, size_t);
250 void print_value_rec(FILE *, struct bt_value
*, size_t);
252 struct print_map_value_data
{
258 bt_bool
print_map_value(const char *key
, struct bt_value
*object
, void *data
)
260 struct print_map_value_data
*print_map_value_data
= data
;
262 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
263 fprintf(print_map_value_data
->fp
, "%s: ", key
);
265 if (bt_value_is_array(object
) &&
266 bt_value_array_is_empty(object
)) {
267 fprintf(print_map_value_data
->fp
, "[ ]\n");
271 if (bt_value_is_map(object
) &&
272 bt_value_map_is_empty(object
)) {
273 fprintf(print_map_value_data
->fp
, "{ }\n");
277 if (bt_value_is_array(object
) ||
278 bt_value_is_map(object
)) {
279 fprintf(print_map_value_data
->fp
, "\n");
282 print_value_rec(print_map_value_data
->fp
, object
,
283 print_map_value_data
->indent
+ 2);
288 void print_value_rec(FILE *fp
, struct bt_value
*value
, size_t indent
)
301 switch (bt_value_get_type(value
)) {
302 case BT_VALUE_TYPE_NULL
:
303 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
304 bt_common_color_reset());
306 case BT_VALUE_TYPE_BOOL
:
307 bt_value_bool_get(value
, &bool_val
);
308 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
309 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
310 bt_common_color_reset());
312 case BT_VALUE_TYPE_INTEGER
:
313 bt_value_integer_get(value
, &int_val
);
314 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
315 bt_common_color_fg_red(), int_val
,
316 bt_common_color_reset());
318 case BT_VALUE_TYPE_FLOAT
:
319 bt_value_float_get(value
, &dbl_val
);
320 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
321 bt_common_color_fg_red(), dbl_val
,
322 bt_common_color_reset());
324 case BT_VALUE_TYPE_STRING
:
325 bt_value_string_get(value
, &str_val
);
326 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
327 bt_common_color_fg_green(), str_val
,
328 bt_common_color_reset());
330 case BT_VALUE_TYPE_ARRAY
:
331 size
= bt_value_array_size(value
);
335 print_indent(fp
, indent
);
336 fprintf(fp
, "[ ]\n");
340 for (i
= 0; i
< size
; i
++) {
341 struct bt_value
*element
=
342 bt_value_array_get(value
, i
);
345 print_indent(fp
, indent
);
348 if (bt_value_is_array(element
) &&
349 bt_value_array_is_empty(element
)) {
350 fprintf(fp
, "[ ]\n");
354 if (bt_value_is_map(element
) &&
355 bt_value_map_is_empty(element
)) {
356 fprintf(fp
, "{ }\n");
360 if (bt_value_is_array(element
) ||
361 bt_value_is_map(element
)) {
365 print_value_rec(fp
, element
, indent
+ 2);
369 case BT_VALUE_TYPE_MAP
:
371 struct print_map_value_data data
= {
376 if (bt_value_map_is_empty(value
)) {
377 print_indent(fp
, indent
);
378 fprintf(fp
, "{ }\n");
382 bt_value_map_foreach(value
, print_map_value
, &data
);
391 void print_value(FILE *fp
, struct bt_value
*value
, size_t indent
)
393 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
394 print_indent(fp
, indent
);
397 print_value_rec(fp
, value
, indent
);
401 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
403 fprintf(stderr
, " ");
404 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
405 bt_config_component
->comp_cls_name
->str
,
406 bt_config_component
->type
);
407 fprintf(stderr
, ":\n");
409 if (bt_config_component
->instance_name
->len
> 0) {
410 fprintf(stderr
, " Name: %s\n",
411 bt_config_component
->instance_name
->str
);
414 fprintf(stderr
, " Parameters:\n");
415 print_value(stderr
, bt_config_component
->params
, 8);
419 void print_bt_config_components(GPtrArray
*array
)
423 for (i
= 0; i
< array
->len
; i
++) {
424 struct bt_config_component
*cfg_component
=
425 bt_config_get_component(array
, i
);
426 print_bt_config_component(cfg_component
);
427 BT_PUT(cfg_component
);
432 void print_plugin_paths(struct bt_value
*plugin_paths
)
434 fprintf(stderr
, " Plugin paths:\n");
435 print_value(stderr
, plugin_paths
, 4);
439 void print_cfg_run(struct bt_config
*cfg
)
443 print_plugin_paths(cfg
->plugin_paths
);
444 fprintf(stderr
, " Source component instances:\n");
445 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
447 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
448 fprintf(stderr
, " Filter component instances:\n");
449 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
452 fprintf(stderr
, " Sink component instances:\n");
453 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
454 fprintf(stderr
, " Connections:\n");
456 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
457 struct bt_config_connection
*cfg_connection
=
458 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
461 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
462 cfg_connection
->upstream_comp_name
->str
,
463 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
464 cfg_connection
->upstream_port_glob
->str
,
465 cfg_connection
->downstream_comp_name
->str
,
466 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
467 cfg_connection
->downstream_port_glob
->str
);
472 void print_cfg_list_plugins(struct bt_config
*cfg
)
474 print_plugin_paths(cfg
->plugin_paths
);
478 void print_cfg_help(struct bt_config
*cfg
)
480 print_plugin_paths(cfg
->plugin_paths
);
484 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
486 print_plugin_paths(cfg
->plugin_paths
);
487 fprintf(stderr
, " Path: %s\n",
488 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
492 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
494 print_plugin_paths(cfg
->plugin_paths
);
495 fprintf(stderr
, " URL: %s\n",
496 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
500 void print_cfg_query(struct bt_config
*cfg
)
502 print_plugin_paths(cfg
->plugin_paths
);
503 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
504 fprintf(stderr
, " Component class:\n");
505 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
509 void print_cfg(struct bt_config
*cfg
)
511 if (!BT_LOG_ON_INFO
) {
515 BT_LOGI_STR("Configuration:");
516 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
517 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
519 switch (cfg
->command
) {
520 case BT_CONFIG_COMMAND_RUN
:
523 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
524 print_cfg_list_plugins(cfg
);
526 case BT_CONFIG_COMMAND_HELP
:
529 case BT_CONFIG_COMMAND_QUERY
:
530 print_cfg_query(cfg
);
532 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
533 print_cfg_print_ctf_metadata(cfg
);
535 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
536 print_cfg_print_lttng_live_sessions(cfg
);
544 void add_to_loaded_plugins(struct bt_plugin_set
*plugin_set
)
549 count
= bt_plugin_set_get_plugin_count(plugin_set
);
552 for (i
= 0; i
< count
; i
++) {
553 struct bt_plugin
*plugin
=
554 bt_plugin_set_get_plugin(plugin_set
, i
);
555 struct bt_plugin
*loaded_plugin
=
556 find_plugin(bt_plugin_get_name(plugin
));
561 BT_LOGI("Not using plugin: another one already exists with the same name: "
562 "plugin-name=\"%s\", plugin-path=\"%s\", "
563 "existing-plugin-path=\"%s\"",
564 bt_plugin_get_name(plugin
),
565 bt_plugin_get_path(plugin
),
566 bt_plugin_get_path(loaded_plugin
));
567 bt_put(loaded_plugin
);
569 /* Add to global array. */
570 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
571 bt_plugin_get_name(plugin
));
572 g_ptr_array_add(loaded_plugins
, bt_get(plugin
));
580 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
582 int nr_paths
, i
, ret
= 0;
584 nr_paths
= bt_value_array_size(plugin_paths
);
586 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
591 BT_LOGI("Loading dynamic plugins.");
593 for (i
= 0; i
< nr_paths
; i
++) {
594 struct bt_value
*plugin_path_value
= NULL
;
595 const char *plugin_path
;
596 struct bt_plugin_set
*plugin_set
;
598 plugin_path_value
= bt_value_array_get(plugin_paths
, i
);
599 bt_value_string_get(plugin_path_value
, &plugin_path
);
603 * Skip this if the directory does not exist because
604 * bt_plugin_create_all_from_dir() expects an existing
607 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
608 BT_LOGV("Skipping nonexistent directory path: "
609 "path=\"%s\"", plugin_path
);
610 BT_PUT(plugin_path_value
);
614 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
616 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
618 BT_PUT(plugin_path_value
);
622 add_to_loaded_plugins(plugin_set
);
624 BT_PUT(plugin_path_value
);
631 int load_static_plugins(void)
634 struct bt_plugin_set
*plugin_set
;
636 BT_LOGI("Loading static plugins.");
637 plugin_set
= bt_plugin_create_all_from_static();
639 BT_LOGE("Unable to load static plugins.");
644 add_to_loaded_plugins(plugin_set
);
651 int load_all_plugins(struct bt_value
*plugin_paths
)
655 if (load_dynamic_plugins(plugin_paths
)) {
660 if (load_static_plugins()) {
665 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
672 void print_plugin_info(struct bt_plugin
*plugin
)
674 unsigned int major
, minor
, patch
;
676 enum bt_plugin_status version_status
;
677 const char *plugin_name
;
681 const char *plugin_description
;
683 plugin_name
= bt_plugin_get_name(plugin
);
684 path
= bt_plugin_get_path(plugin
);
685 author
= bt_plugin_get_author(plugin
);
686 license
= bt_plugin_get_license(plugin
);
687 plugin_description
= bt_plugin_get_description(plugin
);
688 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
690 printf("%s%s%s%s:\n", bt_common_color_bold(),
691 bt_common_color_fg_blue(), plugin_name
,
692 bt_common_color_reset());
693 printf(" %sPath%s: %s\n", bt_common_color_bold(),
694 bt_common_color_reset(), path
? path
: "(None)");
696 if (version_status
== BT_PLUGIN_STATUS_OK
) {
697 printf(" %sVersion%s: %u.%u.%u",
698 bt_common_color_bold(), bt_common_color_reset(),
699 major
, minor
, patch
);
708 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
709 bt_common_color_reset(),
710 plugin_description
? plugin_description
: "(None)");
711 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
712 bt_common_color_reset(), author
? author
: "(Unknown)");
713 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
714 bt_common_color_reset(),
715 license
? license
: "(Unknown)");
719 int cmd_query(struct bt_config
*cfg
)
722 struct bt_component_class
*comp_cls
= NULL
;
723 struct bt_value
*results
= NULL
;
725 comp_cls
= find_component_class(cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
726 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
727 cfg
->cmd_data
.query
.cfg_component
->type
);
729 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
730 "comp-cls-name=\"%s\", comp-cls-type=%d",
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
, "%s%sCannot find component class %s",
735 bt_common_color_bold(),
736 bt_common_color_fg_red(),
737 bt_common_color_reset());
738 print_plugin_comp_cls_opt(stderr
,
739 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
740 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
741 cfg
->cmd_data
.query
.cfg_component
->type
);
742 fprintf(stderr
, "\n");
747 results
= bt_component_class_query(comp_cls
,
748 cfg
->cmd_data
.query
.object
->str
,
749 cfg
->cmd_data
.query
.cfg_component
->params
);
751 BT_LOGE("Failed to query component class: plugin-name=\"%s\", "
752 "comp-cls-name=\"%s\", comp-cls-type=%d "
754 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
755 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
756 cfg
->cmd_data
.query
.cfg_component
->type
,
757 cfg
->cmd_data
.query
.object
->str
);
758 fprintf(stderr
, "%s%sFailed to query info to %s",
759 bt_common_color_bold(),
760 bt_common_color_fg_red(),
761 bt_common_color_reset());
762 print_plugin_comp_cls_opt(stderr
,
763 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
764 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
765 cfg
->cmd_data
.query
.cfg_component
->type
);
766 fprintf(stderr
, "%s%s with object `%s`%s\n",
767 bt_common_color_bold(),
768 bt_common_color_fg_red(),
769 cfg
->cmd_data
.query
.object
->str
,
770 bt_common_color_reset());
775 print_value(stdout
, results
, 0);
784 int cmd_help(struct bt_config
*cfg
)
787 struct bt_plugin
*plugin
= NULL
;
790 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
792 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
793 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
794 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
795 bt_common_color_bold(), bt_common_color_fg_red(),
796 bt_common_color_fg_blue(),
797 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
798 bt_common_color_reset());
803 print_plugin_info(plugin
);
804 printf(" %sComponent classes%s: %d\n",
805 bt_common_color_bold(),
806 bt_common_color_reset(),
807 (int) bt_plugin_get_component_class_count(plugin
));
810 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
811 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
812 struct bt_component_class
*needed_comp_cls
=
813 find_component_class(
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
);
818 if (!needed_comp_cls
) {
819 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
820 "comp-cls-name=\"%s\", comp-cls-type=%d",
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%s%sCannot find component class %s",
825 bt_common_color_bold(),
826 bt_common_color_fg_red(),
827 bt_common_color_reset());
828 print_plugin_comp_cls_opt(stderr
,
829 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
830 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
831 cfg
->cmd_data
.help
.cfg_component
->type
);
832 fprintf(stderr
, "\n");
837 bt_put(needed_comp_cls
);
840 for (i
= 0; i
< bt_plugin_get_component_class_count(plugin
); i
++) {
841 struct bt_component_class
*comp_cls
=
842 bt_plugin_get_component_class_by_index(plugin
, i
);
843 const char *comp_class_name
=
844 bt_component_class_get_name(comp_cls
);
845 const char *comp_class_description
=
846 bt_component_class_get_description(comp_cls
);
847 const char *comp_class_help
=
848 bt_component_class_get_help(comp_cls
);
849 enum bt_component_class_type type
=
850 bt_component_class_get_type(comp_cls
);
854 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
855 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
856 if (strcmp(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
857 comp_class_name
) != 0 &&
859 cfg
->cmd_data
.help
.cfg_component
->type
) {
866 print_plugin_comp_cls_opt(stdout
,
867 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
871 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
872 bt_common_color_reset(),
873 comp_class_description
? comp_class_description
: "(None)");
875 if (comp_class_help
) {
876 printf("\n%s\n", comp_class_help
);
888 int cmd_list_plugins(struct bt_config
*cfg
)
891 int plugins_count
, component_classes_count
= 0, i
;
893 printf("From the following plugin paths:\n\n");
894 print_value(stdout
, cfg
->plugin_paths
, 2);
896 plugins_count
= loaded_plugins
->len
;
897 if (plugins_count
== 0) {
898 printf("No plugins found.\n");
902 for (i
= 0; i
< plugins_count
; i
++) {
903 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
905 component_classes_count
+= bt_plugin_get_component_class_count(plugin
);
908 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
909 bt_common_color_bold(),
910 component_classes_count
,
911 bt_common_color_reset(),
912 bt_common_color_bold(),
914 bt_common_color_reset());
916 for (i
= 0; i
< plugins_count
; i
++) {
918 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
920 component_classes_count
=
921 bt_plugin_get_component_class_count(plugin
);
923 print_plugin_info(plugin
);
925 if (component_classes_count
== 0) {
926 printf(" %sComponent classes%s: (none)\n",
927 bt_common_color_bold(),
928 bt_common_color_reset());
930 printf(" %sComponent classes%s:\n",
931 bt_common_color_bold(),
932 bt_common_color_reset());
935 for (j
= 0; j
< component_classes_count
; j
++) {
936 struct bt_component_class
*comp_class
=
937 bt_plugin_get_component_class_by_index(
939 const char *comp_class_name
=
940 bt_component_class_get_name(comp_class
);
941 const char *comp_class_description
=
942 bt_component_class_get_description(comp_class
);
943 enum bt_component_class_type type
=
944 bt_component_class_get_type(comp_class
);
947 print_plugin_comp_cls_opt(stdout
,
948 bt_plugin_get_name(plugin
), comp_class_name
,
951 if (comp_class_description
) {
952 printf(": %s", comp_class_description
);
965 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
968 struct bt_component_class
*comp_cls
= NULL
;
969 struct bt_value
*results
= NULL
;
970 struct bt_value
*params
= NULL
;
971 struct bt_value
*map
= NULL
;
972 struct bt_value
*v
= NULL
;
973 static const char * const plugin_name
= "ctf";
974 static const char * const comp_cls_name
= "lttng-live";
975 static const enum bt_component_class_type comp_cls_type
=
976 BT_COMPONENT_CLASS_TYPE_SOURCE
;
977 int64_t array_size
, i
;
979 assert(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
980 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
983 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
984 "comp-cls-name=\"%s\", comp-cls-type=%d",
985 plugin_name
, comp_cls_name
,
986 BT_COMPONENT_CLASS_TYPE_SOURCE
);
987 fprintf(stderr
, "%s%sCannot find component class %s",
988 bt_common_color_bold(),
989 bt_common_color_fg_red(),
990 bt_common_color_reset());
991 print_plugin_comp_cls_opt(stderr
, plugin_name
,
992 comp_cls_name
, comp_cls_type
);
993 fprintf(stderr
, "\n");
997 params
= bt_value_map_create();
1002 ret
= bt_value_map_insert_string(params
, "url",
1003 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1008 results
= bt_component_class_query(comp_cls
, "sessions",
1011 BT_LOGE_STR("Failed to query for sessions.");
1012 fprintf(stderr
, "%s%sFailed to request sessions%s\n",
1013 bt_common_color_bold(),
1014 bt_common_color_fg_red(),
1015 bt_common_color_reset());
1019 if (!bt_value_is_array(results
)) {
1020 BT_LOGE_STR("Expecting an array for sessions query.");
1021 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1022 bt_common_color_bold(),
1023 bt_common_color_fg_red(),
1024 bt_common_color_reset());
1028 array_size
= bt_value_array_size(results
);
1029 for (i
= 0; i
< array_size
; i
++) {
1030 const char *url_text
;
1031 int64_t timer_us
, streams
, clients
;
1033 map
= bt_value_array_get(results
, i
);
1035 BT_LOGE_STR("Unexpected empty array entry.");
1038 if (!bt_value_is_map(map
)) {
1039 BT_LOGE_STR("Unexpected entry type.");
1043 v
= bt_value_map_get(map
, "url");
1045 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1048 ret
= bt_value_string_get(v
, &url_text
);
1050 printf("%s", url_text
);
1053 v
= bt_value_map_get(map
, "timer-us");
1055 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1058 ret
= bt_value_integer_get(v
, &timer_us
);
1060 printf(" (timer = %" PRIu64
", ", timer_us
);
1063 v
= bt_value_map_get(map
, "stream-count");
1065 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1068 ret
= bt_value_integer_get(v
, &streams
);
1070 printf("%" PRIu64
" stream(s), ", streams
);
1073 v
= bt_value_map_get(map
, "client-count");
1075 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1078 ret
= bt_value_integer_get(v
, &clients
);
1080 printf("%" PRIu64
" client(s) connected)\n", clients
);
1099 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1102 struct bt_component_class
*comp_cls
= NULL
;
1103 struct bt_value
*results
= NULL
;
1104 struct bt_value
*params
= NULL
;
1105 struct bt_value
*metadata_text_value
= NULL
;
1106 const char *metadata_text
= NULL
;
1107 static const char * const plugin_name
= "ctf";
1108 static const char * const comp_cls_name
= "fs";
1109 static const enum bt_component_class_type comp_cls_type
=
1110 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1112 assert(cfg
->cmd_data
.print_ctf_metadata
.path
);
1113 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1116 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1117 "comp-cls-name=\"%s\", comp-cls-type=%d",
1118 plugin_name
, comp_cls_name
,
1119 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1120 fprintf(stderr
, "%s%sCannot find component class %s",
1121 bt_common_color_bold(),
1122 bt_common_color_fg_red(),
1123 bt_common_color_reset());
1124 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1125 comp_cls_name
, comp_cls_type
);
1126 fprintf(stderr
, "\n");
1131 params
= bt_value_map_create();
1137 ret
= bt_value_map_insert_string(params
, "path",
1138 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1144 results
= bt_component_class_query(comp_cls
, "metadata-info",
1148 BT_LOGE_STR("Failed to query for metadata info.");
1149 fprintf(stderr
, "%s%sFailed to request metadata info%s\n",
1150 bt_common_color_bold(),
1151 bt_common_color_fg_red(),
1152 bt_common_color_reset());
1156 metadata_text_value
= bt_value_map_get(results
, "text");
1157 if (!metadata_text_value
) {
1158 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1163 ret
= bt_value_string_get(metadata_text_value
, &metadata_text
);
1165 printf("%s\n", metadata_text
);
1170 bt_put(metadata_text_value
);
1176 char *instance_name
;
1180 struct trace_range
{
1181 uint64_t intersection_range_begin_ns
;
1182 uint64_t intersection_range_end_ns
;
1186 guint
port_id_hash(gconstpointer v
)
1188 const struct port_id
*id
= v
;
1190 assert(id
->instance_name
);
1191 assert(id
->port_name
);
1193 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1197 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1199 const struct port_id
*id1
= v1
;
1200 const struct port_id
*id2
= v2
;
1202 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1203 !strcmp(id1
->port_name
, id2
->port_name
);
1207 void port_id_destroy(gpointer data
)
1209 struct port_id
*id
= data
;
1211 free(id
->instance_name
);
1212 free(id
->port_name
);
1217 void trace_range_destroy(gpointer data
)
1222 struct cmd_run_ctx
{
1224 GHashTable
*components
;
1227 struct bt_graph
*graph
;
1230 struct bt_config
*cfg
;
1234 bool stream_intersection_mode
;
1237 * Association of struct port_id -> struct trace_range.
1239 GHashTable
*intersections
;
1242 /* Returns a timestamp of the form "(-)s.ns" */
1244 char *s_from_ns(int64_t ns
)
1249 int64_t ts_sec_abs
, ts_nsec_abs
;
1250 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1251 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1253 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1254 is_negative
= false;
1255 ts_sec_abs
= ts_sec
;
1256 ts_nsec_abs
= ts_nsec
;
1257 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1258 is_negative
= false;
1259 ts_sec_abs
= ts_sec
- 1;
1260 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1261 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1263 ts_sec_abs
= ts_sec
;
1264 ts_nsec_abs
= -ts_nsec
;
1265 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1267 ts_sec_abs
= -(ts_sec
+ 1);
1268 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1269 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1271 ts_sec_abs
= -ts_sec
;
1272 ts_nsec_abs
= ts_nsec
;
1273 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1275 ts_sec_abs
= -ts_sec
;
1276 ts_nsec_abs
= -ts_nsec
;
1279 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1280 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1288 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1289 struct cmd_run_ctx
*ctx
, struct bt_component
*upstream_comp
,
1290 struct bt_port
*upstream_port
,
1291 struct bt_config_connection
*cfg_conn
)
1294 GQuark downstreamp_comp_name_quark
;
1295 struct bt_component
*downstream_comp
;
1296 int64_t downstream_port_count
;
1298 int64_t (*port_count_fn
)(struct bt_component
*);
1299 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
1300 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1301 bool insert_trimmer
= false;
1302 struct bt_value
*trimmer_params
= NULL
;
1303 char *intersection_begin
= NULL
;
1304 char *intersection_end
= NULL
;
1305 struct bt_component
*trimmer
= NULL
;
1306 struct bt_component_class
*trimmer_class
= NULL
;
1307 struct bt_port
*trimmer_input
= NULL
;
1308 struct bt_port
*trimmer_output
= NULL
;
1310 if (ctx
->intersections
&&
1311 bt_component_get_class_type(upstream_comp
) ==
1312 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1313 struct trace_range
*range
;
1314 struct port_id port_id
= {
1315 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1316 .port_name
= (char *) bt_port_get_name(upstream_port
)
1319 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1323 range
= (struct trace_range
*) g_hash_table_lookup(
1324 ctx
->intersections
, &port_id
);
1326 enum bt_value_status status
;
1328 intersection_begin
= s_from_ns(
1329 range
->intersection_range_begin_ns
);
1330 intersection_end
= s_from_ns(
1331 range
->intersection_range_end_ns
);
1332 if (!intersection_begin
|| !intersection_end
) {
1333 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1337 insert_trimmer
= true;
1338 trimmer_params
= bt_value_map_create();
1339 if (!trimmer_params
) {
1343 status
= bt_value_map_insert_string(trimmer_params
,
1344 "begin", intersection_begin
);
1345 if (status
!= BT_VALUE_STATUS_OK
) {
1348 status
= bt_value_map_insert_string(trimmer_params
,
1349 "end", intersection_end
);
1350 if (status
!= BT_VALUE_STATUS_OK
) {
1355 trimmer_class
= find_component_class("utils", "trimmer",
1356 BT_COMPONENT_CLASS_TYPE_FILTER
);
1357 if (!trimmer_class
) {
1362 BT_LOGI("Connecting upstream port to the next available downstream port: "
1363 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1364 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1365 upstream_port
, bt_port_get_name(upstream_port
),
1366 cfg_conn
->downstream_comp_name
->str
,
1367 cfg_conn
->arg
->str
);
1368 downstreamp_comp_name_quark
= g_quark_from_string(
1369 cfg_conn
->downstream_comp_name
->str
);
1370 assert(downstreamp_comp_name_quark
> 0);
1371 downstream_comp
= g_hash_table_lookup(ctx
->components
,
1372 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1373 if (!downstream_comp
) {
1374 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1375 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1376 cfg_conn
->arg
->str
);
1377 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1378 cfg_conn
->arg
->str
);
1382 if (bt_component_is_filter(downstream_comp
)) {
1383 port_count_fn
= bt_component_filter_get_input_port_count
;
1384 port_by_index_fn
= bt_component_filter_get_input_port_by_index
;
1385 } else if (bt_component_is_sink(downstream_comp
)) {
1386 port_count_fn
= bt_component_sink_get_input_port_count
;
1387 port_by_index_fn
= bt_component_sink_get_input_port_by_index
;
1390 * Should never happen because the connections are
1391 * validated before we get here.
1393 BT_LOGF("Invalid connection: downstream component is a source: "
1394 "conn-arg=\"%s\"", cfg_conn
->arg
->str
);
1398 downstream_port_count
= port_count_fn(downstream_comp
);
1399 assert(downstream_port_count
>= 0);
1401 for (i
= 0; i
< downstream_port_count
; i
++) {
1402 struct bt_port
*downstream_port
=
1403 port_by_index_fn(downstream_comp
, i
);
1404 const char *upstream_port_name
;
1405 const char *downstream_port_name
;
1407 assert(downstream_port
);
1409 /* Skip port if it's already connected. */
1410 if (bt_port_is_connected(downstream_port
)) {
1411 bt_put(downstream_port
);
1412 BT_LOGD("Skipping downstream port: already connected: "
1413 "port-addr=%p, port-name=\"%s\"",
1415 bt_port_get_name(downstream_port
));
1419 downstream_port_name
= bt_port_get_name(downstream_port
);
1420 assert(downstream_port_name
);
1421 upstream_port_name
= bt_port_get_name(upstream_port
);
1422 assert(upstream_port_name
);
1424 if (!bt_common_star_glob_match(
1425 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1426 downstream_port_name
, SIZE_MAX
)) {
1427 bt_put(downstream_port
);
1431 if (insert_trimmer
) {
1433 * In order to insert the trimmer between the two
1434 * components that were being connected, we create
1435 * a connection configuration entry which describes
1436 * a connection from the trimmer's output to the
1437 * original input that was being connected.
1439 * Hence, the creation of the trimmer will cause the
1440 * graph "new port" listener to establish all downstream
1441 * connections as its output port is connected. We will
1442 * then establish the connection between the original
1443 * upstream source and the trimmer.
1445 char *trimmer_name
= NULL
;
1446 enum bt_graph_status graph_status
;
1448 ret
= asprintf(&trimmer_name
, "%s-%s",
1449 "stream-intersection-trimmer",
1450 upstream_port_name
);
1456 ctx
->connect_ports
= false;
1457 graph_status
= bt_graph_add_component(ctx
->graph
,
1458 trimmer_class
, trimmer_name
, trimmer_params
,
1461 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1467 bt_component_filter_get_input_port_by_index(
1469 if (!trimmer_input
) {
1473 bt_component_filter_get_output_port_by_index(
1475 if (!trimmer_output
) {
1480 * Replace the current downstream port by the trimmer's
1483 BT_MOVE(downstream_port
, trimmer_input
);
1484 downstream_port_name
= bt_port_get_name(
1486 if (!downstream_port_name
) {
1491 /* We have a winner! */
1492 status
= bt_graph_connect_ports(ctx
->graph
,
1493 upstream_port
, downstream_port
, NULL
);
1494 BT_PUT(downstream_port
);
1496 case BT_GRAPH_STATUS_OK
:
1498 case BT_GRAPH_STATUS_CANCELED
:
1499 BT_LOGI_STR("Graph was canceled by user.");
1500 status
= BT_GRAPH_STATUS_OK
;
1502 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1503 BT_LOGE("A component refused a connection to one of its ports: "
1504 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1505 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1506 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1507 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1509 upstream_comp
, bt_component_get_name(upstream_comp
),
1510 upstream_port
, bt_port_get_name(upstream_port
),
1511 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1512 downstream_port
, downstream_port_name
,
1513 cfg_conn
->arg
->str
);
1515 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1516 bt_port_get_name(upstream_port
),
1517 downstream_port_name
,
1518 cfg_conn
->arg
->str
);
1521 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1522 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1523 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1524 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1525 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1527 upstream_comp
, bt_component_get_name(upstream_comp
),
1528 upstream_port
, bt_port_get_name(upstream_port
),
1529 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1530 downstream_port
, downstream_port_name
,
1531 cfg_conn
->arg
->str
);
1533 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1534 bt_port_get_name(upstream_port
),
1535 downstream_port_name
,
1536 cfg_conn
->arg
->str
);
1540 BT_LOGI("Connected component ports: "
1541 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1542 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1543 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1544 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1546 upstream_comp
, bt_component_get_name(upstream_comp
),
1547 upstream_port
, bt_port_get_name(upstream_port
),
1548 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1549 downstream_port
, downstream_port_name
,
1550 cfg_conn
->arg
->str
);
1552 if (insert_trimmer
) {
1554 * The first connection, from the source to the trimmer,
1555 * has been done. We now connect the trimmer to the
1556 * original downstream port.
1558 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1559 ctx
, trimmer
, trimmer_output
, cfg_conn
);
1563 ctx
->connect_ports
= true;
1568 if (status
!= BT_GRAPH_STATUS_OK
) {
1569 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1570 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1571 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1572 upstream_port
, bt_port_get_name(upstream_port
),
1573 cfg_conn
->downstream_comp_name
->str
,
1574 cfg_conn
->arg
->str
);
1576 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1577 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1587 free(intersection_begin
);
1588 free(intersection_end
);
1589 BT_PUT(trimmer_params
);
1590 BT_PUT(trimmer_class
);
1592 BT_PUT(trimmer_input
);
1593 BT_PUT(trimmer_output
);
1598 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1599 struct bt_port
*upstream_port
)
1602 const char *upstream_port_name
;
1603 const char *upstream_comp_name
;
1604 struct bt_component
*upstream_comp
= NULL
;
1608 assert(upstream_port
);
1609 upstream_port_name
= bt_port_get_name(upstream_port
);
1610 assert(upstream_port_name
);
1611 upstream_comp
= bt_port_get_component(upstream_port
);
1612 if (!upstream_comp
) {
1613 BT_LOGW("Upstream port to connect is not part of a component: "
1614 "port-addr=%p, port-name=\"%s\"",
1615 upstream_port
, upstream_port_name
);
1620 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1621 assert(upstream_comp_name
);
1622 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1623 "port-addr=%p, port-name=\"%s\"",
1624 upstream_comp
, upstream_comp_name
,
1625 upstream_port
, upstream_port_name
);
1627 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1628 struct bt_config_connection
*cfg_conn
=
1630 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1632 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1633 upstream_comp_name
)) {
1637 if (!bt_common_star_glob_match(
1638 cfg_conn
->upstream_port_glob
->str
,
1639 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1643 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1644 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1646 BT_LOGE("Cannot connect upstream port: "
1647 "port-addr=%p, port-name=\"%s\"",
1649 upstream_port_name
);
1651 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1654 cfg_conn
->arg
->str
);
1660 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1661 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1662 upstream_port_name
);
1664 "Cannot create connection: upstream port `%s` does not match any connection\n",
1665 upstream_port_name
);
1671 bt_put(upstream_comp
);
1676 void graph_port_added_listener(struct bt_port
*port
, void *data
)
1678 struct bt_component
*comp
= NULL
;
1679 struct cmd_run_ctx
*ctx
= data
;
1681 comp
= bt_port_get_component(port
);
1682 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1683 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1684 comp
, comp
? bt_component_get_name(comp
) : "",
1685 port
, bt_port_get_name(port
));
1687 if (!ctx
->connect_ports
) {
1692 BT_LOGW_STR("Port has no component.");
1696 if (bt_port_is_connected(port
)) {
1697 BT_LOGW_STR("Port is already connected.");
1701 if (!bt_port_is_output(port
)) {
1702 BT_LOGI_STR("Skipping input port.");
1706 if (cmd_run_ctx_connect_upstream_port(ctx
, port
)) {
1707 BT_LOGF_STR("Cannot connect upstream port.");
1708 fprintf(stderr
, "Added port could not be connected: aborting\n");
1718 void graph_port_removed_listener(struct bt_component
*component
,
1719 struct bt_port
*port
, void *data
)
1721 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1722 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1723 component
, bt_component_get_name(component
),
1724 port
, bt_port_get_name(port
));
1728 void graph_ports_connected_listener(struct bt_port
*upstream_port
,
1729 struct bt_port
*downstream_port
, void *data
)
1731 struct bt_component
*upstream_comp
= bt_port_get_component(upstream_port
);
1732 struct bt_component
*downstream_comp
= bt_port_get_component(downstream_port
);
1734 assert(upstream_comp
);
1735 assert(downstream_comp
);
1736 BT_LOGI("Graph's component ports connected: "
1737 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1738 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1739 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1740 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1741 upstream_comp
, bt_component_get_name(upstream_comp
),
1742 upstream_port
, bt_port_get_name(upstream_port
),
1743 downstream_comp
, bt_component_get_name(downstream_comp
),
1744 downstream_port
, bt_port_get_name(downstream_port
));
1745 bt_put(upstream_comp
);
1746 bt_put(downstream_comp
);
1750 void graph_ports_disconnected_listener(
1751 struct bt_component
*upstream_component
,
1752 struct bt_component
*downstream_component
,
1753 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
1756 BT_LOGI("Graph's component ports disconnected: "
1757 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1758 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1759 upstream_port
, bt_port_get_name(upstream_port
),
1760 downstream_port
, bt_port_get_name(downstream_port
));
1764 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1770 if (ctx
->components
) {
1771 g_hash_table_destroy(ctx
->components
);
1772 ctx
->components
= NULL
;
1775 if (ctx
->intersections
) {
1776 g_hash_table_destroy(ctx
->intersections
);
1777 ctx
->intersections
= NULL
;
1786 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1791 ctx
->connect_ports
= false;
1792 ctx
->components
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
1794 if (!ctx
->components
) {
1798 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1799 ctx
->stream_intersection_mode
= true;
1800 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1801 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1802 if (!ctx
->intersections
) {
1807 ctx
->graph
= bt_graph_create();
1812 the_graph
= ctx
->graph
;
1813 ret
= bt_graph_add_port_added_listener(ctx
->graph
,
1814 graph_port_added_listener
, ctx
);
1816 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
1820 ret
= bt_graph_add_port_removed_listener(ctx
->graph
,
1821 graph_port_removed_listener
, ctx
);
1823 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
1827 ret
= bt_graph_add_ports_connected_listener(ctx
->graph
,
1828 graph_ports_connected_listener
, ctx
);
1830 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
1834 ret
= bt_graph_add_ports_disconnected_listener(ctx
->graph
,
1835 graph_ports_disconnected_listener
, ctx
);
1837 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
1844 cmd_run_ctx_destroy(ctx
);
1852 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
1853 struct bt_config_component
*cfg_comp
,
1854 struct bt_component_class
*comp_cls
)
1858 int64_t trace_count
;
1859 enum bt_value_status value_status
;
1860 const char *path
= NULL
;
1861 struct bt_value
*component_path_value
= NULL
;
1862 struct bt_value
*query_params
= NULL
;
1863 struct bt_value
*query_result
= NULL
;
1864 struct bt_value
*trace_info
= NULL
;
1865 struct bt_value
*intersection_range
= NULL
;
1866 struct bt_value
*intersection_begin
= NULL
;
1867 struct bt_value
*intersection_end
= NULL
;
1868 struct bt_value
*stream_path_value
= NULL
;
1869 struct bt_value
*stream_paths
= NULL
;
1870 struct bt_value
*stream_infos
= NULL
;
1871 struct bt_value
*stream_info
= NULL
;
1872 struct port_id
*port_id
= NULL
;
1873 struct trace_range
*trace_range
= NULL
;
1875 component_path_value
= bt_value_map_get(cfg_comp
->params
, "path");
1876 if (!bt_value_is_string(component_path_value
)) {
1877 BT_LOGD("Cannot get path parameter: component-name=%s",
1878 cfg_comp
->instance_name
->str
);
1883 value_status
= bt_value_string_get(component_path_value
, &path
);
1884 if (value_status
!= BT_VALUE_STATUS_OK
) {
1885 BT_LOGD("Cannot get path string value: component-name=%s",
1886 cfg_comp
->instance_name
->str
);
1891 query_params
= bt_value_map_create();
1892 if (!query_params
) {
1893 BT_LOGE_STR("Cannot create query parameters.");
1898 value_status
= bt_value_map_insert(query_params
, "path", component_path_value
);
1899 if (value_status
!= BT_VALUE_STATUS_OK
) {
1900 BT_LOGE_STR("Cannot insert path parameter in query paramater map.");
1905 query_result
= bt_component_class_query(comp_cls
, "trace-info",
1907 if (!query_result
) {
1908 BT_LOGD("Component class \'%s\' does not support the \'trace-info\' query.",
1909 bt_component_class_get_name(comp_cls
));
1914 if (!bt_value_is_array(query_result
)) {
1915 BT_LOGD("Unexpected format of \'trace-info\' query result: "
1916 "component-class-name=%s",
1917 bt_component_class_get_name(comp_cls
));
1922 trace_count
= bt_value_array_size(query_result
);
1923 if (trace_count
< 0) {
1928 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
1930 uint64_t stream_idx
;
1931 int64_t stream_count
;
1933 trace_info
= bt_value_array_get(query_result
, trace_idx
);
1934 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
1936 BT_LOGD_STR("Cannot retrieve trace from query result.");
1940 intersection_range
= bt_value_map_get(trace_info
,
1941 "intersection-range-ns");
1942 if (!intersection_range
) {
1944 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
1948 intersection_begin
= bt_value_map_get(intersection_range
,
1950 if (!intersection_begin
) {
1952 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
1956 intersection_end
= bt_value_map_get(intersection_range
,
1958 if (!intersection_end
) {
1960 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
1964 value_status
= bt_value_integer_get(intersection_begin
, &begin
);
1965 if (value_status
!= BT_VALUE_STATUS_OK
) {
1967 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'begin\' field from query result.");
1971 value_status
= bt_value_integer_get(intersection_end
, &end
);
1972 if (value_status
!= BT_VALUE_STATUS_OK
) {
1974 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'end\' field from query result.");
1978 if (begin
< 0 || end
< 0 || end
< begin
) {
1979 BT_LOGW("Invalid trace stream intersection values: "
1980 "intersection-range-ns:begin=%" PRId64
1981 ", intersection-range-ns:end=%" PRId64
,
1987 stream_infos
= bt_value_map_get(trace_info
, "streams");
1988 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
1990 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
1994 stream_count
= bt_value_array_size(stream_infos
);
1995 if (stream_count
< 0) {
2003 * The first path of a stream's "paths" is currently used to
2004 * associate streams/ports to a given trace intersection.
2006 * This is a fragile hack as it relies on the port names
2007 * being set to the various streams path.
2009 * A stream name should be introduced as part of the trace-info
2012 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2013 const char *stream_path
;
2015 port_id
= g_new0(struct port_id
, 1);
2018 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2021 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2022 if (!port_id
->instance_name
) {
2024 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2028 trace_range
= g_new0(struct trace_range
, 1);
2031 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2034 trace_range
->intersection_range_begin_ns
= begin
;
2035 trace_range
->intersection_range_end_ns
= end
;
2037 stream_info
= bt_value_array_get(stream_infos
,
2039 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2041 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2045 stream_paths
= bt_value_map_get(stream_info
, "paths");
2046 if (!stream_paths
|| !bt_value_is_array(stream_paths
)) {
2048 BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
2052 stream_path_value
= bt_value_array_get(stream_paths
, 0);
2053 if (!stream_path_value
||
2054 !bt_value_is_string(stream_path_value
)) {
2056 BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
2060 value_status
= bt_value_string_get(stream_path_value
,
2062 if (value_status
!= BT_VALUE_STATUS_OK
) {
2067 port_id
->port_name
= strdup(stream_path
);
2068 if (!port_id
->port_name
) {
2070 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2074 BT_LOGD("Inserting stream intersection ");
2076 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2080 BT_PUT(stream_info
);
2081 BT_PUT(stream_paths
);
2082 BT_PUT(stream_path_value
);
2086 BT_PUT(stream_paths
);
2087 BT_PUT(stream_path_value
);
2088 BT_PUT(intersection_range
);
2089 BT_PUT(intersection_begin
);
2090 BT_PUT(intersection_end
);
2091 BT_PUT(stream_paths
);
2092 BT_PUT(stream_path_value
);
2098 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2099 bt_common_color_bold(),
2100 bt_common_color_fg_yellow(),
2101 path
? path
: "(unknown)",
2102 bt_common_color_reset());
2104 bt_put(component_path_value
);
2105 bt_put(query_params
);
2106 bt_put(query_result
);
2108 bt_put(intersection_range
);
2109 bt_put(intersection_begin
);
2110 bt_put(intersection_end
);
2111 bt_put(stream_infos
);
2112 bt_put(stream_info
);
2113 bt_put(stream_paths
);
2114 bt_put(stream_path_value
);
2116 g_free(trace_range
);
2121 int cmd_run_ctx_create_components_from_config_components(
2122 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2125 struct bt_component_class
*comp_cls
= NULL
;
2126 struct bt_component
*comp
= NULL
;
2129 for (i
= 0; i
< cfg_components
->len
; i
++) {
2130 struct bt_config_component
*cfg_comp
=
2131 g_ptr_array_index(cfg_components
, i
);
2134 comp_cls
= find_component_class(cfg_comp
->plugin_name
->str
,
2135 cfg_comp
->comp_cls_name
->str
, cfg_comp
->type
);
2137 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2138 "comp-cls-name=\"%s\", comp-cls-type=%d",
2139 cfg_comp
->plugin_name
->str
,
2140 cfg_comp
->comp_cls_name
->str
,
2142 fprintf(stderr
, "%s%sCannot find component class %s",
2143 bt_common_color_bold(),
2144 bt_common_color_fg_red(),
2145 bt_common_color_reset());
2146 print_plugin_comp_cls_opt(stderr
,
2147 cfg_comp
->plugin_name
->str
,
2148 cfg_comp
->comp_cls_name
->str
,
2150 fprintf(stderr
, "\n");
2154 ret
= bt_graph_add_component(ctx
->graph
, comp_cls
,
2155 cfg_comp
->instance_name
->str
, cfg_comp
->params
, &comp
);
2157 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2158 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2160 cfg_comp
->plugin_name
->str
,
2161 cfg_comp
->comp_cls_name
->str
,
2162 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2163 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2164 bt_common_color_bold(),
2165 bt_common_color_fg_red(),
2166 cfg_comp
->instance_name
->str
,
2167 bt_common_color_reset());
2171 if (ctx
->stream_intersection_mode
&&
2172 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2173 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2179 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2180 comp
, cfg_comp
->instance_name
->str
);
2181 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2183 g_hash_table_insert(ctx
->components
,
2184 GUINT_TO_POINTER(quark
), comp
);
2201 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2206 * Make sure that, during this phase, our graph's "port added"
2207 * listener does not connect ports while we are creating the
2208 * components because we have a special, initial phase for
2211 ctx
->connect_ports
= false;
2213 ret
= cmd_run_ctx_create_components_from_config_components(
2214 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2220 ret
= cmd_run_ctx_create_components_from_config_components(
2221 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2227 ret
= cmd_run_ctx_create_components_from_config_components(
2228 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2239 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2240 struct bt_component
*comp
,
2241 int64_t (*port_count_fn
)(struct bt_component
*),
2242 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t))
2248 count
= port_count_fn(comp
);
2251 for (i
= 0; i
< count
; i
++) {
2252 struct bt_port
*upstream_port
= port_by_index_fn(comp
, i
);
2254 assert(upstream_port
);
2255 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2256 bt_put(upstream_port
);
2267 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2270 GHashTableIter iter
;
2271 gpointer g_name_quark
, g_comp
;
2273 ctx
->connect_ports
= true;
2274 g_hash_table_iter_init(&iter
, ctx
->components
);
2276 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2277 int64_t (*port_count_fn
)(struct bt_component
*);
2278 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
2280 if (bt_component_is_source(g_comp
)) {
2282 bt_component_source_get_output_port_count
;
2284 bt_component_source_get_output_port_by_index
;
2285 } else if (bt_component_is_filter(g_comp
)) {
2287 bt_component_filter_get_output_port_count
;
2289 bt_component_filter_get_output_port_by_index
;
2294 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
2295 g_comp
, port_count_fn
, port_by_index_fn
);
2306 const char *bt_graph_status_str(enum bt_graph_status status
)
2309 case BT_GRAPH_STATUS_CANCELED
:
2310 return "BT_GRAPH_STATUS_CANCELED";
2311 case BT_GRAPH_STATUS_AGAIN
:
2312 return "BT_GRAPH_STATUS_AGAIN";
2313 case BT_GRAPH_STATUS_END
:
2314 return "BT_GRAPH_STATUS_END";
2315 case BT_GRAPH_STATUS_OK
:
2316 return "BT_GRAPH_STATUS_OK";
2317 case BT_GRAPH_STATUS_INVALID
:
2318 return "BT_GRAPH_STATUS_INVALID";
2319 case BT_GRAPH_STATUS_NO_SINK
:
2320 return "BT_GRAPH_STATUS_NO_SINK";
2321 case BT_GRAPH_STATUS_ERROR
:
2322 return "BT_GRAPH_STATUS_ERROR";
2329 int cmd_run(struct bt_config
*cfg
)
2332 struct cmd_run_ctx ctx
= { 0 };
2334 /* Initialize the command's context and the graph object */
2335 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2336 BT_LOGE_STR("Cannot initialize the command's context.");
2337 fprintf(stderr
, "Cannot initialize the command's context\n");
2342 BT_LOGI_STR("Canceled by user before creating components.");
2346 BT_LOGI_STR("Creating components.");
2348 /* Create the requested component instances */
2349 if (cmd_run_ctx_create_components(&ctx
)) {
2350 BT_LOGE_STR("Cannot create components.");
2351 fprintf(stderr
, "Cannot create components\n");
2356 BT_LOGI_STR("Canceled by user before connecting components.");
2360 BT_LOGI_STR("Connecting components.");
2362 /* Connect the initially visible component ports */
2363 if (cmd_run_ctx_connect_ports(&ctx
)) {
2364 BT_LOGE_STR("Cannot connect initial component ports.");
2365 fprintf(stderr
, "Cannot connect initial component ports\n");
2370 BT_LOGI_STR("Canceled by user before running the graph.");
2374 BT_LOGI_STR("Running the graph.");
2378 enum bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2381 * Reset console in case something messed with console
2382 * codes during the graph's execution.
2384 printf("%s", bt_common_color_reset());
2386 fprintf(stderr
, "%s", bt_common_color_reset());
2387 BT_LOGV("bt_graph_run() returned: status=%s",
2388 bt_graph_status_str(graph_status
));
2390 switch (graph_status
) {
2391 case BT_GRAPH_STATUS_OK
:
2393 case BT_GRAPH_STATUS_CANCELED
:
2394 BT_LOGI_STR("Graph was canceled by user.");
2396 case BT_GRAPH_STATUS_AGAIN
:
2397 if (bt_graph_is_canceled(ctx
.graph
)) {
2398 BT_LOGI_STR("Graph was canceled by user.");
2402 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2403 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2405 cfg
->cmd_data
.run
.retry_duration_us
);
2407 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2408 if (bt_graph_is_canceled(ctx
.graph
)) {
2409 BT_LOGI_STR("Graph was canceled by user.");
2415 case BT_COMPONENT_STATUS_END
:
2418 BT_LOGE_STR("Graph failed to complete successfully");
2419 fprintf(stderr
, "Graph failed to complete successfully\n");
2432 cmd_run_ctx_destroy(&ctx
);
2437 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2439 const char *env_clash
;
2441 if (!cfg
->command_name
) {
2445 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2446 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2450 if (g_file_test(cfg
->command_name
,
2451 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2452 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2454 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2456 fprintf(stderr
, "\n");
2457 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
2463 void init_log_level(void)
2465 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2469 void set_auto_log_levels(struct bt_config
*cfg
)
2471 const char **env_var_name
;
2474 * Override the configuration's default log level if
2475 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2476 * are found for backward compatibility with legacy Babetrace 1.
2478 if (getenv("BABELTRACE_DEBUG") &&
2479 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2480 cfg
->log_level
= 'V';
2481 } else if (getenv("BABELTRACE_VERBOSE") &&
2482 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2483 cfg
->log_level
= 'I';
2487 * Set log levels according to --debug or --verbose. For
2488 * backward compatibility, --debug is more verbose than
2491 * --verbose: INFO log level
2492 * --debug: VERBOSE log level (includes DEBUG, which is
2493 * is less verbose than VERBOSE in the internal
2494 * logging framework)
2496 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2498 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2499 } else if (cfg
->debug
) {
2500 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2503 * Set library's default log level if not
2504 * explicitly specified.
2506 switch (cfg
->log_level
) {
2508 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE
);
2511 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2514 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
2517 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2520 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN
);
2523 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR
);
2526 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL
);
2534 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2536 bt_cli_log_level
= BT_LOG_INFO
;
2537 } else if (cfg
->debug
) {
2538 bt_cli_log_level
= BT_LOG_VERBOSE
;
2541 * Set CLI's default log level if not explicitly
2544 switch (cfg
->log_level
) {
2546 bt_cli_log_level
= BT_LOG_NONE
;
2549 bt_cli_log_level
= BT_LOG_VERBOSE
;
2552 bt_cli_log_level
= BT_LOG_DEBUG
;
2555 bt_cli_log_level
= BT_LOG_INFO
;
2558 bt_cli_log_level
= BT_LOG_WARN
;
2561 bt_cli_log_level
= BT_LOG_ERROR
;
2564 bt_cli_log_level
= BT_LOG_FATAL
;
2572 env_var_name
= log_level_env_var_names
;
2574 while (*env_var_name
) {
2575 if (!getenv(*env_var_name
)) {
2577 g_setenv(*env_var_name
, "I", 1);
2578 } else if (cfg
->debug
) {
2579 g_setenv(*env_var_name
, "V", 1);
2581 char val
[2] = { 0 };
2584 * Set module's default log level if not
2585 * explicitly specified.
2587 val
[0] = cfg
->log_level
;
2588 g_setenv(*env_var_name
, val
, 1);
2597 void set_sigint_handler(void)
2599 struct sigaction new_action
, old_action
;
2601 new_action
.sa_handler
= sigint_handler
;
2602 sigemptyset(&new_action
.sa_mask
);
2603 new_action
.sa_flags
= 0;
2604 sigaction(SIGINT
, NULL
, &old_action
);
2606 if (old_action
.sa_handler
!= SIG_IGN
) {
2607 sigaction(SIGINT
, &new_action
, NULL
);
2611 int main(int argc
, const char **argv
)
2615 struct bt_config
*cfg
;
2618 set_sigint_handler();
2620 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2623 /* Quit without errors; typically usage/version */
2625 BT_LOGI_STR("Quitting without errors.");
2630 BT_LOGE("Command-line error: retcode=%d", retcode
);
2635 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2636 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2641 set_auto_log_levels(cfg
);
2644 if (cfg
->command_needs_plugins
) {
2645 ret
= load_all_plugins(cfg
->plugin_paths
);
2647 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2653 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2654 cfg
->command
, cfg
->command_name
);
2656 switch (cfg
->command
) {
2657 case BT_CONFIG_COMMAND_RUN
:
2660 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2661 ret
= cmd_list_plugins(cfg
);
2663 case BT_CONFIG_COMMAND_HELP
:
2664 ret
= cmd_help(cfg
);
2666 case BT_CONFIG_COMMAND_QUERY
:
2667 ret
= cmd_query(cfg
);
2669 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2670 ret
= cmd_print_ctf_metadata(cfg
);
2672 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2673 ret
= cmd_print_lttng_live_sessions(cfg
);
2676 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2680 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2681 cfg
->command
, cfg
->command_name
, ret
);
2682 warn_command_name_and_directory_clash(cfg
);
2683 retcode
= ret
? 1 : 0;