4 * Babeltrace Trace Converter
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #define BT_LOG_TAG "CLI"
32 #include <babeltrace/babeltrace.h>
33 #include <babeltrace/plugin/plugin.h>
34 #include <babeltrace/common-internal.h>
35 #include <babeltrace/graph/component.h>
36 #include <babeltrace/graph/component-source.h>
37 #include <babeltrace/graph/component-sink.h>
38 #include <babeltrace/graph/component-filter.h>
39 #include <babeltrace/graph/component-class.h>
40 #include <babeltrace/graph/port.h>
41 #include <babeltrace/graph/graph.h>
42 #include <babeltrace/graph/connection.h>
43 #include <babeltrace/graph/notification-iterator.h>
44 #include <babeltrace/ref.h>
45 #include <babeltrace/values.h>
46 #include <babeltrace/logging.h>
56 #include "babeltrace-cfg.h"
57 #include "babeltrace-cfg-cli-args.h"
58 #include "babeltrace-cfg-cli-args-default.h"
60 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
61 #define ENV_BABELTRACE_CLI_LOG_LEVEL "BABELTRACE_CLI_LOG_LEVEL"
62 #define NSEC_PER_SEC 1000000000LL
65 * Known environment variable names for the log levels of the project's
68 static const char* log_level_env_var_names
[] = {
69 "BABELTRACE_COMMON_LOG_LEVEL",
70 "BABELTRACE_PLUGIN_CTF_BTR_LOG_LEVEL",
71 "BABELTRACE_PLUGIN_CTF_FS_SRC_LOG_LEVEL",
72 "BABELTRACE_PLUGIN_CTF_LTTNG_LIVE_SRC_LOG_LEVEL",
73 "BABELTRACE_PLUGIN_CTF_METADATA_LOG_LEVEL",
74 "BABELTRACE_PLUGIN_CTF_NOTIF_ITER_LOG_LEVEL",
75 "BABELTRACE_PLUGIN_LTTNG_UTILS_DEBUG_INFO_FLT_LOG_LEVEL",
76 "BABELTRACE_PLUGIN_TEXT_DMESG_SRC_LOG_LEVEL",
77 "BABELTRACE_PLUGIN_UTILS_MUXER_FLT_LOG_LEVEL",
78 "BABELTRACE_PLUGIN_UTILS_TRIMMER_FLT_LOG_LEVEL",
79 "BABELTRACE_PLUGIN_CTFCOPYTRACE_LIB_LOG_LEVEL",
80 "BABELTRACE_PLUGIN_CTF_FS_SINK_LOG_LEVEL",
81 "BABELTRACE_PYTHON_PLUGIN_PROVIDER_LOG_LEVEL",
85 /* Application's processing graph (weak) */
86 static struct bt_graph
*the_graph
;
87 static bool canceled
= false;
89 GPtrArray
*loaded_plugins
;
92 void sigint_handler(int signum
)
94 if (signum
!= SIGINT
) {
99 bt_graph_cancel(the_graph
);
106 void init_static_data(void)
108 loaded_plugins
= g_ptr_array_new_with_free_func(bt_put
);
112 void fini_static_data(void)
114 g_ptr_array_free(loaded_plugins
, TRUE
);
118 struct bt_plugin
*find_plugin(const char *name
)
121 struct bt_plugin
*plugin
= NULL
;
124 BT_LOGD("Finding plugin: name=\"%s\"", name
);
126 for (i
= 0; i
< loaded_plugins
->len
; i
++) {
127 plugin
= g_ptr_array_index(loaded_plugins
, i
);
129 if (strcmp(name
, bt_plugin_get_name(plugin
)) == 0) {
136 if (BT_LOG_ON_DEBUG
) {
138 BT_LOGD("Found plugin: plugin-addr=%p", plugin
);
140 BT_LOGD("Cannot find plugin.");
144 return bt_get(plugin
);
148 struct bt_component_class
*find_component_class(const char *plugin_name
,
149 const char *comp_class_name
,
150 enum bt_component_class_type comp_class_type
)
152 struct bt_component_class
*comp_class
= NULL
;
153 struct bt_plugin
*plugin
;
155 BT_LOGD("Finding component class: plugin-name=\"%s\", "
156 "comp-cls-name=\"%s\", comp-cls-type=%d",
157 plugin_name
, comp_class_name
, comp_class_type
);
159 plugin
= find_plugin(plugin_name
);
165 comp_class
= bt_plugin_get_component_class_by_name_and_type(plugin
,
166 comp_class_name
, comp_class_type
);
170 if (BT_LOG_ON_DEBUG
) {
172 BT_LOGD("Found component class: comp-cls-addr=%p",
175 BT_LOGD("Cannot find component class.");
183 void print_indent(FILE *fp
, size_t indent
)
187 for (i
= 0; i
< indent
; i
++) {
193 const char *component_type_str(enum bt_component_class_type type
)
196 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
198 case BT_COMPONENT_CLASS_TYPE_SINK
:
200 case BT_COMPONENT_CLASS_TYPE_FILTER
:
202 case BT_COMPONENT_CLASS_TYPE_UNKNOWN
:
209 void print_plugin_comp_cls_opt(FILE *fh
, const char *plugin_name
,
210 const char *comp_cls_name
, enum bt_component_class_type type
)
212 GString
*shell_plugin_name
= NULL
;
213 GString
*shell_comp_cls_name
= NULL
;
215 shell_plugin_name
= bt_common_shell_quote(plugin_name
, false);
216 if (!shell_plugin_name
) {
220 shell_comp_cls_name
= bt_common_shell_quote(comp_cls_name
, false);
221 if (!shell_comp_cls_name
) {
225 fprintf(fh
, "'%s%s%s%s.%s%s%s.%s%s%s'",
226 bt_common_color_bold(),
227 bt_common_color_fg_cyan(),
228 component_type_str(type
),
229 bt_common_color_fg_default(),
230 bt_common_color_fg_blue(),
231 shell_plugin_name
->str
,
232 bt_common_color_fg_default(),
233 bt_common_color_fg_yellow(),
234 shell_comp_cls_name
->str
,
235 bt_common_color_reset());
238 if (shell_plugin_name
) {
239 g_string_free(shell_plugin_name
, TRUE
);
242 if (shell_comp_cls_name
) {
243 g_string_free(shell_comp_cls_name
, TRUE
);
248 void print_value(FILE *, struct bt_value
*, size_t);
251 void print_value_rec(FILE *, struct bt_value
*, size_t);
253 struct print_map_value_data
{
259 bt_bool
print_map_value(const char *key
, struct bt_value
*object
, void *data
)
261 struct print_map_value_data
*print_map_value_data
= data
;
263 print_indent(print_map_value_data
->fp
, print_map_value_data
->indent
);
264 fprintf(print_map_value_data
->fp
, "%s: ", key
);
266 if (bt_value_is_array(object
) &&
267 bt_value_array_is_empty(object
)) {
268 fprintf(print_map_value_data
->fp
, "[ ]\n");
272 if (bt_value_is_map(object
) &&
273 bt_value_map_is_empty(object
)) {
274 fprintf(print_map_value_data
->fp
, "{ }\n");
278 if (bt_value_is_array(object
) ||
279 bt_value_is_map(object
)) {
280 fprintf(print_map_value_data
->fp
, "\n");
283 print_value_rec(print_map_value_data
->fp
, object
,
284 print_map_value_data
->indent
+ 2);
289 void print_value_rec(FILE *fp
, struct bt_value
*value
, size_t indent
)
302 switch (bt_value_get_type(value
)) {
303 case BT_VALUE_TYPE_NULL
:
304 fprintf(fp
, "%snull%s\n", bt_common_color_bold(),
305 bt_common_color_reset());
307 case BT_VALUE_TYPE_BOOL
:
308 bt_value_bool_get(value
, &bool_val
);
309 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
310 bt_common_color_fg_cyan(), bool_val
? "yes" : "no",
311 bt_common_color_reset());
313 case BT_VALUE_TYPE_INTEGER
:
314 bt_value_integer_get(value
, &int_val
);
315 fprintf(fp
, "%s%s%" PRId64
"%s\n", bt_common_color_bold(),
316 bt_common_color_fg_red(), int_val
,
317 bt_common_color_reset());
319 case BT_VALUE_TYPE_FLOAT
:
320 bt_value_float_get(value
, &dbl_val
);
321 fprintf(fp
, "%s%s%lf%s\n", bt_common_color_bold(),
322 bt_common_color_fg_red(), dbl_val
,
323 bt_common_color_reset());
325 case BT_VALUE_TYPE_STRING
:
326 bt_value_string_get(value
, &str_val
);
327 fprintf(fp
, "%s%s%s%s\n", bt_common_color_bold(),
328 bt_common_color_fg_green(), str_val
,
329 bt_common_color_reset());
331 case BT_VALUE_TYPE_ARRAY
:
332 size
= bt_value_array_size(value
);
336 print_indent(fp
, indent
);
337 fprintf(fp
, "[ ]\n");
341 for (i
= 0; i
< size
; i
++) {
342 struct bt_value
*element
=
343 bt_value_array_get(value
, i
);
346 print_indent(fp
, indent
);
349 if (bt_value_is_array(element
) &&
350 bt_value_array_is_empty(element
)) {
351 fprintf(fp
, "[ ]\n");
355 if (bt_value_is_map(element
) &&
356 bt_value_map_is_empty(element
)) {
357 fprintf(fp
, "{ }\n");
361 if (bt_value_is_array(element
) ||
362 bt_value_is_map(element
)) {
366 print_value_rec(fp
, element
, indent
+ 2);
370 case BT_VALUE_TYPE_MAP
:
372 struct print_map_value_data data
= {
377 if (bt_value_map_is_empty(value
)) {
378 print_indent(fp
, indent
);
379 fprintf(fp
, "{ }\n");
383 bt_value_map_foreach(value
, print_map_value
, &data
);
392 void print_value(FILE *fp
, struct bt_value
*value
, size_t indent
)
394 if (!bt_value_is_array(value
) && !bt_value_is_map(value
)) {
395 print_indent(fp
, indent
);
398 print_value_rec(fp
, value
, indent
);
402 void print_bt_config_component(struct bt_config_component
*bt_config_component
)
404 fprintf(stderr
, " ");
405 print_plugin_comp_cls_opt(stderr
, bt_config_component
->plugin_name
->str
,
406 bt_config_component
->comp_cls_name
->str
,
407 bt_config_component
->type
);
408 fprintf(stderr
, ":\n");
410 if (bt_config_component
->instance_name
->len
> 0) {
411 fprintf(stderr
, " Name: %s\n",
412 bt_config_component
->instance_name
->str
);
415 fprintf(stderr
, " Parameters:\n");
416 print_value(stderr
, bt_config_component
->params
, 8);
420 void print_bt_config_components(GPtrArray
*array
)
424 for (i
= 0; i
< array
->len
; i
++) {
425 struct bt_config_component
*cfg_component
=
426 bt_config_get_component(array
, i
);
427 print_bt_config_component(cfg_component
);
428 BT_PUT(cfg_component
);
433 void print_plugin_paths(struct bt_value
*plugin_paths
)
435 fprintf(stderr
, " Plugin paths:\n");
436 print_value(stderr
, plugin_paths
, 4);
440 void print_cfg_run(struct bt_config
*cfg
)
444 print_plugin_paths(cfg
->plugin_paths
);
445 fprintf(stderr
, " Source component instances:\n");
446 print_bt_config_components(cfg
->cmd_data
.run
.sources
);
448 if (cfg
->cmd_data
.run
.filters
->len
> 0) {
449 fprintf(stderr
, " Filter component instances:\n");
450 print_bt_config_components(cfg
->cmd_data
.run
.filters
);
453 fprintf(stderr
, " Sink component instances:\n");
454 print_bt_config_components(cfg
->cmd_data
.run
.sinks
);
455 fprintf(stderr
, " Connections:\n");
457 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
458 struct bt_config_connection
*cfg_connection
=
459 g_ptr_array_index(cfg
->cmd_data
.run
.connections
,
462 fprintf(stderr
, " %s%s%s -> %s%s%s\n",
463 cfg_connection
->upstream_comp_name
->str
,
464 cfg_connection
->upstream_port_glob
->len
> 0 ? "." : "",
465 cfg_connection
->upstream_port_glob
->str
,
466 cfg_connection
->downstream_comp_name
->str
,
467 cfg_connection
->downstream_port_glob
->len
> 0 ? "." : "",
468 cfg_connection
->downstream_port_glob
->str
);
473 void print_cfg_list_plugins(struct bt_config
*cfg
)
475 print_plugin_paths(cfg
->plugin_paths
);
479 void print_cfg_help(struct bt_config
*cfg
)
481 print_plugin_paths(cfg
->plugin_paths
);
485 void print_cfg_print_ctf_metadata(struct bt_config
*cfg
)
487 print_plugin_paths(cfg
->plugin_paths
);
488 fprintf(stderr
, " Path: %s\n",
489 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
493 void print_cfg_print_lttng_live_sessions(struct bt_config
*cfg
)
495 print_plugin_paths(cfg
->plugin_paths
);
496 fprintf(stderr
, " URL: %s\n",
497 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
501 void print_cfg_query(struct bt_config
*cfg
)
503 print_plugin_paths(cfg
->plugin_paths
);
504 fprintf(stderr
, " Object: `%s`\n", cfg
->cmd_data
.query
.object
->str
);
505 fprintf(stderr
, " Component class:\n");
506 print_bt_config_component(cfg
->cmd_data
.query
.cfg_component
);
510 void print_cfg(struct bt_config
*cfg
)
512 if (!BT_LOG_ON_INFO
) {
516 BT_LOGI_STR("Configuration:");
517 fprintf(stderr
, " Debug mode: %s\n", cfg
->debug
? "yes" : "no");
518 fprintf(stderr
, " Verbose mode: %s\n", cfg
->verbose
? "yes" : "no");
520 switch (cfg
->command
) {
521 case BT_CONFIG_COMMAND_RUN
:
524 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
525 print_cfg_list_plugins(cfg
);
527 case BT_CONFIG_COMMAND_HELP
:
530 case BT_CONFIG_COMMAND_QUERY
:
531 print_cfg_query(cfg
);
533 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
534 print_cfg_print_ctf_metadata(cfg
);
536 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
537 print_cfg_print_lttng_live_sessions(cfg
);
545 void add_to_loaded_plugins(struct bt_plugin_set
*plugin_set
)
550 count
= bt_plugin_set_get_plugin_count(plugin_set
);
553 for (i
= 0; i
< count
; i
++) {
554 struct bt_plugin
*plugin
=
555 bt_plugin_set_get_plugin(plugin_set
, i
);
556 struct bt_plugin
*loaded_plugin
=
557 find_plugin(bt_plugin_get_name(plugin
));
562 BT_LOGI("Not using plugin: another one already exists with the same name: "
563 "plugin-name=\"%s\", plugin-path=\"%s\", "
564 "existing-plugin-path=\"%s\"",
565 bt_plugin_get_name(plugin
),
566 bt_plugin_get_path(plugin
),
567 bt_plugin_get_path(loaded_plugin
));
568 bt_put(loaded_plugin
);
570 /* Add to global array. */
571 BT_LOGD("Adding plugin to loaded plugins: plugin-path=\"%s\"",
572 bt_plugin_get_name(plugin
));
573 g_ptr_array_add(loaded_plugins
, bt_get(plugin
));
581 int load_dynamic_plugins(struct bt_value
*plugin_paths
)
583 int nr_paths
, i
, ret
= 0;
585 nr_paths
= bt_value_array_size(plugin_paths
);
587 BT_LOGE_STR("Cannot load dynamic plugins: no plugin path.");
592 BT_LOGI("Loading dynamic plugins.");
594 for (i
= 0; i
< nr_paths
; i
++) {
595 struct bt_value
*plugin_path_value
= NULL
;
596 const char *plugin_path
;
597 struct bt_plugin_set
*plugin_set
;
598 enum bt_value_status status
;
600 plugin_path_value
= bt_value_array_get(plugin_paths
, i
);
601 status
= bt_value_string_get(plugin_path_value
, &plugin_path
);
602 if (status
!= BT_VALUE_STATUS_OK
) {
603 BT_LOGD_STR("Cannot get plugin path string.");
604 BT_PUT(plugin_path_value
);
609 * Skip this if the directory does not exist because
610 * bt_plugin_create_all_from_dir() expects an existing
613 if (!g_file_test(plugin_path
, G_FILE_TEST_IS_DIR
)) {
614 BT_LOGV("Skipping nonexistent directory path: "
615 "path=\"%s\"", plugin_path
);
616 BT_PUT(plugin_path_value
);
620 plugin_set
= bt_plugin_create_all_from_dir(plugin_path
, false);
622 BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
624 BT_PUT(plugin_path_value
);
628 add_to_loaded_plugins(plugin_set
);
630 BT_PUT(plugin_path_value
);
637 int load_static_plugins(void)
640 struct bt_plugin_set
*plugin_set
;
642 BT_LOGI("Loading static plugins.");
643 plugin_set
= bt_plugin_create_all_from_static();
645 BT_LOGE("Unable to load static plugins.");
650 add_to_loaded_plugins(plugin_set
);
657 int load_all_plugins(struct bt_value
*plugin_paths
)
661 if (load_dynamic_plugins(plugin_paths
)) {
666 if (load_static_plugins()) {
671 BT_LOGI("Loaded all plugins: count=%u", loaded_plugins
->len
);
678 void print_plugin_info(struct bt_plugin
*plugin
)
680 unsigned int major
, minor
, patch
;
682 enum bt_plugin_status version_status
;
683 const char *plugin_name
;
687 const char *plugin_description
;
689 plugin_name
= bt_plugin_get_name(plugin
);
690 path
= bt_plugin_get_path(plugin
);
691 author
= bt_plugin_get_author(plugin
);
692 license
= bt_plugin_get_license(plugin
);
693 plugin_description
= bt_plugin_get_description(plugin
);
694 version_status
= bt_plugin_get_version(plugin
, &major
, &minor
,
696 printf("%s%s%s%s:\n", bt_common_color_bold(),
697 bt_common_color_fg_blue(), plugin_name
,
698 bt_common_color_reset());
699 printf(" %sPath%s: %s\n", bt_common_color_bold(),
700 bt_common_color_reset(), path
? path
: "(None)");
702 if (version_status
== BT_PLUGIN_STATUS_OK
) {
703 printf(" %sVersion%s: %u.%u.%u",
704 bt_common_color_bold(), bt_common_color_reset(),
705 major
, minor
, patch
);
714 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
715 bt_common_color_reset(),
716 plugin_description
? plugin_description
: "(None)");
717 printf(" %sAuthor%s: %s\n", bt_common_color_bold(),
718 bt_common_color_reset(), author
? author
: "(Unknown)");
719 printf(" %sLicense%s: %s\n", bt_common_color_bold(),
720 bt_common_color_reset(),
721 license
? license
: "(Unknown)");
725 int cmd_query(struct bt_config
*cfg
)
728 struct bt_component_class
*comp_cls
= NULL
;
729 struct bt_value
*results
= NULL
;
731 comp_cls
= find_component_class(cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
732 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
733 cfg
->cmd_data
.query
.cfg_component
->type
);
735 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
736 "comp-cls-name=\"%s\", comp-cls-type=%d",
737 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
738 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
739 cfg
->cmd_data
.query
.cfg_component
->type
);
740 fprintf(stderr
, "%s%sCannot find component class %s",
741 bt_common_color_bold(),
742 bt_common_color_fg_red(),
743 bt_common_color_reset());
744 print_plugin_comp_cls_opt(stderr
,
745 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
746 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
747 cfg
->cmd_data
.query
.cfg_component
->type
);
748 fprintf(stderr
, "\n");
753 results
= bt_component_class_query(comp_cls
,
754 cfg
->cmd_data
.query
.object
->str
,
755 cfg
->cmd_data
.query
.cfg_component
->params
);
757 BT_LOGE("Failed to query component class: plugin-name=\"%s\", "
758 "comp-cls-name=\"%s\", comp-cls-type=%d "
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 cfg
->cmd_data
.query
.object
->str
);
764 fprintf(stderr
, "%s%sFailed to query info to %s",
765 bt_common_color_bold(),
766 bt_common_color_fg_red(),
767 bt_common_color_reset());
768 print_plugin_comp_cls_opt(stderr
,
769 cfg
->cmd_data
.query
.cfg_component
->plugin_name
->str
,
770 cfg
->cmd_data
.query
.cfg_component
->comp_cls_name
->str
,
771 cfg
->cmd_data
.query
.cfg_component
->type
);
772 fprintf(stderr
, "%s%s with object `%s`%s\n",
773 bt_common_color_bold(),
774 bt_common_color_fg_red(),
775 cfg
->cmd_data
.query
.object
->str
,
776 bt_common_color_reset());
781 print_value(stdout
, results
, 0);
790 int cmd_help(struct bt_config
*cfg
)
793 struct bt_plugin
*plugin
= NULL
;
796 plugin
= find_plugin(cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
798 BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
799 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
);
800 fprintf(stderr
, "%s%sCannot find plugin %s%s%s\n",
801 bt_common_color_bold(), bt_common_color_fg_red(),
802 bt_common_color_fg_blue(),
803 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
804 bt_common_color_reset());
809 print_plugin_info(plugin
);
810 printf(" %sComponent classes%s: %d\n",
811 bt_common_color_bold(),
812 bt_common_color_reset(),
813 (int) bt_plugin_get_component_class_count(plugin
));
816 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
817 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
818 struct bt_component_class
*needed_comp_cls
=
819 find_component_class(
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
);
824 if (!needed_comp_cls
) {
825 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
826 "comp-cls-name=\"%s\", comp-cls-type=%d",
827 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
828 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
829 cfg
->cmd_data
.help
.cfg_component
->type
);
830 fprintf(stderr
, "\n%s%sCannot find component class %s",
831 bt_common_color_bold(),
832 bt_common_color_fg_red(),
833 bt_common_color_reset());
834 print_plugin_comp_cls_opt(stderr
,
835 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
836 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
837 cfg
->cmd_data
.help
.cfg_component
->type
);
838 fprintf(stderr
, "\n");
843 bt_put(needed_comp_cls
);
846 for (i
= 0; i
< bt_plugin_get_component_class_count(plugin
); i
++) {
847 struct bt_component_class
*comp_cls
=
848 bt_plugin_get_component_class_by_index(plugin
, i
);
849 const char *comp_class_name
=
850 bt_component_class_get_name(comp_cls
);
851 const char *comp_class_description
=
852 bt_component_class_get_description(comp_cls
);
853 const char *comp_class_help
=
854 bt_component_class_get_help(comp_cls
);
855 enum bt_component_class_type type
=
856 bt_component_class_get_type(comp_cls
);
860 if (cfg
->cmd_data
.help
.cfg_component
->type
!=
861 BT_COMPONENT_CLASS_TYPE_UNKNOWN
) {
862 if (strcmp(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
->str
,
863 comp_class_name
) != 0 &&
865 cfg
->cmd_data
.help
.cfg_component
->type
) {
872 print_plugin_comp_cls_opt(stdout
,
873 cfg
->cmd_data
.help
.cfg_component
->plugin_name
->str
,
877 printf(" %sDescription%s: %s\n", bt_common_color_bold(),
878 bt_common_color_reset(),
879 comp_class_description
? comp_class_description
: "(None)");
881 if (comp_class_help
) {
882 printf("\n%s\n", comp_class_help
);
894 int cmd_list_plugins(struct bt_config
*cfg
)
897 int plugins_count
, component_classes_count
= 0, i
;
899 printf("From the following plugin paths:\n\n");
900 print_value(stdout
, cfg
->plugin_paths
, 2);
902 plugins_count
= loaded_plugins
->len
;
903 if (plugins_count
== 0) {
904 printf("No plugins found.\n");
908 for (i
= 0; i
< plugins_count
; i
++) {
909 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
911 component_classes_count
+= bt_plugin_get_component_class_count(plugin
);
914 printf("Found %s%d%s component classes in %s%d%s plugins.\n",
915 bt_common_color_bold(),
916 component_classes_count
,
917 bt_common_color_reset(),
918 bt_common_color_bold(),
920 bt_common_color_reset());
922 for (i
= 0; i
< plugins_count
; i
++) {
924 struct bt_plugin
*plugin
= g_ptr_array_index(loaded_plugins
, i
);
926 component_classes_count
=
927 bt_plugin_get_component_class_count(plugin
);
929 print_plugin_info(plugin
);
931 if (component_classes_count
== 0) {
932 printf(" %sComponent classes%s: (none)\n",
933 bt_common_color_bold(),
934 bt_common_color_reset());
936 printf(" %sComponent classes%s:\n",
937 bt_common_color_bold(),
938 bt_common_color_reset());
941 for (j
= 0; j
< component_classes_count
; j
++) {
942 struct bt_component_class
*comp_class
=
943 bt_plugin_get_component_class_by_index(
945 const char *comp_class_name
=
946 bt_component_class_get_name(comp_class
);
947 const char *comp_class_description
=
948 bt_component_class_get_description(comp_class
);
949 enum bt_component_class_type type
=
950 bt_component_class_get_type(comp_class
);
953 print_plugin_comp_cls_opt(stdout
,
954 bt_plugin_get_name(plugin
), comp_class_name
,
957 if (comp_class_description
) {
958 printf(": %s", comp_class_description
);
971 int cmd_print_lttng_live_sessions(struct bt_config
*cfg
)
974 struct bt_component_class
*comp_cls
= NULL
;
975 struct bt_value
*results
= NULL
;
976 struct bt_value
*params
= NULL
;
977 struct bt_value
*map
= NULL
;
978 struct bt_value
*v
= NULL
;
979 static const char * const plugin_name
= "ctf";
980 static const char * const comp_cls_name
= "lttng-live";
981 static const enum bt_component_class_type comp_cls_type
=
982 BT_COMPONENT_CLASS_TYPE_SOURCE
;
983 int64_t array_size
, i
;
985 assert(cfg
->cmd_data
.print_lttng_live_sessions
.url
);
986 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
989 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
990 "comp-cls-name=\"%s\", comp-cls-type=%d",
991 plugin_name
, comp_cls_name
,
992 BT_COMPONENT_CLASS_TYPE_SOURCE
);
993 fprintf(stderr
, "%s%sCannot find component class %s",
994 bt_common_color_bold(),
995 bt_common_color_fg_red(),
996 bt_common_color_reset());
997 print_plugin_comp_cls_opt(stderr
, plugin_name
,
998 comp_cls_name
, comp_cls_type
);
999 fprintf(stderr
, "\n");
1003 params
= bt_value_map_create();
1008 ret
= bt_value_map_insert_string(params
, "url",
1009 cfg
->cmd_data
.print_lttng_live_sessions
.url
->str
);
1014 results
= bt_component_class_query(comp_cls
, "sessions",
1017 BT_LOGE_STR("Failed to query for sessions.");
1018 fprintf(stderr
, "%s%sFailed to request sessions%s\n",
1019 bt_common_color_bold(),
1020 bt_common_color_fg_red(),
1021 bt_common_color_reset());
1025 if (!bt_value_is_array(results
)) {
1026 BT_LOGE_STR("Expecting an array for sessions query.");
1027 fprintf(stderr
, "%s%sUnexpected type returned by session query%s\n",
1028 bt_common_color_bold(),
1029 bt_common_color_fg_red(),
1030 bt_common_color_reset());
1034 array_size
= bt_value_array_size(results
);
1035 for (i
= 0; i
< array_size
; i
++) {
1036 const char *url_text
;
1037 int64_t timer_us
, streams
, clients
;
1039 map
= bt_value_array_get(results
, i
);
1041 BT_LOGE_STR("Unexpected empty array entry.");
1044 if (!bt_value_is_map(map
)) {
1045 BT_LOGE_STR("Unexpected entry type.");
1049 v
= bt_value_map_get(map
, "url");
1051 BT_LOGE_STR("Unexpected empty array \"url\" entry.");
1054 ret
= bt_value_string_get(v
, &url_text
);
1056 printf("%s", url_text
);
1059 v
= bt_value_map_get(map
, "timer-us");
1061 BT_LOGE_STR("Unexpected empty array \"timer-us\" entry.");
1064 ret
= bt_value_integer_get(v
, &timer_us
);
1066 printf(" (timer = %" PRIu64
", ", timer_us
);
1069 v
= bt_value_map_get(map
, "stream-count");
1071 BT_LOGE_STR("Unexpected empty array \"stream-count\" entry.");
1074 ret
= bt_value_integer_get(v
, &streams
);
1076 printf("%" PRIu64
" stream(s), ", streams
);
1079 v
= bt_value_map_get(map
, "client-count");
1081 BT_LOGE_STR("Unexpected empty array \"client-count\" entry.");
1084 ret
= bt_value_integer_get(v
, &clients
);
1086 printf("%" PRIu64
" client(s) connected)\n", clients
);
1105 int cmd_print_ctf_metadata(struct bt_config
*cfg
)
1108 struct bt_component_class
*comp_cls
= NULL
;
1109 struct bt_value
*results
= NULL
;
1110 struct bt_value
*params
= NULL
;
1111 struct bt_value
*metadata_text_value
= NULL
;
1112 const char *metadata_text
= NULL
;
1113 static const char * const plugin_name
= "ctf";
1114 static const char * const comp_cls_name
= "fs";
1115 static const enum bt_component_class_type comp_cls_type
=
1116 BT_COMPONENT_CLASS_TYPE_SOURCE
;
1118 assert(cfg
->cmd_data
.print_ctf_metadata
.path
);
1119 comp_cls
= find_component_class(plugin_name
, comp_cls_name
,
1122 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
1123 "comp-cls-name=\"%s\", comp-cls-type=%d",
1124 plugin_name
, comp_cls_name
,
1125 BT_COMPONENT_CLASS_TYPE_SOURCE
);
1126 fprintf(stderr
, "%s%sCannot find component class %s",
1127 bt_common_color_bold(),
1128 bt_common_color_fg_red(),
1129 bt_common_color_reset());
1130 print_plugin_comp_cls_opt(stderr
, plugin_name
,
1131 comp_cls_name
, comp_cls_type
);
1132 fprintf(stderr
, "\n");
1137 params
= bt_value_map_create();
1143 ret
= bt_value_map_insert_string(params
, "path",
1144 cfg
->cmd_data
.print_ctf_metadata
.path
->str
);
1150 results
= bt_component_class_query(comp_cls
, "metadata-info",
1154 BT_LOGE_STR("Failed to query for metadata info.");
1155 fprintf(stderr
, "%s%sFailed to request metadata info%s\n",
1156 bt_common_color_bold(),
1157 bt_common_color_fg_red(),
1158 bt_common_color_reset());
1162 metadata_text_value
= bt_value_map_get(results
, "text");
1163 if (!metadata_text_value
) {
1164 BT_LOGE_STR("Cannot find `text` string value in the resulting metadata info object.");
1169 ret
= bt_value_string_get(metadata_text_value
, &metadata_text
);
1171 printf("%s\n", metadata_text
);
1176 bt_put(metadata_text_value
);
1182 char *instance_name
;
1186 struct trace_range
{
1187 uint64_t intersection_range_begin_ns
;
1188 uint64_t intersection_range_end_ns
;
1192 guint
port_id_hash(gconstpointer v
)
1194 const struct port_id
*id
= v
;
1196 assert(id
->instance_name
);
1197 assert(id
->port_name
);
1199 return g_str_hash(id
->instance_name
) ^ g_str_hash(id
->port_name
);
1203 gboolean
port_id_equal(gconstpointer v1
, gconstpointer v2
)
1205 const struct port_id
*id1
= v1
;
1206 const struct port_id
*id2
= v2
;
1208 return !strcmp(id1
->instance_name
, id2
->instance_name
) &&
1209 !strcmp(id1
->port_name
, id2
->port_name
);
1213 void port_id_destroy(gpointer data
)
1215 struct port_id
*id
= data
;
1217 free(id
->instance_name
);
1218 free(id
->port_name
);
1223 void trace_range_destroy(gpointer data
)
1228 struct cmd_run_ctx
{
1230 GHashTable
*components
;
1233 struct bt_graph
*graph
;
1236 struct bt_config
*cfg
;
1240 bool stream_intersection_mode
;
1243 * Association of struct port_id -> struct trace_range.
1245 GHashTable
*intersections
;
1248 /* Returns a timestamp of the form "(-)s.ns" */
1250 char *s_from_ns(int64_t ns
)
1255 int64_t ts_sec_abs
, ts_nsec_abs
;
1256 int64_t ts_sec
= ns
/ NSEC_PER_SEC
;
1257 int64_t ts_nsec
= ns
% NSEC_PER_SEC
;
1259 if (ts_sec
>= 0 && ts_nsec
>= 0) {
1260 is_negative
= false;
1261 ts_sec_abs
= ts_sec
;
1262 ts_nsec_abs
= ts_nsec
;
1263 } else if (ts_sec
> 0 && ts_nsec
< 0) {
1264 is_negative
= false;
1265 ts_sec_abs
= ts_sec
- 1;
1266 ts_nsec_abs
= NSEC_PER_SEC
+ ts_nsec
;
1267 } else if (ts_sec
== 0 && ts_nsec
< 0) {
1269 ts_sec_abs
= ts_sec
;
1270 ts_nsec_abs
= -ts_nsec
;
1271 } else if (ts_sec
< 0 && ts_nsec
> 0) {
1273 ts_sec_abs
= -(ts_sec
+ 1);
1274 ts_nsec_abs
= NSEC_PER_SEC
- ts_nsec
;
1275 } else if (ts_sec
< 0 && ts_nsec
== 0) {
1277 ts_sec_abs
= -ts_sec
;
1278 ts_nsec_abs
= ts_nsec
;
1279 } else { /* (ts_sec < 0 && ts_nsec < 0) */
1281 ts_sec_abs
= -ts_sec
;
1282 ts_nsec_abs
= -ts_nsec
;
1285 ret
= asprintf(&s_ret
, "%s%" PRId64
".%09" PRId64
,
1286 is_negative
? "-" : "", ts_sec_abs
, ts_nsec_abs
);
1294 int cmd_run_ctx_connect_upstream_port_to_downstream_component(
1295 struct cmd_run_ctx
*ctx
, struct bt_component
*upstream_comp
,
1296 struct bt_port
*upstream_port
,
1297 struct bt_config_connection
*cfg_conn
)
1300 GQuark downstreamp_comp_name_quark
;
1301 struct bt_component
*downstream_comp
;
1302 int64_t downstream_port_count
;
1304 int64_t (*port_count_fn
)(struct bt_component
*);
1305 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
1306 enum bt_graph_status status
= BT_GRAPH_STATUS_ERROR
;
1307 bool insert_trimmer
= false;
1308 struct bt_value
*trimmer_params
= NULL
;
1309 char *intersection_begin
= NULL
;
1310 char *intersection_end
= NULL
;
1311 struct bt_component
*trimmer
= NULL
;
1312 struct bt_component_class
*trimmer_class
= NULL
;
1313 struct bt_port
*trimmer_input
= NULL
;
1314 struct bt_port
*trimmer_output
= NULL
;
1316 if (ctx
->intersections
&&
1317 bt_component_get_class_type(upstream_comp
) ==
1318 BT_COMPONENT_CLASS_TYPE_SOURCE
) {
1319 struct trace_range
*range
;
1320 struct port_id port_id
= {
1321 .instance_name
= (char *) bt_component_get_name(upstream_comp
),
1322 .port_name
= (char *) bt_port_get_name(upstream_port
)
1325 if (!port_id
.instance_name
|| !port_id
.port_name
) {
1329 range
= (struct trace_range
*) g_hash_table_lookup(
1330 ctx
->intersections
, &port_id
);
1332 enum bt_value_status status
;
1334 intersection_begin
= s_from_ns(
1335 range
->intersection_range_begin_ns
);
1336 intersection_end
= s_from_ns(
1337 range
->intersection_range_end_ns
);
1338 if (!intersection_begin
|| !intersection_end
) {
1339 BT_LOGE_STR("Cannot create trimmer argument timestamp string.");
1343 insert_trimmer
= true;
1344 trimmer_params
= bt_value_map_create();
1345 if (!trimmer_params
) {
1349 status
= bt_value_map_insert_string(trimmer_params
,
1350 "begin", intersection_begin
);
1351 if (status
!= BT_VALUE_STATUS_OK
) {
1354 status
= bt_value_map_insert_string(trimmer_params
,
1355 "end", intersection_end
);
1356 if (status
!= BT_VALUE_STATUS_OK
) {
1361 trimmer_class
= find_component_class("utils", "trimmer",
1362 BT_COMPONENT_CLASS_TYPE_FILTER
);
1363 if (!trimmer_class
) {
1368 BT_LOGI("Connecting upstream port to the next available downstream port: "
1369 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1370 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1371 upstream_port
, bt_port_get_name(upstream_port
),
1372 cfg_conn
->downstream_comp_name
->str
,
1373 cfg_conn
->arg
->str
);
1374 downstreamp_comp_name_quark
= g_quark_from_string(
1375 cfg_conn
->downstream_comp_name
->str
);
1376 assert(downstreamp_comp_name_quark
> 0);
1377 downstream_comp
= g_hash_table_lookup(ctx
->components
,
1378 GUINT_TO_POINTER(downstreamp_comp_name_quark
));
1379 if (!downstream_comp
) {
1380 BT_LOGE("Cannot find downstream component: comp-name=\"%s\", "
1381 "conn-arg=\"%s\"", cfg_conn
->downstream_comp_name
->str
,
1382 cfg_conn
->arg
->str
);
1383 fprintf(stderr
, "Cannot create connection: cannot find downstream component: %s\n",
1384 cfg_conn
->arg
->str
);
1388 if (bt_component_is_filter(downstream_comp
)) {
1389 port_count_fn
= bt_component_filter_get_input_port_count
;
1390 port_by_index_fn
= bt_component_filter_get_input_port_by_index
;
1391 } else if (bt_component_is_sink(downstream_comp
)) {
1392 port_count_fn
= bt_component_sink_get_input_port_count
;
1393 port_by_index_fn
= bt_component_sink_get_input_port_by_index
;
1396 * Should never happen because the connections are
1397 * validated before we get here.
1399 BT_LOGF("Invalid connection: downstream component is a source: "
1400 "conn-arg=\"%s\"", cfg_conn
->arg
->str
);
1404 downstream_port_count
= port_count_fn(downstream_comp
);
1405 assert(downstream_port_count
>= 0);
1407 for (i
= 0; i
< downstream_port_count
; i
++) {
1408 struct bt_port
*downstream_port
=
1409 port_by_index_fn(downstream_comp
, i
);
1410 const char *upstream_port_name
;
1411 const char *downstream_port_name
;
1413 assert(downstream_port
);
1415 /* Skip port if it's already connected. */
1416 if (bt_port_is_connected(downstream_port
)) {
1417 bt_put(downstream_port
);
1418 BT_LOGD("Skipping downstream port: already connected: "
1419 "port-addr=%p, port-name=\"%s\"",
1421 bt_port_get_name(downstream_port
));
1425 downstream_port_name
= bt_port_get_name(downstream_port
);
1426 assert(downstream_port_name
);
1427 upstream_port_name
= bt_port_get_name(upstream_port
);
1428 assert(upstream_port_name
);
1430 if (!bt_common_star_glob_match(
1431 cfg_conn
->downstream_port_glob
->str
, SIZE_MAX
,
1432 downstream_port_name
, SIZE_MAX
)) {
1433 bt_put(downstream_port
);
1437 if (insert_trimmer
) {
1439 * In order to insert the trimmer between the two
1440 * components that were being connected, we create
1441 * a connection configuration entry which describes
1442 * a connection from the trimmer's output to the
1443 * original input that was being connected.
1445 * Hence, the creation of the trimmer will cause the
1446 * graph "new port" listener to establish all downstream
1447 * connections as its output port is connected. We will
1448 * then establish the connection between the original
1449 * upstream source and the trimmer.
1451 char *trimmer_name
= NULL
;
1452 enum bt_graph_status graph_status
;
1454 ret
= asprintf(&trimmer_name
, "%s-%s",
1455 "stream-intersection-trimmer",
1456 upstream_port_name
);
1462 ctx
->connect_ports
= false;
1463 graph_status
= bt_graph_add_component(ctx
->graph
,
1464 trimmer_class
, trimmer_name
, trimmer_params
,
1467 if (graph_status
!= BT_GRAPH_STATUS_OK
) {
1473 bt_component_filter_get_input_port_by_index(
1475 if (!trimmer_input
) {
1479 bt_component_filter_get_output_port_by_index(
1481 if (!trimmer_output
) {
1486 * Replace the current downstream port by the trimmer's
1489 BT_MOVE(downstream_port
, trimmer_input
);
1490 downstream_port_name
= bt_port_get_name(
1492 if (!downstream_port_name
) {
1497 /* We have a winner! */
1498 status
= bt_graph_connect_ports(ctx
->graph
,
1499 upstream_port
, downstream_port
, NULL
);
1500 BT_PUT(downstream_port
);
1502 case BT_GRAPH_STATUS_OK
:
1504 case BT_GRAPH_STATUS_CANCELED
:
1505 BT_LOGI_STR("Graph was canceled by user.");
1506 status
= BT_GRAPH_STATUS_OK
;
1508 case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION
:
1509 BT_LOGE("A component refused a connection to one of its ports: "
1510 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1511 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1512 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1513 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1515 upstream_comp
, bt_component_get_name(upstream_comp
),
1516 upstream_port
, bt_port_get_name(upstream_port
),
1517 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1518 downstream_port
, downstream_port_name
,
1519 cfg_conn
->arg
->str
);
1521 "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
1522 bt_port_get_name(upstream_port
),
1523 downstream_port_name
,
1524 cfg_conn
->arg
->str
);
1527 BT_LOGE("Cannot create connection: graph refuses to connect ports: "
1528 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1529 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1530 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1531 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1533 upstream_comp
, bt_component_get_name(upstream_comp
),
1534 upstream_port
, bt_port_get_name(upstream_port
),
1535 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1536 downstream_port
, downstream_port_name
,
1537 cfg_conn
->arg
->str
);
1539 "Cannot create connection: graph refuses to connect ports (`%s` to `%s`): %s\n",
1540 bt_port_get_name(upstream_port
),
1541 downstream_port_name
,
1542 cfg_conn
->arg
->str
);
1546 BT_LOGI("Connected component ports: "
1547 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1548 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1549 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1550 "downstream-port-addr=%p, downstream-port-name=\"%s\", "
1552 upstream_comp
, bt_component_get_name(upstream_comp
),
1553 upstream_port
, bt_port_get_name(upstream_port
),
1554 downstream_comp
, cfg_conn
->downstream_comp_name
->str
,
1555 downstream_port
, downstream_port_name
,
1556 cfg_conn
->arg
->str
);
1558 if (insert_trimmer
) {
1560 * The first connection, from the source to the trimmer,
1561 * has been done. We now connect the trimmer to the
1562 * original downstream port.
1564 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1565 ctx
, trimmer
, trimmer_output
, cfg_conn
);
1569 ctx
->connect_ports
= true;
1574 if (status
!= BT_GRAPH_STATUS_OK
) {
1575 BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
1576 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1577 "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
1578 upstream_port
, bt_port_get_name(upstream_port
),
1579 cfg_conn
->downstream_comp_name
->str
,
1580 cfg_conn
->arg
->str
);
1582 "Cannot create connection: cannot find a matching downstream port for upstream port `%s`: %s\n",
1583 bt_port_get_name(upstream_port
), cfg_conn
->arg
->str
);
1593 free(intersection_begin
);
1594 free(intersection_end
);
1595 BT_PUT(trimmer_params
);
1596 BT_PUT(trimmer_class
);
1598 BT_PUT(trimmer_input
);
1599 BT_PUT(trimmer_output
);
1604 int cmd_run_ctx_connect_upstream_port(struct cmd_run_ctx
*ctx
,
1605 struct bt_port
*upstream_port
)
1608 const char *upstream_port_name
;
1609 const char *upstream_comp_name
;
1610 struct bt_component
*upstream_comp
= NULL
;
1614 assert(upstream_port
);
1615 upstream_port_name
= bt_port_get_name(upstream_port
);
1616 assert(upstream_port_name
);
1617 upstream_comp
= bt_port_get_component(upstream_port
);
1618 if (!upstream_comp
) {
1619 BT_LOGW("Upstream port to connect is not part of a component: "
1620 "port-addr=%p, port-name=\"%s\"",
1621 upstream_port
, upstream_port_name
);
1626 upstream_comp_name
= bt_component_get_name(upstream_comp
);
1627 assert(upstream_comp_name
);
1628 BT_LOGI("Connecting upstream port: comp-addr=%p, comp-name=\"%s\", "
1629 "port-addr=%p, port-name=\"%s\"",
1630 upstream_comp
, upstream_comp_name
,
1631 upstream_port
, upstream_port_name
);
1633 for (i
= 0; i
< ctx
->cfg
->cmd_data
.run
.connections
->len
; i
++) {
1634 struct bt_config_connection
*cfg_conn
=
1636 ctx
->cfg
->cmd_data
.run
.connections
, i
);
1638 if (strcmp(cfg_conn
->upstream_comp_name
->str
,
1639 upstream_comp_name
)) {
1643 if (!bt_common_star_glob_match(
1644 cfg_conn
->upstream_port_glob
->str
,
1645 SIZE_MAX
, upstream_port_name
, SIZE_MAX
)) {
1649 ret
= cmd_run_ctx_connect_upstream_port_to_downstream_component(
1650 ctx
, upstream_comp
, upstream_port
, cfg_conn
);
1652 BT_LOGE("Cannot connect upstream port: "
1653 "port-addr=%p, port-name=\"%s\"",
1655 upstream_port_name
);
1657 "Cannot connect port `%s` of component `%s` to a downstream port: %s\n",
1660 cfg_conn
->arg
->str
);
1666 BT_LOGE("Cannot connect upstream port: port does not match any connection argument: "
1667 "port-addr=%p, port-name=\"%s\"", upstream_port
,
1668 upstream_port_name
);
1670 "Cannot create connection: upstream port `%s` does not match any connection\n",
1671 upstream_port_name
);
1677 bt_put(upstream_comp
);
1682 void graph_port_added_listener(struct bt_port
*port
, void *data
)
1684 struct bt_component
*comp
= NULL
;
1685 struct cmd_run_ctx
*ctx
= data
;
1687 comp
= bt_port_get_component(port
);
1688 BT_LOGI("Port added to a graph's component: comp-addr=%p, "
1689 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1690 comp
, comp
? bt_component_get_name(comp
) : "",
1691 port
, bt_port_get_name(port
));
1693 if (!ctx
->connect_ports
) {
1698 BT_LOGW_STR("Port has no component.");
1702 if (bt_port_is_connected(port
)) {
1703 BT_LOGW_STR("Port is already connected.");
1707 if (!bt_port_is_output(port
)) {
1708 BT_LOGI_STR("Skipping input port.");
1712 if (cmd_run_ctx_connect_upstream_port(ctx
, port
)) {
1713 BT_LOGF_STR("Cannot connect upstream port.");
1714 fprintf(stderr
, "Added port could not be connected: aborting\n");
1724 void graph_port_removed_listener(struct bt_component
*component
,
1725 struct bt_port
*port
, void *data
)
1727 BT_LOGI("Port removed from a graph's component: comp-addr=%p, "
1728 "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
1729 component
, bt_component_get_name(component
),
1730 port
, bt_port_get_name(port
));
1734 void graph_ports_connected_listener(struct bt_port
*upstream_port
,
1735 struct bt_port
*downstream_port
, void *data
)
1737 struct bt_component
*upstream_comp
= bt_port_get_component(upstream_port
);
1738 struct bt_component
*downstream_comp
= bt_port_get_component(downstream_port
);
1740 assert(upstream_comp
);
1741 assert(downstream_comp
);
1742 BT_LOGI("Graph's component ports connected: "
1743 "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
1744 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1745 "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
1746 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1747 upstream_comp
, bt_component_get_name(upstream_comp
),
1748 upstream_port
, bt_port_get_name(upstream_port
),
1749 downstream_comp
, bt_component_get_name(downstream_comp
),
1750 downstream_port
, bt_port_get_name(downstream_port
));
1751 bt_put(upstream_comp
);
1752 bt_put(downstream_comp
);
1756 void graph_ports_disconnected_listener(
1757 struct bt_component
*upstream_component
,
1758 struct bt_component
*downstream_component
,
1759 struct bt_port
*upstream_port
, struct bt_port
*downstream_port
,
1762 BT_LOGI("Graph's component ports disconnected: "
1763 "upstream-port-addr=%p, upstream-port-name=\"%s\", "
1764 "downstream-port-addr=%p, downstream-port-name=\"%s\"",
1765 upstream_port
, bt_port_get_name(upstream_port
),
1766 downstream_port
, bt_port_get_name(downstream_port
));
1770 void cmd_run_ctx_destroy(struct cmd_run_ctx
*ctx
)
1776 if (ctx
->components
) {
1777 g_hash_table_destroy(ctx
->components
);
1778 ctx
->components
= NULL
;
1781 if (ctx
->intersections
) {
1782 g_hash_table_destroy(ctx
->intersections
);
1783 ctx
->intersections
= NULL
;
1792 int cmd_run_ctx_init(struct cmd_run_ctx
*ctx
, struct bt_config
*cfg
)
1797 ctx
->connect_ports
= false;
1798 ctx
->components
= g_hash_table_new_full(g_direct_hash
, g_direct_equal
,
1800 if (!ctx
->components
) {
1804 if (cfg
->cmd_data
.run
.stream_intersection_mode
) {
1805 ctx
->stream_intersection_mode
= true;
1806 ctx
->intersections
= g_hash_table_new_full(port_id_hash
,
1807 port_id_equal
, port_id_destroy
, trace_range_destroy
);
1808 if (!ctx
->intersections
) {
1813 ctx
->graph
= bt_graph_create();
1818 the_graph
= ctx
->graph
;
1819 ret
= bt_graph_add_port_added_listener(ctx
->graph
,
1820 graph_port_added_listener
, ctx
);
1822 BT_LOGE_STR("Cannot add \"port added\" listener to graph.");
1826 ret
= bt_graph_add_port_removed_listener(ctx
->graph
,
1827 graph_port_removed_listener
, ctx
);
1829 BT_LOGE_STR("Cannot add \"port removed\" listener to graph.");
1833 ret
= bt_graph_add_ports_connected_listener(ctx
->graph
,
1834 graph_ports_connected_listener
, ctx
);
1836 BT_LOGE_STR("Cannot add \"ports connected\" listener to graph.");
1840 ret
= bt_graph_add_ports_disconnected_listener(ctx
->graph
,
1841 graph_ports_disconnected_listener
, ctx
);
1843 BT_LOGE_STR("Cannot add \"ports disconnected\" listener to graph.");
1850 cmd_run_ctx_destroy(ctx
);
1858 int set_stream_intersections(struct cmd_run_ctx
*ctx
,
1859 struct bt_config_component
*cfg_comp
,
1860 struct bt_component_class
*comp_cls
)
1864 int64_t trace_count
;
1865 enum bt_value_status value_status
;
1866 const char *path
= NULL
;
1867 struct bt_value
*component_path_value
= NULL
;
1868 struct bt_value
*query_params
= NULL
;
1869 struct bt_value
*query_result
= NULL
;
1870 struct bt_value
*trace_info
= NULL
;
1871 struct bt_value
*intersection_range
= NULL
;
1872 struct bt_value
*intersection_begin
= NULL
;
1873 struct bt_value
*intersection_end
= NULL
;
1874 struct bt_value
*stream_path_value
= NULL
;
1875 struct bt_value
*stream_paths
= NULL
;
1876 struct bt_value
*stream_infos
= NULL
;
1877 struct bt_value
*stream_info
= NULL
;
1878 struct port_id
*port_id
= NULL
;
1879 struct trace_range
*trace_range
= NULL
;
1881 component_path_value
= bt_value_map_get(cfg_comp
->params
, "path");
1882 if (!bt_value_is_string(component_path_value
)) {
1883 BT_LOGD("Cannot get path parameter: component-name=%s",
1884 cfg_comp
->instance_name
->str
);
1889 value_status
= bt_value_string_get(component_path_value
, &path
);
1890 if (value_status
!= BT_VALUE_STATUS_OK
) {
1891 BT_LOGD("Cannot get path string value: component-name=%s",
1892 cfg_comp
->instance_name
->str
);
1897 query_params
= bt_value_map_create();
1898 if (!query_params
) {
1899 BT_LOGE_STR("Cannot create query parameters.");
1904 value_status
= bt_value_map_insert(query_params
, "path", component_path_value
);
1905 if (value_status
!= BT_VALUE_STATUS_OK
) {
1906 BT_LOGE_STR("Cannot insert path parameter in query paramater map.");
1911 query_result
= bt_component_class_query(comp_cls
, "trace-info",
1913 if (!query_result
) {
1914 BT_LOGD("Component class \'%s\' does not support the \'trace-info\' query.",
1915 bt_component_class_get_name(comp_cls
));
1920 if (!bt_value_is_array(query_result
)) {
1921 BT_LOGD("Unexpected format of \'trace-info\' query result: "
1922 "component-class-name=%s",
1923 bt_component_class_get_name(comp_cls
));
1928 trace_count
= bt_value_array_size(query_result
);
1929 if (trace_count
< 0) {
1934 for (trace_idx
= 0; trace_idx
< trace_count
; trace_idx
++) {
1936 uint64_t stream_idx
;
1937 int64_t stream_count
;
1939 trace_info
= bt_value_array_get(query_result
, trace_idx
);
1940 if (!trace_info
|| !bt_value_is_map(trace_info
)) {
1942 BT_LOGD_STR("Cannot retrieve trace from query result.");
1946 intersection_range
= bt_value_map_get(trace_info
,
1947 "intersection-range-ns");
1948 if (!intersection_range
) {
1950 BT_LOGD_STR("Cannot retrieve \'intersetion-range-ns\' field from query result.");
1954 intersection_begin
= bt_value_map_get(intersection_range
,
1956 if (!intersection_begin
) {
1958 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'begin\' field from query result.");
1962 intersection_end
= bt_value_map_get(intersection_range
,
1964 if (!intersection_end
) {
1966 BT_LOGD_STR("Cannot retrieve intersection-range-ns \'end\' field from query result.");
1970 value_status
= bt_value_integer_get(intersection_begin
, &begin
);
1971 if (value_status
!= BT_VALUE_STATUS_OK
) {
1973 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'begin\' field from query result.");
1977 value_status
= bt_value_integer_get(intersection_end
, &end
);
1978 if (value_status
!= BT_VALUE_STATUS_OK
) {
1980 BT_LOGD_STR("Cannot retrieve value of intersection-range-ns \'end\' field from query result.");
1984 if (begin
< 0 || end
< 0 || end
< begin
) {
1985 BT_LOGW("Invalid trace stream intersection values: "
1986 "intersection-range-ns:begin=%" PRId64
1987 ", intersection-range-ns:end=%" PRId64
,
1993 stream_infos
= bt_value_map_get(trace_info
, "streams");
1994 if (!stream_infos
|| !bt_value_is_array(stream_infos
)) {
1996 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2000 stream_count
= bt_value_array_size(stream_infos
);
2001 if (stream_count
< 0) {
2009 * The first path of a stream's "paths" is currently used to
2010 * associate streams/ports to a given trace intersection.
2012 * This is a fragile hack as it relies on the port names
2013 * being set to the various streams path.
2015 * A stream name should be introduced as part of the trace-info
2018 for (stream_idx
= 0; stream_idx
< stream_count
; stream_idx
++) {
2019 const char *stream_path
;
2021 port_id
= g_new0(struct port_id
, 1);
2024 BT_LOGE_STR("Cannot allocate memory for port_id structure.");
2027 port_id
->instance_name
= strdup(cfg_comp
->instance_name
->str
);
2028 if (!port_id
->instance_name
) {
2030 BT_LOGE_STR("Cannot allocate memory for port_id component instance name.");
2034 trace_range
= g_new0(struct trace_range
, 1);
2037 BT_LOGE_STR("Cannot allocate memory for trace_range structure.");
2040 trace_range
->intersection_range_begin_ns
= begin
;
2041 trace_range
->intersection_range_end_ns
= end
;
2043 stream_info
= bt_value_array_get(stream_infos
,
2045 if (!stream_info
|| !bt_value_is_map(stream_info
)) {
2047 BT_LOGD_STR("Cannot retrieve stream informations from trace in query result.");
2051 stream_paths
= bt_value_map_get(stream_info
, "paths");
2052 if (!stream_paths
|| !bt_value_is_array(stream_paths
)) {
2054 BT_LOGD_STR("Cannot retrieve stream paths from trace in query result.");
2058 stream_path_value
= bt_value_array_get(stream_paths
, 0);
2059 if (!stream_path_value
||
2060 !bt_value_is_string(stream_path_value
)) {
2062 BT_LOGD_STR("Cannot retrieve stream path value from trace in query result.");
2066 value_status
= bt_value_string_get(stream_path_value
,
2068 if (value_status
!= BT_VALUE_STATUS_OK
) {
2073 port_id
->port_name
= strdup(stream_path
);
2074 if (!port_id
->port_name
) {
2076 BT_LOGE_STR("Cannot allocate memory for port_id port_name.");
2080 BT_LOGD("Inserting stream intersection ");
2082 g_hash_table_insert(ctx
->intersections
, port_id
, trace_range
);
2086 BT_PUT(stream_info
);
2087 BT_PUT(stream_paths
);
2088 BT_PUT(stream_path_value
);
2092 BT_PUT(stream_paths
);
2093 BT_PUT(stream_path_value
);
2094 BT_PUT(intersection_range
);
2095 BT_PUT(intersection_begin
);
2096 BT_PUT(intersection_end
);
2097 BT_PUT(stream_paths
);
2098 BT_PUT(stream_path_value
);
2104 fprintf(stderr
, "%s%sCannot determine stream intersection of trace at path \'%s\'.%s\n",
2105 bt_common_color_bold(),
2106 bt_common_color_fg_yellow(),
2107 path
? path
: "(unknown)",
2108 bt_common_color_reset());
2110 bt_put(component_path_value
);
2111 bt_put(query_params
);
2112 bt_put(query_result
);
2114 bt_put(intersection_range
);
2115 bt_put(intersection_begin
);
2116 bt_put(intersection_end
);
2117 bt_put(stream_infos
);
2118 bt_put(stream_info
);
2119 bt_put(stream_paths
);
2120 bt_put(stream_path_value
);
2122 g_free(trace_range
);
2127 int cmd_run_ctx_create_components_from_config_components(
2128 struct cmd_run_ctx
*ctx
, GPtrArray
*cfg_components
)
2131 struct bt_component_class
*comp_cls
= NULL
;
2132 struct bt_component
*comp
= NULL
;
2135 for (i
= 0; i
< cfg_components
->len
; i
++) {
2136 struct bt_config_component
*cfg_comp
=
2137 g_ptr_array_index(cfg_components
, i
);
2140 comp_cls
= find_component_class(cfg_comp
->plugin_name
->str
,
2141 cfg_comp
->comp_cls_name
->str
, cfg_comp
->type
);
2143 BT_LOGE("Cannot find component class: plugin-name=\"%s\", "
2144 "comp-cls-name=\"%s\", comp-cls-type=%d",
2145 cfg_comp
->plugin_name
->str
,
2146 cfg_comp
->comp_cls_name
->str
,
2148 fprintf(stderr
, "%s%sCannot find component class %s",
2149 bt_common_color_bold(),
2150 bt_common_color_fg_red(),
2151 bt_common_color_reset());
2152 print_plugin_comp_cls_opt(stderr
,
2153 cfg_comp
->plugin_name
->str
,
2154 cfg_comp
->comp_cls_name
->str
,
2156 fprintf(stderr
, "\n");
2160 ret
= bt_graph_add_component(ctx
->graph
, comp_cls
,
2161 cfg_comp
->instance_name
->str
, cfg_comp
->params
, &comp
);
2163 BT_LOGE("Cannot create component: plugin-name=\"%s\", "
2164 "comp-cls-name=\"%s\", comp-cls-type=%d, "
2166 cfg_comp
->plugin_name
->str
,
2167 cfg_comp
->comp_cls_name
->str
,
2168 cfg_comp
->type
, cfg_comp
->instance_name
->str
);
2169 fprintf(stderr
, "%s%sCannot create component `%s`%s\n",
2170 bt_common_color_bold(),
2171 bt_common_color_fg_red(),
2172 cfg_comp
->instance_name
->str
,
2173 bt_common_color_reset());
2177 if (ctx
->stream_intersection_mode
&&
2178 cfg_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
2179 ret
= set_stream_intersections(ctx
, cfg_comp
, comp_cls
);
2185 BT_LOGI("Created and inserted component: comp-addr=%p, comp-name=\"%s\"",
2186 comp
, cfg_comp
->instance_name
->str
);
2187 quark
= g_quark_from_string(cfg_comp
->instance_name
->str
);
2189 g_hash_table_insert(ctx
->components
,
2190 GUINT_TO_POINTER(quark
), comp
);
2207 int cmd_run_ctx_create_components(struct cmd_run_ctx
*ctx
)
2212 * Make sure that, during this phase, our graph's "port added"
2213 * listener does not connect ports while we are creating the
2214 * components because we have a special, initial phase for
2217 ctx
->connect_ports
= false;
2219 ret
= cmd_run_ctx_create_components_from_config_components(
2220 ctx
, ctx
->cfg
->cmd_data
.run
.sources
);
2226 ret
= cmd_run_ctx_create_components_from_config_components(
2227 ctx
, ctx
->cfg
->cmd_data
.run
.filters
);
2233 ret
= cmd_run_ctx_create_components_from_config_components(
2234 ctx
, ctx
->cfg
->cmd_data
.run
.sinks
);
2245 int cmd_run_ctx_connect_comp_ports(struct cmd_run_ctx
*ctx
,
2246 struct bt_component
*comp
,
2247 int64_t (*port_count_fn
)(struct bt_component
*),
2248 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t))
2254 count
= port_count_fn(comp
);
2257 for (i
= 0; i
< count
; i
++) {
2258 struct bt_port
*upstream_port
= port_by_index_fn(comp
, i
);
2260 assert(upstream_port
);
2261 ret
= cmd_run_ctx_connect_upstream_port(ctx
, upstream_port
);
2262 bt_put(upstream_port
);
2273 int cmd_run_ctx_connect_ports(struct cmd_run_ctx
*ctx
)
2276 GHashTableIter iter
;
2277 gpointer g_name_quark
, g_comp
;
2279 ctx
->connect_ports
= true;
2280 g_hash_table_iter_init(&iter
, ctx
->components
);
2282 while (g_hash_table_iter_next(&iter
, &g_name_quark
, &g_comp
)) {
2283 int64_t (*port_count_fn
)(struct bt_component
*);
2284 struct bt_port
*(*port_by_index_fn
)(struct bt_component
*, uint64_t);
2286 if (bt_component_is_source(g_comp
)) {
2288 bt_component_source_get_output_port_count
;
2290 bt_component_source_get_output_port_by_index
;
2291 } else if (bt_component_is_filter(g_comp
)) {
2293 bt_component_filter_get_output_port_count
;
2295 bt_component_filter_get_output_port_by_index
;
2300 ret
= cmd_run_ctx_connect_comp_ports(ctx
,
2301 g_comp
, port_count_fn
, port_by_index_fn
);
2312 const char *bt_graph_status_str(enum bt_graph_status status
)
2315 case BT_GRAPH_STATUS_CANCELED
:
2316 return "BT_GRAPH_STATUS_CANCELED";
2317 case BT_GRAPH_STATUS_AGAIN
:
2318 return "BT_GRAPH_STATUS_AGAIN";
2319 case BT_GRAPH_STATUS_END
:
2320 return "BT_GRAPH_STATUS_END";
2321 case BT_GRAPH_STATUS_OK
:
2322 return "BT_GRAPH_STATUS_OK";
2323 case BT_GRAPH_STATUS_INVALID
:
2324 return "BT_GRAPH_STATUS_INVALID";
2325 case BT_GRAPH_STATUS_NO_SINK
:
2326 return "BT_GRAPH_STATUS_NO_SINK";
2327 case BT_GRAPH_STATUS_ERROR
:
2328 return "BT_GRAPH_STATUS_ERROR";
2335 int cmd_run(struct bt_config
*cfg
)
2338 struct cmd_run_ctx ctx
= { 0 };
2340 /* Initialize the command's context and the graph object */
2341 if (cmd_run_ctx_init(&ctx
, cfg
)) {
2342 BT_LOGE_STR("Cannot initialize the command's context.");
2343 fprintf(stderr
, "Cannot initialize the command's context\n");
2348 BT_LOGI_STR("Canceled by user before creating components.");
2352 BT_LOGI_STR("Creating components.");
2354 /* Create the requested component instances */
2355 if (cmd_run_ctx_create_components(&ctx
)) {
2356 BT_LOGE_STR("Cannot create components.");
2357 fprintf(stderr
, "Cannot create components\n");
2362 BT_LOGI_STR("Canceled by user before connecting components.");
2366 BT_LOGI_STR("Connecting components.");
2368 /* Connect the initially visible component ports */
2369 if (cmd_run_ctx_connect_ports(&ctx
)) {
2370 BT_LOGE_STR("Cannot connect initial component ports.");
2371 fprintf(stderr
, "Cannot connect initial component ports\n");
2376 BT_LOGI_STR("Canceled by user before running the graph.");
2380 BT_LOGI_STR("Running the graph.");
2384 enum bt_graph_status graph_status
= bt_graph_run(ctx
.graph
);
2387 * Reset console in case something messed with console
2388 * codes during the graph's execution.
2390 printf("%s", bt_common_color_reset());
2392 fprintf(stderr
, "%s", bt_common_color_reset());
2393 BT_LOGV("bt_graph_run() returned: status=%s",
2394 bt_graph_status_str(graph_status
));
2396 switch (graph_status
) {
2397 case BT_GRAPH_STATUS_OK
:
2399 case BT_GRAPH_STATUS_CANCELED
:
2400 BT_LOGI_STR("Graph was canceled by user.");
2402 case BT_GRAPH_STATUS_AGAIN
:
2403 if (bt_graph_is_canceled(ctx
.graph
)) {
2404 BT_LOGI_STR("Graph was canceled by user.");
2408 if (cfg
->cmd_data
.run
.retry_duration_us
> 0) {
2409 BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
2411 cfg
->cmd_data
.run
.retry_duration_us
);
2413 if (usleep(cfg
->cmd_data
.run
.retry_duration_us
)) {
2414 if (bt_graph_is_canceled(ctx
.graph
)) {
2415 BT_LOGI_STR("Graph was canceled by user.");
2421 case BT_COMPONENT_STATUS_END
:
2424 BT_LOGE_STR("Graph failed to complete successfully");
2425 fprintf(stderr
, "Graph failed to complete successfully\n");
2438 cmd_run_ctx_destroy(&ctx
);
2443 void warn_command_name_and_directory_clash(struct bt_config
*cfg
)
2445 const char *env_clash
;
2447 if (!cfg
->command_name
) {
2451 env_clash
= getenv(ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH
);
2452 if (env_clash
&& strcmp(env_clash
, "0") == 0) {
2456 if (g_file_test(cfg
->command_name
,
2457 G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
)) {
2458 fprintf(stderr
, "\nNOTE: The `%s` command was executed. If you meant to convert a\n",
2460 fprintf(stderr
, "trace located in the local `%s` directory, please use:\n",
2462 fprintf(stderr
, "\n");
2463 fprintf(stderr
, " babeltrace convert %s [OPTIONS]\n",
2469 void init_log_level(void)
2471 bt_cli_log_level
= bt_log_get_level_from_env(ENV_BABELTRACE_CLI_LOG_LEVEL
);
2475 void set_auto_log_levels(struct bt_config
*cfg
)
2477 const char **env_var_name
;
2480 * Override the configuration's default log level if
2481 * BABELTRACE_VERBOSE or BABELTRACE_DEBUG environment variables
2482 * are found for backward compatibility with legacy Babetrace 1.
2484 if (getenv("BABELTRACE_DEBUG") &&
2485 strcmp(getenv("BABELTRACE_DEBUG"), "1") == 0) {
2486 cfg
->log_level
= 'V';
2487 } else if (getenv("BABELTRACE_VERBOSE") &&
2488 strcmp(getenv("BABELTRACE_VERBOSE"), "1") == 0) {
2489 cfg
->log_level
= 'I';
2493 * Set log levels according to --debug or --verbose. For
2494 * backward compatibility, --debug is more verbose than
2497 * --verbose: INFO log level
2498 * --debug: VERBOSE log level (includes DEBUG, which is
2499 * is less verbose than VERBOSE in the internal
2500 * logging framework)
2502 if (!getenv("BABELTRACE_LOGGING_GLOBAL_LEVEL")) {
2504 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2505 } else if (cfg
->debug
) {
2506 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2509 * Set library's default log level if not
2510 * explicitly specified.
2512 switch (cfg
->log_level
) {
2514 bt_logging_set_global_level(BT_LOGGING_LEVEL_NONE
);
2517 bt_logging_set_global_level(BT_LOGGING_LEVEL_VERBOSE
);
2520 bt_logging_set_global_level(BT_LOGGING_LEVEL_DEBUG
);
2523 bt_logging_set_global_level(BT_LOGGING_LEVEL_INFO
);
2526 bt_logging_set_global_level(BT_LOGGING_LEVEL_WARN
);
2529 bt_logging_set_global_level(BT_LOGGING_LEVEL_ERROR
);
2532 bt_logging_set_global_level(BT_LOGGING_LEVEL_FATAL
);
2540 if (!getenv(ENV_BABELTRACE_CLI_LOG_LEVEL
)) {
2542 bt_cli_log_level
= BT_LOG_INFO
;
2543 } else if (cfg
->debug
) {
2544 bt_cli_log_level
= BT_LOG_VERBOSE
;
2547 * Set CLI's default log level if not explicitly
2550 switch (cfg
->log_level
) {
2552 bt_cli_log_level
= BT_LOG_NONE
;
2555 bt_cli_log_level
= BT_LOG_VERBOSE
;
2558 bt_cli_log_level
= BT_LOG_DEBUG
;
2561 bt_cli_log_level
= BT_LOG_INFO
;
2564 bt_cli_log_level
= BT_LOG_WARN
;
2567 bt_cli_log_level
= BT_LOG_ERROR
;
2570 bt_cli_log_level
= BT_LOG_FATAL
;
2578 env_var_name
= log_level_env_var_names
;
2580 while (*env_var_name
) {
2581 if (!getenv(*env_var_name
)) {
2583 g_setenv(*env_var_name
, "I", 1);
2584 } else if (cfg
->debug
) {
2585 g_setenv(*env_var_name
, "V", 1);
2587 char val
[2] = { 0 };
2590 * Set module's default log level if not
2591 * explicitly specified.
2593 val
[0] = cfg
->log_level
;
2594 g_setenv(*env_var_name
, val
, 1);
2603 void set_sigint_handler(void)
2605 struct sigaction new_action
, old_action
;
2607 new_action
.sa_handler
= sigint_handler
;
2608 sigemptyset(&new_action
.sa_mask
);
2609 new_action
.sa_flags
= 0;
2610 sigaction(SIGINT
, NULL
, &old_action
);
2612 if (old_action
.sa_handler
!= SIG_IGN
) {
2613 sigaction(SIGINT
, &new_action
, NULL
);
2617 int main(int argc
, const char **argv
)
2621 struct bt_config
*cfg
;
2624 set_sigint_handler();
2626 cfg
= bt_config_cli_args_create_with_default(argc
, argv
, &retcode
);
2629 /* Quit without errors; typically usage/version */
2631 BT_LOGI_STR("Quitting without errors.");
2636 BT_LOGE("Command-line error: retcode=%d", retcode
);
2641 BT_LOGE_STR("Failed to create a valid Babeltrace configuration.");
2642 fprintf(stderr
, "Failed to create Babeltrace configuration\n");
2647 set_auto_log_levels(cfg
);
2650 if (cfg
->command_needs_plugins
) {
2651 ret
= load_all_plugins(cfg
->plugin_paths
);
2653 BT_LOGE("Failed to load plugins: ret=%d", ret
);
2659 BT_LOGI("Executing command: cmd=%d, command-name=\"%s\"",
2660 cfg
->command
, cfg
->command_name
);
2662 switch (cfg
->command
) {
2663 case BT_CONFIG_COMMAND_RUN
:
2666 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
2667 ret
= cmd_list_plugins(cfg
);
2669 case BT_CONFIG_COMMAND_HELP
:
2670 ret
= cmd_help(cfg
);
2672 case BT_CONFIG_COMMAND_QUERY
:
2673 ret
= cmd_query(cfg
);
2675 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
2676 ret
= cmd_print_ctf_metadata(cfg
);
2678 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
2679 ret
= cmd_print_lttng_live_sessions(cfg
);
2682 BT_LOGF("Invalid/unknown command: cmd=%d", cfg
->command
);
2686 BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
2687 cfg
->command
, cfg
->command_name
, ret
);
2688 warn_command_name_and_directory_clash(cfg
);
2689 retcode
= ret
? 1 : 0;