2 * Babeltrace trace converter - parameter parsing
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "CLI/CFG-CLI-ARGS"
31 #include "common/assert.h"
35 #include <babeltrace2/babeltrace.h>
36 #include "common/common.h"
38 #include <sys/types.h>
39 #include "argpar/argpar.h"
40 #include "babeltrace2-cfg.h"
41 #include "babeltrace2-cfg-cli-args.h"
42 #include "babeltrace2-cfg-cli-args-connect.h"
43 #include "babeltrace2-cfg-cli-params-arg.h"
44 #include "babeltrace2-log-level.h"
45 #include "babeltrace2-plugins.h"
46 #include "babeltrace2-query.h"
47 #include "autodisc/autodisc.h"
48 #include "common/version.h"
50 /* INI-style parsing FSM states */
51 enum ini_parsing_fsm_state
{
52 /* Expect a map key (identifier) */
55 /* Expect an equal character ('=') */
61 /* Expect a comma character (',') */
65 /* INI-style parsing state variables */
66 struct ini_parsing_state
{
67 /* Lexical scanner (owned by this) */
70 /* Output map value object being filled (owned by this) */
73 /* Next expected FSM state */
74 enum ini_parsing_fsm_state expecting
;
76 /* Last decoded map key (owned by this) */
79 /* Complete INI-style string to parse (not owned by this) */
82 /* Error buffer (not owned by this) */
86 /* Offset option with "is set" boolean */
92 /* Legacy "ctf"/"lttng-live" format options */
93 struct ctf_legacy_opts
{
94 struct offset_opt offset_s
;
95 struct offset_opt offset_ns
;
96 bool stream_intersection
;
99 /* Legacy "text" format options */
100 struct text_legacy_opts
{
102 * output, dbg_info_dir, dbg_info_target_prefix, names,
103 * and fields are owned by this.
106 GString
*dbg_info_dir
;
107 GString
*dbg_info_target_prefix
;
108 const bt_value
*names
;
109 const bt_value
*fields
;
117 bool dbg_info_full_path
;
121 /* Legacy input format format */
122 enum legacy_input_format
{
123 LEGACY_INPUT_FORMAT_NONE
= 0,
124 LEGACY_INPUT_FORMAT_CTF
,
125 LEGACY_INPUT_FORMAT_LTTNG_LIVE
,
128 /* Legacy output format format */
129 enum legacy_output_format
{
130 LEGACY_OUTPUT_FORMAT_NONE
= 0,
131 LEGACY_OUTPUT_FORMAT_TEXT
,
132 LEGACY_OUTPUT_FORMAT_DUMMY
,
135 #define BT_CLI_LOGE_APPEND_CAUSE_OOM() BT_CLI_LOGE_APPEND_CAUSE("Out of memory.")
138 * Returns the plugin name, component class name, component class type,
139 * and component name from a command-line --component option's argument.
140 * arg must have the following format:
142 * [NAME:]TYPE.PLUGIN.CLS
144 * where NAME is the optional component name, TYPE is either `source`,
145 * `filter`, or `sink`, PLUGIN is the plugin name, and CLS is the
146 * component class name.
148 * On success, both *plugin and *component are not NULL. *plugin
149 * and *comp_cls are owned by the caller. On success, *name can be NULL
150 * if no component class name was found, and *comp_cls_type is set.
153 void plugin_comp_cls_names(const char *arg
, char **name
, char **plugin
,
154 char **comp_cls
, bt_component_class_type
*comp_cls_type
)
156 const char *at
= arg
;
157 GString
*gs_name
= NULL
;
158 GString
*gs_comp_cls_type
= NULL
;
159 GString
*gs_plugin
= NULL
;
160 GString
*gs_comp_cls
= NULL
;
166 BT_ASSERT(comp_cls_type
);
168 if (!bt_common_string_is_printable(arg
)) {
169 BT_CLI_LOGE_APPEND_CAUSE("Argument contains a non-printable character.");
173 /* Parse the component name */
174 gs_name
= bt_common_string_until(at
, ".:\\", ":", &end_pos
);
179 if (arg
[end_pos
] == ':') {
183 g_string_assign(gs_name
, "");
186 /* Parse the component class type */
187 gs_comp_cls_type
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
188 if (!gs_comp_cls_type
|| at
[end_pos
] == '\0') {
189 BT_CLI_LOGE_APPEND_CAUSE("Missing component class type (`source`, `filter`, or `sink`).");
193 if (strcmp(gs_comp_cls_type
->str
, "source") == 0 ||
194 strcmp(gs_comp_cls_type
->str
, "src") == 0) {
195 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SOURCE
;
196 } else if (strcmp(gs_comp_cls_type
->str
, "filter") == 0 ||
197 strcmp(gs_comp_cls_type
->str
, "flt") == 0) {
198 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_FILTER
;
199 } else if (strcmp(gs_comp_cls_type
->str
, "sink") == 0) {
200 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SINK
;
202 BT_CLI_LOGE_APPEND_CAUSE("Unknown component class type: `%s`.",
203 gs_comp_cls_type
->str
);
209 /* Parse the plugin name */
210 gs_plugin
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
211 if (!gs_plugin
|| gs_plugin
->len
== 0 || at
[end_pos
] == '\0') {
212 BT_CLI_LOGE_APPEND_CAUSE("Missing plugin or component class name.");
218 /* Parse the component class name */
219 gs_comp_cls
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
220 if (!gs_comp_cls
|| gs_comp_cls
->len
== 0) {
221 BT_CLI_LOGE_APPEND_CAUSE("Missing component class name.");
225 if (at
[end_pos
] != '\0') {
226 /* Found a non-escaped `.` */
231 if (gs_name
->len
== 0) {
233 g_string_free(gs_name
, TRUE
);
235 *name
= gs_name
->str
;
236 g_string_free(gs_name
, FALSE
);
239 g_string_free(gs_name
, TRUE
);
242 *plugin
= gs_plugin
->str
;
243 *comp_cls
= gs_comp_cls
->str
;
244 g_string_free(gs_plugin
, FALSE
);
245 g_string_free(gs_comp_cls
, FALSE
);
261 g_string_free(gs_name
, TRUE
);
265 g_string_free(gs_plugin
, TRUE
);
269 g_string_free(gs_comp_cls
, TRUE
);
272 if (gs_comp_cls_type
) {
273 g_string_free(gs_comp_cls_type
, TRUE
);
280 * Prints the Babeltrace version.
283 void print_version(void)
285 if (GIT_VERSION
[0] == '\0') {
286 puts("Babeltrace " VERSION
);
288 puts("Babeltrace " VERSION
" - " GIT_VERSION
);
293 * Destroys a component configuration.
296 void bt_config_component_destroy(bt_object
*obj
)
298 struct bt_config_component
*bt_config_component
=
299 container_of(obj
, struct bt_config_component
, base
);
305 if (bt_config_component
->plugin_name
) {
306 g_string_free(bt_config_component
->plugin_name
, TRUE
);
309 if (bt_config_component
->comp_cls_name
) {
310 g_string_free(bt_config_component
->comp_cls_name
, TRUE
);
313 if (bt_config_component
->instance_name
) {
314 g_string_free(bt_config_component
->instance_name
, TRUE
);
317 BT_VALUE_PUT_REF_AND_RESET(bt_config_component
->params
);
318 g_free(bt_config_component
);
325 * Creates a component configuration using the given plugin name and
326 * component name. `plugin_name` and `comp_cls_name` are copied (belong
327 * to the return value).
329 * Return value is owned by the caller.
332 struct bt_config_component
*bt_config_component_create(
333 bt_component_class_type type
,
334 const char *plugin_name
, const char *comp_cls_name
,
337 struct bt_config_component
*cfg_component
= NULL
;
339 cfg_component
= g_new0(struct bt_config_component
, 1);
340 if (!cfg_component
) {
341 BT_CLI_LOGE_APPEND_CAUSE_OOM();
345 bt_object_init_shared(&cfg_component
->base
,
346 bt_config_component_destroy
);
347 cfg_component
->type
= type
;
348 cfg_component
->plugin_name
= g_string_new(plugin_name
);
349 if (!cfg_component
->plugin_name
) {
350 BT_CLI_LOGE_APPEND_CAUSE_OOM();
354 cfg_component
->comp_cls_name
= g_string_new(comp_cls_name
);
355 if (!cfg_component
->comp_cls_name
) {
356 BT_CLI_LOGE_APPEND_CAUSE_OOM();
360 cfg_component
->instance_name
= g_string_new(NULL
);
361 if (!cfg_component
->instance_name
) {
362 BT_CLI_LOGE_APPEND_CAUSE_OOM();
366 cfg_component
->log_level
= init_log_level
;
368 /* Start with empty parameters */
369 cfg_component
->params
= bt_value_map_create();
370 if (!cfg_component
->params
) {
371 BT_CLI_LOGE_APPEND_CAUSE_OOM();
378 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
381 return cfg_component
;
385 * Creates a component configuration from a command-line --component
389 struct bt_config_component
*bt_config_component_from_arg(const char *arg
,
392 struct bt_config_component
*cfg_comp
= NULL
;
394 char *plugin_name
= NULL
;
395 char *comp_cls_name
= NULL
;
396 bt_component_class_type type
;
398 plugin_comp_cls_names(arg
, &name
, &plugin_name
, &comp_cls_name
, &type
);
399 if (!plugin_name
|| !comp_cls_name
) {
403 cfg_comp
= bt_config_component_create(type
, plugin_name
, comp_cls_name
,
410 g_string_assign(cfg_comp
->instance_name
, name
);
416 BT_OBJECT_PUT_REF_AND_RESET(cfg_comp
);
421 g_free(comp_cls_name
);
426 * Destroys a configuration.
429 void bt_config_destroy(bt_object
*obj
)
431 struct bt_config
*cfg
=
432 container_of(obj
, struct bt_config
, base
);
438 BT_VALUE_PUT_REF_AND_RESET(cfg
->plugin_paths
);
440 switch (cfg
->command
) {
441 case BT_CONFIG_COMMAND_RUN
:
442 if (cfg
->cmd_data
.run
.sources
) {
443 g_ptr_array_free(cfg
->cmd_data
.run
.sources
, TRUE
);
446 if (cfg
->cmd_data
.run
.filters
) {
447 g_ptr_array_free(cfg
->cmd_data
.run
.filters
, TRUE
);
450 if (cfg
->cmd_data
.run
.sinks
) {
451 g_ptr_array_free(cfg
->cmd_data
.run
.sinks
, TRUE
);
454 if (cfg
->cmd_data
.run
.connections
) {
455 g_ptr_array_free(cfg
->cmd_data
.run
.connections
,
459 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
461 case BT_CONFIG_COMMAND_HELP
:
462 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.help
.cfg_component
);
464 case BT_CONFIG_COMMAND_QUERY
:
465 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.query
.cfg_component
);
467 if (cfg
->cmd_data
.query
.object
) {
468 g_string_free(cfg
->cmd_data
.query
.object
, TRUE
);
471 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
472 if (cfg
->cmd_data
.print_ctf_metadata
.path
) {
473 g_string_free(cfg
->cmd_data
.print_ctf_metadata
.path
,
476 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
480 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
481 if (cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
483 cfg
->cmd_data
.print_lttng_live_sessions
.url
,
486 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
501 void destroy_glist_of_gstring(GList
*list
)
509 for (at
= list
; at
; at
= g_list_next(at
)) {
510 g_string_free(at
->data
, TRUE
);
517 * Creates a simple lexical scanner for parsing comma-delimited names
520 * Return value is owned by the caller.
523 GScanner
*create_csv_identifiers_scanner(void)
526 GScannerConfig scanner_config
= {
527 .cset_skip_characters
= " \t\n",
528 .cset_identifier_first
= G_CSET_a_2_z G_CSET_A_2_Z
"_",
529 .cset_identifier_nth
= G_CSET_a_2_z G_CSET_A_2_Z
":_-",
530 .case_sensitive
= TRUE
,
531 .cpair_comment_single
= NULL
,
532 .skip_comment_multi
= TRUE
,
533 .skip_comment_single
= TRUE
,
534 .scan_comment_multi
= FALSE
,
535 .scan_identifier
= TRUE
,
536 .scan_identifier_1char
= TRUE
,
537 .scan_identifier_NULL
= FALSE
,
538 .scan_symbols
= FALSE
,
539 .symbol_2_token
= FALSE
,
540 .scope_0_fallback
= FALSE
,
541 .scan_binary
= FALSE
,
545 .scan_hex_dollar
= FALSE
,
546 .numbers_2_int
= FALSE
,
547 .int_2_float
= FALSE
,
548 .store_int64
= FALSE
,
549 .scan_string_sq
= FALSE
,
550 .scan_string_dq
= FALSE
,
551 .identifier_2_string
= FALSE
,
552 .char_2_token
= TRUE
,
555 scanner
= g_scanner_new(&scanner_config
);
557 BT_CLI_LOGE_APPEND_CAUSE_OOM();
564 * Converts a comma-delimited list of known names (--names option) to
565 * an array value object containing those names as string value objects.
567 * Return value is owned by the caller.
570 bt_value
*names_from_arg(const char *arg
)
572 GScanner
*scanner
= NULL
;
573 bt_value
*names
= NULL
;
574 bool found_all
= false, found_none
= false, found_item
= false;
576 names
= bt_value_array_create();
578 BT_CLI_LOGE_APPEND_CAUSE_OOM();
582 scanner
= create_csv_identifiers_scanner();
587 g_scanner_input_text(scanner
, arg
, strlen(arg
));
590 GTokenType token_type
= g_scanner_get_next_token(scanner
);
592 switch (token_type
) {
593 case G_TOKEN_IDENTIFIER
:
595 const char *identifier
= scanner
->value
.v_identifier
;
597 if (strcmp(identifier
, "payload") == 0 ||
598 strcmp(identifier
, "args") == 0 ||
599 strcmp(identifier
, "arg") == 0) {
601 if (bt_value_array_append_string_element(names
,
605 } else if (strcmp(identifier
, "context") == 0 ||
606 strcmp(identifier
, "ctx") == 0) {
608 if (bt_value_array_append_string_element(names
,
612 } else if (strcmp(identifier
, "scope") == 0 ||
613 strcmp(identifier
, "header") == 0) {
615 if (bt_value_array_append_string_element(names
,
619 } else if (strcmp(identifier
, "all") == 0) {
621 if (bt_value_array_append_string_element(names
,
625 } else if (strcmp(identifier
, "none") == 0) {
627 if (bt_value_array_append_string_element(names
,
632 BT_CLI_LOGE_APPEND_CAUSE("Unknown name: `%s`.",
648 if (found_none
&& found_all
) {
649 BT_CLI_LOGE_APPEND_CAUSE("Only either `all` or `none` can be specified in the list given to the --names option, but not both.");
653 * Legacy behavior is to clear the defaults (show none) when at
654 * least one item is specified.
656 if (found_item
&& !found_none
&& !found_all
) {
657 if (bt_value_array_append_string_element(names
, "none")) {
662 g_scanner_destroy(scanner
);
667 BT_VALUE_PUT_REF_AND_RESET(names
);
669 g_scanner_destroy(scanner
);
675 * Converts a comma-delimited list of known fields (--fields option) to
676 * an array value object containing those fields as string
679 * Return value is owned by the caller.
682 bt_value
*fields_from_arg(const char *arg
)
684 GScanner
*scanner
= NULL
;
687 fields
= bt_value_array_create();
689 BT_CLI_LOGE_APPEND_CAUSE_OOM();
693 scanner
= create_csv_identifiers_scanner();
698 g_scanner_input_text(scanner
, arg
, strlen(arg
));
701 GTokenType token_type
= g_scanner_get_next_token(scanner
);
703 switch (token_type
) {
704 case G_TOKEN_IDENTIFIER
:
706 const char *identifier
= scanner
->value
.v_identifier
;
708 if (strcmp(identifier
, "trace") == 0 ||
709 strcmp(identifier
, "trace:hostname") == 0 ||
710 strcmp(identifier
, "trace:domain") == 0 ||
711 strcmp(identifier
, "trace:procname") == 0 ||
712 strcmp(identifier
, "trace:vpid") == 0 ||
713 strcmp(identifier
, "loglevel") == 0 ||
714 strcmp(identifier
, "emf") == 0 ||
715 strcmp(identifier
, "callsite") == 0 ||
716 strcmp(identifier
, "all") == 0) {
717 if (bt_value_array_append_string_element(fields
,
722 BT_CLI_LOGE_APPEND_CAUSE("Unknown field: `%s`.",
740 BT_VALUE_PUT_REF_AND_RESET(fields
);
744 g_scanner_destroy(scanner
);
750 void append_param_arg(GString
*params_arg
, const char *key
, const char *value
)
752 BT_ASSERT(params_arg
);
756 if (params_arg
->len
!= 0) {
757 g_string_append_c(params_arg
, ',');
760 g_string_append(params_arg
, key
);
761 g_string_append_c(params_arg
, '=');
762 g_string_append(params_arg
, value
);
766 * Inserts the equivalent "prefix-NAME=yes" strings into params_arg
767 * where the names are in names_array.
770 int insert_flat_params_from_array(GString
*params_arg
,
771 const bt_value
*names_array
, const char *prefix
)
775 GString
*tmpstr
= NULL
, *default_value
= NULL
;
776 bool default_set
= false, non_default_set
= false;
779 * names_array may be NULL if no CLI options were specified to
780 * trigger its creation.
786 tmpstr
= g_string_new(NULL
);
788 BT_CLI_LOGE_APPEND_CAUSE_OOM();
793 default_value
= g_string_new(NULL
);
794 if (!default_value
) {
795 BT_CLI_LOGE_APPEND_CAUSE_OOM();
800 for (i
= 0; i
< bt_value_array_get_length(names_array
); i
++) {
801 const bt_value
*str_obj
=
802 bt_value_array_borrow_element_by_index_const(names_array
,
805 bool is_default
= false;
808 BT_CLI_LOGE_APPEND_CAUSE("Unexpected error.");
813 suffix
= bt_value_string_get(str_obj
);
815 g_string_assign(tmpstr
, prefix
);
816 g_string_append(tmpstr
, "-");
818 /* Special-case for "all" and "none". */
819 if (strcmp(suffix
, "all") == 0) {
821 g_string_assign(default_value
, "show");
822 } else if (strcmp(suffix
, "none") == 0) {
824 g_string_assign(default_value
, "hide");
828 g_string_append(tmpstr
, "default");
829 append_param_arg(params_arg
, tmpstr
->str
,
832 non_default_set
= true;
833 g_string_append(tmpstr
, suffix
);
834 append_param_arg(params_arg
, tmpstr
->str
, "yes");
838 /* Implicit field-default=hide if any non-default option is set. */
839 if (non_default_set
&& !default_set
) {
840 g_string_assign(tmpstr
, prefix
);
841 g_string_append(tmpstr
, "-default");
842 g_string_assign(default_value
, "hide");
843 append_param_arg(params_arg
, tmpstr
->str
, default_value
->str
);
848 g_string_free(default_value
, TRUE
);
852 g_string_free(tmpstr
, TRUE
);
865 OPT_CLOCK_FORCE_CORRELATE
,
876 OPT_DEBUG_INFO_FULL_PATH
,
877 OPT_DEBUG_INFO_TARGET_PREFIX
,
886 OPT_OMIT_HOME_PLUGIN_PATH
,
887 OPT_OMIT_SYSTEM_PLUGIN_PATH
,
892 OPT_RESET_BASE_PARAMS
,
896 OPT_STREAM_INTERSECTION
,
902 enum bt_config_component_dest
{
903 BT_CONFIG_COMPONENT_DEST_UNKNOWN
= -1,
904 BT_CONFIG_COMPONENT_DEST_SOURCE
,
905 BT_CONFIG_COMPONENT_DEST_FILTER
,
906 BT_CONFIG_COMPONENT_DEST_SINK
,
910 * Adds a configuration component to the appropriate configuration
911 * array depending on the destination.
914 void add_run_cfg_comp(struct bt_config
*cfg
,
915 struct bt_config_component
*cfg_comp
,
916 enum bt_config_component_dest dest
)
918 bt_object_get_ref(cfg_comp
);
921 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
922 g_ptr_array_add(cfg
->cmd_data
.run
.sources
, cfg_comp
);
924 case BT_CONFIG_COMPONENT_DEST_FILTER
:
925 g_ptr_array_add(cfg
->cmd_data
.run
.filters
, cfg_comp
);
927 case BT_CONFIG_COMPONENT_DEST_SINK
:
928 g_ptr_array_add(cfg
->cmd_data
.run
.sinks
, cfg_comp
);
936 int add_run_cfg_comp_check_name(struct bt_config
*cfg
,
937 struct bt_config_component
*cfg_comp
,
938 enum bt_config_component_dest dest
,
939 bt_value
*instance_names
)
943 if (cfg_comp
->instance_name
->len
== 0) {
944 BT_CLI_LOGE_APPEND_CAUSE("Found an unnamed component.");
949 if (bt_value_map_has_entry(instance_names
,
950 cfg_comp
->instance_name
->str
)) {
951 BT_CLI_LOGE_APPEND_CAUSE("Duplicate component instance name:\n %s",
952 cfg_comp
->instance_name
->str
);
957 if (bt_value_map_insert_entry(instance_names
,
958 cfg_comp
->instance_name
->str
, bt_value_null
)) {
959 BT_CLI_LOGE_APPEND_CAUSE_OOM();
964 add_run_cfg_comp(cfg
, cfg_comp
, dest
);
971 int append_env_var_plugin_paths(bt_value
*plugin_paths
)
976 if (bt_common_is_setuid_setgid()) {
977 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
981 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
986 ret
= bt_config_append_plugin_paths(plugin_paths
, envvar
);
990 BT_CLI_LOGE_APPEND_CAUSE("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH.");
997 int append_home_and_system_plugin_paths(bt_value
*plugin_paths
,
998 bool omit_system_plugin_path
, bool omit_home_plugin_path
)
1002 if (!omit_home_plugin_path
) {
1003 if (bt_common_is_setuid_setgid()) {
1004 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1006 char *home_plugin_dir
= bt_common_get_home_plugin_path(
1007 BT_LOG_OUTPUT_LEVEL
);
1009 if (home_plugin_dir
) {
1010 ret
= bt_config_append_plugin_paths(
1011 plugin_paths
, home_plugin_dir
);
1012 free(home_plugin_dir
);
1015 BT_CLI_LOGE_APPEND_CAUSE("Invalid home plugin path.");
1022 if (!omit_system_plugin_path
) {
1023 if (bt_config_append_plugin_paths(plugin_paths
,
1024 bt_common_get_system_plugin_path())) {
1025 BT_CLI_LOGE_APPEND_CAUSE("Invalid system plugin path.");
1031 BT_CLI_LOGE_APPEND_CAUSE("Cannot append home and system plugin paths.");
1036 struct bt_config
*bt_config_base_create(enum bt_config_command command
,
1037 const bt_value
*plugin_paths
, bool needs_plugins
)
1039 struct bt_config
*cfg
;
1042 cfg
= g_new0(struct bt_config
, 1);
1044 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1048 bt_object_init_shared(&cfg
->base
, bt_config_destroy
);
1049 cfg
->command
= command
;
1050 cfg
->command_needs_plugins
= needs_plugins
;
1053 bt_value
*plugin_paths_copy
;
1055 (void) bt_value_copy(plugin_paths
,
1056 &plugin_paths_copy
);
1057 cfg
->plugin_paths
= plugin_paths_copy
;
1059 cfg
->plugin_paths
= bt_value_array_create();
1060 if (!cfg
->plugin_paths
) {
1061 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1069 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1076 struct bt_config
*bt_config_run_create(const bt_value
*plugin_paths
)
1078 struct bt_config
*cfg
;
1081 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_RUN
,
1082 plugin_paths
, true);
1087 cfg
->cmd_data
.run
.sources
= g_ptr_array_new_with_free_func(
1088 (GDestroyNotify
) bt_object_put_ref
);
1089 if (!cfg
->cmd_data
.run
.sources
) {
1090 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1094 cfg
->cmd_data
.run
.filters
= g_ptr_array_new_with_free_func(
1095 (GDestroyNotify
) bt_object_put_ref
);
1096 if (!cfg
->cmd_data
.run
.filters
) {
1097 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1101 cfg
->cmd_data
.run
.sinks
= g_ptr_array_new_with_free_func(
1102 (GDestroyNotify
) bt_object_put_ref
);
1103 if (!cfg
->cmd_data
.run
.sinks
) {
1104 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1108 cfg
->cmd_data
.run
.connections
= g_ptr_array_new_with_free_func(
1109 (GDestroyNotify
) bt_config_connection_destroy
);
1110 if (!cfg
->cmd_data
.run
.connections
) {
1111 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1118 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1125 struct bt_config
*bt_config_list_plugins_create(const bt_value
*plugin_paths
)
1127 return bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS
,
1128 plugin_paths
, true);
1132 struct bt_config
*bt_config_help_create(const bt_value
*plugin_paths
,
1133 int default_log_level
)
1135 struct bt_config
*cfg
;
1138 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_HELP
,
1139 plugin_paths
, true);
1144 cfg
->cmd_data
.help
.cfg_component
=
1145 bt_config_component_create(-1, NULL
, NULL
, default_log_level
);
1146 if (!cfg
->cmd_data
.help
.cfg_component
) {
1153 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1160 struct bt_config
*bt_config_query_create(const bt_value
*plugin_paths
)
1162 struct bt_config
*cfg
;
1165 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_QUERY
,
1166 plugin_paths
, true);
1171 cfg
->cmd_data
.query
.object
= g_string_new(NULL
);
1172 if (!cfg
->cmd_data
.query
.object
) {
1173 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1180 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1187 struct bt_config
*bt_config_print_ctf_metadata_create(
1188 const bt_value
*plugin_paths
)
1190 struct bt_config
*cfg
;
1193 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_CTF_METADATA
,
1194 plugin_paths
, true);
1199 cfg
->cmd_data
.print_ctf_metadata
.path
= g_string_new(NULL
);
1200 if (!cfg
->cmd_data
.print_ctf_metadata
.path
) {
1201 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1205 cfg
->cmd_data
.print_ctf_metadata
.output_path
= g_string_new(NULL
);
1206 if (!cfg
->cmd_data
.print_ctf_metadata
.output_path
) {
1207 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1214 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1221 struct bt_config
*bt_config_print_lttng_live_sessions_create(
1222 const bt_value
*plugin_paths
)
1224 struct bt_config
*cfg
;
1227 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
,
1228 plugin_paths
, true);
1233 cfg
->cmd_data
.print_lttng_live_sessions
.url
= g_string_new(NULL
);
1234 if (!cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
1235 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1239 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
=
1241 if (!cfg
->cmd_data
.print_lttng_live_sessions
.output_path
) {
1242 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1249 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1256 int bt_config_append_plugin_paths_check_setuid_setgid(
1257 bt_value
*plugin_paths
, const char *arg
)
1261 if (bt_common_is_setuid_setgid()) {
1262 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1266 if (bt_config_append_plugin_paths(plugin_paths
, arg
)) {
1267 BT_CLI_LOGE_APPEND_CAUSE("Invalid --plugin-path option's argument:\n %s",
1278 * Prints the expected format for a --params option.
1281 void print_expected_params_format(FILE *fp
)
1283 fprintf(fp
, "Expected format of PARAMS\n");
1284 fprintf(fp
, "-------------------------\n");
1286 fprintf(fp
, " PARAM=VALUE[,PARAM=VALUE]...\n");
1288 fprintf(fp
, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
1289 fprintf(fp
, "where PARAM is the parameter name (C identifier plus the [:.-] characters),\n");
1290 fprintf(fp
, "and VALUE can be one of:\n");
1292 fprintf(fp
, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
1293 fprintf(fp
, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
1294 fprintf(fp
, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
1295 fprintf(fp
, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
1296 fprintf(fp
, " (`0x` prefix) unsigned (with `+` prefix) or signed 64-bit integer.\n");
1297 fprintf(fp
, "* Double precision floating point number (scientific notation is accepted).\n");
1298 fprintf(fp
, "* Unquoted string with no special characters, and not matching any of\n");
1299 fprintf(fp
, " the null and boolean value symbols above.\n");
1300 fprintf(fp
, "* Double-quoted string (accepts escape characters).\n");
1301 fprintf(fp
, "* Array, formatted as an opening `[`, a list of comma-separated values\n");
1302 fprintf(fp
, " (as described by the current list) and a closing `]`.\n");
1303 fprintf(fp
, "* Map, formatted as an opening `{`, a comma-separated list of PARAM=VALUE\n");
1304 fprintf(fp
, " assignments and a closing `}`.\n");
1306 fprintf(fp
, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1308 fprintf(fp
, "Example:\n");
1310 fprintf(fp
, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
1311 fprintf(fp
, " play=+23, observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
1312 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\",\n");
1313 fprintf(fp
, " things=[1, \"2\", 3]\n");
1315 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1316 fprintf(fp
, "babeltrace2 from a shell.\n");
1320 bool help_option_is_specified(
1321 const struct bt_argpar_parse_ret
*argpar_parse_ret
)
1324 bool specified
= false;
1326 for (i
= 0; i
< argpar_parse_ret
->items
->len
; i
++) {
1327 struct bt_argpar_item
*argpar_item
=
1328 g_ptr_array_index(argpar_parse_ret
->items
, i
);
1329 struct bt_argpar_item_opt
*argpar_item_opt
;
1331 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
1335 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
1336 if (argpar_item_opt
->descr
->id
== OPT_HELP
) {
1346 * Prints the help command usage.
1349 void print_help_usage(FILE *fp
)
1351 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1352 fprintf(fp
, " babeltrace2 [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
1354 fprintf(fp
, "Options:\n");
1356 fprintf(fp
, " -h, --help Show this help and quit\n");
1358 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1360 fprintf(fp
, "Use `babeltrace2 list-plugins` to show the list of available plugins.\n");
1364 const struct bt_argpar_opt_descr help_options
[] = {
1365 /* id, short_name, long_name, with_arg */
1366 { OPT_HELP
, 'h', "help", false },
1367 BT_ARGPAR_OPT_DESCR_SENTINEL
1371 * Creates a Babeltrace config object from the arguments of a help
1374 * *retcode is set to the appropriate exit code to use.
1377 struct bt_config
*bt_config_help_from_args(int argc
, const char *argv
[],
1378 int *retcode
, const bt_value
*plugin_paths
,
1379 int default_log_level
)
1381 struct bt_config
*cfg
= NULL
;
1382 char *plugin_name
= NULL
, *comp_cls_name
= NULL
;
1383 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1384 struct bt_argpar_item_non_opt
*non_opt
;
1387 cfg
= bt_config_help_create(plugin_paths
, default_log_level
);
1393 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, help_options
, true);
1394 if (argpar_parse_ret
.error
) {
1395 BT_CLI_LOGE_APPEND_CAUSE(
1396 "While parsing `help` command's command-line arguments: %s",
1397 argpar_parse_ret
.error
->str
);
1401 if (help_option_is_specified(&argpar_parse_ret
)) {
1402 print_help_usage(stdout
);
1404 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1408 if (argpar_parse_ret
.items
->len
== 0) {
1409 BT_CLI_LOGE_APPEND_CAUSE(
1410 "Missing plugin name or component class descriptor.");
1412 } else if (argpar_parse_ret
.items
->len
> 1) {
1414 * At this point we know there are least two non-option
1415 * arguments because we don't reach here with `--help`,
1418 non_opt
= argpar_parse_ret
.items
->pdata
[1];
1419 BT_CLI_LOGE_APPEND_CAUSE(
1420 "Extraneous command-line argument specified to `help` command: `%s`.",
1425 non_opt
= argpar_parse_ret
.items
->pdata
[0];
1426 plugin_comp_cls_names(non_opt
->arg
, NULL
, &plugin_name
, &comp_cls_name
,
1427 &cfg
->cmd_data
.help
.cfg_component
->type
);
1428 if (plugin_name
&& comp_cls_name
) {
1429 /* Component class help */
1430 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1432 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
,
1435 /* Fall back to plugin help */
1436 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1444 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1447 g_free(plugin_name
);
1448 g_free(comp_cls_name
);
1450 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1456 * Prints the help command usage.
1459 void print_query_usage(FILE *fp
)
1461 fprintf(fp
, "Usage: babeltrace2 [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
1463 fprintf(fp
, "Options:\n");
1465 fprintf(fp
, " -p, --params=PARAMS Set the query parameters to PARAMS (see the expected\n");
1466 fprintf(fp
, " format of PARAMS below)\n");
1467 fprintf(fp
, " -h, --help Show this help and quit\n");
1468 fprintf(fp
, "\n\n");
1469 print_expected_params_format(fp
);
1473 const struct bt_argpar_opt_descr query_options
[] = {
1474 /* id, short_name, long_name, with_arg */
1475 { OPT_HELP
, 'h', "help", false },
1476 { OPT_PARAMS
, 'p', "params", true },
1477 BT_ARGPAR_OPT_DESCR_SENTINEL
1481 * Creates a Babeltrace config object from the arguments of a query
1484 * *retcode is set to the appropriate exit code to use.
1487 struct bt_config
*bt_config_query_from_args(int argc
, const char *argv
[],
1488 int *retcode
, const bt_value
*plugin_paths
,
1489 int default_log_level
)
1492 struct bt_config
*cfg
= NULL
;
1493 const char *component_class_spec
= NULL
;
1494 const char *query_object
= NULL
;
1496 GString
*error_str
= NULL
;
1497 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1499 params
= bt_value_null
;
1500 bt_value_get_ref(bt_value_null
);
1503 cfg
= bt_config_query_create(plugin_paths
);
1508 error_str
= g_string_new(NULL
);
1510 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1515 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, query_options
, true);
1516 if (argpar_parse_ret
.error
) {
1517 BT_CLI_LOGE_APPEND_CAUSE(
1518 "While parsing `query` command's command-line arguments: %s",
1519 argpar_parse_ret
.error
->str
);
1523 if (help_option_is_specified(&argpar_parse_ret
)) {
1524 print_query_usage(stdout
);
1526 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1530 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
1531 struct bt_argpar_item
*argpar_item
=
1532 g_ptr_array_index(argpar_parse_ret
.items
, i
);
1534 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
1535 struct bt_argpar_item_opt
*argpar_item_opt
=
1536 (struct bt_argpar_item_opt
*) argpar_item
;
1537 const char *arg
= argpar_item_opt
->arg
;
1539 switch (argpar_item_opt
->descr
->id
) {
1542 bt_value_put_ref(params
);
1543 params
= cli_value_from_arg(arg
, error_str
);
1545 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1552 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1553 argpar_item_opt
->descr
->id
);
1557 struct bt_argpar_item_non_opt
*argpar_item_non_opt
1558 = (struct bt_argpar_item_non_opt
*) argpar_item
;
1561 * We need exactly two non-option arguments
1562 * which are the mandatory component class
1563 * specification and query object.
1565 if (!component_class_spec
) {
1566 component_class_spec
= argpar_item_non_opt
->arg
;
1567 } else if (!query_object
) {
1568 query_object
= argpar_item_non_opt
->arg
;
1570 BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `query` command: `%s`.",
1571 argpar_item_non_opt
->arg
);
1577 if (!component_class_spec
|| !query_object
) {
1578 print_query_usage(stdout
);
1580 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1584 cfg
->cmd_data
.query
.cfg_component
=
1585 bt_config_component_from_arg(component_class_spec
,
1587 if (!cfg
->cmd_data
.query
.cfg_component
) {
1588 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for component class specification:\n %s",
1589 component_class_spec
);
1594 BT_OBJECT_MOVE_REF(cfg
->cmd_data
.query
.cfg_component
->params
, params
);
1596 if (strlen(query_object
) == 0) {
1597 BT_CLI_LOGE_APPEND_CAUSE("Invalid empty object.");
1601 g_string_assign(cfg
->cmd_data
.query
.object
, query_object
);
1606 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1609 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1612 g_string_free(error_str
, TRUE
);
1615 bt_value_put_ref(params
);
1620 * Prints the list-plugins command usage.
1623 void print_list_plugins_usage(FILE *fp
)
1625 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
1627 fprintf(fp
, "Options:\n");
1629 fprintf(fp
, " -h, --help Show this help and quit\n");
1631 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1633 fprintf(fp
, "Use `babeltrace2 help` to get help for a specific plugin or component class.\n");
1637 const struct bt_argpar_opt_descr list_plugins_options
[] = {
1638 /* id, short_name, long_name, with_arg */
1639 { OPT_HELP
, 'h', "help", false },
1640 BT_ARGPAR_OPT_DESCR_SENTINEL
1644 * Creates a Babeltrace config object from the arguments of a
1645 * list-plugins command.
1647 * *retcode is set to the appropriate exit code to use.
1650 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
1651 int *retcode
, const bt_value
*plugin_paths
)
1653 struct bt_config
*cfg
= NULL
;
1654 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1657 cfg
= bt_config_list_plugins_create(plugin_paths
);
1663 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, list_plugins_options
, true);
1664 if (argpar_parse_ret
.error
) {
1665 BT_CLI_LOGE_APPEND_CAUSE(
1666 "While parsing `list-plugins` command's command-line arguments: %s",
1667 argpar_parse_ret
.error
->str
);
1671 if (help_option_is_specified(&argpar_parse_ret
)) {
1672 print_list_plugins_usage(stdout
);
1674 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1678 if (argpar_parse_ret
.items
->len
> 0) {
1680 * At this point we know there's at least one non-option
1681 * argument because we don't reach here with `--help`,
1684 struct bt_argpar_item_non_opt
*non_opt
=
1685 argpar_parse_ret
.items
->pdata
[0];
1687 BT_CLI_LOGE_APPEND_CAUSE(
1688 "Extraneous command-line argument specified to `list-plugins` command: `%s`.",
1697 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1700 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1706 * Prints the run command usage.
1709 void print_run_usage(FILE *fp
)
1711 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n");
1713 fprintf(fp
, "Options:\n");
1715 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
1716 fprintf(fp
, " for all the following components until\n");
1717 fprintf(fp
, " --reset-base-params is encountered\n");
1718 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1719 fprintf(fp
, " -c, --component=NAME:TYPE.PLUGIN.CLS\n");
1720 fprintf(fp
, " Instantiate the component class CLS of type\n");
1721 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
1722 fprintf(fp
, " in the plugin PLUGIN, add it to the graph,\n");
1723 fprintf(fp
, " and name it NAME");
1724 fprintf(fp
, " -x, --connect=CONNECTION Connect two created components (see the\n");
1725 fprintf(fp
, " expected format of CONNECTION below)\n");
1726 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
1727 fprintf(fp
, " (`N`, `T`, `D`, `I`, `W`, `E`, or `F`)\n");
1728 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
1729 fprintf(fp
, " current component (see the expected format\n");
1730 fprintf(fp
, " of PARAMS below)\n");
1731 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters to an\n");
1732 fprintf(fp
, " empty map\n");
1733 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
1734 fprintf(fp
, " the graph later, retry in DUR µs\n");
1735 fprintf(fp
, " (default: 100000)\n");
1736 fprintf(fp
, " -h, --help Show this help and quit\n");
1738 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1739 fprintf(fp
, "\n\n");
1740 fprintf(fp
, "Expected format of CONNECTION\n");
1741 fprintf(fp
, "-----------------------------\n");
1743 fprintf(fp
, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
1745 fprintf(fp
, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
1746 fprintf(fp
, "components to connect together. You must escape the following characters\n\n");
1747 fprintf(fp
, "with `\\`: `\\`, `.`, and `:`. You must set the name of the current\n");
1748 fprintf(fp
, "component using the NAME prefix of the --component option.\n");
1750 fprintf(fp
, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
1751 fprintf(fp
, "identify the upstream and downstream ports to use for the connection.\n");
1752 fprintf(fp
, "When the port is not specified, `*` is used.\n");
1754 fprintf(fp
, "When a component named UPSTREAM has an available port which matches the\n");
1755 fprintf(fp
, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
1756 fprintf(fp
, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
1757 fprintf(fp
, "DOWNSTREAM.\n");
1759 fprintf(fp
, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
1760 fprintf(fp
, "which matches anything. You must escape the following characters\n");
1761 fprintf(fp
, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
1763 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
1764 fprintf(fp
, "can connect a filter component to a sink component.\n");
1766 fprintf(fp
, "Examples:\n");
1768 fprintf(fp
, " my-src:my-sink\n");
1769 fprintf(fp
, " ctf-fs.*stream*:utils-muxer:*\n");
1771 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1772 fprintf(fp
, "babeltrace2 from a shell.\n");
1773 fprintf(fp
, "\n\n");
1774 print_expected_params_format(fp
);
1778 * Creates a Babeltrace config object from the arguments of a run
1781 * *retcode is set to the appropriate exit code to use.
1784 struct bt_config
*bt_config_run_from_args(int argc
, const char *argv
[],
1785 int *retcode
, const bt_value
*plugin_paths
,
1786 int default_log_level
)
1788 struct bt_config_component
*cur_cfg_comp
= NULL
;
1789 bt_value
*cur_base_params
= NULL
;
1791 struct bt_config
*cfg
= NULL
;
1792 bt_value
*instance_names
= NULL
;
1793 bt_value
*connection_args
= NULL
;
1794 char error_buf
[256] = { 0 };
1795 long retry_duration
= -1;
1796 bt_value_map_extend_status extend_status
;
1797 GString
*error_str
= NULL
;
1798 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1801 static const struct bt_argpar_opt_descr run_options
[] = {
1802 { OPT_BASE_PARAMS
, 'b', "base-params", true },
1803 { OPT_COMPONENT
, 'c', "component", true },
1804 { OPT_CONNECT
, 'x', "connect", true },
1805 { OPT_HELP
, 'h', "help", false },
1806 { OPT_LOG_LEVEL
, 'l', "log-level", true },
1807 { OPT_PARAMS
, 'p', "params", true },
1808 { OPT_RESET_BASE_PARAMS
, 'r', "reset-base-params", false },
1809 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
1810 BT_ARGPAR_OPT_DESCR_SENTINEL
1815 error_str
= g_string_new(NULL
);
1817 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1822 print_run_usage(stdout
);
1827 cfg
= bt_config_run_create(plugin_paths
);
1832 cfg
->cmd_data
.run
.retry_duration_us
= 100000;
1833 cur_base_params
= bt_value_map_create();
1834 if (!cur_base_params
) {
1835 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1839 instance_names
= bt_value_map_create();
1840 if (!instance_names
) {
1841 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1845 connection_args
= bt_value_array_create();
1846 if (!connection_args
) {
1847 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1852 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, run_options
, true);
1853 if (argpar_parse_ret
.error
) {
1854 BT_CLI_LOGE_APPEND_CAUSE(
1855 "While parsing `run` command's command-line arguments: %s",
1856 argpar_parse_ret
.error
->str
);
1860 if (help_option_is_specified(&argpar_parse_ret
)) {
1861 print_run_usage(stdout
);
1863 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1867 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
1868 struct bt_argpar_item
*argpar_item
=
1869 g_ptr_array_index(argpar_parse_ret
.items
, i
);
1870 struct bt_argpar_item_opt
*argpar_item_opt
;
1873 /* This command does not accept non-option arguments.*/
1874 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
1875 struct bt_argpar_item_non_opt
*argpar_nonopt_item
=
1876 (struct bt_argpar_item_non_opt
*) argpar_item
;
1878 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`",
1879 argpar_nonopt_item
->arg
);
1883 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
1884 arg
= argpar_item_opt
->arg
;
1886 switch (argpar_item_opt
->descr
->id
) {
1889 enum bt_config_component_dest dest
;
1891 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
1892 cur_cfg_comp
= bt_config_component_from_arg(arg
,
1894 if (!cur_cfg_comp
) {
1895 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --component option's argument:\n %s",
1900 switch (cur_cfg_comp
->type
) {
1901 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1902 dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
1904 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1905 dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
1907 case BT_COMPONENT_CLASS_TYPE_SINK
:
1908 dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
1914 BT_ASSERT(cur_base_params
);
1915 bt_value_put_ref(cur_cfg_comp
->params
);
1916 if (bt_value_copy(cur_base_params
,
1917 &cur_cfg_comp
->params
) < 0) {
1918 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1922 ret
= add_run_cfg_comp_check_name(cfg
,
1934 bt_value
*params_to_set
;
1936 if (!cur_cfg_comp
) {
1937 BT_CLI_LOGE_APPEND_CAUSE("Cannot add parameters to unavailable component:\n %s",
1942 params
= cli_value_from_arg(arg
, error_str
);
1944 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1949 extend_status
= bt_value_map_extend(
1950 cur_cfg_comp
->params
, params
, ¶ms_to_set
);
1951 BT_VALUE_PUT_REF_AND_RESET(params
);
1952 if (extend_status
!= BT_VALUE_MAP_EXTEND_STATUS_OK
) {
1953 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current component parameters with --params option's argument:\n %s",
1958 BT_OBJECT_MOVE_REF(cur_cfg_comp
->params
, params_to_set
);
1962 if (!cur_cfg_comp
) {
1963 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the log level of unavailable component:\n %s",
1968 cur_cfg_comp
->log_level
=
1969 bt_log_get_level_from_string(arg
);
1970 if (cur_cfg_comp
->log_level
< 0) {
1971 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
1976 case OPT_BASE_PARAMS
:
1978 bt_value
*params
= cli_value_from_arg(arg
, error_str
);
1981 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --base-params option's argument:\n %s",
1986 BT_OBJECT_MOVE_REF(cur_base_params
, params
);
1989 case OPT_RESET_BASE_PARAMS
:
1990 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
1991 cur_base_params
= bt_value_map_create();
1992 if (!cur_base_params
) {
1993 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1998 if (bt_value_array_append_string_element(
1999 connection_args
, arg
)) {
2000 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2004 case OPT_RETRY_DURATION
: {
2006 size_t arg_len
= strlen(argpar_item_opt
->arg
);
2008 retry_duration
= g_ascii_strtoll(argpar_item_opt
->arg
, &end
, 10);
2010 if (arg_len
== 0 || end
!= (argpar_item_opt
->arg
+ arg_len
)) {
2011 BT_CLI_LOGE_APPEND_CAUSE(
2012 "Could not parse --retry-duration option's argument as an unsigned integer: `%s`",
2013 argpar_item_opt
->arg
);
2017 if (retry_duration
< 0) {
2018 BT_CLI_LOGE_APPEND_CAUSE("--retry-duration option's argument must be positive or 0: %ld",
2023 cfg
->cmd_data
.run
.retry_duration_us
=
2024 (uint64_t) retry_duration
;
2028 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
2029 argpar_item_opt
->descr
->id
);
2034 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2036 if (cfg
->cmd_data
.run
.sources
->len
== 0) {
2037 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no source component.");
2041 if (cfg
->cmd_data
.run
.sinks
->len
== 0) {
2042 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no sink component.");
2046 ret
= bt_config_cli_args_create_connections(cfg
,
2050 BT_CLI_LOGE_APPEND_CAUSE("Cannot creation connections:\n%s", error_buf
);
2058 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2062 g_string_free(error_str
, TRUE
);
2065 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
2066 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2067 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2068 BT_VALUE_PUT_REF_AND_RESET(instance_names
);
2069 BT_VALUE_PUT_REF_AND_RESET(connection_args
);
2074 struct bt_config
*bt_config_run_from_args_array(const bt_value
*run_args
,
2075 int *retcode
, const bt_value
*plugin_paths
,
2076 int default_log_level
)
2078 struct bt_config
*cfg
= NULL
;
2081 const size_t argc
= bt_value_array_get_length(run_args
);
2083 argv
= calloc(argc
, sizeof(*argv
));
2085 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2089 len
= bt_value_array_get_length(run_args
);
2091 BT_CLI_LOGE_APPEND_CAUSE("Invalid executable arguments.");
2094 for (i
= 0; i
< len
; i
++) {
2095 const bt_value
*arg_value
=
2096 bt_value_array_borrow_element_by_index_const(run_args
,
2100 BT_ASSERT(arg_value
);
2101 arg
= bt_value_string_get(arg_value
);
2106 cfg
= bt_config_run_from_args(argc
, argv
, retcode
,
2107 plugin_paths
, default_log_level
);
2115 * Prints the convert command usage.
2118 void print_convert_usage(FILE *fp
)
2120 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2122 fprintf(fp
, "Options:\n");
2124 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2125 fprintf(fp
, " Instantiate the component class CLS of type\n");
2126 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2127 fprintf(fp
, " in the plugin PLUGIN, add it to the\n");
2128 fprintf(fp
, " conversion graph, and optionally name it\n");
2129 fprintf(fp
, " NAME\n");
2130 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
2131 fprintf(fp
, " (`N`, `T`, `D`, `I`, `W`, `E`, or `F`)\n");
2132 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2133 fprintf(fp
, " current component (see the expected format\n");
2134 fprintf(fp
, " of PARAMS below)\n");
2135 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
2136 fprintf(fp
, " the graph later, retry in DUR µs\n");
2137 fprintf(fp
, " (default: 100000)\n");
2138 fprintf(fp
, " dynamic plugins can be loaded\n");
2139 fprintf(fp
, " --run-args Print the equivalent arguments for the\n");
2140 fprintf(fp
, " `run` command to the standard output,\n");
2141 fprintf(fp
, " formatted for a shell, and quit\n");
2142 fprintf(fp
, " --run-args-0 Print the equivalent arguments for the\n");
2143 fprintf(fp
, " `run` command to the standard output,\n");
2144 fprintf(fp
, " formatted for `xargs -0`, and quit\n");
2145 fprintf(fp
, " --stream-intersection Only process events when all streams\n");
2146 fprintf(fp
, " are active\n");
2147 fprintf(fp
, " -h, --help Show this help and quit\n");
2149 fprintf(fp
, "Implicit `source.ctf.fs` component options:\n");
2151 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2152 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2154 fprintf(fp
, "Implicit `sink.text.pretty` component options:\n");
2156 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
2157 fprintf(fp
, " --clock-date Print timestamp dates\n");
2158 fprintf(fp
, " --clock-gmt Print and parse timestamps in the GMT\n");
2159 fprintf(fp
, " time zone instead of the local time zone\n");
2160 fprintf(fp
, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2161 fprintf(fp
, " of `hh:mm:ss.nnnnnnnnn`\n");
2162 fprintf(fp
, " --color=(never | auto | always)\n");
2163 fprintf(fp
, " Never, automatically, or always emit\n");
2164 fprintf(fp
, " console color codes\n");
2165 fprintf(fp
, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2166 fprintf(fp
, " `all`, `trace`, `trace:hostname`,\n");
2167 fprintf(fp
, " `trace:domain`, `trace:procname`,\n");
2168 fprintf(fp
, " `trace:vpid`, `loglevel`, `emf`\n");
2169 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2170 fprintf(fp
, " `payload` (or `arg` or `args`), `none`,\n");
2171 fprintf(fp
, " `all`, `scope`, `header`, `context`\n");
2172 fprintf(fp
, " (or `ctx`)\n");
2173 fprintf(fp
, " --no-delta Do not print time delta between\n");
2174 fprintf(fp
, " consecutive events\n");
2175 fprintf(fp
, " -w, --output=PATH Write output text to PATH instead of\n");
2176 fprintf(fp
, " the standard output\n");
2178 fprintf(fp
, "Implicit `filter.utils.muxer` component options:\n");
2180 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently\n");
2181 fprintf(fp
, " correlated across traces\n");
2183 fprintf(fp
, "Implicit `filter.utils.trimmer` component options:\n");
2185 fprintf(fp
, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2186 fprintf(fp
, " time range to BEGIN (see the format of\n");
2187 fprintf(fp
, " BEGIN below)\n");
2188 fprintf(fp
, " -e, --end=END Set the end time of the conversion time\n");
2189 fprintf(fp
, " range to END (see the format of END below)\n");
2190 fprintf(fp
, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2191 fprintf(fp
, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2192 fprintf(fp
, " `]`) (see the format of BEGIN/END below)\n");
2194 fprintf(fp
, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2196 fprintf(fp
, " --debug-info Create an implicit\n");
2197 fprintf(fp
, " `filter.lttng-utils.debug-info` component\n");
2198 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2199 fprintf(fp
, " instead of `/usr/lib/debug`\n");
2200 fprintf(fp
, " --debug-info-full-path Show full debug info source and\n");
2201 fprintf(fp
, " binary paths instead of just names\n");
2202 fprintf(fp
, " --debug-info-target-prefix=DIR\n");
2203 fprintf(fp
, " Use directory DIR as a prefix when\n");
2204 fprintf(fp
, " looking up executables during debug\n");
2205 fprintf(fp
, " info analysis\n");
2207 fprintf(fp
, "Legacy options that still work:\n");
2209 fprintf(fp
, " -i, --input-format=(ctf | lttng-live)\n");
2210 fprintf(fp
, " `ctf`:\n");
2211 fprintf(fp
, " Create an implicit `source.ctf.fs`\n");
2212 fprintf(fp
, " component\n");
2213 fprintf(fp
, " `lttng-live`:\n");
2214 fprintf(fp
, " Create an implicit `source.ctf.lttng-live`\n");
2215 fprintf(fp
, " component\n");
2216 fprintf(fp
, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2217 fprintf(fp
, " `text`:\n");
2218 fprintf(fp
, " Create an implicit `sink.text.pretty`\n");
2219 fprintf(fp
, " component\n");
2220 fprintf(fp
, " `ctf`:\n");
2221 fprintf(fp
, " Create an implicit `sink.ctf.fs`\n");
2222 fprintf(fp
, " component\n");
2223 fprintf(fp
, " `dummy`:\n");
2224 fprintf(fp
, " Create an implicit `sink.utils.dummy`\n");
2225 fprintf(fp
, " component\n");
2226 fprintf(fp
, " `ctf-metadata`:\n");
2227 fprintf(fp
, " Query the `source.ctf.fs` component class\n");
2228 fprintf(fp
, " for metadata text and quit\n");
2230 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
2231 fprintf(fp
, "\n\n");
2232 fprintf(fp
, "Format of BEGIN and END\n");
2233 fprintf(fp
, "-----------------------\n");
2235 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2236 fprintf(fp
, "\n\n");
2237 print_expected_params_format(fp
);
2241 const struct bt_argpar_opt_descr convert_options
[] = {
2242 /* id, short_name, long_name, with_arg */
2243 { OPT_BEGIN
, 'b', "begin", true },
2244 { OPT_CLOCK_CYCLES
, '\0', "clock-cycles", false },
2245 { OPT_CLOCK_DATE
, '\0', "clock-date", false },
2246 { OPT_CLOCK_FORCE_CORRELATE
, '\0', "clock-force-correlate", false },
2247 { OPT_CLOCK_GMT
, '\0', "clock-gmt", false },
2248 { OPT_CLOCK_OFFSET
, '\0', "clock-offset", true },
2249 { OPT_CLOCK_OFFSET_NS
, '\0', "clock-offset-ns", true },
2250 { OPT_CLOCK_SECONDS
, '\0', "clock-seconds", false },
2251 { OPT_COLOR
, '\0', "color", true },
2252 { OPT_COMPONENT
, 'c', "component", true },
2253 { OPT_DEBUG
, 'd', "debug", false },
2254 { OPT_DEBUG_INFO_DIR
, '\0', "debug-info-dir", true },
2255 { OPT_DEBUG_INFO_FULL_PATH
, '\0', "debug-info-full-path", false },
2256 { OPT_DEBUG_INFO_TARGET_PREFIX
, '\0', "debug-info-target-prefix", true },
2257 { OPT_END
, 'e', "end", true },
2258 { OPT_FIELDS
, 'f', "fields", true },
2259 { OPT_HELP
, 'h', "help", false },
2260 { OPT_INPUT_FORMAT
, 'i', "input-format", true },
2261 { OPT_LOG_LEVEL
, 'l', "log-level", true },
2262 { OPT_NAMES
, 'n', "names", true },
2263 { OPT_DEBUG_INFO
, '\0', "debug-info", false },
2264 { OPT_NO_DELTA
, '\0', "no-delta", false },
2265 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
2266 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
2267 { OPT_OUTPUT
, 'w', "output", true },
2268 { OPT_OUTPUT_FORMAT
, 'o', "output-format", true },
2269 { OPT_PARAMS
, 'p', "params", true },
2270 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
2271 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
2272 { OPT_RUN_ARGS
, '\0', "run-args", false },
2273 { OPT_RUN_ARGS_0
, '\0', "run-args-0", false },
2274 { OPT_STREAM_INTERSECTION
, '\0', "stream-intersection", false },
2275 { OPT_TIMERANGE
, '\0', "timerange", true },
2276 { OPT_VERBOSE
, 'v', "verbose", false },
2277 BT_ARGPAR_OPT_DESCR_SENTINEL
2281 GString
*get_component_auto_name(const char *prefix
,
2282 const bt_value
*existing_names
)
2285 GString
*auto_name
= g_string_new(NULL
);
2288 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2292 if (!bt_value_map_has_entry(existing_names
, prefix
)) {
2293 g_string_assign(auto_name
, prefix
);
2298 g_string_printf(auto_name
, "%s-%d", prefix
, i
);
2300 } while (bt_value_map_has_entry(existing_names
, auto_name
->str
));
2306 struct implicit_component_args
{
2309 /* The component class name (e.g. src.ctf.fs). */
2312 /* The component instance name. */
2315 GString
*params_arg
;
2316 bt_value
*extra_params
;
2320 int assign_name_to_implicit_component(struct implicit_component_args
*args
,
2321 const char *prefix
, bt_value
*existing_names
,
2322 GList
**comp_names
, bool append_to_comp_names
)
2325 GString
*name
= NULL
;
2327 if (!args
->exists
) {
2331 name
= get_component_auto_name(prefix
,
2339 g_string_assign(args
->name_arg
, name
->str
);
2341 if (bt_value_map_insert_entry(existing_names
, name
->str
,
2343 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2348 if (append_to_comp_names
) {
2349 *comp_names
= g_list_append(*comp_names
, name
);
2355 g_string_free(name
, TRUE
);
2362 int append_run_args_for_implicit_component(
2363 struct implicit_component_args
*impl_args
,
2368 GString
*component_arg_for_run
= NULL
;
2370 if (!impl_args
->exists
) {
2374 component_arg_for_run
= g_string_new(NULL
);
2375 if (!component_arg_for_run
) {
2376 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2380 /* Build the full `name:type.plugin.cls`. */
2381 BT_ASSERT(!strchr(impl_args
->name_arg
->str
, '\\'));
2382 BT_ASSERT(!strchr(impl_args
->name_arg
->str
, ':'));
2383 g_string_printf(component_arg_for_run
, "%s:%s",
2384 impl_args
->name_arg
->str
, impl_args
->comp_arg
->str
);
2386 if (bt_value_array_append_string_element(run_args
, "--component")) {
2387 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2391 if (bt_value_array_append_string_element(run_args
,
2392 component_arg_for_run
->str
)) {
2393 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2397 if (impl_args
->params_arg
->len
> 0) {
2398 if (bt_value_array_append_string_element(run_args
, "--params")) {
2399 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2403 if (bt_value_array_append_string_element(run_args
,
2404 impl_args
->params_arg
->str
)) {
2405 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2410 for (i
= 0; i
< bt_value_array_get_length(impl_args
->extra_params
);
2412 const bt_value
*elem
;
2415 elem
= bt_value_array_borrow_element_by_index(impl_args
->extra_params
,
2421 BT_ASSERT(bt_value_is_string(elem
));
2422 arg
= bt_value_string_get(elem
);
2423 ret
= bt_value_array_append_string_element(run_args
, arg
);
2425 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2436 if (component_arg_for_run
) {
2437 g_string_free(component_arg_for_run
, TRUE
);
2443 /* Free the fields of a `struct implicit_component_args`. */
2446 void finalize_implicit_component_args(struct implicit_component_args
*args
)
2450 if (args
->comp_arg
) {
2451 g_string_free(args
->comp_arg
, TRUE
);
2454 if (args
->name_arg
) {
2455 g_string_free(args
->name_arg
, TRUE
);
2458 if (args
->params_arg
) {
2459 g_string_free(args
->params_arg
, TRUE
);
2462 bt_value_put_ref(args
->extra_params
);
2465 /* Destroy a dynamically-allocated `struct implicit_component_args`. */
2468 void destroy_implicit_component_args(struct implicit_component_args
*args
)
2470 finalize_implicit_component_args(args
);
2474 /* Initialize the fields of an already allocated `struct implicit_component_args`. */
2477 int init_implicit_component_args(struct implicit_component_args
*args
,
2478 const char *comp_arg
, bool exists
)
2482 args
->exists
= exists
;
2483 args
->comp_arg
= g_string_new(comp_arg
);
2484 args
->name_arg
= g_string_new(NULL
);
2485 args
->params_arg
= g_string_new(NULL
);
2486 args
->extra_params
= bt_value_array_create();
2488 if (!args
->comp_arg
|| !args
->name_arg
||
2489 !args
->params_arg
|| !args
->extra_params
) {
2491 finalize_implicit_component_args(args
);
2492 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2500 /* Dynamically allocate and initialize a `struct implicit_component_args`. */
2503 struct implicit_component_args
*create_implicit_component_args(
2504 const char *comp_arg
)
2506 struct implicit_component_args
*args
;
2509 args
= g_new(struct implicit_component_args
, 1);
2511 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2515 status
= init_implicit_component_args(args
, comp_arg
, true);
2526 void append_implicit_component_param(struct implicit_component_args
*args
,
2527 const char *key
, const char *value
)
2532 append_param_arg(args
->params_arg
, key
, value
);
2536 * Append the given parameter (`key=value`) to all component specifications
2537 * in `implicit_comp_args` (an array of `struct implicit_component_args *`)
2538 * which match `comp_arg`.
2540 * Return the number of matching components.
2544 int append_multiple_implicit_components_param(GPtrArray
*implicit_comp_args
,
2545 const char *comp_arg
, const char *key
, const char *value
)
2550 for (i
= 0; i
< implicit_comp_args
->len
; i
++) {
2551 struct implicit_component_args
*args
= implicit_comp_args
->pdata
[i
];
2553 if (strcmp(args
->comp_arg
->str
, comp_arg
) == 0) {
2554 append_implicit_component_param(args
, key
, value
);
2562 /* Escape value to make it suitable to use as a string parameter value. */
2564 gchar
*escape_string_value(const char *value
)
2569 ret
= g_string_new(NULL
);
2571 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2580 g_string_append_c(ret
, '\\');
2584 g_string_append_c(ret
, *in
);
2590 return g_string_free(ret
, FALSE
);
2594 int bt_value_to_cli_param_value_append(const bt_value
*value
, GString
*buf
)
2600 switch (bt_value_get_type(value
)) {
2601 case BT_VALUE_TYPE_STRING
:
2603 const char *str_value
= bt_value_string_get(value
);
2604 gchar
*escaped_str_value
;
2606 escaped_str_value
= escape_string_value(str_value
);
2607 if (!escaped_str_value
) {
2611 g_string_append_printf(buf
, "\"%s\"", escaped_str_value
);
2613 g_free(escaped_str_value
);
2616 case BT_VALUE_TYPE_ARRAY
: {
2617 g_string_append_c(buf
, '[');
2618 uint64_t sz
= bt_value_array_get_length(value
);
2619 for (uint64_t i
= 0; i
< sz
; i
++) {
2620 const bt_value
*item
;
2624 g_string_append(buf
, ", ");
2627 item
= bt_value_array_borrow_element_by_index_const(
2629 ret
= bt_value_to_cli_param_value_append(item
, buf
);
2635 g_string_append_c(buf
, ']');
2649 * Convert `value` to its equivalent representation as a command line parameter
2654 gchar
*bt_value_to_cli_param_value(bt_value
*value
)
2657 gchar
*result
= NULL
;
2659 buf
= g_string_new(NULL
);
2661 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2665 if (bt_value_to_cli_param_value_append(value
, buf
)) {
2669 result
= g_string_free(buf
, FALSE
);
2676 g_string_free(buf
, TRUE
);
2684 int append_parameter_to_args(bt_value
*args
, const char *key
, bt_value
*value
)
2687 BT_ASSERT(bt_value_get_type(args
) == BT_VALUE_TYPE_ARRAY
);
2692 gchar
*str_value
= NULL
;
2693 GString
*parameter
= NULL
;
2695 if (bt_value_array_append_string_element(args
, "--params")) {
2696 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2701 str_value
= bt_value_to_cli_param_value(value
);
2707 parameter
= g_string_new(NULL
);
2709 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2714 g_string_printf(parameter
, "%s=%s", key
, str_value
);
2716 if (bt_value_array_append_string_element(args
, parameter
->str
)) {
2717 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2724 g_string_free(parameter
, TRUE
);
2737 int append_string_parameter_to_args(bt_value
*args
, const char *key
, const char *value
)
2739 bt_value
*str_value
;
2742 str_value
= bt_value_string_create_init(value
);
2745 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2750 ret
= append_parameter_to_args(args
, key
, str_value
);
2753 BT_VALUE_PUT_REF_AND_RESET(str_value
);
2758 int append_implicit_component_extra_param(struct implicit_component_args
*args
,
2759 const char *key
, const char *value
)
2761 return append_string_parameter_to_args(args
->extra_params
, key
, value
);
2765 * Escapes `.`, `:`, and `\` of `input` with `\`.
2768 GString
*escape_dot_colon(const char *input
)
2770 GString
*output
= g_string_new(NULL
);
2774 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2778 for (ch
= input
; *ch
!= '\0'; ch
++) {
2779 if (*ch
== '\\' || *ch
== '.' || *ch
== ':') {
2780 g_string_append_c(output
, '\\');
2783 g_string_append_c(output
, *ch
);
2791 * Appends a --connect option to a list of arguments. `upstream_name`
2792 * and `downstream_name` are escaped with escape_dot_colon() in this
2796 int append_connect_arg(bt_value
*run_args
,
2797 const char *upstream_name
, const char *downstream_name
)
2800 GString
*e_upstream_name
= escape_dot_colon(upstream_name
);
2801 GString
*e_downstream_name
= escape_dot_colon(downstream_name
);
2802 GString
*arg
= g_string_new(NULL
);
2804 if (!e_upstream_name
|| !e_downstream_name
|| !arg
) {
2805 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2810 ret
= bt_value_array_append_string_element(run_args
, "--connect");
2812 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2817 g_string_append(arg
, e_upstream_name
->str
);
2818 g_string_append_c(arg
, ':');
2819 g_string_append(arg
, e_downstream_name
->str
);
2820 ret
= bt_value_array_append_string_element(run_args
, arg
->str
);
2822 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2829 g_string_free(arg
, TRUE
);
2832 if (e_upstream_name
) {
2833 g_string_free(e_upstream_name
, TRUE
);
2836 if (e_downstream_name
) {
2837 g_string_free(e_downstream_name
, TRUE
);
2844 * Appends the run command's --connect options for the convert command.
2847 int convert_auto_connect(bt_value
*run_args
,
2848 GList
*source_names
, GList
*filter_names
,
2852 GList
*source_at
= source_names
;
2853 GList
*filter_at
= filter_names
;
2855 GList
*sink_at
= sink_names
;
2857 BT_ASSERT(source_names
);
2858 BT_ASSERT(filter_names
);
2859 BT_ASSERT(sink_names
);
2861 /* Connect all sources to the first filter */
2862 for (source_at
= source_names
; source_at
; source_at
= g_list_next(source_at
)) {
2863 GString
*source_name
= source_at
->data
;
2864 GString
*filter_name
= filter_at
->data
;
2866 ret
= append_connect_arg(run_args
, source_name
->str
,
2873 filter_prev
= filter_at
;
2874 filter_at
= g_list_next(filter_at
);
2876 /* Connect remaining filters */
2877 for (; filter_at
; filter_prev
= filter_at
, filter_at
= g_list_next(filter_at
)) {
2878 GString
*filter_name
= filter_at
->data
;
2879 GString
*filter_prev_name
= filter_prev
->data
;
2881 ret
= append_connect_arg(run_args
, filter_prev_name
->str
,
2888 /* Connect last filter to all sinks */
2889 for (sink_at
= sink_names
; sink_at
; sink_at
= g_list_next(sink_at
)) {
2890 GString
*filter_name
= filter_prev
->data
;
2891 GString
*sink_name
= sink_at
->data
;
2893 ret
= append_connect_arg(run_args
, filter_name
->str
,
2910 int split_timerange(const char *arg
, char **begin
, char **end
)
2913 const char *ch
= arg
;
2915 GString
*g_begin
= NULL
;
2916 GString
*g_end
= NULL
;
2924 g_begin
= bt_common_string_until(ch
, "", ",", &end_pos
);
2925 if (!g_begin
|| ch
[end_pos
] != ',' || g_begin
->len
== 0) {
2931 g_end
= bt_common_string_until(ch
, "", "]", &end_pos
);
2932 if (!g_end
|| g_end
->len
== 0) {
2938 *begin
= g_begin
->str
;
2940 g_string_free(g_begin
, FALSE
);
2941 g_string_free(g_end
, FALSE
);
2951 g_string_free(g_begin
, TRUE
);
2955 g_string_free(g_end
, TRUE
);
2962 int g_list_prepend_gstring(GList
**list
, const char *string
)
2965 GString
*gs
= g_string_new(string
);
2970 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2974 *list
= g_list_prepend(*list
, gs
);
2981 * Create `struct implicit_component_args` structures for each of the
2982 * source components we identified. Add them to `component_args`.
2984 * `non_opt_params` is an array where each element is an array of
2985 * strings containing all the arguments to `--params` that apply to the
2986 * non-option argument at the same index. For example, if, for a
2987 * non-option argument, the following `--params` options applied:
2989 * --params=a=2 --params=b=3,c=4
2991 * its entry in `non_opt_params` would contain
2993 * ["a=2", "b=3,c=4"]
2997 int create_implicit_component_args_from_auto_discovered_sources(
2998 const struct auto_source_discovery
*auto_disc
,
2999 const bt_value
*non_opt_params
,
3000 const bt_value
*non_opt_loglevels
,
3001 GPtrArray
*component_args
)
3003 gchar
*cc_name
= NULL
;
3004 struct implicit_component_args
*comp
= NULL
;
3008 len
= auto_disc
->results
->len
;
3010 for (i
= 0; i
< len
; i
++) {
3011 struct auto_source_discovery_result
*res
=
3012 g_ptr_array_index(auto_disc
->results
, i
);
3013 uint64_t orig_indices_i
, orig_indices_count
;
3016 cc_name
= g_strdup_printf("source.%s.%s", res
->plugin_name
, res
->source_cc_name
);
3018 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3022 comp
= create_implicit_component_args(cc_name
);
3028 * Append parameters and log levels of all the
3029 * non-option arguments that contributed to this
3030 * component instance coming into existence.
3032 orig_indices_count
= bt_value_array_get_length(res
->original_input_indices
);
3033 for (orig_indices_i
= 0; orig_indices_i
< orig_indices_count
; orig_indices_i
++) {
3034 const bt_value
*orig_idx_value
=
3035 bt_value_array_borrow_element_by_index(
3036 res
->original_input_indices
, orig_indices_i
);
3037 uint64_t orig_idx
= bt_value_integer_unsigned_get(orig_idx_value
);
3038 const bt_value
*params_array
=
3039 bt_value_array_borrow_element_by_index_const(
3040 non_opt_params
, orig_idx
);
3041 uint64_t params_i
, params_count
;
3042 const bt_value
*loglevel_value
;
3044 params_count
= bt_value_array_get_length(params_array
);
3045 for (params_i
= 0; params_i
< params_count
; params_i
++) {
3046 const bt_value
*params_value
=
3047 bt_value_array_borrow_element_by_index_const(
3048 params_array
, params_i
);
3049 const char *params
= bt_value_string_get(params_value
);
3050 bt_value_array_append_element_status append_status
;
3052 append_status
= bt_value_array_append_string_element(
3053 comp
->extra_params
, "--params");
3054 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3055 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3059 append_status
= bt_value_array_append_string_element(
3060 comp
->extra_params
, params
);
3061 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3062 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3067 loglevel_value
= bt_value_array_borrow_element_by_index_const(
3068 non_opt_loglevels
, orig_idx
);
3069 if (bt_value_get_type(loglevel_value
) == BT_VALUE_TYPE_STRING
) {
3070 const char *loglevel
= bt_value_string_get(loglevel_value
);
3071 bt_value_array_append_element_status append_status
;
3073 append_status
= bt_value_array_append_string_element(
3074 comp
->extra_params
, "--log-level");
3075 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3076 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3080 append_status
= bt_value_array_append_string_element(
3081 comp
->extra_params
, loglevel
);
3082 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3083 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3089 status
= append_parameter_to_args(comp
->extra_params
, "inputs", res
->inputs
);
3094 g_ptr_array_add(component_args
, comp
);
3108 destroy_implicit_component_args(comp
);
3115 * As we iterate the arguments to the convert command, this tracks what is the
3116 * type of the current item, to which some contextual options (e.g. --params)
3119 enum convert_current_item_type
{
3120 /* There is no current item. */
3121 CONVERT_CURRENT_ITEM_TYPE_NONE
,
3123 /* Current item is a component. */
3124 CONVERT_CURRENT_ITEM_TYPE_COMPONENT
,
3126 /* Current item is a non-option argument. */
3127 CONVERT_CURRENT_ITEM_TYPE_NON_OPT
,
3131 * Creates a Babeltrace config object from the arguments of a convert
3134 * *retcode is set to the appropriate exit code to use.
3137 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3138 int *retcode
, const bt_value
*plugin_paths
,
3139 int *default_log_level
)
3141 enum convert_current_item_type current_item_type
=
3142 CONVERT_CURRENT_ITEM_TYPE_NONE
;
3144 struct bt_config
*cfg
= NULL
;
3145 bool got_input_format_opt
= false;
3146 bool got_output_format_opt
= false;
3147 bool trimmer_has_begin
= false;
3148 bool trimmer_has_end
= false;
3149 bool stream_intersection_mode
= false;
3150 bool print_run_args
= false;
3151 bool print_run_args_0
= false;
3152 bool print_ctf_metadata
= false;
3153 bt_value
*run_args
= NULL
;
3154 bt_value
*all_names
= NULL
;
3155 GList
*source_names
= NULL
;
3156 GList
*filter_names
= NULL
;
3157 GList
*sink_names
= NULL
;
3158 bt_value
*non_opts
= NULL
;
3159 bt_value
*non_opt_params
= NULL
;
3160 bt_value
*non_opt_loglevels
= NULL
;
3161 struct implicit_component_args implicit_ctf_output_args
= { 0 };
3162 struct implicit_component_args implicit_lttng_live_args
= { 0 };
3163 struct implicit_component_args implicit_dummy_args
= { 0 };
3164 struct implicit_component_args implicit_text_args
= { 0 };
3165 struct implicit_component_args implicit_debug_info_args
= { 0 };
3166 struct implicit_component_args implicit_muxer_args
= { 0 };
3167 struct implicit_component_args implicit_trimmer_args
= { 0 };
3168 char error_buf
[256] = { 0 };
3170 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
3171 char *output
= NULL
;
3172 struct auto_source_discovery auto_disc
= { NULL
};
3173 GString
*auto_disc_comp_name
= NULL
;
3174 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
3175 GString
*name_gstr
= NULL
;
3176 GString
*component_arg_for_run
= NULL
;
3177 bt_value
*live_inputs_array_val
= NULL
;
3180 * Array of `struct implicit_component_args *` created for the sources
3181 * we have auto-discovered.
3183 GPtrArray
*discovered_source_args
= NULL
;
3186 * If set, restrict automatic source discovery to this component class
3189 const char *auto_source_discovery_restrict_plugin_name
= NULL
;
3190 const char *auto_source_discovery_restrict_component_class_name
= NULL
;
3192 gchar
*ctf_fs_source_clock_class_offset_arg
= NULL
;
3193 gchar
*ctf_fs_source_clock_class_offset_ns_arg
= NULL
;
3197 print_convert_usage(stdout
);
3202 if (init_implicit_component_args(&implicit_ctf_output_args
,
3203 "sink.ctf.fs", false)) {
3207 if (init_implicit_component_args(&implicit_lttng_live_args
,
3208 "source.ctf.lttng-live", false)) {
3212 if (init_implicit_component_args(&implicit_text_args
,
3213 "sink.text.pretty", false)) {
3217 if (init_implicit_component_args(&implicit_dummy_args
,
3218 "sink.utils.dummy", false)) {
3222 if (init_implicit_component_args(&implicit_debug_info_args
,
3223 "filter.lttng-utils.debug-info", false)) {
3227 if (init_implicit_component_args(&implicit_muxer_args
,
3228 "filter.utils.muxer", true)) {
3232 if (init_implicit_component_args(&implicit_trimmer_args
,
3233 "filter.utils.trimmer", false)) {
3237 all_names
= bt_value_map_create();
3239 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3243 run_args
= bt_value_array_create();
3245 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3249 component_arg_for_run
= g_string_new(NULL
);
3250 if (!component_arg_for_run
) {
3251 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3255 non_opts
= bt_value_array_create();
3257 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3261 non_opt_params
= bt_value_array_create();
3262 if (!non_opt_params
) {
3263 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3267 non_opt_loglevels
= bt_value_array_create();
3268 if (!non_opt_loglevels
) {
3269 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3273 if (auto_source_discovery_init(&auto_disc
) != 0) {
3277 discovered_source_args
=
3278 g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_implicit_component_args
);
3279 if (!discovered_source_args
) {
3280 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3284 auto_disc_comp_name
= g_string_new(NULL
);
3285 if (!auto_disc_comp_name
) {
3286 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3291 * First pass: collect all arguments which need to be passed
3292 * as is to the run command. This pass can also add --name
3293 * arguments if needed to automatically name unnamed component
3296 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, convert_options
, true);
3297 if (argpar_parse_ret
.error
) {
3298 BT_CLI_LOGE_APPEND_CAUSE(
3299 "While parsing `convert` command's command-line arguments: %s",
3300 argpar_parse_ret
.error
->str
);
3304 if (help_option_is_specified(&argpar_parse_ret
)) {
3305 print_convert_usage(stdout
);
3307 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
3311 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
3312 struct bt_argpar_item
*argpar_item
=
3313 g_ptr_array_index(argpar_parse_ret
.items
, i
);
3314 struct bt_argpar_item_opt
*argpar_item_opt
;
3316 char *plugin_name
= NULL
;
3317 char *comp_cls_name
= NULL
;
3320 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
3321 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
3322 arg
= argpar_item_opt
->arg
;
3324 switch (argpar_item_opt
->descr
->id
) {
3327 bt_component_class_type type
;
3329 current_item_type
= CONVERT_CURRENT_ITEM_TYPE_COMPONENT
;
3331 /* Parse the argument */
3332 plugin_comp_cls_names(arg
, &name
, &plugin_name
,
3333 &comp_cls_name
, &type
);
3334 if (!plugin_name
|| !comp_cls_name
) {
3335 BT_CLI_LOGE_APPEND_CAUSE(
3336 "Invalid format for --component option's argument:\n %s",
3343 * Name was given by the user, verify it isn't
3346 if (bt_value_map_has_entry(all_names
, name
)) {
3347 BT_CLI_LOGE_APPEND_CAUSE(
3348 "Duplicate component instance name:\n %s",
3353 name_gstr
= g_string_new(name
);
3355 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3359 g_string_assign(component_arg_for_run
, arg
);
3361 /* Name not given by user, generate one. */
3362 name_gstr
= get_component_auto_name(arg
, all_names
);
3367 g_string_printf(component_arg_for_run
, "%s:%s",
3368 name_gstr
->str
, arg
);
3371 if (bt_value_array_append_string_element(run_args
,
3373 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3377 if (bt_value_array_append_string_element(run_args
,
3378 component_arg_for_run
->str
)) {
3379 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3384 * Remember this name globally, for the uniqueness of
3385 * all component names.
3387 if (bt_value_map_insert_entry(all_names
,
3388 name_gstr
->str
, bt_value_null
)) {
3389 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3394 * Remember this name specifically for the type of the
3395 * component. This is to create connection arguments.
3397 * The list takes ownership of `name_gstr`.
3400 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
3401 source_names
= g_list_append(source_names
, name_gstr
);
3403 case BT_COMPONENT_CLASS_TYPE_FILTER
:
3404 filter_names
= g_list_append(filter_names
, name_gstr
);
3406 case BT_COMPONENT_CLASS_TYPE_SINK
:
3407 sink_names
= g_list_append(sink_names
, name_gstr
);
3416 free(comp_cls_name
);
3419 comp_cls_name
= NULL
;
3423 if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_COMPONENT
) {
3425 * The current item is a component (--component option),
3426 * pass it directly to the run args.
3428 if (bt_value_array_append_string_element(run_args
,
3430 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3434 if (bt_value_array_append_string_element(run_args
, arg
)) {
3435 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3438 } else if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_NON_OPT
) {
3440 * The current item is a
3441 * non-option argument, record
3442 * it in `non_opt_params`.
3445 uint64_t idx
= bt_value_array_get_length(non_opt_params
) - 1;
3447 array
= bt_value_array_borrow_element_by_index(non_opt_params
, idx
);
3448 bt_value_array_append_string_element(array
, arg
);
3450 BT_CLI_LOGE_APPEND_CAUSE(
3451 "No current component (--component option) or non-option argument of which to set parameters:\n %s",
3457 if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_COMPONENT
) {
3458 if (bt_value_array_append_string_element(run_args
, "--log-level")) {
3459 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3463 if (bt_value_array_append_string_element(run_args
, arg
)) {
3464 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3467 } else if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_NON_OPT
) {
3468 uint64_t idx
= bt_value_array_get_length(non_opt_loglevels
) - 1;
3469 bt_value
*log_level_str_value
;
3471 log_level_str_value
= bt_value_string_create_init(arg
);
3472 if (!log_level_str_value
) {
3473 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3477 if (bt_value_array_set_element_by_index(non_opt_loglevels
, idx
,
3478 log_level_str_value
)) {
3479 bt_value_put_ref(log_level_str_value
);
3480 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3484 BT_CLI_LOGE_APPEND_CAUSE(
3485 "No current component (--component option) or non-option argument to assign a log level to:\n %s",
3491 case OPT_RETRY_DURATION
:
3492 if (bt_value_array_append_string_element(run_args
,
3493 "--retry-duration")) {
3494 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3498 if (bt_value_array_append_string_element(run_args
, arg
)) {
3499 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3504 case OPT_CLOCK_CYCLES
:
3505 case OPT_CLOCK_DATE
:
3506 case OPT_CLOCK_FORCE_CORRELATE
:
3508 case OPT_CLOCK_OFFSET
:
3509 case OPT_CLOCK_OFFSET_NS
:
3510 case OPT_CLOCK_SECONDS
:
3513 case OPT_DEBUG_INFO
:
3514 case OPT_DEBUG_INFO_DIR
:
3515 case OPT_DEBUG_INFO_FULL_PATH
:
3516 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3519 case OPT_INPUT_FORMAT
:
3522 case OPT_OUTPUT_FORMAT
:
3525 case OPT_RUN_ARGS_0
:
3526 case OPT_STREAM_INTERSECTION
:
3529 /* Ignore in this pass */
3532 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
3533 argpar_item_opt
->descr
->id
);
3536 } else if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
3537 struct bt_argpar_item_non_opt
*argpar_item_non_opt
;
3538 bt_value_array_append_element_status append_status
;
3540 current_item_type
= CONVERT_CURRENT_ITEM_TYPE_NON_OPT
;
3542 argpar_item_non_opt
= (struct bt_argpar_item_non_opt
*) argpar_item
;
3544 append_status
= bt_value_array_append_string_element(non_opts
,
3545 argpar_item_non_opt
->arg
);
3546 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3547 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3551 append_status
= bt_value_array_append_empty_array_element(
3552 non_opt_params
, NULL
);
3553 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3554 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3558 append_status
= bt_value_array_append_element(non_opt_loglevels
, bt_value_null
);
3559 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3560 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3569 * Second pass: transform the convert-specific options and
3570 * arguments into implicit component instances for the run
3573 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
3574 struct bt_argpar_item
*argpar_item
=
3575 g_ptr_array_index(argpar_parse_ret
.items
, i
);
3576 struct bt_argpar_item_opt
*argpar_item_opt
;
3579 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
3583 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
3584 arg
= argpar_item_opt
->arg
;
3586 switch (argpar_item_opt
->descr
->id
) {
3588 if (trimmer_has_begin
) {
3589 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
3594 trimmer_has_begin
= true;
3595 ret
= append_implicit_component_extra_param(
3596 &implicit_trimmer_args
, "begin", arg
);
3597 implicit_trimmer_args
.exists
= true;
3603 if (trimmer_has_end
) {
3604 printf("At --end option: --end or --timerange option already specified\n %s\n",
3609 trimmer_has_end
= true;
3610 ret
= append_implicit_component_extra_param(
3611 &implicit_trimmer_args
, "end", arg
);
3612 implicit_trimmer_args
.exists
= true;
3622 if (trimmer_has_begin
|| trimmer_has_end
) {
3623 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
3628 ret
= split_timerange(arg
, &begin
, &end
);
3630 BT_CLI_LOGE_APPEND_CAUSE("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s",
3635 ret
= append_implicit_component_extra_param(
3636 &implicit_trimmer_args
, "begin", begin
);
3637 ret
|= append_implicit_component_extra_param(
3638 &implicit_trimmer_args
, "end", end
);
3639 implicit_trimmer_args
.exists
= true;
3647 case OPT_CLOCK_CYCLES
:
3648 append_implicit_component_param(
3649 &implicit_text_args
, "clock-cycles", "yes");
3650 implicit_text_args
.exists
= true;
3652 case OPT_CLOCK_DATE
:
3653 append_implicit_component_param(
3654 &implicit_text_args
, "clock-date", "yes");
3655 implicit_text_args
.exists
= true;
3657 case OPT_CLOCK_FORCE_CORRELATE
:
3658 append_implicit_component_param(
3659 &implicit_muxer_args
,
3660 "assume-absolute-clock-classes", "yes");
3663 append_implicit_component_param(
3664 &implicit_text_args
, "clock-gmt", "yes");
3665 append_implicit_component_param(
3666 &implicit_trimmer_args
, "gmt", "yes");
3667 implicit_text_args
.exists
= true;
3669 case OPT_CLOCK_OFFSET
:
3670 if (ctf_fs_source_clock_class_offset_arg
) {
3671 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset option\n");
3675 ctf_fs_source_clock_class_offset_arg
= g_strdup(arg
);
3676 if (!ctf_fs_source_clock_class_offset_arg
) {
3677 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3681 case OPT_CLOCK_OFFSET_NS
:
3682 if (ctf_fs_source_clock_class_offset_ns_arg
) {
3683 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset-ns option\n");
3687 ctf_fs_source_clock_class_offset_ns_arg
= g_strdup(arg
);
3688 if (!ctf_fs_source_clock_class_offset_ns_arg
) {
3689 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3693 case OPT_CLOCK_SECONDS
:
3694 append_implicit_component_param(
3695 &implicit_text_args
, "clock-seconds", "yes");
3696 implicit_text_args
.exists
= true;
3699 implicit_text_args
.exists
= true;
3700 ret
= append_implicit_component_extra_param(
3701 &implicit_text_args
, "color", arg
);
3706 case OPT_DEBUG_INFO
:
3707 implicit_debug_info_args
.exists
= true;
3709 case OPT_DEBUG_INFO_DIR
:
3710 implicit_debug_info_args
.exists
= true;
3711 ret
= append_implicit_component_extra_param(
3712 &implicit_debug_info_args
, "debug-info-dir", arg
);
3717 case OPT_DEBUG_INFO_FULL_PATH
:
3718 implicit_debug_info_args
.exists
= true;
3719 append_implicit_component_param(
3720 &implicit_debug_info_args
, "full-path", "yes");
3722 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3723 implicit_debug_info_args
.exists
= true;
3724 ret
= append_implicit_component_extra_param(
3725 &implicit_debug_info_args
,
3726 "target-prefix", arg
);
3733 bt_value
*fields
= fields_from_arg(arg
);
3739 implicit_text_args
.exists
= true;
3740 ret
= insert_flat_params_from_array(
3741 implicit_text_args
.params_arg
,
3743 bt_value_put_ref(fields
);
3751 bt_value
*names
= names_from_arg(arg
);
3757 implicit_text_args
.exists
= true;
3758 ret
= insert_flat_params_from_array(
3759 implicit_text_args
.params_arg
,
3761 bt_value_put_ref(names
);
3768 append_implicit_component_param(
3769 &implicit_text_args
, "no-delta", "yes");
3770 implicit_text_args
.exists
= true;
3772 case OPT_INPUT_FORMAT
:
3773 if (got_input_format_opt
) {
3774 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --input-format option.");
3778 got_input_format_opt
= true;
3780 if (strcmp(arg
, "ctf") == 0) {
3781 auto_source_discovery_restrict_plugin_name
= "ctf";
3782 auto_source_discovery_restrict_component_class_name
= "fs";
3783 } else if (strcmp(arg
, "lttng-live") == 0) {
3784 auto_source_discovery_restrict_plugin_name
= "ctf";
3785 auto_source_discovery_restrict_component_class_name
= "lttng-live";
3786 implicit_lttng_live_args
.exists
= true;
3788 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy input format:\n %s",
3793 case OPT_OUTPUT_FORMAT
:
3794 if (got_output_format_opt
) {
3795 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output-format option.");
3799 got_output_format_opt
= true;
3801 if (strcmp(arg
, "text") == 0) {
3802 implicit_text_args
.exists
= true;
3803 } else if (strcmp(arg
, "ctf") == 0) {
3804 implicit_ctf_output_args
.exists
= true;
3805 } else if (strcmp(arg
, "dummy") == 0) {
3806 implicit_dummy_args
.exists
= true;
3807 } else if (strcmp(arg
, "ctf-metadata") == 0) {
3808 print_ctf_metadata
= true;
3810 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy output format:\n %s",
3817 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output option");
3821 output
= strdup(arg
);
3823 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3828 if (print_run_args_0
) {
3829 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
3833 print_run_args
= true;
3835 case OPT_RUN_ARGS_0
:
3836 if (print_run_args
) {
3837 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
3841 print_run_args_0
= true;
3843 case OPT_STREAM_INTERSECTION
:
3845 * Applies to all traces implementing the
3846 * babeltrace.trace-info query.
3848 stream_intersection_mode
= true;
3851 *default_log_level
=
3852 logging_level_min(*default_log_level
, BT_LOG_INFO
);
3855 *default_log_level
=
3856 logging_level_min(*default_log_level
, BT_LOG_TRACE
);
3863 set_auto_log_levels(default_log_level
);
3866 * Legacy behaviour: --verbose used to make the `text` output
3867 * format print more information. --verbose is now equivalent to
3868 * the INFO log level, which is why we compare to `BT_LOG_INFO`
3871 if (*default_log_level
== BT_LOG_INFO
) {
3872 append_implicit_component_param(&implicit_text_args
,
3876 /* Print CTF metadata or print LTTng live sessions */
3877 if (print_ctf_metadata
) {
3878 const bt_value
*bt_val_non_opt
;
3880 if (bt_value_array_is_empty(non_opts
)) {
3881 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf-metadata specified without a path.");
3885 if (bt_value_array_get_length(non_opts
) > 1) {
3886 BT_CLI_LOGE_APPEND_CAUSE("Too many paths specified for --output-format=ctf-metadata.");
3890 cfg
= bt_config_print_ctf_metadata_create(plugin_paths
);
3895 bt_val_non_opt
= bt_value_array_borrow_element_by_index_const(non_opts
, 0);
3896 g_string_assign(cfg
->cmd_data
.print_ctf_metadata
.path
,
3897 bt_value_string_get(bt_val_non_opt
));
3901 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
3909 * If -o ctf was specified, make sure an output path (--output)
3910 * was also specified. --output does not imply -o ctf because
3911 * it's also used for the default, implicit -o text if -o ctf
3914 if (implicit_ctf_output_args
.exists
) {
3916 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf specified without --output (trace output path).");
3921 * At this point we know that -o ctf AND --output were
3922 * specified. Make sure that no options were specified
3923 * which would imply -o text because --output would be
3924 * ambiguous in this case. For example, this is wrong:
3926 * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace
3928 * because --names=all implies -o text, and --output
3929 * could apply to both the sink.text.pretty and
3930 * sink.ctf.fs implicit components.
3932 if (implicit_text_args
.exists
) {
3933 BT_CLI_LOGE_APPEND_CAUSE("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text.");
3939 * If -o dummy and -o ctf were not specified, and if there are
3940 * no explicit sink components, then use an implicit
3941 * `sink.text.pretty` component.
3943 if (!implicit_dummy_args
.exists
&& !implicit_ctf_output_args
.exists
&&
3945 implicit_text_args
.exists
= true;
3949 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
3950 * `path` parameter if --output was specified.
3953 if (implicit_text_args
.exists
) {
3954 append_implicit_component_extra_param(&implicit_text_args
,
3956 } else if (implicit_ctf_output_args
.exists
) {
3957 append_implicit_component_extra_param(&implicit_ctf_output_args
,
3962 /* Decide where the non-option argument(s) go */
3963 if (bt_value_array_get_length(non_opts
) > 0) {
3964 if (implicit_lttng_live_args
.exists
) {
3965 const bt_value
*bt_val_non_opt
;
3967 if (bt_value_array_get_length(non_opts
) > 1) {
3968 BT_CLI_LOGE_APPEND_CAUSE("Too many URLs specified for --input-format=lttng-live.");
3972 bt_val_non_opt
= bt_value_array_borrow_element_by_index_const(non_opts
, 0);
3973 lttng_live_url_parts
=
3974 bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_non_opt
),
3975 error_buf
, sizeof(error_buf
));
3976 if (!lttng_live_url_parts
.proto
) {
3977 BT_CLI_LOGE_APPEND_CAUSE("Invalid LTTng live URL format: %s.",
3982 if (!lttng_live_url_parts
.session_name
) {
3983 /* Print LTTng live sessions */
3984 cfg
= bt_config_print_lttng_live_sessions_create(
3990 g_string_assign(cfg
->cmd_data
.print_lttng_live_sessions
.url
,
3991 bt_value_string_get(bt_val_non_opt
));
3995 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
4002 live_inputs_array_val
= bt_value_array_create();
4003 if (!live_inputs_array_val
) {
4004 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4008 if (bt_value_array_append_string_element(
4009 live_inputs_array_val
,
4010 bt_value_string_get(bt_val_non_opt
))) {
4011 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4015 ret
= append_parameter_to_args(
4016 implicit_lttng_live_args
.extra_params
,
4017 "inputs", live_inputs_array_val
);
4022 ret
= append_implicit_component_extra_param(
4023 &implicit_lttng_live_args
,
4024 "session-not-found-action", "end");
4030 size_t plugin_count
;
4031 const bt_plugin
**plugins
;
4032 const bt_plugin
*plugin
;
4034 status
= require_loaded_plugins(plugin_paths
);
4039 if (auto_source_discovery_restrict_plugin_name
) {
4041 plugin
= find_loaded_plugin(auto_source_discovery_restrict_plugin_name
);
4044 plugin_count
= get_loaded_plugins_count();
4045 plugins
= borrow_loaded_plugins();
4048 status
= auto_discover_source_components(non_opts
, plugins
, plugin_count
,
4049 auto_source_discovery_restrict_component_class_name
,
4050 *default_log_level
, &auto_disc
);
4056 status
= create_implicit_component_args_from_auto_discovered_sources(
4057 &auto_disc
, non_opt_params
, non_opt_loglevels
,
4058 discovered_source_args
);
4065 /* If --clock-offset was given, apply it to any src.ctf.fs component. */
4066 if (ctf_fs_source_clock_class_offset_arg
) {
4069 n
= append_multiple_implicit_components_param(
4070 discovered_source_args
, "source.ctf.fs", "clock-class-offset-s",
4071 ctf_fs_source_clock_class_offset_arg
);
4074 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset specified, but no source.ctf.fs component instantiated.");
4079 /* If --clock-offset-ns was given, apply it to any src.ctf.fs component. */
4080 if (ctf_fs_source_clock_class_offset_ns_arg
) {
4083 n
= append_multiple_implicit_components_param(
4084 discovered_source_args
, "source.ctf.fs", "clock-class-offset-ns",
4085 ctf_fs_source_clock_class_offset_ns_arg
);
4088 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset-ns specified, but no source.ctf.fs component instantiated.");
4094 * If the implicit `source.ctf.lttng-live` component exists,
4095 * make sure there's at least one non-option argument (which is
4098 if (implicit_lttng_live_args
.exists
&& bt_value_array_is_empty(non_opts
)) {
4099 BT_CLI_LOGE_APPEND_CAUSE("Missing URL for implicit `%s` component.",
4100 implicit_lttng_live_args
.comp_arg
->str
);
4104 /* Assign names to implicit components */
4105 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4106 struct implicit_component_args
*args
;
4109 args
= discovered_source_args
->pdata
[i
];
4111 g_string_printf(auto_disc_comp_name
, "auto-disc-%s", args
->comp_arg
->str
);
4113 /* Give it a name like `auto-disc-src-ctf-fs`. */
4114 for (j
= 0; j
< auto_disc_comp_name
->len
; j
++) {
4115 if (auto_disc_comp_name
->str
[j
] == '.') {
4116 auto_disc_comp_name
->str
[j
] = '-';
4120 ret
= assign_name_to_implicit_component(args
,
4121 auto_disc_comp_name
->str
, all_names
, &source_names
, true);
4127 ret
= assign_name_to_implicit_component(&implicit_lttng_live_args
,
4128 "lttng-live", all_names
, &source_names
, true);
4133 ret
= assign_name_to_implicit_component(&implicit_text_args
,
4134 "pretty", all_names
, &sink_names
, true);
4139 ret
= assign_name_to_implicit_component(&implicit_ctf_output_args
,
4140 "sink-ctf-fs", all_names
, &sink_names
, true);
4145 ret
= assign_name_to_implicit_component(&implicit_dummy_args
,
4146 "dummy", all_names
, &sink_names
, true);
4151 ret
= assign_name_to_implicit_component(&implicit_muxer_args
,
4152 "muxer", all_names
, NULL
, false);
4157 ret
= assign_name_to_implicit_component(&implicit_trimmer_args
,
4158 "trimmer", all_names
, NULL
, false);
4163 ret
= assign_name_to_implicit_component(&implicit_debug_info_args
,
4164 "debug-info", all_names
, NULL
, false);
4169 /* Make sure there's at least one source and one sink */
4170 if (!source_names
) {
4171 BT_CLI_LOGE_APPEND_CAUSE("No source component.");
4176 BT_CLI_LOGE_APPEND_CAUSE("No sink component.");
4180 /* Make sure there's a single sink component */
4181 if (g_list_length(sink_names
) != 1) {
4182 BT_CLI_LOGE_APPEND_CAUSE(
4183 "More than one sink component specified.");
4188 * Prepend the muxer, the trimmer, and the debug info to the
4189 * filter chain so that we have:
4191 * sources -> muxer -> [trimmer] -> [debug info] ->
4192 * [user filters] -> sinks
4194 if (implicit_debug_info_args
.exists
) {
4195 if (g_list_prepend_gstring(&filter_names
,
4196 implicit_debug_info_args
.name_arg
->str
)) {
4201 if (implicit_trimmer_args
.exists
) {
4202 if (g_list_prepend_gstring(&filter_names
,
4203 implicit_trimmer_args
.name_arg
->str
)) {
4208 if (g_list_prepend_gstring(&filter_names
,
4209 implicit_muxer_args
.name_arg
->str
)) {
4214 * Append the equivalent run arguments for the implicit
4217 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4218 struct implicit_component_args
*args
=
4219 discovered_source_args
->pdata
[i
];
4221 ret
= append_run_args_for_implicit_component(args
, run_args
);
4227 ret
= append_run_args_for_implicit_component(&implicit_lttng_live_args
,
4233 ret
= append_run_args_for_implicit_component(&implicit_text_args
,
4239 ret
= append_run_args_for_implicit_component(&implicit_ctf_output_args
,
4245 ret
= append_run_args_for_implicit_component(&implicit_dummy_args
,
4251 ret
= append_run_args_for_implicit_component(&implicit_muxer_args
,
4257 ret
= append_run_args_for_implicit_component(&implicit_trimmer_args
,
4263 ret
= append_run_args_for_implicit_component(&implicit_debug_info_args
,
4269 /* Auto-connect components */
4270 ret
= convert_auto_connect(run_args
, source_names
, filter_names
,
4273 BT_CLI_LOGE_APPEND_CAUSE("Cannot auto-connect components.");
4278 * We have all the run command arguments now. Depending on
4279 * --run-args, we pass this to the run command or print them
4282 if (print_run_args
|| print_run_args_0
) {
4283 if (stream_intersection_mode
) {
4284 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --stream-intersection with --run-args or --run-args-0.");
4288 for (i
= 0; i
< bt_value_array_get_length(run_args
); i
++) {
4289 const bt_value
*arg_value
=
4290 bt_value_array_borrow_element_by_index(run_args
,
4293 GString
*quoted
= NULL
;
4294 const char *arg_to_print
;
4296 BT_ASSERT(arg_value
);
4297 arg
= bt_value_string_get(arg_value
);
4299 if (print_run_args
) {
4300 quoted
= bt_common_shell_quote(arg
, true);
4305 arg_to_print
= quoted
->str
;
4310 printf("%s", arg_to_print
);
4313 g_string_free(quoted
, TRUE
);
4316 if (i
< bt_value_array_get_length(run_args
) - 1) {
4317 if (print_run_args
) {
4326 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4330 cfg
= bt_config_run_from_args_array(run_args
, retcode
,
4331 plugin_paths
, *default_log_level
);
4336 cfg
->cmd_data
.run
.stream_intersection_mode
= stream_intersection_mode
;
4341 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4344 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
4348 if (component_arg_for_run
) {
4349 g_string_free(component_arg_for_run
, TRUE
);
4353 g_string_free(name_gstr
, TRUE
);
4356 bt_value_put_ref(live_inputs_array_val
);
4357 bt_value_put_ref(run_args
);
4358 bt_value_put_ref(all_names
);
4359 destroy_glist_of_gstring(source_names
);
4360 destroy_glist_of_gstring(filter_names
);
4361 destroy_glist_of_gstring(sink_names
);
4362 bt_value_put_ref(non_opt_params
);
4363 bt_value_put_ref(non_opt_loglevels
);
4364 bt_value_put_ref(non_opts
);
4365 finalize_implicit_component_args(&implicit_ctf_output_args
);
4366 finalize_implicit_component_args(&implicit_lttng_live_args
);
4367 finalize_implicit_component_args(&implicit_dummy_args
);
4368 finalize_implicit_component_args(&implicit_text_args
);
4369 finalize_implicit_component_args(&implicit_debug_info_args
);
4370 finalize_implicit_component_args(&implicit_muxer_args
);
4371 finalize_implicit_component_args(&implicit_trimmer_args
);
4372 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
4373 auto_source_discovery_fini(&auto_disc
);
4375 if (discovered_source_args
) {
4376 g_ptr_array_free(discovered_source_args
, TRUE
);
4379 g_free(ctf_fs_source_clock_class_offset_arg
);
4380 g_free(ctf_fs_source_clock_class_offset_ns_arg
);
4382 if (auto_disc_comp_name
) {
4383 g_string_free(auto_disc_comp_name
, TRUE
);
4390 * Prints the Babeltrace 2.x general usage.
4393 void print_gen_usage(FILE *fp
)
4395 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4397 fprintf(fp
, "General options:\n");
4399 fprintf(fp
, " -d, --debug Enable debug mode (same as --log-level=T)\n");
4400 fprintf(fp
, " -h, --help Show this help and quit\n");
4401 fprintf(fp
, " -l, --log-level=LVL Set the default log level to LVL (`N`, `T`, `D`,\n");
4402 fprintf(fp
, " `I`, `W` (default), `E`, or `F`)\n");
4403 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
4404 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
4405 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
4406 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
4407 fprintf(fp
, " dynamic plugins can be loaded\n");
4408 fprintf(fp
, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4409 fprintf(fp
, " -V, --version Show version and quit\n");
4411 fprintf(fp
, "Available commands:\n");
4413 fprintf(fp
, " convert Convert and trim traces (default)\n");
4414 fprintf(fp
, " help Get help for a plugin or a component class\n");
4415 fprintf(fp
, " list-plugins List available plugins and their content\n");
4416 fprintf(fp
, " query Query objects from a component class\n");
4417 fprintf(fp
, " run Build a processing graph and run it\n");
4419 fprintf(fp
, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n");
4422 struct bt_config
*bt_config_cli_args_create(int argc
, const char *argv
[],
4423 int *retcode
, bool omit_system_plugin_path
,
4424 bool omit_home_plugin_path
,
4425 const bt_value
*initial_plugin_paths
)
4427 struct bt_config
*config
= NULL
;
4430 const char **top_level_argv
;
4431 int command_argc
= -1;
4432 const char **command_argv
= NULL
;
4433 const char *command_name
= NULL
;
4434 int default_log_level
= -1;
4435 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
4436 bt_value
*plugin_paths
= NULL
;
4438 /* Top-level option descriptions. */
4439 static const struct bt_argpar_opt_descr descrs
[] = {
4440 { OPT_DEBUG
, 'd', "debug", false },
4441 { OPT_HELP
, 'h', "help", false },
4442 { OPT_LOG_LEVEL
, 'l', "log-level", true },
4443 { OPT_VERBOSE
, 'v', "verbose", false },
4444 { OPT_VERSION
, 'V', "version", false},
4445 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
4446 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
4447 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
4448 BT_ARGPAR_OPT_DESCR_SENTINEL
4452 COMMAND_TYPE_NONE
= -1,
4453 COMMAND_TYPE_RUN
= 0,
4454 COMMAND_TYPE_CONVERT
,
4455 COMMAND_TYPE_LIST_PLUGINS
,
4458 } command_type
= COMMAND_TYPE_NONE
;
4462 if (!initial_plugin_paths
) {
4463 plugin_paths
= bt_value_array_create();
4464 if (!plugin_paths
) {
4468 bt_value_copy_status copy_status
= bt_value_copy(
4469 initial_plugin_paths
, &plugin_paths
);
4475 BT_ASSERT(plugin_paths
);
4478 * The `BABELTRACE_PLUGIN_PATH` paths take precedence over the
4479 * `--plugin-path` option's paths, so append it now before
4480 * parsing the general options.
4482 if (append_env_var_plugin_paths(plugin_paths
)) {
4489 print_gen_usage(stdout
);
4493 /* Skip first argument, the name of the program. */
4494 top_level_argc
= argc
- 1;
4495 top_level_argv
= argv
+ 1;
4496 argpar_parse_ret
= bt_argpar_parse(top_level_argc
, top_level_argv
,
4499 if (argpar_parse_ret
.error
) {
4500 BT_CLI_LOGE_APPEND_CAUSE(
4501 "While parsing command-line arguments: %s",
4502 argpar_parse_ret
.error
->str
);
4506 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
4507 struct bt_argpar_item
*item
;
4509 item
= g_ptr_array_index(argpar_parse_ret
.items
, i
);
4511 if (item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
4512 struct bt_argpar_item_opt
*item_opt
=
4513 (struct bt_argpar_item_opt
*) item
;
4515 switch (item_opt
->descr
->id
) {
4518 logging_level_min(default_log_level
, BT_LOG_TRACE
);
4522 logging_level_min(default_log_level
, BT_LOG_INFO
);
4526 int level
= bt_log_get_level_from_string(item_opt
->arg
);
4529 BT_CLI_LOGE_APPEND_CAUSE(
4530 "Invalid argument for --log-level option:\n %s",
4536 logging_level_min(default_log_level
, level
);
4539 case OPT_PLUGIN_PATH
:
4540 if (bt_config_append_plugin_paths_check_setuid_setgid(
4541 plugin_paths
, item_opt
->arg
)) {
4545 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
4546 omit_system_plugin_path
= true;
4548 case OPT_OMIT_HOME_PLUGIN_PATH
:
4549 omit_home_plugin_path
= true;
4555 print_gen_usage(stdout
);
4558 } else if (item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
4559 struct bt_argpar_item_non_opt
*item_non_opt
=
4560 (struct bt_argpar_item_non_opt
*) item
;
4562 * First unknown argument: is it a known command
4566 top_level_argc
- item_non_opt
->orig_index
- 1;
4568 &top_level_argv
[item_non_opt
->orig_index
+ 1];
4570 if (strcmp(item_non_opt
->arg
, "convert") == 0) {
4571 command_type
= COMMAND_TYPE_CONVERT
;
4572 } else if (strcmp(item_non_opt
->arg
, "list-plugins") == 0) {
4573 command_type
= COMMAND_TYPE_LIST_PLUGINS
;
4574 } else if (strcmp(item_non_opt
->arg
, "help") == 0) {
4575 command_type
= COMMAND_TYPE_HELP
;
4576 } else if (strcmp(item_non_opt
->arg
, "query") == 0) {
4577 command_type
= COMMAND_TYPE_QUERY
;
4578 } else if (strcmp(item_non_opt
->arg
, "run") == 0) {
4579 command_type
= COMMAND_TYPE_RUN
;
4582 * Non-option argument, but not a known
4583 * command name: assume the default
4584 * `convert` command.
4586 command_type
= COMMAND_TYPE_CONVERT
;
4587 command_name
= "convert";
4595 if (command_type
== COMMAND_TYPE_NONE
) {
4596 if (argpar_parse_ret
.ingested_orig_args
== top_level_argc
) {
4598 * We only got non-help, non-version general options
4599 * like --verbose and --debug, without any other
4600 * arguments, so we can't do anything useful: print the
4603 print_gen_usage(stdout
);
4608 * We stopped on an unknown option argument (and therefore
4609 * didn't see a command name). Assume `convert` command.
4611 command_type
= COMMAND_TYPE_CONVERT
;
4612 command_name
= "convert";
4614 top_level_argc
- argpar_parse_ret
.ingested_orig_args
;
4616 &top_level_argv
[argpar_parse_ret
.ingested_orig_args
];
4619 BT_ASSERT(command_argv
);
4620 BT_ASSERT(command_argc
>= 0);
4623 * For all commands other than `convert`, we now know the log level to
4624 * use, so we can apply it with `set_auto_log_levels`.
4626 * The convert command has `--debug` and `--verbose` arguments that are
4627 * equivalent to the top-level arguments of the same name. So after it
4628 * has parsed its arguments, `bt_config_convert_from_args` calls
4629 * `set_auto_log_levels` itself.
4631 if (command_type
!= COMMAND_TYPE_CONVERT
) {
4632 set_auto_log_levels(&default_log_level
);
4636 * At this point, `plugin_paths` contains the initial plugin
4637 * paths, the paths from the `BABELTRACE_PLUGIN_PATH` paths, and
4638 * the paths from the `--plugin-path` option.
4640 * Now append the user and system plugin paths.
4642 if (append_home_and_system_plugin_paths(plugin_paths
,
4643 omit_system_plugin_path
, omit_home_plugin_path
)) {
4647 switch (command_type
) {
4648 case COMMAND_TYPE_RUN
:
4649 config
= bt_config_run_from_args(command_argc
, command_argv
,
4650 retcode
, plugin_paths
,
4653 case COMMAND_TYPE_CONVERT
:
4654 config
= bt_config_convert_from_args(command_argc
, command_argv
,
4655 retcode
, plugin_paths
, &default_log_level
);
4657 case COMMAND_TYPE_LIST_PLUGINS
:
4658 config
= bt_config_list_plugins_from_args(command_argc
,
4659 command_argv
, retcode
, plugin_paths
);
4661 case COMMAND_TYPE_HELP
:
4662 config
= bt_config_help_from_args(command_argc
,
4663 command_argv
, retcode
, plugin_paths
,
4666 case COMMAND_TYPE_QUERY
:
4667 config
= bt_config_query_from_args(command_argc
,
4668 command_argv
, retcode
, plugin_paths
,
4676 BT_ASSERT(default_log_level
>= BT_LOG_TRACE
);
4677 config
->log_level
= default_log_level
;
4678 config
->command_name
= command_name
;
4687 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
4688 bt_value_put_ref(plugin_paths
);