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_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
83 /* Application's processing graph (weak) */
84 static struct bt_graph
*the_graph
;
85 static bool canceled
= false;
87 GPtrArray
*loaded_plugins
;
90 void sigint_handler(int signum
)
92 if (signum
!= SIGINT
) {
97 bt_graph_cancel(the_graph
);
104 void init_static_data(void)
106 loaded_plugins
= g_ptr_array_new_with_free_func(bt_put
);
110 void fini_static_data(void)
112 g_ptr_array_free(loaded_plugins
, TRUE
);
116 struct bt_plugin
*find_plugin(const char *name
)
119 struct bt_plugin
*plugin
= NULL
;
122 BT_LOGD("Finding plugin: name=\"%s\"", name
);
124 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
125 plugin
= g_ptr_array_index(loaded_plugins
, i
);
127 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
134 if (BT_LOG_ON_DEBUG
) {
136 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
138 BT_LOGD("Cannot find plugin.");
142 return bt_get(plugin
);
146 struct bt_component_class
*find_component_class(const char *plugin_name
,
147 const char *comp_class_name
,
148 enum bt_component_class_type comp_class_type
)
150 struct bt_component_class
*comp_class
= NULL
;
151 struct bt_plugin
*plugin
;
153 BT_LOGD("Finding component class: plugin-name=\"%s\", "
154 "comp-cls-name=\"%s\", comp-cls-type=%d",
155 plugin_name
, comp_class_name
, comp_class_type
);
157 plugin
= find_plugin(plugin_name
);
163 comp_class
= bt_plugin_get_component_class_by_name_and_type(plugin
,
164 comp_class_name
, comp_class_type
);
168 if (BT_LOG_ON_DEBUG
) {
170 BT_LOGD("Found component class: comp-cls-addr=%p",
173 BT_LOGD("Cannot find component class.");
181 void print_indent(FILE *fp
, size_t indent
)
185 for (i
= 0; i
< indent
; i
++) {
191 const char *component_type_str(enum bt_component_class_type type
)
194 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
196 case BT_COMPONENT_CLASS_TYPE_SINK
:
198 case BT_COMPONENT_CLASS_TYPE_FILTER
:
200 case BT_COMPONENT_CLASS_TYPE_UNKNOWN
:
207 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
208 const char *comp_cls_name
, enum bt_component_class_type type
)
210 GString
*shell_plugin_name
= NULL
;
211 GString
*shell_comp_cls_name
= NULL
;
213 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
214 if (!shell_plugin_name
) {
218 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
219 if (!shell_comp_cls_name
) {
223 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
224 bt_common_color_bold(),
225 bt_common_color_fg_cyan(),
226 component_type_str(type
),
227 bt_common_color_fg_default(),
228 bt_common_color_fg_blue(),
229 shell_plugin_name
->str
,
230 bt_common_color_fg_default(),
231 bt_common_color_fg_yellow(),
232 shell_comp_cls_name
->str
,
233 bt_common_color_reset());
236 if (shell_plugin_name
) {
237 g_string_free(shell_plugin_name
, TRUE
);
240 if (shell_comp_cls_name
) {
241 g_string_free(shell_comp_cls_name
, TRUE
);
246 void print_value(FILE *, struct bt_value
*, size_t);
249 void print_value_rec(FILE *, struct bt_value
*, size_t);
251 struct print_map_value_data
{
257 bt_bool
print_map_value(const char *key
, struct bt_value
*object
, void *data
)
259 struct print_map_value_data
*print_map_value_data
= data
;
261 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
262 fprintf(print_map_value_data
->fp
, "%s: ", key
);
264 if (bt_value_is_array(object
) &&
265 bt_value_array_is_empty(object
)) {
266 fprintf(print_map_value_data
->fp
, "[ ]\n");
270 if (bt_value_is_map(object
) &&
271 bt_value_map_is_empty(object
)) {
272 fprintf(print_map_value_data
->fp
, "{ }\n");
276 if (bt_value_is_array(object
) ||
277 bt_value_is_map(object
)) {
278 fprintf(print_map_value_data
->fp
, "\n");
281 print_value_rec(print_map_value_data
->fp
, object
,
282 print_map_value_data
->indent
+ 2);
287 void print_value_rec(FILE *fp
, struct bt_value
*value
, size_t indent
)
300 switch (bt_value_get_type(value
)) {
301 case BT_VALUE_TYPE_NULL
:
302 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
303 bt_common_color_reset());
305 case BT_VALUE_TYPE_BOOL
:
306 bt_value_bool_get(value
, &bool_val
);
307 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
308 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
309 bt_common_color_reset());
311 case BT_VALUE_TYPE_INTEGER
:
312 bt_value_integer_get(value
, &int_val
);
313 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
314 bt_common_color_fg_red(), int_val
,
315 bt_common_color_reset());
317 case BT_VALUE_TYPE_FLOAT
:
318 bt_value_float_get(value
, &dbl_val
);
319 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
320 bt_common_color_fg_red(), dbl_val
,
321 bt_common_color_reset());
323 case BT_VALUE_TYPE_STRING
:
324 bt_value_string_get(value
, &str_val
);
325 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
326 bt_common_color_fg_green(), str_val
,
327 bt_common_color_reset());
329 case BT_VALUE_TYPE_ARRAY
:
330 size
= bt_value_array_size(value
);
334 print_indent(fp
, indent
);
335 fprintf(fp
, "[ ]\n");
339 for (i
= 0; i
< size
; i
++) {
340 struct bt_value
*element
=
341 bt_value_array_get(value
, i
);
344 print_indent(fp
, indent
);
347 if (bt_value_is_array(element
) &&
348 bt_value_array_is_empty(element
)) {
349 fprintf(fp
, "[ ]\n");
353 if (bt_value_is_map(element
) &&
354 bt_value_map_is_empty(element
)) {
355 fprintf(fp
, "{ }\n");
359 if (bt_value_is_array(element
) ||
360 bt_value_is_map(element
)) {
364 print_value_rec(fp
, element
, indent
+ 2);
368 case BT_VALUE_TYPE_MAP
:
370 struct print_map_value_data data
= {
375 if (bt_value_map_is_empty(value
)) {
376 print_indent(fp
, indent
);
377 fprintf(fp
, "{ }\n");
381 bt_value_map_foreach(value
, print_map_value
, &data
);
390 void print_value(FILE *fp
, struct bt_value
*value
, size_t indent
)
392 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
393 print_indent(fp
, indent
);
396 print_value_rec(fp
, value
, indent
);
400 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
402 fprintf(stderr
, " ");
403 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
404 bt_config_component
->comp_cls_name
->str
,
405 bt_config_component
->type
);
406 fprintf(stderr
, ":\n");
408 if (bt_config_component
->instance_name
->len
> 0) {
409 fprintf(stderr
, " Name: %s\n",
410 bt_config_component
->instance_name
->str
);
413 fprintf(stderr
, " Parameters:\n");
414 print_value(stderr
, bt_config_component
->params
, 8);
418 void print_bt_config_components(GPtrArray
*array
)
422 for (i
= 0; i
< array
->len
; i
++) {
423 struct bt_config_component
*cfg_component
=
424 bt_config_get_component(array
, i
);
425 print_bt_config_component(cfg_component
);
426 BT_PUT(cfg_component
);
431 void print_plugin_paths(struct bt_value
*plugin_paths
)
433 fprintf(stderr
, " Plugin paths:\n");
434 print_value(stderr
, plugin_paths
, 4);
438 void print_cfg_run(struct bt_config
*cfg
)
442 print_plugin_paths(cfg
->plugin_paths
);
443 fprintf(stderr
, " Source component instances:\n");
444 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
446 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
447 fprintf(stderr
, " Filter component instances:\n");
448 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
451 fprintf(stderr
, " Sink component instances:\n");
452 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
453 fprintf(stderr
, " Connections:\n");
455 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
456 struct bt_config_connection
*cfg_connection
=
457 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
460 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
461 cfg_connection
->upstream_comp_name
->str
,
462 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
463 cfg_connection
->upstream_port_glob
->str
,
464 cfg_connection
->downstream_comp_name
->str
,
465 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
466 cfg_connection
->downstream_port_glob
->str
);
471 void print_cfg_list_plugins(struct bt_config
*cfg
)
473 print_plugin_paths(cfg
->plugin_paths
);
477 void print_cfg_help(struct bt_config
*cfg
)
479 print_plugin_paths(cfg
->plugin_paths
);
483 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
485 print_plugin_paths(cfg
->plugin_paths
);
486 fprintf(stderr
, " Path: %s\n",
487 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
491 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
493 print_plugin_paths(cfg
->plugin_paths
);
494 fprintf(stderr
, " URL: %s\n",
495 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
499 void print_cfg_query(struct bt_config
*cfg
)
501 print_plugin_paths(cfg
->plugin_paths
);
502 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
503 fprintf(stderr
, " Component class:\n");
504 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
508 void print_cfg(struct bt_config
*cfg
)
510 if (!BT_LOG_ON_INFO
) {
514 BT_LOGI_STR("Configuration:");
515 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
516 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
518 switch (cfg
->command
) {
519 case BT_CONFIG_COMMAND_RUN
:
522 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
523 print_cfg_list_plugins(cfg
);
525 case BT_CONFIG_COMMAND_HELP
:
528 case BT_CONFIG_COMMAND_QUERY
:
529 print_cfg_query(cfg
);
531 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
532 print_cfg_print_ctf_metadata(cfg
);
534 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
535 print_cfg_print_lttng_live_sessions(cfg
);
543 void add_to_loaded_plugins(struct bt_plugin_set
*plugin_set
)
548 count
= bt_plugin_set_get_plugin_count(plugin_set
);
551 for (i
= 0; i
< count
; i
++) {
552 struct bt_plugin
*plugin
=
553 bt_plugin_set_get_plugin(plugin_set
, i
);
554 struct bt_plugin
*loaded_plugin
=
555 find_plugin(bt_plugin_get_name(plugin
));
560 BT_LOGI("Not using plugin: another one already exists with the same name: "
561 "plugin-name=\"%s\", plugin-path=\"%s\", "
562 "existing-plugin-path=\"%s\"",
563 bt_plugin_get_name(plugin
),
564 bt_plugin_get_path(plugin
),
565 bt_plugin_get_path(loaded_plugin
));
566 bt_put(loaded_plugin
);
568 /* Add to global array. */
569 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
570 bt_plugin_get_name(plugin
));
571 g_ptr_array_add(loaded_plugins
, bt_get(plugin
));
579 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
581 int nr_paths
, i
, ret
= 0;
583 nr_paths
= bt_value_array_size(plugin_paths
);
585 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
590 BT_LOGI("Loading dynamic plugins.");
592 for (i
= 0; i
< nr_paths
; i
++) {
593 struct bt_value
*plugin_path_value
= NULL
;
594 const char *plugin_path
;
595 struct bt_plugin_set
*plugin_set
;
597 plugin_path_value
= bt_value_array_get(plugin_paths
, i
);
598 bt_value_string_get(plugin_path_value
, &plugin_path
);
602 * Skip this if the directory does not exist because
603 * bt_plugin_create_all_from_dir() expects an existing
606 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
607 BT_LOGV("Skipping nonexistent directory path: "
608 "path=\"%s\"", plugin_path
);
609 BT_PUT(plugin_path_value
);
613 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
615 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
617 BT_PUT(plugin_path_value
);
621 add_to_loaded_plugins(plugin_set
);
623 BT_PUT(plugin_path_value
);
630 int load_static_plugins(void)
633 struct bt_plugin_set
*plugin_set
;
635 BT_LOGI("Loading static plugins.");
636 plugin_set
= bt_plugin_create_all_from_static();
638 BT_LOGE("Unable to load static plugins.");
643 add_to_loaded_plugins(plugin_set
);
650 int load_all_plugins(struct bt_value
*plugin_paths
)
654 if (load_dynamic_plugins(plugin_paths
)) {
659 if (load_static_plugins()) {
664 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
671 void print_plugin_info(struct bt_plugin
*plugin
)
673 unsigned int major
, minor
, patch
;
675 enum bt_plugin_status version_status
;
676 const char *plugin_name
;
680 const char *plugin_description
;
682 plugin_name
= bt_plugin_get_name(plugin
);
683 path
= bt_plugin_get_path(plugin
);
684 author
= bt_plugin_get_author(plugin
);
685 license
= bt_plugin_get_license(plugin
);
686 plugin_description
= bt_plugin_get_description(plugin
);
687 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
689 printf("%s%s%s%s:\n", bt_common_color_bold(),
690 bt_common_color_fg_blue(), plugin_name
,
691 bt_common_color_reset());
692 printf(" %sPath%s: %s\n", bt_common_color_bold(),
693 bt_common_color_reset(), path
? path
: "(None)");
695 if (version_status
== BT_PLUGIN_STATUS_OK
) {
696 printf(" %sVersion%s: %u.%u.%u",
697 bt_common_color_bold(), bt_common_color_reset(),
698 major
, minor
, patch
);
707 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
708 bt_common_color_reset(),
709 plugin_description
? plugin_description
: "(None)");
710 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
711 bt_common_color_reset(), author
? author
: "(Unknown)");
712 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
713 bt_common_color_reset(),
714 license
? license
: "(Unknown)");
718 int cmd_query(struct bt_config
*cfg
)
721 struct bt_component_class
*comp_cls
= NULL
;
722 struct bt_value
*results
= NULL
;
724 comp_cls
= find_component_class(cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
725 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
726 cfg
->cmd_data
.query
.cfg_component
->type
);
728 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
729 "comp-cls-name=\"%s\", comp-cls-type=%d",
730 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
731 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
732 cfg
->cmd_data
.query
.cfg_component
->type
);
733 fprintf(stderr
, "%s%sCannot find component class %s",
734 bt_common_color_bold(),
735 bt_common_color_fg_red(),
736 bt_common_color_reset());
737 print_plugin_comp_cls_opt(stderr
,
738 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
739 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
740 cfg
->cmd_data
.query
.cfg_component
->type
);
741 fprintf(stderr
, "\n");
746 results
= bt_component_class_query(comp_cls
,
747 cfg
->cmd_data
.query
.object
->str
,
748 cfg
->cmd_data
.query
.cfg_component
->params
);
750 BT_LOGE("Failed to query component class: plugin-name=\"%s\", "
751 "comp-cls-name=\"%s\", comp-cls-type=%d "
753 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
754 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
755 cfg
->cmd_data
.query
.cfg_component
->type
,
756 cfg
->cmd_data
.query
.object
->str
);
757 fprintf(stderr
, "%s%sFailed to query info to %s",
758 bt_common_color_bold(),
759 bt_common_color_fg_red(),
760 bt_common_color_reset());
761 print_plugin_comp_cls_opt(stderr
,
762 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
763 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
764 cfg
->cmd_data
.query
.cfg_component
->type
);
765 fprintf(stderr
, "%s%s with object `%s`%s\n",
766 bt_common_color_bold(),
767 bt_common_color_fg_red(),
768 cfg
->cmd_data
.query
.object
->str
,
769 bt_common_color_reset());
774 print_value(stdout
, results
, 0);
783 int cmd_help(struct bt_config
*cfg
)
786 struct bt_plugin
*plugin
= NULL
;
789 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
791 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
792 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
793 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
794 bt_common_color_bold(), bt_common_color_fg_red(),
795 bt_common_color_fg_blue(),
796 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
797 bt_common_color_reset());
802 print_plugin_info(plugin
);
803 printf(" %sComponent classes%s: %d\n",
804 bt_common_color_bold(),
805 bt_common_color_reset(),
806 (int) bt_plugin_get_component_class_count(plugin
));
809 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
810 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
811 struct bt_component_class
*needed_comp_cls
=
812 find_component_class(
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
);
817 if (!needed_comp_cls
) {
818 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
819 "comp-cls-name=\"%s\", comp-cls-type=%d",
820 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
821 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
822 cfg
->cmd_data
.help
.cfg_component
->type
);
823 fprintf(stderr
, "\n%s%sCannot find component class %s",
824 bt_common_color_bold(),
825 bt_common_color_fg_red(),
826 bt_common_color_reset());
827 print_plugin_comp_cls_opt(stderr
,
828 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
829 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
830 cfg
->cmd_data
.help
.cfg_component
->type
);
831 fprintf(stderr
, "\n");
836 bt_put(needed_comp_cls
);
839 for (i
= 0; i
< bt_plugin_get_component_class_count(plugin
); i
++) {
840 struct bt_component_class
*comp_cls
=
841 bt_plugin_get_component_class_by_index(plugin
, i
);
842 const char *comp_class_name
=
843 bt_component_class_get_name(comp_cls
);
844 const char *comp_class_description
=
845 bt_component_class_get_description(comp_cls
);
846 const char *comp_class_help
=
847 bt_component_class_get_help(comp_cls
);
848 enum bt_component_class_type type
=
849 bt_component_class_get_type(comp_cls
);
853 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
854 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
855 if (strcmp(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
856 comp_class_name
) != 0 &&
858 cfg
->cmd_data
.help
.cfg_component
->type
) {
865 print_plugin_comp_cls_opt(stdout
,
866 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
870 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
871 bt_common_color_reset(),
872 comp_class_description
? comp_class_description
: "(None)");
874 if (comp_class_help
) {
875 printf("\n%s\n", comp_class_help
);
887 int cmd_list_plugins(struct bt_config
*cfg
)
890 int plugins_count
, component_classes_count
= 0, i
;
892 printf("From the following plugin paths:\n\n");
893 print_value(stdout
, cfg
->plugin_paths
, 2);
895 plugins_count
= loaded_plugins
->len
;
896 if (plugins_count
== 0) {
897 printf("No plugins found.\n");
901 for (i
= 0; i
< plugins_count
; i
++) {
902 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
904 component_classes_count
+= bt_plugin_get_component_class_count(plugin
);
907 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
908 bt_common_color_bold(),
909 component_classes_count
,
910 bt_common_color_reset(),
911 bt_common_color_bold(),
913 bt_common_color_reset());
915 for (i
= 0; i
< plugins_count
; i
++) {
917 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
919 component_classes_count
=
920 bt_plugin_get_component_class_count(plugin
);
922 print_plugin_info(plugin
);
924 if (component_classes_count
== 0) {
925 printf(" %sComponent classes%s: (none)\n",
926 bt_common_color_bold(),
927 bt_common_color_reset());
929 printf(" %sComponent classes%s:\n",
930 bt_common_color_bold(),
931 bt_common_color_reset());
934 for (j
= 0; j
< component_classes_count
; j
++) {
935 struct bt_component_class
*comp_class
=
936 bt_plugin_get_component_class_by_index(
938 const char *comp_class_name
=
939 bt_component_class_get_name(comp_class
);
940 const char *comp_class_description
=
941 bt_component_class_get_description(comp_class
);
942 enum bt_component_class_type type
=
943 bt_component_class_get_type(comp_class
);
946 print_plugin_comp_cls_opt(stdout
,
947 bt_plugin_get_name(plugin
), comp_class_name
,
950 if (comp_class_description
) {
951 printf(": %s", comp_class_description
);
964 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
967 struct bt_component_class
*comp_cls
= NULL
;
968 struct bt_value
*results
= NULL
;
969 struct bt_value
*params
= NULL
;
970 struct bt_value
*map
= NULL
;
971 struct bt_value
*v
= NULL
;
972 static const char * const plugin_name
= "ctf";
973 static const char * const comp_cls_name
= "lttng-live";
974 static const enum bt_component_class_type comp_cls_type
=
975 BT_COMPONENT_CLASS_TYPE_SOURCE
;
976 int64_t array_size
, i
;
978 assert(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
979 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
982 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
983 "comp-cls-name=\"%s\", comp-cls-type=%d",
984 plugin_name
, comp_cls_name
,
985 BT_COMPONENT_CLASS_TYPE_SOURCE
);
986 fprintf(stderr
, "%s%sCannot find component class %s",
987 bt_common_color_bold(),
988 bt_common_color_fg_red(),
989 bt_common_color_reset());
990 print_plugin_comp_cls_opt(stderr
, plugin_name
,
991 comp_cls_name
, comp_cls_type
);
992 fprintf(stderr
, "\n");
996 params
= bt_value_map_create();
1001 ret
= bt_value_map_insert_string(params
, "url",
1002 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1007 results
= bt_component_class_query(comp_cls
, "sessions",
1010 BT_LOGE_STR("Failed to query for sessions.");
1011 fprintf(stderr
, "%s%sFailed to request sessions%s\n",
1012 bt_common_color_bold(),
1013 bt_common_color_fg_red(),
1014 bt_common_color_reset());
1018 if (!bt_value_is_array(results
)) {
1019 BT_LOGE_STR("Expecting an array for sessions query.");
1020 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1021 bt_common_color_bold(),
1022 bt_common_color_fg_red(),
1023 bt_common_color_reset());
1027 array_size
= bt_value_array_size(results
);
1028 for (i
= 0; i
< array_size
; i
++) {
1029 const char *url_text
;
1030 int64_t timer_us
, streams
, clients
;
1032 map
= bt_value_array_get(results
, i
);
1034 BT_LOGE_STR("Unexpected empty array entry.");
1037 if (!bt_value_is_map(map
)) {
1038 BT_LOGE_STR("Unexpected entry type.");
1042 v
= bt_value_map_get(map
, "url");
1044 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1047 ret
= bt_value_string_get(v
, &url_text
);
1049 printf("%s", url_text
);
1052 v
= bt_value_map_get(map
, "timer-us");
1054 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1057 ret
= bt_value_integer_get(v
, &timer_us
);
1059 printf(" (timer = %" PRIu64
", ", timer_us
);
1062 v
= bt_value_map_get(map
, "stream-count");
1064 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1067 ret
= bt_value_integer_get(v
, &streams
);
1069 printf("%" PRIu64
" stream(s), ", streams
);
1072 v
= bt_value_map_get(map
, "client-count");
1074 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1077 ret
= bt_value_integer_get(v
, &clients
);
1079 printf("%" PRIu64
" client(s) connected)\n", clients
);
1098 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1101 struct bt_component_class
*comp_cls
= NULL
;
1102 struct bt_value
*results
= NULL
;
1103 struct bt_value
*params
= NULL
;
1104 struct bt_value
*metadata_text_value
= NULL
;
1105 const char *metadata_text
= NULL
;
1106 static const char * const plugin_name
= "ctf";
1107 static const char * const comp_cls_name
= "fs";
1108 static const enum bt_component_class_type comp_cls_type
=
1109 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1111 assert(cfg
->cmd_data
.print_ctf_metadata
.path
);
1112 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1115 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1116 "comp-cls-name=\"%s\", comp-cls-type=%d",
1117 plugin_name
, comp_cls_name
,
1118 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1119 fprintf(stderr
, "%s%sCannot find component class %s",
1120 bt_common_color_bold(),
1121 bt_common_color_fg_red(),
1122 bt_common_color_reset());
1123 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1124 comp_cls_name
, comp_cls_type
);
1125 fprintf(stderr
, "\n");
1130 params
= bt_value_map_create();
1136 ret
= bt_value_map_insert_string(params
, "path",
1137 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1143 results
= bt_component_class_query(comp_cls
, "metadata-info",
1147 BT_LOGE_STR("Failed to query for metadata info.");
1148 fprintf(stderr
, "%s%sFailed to request metadata info%s\n",
1149 bt_common_color_bold(),
1150 bt_common_color_fg_red(),
1151 bt_common_color_reset());
1155 metadata_text_value
= bt_value_map_get(results
, "text");
1156 if (!metadata_text_value
) {
1157 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1162 ret
= bt_value_string_get(metadata_text_value
, &metadata_text
);
1164 printf("%s\n", metadata_text
);
1169 bt_put(metadata_text_value
);
1175 char *instance_name
;
1179 struct trace_range
{
1180 uint64_t intersection_range_begin_ns
;
1181 uint64_t intersection_range_end_ns
;
1185 guint
port_id_hash(gconstpointer v
)
1187 const struct port_id
*id
= v
;
1189 assert(id
->instance_name
);
1190 assert(id
->port_name
);
1192 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1196 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1198 const struct port_id
*id1
= v1
;
1199 const struct port_id
*id2
= v2
;
1201 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1202 !strcmp(id1
->port_name
, id2
->port_name
);
1206 void port_id_destroy(gpointer data
)
1208 struct port_id
*id
= data
;
1210 free(id
->instance_name
);
1211 free(id
->port_name
);
1216 void trace_range_destroy(gpointer data
)
1221 struct cmd_run_ctx
{
1223 GHashTable
*components
;
1226 struct bt_graph
*graph
;
1229 struct bt_config
*cfg
;
1233 bool stream_intersection_mode
;
1236 * Association of struct port_id -> struct trace_range.
1238 GHashTable
*intersections
;
1241 /* Returns a timestamp of the form "(-)s.ns" */
1243 char *s_from_ns(int64_t ns
)
1248 int64_t ts_sec_abs
, ts_nsec_abs
;
1249 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1250 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1252 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1253 is_negative
= false;
1254 ts_sec_abs
= ts_sec
;
1255 ts_nsec_abs
= ts_nsec
;
1256 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1257 is_negative
= false;
1258 ts_sec_abs
= ts_sec
- 1;
1259 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1260 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1262 ts_sec_abs
= ts_sec
;
1263 ts_nsec_abs
= -ts_nsec
;
1264 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1266 ts_sec_abs
= -(ts_sec
+ 1);
1267 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1268 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1270 ts_sec_abs
= -ts_sec
;
1271 ts_nsec_abs
= ts_nsec
;
1272 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1274 ts_sec_abs
= -ts_sec
;
1275 ts_nsec_abs
= -ts_nsec
;
1278 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1279 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1287 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1288 struct cmd_run_ctx
*ctx
, struct bt_component
*upstream_comp
,
1289 struct bt_port
*upstream_port
,
1290 struct bt_config_connection
*cfg_conn
)
1293 GQuark downstreamp_comp_name_quark
;
1294 struct bt_component
*downstream_comp
;
1295 int64_t downstream_port_count
;
1297 int64_t (*port_count_fn
)(struct bt_component
*);
1298 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
1299 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1300 bool insert_trimmer
= false;
1301 struct bt_value
*trimmer_params
= NULL
;
1302 char *intersection_begin
= NULL
;
1303 char *intersection_end
= NULL
;
1304 struct bt_component
*trimmer
= NULL
;
1305 struct bt_component_class
*trimmer_class
= NULL
;
1306 struct bt_port
*trimmer_input
= NULL
;
1307 struct bt_port
*trimmer_output
= NULL
;
1309 if (ctx
->intersections
&&
1310 bt_component_get_class_type(upstream_comp
) ==
1311 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1312 struct trace_range
*range
;
1313 struct port_id port_id
= {
1314 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1315 .port_name
= (char *) bt_port_get_name(upstream_port
)
1318 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1322 range
= (struct trace_range
*) g_hash_table_lookup(
1323 ctx
->intersections
, &port_id
);
1325 enum bt_value_status status
;
1327 intersection_begin
= s_from_ns(
1328 range
->intersection_range_begin_ns
);
1329 intersection_end
= s_from_ns(
1330 range
->intersection_range_end_ns
);
1331 if (!intersection_begin
|| !intersection_end
) {
1332 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1336 insert_trimmer
= true;
1337 trimmer_params
= bt_value_map_create();
1338 if (!trimmer_params
) {
1342 status
= bt_value_map_insert_string(trimmer_params
,
1343 "begin", intersection_begin
);
1344 if (status
!= BT_VALUE_STATUS_OK
) {
1347 status
= bt_value_map_insert_string(trimmer_params
,
1348 "end", intersection_end
);
1349 if (status
!= BT_VALUE_STATUS_OK
) {
1354 trimmer_class
= find_component_class("utils", "trimmer",
1355 BT_COMPONENT_CLASS_TYPE_FILTER
);
1356 if (!trimmer_class
) {
1361 BT_LOGI("Connecting upstream port to the next available downstream port: "
1362 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1363 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1364 upstream_port
, bt_port_get_name(upstream_port
),
1365 cfg_conn
->downstream_comp_name
->str
,
1366 cfg_conn
->arg
->str
);
1367 downstreamp_comp_name_quark
= g_quark_from_string(
1368 cfg_conn
->downstream_comp_name
->str
);
1369 assert(downstreamp_comp_name_quark
> 0);
1370 downstream_comp
= g_hash_table_lookup(ctx
->components
,
1371 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1372 if (!downstream_comp
) {
1373 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1374 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1375 cfg_conn
->arg
->str
);
1376 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1377 cfg_conn
->arg
->str
);
1381 if (bt_component_is_filter(downstream_comp
)) {
1382 port_count_fn
= bt_component_filter_get_input_port_count
;
1383 port_by_index_fn
= bt_component_filter_get_input_port_by_index
;
1384 } else if (bt_component_is_sink(downstream_comp
)) {
1385 port_count_fn
= bt_component_sink_get_input_port_count
;
1386 port_by_index_fn
= bt_component_sink_get_input_port_by_index
;
1389 * Should never happen because the connections are
1390 * validated before we get here.
1392 BT_LOGF("Invalid connection: downstream component is a source: "
1393 "conn-arg=\"%s\"", cfg_conn
->arg
->str
);
1397 downstream_port_count
= port_count_fn(downstream_comp
);
1398 assert(downstream_port_count
>= 0);
1400 for (i
= 0; i
< downstream_port_count
; i
++) {
1401 struct bt_port
*downstream_port
=
1402 port_by_index_fn(downstream_comp
, i
);
1403 const char *upstream_port_name
;
1404 const char *downstream_port_name
;
1406 assert(downstream_port
);
1408 /* Skip port if it's already connected. */
1409 if (bt_port_is_connected(downstream_port
)) {
1410 bt_put(downstream_port
);
1411 BT_LOGD("Skipping downstream port: already connected: "
1412 "port-addr=%p, port-name=\"%s\"",
1414 bt_port_get_name(downstream_port
));
1418 downstream_port_name
= bt_port_get_name(downstream_port
);
1419 assert(downstream_port_name
);
1420 upstream_port_name
= bt_port_get_name(upstream_port
);
1421 assert(upstream_port_name
);
1423 if (!bt_common_star_glob_match(
1424 cfg_conn
->downstream_port_glob
->str
, -1ULL,
1425 downstream_port_name
, -1ULL)) {
1426 bt_put(downstream_port
);
1430 if (insert_trimmer
) {
1432 * In order to insert the trimmer between the two
1433 * components that were being connected, we create
1434 * a connection configuration entry which describes
1435 * a connection from the trimmer's output to the
1436 * original input that was being connected.
1438 * Hence, the creation of the trimmer will cause the
1439 * graph "new port" listener to establish all downstream
1440 * connections as its output port is connected. We will
1441 * then establish the connection between the original
1442 * upstream source and the trimmer.
1444 char *trimmer_name
= NULL
;
1445 enum bt_graph_status graph_status
;
1447 ret
= asprintf(&trimmer_name
, "%s-%s",
1448 "stream-intersection-trimmer",
1449 upstream_port_name
);
1455 ctx
->connect_ports
= false;
1456 graph_status
= bt_graph_add_component(ctx
->graph
,
1457 trimmer_class
, trimmer_name
, trimmer_params
,
1460 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1466 bt_component_filter_get_input_port_by_index(
1468 if (!trimmer_input
) {
1472 bt_component_filter_get_output_port_by_index(
1474 if (!trimmer_output
) {
1479 * Replace the current downstream port by the trimmer's
1482 BT_MOVE(downstream_port
, trimmer_input
);
1483 downstream_port_name
= bt_port_get_name(
1485 if (!downstream_port_name
) {
1490 /* We have a winner! */
1491 status
= bt_graph_connect_ports(ctx
->graph
,
1492 upstream_port
, downstream_port
, NULL
);
1493 BT_PUT(downstream_port
);
1495 case BT_GRAPH_STATUS_OK
:
1497 case BT_GRAPH_STATUS_CANCELED
:
1498 BT_LOGI_STR("Graph was canceled by user.");
1499 status
= BT_GRAPH_STATUS_OK
;
1501 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1502 BT_LOGE("A component refused a connection to one of its ports: "
1503 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1504 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1505 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1506 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1508 upstream_comp
, bt_component_get_name(upstream_comp
),
1509 upstream_port
, bt_port_get_name(upstream_port
),
1510 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1511 downstream_port
, downstream_port_name
,
1512 cfg_conn
->arg
->str
);
1514 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1515 bt_port_get_name(upstream_port
),
1516 downstream_port_name
,
1517 cfg_conn
->arg
->str
);
1520 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1521 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1522 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1523 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1524 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1526 upstream_comp
, bt_component_get_name(upstream_comp
),
1527 upstream_port
, bt_port_get_name(upstream_port
),
1528 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1529 downstream_port
, downstream_port_name
,
1530 cfg_conn
->arg
->str
);
1532 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1533 bt_port_get_name(upstream_port
),
1534 downstream_port_name
,
1535 cfg_conn
->arg
->str
);
1539 BT_LOGI("Connected component ports: "
1540 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1541 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1542 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1543 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1545 upstream_comp
, bt_component_get_name(upstream_comp
),
1546 upstream_port
, bt_port_get_name(upstream_port
),
1547 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1548 downstream_port
, downstream_port_name
,
1549 cfg_conn
->arg
->str
);
1551 if (insert_trimmer
) {
1553 * The first connection, from the source to the trimmer,
1554 * has been done. We now connect the trimmer to the
1555 * original downstream port.
1557 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1558 ctx
, trimmer
, trimmer_output
, cfg_conn
);
1562 ctx
->connect_ports
= true;
1567 if (status
!= BT_GRAPH_STATUS_OK
) {
1568 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1569 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1570 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1571 upstream_port
, bt_port_get_name(upstream_port
),
1572 cfg_conn
->downstream_comp_name
->str
,
1573 cfg_conn
->arg
->str
);
1575 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1576 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1586 free(intersection_begin
);
1587 free(intersection_end
);
1588 BT_PUT(trimmer_params
);
1589 BT_PUT(trimmer_class
);
1591 BT_PUT(trimmer_input
);
1592 BT_PUT(trimmer_output
);
1597 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1598 struct bt_port
*upstream_port
)
1601 const char *upstream_port_name
;
1602 const char *upstream_comp_name
;
1603 struct bt_component
*upstream_comp
= NULL
;
1607 assert(upstream_port
);
1608 upstream_port_name
= bt_port_get_name(upstream_port
);
1609 assert(upstream_port_name
);
1610 upstream_comp
= bt_port_get_component(upstream_port
);
1611 if (!upstream_comp
) {
1612 BT_LOGW("Upstream port to connect is not part of a component: "
1613 "port-addr=%p, port-name=\"%s\"",
1614 upstream_port
, upstream_port_name
);
1619 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1620 assert(upstream_comp_name
);
1621 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1622 "port-addr=%p, port-name=\"%s\"",
1623 upstream_comp
, upstream_comp_name
,
1624 upstream_port
, upstream_port_name
);
1626 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1627 struct bt_config_connection
*cfg_conn
=
1629 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1631 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1632 upstream_comp_name
)) {
1636 if (!bt_common_star_glob_match(
1637 cfg_conn
->upstream_port_glob
->str
,
1638 -1ULL, upstream_port_name
, -1ULL)) {
1642 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1643 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1645 BT_LOGE("Cannot connect upstream port: "
1646 "port-addr=%p, port-name=\"%s\"",
1648 upstream_port_name
);
1650 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1653 cfg_conn
->arg
->str
);
1659 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1660 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1661 upstream_port_name
);
1663 "Cannot create connection: upstream port `%s` does not match any connection\n",
1664 upstream_port_name
);
1670 bt_put(upstream_comp
);
1675 void graph_port_added_listener(struct bt_port
*port
, void *data
)
1677 struct bt_component
*comp
= NULL
;
1678 struct cmd_run_ctx
*ctx
= data
;
1680 comp
= bt_port_get_component(port
);
1681 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1682 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1683 comp
, comp
? bt_component_get_name(comp
) : "",
1684 port
, bt_port_get_name(port
));
1686 if (!ctx
->connect_ports
) {
1691 BT_LOGW_STR("Port has no component.");
1695 if (bt_port_is_connected(port
)) {
1696 BT_LOGW_STR("Port is already connected.");
1700 if (!bt_port_is_output(port
)) {
1701 BT_LOGI_STR("Skipping input port.");
1705 if (cmd_run_ctx_connect_upstream_port(ctx
, port
)) {
1706 BT_LOGF_STR("Cannot connect upstream port.");
1707 fprintf(stderr
, "Added port could not be connected: aborting\n");
1717 void graph_port_removed_listener(struct bt_component
*component
,
1718 struct bt_port
*port
, void *data
)
1720 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1721 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1722 component
, bt_component_get_name(component
),
1723 port
, bt_port_get_name(port
));
1727 void graph_ports_connected_listener(struct bt_port
*upstream_port
,
1728 struct bt_port
*downstream_port
, void *data
)
1730 struct bt_component
*upstream_comp
= bt_port_get_component(upstream_port
);
1731 struct bt_component
*downstream_comp
= bt_port_get_component(downstream_port
);
1733 assert(upstream_comp
);
1734 assert(downstream_comp
);
1735 BT_LOGI("Graph's component ports connected: "
1736 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1737 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1738 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1739 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1740 upstream_comp
, bt_component_get_name(upstream_comp
),
1741 upstream_port
, bt_port_get_name(upstream_port
),
1742 downstream_comp
, bt_component_get_name(downstream_comp
),
1743 downstream_port
, bt_port_get_name(downstream_port
));
1744 bt_put(upstream_comp
);
1745 bt_put(downstream_comp
);
1749 void graph_ports_disconnected_listener(
1750 struct bt_component
*upstream_component
,
1751 struct bt_component
*downstream_component
,
1752 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
1755 BT_LOGI("Graph's component ports disconnected: "
1756 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1757 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1758 upstream_port
, bt_port_get_name(upstream_port
),
1759 downstream_port
, bt_port_get_name(downstream_port
));
1763 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1769 if (ctx
->components
) {
1770 g_hash_table_destroy(ctx
->components
);
1771 ctx
->components
= NULL
;
1774 if (ctx
->intersections
) {
1775 g_hash_table_destroy(ctx
->intersections
);
1776 ctx
->intersections
= NULL
;
1785 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1790 ctx
->connect_ports
= false;
1791 ctx
->components
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
1793 if (!ctx
->components
) {
1797 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1798 ctx
->stream_intersection_mode
= true;
1799 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1800 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1801 if (!ctx
->intersections
) {
1806 ctx
->graph
= bt_graph_create();
1811 the_graph
= ctx
->graph
;
1812 ret
= bt_graph_add_port_added_listener(ctx
->graph
,
1813 graph_port_added_listener
, ctx
);
1815 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
1819 ret
= bt_graph_add_port_removed_listener(ctx
->graph
,
1820 graph_port_removed_listener
, ctx
);
1822 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
1826 ret
= bt_graph_add_ports_connected_listener(ctx
->graph
,
1827 graph_ports_connected_listener
, ctx
);
1829 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
1833 ret
= bt_graph_add_ports_disconnected_listener(ctx
->graph
,
1834 graph_ports_disconnected_listener
, ctx
);
1836 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
1843 cmd_run_ctx_destroy(ctx
);
1851 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
1852 struct bt_config_component
*cfg_comp
,
1853 struct bt_component_class
*comp_cls
)
1857 int64_t trace_count
;
1858 enum bt_value_status value_status
;
1859 const char *path
= NULL
;
1860 struct bt_value
*component_path_value
= NULL
;
1861 struct bt_value
*query_params
= NULL
;
1862 struct bt_value
*query_result
= NULL
;
1863 struct bt_value
*trace_info
= NULL
;
1864 struct bt_value
*intersection_range
= NULL
;
1865 struct bt_value
*intersection_begin
= NULL
;
1866 struct bt_value
*intersection_end
= NULL
;
1867 struct bt_value
*stream_path_value
= NULL
;
1868 struct bt_value
*stream_paths
= NULL
;
1869 struct bt_value
*stream_infos
= NULL
;
1870 struct bt_value
*stream_info
= NULL
;
1871 struct port_id
*port_id
= NULL
;
1872 struct trace_range
*trace_range
= NULL
;
1874 component_path_value
= bt_value_map_get(cfg_comp
->params
, "path");
1875 if (!bt_value_is_string(component_path_value
)) {
1876 BT_LOGD("Cannot get path parameter: component-name=%s",
1877 cfg_comp
->instance_name
->str
);
1882 value_status
= bt_value_string_get(component_path_value
, &path
);
1883 if (value_status
!= BT_VALUE_STATUS_OK
) {
1884 BT_LOGD("Cannot get path string value: component-name=%s",
1885 cfg_comp
->instance_name
->str
);
1890 query_params
= bt_value_map_create();
1891 if (!query_params
) {
1892 BT_LOGE_STR("Cannot create query parameters.");
1897 value_status
= bt_value_map_insert(query_params
, "path", component_path_value
);
1898 if (value_status
!= BT_VALUE_STATUS_OK
) {
1899 BT_LOGE_STR("Cannot insert path parameter in query paramater map.");
1904 query_result
= bt_component_class_query(comp_cls
, "trace-info",
1906 if (!query_result
) {
1907 BT_LOGD("Component class \'%s\' does not support the \'trace-info\' query.",
1908 bt_component_class_get_name(comp_cls
));
1913 if (!bt_value_is_array(query_result
)) {
1914 BT_LOGD("Unexpected format of \'trace-info\' query result: "
1915 "component-class-name=%s",
1916 bt_component_class_get_name(comp_cls
));
1921 trace_count
= bt_value_array_size(query_result
);
1922 if (trace_count
< 0) {
1927 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
1929 uint64_t stream_idx
;
1930 int64_t stream_count
;
1932 trace_info
= bt_value_array_get(query_result
, trace_idx
);
1933 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
1935 BT_LOGD_STR("Cannot retrieve trace from query result.");
1939 intersection_range
= bt_value_map_get(trace_info
,
1940 "intersection-range-ns");
1941 if (!intersection_range
) {
1943 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
1947 intersection_begin
= bt_value_map_get(intersection_range
,
1949 if (!intersection_begin
) {
1951 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
1955 intersection_end
= bt_value_map_get(intersection_range
,
1957 if (!intersection_end
) {
1959 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
1963 value_status
= bt_value_integer_get(intersection_begin
, &begin
);
1964 if (value_status
!= BT_VALUE_STATUS_OK
) {
1966 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'begin\' field from query result.");
1970 value_status
= bt_value_integer_get(intersection_end
, &end
);
1971 if (value_status
!= BT_VALUE_STATUS_OK
) {
1973 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'end\' field from query result.");
1977 if (begin
< 0 || end
< 0 || end
< begin
) {
1978 BT_LOGW("Invalid trace stream intersection values: "
1979 "intersection-range-ns:begin=%" PRId64
1980 ", intersection-range-ns:end=%" PRId64
,
1986 stream_infos
= bt_value_map_get(trace_info
, "streams");
1987 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
1989 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
1993 stream_count
= bt_value_array_size(stream_infos
);
1994 if (stream_count
< 0) {
2002 * The first path of a stream's "paths" is currently used to
2003 * associate streams/ports to a given trace intersection.
2005 * This is a fragile hack as it relies on the port names
2006 * being set to the various streams path.
2008 * A stream name should be introduced as part of the trace-info
2011 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2012 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 hash_ret
= g_hash_table_insert(ctx
->intersections
,
2077 port_id
, trace_range
);
2082 BT_PUT(stream_info
);
2083 BT_PUT(stream_paths
);
2084 BT_PUT(stream_path_value
);
2088 BT_PUT(stream_paths
);
2089 BT_PUT(stream_path_value
);
2090 BT_PUT(intersection_range
);
2091 BT_PUT(intersection_begin
);
2092 BT_PUT(intersection_end
);
2093 BT_PUT(stream_paths
);
2094 BT_PUT(stream_path_value
);
2100 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2101 bt_common_color_bold(),
2102 bt_common_color_fg_yellow(),
2103 path
? path
: "(unknown)",
2104 bt_common_color_reset());
2106 bt_put(component_path_value
);
2107 bt_put(query_params
);
2108 bt_put(query_result
);
2110 bt_put(intersection_range
);
2111 bt_put(intersection_begin
);
2112 bt_put(intersection_end
);
2113 bt_put(stream_infos
);
2114 bt_put(stream_info
);
2115 bt_put(stream_paths
);
2116 bt_put(stream_path_value
);
2118 g_free(trace_range
);
2123 int cmd_run_ctx_create_components_from_config_components(
2124 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2127 struct bt_component_class
*comp_cls
= NULL
;
2128 struct bt_component
*comp
= NULL
;
2131 for (i
= 0; i
< cfg_components
->len
; i
++) {
2132 struct bt_config_component
*cfg_comp
=
2133 g_ptr_array_index(cfg_components
, i
);
2136 comp_cls
= find_component_class(cfg_comp
->plugin_name
->str
,
2137 cfg_comp
->comp_cls_name
->str
, cfg_comp
->type
);
2139 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2140 "comp-cls-name=\"%s\", comp-cls-type=%d",
2141 cfg_comp
->plugin_name
->str
,
2142 cfg_comp
->comp_cls_name
->str
,
2144 fprintf(stderr
, "%s%sCannot find component class %s",
2145 bt_common_color_bold(),
2146 bt_common_color_fg_red(),
2147 bt_common_color_reset());
2148 print_plugin_comp_cls_opt(stderr
,
2149 cfg_comp
->plugin_name
->str
,
2150 cfg_comp
->comp_cls_name
->str
,
2152 fprintf(stderr
, "\n");
2156 ret
= bt_graph_add_component(ctx
->graph
, comp_cls
,
2157 cfg_comp
->instance_name
->str
, cfg_comp
->params
, &comp
);
2159 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2160 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2162 cfg_comp
->plugin_name
->str
,
2163 cfg_comp
->comp_cls_name
->str
,
2164 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2165 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2166 bt_common_color_bold(),
2167 bt_common_color_fg_red(),
2168 cfg_comp
->instance_name
->str
,
2169 bt_common_color_reset());
2173 if (ctx
->stream_intersection_mode
&&
2174 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2175 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2181 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2182 comp
, cfg_comp
->instance_name
->str
);
2183 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2185 g_hash_table_insert(ctx
->components
,
2186 GUINT_TO_POINTER(quark
), comp
);
2203 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2208 * Make sure that, during this phase, our graph's "port added"
2209 * listener does not connect ports while we are creating the
2210 * components because we have a special, initial phase for
2213 ctx
->connect_ports
= false;
2215 ret
= cmd_run_ctx_create_components_from_config_components(
2216 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2222 ret
= cmd_run_ctx_create_components_from_config_components(
2223 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2229 ret
= cmd_run_ctx_create_components_from_config_components(
2230 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2241 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2242 struct bt_component
*comp
,
2243 int64_t (*port_count_fn
)(struct bt_component
*),
2244 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t))
2250 count
= port_count_fn(comp
);
2253 for (i
= 0; i
< count
; i
++) {
2254 struct bt_port
*upstream_port
= port_by_index_fn(comp
, i
);
2256 assert(upstream_port
);
2257 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2258 bt_put(upstream_port
);
2269 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2272 GHashTableIter iter
;
2273 gpointer g_name_quark
, g_comp
;
2275 ctx
->connect_ports
= true;
2276 g_hash_table_iter_init(&iter
, ctx
->components
);
2278 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2279 int64_t (*port_count_fn
)(struct bt_component
*);
2280 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
2282 if (bt_component_is_source(g_comp
)) {
2284 bt_component_source_get_output_port_count
;
2286 bt_component_source_get_output_port_by_index
;
2287 } else if (bt_component_is_filter(g_comp
)) {
2289 bt_component_filter_get_output_port_count
;
2291 bt_component_filter_get_output_port_by_index
;
2296 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
2297 g_comp
, port_count_fn
, port_by_index_fn
);
2308 const char *bt_graph_status_str(enum bt_graph_status status
)
2311 case BT_GRAPH_STATUS_CANCELED
:
2312 return "BT_GRAPH_STATUS_CANCELED";
2313 case BT_GRAPH_STATUS_AGAIN
:
2314 return "BT_GRAPH_STATUS_AGAIN";
2315 case BT_GRAPH_STATUS_END
:
2316 return "BT_GRAPH_STATUS_END";
2317 case BT_GRAPH_STATUS_OK
:
2318 return "BT_GRAPH_STATUS_OK";
2319 case BT_GRAPH_STATUS_INVALID
:
2320 return "BT_GRAPH_STATUS_INVALID";
2321 case BT_GRAPH_STATUS_NO_SINK
:
2322 return "BT_GRAPH_STATUS_NO_SINK";
2323 case BT_GRAPH_STATUS_ERROR
:
2324 return "BT_GRAPH_STATUS_ERROR";
2331 int cmd_run(struct bt_config
*cfg
)
2334 struct cmd_run_ctx ctx
= { 0 };
2336 /* Initialize the command's context and the graph object */
2337 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2338 BT_LOGE_STR("Cannot initialize the command's context.");
2339 fprintf(stderr
, "Cannot initialize the command's context\n");
2344 BT_LOGI_STR("Canceled by user before creating components.");
2348 BT_LOGI_STR("Creating components.");
2350 /* Create the requested component instances */
2351 if (cmd_run_ctx_create_components(&ctx
)) {
2352 BT_LOGE_STR("Cannot create components.");
2353 fprintf(stderr
, "Cannot create components\n");
2358 BT_LOGI_STR("Canceled by user before connecting components.");
2362 BT_LOGI_STR("Connecting components.");
2364 /* Connect the initially visible component ports */
2365 if (cmd_run_ctx_connect_ports(&ctx
)) {
2366 BT_LOGE_STR("Cannot connect initial component ports.");
2367 fprintf(stderr
, "Cannot connect initial component ports\n");
2372 BT_LOGI_STR("Canceled by user before running the graph.");
2376 BT_LOGI_STR("Running the graph.");
2380 enum bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2383 * Reset console in case something messed with console
2384 * codes during the graph's execution.
2386 printf("%s", bt_common_color_reset());
2388 fprintf(stderr
, "%s", bt_common_color_reset());
2389 BT_LOGV("bt_graph_run() returned: status=%s",
2390 bt_graph_status_str(graph_status
));
2392 switch (graph_status
) {
2393 case BT_GRAPH_STATUS_OK
:
2395 case BT_GRAPH_STATUS_CANCELED
:
2396 BT_LOGI_STR("Graph was canceled by user.");
2398 case BT_GRAPH_STATUS_AGAIN
:
2399 if (bt_graph_is_canceled(ctx
.graph
)) {
2400 BT_LOGI_STR("Graph was canceled by user.");
2404 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2405 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2407 cfg
->cmd_data
.run
.retry_duration_us
);
2409 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2410 if (bt_graph_is_canceled(ctx
.graph
)) {
2411 BT_LOGI_STR("Graph was canceled by user.");
2417 case BT_COMPONENT_STATUS_END
:
2420 BT_LOGE_STR("Graph failed to complete successfully");
2421 fprintf(stderr
, "Graph failed to complete successfully\n");
2434 cmd_run_ctx_destroy(&ctx
);
2439 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2441 const char *env_clash
;
2443 if (!cfg
->command_name
) {
2447 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2448 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2452 if (g_file_test(cfg
->command_name
,
2453 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2454 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2456 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2458 fprintf(stderr
, "\n");
2459 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
2465 void init_log_level(void)
2467 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2471 void set_auto_log_levels(struct bt_config
*cfg
)
2473 const char **env_var_name
;
2476 * Override the configuration's default log level if
2477 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2478 * are found for backward compatibility with legacy Babetrace 1.
2480 if (getenv("BABELTRACE_DEBUG") &&
2481 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2482 cfg
->log_level
= 'V';
2483 } else if (getenv("BABELTRACE_VERBOSE") &&
2484 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2485 cfg
->log_level
= 'I';
2489 * Set log levels according to --debug or --verbose. For
2490 * backward compatibility, --debug is more verbose than
2493 * --verbose: INFO log level
2494 * --debug: VERBOSE log level (includes DEBUG, which is
2495 * is less verbose than VERBOSE in the internal
2496 * logging framework)
2498 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2500 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2501 } else if (cfg
->debug
) {
2502 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2505 * Set library's default log level if not
2506 * explicitly specified.
2508 switch (cfg
->log_level
) {
2510 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE
);
2513 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2516 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
2519 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2522 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN
);
2525 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR
);
2528 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL
);
2536 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2538 bt_cli_log_level
= BT_LOG_INFO
;
2539 } else if (cfg
->debug
) {
2540 bt_cli_log_level
= BT_LOG_VERBOSE
;
2543 * Set CLI's default log level if not explicitly
2546 switch (cfg
->log_level
) {
2548 bt_cli_log_level
= BT_LOG_NONE
;
2551 bt_cli_log_level
= BT_LOG_VERBOSE
;
2554 bt_cli_log_level
= BT_LOG_DEBUG
;
2557 bt_cli_log_level
= BT_LOG_INFO
;
2560 bt_cli_log_level
= BT_LOG_WARN
;
2563 bt_cli_log_level
= BT_LOG_ERROR
;
2566 bt_cli_log_level
= BT_LOG_FATAL
;
2574 env_var_name
= log_level_env_var_names
;
2576 while (*env_var_name
) {
2577 if (!getenv(*env_var_name
)) {
2579 setenv(*env_var_name
, "I", 1);
2580 } else if (cfg
->debug
) {
2581 setenv(*env_var_name
, "V", 1);
2583 char val
[2] = { 0 };
2586 * Set module's default log level if not
2587 * explicitly specified.
2589 val
[0] = cfg
->log_level
;
2590 setenv(*env_var_name
, val
, 1);
2599 void set_sigint_handler(void)
2601 struct sigaction new_action
, old_action
;
2603 new_action
.sa_handler
= sigint_handler
;
2604 sigemptyset(&new_action
.sa_mask
);
2605 new_action
.sa_flags
= 0;
2606 sigaction(SIGINT
, NULL
, &old_action
);
2608 if (old_action
.sa_handler
!= SIG_IGN
) {
2609 sigaction(SIGINT
, &new_action
, NULL
);
2613 int main(int argc
, const char **argv
)
2617 struct bt_config
*cfg
;
2620 set_sigint_handler();
2622 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2625 /* Quit without errors; typically usage/version */
2627 BT_LOGI_STR("Quitting without errors.");
2632 BT_LOGE("Command-line error: retcode=%d", retcode
);
2637 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2638 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2643 set_auto_log_levels(cfg
);
2646 if (cfg
->command_needs_plugins
) {
2647 ret
= load_all_plugins(cfg
->plugin_paths
);
2649 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2655 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2656 cfg
->command
, cfg
->command_name
);
2658 switch (cfg
->command
) {
2659 case BT_CONFIG_COMMAND_RUN
:
2662 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2663 ret
= cmd_list_plugins(cfg
);
2665 case BT_CONFIG_COMMAND_HELP
:
2666 ret
= cmd_help(cfg
);
2668 case BT_CONFIG_COMMAND_QUERY
:
2669 ret
= cmd_query(cfg
);
2671 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2672 ret
= cmd_print_ctf_metadata(cfg
);
2674 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2675 ret
= cmd_print_lttng_live_sessions(cfg
);
2678 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2682 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2683 cfg
->command
, cfg
->command_name
, ret
);
2684 warn_command_name_and_directory_clash(cfg
);
2685 retcode
= ret
? 1 : 0;