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_UTILS_TRIMMER_FLT_LOG_LEVEL",
77 "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
81 /* Application's processing graph (weak) */
82 static struct bt_graph
*the_graph
;
83 static bool canceled
= false;
85 GPtrArray
*loaded_plugins
;
88 void sigint_handler(int signum
)
90 if (signum
!= SIGINT
) {
95 bt_graph_cancel(the_graph
);
102 void init_static_data(void)
104 loaded_plugins
= g_ptr_array_new_with_free_func(bt_put
);
108 void fini_static_data(void)
110 g_ptr_array_free(loaded_plugins
, TRUE
);
114 struct bt_plugin
*find_plugin(const char *name
)
117 struct bt_plugin
*plugin
= NULL
;
120 BT_LOGD("Finding plugin: name=\"%s\"", name
);
122 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
123 plugin
= g_ptr_array_index(loaded_plugins
, i
);
125 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
132 if (BT_LOG_ON_DEBUG
) {
134 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
136 BT_LOGD("Cannot find plugin.");
140 return bt_get(plugin
);
144 struct bt_component_class
*find_component_class(const char *plugin_name
,
145 const char *comp_class_name
,
146 enum bt_component_class_type comp_class_type
)
148 struct bt_component_class
*comp_class
= NULL
;
149 struct bt_plugin
*plugin
;
151 BT_LOGD("Finding component class: plugin-name=\"%s\", "
152 "comp-cls-name=\"%s\", comp-cls-type=%d",
153 plugin_name
, comp_class_name
, comp_class_type
);
155 plugin
= find_plugin(plugin_name
);
161 comp_class
= bt_plugin_get_component_class_by_name_and_type(plugin
,
162 comp_class_name
, comp_class_type
);
166 if (BT_LOG_ON_DEBUG
) {
168 BT_LOGD("Found component class: comp-cls-addr=%p",
171 BT_LOGD("Cannot find component class.");
179 void print_indent(FILE *fp
, size_t indent
)
183 for (i
= 0; i
< indent
; i
++) {
189 const char *component_type_str(enum bt_component_class_type type
)
192 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
194 case BT_COMPONENT_CLASS_TYPE_SINK
:
196 case BT_COMPONENT_CLASS_TYPE_FILTER
:
198 case BT_COMPONENT_CLASS_TYPE_UNKNOWN
:
205 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
206 const char *comp_cls_name
, enum bt_component_class_type type
)
208 GString
*shell_plugin_name
= NULL
;
209 GString
*shell_comp_cls_name
= NULL
;
211 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
212 if (!shell_plugin_name
) {
216 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
217 if (!shell_comp_cls_name
) {
221 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
222 bt_common_color_bold(),
223 bt_common_color_fg_cyan(),
224 component_type_str(type
),
225 bt_common_color_fg_default(),
226 bt_common_color_fg_blue(),
227 shell_plugin_name
->str
,
228 bt_common_color_fg_default(),
229 bt_common_color_fg_yellow(),
230 shell_comp_cls_name
->str
,
231 bt_common_color_reset());
234 if (shell_plugin_name
) {
235 g_string_free(shell_plugin_name
, TRUE
);
238 if (shell_comp_cls_name
) {
239 g_string_free(shell_comp_cls_name
, TRUE
);
244 void print_value(FILE *, struct bt_value
*, size_t);
247 void print_value_rec(FILE *, struct bt_value
*, size_t);
249 struct print_map_value_data
{
255 bt_bool
print_map_value(const char *key
, struct bt_value
*object
, void *data
)
257 struct print_map_value_data
*print_map_value_data
= data
;
259 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
260 fprintf(print_map_value_data
->fp
, "%s: ", key
);
262 if (bt_value_is_array(object
) &&
263 bt_value_array_is_empty(object
)) {
264 fprintf(print_map_value_data
->fp
, "[ ]\n");
268 if (bt_value_is_map(object
) &&
269 bt_value_map_is_empty(object
)) {
270 fprintf(print_map_value_data
->fp
, "{ }\n");
274 if (bt_value_is_array(object
) ||
275 bt_value_is_map(object
)) {
276 fprintf(print_map_value_data
->fp
, "\n");
279 print_value_rec(print_map_value_data
->fp
, object
,
280 print_map_value_data
->indent
+ 2);
285 void print_value_rec(FILE *fp
, struct bt_value
*value
, size_t indent
)
298 switch (bt_value_get_type(value
)) {
299 case BT_VALUE_TYPE_NULL
:
300 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
301 bt_common_color_reset());
303 case BT_VALUE_TYPE_BOOL
:
304 bt_value_bool_get(value
, &bool_val
);
305 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
306 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
307 bt_common_color_reset());
309 case BT_VALUE_TYPE_INTEGER
:
310 bt_value_integer_get(value
, &int_val
);
311 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
312 bt_common_color_fg_red(), int_val
,
313 bt_common_color_reset());
315 case BT_VALUE_TYPE_FLOAT
:
316 bt_value_float_get(value
, &dbl_val
);
317 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
318 bt_common_color_fg_red(), dbl_val
,
319 bt_common_color_reset());
321 case BT_VALUE_TYPE_STRING
:
322 bt_value_string_get(value
, &str_val
);
323 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
324 bt_common_color_fg_green(), str_val
,
325 bt_common_color_reset());
327 case BT_VALUE_TYPE_ARRAY
:
328 size
= bt_value_array_size(value
);
332 print_indent(fp
, indent
);
333 fprintf(fp
, "[ ]\n");
337 for (i
= 0; i
< size
; i
++) {
338 struct bt_value
*element
=
339 bt_value_array_get(value
, i
);
342 print_indent(fp
, indent
);
345 if (bt_value_is_array(element
) &&
346 bt_value_array_is_empty(element
)) {
347 fprintf(fp
, "[ ]\n");
351 if (bt_value_is_map(element
) &&
352 bt_value_map_is_empty(element
)) {
353 fprintf(fp
, "{ }\n");
357 if (bt_value_is_array(element
) ||
358 bt_value_is_map(element
)) {
362 print_value_rec(fp
, element
, indent
+ 2);
366 case BT_VALUE_TYPE_MAP
:
368 struct print_map_value_data data
= {
373 if (bt_value_map_is_empty(value
)) {
374 print_indent(fp
, indent
);
375 fprintf(fp
, "{ }\n");
379 bt_value_map_foreach(value
, print_map_value
, &data
);
388 void print_value(FILE *fp
, struct bt_value
*value
, size_t indent
)
390 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
391 print_indent(fp
, indent
);
394 print_value_rec(fp
, value
, indent
);
398 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
400 fprintf(stderr
, " ");
401 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
402 bt_config_component
->comp_cls_name
->str
,
403 bt_config_component
->type
);
404 fprintf(stderr
, ":\n");
406 if (bt_config_component
->instance_name
->len
> 0) {
407 fprintf(stderr
, " Name: %s\n",
408 bt_config_component
->instance_name
->str
);
411 fprintf(stderr
, " Parameters:\n");
412 print_value(stderr
, bt_config_component
->params
, 8);
416 void print_bt_config_components(GPtrArray
*array
)
420 for (i
= 0; i
< array
->len
; i
++) {
421 struct bt_config_component
*cfg_component
=
422 bt_config_get_component(array
, i
);
423 print_bt_config_component(cfg_component
);
424 BT_PUT(cfg_component
);
429 void print_plugin_paths(struct bt_value
*plugin_paths
)
431 fprintf(stderr
, " Plugin paths:\n");
432 print_value(stderr
, plugin_paths
, 4);
436 void print_cfg_run(struct bt_config
*cfg
)
440 print_plugin_paths(cfg
->plugin_paths
);
441 fprintf(stderr
, " Source component instances:\n");
442 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
444 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
445 fprintf(stderr
, " Filter component instances:\n");
446 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
449 fprintf(stderr
, " Sink component instances:\n");
450 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
451 fprintf(stderr
, " Connections:\n");
453 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
454 struct bt_config_connection
*cfg_connection
=
455 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
458 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
459 cfg_connection
->upstream_comp_name
->str
,
460 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
461 cfg_connection
->upstream_port_glob
->str
,
462 cfg_connection
->downstream_comp_name
->str
,
463 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
464 cfg_connection
->downstream_port_glob
->str
);
469 void print_cfg_list_plugins(struct bt_config
*cfg
)
471 print_plugin_paths(cfg
->plugin_paths
);
475 void print_cfg_help(struct bt_config
*cfg
)
477 print_plugin_paths(cfg
->plugin_paths
);
481 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
483 print_plugin_paths(cfg
->plugin_paths
);
484 fprintf(stderr
, " Path: %s\n",
485 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
489 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
491 print_plugin_paths(cfg
->plugin_paths
);
492 fprintf(stderr
, " URL: %s\n",
493 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
497 void print_cfg_query(struct bt_config
*cfg
)
499 print_plugin_paths(cfg
->plugin_paths
);
500 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
501 fprintf(stderr
, " Component class:\n");
502 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
506 void print_cfg(struct bt_config
*cfg
)
508 if (!BT_LOG_ON_INFO
) {
512 BT_LOGI_STR("Configuration:");
513 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
514 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
516 switch (cfg
->command
) {
517 case BT_CONFIG_COMMAND_RUN
:
520 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
521 print_cfg_list_plugins(cfg
);
523 case BT_CONFIG_COMMAND_HELP
:
526 case BT_CONFIG_COMMAND_QUERY
:
527 print_cfg_query(cfg
);
529 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
530 print_cfg_print_ctf_metadata(cfg
);
532 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
533 print_cfg_print_lttng_live_sessions(cfg
);
541 void add_to_loaded_plugins(struct bt_plugin_set
*plugin_set
)
546 count
= bt_plugin_set_get_plugin_count(plugin_set
);
549 for (i
= 0; i
< count
; i
++) {
550 struct bt_plugin
*plugin
=
551 bt_plugin_set_get_plugin(plugin_set
, i
);
552 struct bt_plugin
*loaded_plugin
=
553 find_plugin(bt_plugin_get_name(plugin
));
558 BT_LOGI("Not using plugin: another one already exists with the same name: "
559 "plugin-name=\"%s\", plugin-path=\"%s\", "
560 "existing-plugin-path=\"%s\"",
561 bt_plugin_get_name(plugin
),
562 bt_plugin_get_path(plugin
),
563 bt_plugin_get_path(loaded_plugin
));
564 bt_put(loaded_plugin
);
566 /* Add to global array. */
567 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
568 bt_plugin_get_name(plugin
));
569 g_ptr_array_add(loaded_plugins
, bt_get(plugin
));
577 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
579 int nr_paths
, i
, ret
= 0;
581 nr_paths
= bt_value_array_size(plugin_paths
);
583 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
588 BT_LOGI("Loading dynamic plugins.");
590 for (i
= 0; i
< nr_paths
; i
++) {
591 struct bt_value
*plugin_path_value
= NULL
;
592 const char *plugin_path
;
593 struct bt_plugin_set
*plugin_set
;
595 plugin_path_value
= bt_value_array_get(plugin_paths
, i
);
596 bt_value_string_get(plugin_path_value
, &plugin_path
);
600 * Skip this if the directory does not exist because
601 * bt_plugin_create_all_from_dir() expects an existing
604 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
605 BT_LOGV("Skipping nonexistent directory path: "
606 "path=\"%s\"", plugin_path
);
607 BT_PUT(plugin_path_value
);
611 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
613 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
615 BT_PUT(plugin_path_value
);
619 add_to_loaded_plugins(plugin_set
);
621 BT_PUT(plugin_path_value
);
628 int load_static_plugins(void)
631 struct bt_plugin_set
*plugin_set
;
633 BT_LOGI("Loading static plugins.");
634 plugin_set
= bt_plugin_create_all_from_static();
636 BT_LOGE("Unable to load static plugins.");
641 add_to_loaded_plugins(plugin_set
);
648 int load_all_plugins(struct bt_value
*plugin_paths
)
652 if (load_dynamic_plugins(plugin_paths
)) {
657 if (load_static_plugins()) {
662 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
669 void print_plugin_info(struct bt_plugin
*plugin
)
671 unsigned int major
, minor
, patch
;
673 enum bt_plugin_status version_status
;
674 const char *plugin_name
;
678 const char *plugin_description
;
680 plugin_name
= bt_plugin_get_name(plugin
);
681 path
= bt_plugin_get_path(plugin
);
682 author
= bt_plugin_get_author(plugin
);
683 license
= bt_plugin_get_license(plugin
);
684 plugin_description
= bt_plugin_get_description(plugin
);
685 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
687 printf("%s%s%s%s:\n", bt_common_color_bold(),
688 bt_common_color_fg_blue(), plugin_name
,
689 bt_common_color_reset());
690 printf(" %sPath%s: %s\n", bt_common_color_bold(),
691 bt_common_color_reset(), path
? path
: "(None)");
693 if (version_status
== BT_PLUGIN_STATUS_OK
) {
694 printf(" %sVersion%s: %u.%u.%u",
695 bt_common_color_bold(), bt_common_color_reset(),
696 major
, minor
, patch
);
705 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
706 bt_common_color_reset(),
707 plugin_description
? plugin_description
: "(None)");
708 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
709 bt_common_color_reset(), author
? author
: "(Unknown)");
710 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
711 bt_common_color_reset(),
712 license
? license
: "(Unknown)");
716 int cmd_query(struct bt_config
*cfg
)
719 struct bt_component_class
*comp_cls
= NULL
;
720 struct bt_value
*results
= NULL
;
722 comp_cls
= find_component_class(cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
723 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
724 cfg
->cmd_data
.query
.cfg_component
->type
);
726 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
727 "comp-cls-name=\"%s\", comp-cls-type=%d",
728 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
729 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
730 cfg
->cmd_data
.query
.cfg_component
->type
);
731 fprintf(stderr
, "%s%sCannot find component class %s",
732 bt_common_color_bold(),
733 bt_common_color_fg_red(),
734 bt_common_color_reset());
735 print_plugin_comp_cls_opt(stderr
,
736 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
737 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
738 cfg
->cmd_data
.query
.cfg_component
->type
);
739 fprintf(stderr
, "\n");
744 results
= bt_component_class_query(comp_cls
,
745 cfg
->cmd_data
.query
.object
->str
,
746 cfg
->cmd_data
.query
.cfg_component
->params
);
748 BT_LOGE("Failed to query component class: plugin-name=\"%s\", "
749 "comp-cls-name=\"%s\", comp-cls-type=%d "
751 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
752 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
753 cfg
->cmd_data
.query
.cfg_component
->type
,
754 cfg
->cmd_data
.query
.object
->str
);
755 fprintf(stderr
, "%s%sFailed to query info to %s",
756 bt_common_color_bold(),
757 bt_common_color_fg_red(),
758 bt_common_color_reset());
759 print_plugin_comp_cls_opt(stderr
,
760 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
761 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
762 cfg
->cmd_data
.query
.cfg_component
->type
);
763 fprintf(stderr
, "%s%s with object `%s`%s\n",
764 bt_common_color_bold(),
765 bt_common_color_fg_red(),
766 cfg
->cmd_data
.query
.object
->str
,
767 bt_common_color_reset());
772 print_value(stdout
, results
, 0);
781 int cmd_help(struct bt_config
*cfg
)
784 struct bt_plugin
*plugin
= NULL
;
787 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
789 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
790 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
791 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
792 bt_common_color_bold(), bt_common_color_fg_red(),
793 bt_common_color_fg_blue(),
794 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
795 bt_common_color_reset());
800 print_plugin_info(plugin
);
801 printf(" %sComponent classes%s: %d\n",
802 bt_common_color_bold(),
803 bt_common_color_reset(),
804 (int) bt_plugin_get_component_class_count(plugin
));
807 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
808 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
809 struct bt_component_class
*needed_comp_cls
=
810 find_component_class(
811 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
812 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
813 cfg
->cmd_data
.help
.cfg_component
->type
);
815 if (!needed_comp_cls
) {
816 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
817 "comp-cls-name=\"%s\", comp-cls-type=%d",
818 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
819 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
820 cfg
->cmd_data
.help
.cfg_component
->type
);
821 fprintf(stderr
, "\n%s%sCannot find component class %s",
822 bt_common_color_bold(),
823 bt_common_color_fg_red(),
824 bt_common_color_reset());
825 print_plugin_comp_cls_opt(stderr
,
826 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
827 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
828 cfg
->cmd_data
.help
.cfg_component
->type
);
829 fprintf(stderr
, "\n");
834 bt_put(needed_comp_cls
);
837 for (i
= 0; i
< bt_plugin_get_component_class_count(plugin
); i
++) {
838 struct bt_component_class
*comp_cls
=
839 bt_plugin_get_component_class_by_index(plugin
, i
);
840 const char *comp_class_name
=
841 bt_component_class_get_name(comp_cls
);
842 const char *comp_class_description
=
843 bt_component_class_get_description(comp_cls
);
844 const char *comp_class_help
=
845 bt_component_class_get_help(comp_cls
);
846 enum bt_component_class_type type
=
847 bt_component_class_get_type(comp_cls
);
851 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
852 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
853 if (strcmp(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
854 comp_class_name
) != 0 &&
856 cfg
->cmd_data
.help
.cfg_component
->type
) {
863 print_plugin_comp_cls_opt(stdout
,
864 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
868 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
869 bt_common_color_reset(),
870 comp_class_description
? comp_class_description
: "(None)");
872 if (comp_class_help
) {
873 printf("\n%s\n", comp_class_help
);
885 int cmd_list_plugins(struct bt_config
*cfg
)
888 int plugins_count
, component_classes_count
= 0, i
;
890 printf("From the following plugin paths:\n\n");
891 print_value(stdout
, cfg
->plugin_paths
, 2);
893 plugins_count
= loaded_plugins
->len
;
894 if (plugins_count
== 0) {
895 printf("No plugins found.\n");
899 for (i
= 0; i
< plugins_count
; i
++) {
900 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
902 component_classes_count
+= bt_plugin_get_component_class_count(plugin
);
905 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
906 bt_common_color_bold(),
907 component_classes_count
,
908 bt_common_color_reset(),
909 bt_common_color_bold(),
911 bt_common_color_reset());
913 for (i
= 0; i
< plugins_count
; i
++) {
915 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
917 component_classes_count
=
918 bt_plugin_get_component_class_count(plugin
);
920 print_plugin_info(plugin
);
922 if (component_classes_count
== 0) {
923 printf(" %sComponent classes%s: (none)\n",
924 bt_common_color_bold(),
925 bt_common_color_reset());
927 printf(" %sComponent classes%s:\n",
928 bt_common_color_bold(),
929 bt_common_color_reset());
932 for (j
= 0; j
< component_classes_count
; j
++) {
933 struct bt_component_class
*comp_class
=
934 bt_plugin_get_component_class_by_index(
936 const char *comp_class_name
=
937 bt_component_class_get_name(comp_class
);
938 const char *comp_class_description
=
939 bt_component_class_get_description(comp_class
);
940 enum bt_component_class_type type
=
941 bt_component_class_get_type(comp_class
);
944 print_plugin_comp_cls_opt(stdout
,
945 bt_plugin_get_name(plugin
), comp_class_name
,
948 if (comp_class_description
) {
949 printf(": %s", comp_class_description
);
962 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
965 struct bt_component_class
*comp_cls
= NULL
;
966 struct bt_value
*results
= NULL
;
967 struct bt_value
*params
= NULL
;
968 struct bt_value
*map
= NULL
;
969 struct bt_value
*v
= NULL
;
970 static const char * const plugin_name
= "ctf";
971 static const char * const comp_cls_name
= "lttng-live";
972 static const enum bt_component_class_type comp_cls_type
=
973 BT_COMPONENT_CLASS_TYPE_SOURCE
;
974 int64_t array_size
, i
;
976 assert(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
977 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
980 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
981 "comp-cls-name=\"%s\", comp-cls-type=%d",
982 plugin_name
, comp_cls_name
,
983 BT_COMPONENT_CLASS_TYPE_SOURCE
);
984 fprintf(stderr
, "%s%sCannot find component class %s",
985 bt_common_color_bold(),
986 bt_common_color_fg_red(),
987 bt_common_color_reset());
988 print_plugin_comp_cls_opt(stderr
, plugin_name
,
989 comp_cls_name
, comp_cls_type
);
990 fprintf(stderr
, "\n");
994 params
= bt_value_map_create();
999 ret
= bt_value_map_insert_string(params
, "url",
1000 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1005 results
= bt_component_class_query(comp_cls
, "sessions",
1008 BT_LOGE_STR("Failed to query for sessions.");
1009 fprintf(stderr
, "%s%sFailed to request sessions%s\n",
1010 bt_common_color_bold(),
1011 bt_common_color_fg_red(),
1012 bt_common_color_reset());
1016 if (!bt_value_is_array(results
)) {
1017 BT_LOGE_STR("Expecting an array for sessions query.");
1018 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1019 bt_common_color_bold(),
1020 bt_common_color_fg_red(),
1021 bt_common_color_reset());
1025 array_size
= bt_value_array_size(results
);
1026 for (i
= 0; i
< array_size
; i
++) {
1027 const char *url_text
;
1028 int64_t timer_us
, streams
, clients
;
1030 map
= bt_value_array_get(results
, i
);
1032 BT_LOGE_STR("Unexpected empty array entry.");
1035 if (!bt_value_is_map(map
)) {
1036 BT_LOGE_STR("Unexpected entry type.");
1040 v
= bt_value_map_get(map
, "url");
1042 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1045 ret
= bt_value_string_get(v
, &url_text
);
1047 printf("%s", url_text
);
1050 v
= bt_value_map_get(map
, "timer-us");
1052 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1055 ret
= bt_value_integer_get(v
, &timer_us
);
1057 printf(" (timer = %" PRIu64
", ", timer_us
);
1060 v
= bt_value_map_get(map
, "stream-count");
1062 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1065 ret
= bt_value_integer_get(v
, &streams
);
1067 printf("%" PRIu64
" stream(s), ", streams
);
1070 v
= bt_value_map_get(map
, "client-count");
1072 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1075 ret
= bt_value_integer_get(v
, &clients
);
1077 printf("%" PRIu64
" client(s) connected)\n", clients
);
1096 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1099 struct bt_component_class
*comp_cls
= NULL
;
1100 struct bt_value
*results
= NULL
;
1101 struct bt_value
*params
= NULL
;
1102 struct bt_value
*metadata_text_value
= NULL
;
1103 const char *metadata_text
= NULL
;
1104 static const char * const plugin_name
= "ctf";
1105 static const char * const comp_cls_name
= "fs";
1106 static const enum bt_component_class_type comp_cls_type
=
1107 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1109 assert(cfg
->cmd_data
.print_ctf_metadata
.path
);
1110 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1113 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1114 "comp-cls-name=\"%s\", comp-cls-type=%d",
1115 plugin_name
, comp_cls_name
,
1116 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1117 fprintf(stderr
, "%s%sCannot find component class %s",
1118 bt_common_color_bold(),
1119 bt_common_color_fg_red(),
1120 bt_common_color_reset());
1121 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1122 comp_cls_name
, comp_cls_type
);
1123 fprintf(stderr
, "\n");
1128 params
= bt_value_map_create();
1134 ret
= bt_value_map_insert_string(params
, "path",
1135 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1141 results
= bt_component_class_query(comp_cls
, "metadata-info",
1145 BT_LOGE_STR("Failed to query for metadata info.");
1146 fprintf(stderr
, "%s%sFailed to request metadata info%s\n",
1147 bt_common_color_bold(),
1148 bt_common_color_fg_red(),
1149 bt_common_color_reset());
1153 metadata_text_value
= bt_value_map_get(results
, "text");
1154 if (!metadata_text_value
) {
1155 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1160 ret
= bt_value_string_get(metadata_text_value
, &metadata_text
);
1162 printf("%s\n", metadata_text
);
1167 bt_put(metadata_text_value
);
1173 char *instance_name
;
1177 struct trace_range
{
1178 uint64_t intersection_range_begin_ns
;
1179 uint64_t intersection_range_end_ns
;
1183 guint
port_id_hash(gconstpointer v
)
1185 const struct port_id
*id
= v
;
1187 assert(id
->instance_name
);
1188 assert(id
->port_name
);
1190 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1194 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1196 const struct port_id
*id1
= v1
;
1197 const struct port_id
*id2
= v2
;
1199 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1200 !strcmp(id1
->port_name
, id2
->port_name
);
1204 void port_id_destroy(gpointer data
)
1206 struct port_id
*id
= data
;
1208 free(id
->instance_name
);
1209 free(id
->port_name
);
1214 void trace_range_destroy(gpointer data
)
1219 struct cmd_run_ctx
{
1221 GHashTable
*components
;
1224 struct bt_graph
*graph
;
1227 struct bt_config
*cfg
;
1231 bool stream_intersection_mode
;
1234 * Association of struct port_id -> struct trace_range.
1236 GHashTable
*intersections
;
1239 /* Returns a timestamp of the form "(-)s.ns" */
1241 char *s_from_ns(int64_t ns
)
1246 int64_t ts_sec_abs
, ts_nsec_abs
;
1247 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1248 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1250 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1251 is_negative
= false;
1252 ts_sec_abs
= ts_sec
;
1253 ts_nsec_abs
= ts_nsec
;
1254 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1255 is_negative
= false;
1256 ts_sec_abs
= ts_sec
- 1;
1257 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1258 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1260 ts_sec_abs
= ts_sec
;
1261 ts_nsec_abs
= -ts_nsec
;
1262 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1264 ts_sec_abs
= -(ts_sec
+ 1);
1265 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1266 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1268 ts_sec_abs
= -ts_sec
;
1269 ts_nsec_abs
= ts_nsec
;
1270 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1272 ts_sec_abs
= -ts_sec
;
1273 ts_nsec_abs
= -ts_nsec
;
1276 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1277 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1285 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1286 struct cmd_run_ctx
*ctx
, struct bt_component
*upstream_comp
,
1287 struct bt_port
*upstream_port
,
1288 struct bt_config_connection
*cfg_conn
)
1291 GQuark downstreamp_comp_name_quark
;
1292 struct bt_component
*downstream_comp
;
1293 int64_t downstream_port_count
;
1295 int64_t (*port_count_fn
)(struct bt_component
*);
1296 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
1297 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1298 bool insert_trimmer
= false;
1299 struct bt_value
*trimmer_params
= NULL
;
1300 char *intersection_begin
= NULL
;
1301 char *intersection_end
= NULL
;
1302 struct bt_component
*trimmer
= NULL
;
1303 struct bt_component_class
*trimmer_class
= NULL
;
1304 struct bt_port
*trimmer_input
= NULL
;
1305 struct bt_port
*trimmer_output
= NULL
;
1307 if (ctx
->intersections
&&
1308 bt_component_get_class_type(upstream_comp
) ==
1309 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1310 struct trace_range
*range
;
1311 struct port_id port_id
= {
1312 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1313 .port_name
= (char *) bt_port_get_name(upstream_port
)
1316 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1320 range
= (struct trace_range
*) g_hash_table_lookup(
1321 ctx
->intersections
, &port_id
);
1323 enum bt_value_status status
;
1325 intersection_begin
= s_from_ns(
1326 range
->intersection_range_begin_ns
);
1327 intersection_end
= s_from_ns(
1328 range
->intersection_range_end_ns
);
1329 if (!intersection_begin
|| !intersection_end
) {
1330 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1334 insert_trimmer
= true;
1335 trimmer_params
= bt_value_map_create();
1336 if (!trimmer_params
) {
1340 status
= bt_value_map_insert_string(trimmer_params
,
1341 "begin", intersection_begin
);
1342 if (status
!= BT_VALUE_STATUS_OK
) {
1345 status
= bt_value_map_insert_string(trimmer_params
,
1346 "end", intersection_end
);
1347 if (status
!= BT_VALUE_STATUS_OK
) {
1352 trimmer_class
= find_component_class("utils", "trimmer",
1353 BT_COMPONENT_CLASS_TYPE_FILTER
);
1354 if (!trimmer_class
) {
1359 BT_LOGI("Connecting upstream port to the next available downstream port: "
1360 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1361 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1362 upstream_port
, bt_port_get_name(upstream_port
),
1363 cfg_conn
->downstream_comp_name
->str
,
1364 cfg_conn
->arg
->str
);
1365 downstreamp_comp_name_quark
= g_quark_from_string(
1366 cfg_conn
->downstream_comp_name
->str
);
1367 assert(downstreamp_comp_name_quark
> 0);
1368 downstream_comp
= g_hash_table_lookup(ctx
->components
,
1369 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1370 if (!downstream_comp
) {
1371 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1372 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1373 cfg_conn
->arg
->str
);
1374 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1375 cfg_conn
->arg
->str
);
1379 if (bt_component_is_filter(downstream_comp
)) {
1380 port_count_fn
= bt_component_filter_get_input_port_count
;
1381 port_by_index_fn
= bt_component_filter_get_input_port_by_index
;
1382 } else if (bt_component_is_sink(downstream_comp
)) {
1383 port_count_fn
= bt_component_sink_get_input_port_count
;
1384 port_by_index_fn
= bt_component_sink_get_input_port_by_index
;
1387 * Should never happen because the connections are
1388 * validated before we get here.
1390 BT_LOGF("Invalid connection: downstream component is a source: "
1391 "conn-arg=\"%s\"", cfg_conn
->arg
->str
);
1395 downstream_port_count
= port_count_fn(downstream_comp
);
1396 assert(downstream_port_count
>= 0);
1398 for (i
= 0; i
< downstream_port_count
; i
++) {
1399 struct bt_port
*downstream_port
=
1400 port_by_index_fn(downstream_comp
, i
);
1401 const char *upstream_port_name
;
1402 const char *downstream_port_name
;
1404 assert(downstream_port
);
1406 /* Skip port if it's already connected. */
1407 if (bt_port_is_connected(downstream_port
)) {
1408 bt_put(downstream_port
);
1409 BT_LOGD("Skipping downstream port: already connected: "
1410 "port-addr=%p, port-name=\"%s\"",
1412 bt_port_get_name(downstream_port
));
1416 downstream_port_name
= bt_port_get_name(downstream_port
);
1417 assert(downstream_port_name
);
1418 upstream_port_name
= bt_port_get_name(upstream_port
);
1419 assert(upstream_port_name
);
1421 if (!bt_common_star_glob_match(
1422 cfg_conn
->downstream_port_glob
->str
, -1ULL,
1423 downstream_port_name
, -1ULL)) {
1424 bt_put(downstream_port
);
1428 if (insert_trimmer
) {
1430 * In order to insert the trimmer between the two
1431 * components that were being connected, we create
1432 * a connection configuration entry which describes
1433 * a connection from the trimmer's output to the
1434 * original input that was being connected.
1436 * Hence, the creation of the trimmer will cause the
1437 * graph "new port" listener to establish all downstream
1438 * connections as its output port is connected. We will
1439 * then establish the connection between the original
1440 * upstream source and the trimmer.
1442 char *trimmer_name
= NULL
;
1443 enum bt_graph_status graph_status
;
1445 ret
= asprintf(&trimmer_name
, "%s-%s",
1446 "stream-intersection-trimmer",
1447 upstream_port_name
);
1453 ctx
->connect_ports
= false;
1454 graph_status
= bt_graph_add_component(ctx
->graph
,
1455 trimmer_class
, trimmer_name
, trimmer_params
,
1458 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1464 bt_component_filter_get_input_port_by_index(
1466 if (!trimmer_input
) {
1470 bt_component_filter_get_output_port_by_index(
1472 if (!trimmer_output
) {
1477 * Replace the current downstream port by the trimmer's
1480 BT_MOVE(downstream_port
, trimmer_input
);
1481 downstream_port_name
= bt_port_get_name(
1483 if (!downstream_port_name
) {
1488 /* We have a winner! */
1489 status
= bt_graph_connect_ports(ctx
->graph
,
1490 upstream_port
, downstream_port
, NULL
);
1491 BT_PUT(downstream_port
);
1493 case BT_GRAPH_STATUS_OK
:
1495 case BT_GRAPH_STATUS_CANCELED
:
1496 BT_LOGI_STR("Graph was canceled by user.");
1497 status
= BT_GRAPH_STATUS_OK
;
1499 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1500 BT_LOGE("A component refused a connection to one of its ports: "
1501 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1502 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1503 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1504 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1506 upstream_comp
, bt_component_get_name(upstream_comp
),
1507 upstream_port
, bt_port_get_name(upstream_port
),
1508 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1509 downstream_port
, downstream_port_name
,
1510 cfg_conn
->arg
->str
);
1512 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1513 bt_port_get_name(upstream_port
),
1514 downstream_port_name
,
1515 cfg_conn
->arg
->str
);
1518 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1519 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1520 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1521 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1522 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1524 upstream_comp
, bt_component_get_name(upstream_comp
),
1525 upstream_port
, bt_port_get_name(upstream_port
),
1526 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1527 downstream_port
, downstream_port_name
,
1528 cfg_conn
->arg
->str
);
1530 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1531 bt_port_get_name(upstream_port
),
1532 downstream_port_name
,
1533 cfg_conn
->arg
->str
);
1537 BT_LOGI("Connected component ports: "
1538 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1539 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1540 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1541 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1543 upstream_comp
, bt_component_get_name(upstream_comp
),
1544 upstream_port
, bt_port_get_name(upstream_port
),
1545 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1546 downstream_port
, downstream_port_name
,
1547 cfg_conn
->arg
->str
);
1549 if (insert_trimmer
) {
1551 * The first connection, from the source to the trimmer,
1552 * has been done. We now connect the trimmer to the
1553 * original downstream port.
1555 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1556 ctx
, trimmer
, trimmer_output
, cfg_conn
);
1560 ctx
->connect_ports
= true;
1565 if (status
!= BT_GRAPH_STATUS_OK
) {
1566 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1567 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1568 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1569 upstream_port
, bt_port_get_name(upstream_port
),
1570 cfg_conn
->downstream_comp_name
->str
,
1571 cfg_conn
->arg
->str
);
1573 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1574 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1584 free(intersection_begin
);
1585 free(intersection_end
);
1586 BT_PUT(trimmer_params
);
1587 BT_PUT(trimmer_class
);
1589 BT_PUT(trimmer_input
);
1590 BT_PUT(trimmer_output
);
1595 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1596 struct bt_port
*upstream_port
)
1599 const char *upstream_port_name
;
1600 const char *upstream_comp_name
;
1601 struct bt_component
*upstream_comp
= NULL
;
1605 assert(upstream_port
);
1606 upstream_port_name
= bt_port_get_name(upstream_port
);
1607 assert(upstream_port_name
);
1608 upstream_comp
= bt_port_get_component(upstream_port
);
1609 if (!upstream_comp
) {
1610 BT_LOGW("Upstream port to connect is not part of a component: "
1611 "port-addr=%p, port-name=\"%s\"",
1612 upstream_port
, upstream_port_name
);
1617 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1618 assert(upstream_comp_name
);
1619 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1620 "port-addr=%p, port-name=\"%s\"",
1621 upstream_comp
, upstream_comp_name
,
1622 upstream_port
, upstream_port_name
);
1624 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1625 struct bt_config_connection
*cfg_conn
=
1627 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1629 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1630 upstream_comp_name
)) {
1634 if (!bt_common_star_glob_match(
1635 cfg_conn
->upstream_port_glob
->str
,
1636 -1ULL, upstream_port_name
, -1ULL)) {
1640 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1641 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1643 BT_LOGE("Cannot connect upstream port: "
1644 "port-addr=%p, port-name=\"%s\"",
1646 upstream_port_name
);
1648 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1651 cfg_conn
->arg
->str
);
1657 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1658 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1659 upstream_port_name
);
1661 "Cannot create connection: upstream port `%s` does not match any connection\n",
1662 upstream_port_name
);
1668 bt_put(upstream_comp
);
1673 void graph_port_added_listener(struct bt_port
*port
, void *data
)
1675 struct bt_component
*comp
= NULL
;
1676 struct cmd_run_ctx
*ctx
= data
;
1678 comp
= bt_port_get_component(port
);
1679 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1680 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1681 comp
, comp
? bt_component_get_name(comp
) : "",
1682 port
, bt_port_get_name(port
));
1684 if (!ctx
->connect_ports
) {
1689 BT_LOGW_STR("Port has no component.");
1693 if (bt_port_is_connected(port
)) {
1694 BT_LOGW_STR("Port is already connected.");
1698 if (!bt_port_is_output(port
)) {
1699 BT_LOGI_STR("Skipping input port.");
1703 if (cmd_run_ctx_connect_upstream_port(ctx
, port
)) {
1704 BT_LOGF_STR("Cannot connect upstream port.");
1705 fprintf(stderr
, "Added port could not be connected: aborting\n");
1715 void graph_port_removed_listener(struct bt_component
*component
,
1716 struct bt_port
*port
, void *data
)
1718 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1719 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1720 component
, bt_component_get_name(component
),
1721 port
, bt_port_get_name(port
));
1725 void graph_ports_connected_listener(struct bt_port
*upstream_port
,
1726 struct bt_port
*downstream_port
, void *data
)
1728 struct bt_component
*upstream_comp
= bt_port_get_component(upstream_port
);
1729 struct bt_component
*downstream_comp
= bt_port_get_component(downstream_port
);
1731 assert(upstream_comp
);
1732 assert(downstream_comp
);
1733 BT_LOGI("Graph's component ports connected: "
1734 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1735 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1736 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1737 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1738 upstream_comp
, bt_component_get_name(upstream_comp
),
1739 upstream_port
, bt_port_get_name(upstream_port
),
1740 downstream_comp
, bt_component_get_name(downstream_comp
),
1741 downstream_port
, bt_port_get_name(downstream_port
));
1742 bt_put(upstream_comp
);
1743 bt_put(downstream_comp
);
1747 void graph_ports_disconnected_listener(
1748 struct bt_component
*upstream_component
,
1749 struct bt_component
*downstream_component
,
1750 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
1753 BT_LOGI("Graph's component ports disconnected: "
1754 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1755 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1756 upstream_port
, bt_port_get_name(upstream_port
),
1757 downstream_port
, bt_port_get_name(downstream_port
));
1761 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1767 if (ctx
->components
) {
1768 g_hash_table_destroy(ctx
->components
);
1769 ctx
->components
= NULL
;
1772 if (ctx
->intersections
) {
1773 g_hash_table_destroy(ctx
->intersections
);
1774 ctx
->components
= NULL
;
1783 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1788 ctx
->connect_ports
= false;
1789 ctx
->components
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
1791 if (!ctx
->components
) {
1795 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1796 ctx
->stream_intersection_mode
= true;
1797 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1798 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1799 if (!ctx
->intersections
) {
1804 ctx
->graph
= bt_graph_create();
1809 the_graph
= ctx
->graph
;
1810 ret
= bt_graph_add_port_added_listener(ctx
->graph
,
1811 graph_port_added_listener
, ctx
);
1813 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
1817 ret
= bt_graph_add_port_removed_listener(ctx
->graph
,
1818 graph_port_removed_listener
, ctx
);
1820 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
1824 ret
= bt_graph_add_ports_connected_listener(ctx
->graph
,
1825 graph_ports_connected_listener
, ctx
);
1827 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
1831 ret
= bt_graph_add_ports_disconnected_listener(ctx
->graph
,
1832 graph_ports_disconnected_listener
, ctx
);
1834 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
1841 cmd_run_ctx_destroy(ctx
);
1849 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
1850 struct bt_config_component
*cfg_comp
,
1851 struct bt_component_class
*comp_cls
)
1855 int64_t trace_count
;
1856 enum bt_value_status value_status
;
1857 const char *path
= NULL
;
1858 struct bt_value
*component_path_value
= NULL
;
1859 struct bt_value
*query_params
= NULL
;
1860 struct bt_value
*query_result
= NULL
;
1861 struct bt_value
*trace_info
= NULL
;
1862 struct bt_value
*intersection_range
= NULL
;
1863 struct bt_value
*intersection_begin
= NULL
;
1864 struct bt_value
*intersection_end
= NULL
;
1865 struct bt_value
*stream_path_value
= NULL
;
1866 struct bt_value
*stream_paths
= NULL
;
1867 struct bt_value
*stream_infos
= NULL
;
1868 struct bt_value
*stream_info
= NULL
;
1869 struct port_id
*port_id
= NULL
;
1870 struct trace_range
*trace_range
= NULL
;
1872 component_path_value
= bt_value_map_get(cfg_comp
->params
, "path");
1873 if (!bt_value_is_string(component_path_value
)) {
1874 BT_LOGD("Cannot get path parameter: component-name=%s",
1875 cfg_comp
->instance_name
->str
);
1880 value_status
= bt_value_string_get(component_path_value
, &path
);
1881 if (value_status
!= BT_VALUE_STATUS_OK
) {
1882 BT_LOGD("Cannot get path string value: component-name=%s",
1883 cfg_comp
->instance_name
->str
);
1888 query_params
= bt_value_map_create();
1889 if (!query_params
) {
1890 BT_LOGE_STR("Cannot create query parameters.");
1895 value_status
= bt_value_map_insert(query_params
, "path", component_path_value
);
1896 if (value_status
!= BT_VALUE_STATUS_OK
) {
1897 BT_LOGE_STR("Cannot insert path parameter in query paramater map.");
1902 query_result
= bt_component_class_query(comp_cls
, "trace-info",
1904 if (!query_result
) {
1905 BT_LOGD("Component class \'%s\' does not support the \'trace-info\' query.",
1906 bt_component_class_get_name(comp_cls
));
1911 if (!bt_value_is_array(query_result
)) {
1912 BT_LOGD("Unexpected format of \'trace-info\' query result: "
1913 "component-class-name=%s",
1914 bt_component_class_get_name(comp_cls
));
1919 trace_count
= bt_value_array_size(query_result
);
1920 if (trace_count
< 0) {
1925 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
1927 uint64_t stream_idx
;
1928 int64_t stream_count
;
1930 trace_info
= bt_value_array_get(query_result
, trace_idx
);
1931 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
1933 BT_LOGD_STR("Cannot retrieve trace from query result.");
1937 intersection_range
= bt_value_map_get(trace_info
,
1938 "intersection-range-ns");
1939 if (!intersection_range
) {
1941 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
1945 intersection_begin
= bt_value_map_get(intersection_range
,
1947 if (!intersection_begin
) {
1949 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
1953 intersection_end
= bt_value_map_get(intersection_range
,
1955 if (!intersection_end
) {
1957 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
1961 value_status
= bt_value_integer_get(intersection_begin
, &begin
);
1962 if (value_status
!= BT_VALUE_STATUS_OK
) {
1964 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'begin\' field from query result.");
1968 value_status
= bt_value_integer_get(intersection_end
, &end
);
1969 if (value_status
!= BT_VALUE_STATUS_OK
) {
1971 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'end\' field from query result.");
1975 if (begin
< 0 || end
< 0 || end
< begin
) {
1976 BT_LOGW("Invalid trace stream intersection values: "
1977 "intersection-range-ns:begin=%" PRId64
1978 ", intersection-range-ns:end=%" PRId64
,
1984 stream_infos
= bt_value_map_get(trace_info
, "streams");
1985 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
1987 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
1991 stream_count
= bt_value_array_size(stream_infos
);
1992 if (stream_count
< 0) {
2000 * The first path of a stream's "paths" is currently used to
2001 * associate streams/ports to a given trace intersection.
2003 * This is a fragile hack as it relies on the port names
2004 * being set to the various streams path.
2006 * A stream name should be introduced as part of the trace-info
2009 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2010 const char *stream_path
;
2013 port_id
= g_new0(struct port_id
, 1);
2016 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2019 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2020 if (!port_id
->instance_name
) {
2022 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2026 trace_range
= g_new0(struct trace_range
, 1);
2029 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2032 trace_range
->intersection_range_begin_ns
= begin
;
2033 trace_range
->intersection_range_end_ns
= end
;
2035 stream_info
= bt_value_array_get(stream_infos
,
2037 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2039 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2043 stream_paths
= bt_value_map_get(stream_info
, "paths");
2044 if (!stream_paths
|| !bt_value_is_array(stream_paths
)) {
2046 BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
2050 stream_path_value
= bt_value_array_get(stream_paths
, 0);
2051 if (!stream_path_value
||
2052 !bt_value_is_string(stream_path_value
)) {
2054 BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
2058 value_status
= bt_value_string_get(stream_path_value
,
2060 if (value_status
!= BT_VALUE_STATUS_OK
) {
2065 port_id
->port_name
= strdup(stream_path
);
2066 if (!port_id
->port_name
) {
2068 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2072 BT_LOGD("Inserting stream intersection ");
2074 hash_ret
= g_hash_table_insert(ctx
->intersections
,
2075 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 setenv(*env_var_name
, "I", 1);
2578 } else if (cfg
->debug
) {
2579 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 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;