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 "param-parse/param-parse.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 /* Offset option with "is set" boolean */
56 /* Legacy "ctf"/"lttng-live" format options */
57 struct ctf_legacy_opts
{
58 struct offset_opt offset_s
;
59 struct offset_opt offset_ns
;
60 bool stream_intersection
;
63 /* Legacy "text" format options */
64 struct text_legacy_opts
{
66 * output, dbg_info_dir, dbg_info_target_prefix, names,
67 * and fields are owned by this.
70 GString
*dbg_info_dir
;
71 GString
*dbg_info_target_prefix
;
72 const bt_value
*names
;
73 const bt_value
*fields
;
81 bool dbg_info_full_path
;
85 /* Legacy input format format */
86 enum legacy_input_format
{
87 LEGACY_INPUT_FORMAT_NONE
= 0,
88 LEGACY_INPUT_FORMAT_CTF
,
89 LEGACY_INPUT_FORMAT_LTTNG_LIVE
,
92 /* Legacy output format format */
93 enum legacy_output_format
{
94 LEGACY_OUTPUT_FORMAT_NONE
= 0,
95 LEGACY_OUTPUT_FORMAT_TEXT
,
96 LEGACY_OUTPUT_FORMAT_DUMMY
,
99 #define BT_CLI_LOGE_APPEND_CAUSE_OOM() BT_CLI_LOGE_APPEND_CAUSE("Out of memory.")
102 * Returns the plugin name, component class name, component class type,
103 * and component name from a command-line --component option's argument.
104 * arg must have the following format:
106 * [NAME:]TYPE.PLUGIN.CLS
108 * where NAME is the optional component name, TYPE is either `source`,
109 * `filter`, or `sink`, PLUGIN is the plugin name, and CLS is the
110 * component class name.
112 * On success, both *plugin and *component are not NULL. *plugin
113 * and *comp_cls are owned by the caller. On success, *name can be NULL
114 * if no component class name was found, and *comp_cls_type is set.
117 void plugin_comp_cls_names(const char *arg
, char **name
, char **plugin
,
118 char **comp_cls
, bt_component_class_type
*comp_cls_type
)
120 const char *at
= arg
;
121 GString
*gs_name
= NULL
;
122 GString
*gs_comp_cls_type
= NULL
;
123 GString
*gs_plugin
= NULL
;
124 GString
*gs_comp_cls
= NULL
;
130 BT_ASSERT(comp_cls_type
);
132 if (!bt_common_string_is_printable(arg
)) {
133 BT_CLI_LOGE_APPEND_CAUSE("Argument contains a non-printable character.");
137 /* Parse the component name */
138 gs_name
= bt_common_string_until(at
, ".:\\", ":", &end_pos
);
143 if (arg
[end_pos
] == ':') {
147 g_string_assign(gs_name
, "");
150 /* Parse the component class type */
151 gs_comp_cls_type
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
152 if (!gs_comp_cls_type
|| at
[end_pos
] == '\0') {
153 BT_CLI_LOGE_APPEND_CAUSE("Missing component class type (`source`, `filter`, or `sink`).");
157 if (strcmp(gs_comp_cls_type
->str
, "source") == 0 ||
158 strcmp(gs_comp_cls_type
->str
, "src") == 0) {
159 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SOURCE
;
160 } else if (strcmp(gs_comp_cls_type
->str
, "filter") == 0 ||
161 strcmp(gs_comp_cls_type
->str
, "flt") == 0) {
162 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_FILTER
;
163 } else if (strcmp(gs_comp_cls_type
->str
, "sink") == 0) {
164 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SINK
;
166 BT_CLI_LOGE_APPEND_CAUSE("Unknown component class type: `%s`.",
167 gs_comp_cls_type
->str
);
173 /* Parse the plugin name */
174 gs_plugin
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
175 if (!gs_plugin
|| gs_plugin
->len
== 0 || at
[end_pos
] == '\0') {
176 BT_CLI_LOGE_APPEND_CAUSE("Missing plugin or component class name.");
182 /* Parse the component class name */
183 gs_comp_cls
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
184 if (!gs_comp_cls
|| gs_comp_cls
->len
== 0) {
185 BT_CLI_LOGE_APPEND_CAUSE("Missing component class name.");
189 if (at
[end_pos
] != '\0') {
190 /* Found a non-escaped `.` */
195 if (gs_name
->len
== 0) {
197 g_string_free(gs_name
, TRUE
);
199 *name
= gs_name
->str
;
200 g_string_free(gs_name
, FALSE
);
203 g_string_free(gs_name
, TRUE
);
206 *plugin
= gs_plugin
->str
;
207 *comp_cls
= gs_comp_cls
->str
;
208 g_string_free(gs_plugin
, FALSE
);
209 g_string_free(gs_comp_cls
, FALSE
);
225 g_string_free(gs_name
, TRUE
);
229 g_string_free(gs_plugin
, TRUE
);
233 g_string_free(gs_comp_cls
, TRUE
);
236 if (gs_comp_cls_type
) {
237 g_string_free(gs_comp_cls_type
, TRUE
);
244 * Prints the Babeltrace version.
247 void print_version(void)
249 if (GIT_VERSION
[0] == '\0') {
250 puts("Babeltrace " VERSION
);
252 puts("Babeltrace " VERSION
" - " GIT_VERSION
);
257 * Destroys a component configuration.
260 void bt_config_component_destroy(bt_object
*obj
)
262 struct bt_config_component
*bt_config_component
=
263 container_of(obj
, struct bt_config_component
, base
);
269 if (bt_config_component
->plugin_name
) {
270 g_string_free(bt_config_component
->plugin_name
, TRUE
);
273 if (bt_config_component
->comp_cls_name
) {
274 g_string_free(bt_config_component
->comp_cls_name
, TRUE
);
277 if (bt_config_component
->instance_name
) {
278 g_string_free(bt_config_component
->instance_name
, TRUE
);
281 BT_VALUE_PUT_REF_AND_RESET(bt_config_component
->params
);
282 g_free(bt_config_component
);
289 * Creates a component configuration using the given plugin name and
290 * component name. `plugin_name` and `comp_cls_name` are copied (belong
291 * to the return value).
293 * Return value is owned by the caller.
296 struct bt_config_component
*bt_config_component_create(
297 bt_component_class_type type
,
298 const char *plugin_name
, const char *comp_cls_name
,
301 struct bt_config_component
*cfg_component
= NULL
;
303 cfg_component
= g_new0(struct bt_config_component
, 1);
304 if (!cfg_component
) {
305 BT_CLI_LOGE_APPEND_CAUSE_OOM();
309 bt_object_init_shared(&cfg_component
->base
,
310 bt_config_component_destroy
);
311 cfg_component
->type
= type
;
312 cfg_component
->plugin_name
= g_string_new(plugin_name
);
313 if (!cfg_component
->plugin_name
) {
314 BT_CLI_LOGE_APPEND_CAUSE_OOM();
318 cfg_component
->comp_cls_name
= g_string_new(comp_cls_name
);
319 if (!cfg_component
->comp_cls_name
) {
320 BT_CLI_LOGE_APPEND_CAUSE_OOM();
324 cfg_component
->instance_name
= g_string_new(NULL
);
325 if (!cfg_component
->instance_name
) {
326 BT_CLI_LOGE_APPEND_CAUSE_OOM();
330 cfg_component
->log_level
= init_log_level
;
332 /* Start with empty parameters */
333 cfg_component
->params
= bt_value_map_create();
334 if (!cfg_component
->params
) {
335 BT_CLI_LOGE_APPEND_CAUSE_OOM();
342 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
345 return cfg_component
;
349 * Creates a component configuration from a command-line --component
353 struct bt_config_component
*bt_config_component_from_arg(const char *arg
,
356 struct bt_config_component
*cfg_comp
= NULL
;
358 char *plugin_name
= NULL
;
359 char *comp_cls_name
= NULL
;
360 bt_component_class_type type
;
362 plugin_comp_cls_names(arg
, &name
, &plugin_name
, &comp_cls_name
, &type
);
363 if (!plugin_name
|| !comp_cls_name
) {
367 cfg_comp
= bt_config_component_create(type
, plugin_name
, comp_cls_name
,
374 g_string_assign(cfg_comp
->instance_name
, name
);
380 BT_OBJECT_PUT_REF_AND_RESET(cfg_comp
);
385 g_free(comp_cls_name
);
390 * Destroys a configuration.
393 void bt_config_destroy(bt_object
*obj
)
395 struct bt_config
*cfg
=
396 container_of(obj
, struct bt_config
, base
);
402 BT_VALUE_PUT_REF_AND_RESET(cfg
->plugin_paths
);
404 switch (cfg
->command
) {
405 case BT_CONFIG_COMMAND_RUN
:
406 if (cfg
->cmd_data
.run
.sources
) {
407 g_ptr_array_free(cfg
->cmd_data
.run
.sources
, TRUE
);
410 if (cfg
->cmd_data
.run
.filters
) {
411 g_ptr_array_free(cfg
->cmd_data
.run
.filters
, TRUE
);
414 if (cfg
->cmd_data
.run
.sinks
) {
415 g_ptr_array_free(cfg
->cmd_data
.run
.sinks
, TRUE
);
418 if (cfg
->cmd_data
.run
.connections
) {
419 g_ptr_array_free(cfg
->cmd_data
.run
.connections
,
423 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
425 case BT_CONFIG_COMMAND_HELP
:
426 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.help
.cfg_component
);
428 case BT_CONFIG_COMMAND_QUERY
:
429 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.query
.cfg_component
);
431 if (cfg
->cmd_data
.query
.object
) {
432 g_string_free(cfg
->cmd_data
.query
.object
, TRUE
);
435 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
436 if (cfg
->cmd_data
.print_ctf_metadata
.path
) {
437 g_string_free(cfg
->cmd_data
.print_ctf_metadata
.path
,
440 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
444 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
445 if (cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
447 cfg
->cmd_data
.print_lttng_live_sessions
.url
,
450 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
465 void destroy_glist_of_gstring(GList
*list
)
473 for (at
= list
; at
; at
= g_list_next(at
)) {
474 g_string_free(at
->data
, TRUE
);
481 * Creates a simple lexical scanner for parsing comma-delimited names
484 * Return value is owned by the caller.
487 GScanner
*create_csv_identifiers_scanner(void)
490 GScannerConfig scanner_config
= {
491 .cset_skip_characters
= (gchar
*) " \t\n",
492 .cset_identifier_first
= (gchar
*) G_CSET_a_2_z G_CSET_A_2_Z
"_",
493 .cset_identifier_nth
= (gchar
*) G_CSET_a_2_z G_CSET_A_2_Z
":_-",
494 .case_sensitive
= TRUE
,
495 .cpair_comment_single
= NULL
,
496 .skip_comment_multi
= TRUE
,
497 .skip_comment_single
= TRUE
,
498 .scan_comment_multi
= FALSE
,
499 .scan_identifier
= TRUE
,
500 .scan_identifier_1char
= TRUE
,
501 .scan_identifier_NULL
= FALSE
,
502 .scan_symbols
= FALSE
,
503 .symbol_2_token
= FALSE
,
504 .scope_0_fallback
= FALSE
,
505 .scan_binary
= FALSE
,
509 .scan_hex_dollar
= FALSE
,
510 .numbers_2_int
= FALSE
,
511 .int_2_float
= FALSE
,
512 .store_int64
= FALSE
,
513 .scan_string_sq
= FALSE
,
514 .scan_string_dq
= FALSE
,
515 .identifier_2_string
= FALSE
,
516 .char_2_token
= TRUE
,
519 scanner
= g_scanner_new(&scanner_config
);
521 BT_CLI_LOGE_APPEND_CAUSE_OOM();
528 * Converts a comma-delimited list of known names (--names option) to
529 * an array value object containing those names as string value objects.
531 * Return value is owned by the caller.
534 bt_value
*names_from_arg(const char *arg
)
536 GScanner
*scanner
= NULL
;
537 bt_value
*names
= NULL
;
538 bool found_all
= false, found_none
= false, found_item
= false;
540 names
= bt_value_array_create();
542 BT_CLI_LOGE_APPEND_CAUSE_OOM();
546 scanner
= create_csv_identifiers_scanner();
551 g_scanner_input_text(scanner
, arg
, strlen(arg
));
554 GTokenType token_type
= g_scanner_get_next_token(scanner
);
556 switch (token_type
) {
557 case G_TOKEN_IDENTIFIER
:
559 const char *identifier
= scanner
->value
.v_identifier
;
561 if (strcmp(identifier
, "payload") == 0 ||
562 strcmp(identifier
, "args") == 0 ||
563 strcmp(identifier
, "arg") == 0) {
565 if (bt_value_array_append_string_element(names
,
569 } else if (strcmp(identifier
, "context") == 0 ||
570 strcmp(identifier
, "ctx") == 0) {
572 if (bt_value_array_append_string_element(names
,
576 } else if (strcmp(identifier
, "scope") == 0 ||
577 strcmp(identifier
, "header") == 0) {
579 if (bt_value_array_append_string_element(names
,
583 } else if (strcmp(identifier
, "all") == 0) {
585 if (bt_value_array_append_string_element(names
,
589 } else if (strcmp(identifier
, "none") == 0) {
591 if (bt_value_array_append_string_element(names
,
596 BT_CLI_LOGE_APPEND_CAUSE("Unknown name: `%s`.",
612 if (found_none
&& found_all
) {
613 BT_CLI_LOGE_APPEND_CAUSE("Only either `all` or `none` can be specified in the list given to the --names option, but not both.");
617 * Legacy behavior is to clear the defaults (show none) when at
618 * least one item is specified.
620 if (found_item
&& !found_none
&& !found_all
) {
621 if (bt_value_array_append_string_element(names
, "none")) {
626 g_scanner_destroy(scanner
);
631 BT_VALUE_PUT_REF_AND_RESET(names
);
633 g_scanner_destroy(scanner
);
639 * Converts a comma-delimited list of known fields (--fields option) to
640 * an array value object containing those fields as string
643 * Return value is owned by the caller.
646 bt_value
*fields_from_arg(const char *arg
)
648 GScanner
*scanner
= NULL
;
651 fields
= bt_value_array_create();
653 BT_CLI_LOGE_APPEND_CAUSE_OOM();
657 scanner
= create_csv_identifiers_scanner();
662 g_scanner_input_text(scanner
, arg
, strlen(arg
));
665 GTokenType token_type
= g_scanner_get_next_token(scanner
);
667 switch (token_type
) {
668 case G_TOKEN_IDENTIFIER
:
670 const char *identifier
= scanner
->value
.v_identifier
;
672 if (strcmp(identifier
, "trace") == 0 ||
673 strcmp(identifier
, "trace:hostname") == 0 ||
674 strcmp(identifier
, "trace:domain") == 0 ||
675 strcmp(identifier
, "trace:procname") == 0 ||
676 strcmp(identifier
, "trace:vpid") == 0 ||
677 strcmp(identifier
, "loglevel") == 0 ||
678 strcmp(identifier
, "emf") == 0 ||
679 strcmp(identifier
, "callsite") == 0 ||
680 strcmp(identifier
, "all") == 0) {
681 if (bt_value_array_append_string_element(fields
,
686 BT_CLI_LOGE_APPEND_CAUSE("Unknown field: `%s`.",
704 BT_VALUE_PUT_REF_AND_RESET(fields
);
708 g_scanner_destroy(scanner
);
714 void append_param_arg(GString
*params_arg
, const char *key
, const char *value
)
716 BT_ASSERT(params_arg
);
720 if (params_arg
->len
!= 0) {
721 g_string_append_c(params_arg
, ',');
724 g_string_append(params_arg
, key
);
725 g_string_append_c(params_arg
, '=');
726 g_string_append(params_arg
, value
);
730 * Inserts the equivalent "prefix-NAME=yes" strings into params_arg
731 * where the names are in names_array.
734 int insert_flat_params_from_array(GString
*params_arg
,
735 const bt_value
*names_array
, const char *prefix
)
739 GString
*tmpstr
= NULL
, *default_value
= NULL
;
740 bool default_set
= false, non_default_set
= false;
743 * names_array may be NULL if no CLI options were specified to
744 * trigger its creation.
750 tmpstr
= g_string_new(NULL
);
752 BT_CLI_LOGE_APPEND_CAUSE_OOM();
757 default_value
= g_string_new(NULL
);
758 if (!default_value
) {
759 BT_CLI_LOGE_APPEND_CAUSE_OOM();
764 for (i
= 0; i
< bt_value_array_get_length(names_array
); i
++) {
765 const bt_value
*str_obj
=
766 bt_value_array_borrow_element_by_index_const(names_array
,
769 bool is_default
= false;
771 suffix
= bt_value_string_get(str_obj
);
773 g_string_assign(tmpstr
, prefix
);
774 g_string_append(tmpstr
, "-");
776 /* Special-case for "all" and "none". */
777 if (strcmp(suffix
, "all") == 0) {
779 g_string_assign(default_value
, "show");
780 } else if (strcmp(suffix
, "none") == 0) {
782 g_string_assign(default_value
, "hide");
786 g_string_append(tmpstr
, "default");
787 append_param_arg(params_arg
, tmpstr
->str
,
790 non_default_set
= true;
791 g_string_append(tmpstr
, suffix
);
792 append_param_arg(params_arg
, tmpstr
->str
, "yes");
796 /* Implicit field-default=hide if any non-default option is set. */
797 if (non_default_set
&& !default_set
) {
798 g_string_assign(tmpstr
, prefix
);
799 g_string_append(tmpstr
, "-default");
800 g_string_assign(default_value
, "hide");
801 append_param_arg(params_arg
, tmpstr
->str
, default_value
->str
);
806 g_string_free(default_value
, TRUE
);
810 g_string_free(tmpstr
, TRUE
);
823 OPT_CLOCK_FORCE_CORRELATE
,
834 OPT_DEBUG_INFO_FULL_PATH
,
835 OPT_DEBUG_INFO_TARGET_PREFIX
,
844 OPT_OMIT_HOME_PLUGIN_PATH
,
845 OPT_OMIT_SYSTEM_PLUGIN_PATH
,
850 OPT_RESET_BASE_PARAMS
,
854 OPT_STREAM_INTERSECTION
,
860 enum bt_config_component_dest
{
861 BT_CONFIG_COMPONENT_DEST_UNKNOWN
= -1,
862 BT_CONFIG_COMPONENT_DEST_SOURCE
,
863 BT_CONFIG_COMPONENT_DEST_FILTER
,
864 BT_CONFIG_COMPONENT_DEST_SINK
,
868 * Adds a configuration component to the appropriate configuration
869 * array depending on the destination.
872 void add_run_cfg_comp(struct bt_config
*cfg
,
873 struct bt_config_component
*cfg_comp
,
874 enum bt_config_component_dest dest
)
876 bt_object_get_ref(cfg_comp
);
879 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
880 g_ptr_array_add(cfg
->cmd_data
.run
.sources
, cfg_comp
);
882 case BT_CONFIG_COMPONENT_DEST_FILTER
:
883 g_ptr_array_add(cfg
->cmd_data
.run
.filters
, cfg_comp
);
885 case BT_CONFIG_COMPONENT_DEST_SINK
:
886 g_ptr_array_add(cfg
->cmd_data
.run
.sinks
, cfg_comp
);
894 int add_run_cfg_comp_check_name(struct bt_config
*cfg
,
895 struct bt_config_component
*cfg_comp
,
896 enum bt_config_component_dest dest
,
897 bt_value
*instance_names
)
901 if (cfg_comp
->instance_name
->len
== 0) {
902 BT_CLI_LOGE_APPEND_CAUSE("Found an unnamed component.");
907 if (bt_value_map_has_entry(instance_names
,
908 cfg_comp
->instance_name
->str
)) {
909 BT_CLI_LOGE_APPEND_CAUSE("Duplicate component instance name:\n %s",
910 cfg_comp
->instance_name
->str
);
915 if (bt_value_map_insert_entry(instance_names
,
916 cfg_comp
->instance_name
->str
, bt_value_null
)) {
917 BT_CLI_LOGE_APPEND_CAUSE_OOM();
922 add_run_cfg_comp(cfg
, cfg_comp
, dest
);
929 int append_env_var_plugin_paths(bt_value
*plugin_paths
)
934 if (bt_common_is_setuid_setgid()) {
935 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
939 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
944 ret
= bt_config_append_plugin_paths(plugin_paths
, envvar
);
948 BT_CLI_LOGE_APPEND_CAUSE("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH.");
955 int append_home_and_system_plugin_paths(bt_value
*plugin_paths
,
956 bool omit_system_plugin_path
, bool omit_home_plugin_path
)
960 if (!omit_home_plugin_path
) {
961 if (bt_common_is_setuid_setgid()) {
962 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
964 char *home_plugin_dir
= bt_common_get_home_plugin_path(
965 BT_LOG_OUTPUT_LEVEL
);
967 if (home_plugin_dir
) {
968 ret
= bt_config_append_plugin_paths(
969 plugin_paths
, home_plugin_dir
);
970 free(home_plugin_dir
);
973 BT_CLI_LOGE_APPEND_CAUSE("Invalid home plugin path.");
980 if (!omit_system_plugin_path
) {
981 if (bt_config_append_plugin_paths(plugin_paths
,
982 bt_common_get_system_plugin_path())) {
983 BT_CLI_LOGE_APPEND_CAUSE("Invalid system plugin path.");
989 BT_CLI_LOGE_APPEND_CAUSE("Cannot append home and system plugin paths.");
994 struct bt_config
*bt_config_base_create(enum bt_config_command command
,
995 const bt_value
*plugin_paths
, bool needs_plugins
)
997 struct bt_config
*cfg
;
1000 cfg
= g_new0(struct bt_config
, 1);
1002 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1006 bt_object_init_shared(&cfg
->base
, bt_config_destroy
);
1007 cfg
->command
= command
;
1008 cfg
->command_needs_plugins
= needs_plugins
;
1011 bt_value
*plugin_paths_copy
;
1013 (void) bt_value_copy(plugin_paths
,
1014 &plugin_paths_copy
);
1015 cfg
->plugin_paths
= plugin_paths_copy
;
1017 cfg
->plugin_paths
= bt_value_array_create();
1018 if (!cfg
->plugin_paths
) {
1019 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1027 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1034 struct bt_config
*bt_config_run_create(const bt_value
*plugin_paths
)
1036 struct bt_config
*cfg
;
1039 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_RUN
,
1040 plugin_paths
, true);
1045 cfg
->cmd_data
.run
.sources
= g_ptr_array_new_with_free_func(
1046 (GDestroyNotify
) bt_object_put_ref
);
1047 if (!cfg
->cmd_data
.run
.sources
) {
1048 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1052 cfg
->cmd_data
.run
.filters
= g_ptr_array_new_with_free_func(
1053 (GDestroyNotify
) bt_object_put_ref
);
1054 if (!cfg
->cmd_data
.run
.filters
) {
1055 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1059 cfg
->cmd_data
.run
.sinks
= g_ptr_array_new_with_free_func(
1060 (GDestroyNotify
) bt_object_put_ref
);
1061 if (!cfg
->cmd_data
.run
.sinks
) {
1062 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1066 cfg
->cmd_data
.run
.connections
= g_ptr_array_new_with_free_func(
1067 (GDestroyNotify
) bt_config_connection_destroy
);
1068 if (!cfg
->cmd_data
.run
.connections
) {
1069 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1076 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1083 struct bt_config
*bt_config_list_plugins_create(const bt_value
*plugin_paths
)
1085 return bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS
,
1086 plugin_paths
, true);
1090 struct bt_config
*bt_config_help_create(const bt_value
*plugin_paths
,
1091 int default_log_level
)
1093 struct bt_config
*cfg
;
1096 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_HELP
,
1097 plugin_paths
, true);
1102 cfg
->cmd_data
.help
.cfg_component
=
1103 bt_config_component_create(-1, NULL
, NULL
, default_log_level
);
1104 if (!cfg
->cmd_data
.help
.cfg_component
) {
1111 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1118 struct bt_config
*bt_config_query_create(const bt_value
*plugin_paths
)
1120 struct bt_config
*cfg
;
1123 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_QUERY
,
1124 plugin_paths
, true);
1129 cfg
->cmd_data
.query
.object
= g_string_new(NULL
);
1130 if (!cfg
->cmd_data
.query
.object
) {
1131 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1138 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1145 struct bt_config
*bt_config_print_ctf_metadata_create(
1146 const bt_value
*plugin_paths
)
1148 struct bt_config
*cfg
;
1151 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_CTF_METADATA
,
1152 plugin_paths
, true);
1157 cfg
->cmd_data
.print_ctf_metadata
.path
= g_string_new(NULL
);
1158 if (!cfg
->cmd_data
.print_ctf_metadata
.path
) {
1159 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1163 cfg
->cmd_data
.print_ctf_metadata
.output_path
= g_string_new(NULL
);
1164 if (!cfg
->cmd_data
.print_ctf_metadata
.output_path
) {
1165 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1172 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1179 struct bt_config
*bt_config_print_lttng_live_sessions_create(
1180 const bt_value
*plugin_paths
)
1182 struct bt_config
*cfg
;
1185 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
,
1186 plugin_paths
, true);
1191 cfg
->cmd_data
.print_lttng_live_sessions
.url
= g_string_new(NULL
);
1192 if (!cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
1193 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1197 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
=
1199 if (!cfg
->cmd_data
.print_lttng_live_sessions
.output_path
) {
1200 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1207 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1214 int bt_config_append_plugin_paths_check_setuid_setgid(
1215 bt_value
*plugin_paths
, const char *arg
)
1219 if (bt_common_is_setuid_setgid()) {
1220 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1224 if (bt_config_append_plugin_paths(plugin_paths
, arg
)) {
1225 BT_CLI_LOGE_APPEND_CAUSE("Invalid --plugin-path option's argument:\n %s",
1236 * Prints the expected format for a --params option.
1239 void print_expected_params_format(FILE *fp
)
1241 fprintf(fp
, "Expected format of PARAMS\n");
1242 fprintf(fp
, "-------------------------\n");
1244 fprintf(fp
, " PARAM=VALUE[,PARAM=VALUE]...\n");
1246 fprintf(fp
, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
1247 fprintf(fp
, "where PARAM is the parameter name (C identifier plus the [:.-] characters),\n");
1248 fprintf(fp
, "and VALUE can be one of:\n");
1250 fprintf(fp
, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
1251 fprintf(fp
, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
1252 fprintf(fp
, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
1253 fprintf(fp
, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
1254 fprintf(fp
, " (`0x` prefix) unsigned (with `+` prefix) or signed 64-bit integer.\n");
1255 fprintf(fp
, "* Double precision floating point number (scientific notation is accepted).\n");
1256 fprintf(fp
, "* Unquoted string with no special characters, and not matching any of\n");
1257 fprintf(fp
, " the null and boolean value symbols above.\n");
1258 fprintf(fp
, "* Double-quoted string (accepts escape characters).\n");
1259 fprintf(fp
, "* Array, formatted as an opening `[`, a list of comma-separated values\n");
1260 fprintf(fp
, " (as described by the current list) and a closing `]`.\n");
1261 fprintf(fp
, "* Map, formatted as an opening `{`, a comma-separated list of PARAM=VALUE\n");
1262 fprintf(fp
, " assignments and a closing `}`.\n");
1264 fprintf(fp
, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1266 fprintf(fp
, "Example:\n");
1268 fprintf(fp
, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
1269 fprintf(fp
, " play=+23, observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
1270 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\",\n");
1271 fprintf(fp
, " things=[1, \"2\", 3]\n");
1273 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1274 fprintf(fp
, "babeltrace2 from a shell.\n");
1278 bool help_option_is_specified(
1279 const struct bt_argpar_parse_ret
*argpar_parse_ret
)
1282 bool specified
= false;
1284 for (i
= 0; i
< argpar_parse_ret
->items
->len
; i
++) {
1285 struct bt_argpar_item
*argpar_item
=
1286 g_ptr_array_index(argpar_parse_ret
->items
, i
);
1287 struct bt_argpar_item_opt
*argpar_item_opt
;
1289 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
1293 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
1294 if (argpar_item_opt
->descr
->id
== OPT_HELP
) {
1304 * Prints the help command usage.
1307 void print_help_usage(FILE *fp
)
1309 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1310 fprintf(fp
, " babeltrace2 [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
1312 fprintf(fp
, "Options:\n");
1314 fprintf(fp
, " -h, --help Show this help and quit\n");
1316 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1318 fprintf(fp
, "Use `babeltrace2 list-plugins` to show the list of available plugins.\n");
1322 const struct bt_argpar_opt_descr help_options
[] = {
1323 /* id, short_name, long_name, with_arg */
1324 { OPT_HELP
, 'h', "help", false },
1325 BT_ARGPAR_OPT_DESCR_SENTINEL
1329 * Creates a Babeltrace config object from the arguments of a help
1332 * *retcode is set to the appropriate exit code to use.
1335 struct bt_config
*bt_config_help_from_args(int argc
, const char *argv
[],
1336 int *retcode
, const bt_value
*plugin_paths
,
1337 int default_log_level
)
1339 struct bt_config
*cfg
= NULL
;
1340 char *plugin_name
= NULL
, *comp_cls_name
= NULL
;
1341 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1342 struct bt_argpar_item_non_opt
*non_opt
;
1343 GString
*substring
= NULL
;
1347 cfg
= bt_config_help_create(plugin_paths
, default_log_level
);
1353 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, help_options
, true);
1354 if (argpar_parse_ret
.error
) {
1355 BT_CLI_LOGE_APPEND_CAUSE(
1356 "While parsing `help` command's command-line arguments: %s",
1357 argpar_parse_ret
.error
->str
);
1361 if (help_option_is_specified(&argpar_parse_ret
)) {
1362 print_help_usage(stdout
);
1364 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1368 if (argpar_parse_ret
.items
->len
== 0) {
1369 BT_CLI_LOGE_APPEND_CAUSE(
1370 "Missing plugin name or component class descriptor.");
1372 } else if (argpar_parse_ret
.items
->len
> 1) {
1374 * At this point we know there are least two non-option
1375 * arguments because we don't reach here with `--help`,
1378 non_opt
= argpar_parse_ret
.items
->pdata
[1];
1379 BT_CLI_LOGE_APPEND_CAUSE(
1380 "Extraneous command-line argument specified to `help` command: `%s`.",
1385 non_opt
= argpar_parse_ret
.items
->pdata
[0];
1387 /* Look for unescaped dots in the argument. */
1388 substring
= bt_common_string_until(non_opt
->arg
, ".\\", ".", &end_pos
);
1390 BT_CLI_LOGE_APPEND_CAUSE("Could not consume argument: arg=%s",
1395 if (end_pos
== strlen(non_opt
->arg
)) {
1396 /* Didn't find an unescaped dot, treat it as a plugin name. */
1397 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1401 * Found an unescaped dot, treat it as a component class name.
1403 plugin_comp_cls_names(non_opt
->arg
, NULL
, &plugin_name
, &comp_cls_name
,
1404 &cfg
->cmd_data
.help
.cfg_component
->type
);
1405 if (!plugin_name
|| !comp_cls_name
) {
1406 BT_CLI_LOGE_APPEND_CAUSE(
1407 "Could not parse argument as a component class name: arg=%s",
1412 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1414 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
,
1422 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1425 g_free(plugin_name
);
1426 g_free(comp_cls_name
);
1429 g_string_free(substring
, TRUE
);
1432 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1438 * Prints the help command usage.
1441 void print_query_usage(FILE *fp
)
1443 fprintf(fp
, "Usage: babeltrace2 [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
1445 fprintf(fp
, "Options:\n");
1447 fprintf(fp
, " -p, --params=PARAMS Set the query parameters to PARAMS (see the expected\n");
1448 fprintf(fp
, " format of PARAMS below)\n");
1449 fprintf(fp
, " -h, --help Show this help and quit\n");
1450 fprintf(fp
, "\n\n");
1451 print_expected_params_format(fp
);
1455 const struct bt_argpar_opt_descr query_options
[] = {
1456 /* id, short_name, long_name, with_arg */
1457 { OPT_HELP
, 'h', "help", false },
1458 { OPT_PARAMS
, 'p', "params", true },
1459 BT_ARGPAR_OPT_DESCR_SENTINEL
1463 * Creates a Babeltrace config object from the arguments of a query
1466 * *retcode is set to the appropriate exit code to use.
1469 struct bt_config
*bt_config_query_from_args(int argc
, const char *argv
[],
1470 int *retcode
, const bt_value
*plugin_paths
,
1471 int default_log_level
)
1474 struct bt_config
*cfg
= NULL
;
1475 const char *component_class_spec
= NULL
;
1476 const char *query_object
= NULL
;
1477 GString
*error_str
= NULL
;
1478 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1480 bt_value
*params
= bt_value_map_create();
1482 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1487 cfg
= bt_config_query_create(plugin_paths
);
1492 error_str
= g_string_new(NULL
);
1494 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1499 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, query_options
, true);
1500 if (argpar_parse_ret
.error
) {
1501 BT_CLI_LOGE_APPEND_CAUSE(
1502 "While parsing `query` command's command-line arguments: %s",
1503 argpar_parse_ret
.error
->str
);
1507 if (help_option_is_specified(&argpar_parse_ret
)) {
1508 print_query_usage(stdout
);
1510 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1514 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
1515 struct bt_argpar_item
*argpar_item
=
1516 g_ptr_array_index(argpar_parse_ret
.items
, i
);
1518 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
1519 struct bt_argpar_item_opt
*argpar_item_opt
=
1520 (struct bt_argpar_item_opt
*) argpar_item
;
1521 const char *arg
= argpar_item_opt
->arg
;
1523 switch (argpar_item_opt
->descr
->id
) {
1526 bt_value
*parsed_params
= bt_param_parse(arg
, error_str
);
1527 bt_value_map_extend_status extend_status
;
1528 if (!parsed_params
) {
1529 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1534 extend_status
= bt_value_map_extend(params
, parsed_params
);
1535 BT_VALUE_PUT_REF_AND_RESET(parsed_params
);
1536 if (extend_status
) {
1537 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current parameters with --params option's argument:\n %s",
1544 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1545 argpar_item_opt
->descr
->id
);
1549 struct bt_argpar_item_non_opt
*argpar_item_non_opt
1550 = (struct bt_argpar_item_non_opt
*) argpar_item
;
1553 * We need exactly two non-option arguments
1554 * which are the mandatory component class
1555 * specification and query object.
1557 if (!component_class_spec
) {
1558 component_class_spec
= argpar_item_non_opt
->arg
;
1559 } else if (!query_object
) {
1560 query_object
= argpar_item_non_opt
->arg
;
1562 BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `query` command: `%s`.",
1563 argpar_item_non_opt
->arg
);
1569 if (!component_class_spec
|| !query_object
) {
1570 print_query_usage(stdout
);
1572 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1576 cfg
->cmd_data
.query
.cfg_component
=
1577 bt_config_component_from_arg(component_class_spec
,
1579 if (!cfg
->cmd_data
.query
.cfg_component
) {
1580 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for component class specification:\n %s",
1581 component_class_spec
);
1586 BT_OBJECT_MOVE_REF(cfg
->cmd_data
.query
.cfg_component
->params
, params
);
1588 if (strlen(query_object
) == 0) {
1589 BT_CLI_LOGE_APPEND_CAUSE("Invalid empty object.");
1593 g_string_assign(cfg
->cmd_data
.query
.object
, query_object
);
1598 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1601 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1604 g_string_free(error_str
, TRUE
);
1607 bt_value_put_ref(params
);
1612 * Prints the list-plugins command usage.
1615 void print_list_plugins_usage(FILE *fp
)
1617 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
1619 fprintf(fp
, "Options:\n");
1621 fprintf(fp
, " -h, --help Show this help and quit\n");
1623 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1625 fprintf(fp
, "Use `babeltrace2 help` to get help for a specific plugin or component class.\n");
1629 const struct bt_argpar_opt_descr list_plugins_options
[] = {
1630 /* id, short_name, long_name, with_arg */
1631 { OPT_HELP
, 'h', "help", false },
1632 BT_ARGPAR_OPT_DESCR_SENTINEL
1636 * Creates a Babeltrace config object from the arguments of a
1637 * list-plugins command.
1639 * *retcode is set to the appropriate exit code to use.
1642 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
1643 int *retcode
, const bt_value
*plugin_paths
)
1645 struct bt_config
*cfg
= NULL
;
1646 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1649 cfg
= bt_config_list_plugins_create(plugin_paths
);
1655 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, list_plugins_options
, true);
1656 if (argpar_parse_ret
.error
) {
1657 BT_CLI_LOGE_APPEND_CAUSE(
1658 "While parsing `list-plugins` command's command-line arguments: %s",
1659 argpar_parse_ret
.error
->str
);
1663 if (help_option_is_specified(&argpar_parse_ret
)) {
1664 print_list_plugins_usage(stdout
);
1666 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1670 if (argpar_parse_ret
.items
->len
> 0) {
1672 * At this point we know there's at least one non-option
1673 * argument because we don't reach here with `--help`,
1676 struct bt_argpar_item_non_opt
*non_opt
=
1677 argpar_parse_ret
.items
->pdata
[0];
1679 BT_CLI_LOGE_APPEND_CAUSE(
1680 "Extraneous command-line argument specified to `list-plugins` command: `%s`.",
1689 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1692 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1698 * Prints the run command usage.
1701 void print_run_usage(FILE *fp
)
1703 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n");
1705 fprintf(fp
, "Options:\n");
1707 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
1708 fprintf(fp
, " for all the following components until\n");
1709 fprintf(fp
, " --reset-base-params is encountered\n");
1710 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1711 fprintf(fp
, " -c, --component=NAME:TYPE.PLUGIN.CLS\n");
1712 fprintf(fp
, " Instantiate the component class CLS of type\n");
1713 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
1714 fprintf(fp
, " in the plugin PLUGIN, add it to the graph,\n");
1715 fprintf(fp
, " and name it NAME");
1716 fprintf(fp
, " -x, --connect=CONNECTION Connect two created components (see the\n");
1717 fprintf(fp
, " expected format of CONNECTION below)\n");
1718 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
1719 fprintf(fp
, " (`N`, `T`, `D`, `I`, `W`, `E`, or `F`)\n");
1720 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
1721 fprintf(fp
, " current component (see the expected format\n");
1722 fprintf(fp
, " of PARAMS below)\n");
1723 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters to an\n");
1724 fprintf(fp
, " empty map\n");
1725 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
1726 fprintf(fp
, " the graph later, retry in DUR µs\n");
1727 fprintf(fp
, " (default: 100000)\n");
1728 fprintf(fp
, " -h, --help Show this help and quit\n");
1730 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1731 fprintf(fp
, "\n\n");
1732 fprintf(fp
, "Expected format of CONNECTION\n");
1733 fprintf(fp
, "-----------------------------\n");
1735 fprintf(fp
, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
1737 fprintf(fp
, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
1738 fprintf(fp
, "components to connect together. You must escape the following characters\n\n");
1739 fprintf(fp
, "with `\\`: `\\`, `.`, and `:`. You must set the name of the current\n");
1740 fprintf(fp
, "component using the NAME prefix of the --component option.\n");
1742 fprintf(fp
, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
1743 fprintf(fp
, "identify the upstream and downstream ports to use for the connection.\n");
1744 fprintf(fp
, "When the port is not specified, `*` is used.\n");
1746 fprintf(fp
, "When a component named UPSTREAM has an available port which matches the\n");
1747 fprintf(fp
, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
1748 fprintf(fp
, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
1749 fprintf(fp
, "DOWNSTREAM.\n");
1751 fprintf(fp
, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
1752 fprintf(fp
, "which matches anything. You must escape the following characters\n");
1753 fprintf(fp
, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
1755 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
1756 fprintf(fp
, "can connect a filter component to a sink component.\n");
1758 fprintf(fp
, "Examples:\n");
1760 fprintf(fp
, " my-src:my-sink\n");
1761 fprintf(fp
, " ctf-fs.*stream*:utils-muxer:*\n");
1763 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1764 fprintf(fp
, "babeltrace2 from a shell.\n");
1765 fprintf(fp
, "\n\n");
1766 print_expected_params_format(fp
);
1770 * Creates a Babeltrace config object from the arguments of a run
1773 * *retcode is set to the appropriate exit code to use.
1776 struct bt_config
*bt_config_run_from_args(int argc
, const char *argv
[],
1777 int *retcode
, const bt_value
*plugin_paths
,
1778 int default_log_level
)
1780 struct bt_config_component
*cur_cfg_comp
= NULL
;
1781 bt_value
*cur_base_params
= NULL
;
1783 struct bt_config
*cfg
= NULL
;
1784 bt_value
*instance_names
= NULL
;
1785 bt_value
*connection_args
= NULL
;
1786 char error_buf
[256] = { 0 };
1787 long retry_duration
= -1;
1788 bt_value_map_extend_status extend_status
;
1789 GString
*error_str
= NULL
;
1790 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1793 static const struct bt_argpar_opt_descr run_options
[] = {
1794 { OPT_BASE_PARAMS
, 'b', "base-params", true },
1795 { OPT_COMPONENT
, 'c', "component", true },
1796 { OPT_CONNECT
, 'x', "connect", true },
1797 { OPT_HELP
, 'h', "help", false },
1798 { OPT_LOG_LEVEL
, 'l', "log-level", true },
1799 { OPT_PARAMS
, 'p', "params", true },
1800 { OPT_RESET_BASE_PARAMS
, 'r', "reset-base-params", false },
1801 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
1802 BT_ARGPAR_OPT_DESCR_SENTINEL
1807 error_str
= g_string_new(NULL
);
1809 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1814 print_run_usage(stdout
);
1819 cfg
= bt_config_run_create(plugin_paths
);
1824 cfg
->cmd_data
.run
.retry_duration_us
= 100000;
1825 cur_base_params
= bt_value_map_create();
1826 if (!cur_base_params
) {
1827 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1831 instance_names
= bt_value_map_create();
1832 if (!instance_names
) {
1833 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1837 connection_args
= bt_value_array_create();
1838 if (!connection_args
) {
1839 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1844 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, run_options
, true);
1845 if (argpar_parse_ret
.error
) {
1846 BT_CLI_LOGE_APPEND_CAUSE(
1847 "While parsing `run` command's command-line arguments: %s",
1848 argpar_parse_ret
.error
->str
);
1852 if (help_option_is_specified(&argpar_parse_ret
)) {
1853 print_run_usage(stdout
);
1855 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1859 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
1860 struct bt_argpar_item
*argpar_item
=
1861 g_ptr_array_index(argpar_parse_ret
.items
, i
);
1862 struct bt_argpar_item_opt
*argpar_item_opt
;
1865 /* This command does not accept non-option arguments.*/
1866 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
1867 struct bt_argpar_item_non_opt
*argpar_nonopt_item
=
1868 (struct bt_argpar_item_non_opt
*) argpar_item
;
1870 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`",
1871 argpar_nonopt_item
->arg
);
1875 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
1876 arg
= argpar_item_opt
->arg
;
1878 switch (argpar_item_opt
->descr
->id
) {
1881 enum bt_config_component_dest dest
;
1883 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
1884 cur_cfg_comp
= bt_config_component_from_arg(arg
,
1886 if (!cur_cfg_comp
) {
1887 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --component option's argument:\n %s",
1892 switch (cur_cfg_comp
->type
) {
1893 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1894 dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
1896 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1897 dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
1899 case BT_COMPONENT_CLASS_TYPE_SINK
:
1900 dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
1906 BT_ASSERT(cur_base_params
);
1907 bt_value_put_ref(cur_cfg_comp
->params
);
1908 if (bt_value_copy(cur_base_params
,
1909 &cur_cfg_comp
->params
) < 0) {
1910 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1914 ret
= add_run_cfg_comp_check_name(cfg
,
1927 if (!cur_cfg_comp
) {
1928 BT_CLI_LOGE_APPEND_CAUSE("Cannot add parameters to unavailable component:\n %s",
1933 params
= bt_param_parse(arg
, error_str
);
1935 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1940 extend_status
= bt_value_map_extend(cur_cfg_comp
->params
,
1942 BT_VALUE_PUT_REF_AND_RESET(params
);
1943 if (extend_status
!= BT_VALUE_MAP_EXTEND_STATUS_OK
) {
1944 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current component parameters with --params option's argument:\n %s",
1952 if (!cur_cfg_comp
) {
1953 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the log level of unavailable component:\n %s",
1958 cur_cfg_comp
->log_level
=
1959 bt_log_get_level_from_string(arg
);
1960 if (cur_cfg_comp
->log_level
< 0) {
1961 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
1966 case OPT_BASE_PARAMS
:
1968 bt_value
*params
= bt_param_parse(arg
, error_str
);
1971 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --base-params option's argument:\n %s",
1976 BT_OBJECT_MOVE_REF(cur_base_params
, params
);
1979 case OPT_RESET_BASE_PARAMS
:
1980 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
1981 cur_base_params
= bt_value_map_create();
1982 if (!cur_base_params
) {
1983 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1988 if (bt_value_array_append_string_element(
1989 connection_args
, arg
)) {
1990 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1994 case OPT_RETRY_DURATION
: {
1996 size_t arg_len
= strlen(argpar_item_opt
->arg
);
1998 retry_duration
= g_ascii_strtoll(argpar_item_opt
->arg
, &end
, 10);
2000 if (arg_len
== 0 || end
!= (argpar_item_opt
->arg
+ arg_len
)) {
2001 BT_CLI_LOGE_APPEND_CAUSE(
2002 "Could not parse --retry-duration option's argument as an unsigned integer: `%s`",
2003 argpar_item_opt
->arg
);
2007 if (retry_duration
< 0) {
2008 BT_CLI_LOGE_APPEND_CAUSE("--retry-duration option's argument must be positive or 0: %ld",
2013 cfg
->cmd_data
.run
.retry_duration_us
=
2014 (uint64_t) retry_duration
;
2018 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
2019 argpar_item_opt
->descr
->id
);
2024 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2026 if (cfg
->cmd_data
.run
.sources
->len
== 0) {
2027 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no source component.");
2031 if (cfg
->cmd_data
.run
.sinks
->len
== 0) {
2032 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no sink component.");
2036 ret
= bt_config_cli_args_create_connections(cfg
,
2040 BT_CLI_LOGE_APPEND_CAUSE("Cannot creation connections:\n%s", error_buf
);
2048 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2052 g_string_free(error_str
, TRUE
);
2055 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
2056 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2057 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2058 BT_VALUE_PUT_REF_AND_RESET(instance_names
);
2059 BT_VALUE_PUT_REF_AND_RESET(connection_args
);
2064 struct bt_config
*bt_config_run_from_args_array(const bt_value
*run_args
,
2065 int *retcode
, const bt_value
*plugin_paths
,
2066 int default_log_level
)
2068 struct bt_config
*cfg
= NULL
;
2070 uint64_t i
, len
= bt_value_array_get_length(run_args
);
2072 BT_ASSERT(len
<= SIZE_MAX
);
2073 argv
= calloc((size_t) len
, sizeof(*argv
));
2075 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2079 for (i
= 0; i
< len
; i
++) {
2080 const bt_value
*arg_value
=
2081 bt_value_array_borrow_element_by_index_const(run_args
,
2085 arg
= bt_value_string_get(arg_value
);
2090 cfg
= bt_config_run_from_args((int) len
, argv
, retcode
,
2091 plugin_paths
, default_log_level
);
2099 * Prints the convert command usage.
2102 void print_convert_usage(FILE *fp
)
2104 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2106 fprintf(fp
, "Options:\n");
2108 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2109 fprintf(fp
, " Instantiate the component class CLS of type\n");
2110 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2111 fprintf(fp
, " in the plugin PLUGIN, add it to the\n");
2112 fprintf(fp
, " conversion graph, and optionally name it\n");
2113 fprintf(fp
, " NAME\n");
2114 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
2115 fprintf(fp
, " (`N`, `T`, `D`, `I`, `W`, `E`, or `F`)\n");
2116 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2117 fprintf(fp
, " current component (see the expected format\n");
2118 fprintf(fp
, " of PARAMS below)\n");
2119 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
2120 fprintf(fp
, " the graph later, retry in DUR µs\n");
2121 fprintf(fp
, " (default: 100000)\n");
2122 fprintf(fp
, " dynamic plugins can be loaded\n");
2123 fprintf(fp
, " --run-args Print the equivalent arguments for the\n");
2124 fprintf(fp
, " `run` command to the standard output,\n");
2125 fprintf(fp
, " formatted for a shell, and quit\n");
2126 fprintf(fp
, " --run-args-0 Print the equivalent arguments for the\n");
2127 fprintf(fp
, " `run` command to the standard output,\n");
2128 fprintf(fp
, " formatted for `xargs -0`, and quit\n");
2129 fprintf(fp
, " --stream-intersection Only process events when all streams\n");
2130 fprintf(fp
, " are active\n");
2131 fprintf(fp
, " -h, --help Show this help and quit\n");
2133 fprintf(fp
, "Implicit `source.ctf.fs` component options:\n");
2135 fprintf(fp
, " --clock-force-correlate Force the origin of all clocks\n");
2136 fprintf(fp
, " to the Unix epoch\n");
2137 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2138 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2140 fprintf(fp
, "Implicit `sink.text.pretty` component options:\n");
2142 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
2143 fprintf(fp
, " --clock-date Print timestamp dates\n");
2144 fprintf(fp
, " --clock-gmt Print and parse timestamps in the GMT\n");
2145 fprintf(fp
, " time zone instead of the local time zone\n");
2146 fprintf(fp
, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2147 fprintf(fp
, " of `hh:mm:ss.nnnnnnnnn`\n");
2148 fprintf(fp
, " --color=(never | auto | always)\n");
2149 fprintf(fp
, " Never, automatically, or always emit\n");
2150 fprintf(fp
, " console color codes\n");
2151 fprintf(fp
, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2152 fprintf(fp
, " `all`, `trace`, `trace:hostname`,\n");
2153 fprintf(fp
, " `trace:domain`, `trace:procname`,\n");
2154 fprintf(fp
, " `trace:vpid`, `loglevel`, `emf`\n");
2155 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2156 fprintf(fp
, " `payload` (or `arg` or `args`), `none`,\n");
2157 fprintf(fp
, " `all`, `scope`, `header`, `context`\n");
2158 fprintf(fp
, " (or `ctx`)\n");
2159 fprintf(fp
, " --no-delta Do not print time delta between\n");
2160 fprintf(fp
, " consecutive events\n");
2161 fprintf(fp
, " -w, --output=PATH Write output text to PATH instead of\n");
2162 fprintf(fp
, " the standard output\n");
2164 fprintf(fp
, "Implicit `filter.utils.trimmer` component options:\n");
2166 fprintf(fp
, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2167 fprintf(fp
, " time range to BEGIN (see the format of\n");
2168 fprintf(fp
, " BEGIN below)\n");
2169 fprintf(fp
, " -e, --end=END Set the end time of the conversion time\n");
2170 fprintf(fp
, " range to END (see the format of END below)\n");
2171 fprintf(fp
, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2172 fprintf(fp
, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2173 fprintf(fp
, " `]`) (see the format of BEGIN/END below)\n");
2175 fprintf(fp
, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2177 fprintf(fp
, " --debug-info Create an implicit\n");
2178 fprintf(fp
, " `filter.lttng-utils.debug-info` component\n");
2179 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2180 fprintf(fp
, " instead of `/usr/lib/debug`\n");
2181 fprintf(fp
, " --debug-info-full-path Show full debug info source and\n");
2182 fprintf(fp
, " binary paths instead of just names\n");
2183 fprintf(fp
, " --debug-info-target-prefix=DIR\n");
2184 fprintf(fp
, " Use directory DIR as a prefix when\n");
2185 fprintf(fp
, " looking up executables during debug\n");
2186 fprintf(fp
, " info analysis\n");
2188 fprintf(fp
, "Legacy options that still work:\n");
2190 fprintf(fp
, " -i, --input-format=(ctf | lttng-live)\n");
2191 fprintf(fp
, " `ctf`:\n");
2192 fprintf(fp
, " Create an implicit `source.ctf.fs`\n");
2193 fprintf(fp
, " component\n");
2194 fprintf(fp
, " `lttng-live`:\n");
2195 fprintf(fp
, " Create an implicit `source.ctf.lttng-live`\n");
2196 fprintf(fp
, " component\n");
2197 fprintf(fp
, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2198 fprintf(fp
, " `text`:\n");
2199 fprintf(fp
, " Create an implicit `sink.text.pretty`\n");
2200 fprintf(fp
, " component\n");
2201 fprintf(fp
, " `ctf`:\n");
2202 fprintf(fp
, " Create an implicit `sink.ctf.fs`\n");
2203 fprintf(fp
, " component\n");
2204 fprintf(fp
, " `dummy`:\n");
2205 fprintf(fp
, " Create an implicit `sink.utils.dummy`\n");
2206 fprintf(fp
, " component\n");
2207 fprintf(fp
, " `ctf-metadata`:\n");
2208 fprintf(fp
, " Query the `source.ctf.fs` component class\n");
2209 fprintf(fp
, " for metadata text and quit\n");
2211 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
2212 fprintf(fp
, "\n\n");
2213 fprintf(fp
, "Format of BEGIN and END\n");
2214 fprintf(fp
, "-----------------------\n");
2216 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2217 fprintf(fp
, "\n\n");
2218 print_expected_params_format(fp
);
2222 const struct bt_argpar_opt_descr convert_options
[] = {
2223 /* id, short_name, long_name, with_arg */
2224 { OPT_BEGIN
, 'b', "begin", true },
2225 { OPT_CLOCK_CYCLES
, '\0', "clock-cycles", false },
2226 { OPT_CLOCK_DATE
, '\0', "clock-date", false },
2227 { OPT_CLOCK_FORCE_CORRELATE
, '\0', "clock-force-correlate", false },
2228 { OPT_CLOCK_GMT
, '\0', "clock-gmt", false },
2229 { OPT_CLOCK_OFFSET
, '\0', "clock-offset", true },
2230 { OPT_CLOCK_OFFSET_NS
, '\0', "clock-offset-ns", true },
2231 { OPT_CLOCK_SECONDS
, '\0', "clock-seconds", false },
2232 { OPT_COLOR
, '\0', "color", true },
2233 { OPT_COMPONENT
, 'c', "component", true },
2234 { OPT_DEBUG
, 'd', "debug", false },
2235 { OPT_DEBUG_INFO_DIR
, '\0', "debug-info-dir", true },
2236 { OPT_DEBUG_INFO_FULL_PATH
, '\0', "debug-info-full-path", false },
2237 { OPT_DEBUG_INFO_TARGET_PREFIX
, '\0', "debug-info-target-prefix", true },
2238 { OPT_END
, 'e', "end", true },
2239 { OPT_FIELDS
, 'f', "fields", true },
2240 { OPT_HELP
, 'h', "help", false },
2241 { OPT_INPUT_FORMAT
, 'i', "input-format", true },
2242 { OPT_LOG_LEVEL
, 'l', "log-level", true },
2243 { OPT_NAMES
, 'n', "names", true },
2244 { OPT_DEBUG_INFO
, '\0', "debug-info", false },
2245 { OPT_NO_DELTA
, '\0', "no-delta", false },
2246 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
2247 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
2248 { OPT_OUTPUT
, 'w', "output", true },
2249 { OPT_OUTPUT_FORMAT
, 'o', "output-format", true },
2250 { OPT_PARAMS
, 'p', "params", true },
2251 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
2252 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
2253 { OPT_RUN_ARGS
, '\0', "run-args", false },
2254 { OPT_RUN_ARGS_0
, '\0', "run-args-0", false },
2255 { OPT_STREAM_INTERSECTION
, '\0', "stream-intersection", false },
2256 { OPT_TIMERANGE
, '\0', "timerange", true },
2257 { OPT_VERBOSE
, 'v', "verbose", false },
2258 BT_ARGPAR_OPT_DESCR_SENTINEL
2262 GString
*get_component_auto_name(const char *prefix
,
2263 const bt_value
*existing_names
)
2266 GString
*auto_name
= g_string_new(NULL
);
2269 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2273 if (!bt_value_map_has_entry(existing_names
, prefix
)) {
2274 g_string_assign(auto_name
, prefix
);
2279 g_string_printf(auto_name
, "%s-%d", prefix
, i
);
2281 } while (bt_value_map_has_entry(existing_names
, auto_name
->str
));
2287 struct implicit_component_args
{
2290 /* The component class name (e.g. src.ctf.fs). */
2293 /* The component instance name. */
2296 GString
*params_arg
;
2297 bt_value
*extra_params
;
2301 int assign_name_to_implicit_component(struct implicit_component_args
*args
,
2302 const char *prefix
, bt_value
*existing_names
,
2303 GList
**comp_names
, bool append_to_comp_names
)
2306 GString
*name
= NULL
;
2308 if (!args
->exists
) {
2312 name
= get_component_auto_name(prefix
,
2320 g_string_assign(args
->name_arg
, name
->str
);
2322 if (bt_value_map_insert_entry(existing_names
, name
->str
,
2324 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2329 if (append_to_comp_names
) {
2330 *comp_names
= g_list_append(*comp_names
, name
);
2336 g_string_free(name
, TRUE
);
2343 int append_run_args_for_implicit_component(
2344 struct implicit_component_args
*impl_args
,
2349 GString
*component_arg_for_run
= NULL
;
2351 if (!impl_args
->exists
) {
2355 component_arg_for_run
= g_string_new(NULL
);
2356 if (!component_arg_for_run
) {
2357 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2361 /* Build the full `name:type.plugin.cls`. */
2362 BT_ASSERT(!strchr(impl_args
->name_arg
->str
, '\\'));
2363 BT_ASSERT(!strchr(impl_args
->name_arg
->str
, ':'));
2364 g_string_printf(component_arg_for_run
, "%s:%s",
2365 impl_args
->name_arg
->str
, impl_args
->comp_arg
->str
);
2367 if (bt_value_array_append_string_element(run_args
, "--component")) {
2368 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2372 if (bt_value_array_append_string_element(run_args
,
2373 component_arg_for_run
->str
)) {
2374 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2378 if (impl_args
->params_arg
->len
> 0) {
2379 if (bt_value_array_append_string_element(run_args
, "--params")) {
2380 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2384 if (bt_value_array_append_string_element(run_args
,
2385 impl_args
->params_arg
->str
)) {
2386 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2391 for (i
= 0; i
< bt_value_array_get_length(impl_args
->extra_params
); i
++) {
2392 const bt_value
*elem
;
2395 elem
= bt_value_array_borrow_element_by_index(
2396 impl_args
->extra_params
, i
);
2398 BT_ASSERT(bt_value_is_string(elem
));
2399 arg
= bt_value_string_get(elem
);
2400 ret
= bt_value_array_append_string_element(run_args
, arg
);
2402 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2413 if (component_arg_for_run
) {
2414 g_string_free(component_arg_for_run
, TRUE
);
2420 /* Free the fields of a `struct implicit_component_args`. */
2423 void finalize_implicit_component_args(struct implicit_component_args
*args
)
2427 if (args
->comp_arg
) {
2428 g_string_free(args
->comp_arg
, TRUE
);
2431 if (args
->name_arg
) {
2432 g_string_free(args
->name_arg
, TRUE
);
2435 if (args
->params_arg
) {
2436 g_string_free(args
->params_arg
, TRUE
);
2439 bt_value_put_ref(args
->extra_params
);
2442 /* Destroy a dynamically-allocated `struct implicit_component_args`. */
2445 void destroy_implicit_component_args(struct implicit_component_args
*args
)
2447 finalize_implicit_component_args(args
);
2451 /* Initialize the fields of an already allocated `struct implicit_component_args`. */
2454 int init_implicit_component_args(struct implicit_component_args
*args
,
2455 const char *comp_arg
, bool exists
)
2459 args
->exists
= exists
;
2460 args
->comp_arg
= g_string_new(comp_arg
);
2461 args
->name_arg
= g_string_new(NULL
);
2462 args
->params_arg
= g_string_new(NULL
);
2463 args
->extra_params
= bt_value_array_create();
2465 if (!args
->comp_arg
|| !args
->name_arg
||
2466 !args
->params_arg
|| !args
->extra_params
) {
2468 finalize_implicit_component_args(args
);
2469 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2477 /* Dynamically allocate and initialize a `struct implicit_component_args`. */
2480 struct implicit_component_args
*create_implicit_component_args(
2481 const char *comp_arg
)
2483 struct implicit_component_args
*args
;
2486 args
= g_new(struct implicit_component_args
, 1);
2488 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2492 status
= init_implicit_component_args(args
, comp_arg
, true);
2503 void append_implicit_component_param(struct implicit_component_args
*args
,
2504 const char *key
, const char *value
)
2509 append_param_arg(args
->params_arg
, key
, value
);
2513 * Append the given parameter (`key=value`) to all component specifications
2514 * in `implicit_comp_args` (an array of `struct implicit_component_args *`)
2515 * which match `comp_arg`.
2517 * Return the number of matching components.
2521 int append_multiple_implicit_components_param(GPtrArray
*implicit_comp_args
,
2522 const char *comp_arg
, const char *key
, const char *value
)
2527 for (i
= 0; i
< implicit_comp_args
->len
; i
++) {
2528 struct implicit_component_args
*args
= implicit_comp_args
->pdata
[i
];
2530 if (strcmp(args
->comp_arg
->str
, comp_arg
) == 0) {
2531 append_implicit_component_param(args
, key
, value
);
2539 /* Escape value to make it suitable to use as a string parameter value. */
2541 gchar
*escape_string_value(const char *value
)
2546 ret
= g_string_new(NULL
);
2548 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2557 g_string_append_c(ret
, '\\');
2561 g_string_append_c(ret
, *in
);
2567 return g_string_free(ret
, FALSE
);
2571 int bt_value_to_cli_param_value_append(const bt_value
*value
, GString
*buf
)
2577 switch (bt_value_get_type(value
)) {
2578 case BT_VALUE_TYPE_STRING
:
2580 const char *str_value
= bt_value_string_get(value
);
2581 gchar
*escaped_str_value
;
2583 escaped_str_value
= escape_string_value(str_value
);
2584 if (!escaped_str_value
) {
2588 g_string_append_printf(buf
, "\"%s\"", escaped_str_value
);
2590 g_free(escaped_str_value
);
2593 case BT_VALUE_TYPE_ARRAY
: {
2594 g_string_append_c(buf
, '[');
2595 uint64_t sz
= bt_value_array_get_length(value
);
2596 for (uint64_t i
= 0; i
< sz
; i
++) {
2597 const bt_value
*item
;
2600 g_string_append(buf
, ", ");
2603 item
= bt_value_array_borrow_element_by_index_const(
2605 ret
= bt_value_to_cli_param_value_append(item
, buf
);
2611 g_string_append_c(buf
, ']');
2625 * Convert `value` to its equivalent representation as a command line parameter
2630 gchar
*bt_value_to_cli_param_value(bt_value
*value
)
2633 gchar
*result
= NULL
;
2635 buf
= g_string_new(NULL
);
2637 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2641 if (bt_value_to_cli_param_value_append(value
, buf
)) {
2645 result
= g_string_free(buf
, FALSE
);
2652 g_string_free(buf
, TRUE
);
2660 int append_parameter_to_args(bt_value
*args
, const char *key
, bt_value
*value
)
2663 BT_ASSERT(bt_value_get_type(args
) == BT_VALUE_TYPE_ARRAY
);
2668 gchar
*str_value
= NULL
;
2669 GString
*parameter
= NULL
;
2671 if (bt_value_array_append_string_element(args
, "--params")) {
2672 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2677 str_value
= bt_value_to_cli_param_value(value
);
2683 parameter
= g_string_new(NULL
);
2685 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2690 g_string_printf(parameter
, "%s=%s", key
, str_value
);
2692 if (bt_value_array_append_string_element(args
, parameter
->str
)) {
2693 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2700 g_string_free(parameter
, TRUE
);
2713 int append_string_parameter_to_args(bt_value
*args
, const char *key
, const char *value
)
2715 bt_value
*str_value
;
2718 str_value
= bt_value_string_create_init(value
);
2721 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2726 ret
= append_parameter_to_args(args
, key
, str_value
);
2729 BT_VALUE_PUT_REF_AND_RESET(str_value
);
2734 int append_implicit_component_extra_param(struct implicit_component_args
*args
,
2735 const char *key
, const char *value
)
2737 return append_string_parameter_to_args(args
->extra_params
, key
, value
);
2741 * Escapes `.`, `:`, and `\` of `input` with `\`.
2744 GString
*escape_dot_colon(const char *input
)
2746 GString
*output
= g_string_new(NULL
);
2750 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2754 for (ch
= input
; *ch
!= '\0'; ch
++) {
2755 if (*ch
== '\\' || *ch
== '.' || *ch
== ':') {
2756 g_string_append_c(output
, '\\');
2759 g_string_append_c(output
, *ch
);
2767 * Appends a --connect option to a list of arguments. `upstream_name`
2768 * and `downstream_name` are escaped with escape_dot_colon() in this
2772 int append_connect_arg(bt_value
*run_args
,
2773 const char *upstream_name
, const char *downstream_name
)
2776 GString
*e_upstream_name
= escape_dot_colon(upstream_name
);
2777 GString
*e_downstream_name
= escape_dot_colon(downstream_name
);
2778 GString
*arg
= g_string_new(NULL
);
2780 if (!e_upstream_name
|| !e_downstream_name
|| !arg
) {
2781 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2786 ret
= bt_value_array_append_string_element(run_args
, "--connect");
2788 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2793 g_string_append(arg
, e_upstream_name
->str
);
2794 g_string_append_c(arg
, ':');
2795 g_string_append(arg
, e_downstream_name
->str
);
2796 ret
= bt_value_array_append_string_element(run_args
, arg
->str
);
2798 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2805 g_string_free(arg
, TRUE
);
2808 if (e_upstream_name
) {
2809 g_string_free(e_upstream_name
, TRUE
);
2812 if (e_downstream_name
) {
2813 g_string_free(e_downstream_name
, TRUE
);
2820 * Appends the run command's --connect options for the convert command.
2823 int convert_auto_connect(bt_value
*run_args
,
2824 GList
*source_names
, GList
*filter_names
,
2828 GList
*source_at
= source_names
;
2829 GList
*filter_at
= filter_names
;
2831 GList
*sink_at
= sink_names
;
2833 BT_ASSERT(source_names
);
2834 BT_ASSERT(filter_names
);
2835 BT_ASSERT(sink_names
);
2837 /* Connect all sources to the first filter */
2838 for (source_at
= source_names
; source_at
; source_at
= g_list_next(source_at
)) {
2839 GString
*source_name
= source_at
->data
;
2840 GString
*filter_name
= filter_at
->data
;
2842 ret
= append_connect_arg(run_args
, source_name
->str
,
2849 filter_prev
= filter_at
;
2850 filter_at
= g_list_next(filter_at
);
2852 /* Connect remaining filters */
2853 for (; filter_at
; filter_prev
= filter_at
, filter_at
= g_list_next(filter_at
)) {
2854 GString
*filter_name
= filter_at
->data
;
2855 GString
*filter_prev_name
= filter_prev
->data
;
2857 ret
= append_connect_arg(run_args
, filter_prev_name
->str
,
2864 /* Connect last filter to all sinks */
2865 for (sink_at
= sink_names
; sink_at
; sink_at
= g_list_next(sink_at
)) {
2866 GString
*filter_name
= filter_prev
->data
;
2867 GString
*sink_name
= sink_at
->data
;
2869 ret
= append_connect_arg(run_args
, filter_name
->str
,
2886 int split_timerange(const char *arg
, char **begin
, char **end
)
2889 const char *ch
= arg
;
2891 GString
*g_begin
= NULL
;
2892 GString
*g_end
= NULL
;
2900 g_begin
= bt_common_string_until(ch
, "", ",", &end_pos
);
2901 if (!g_begin
|| ch
[end_pos
] != ',' || g_begin
->len
== 0) {
2907 g_end
= bt_common_string_until(ch
, "", "]", &end_pos
);
2908 if (!g_end
|| g_end
->len
== 0) {
2914 *begin
= g_begin
->str
;
2916 g_string_free(g_begin
, FALSE
);
2917 g_string_free(g_end
, FALSE
);
2927 g_string_free(g_begin
, TRUE
);
2931 g_string_free(g_end
, TRUE
);
2938 int g_list_prepend_gstring(GList
**list
, const char *string
)
2941 GString
*gs
= g_string_new(string
);
2946 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2950 *list
= g_list_prepend(*list
, gs
);
2957 * Create `struct implicit_component_args` structures for each of the
2958 * source components we identified. Add them to `component_args`.
2960 * `non_opts` is an array of the non-option arguments passed on the command
2963 * `non_opt_params` is an array where each element is an array of
2964 * strings containing all the arguments to `--params` that apply to the
2965 * non-option argument at the same index. For example, if, for a
2966 * non-option argument, the following `--params` options applied:
2968 * --params=a=2 --params=b=3,c=4
2970 * its entry in `non_opt_params` would contain
2972 * ["a=2", "b=3,c=4"]
2976 int create_implicit_component_args_from_auto_discovered_sources(
2977 const struct auto_source_discovery
*auto_disc
,
2978 const bt_value
*non_opts
,
2979 const bt_value
*non_opt_params
,
2980 const bt_value
*non_opt_loglevels
,
2981 GPtrArray
*component_args
)
2983 gchar
*cc_name
= NULL
;
2984 struct implicit_component_args
*comp
= NULL
;
2988 len
= auto_disc
->results
->len
;
2990 for (i
= 0; i
< len
; i
++) {
2991 struct auto_source_discovery_result
*res
=
2992 g_ptr_array_index(auto_disc
->results
, i
);
2993 uint64_t orig_indices_i
, orig_indices_count
;
2996 cc_name
= g_strdup_printf("source.%s.%s", res
->plugin_name
, res
->source_cc_name
);
2998 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3002 comp
= create_implicit_component_args(cc_name
);
3008 * Append parameters and log levels of all the
3009 * non-option arguments that contributed to this
3010 * component instance coming into existence.
3012 orig_indices_count
= bt_value_array_get_length(res
->original_input_indices
);
3013 for (orig_indices_i
= 0; orig_indices_i
< orig_indices_count
; orig_indices_i
++) {
3014 const bt_value
*orig_idx_value
=
3015 bt_value_array_borrow_element_by_index(
3016 res
->original_input_indices
, orig_indices_i
);
3017 uint64_t orig_idx
= bt_value_integer_unsigned_get(orig_idx_value
);
3018 const bt_value
*params_array
=
3019 bt_value_array_borrow_element_by_index_const(
3020 non_opt_params
, orig_idx
);
3021 uint64_t params_i
, params_count
;
3022 const bt_value
*loglevel_value
;
3024 params_count
= bt_value_array_get_length(params_array
);
3025 for (params_i
= 0; params_i
< params_count
; params_i
++) {
3026 const bt_value
*params_value
=
3027 bt_value_array_borrow_element_by_index_const(
3028 params_array
, params_i
);
3029 const char *params
= bt_value_string_get(params_value
);
3030 bt_value_array_append_element_status append_status
;
3032 append_status
= bt_value_array_append_string_element(
3033 comp
->extra_params
, "--params");
3034 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3035 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3039 append_status
= bt_value_array_append_string_element(
3040 comp
->extra_params
, params
);
3041 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3042 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3047 loglevel_value
= bt_value_array_borrow_element_by_index_const(
3048 non_opt_loglevels
, orig_idx
);
3049 if (bt_value_get_type(loglevel_value
) == BT_VALUE_TYPE_STRING
) {
3050 const char *loglevel
= bt_value_string_get(loglevel_value
);
3051 bt_value_array_append_element_status append_status
;
3053 append_status
= bt_value_array_append_string_element(
3054 comp
->extra_params
, "--log-level");
3055 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3056 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3060 append_status
= bt_value_array_append_string_element(
3061 comp
->extra_params
, loglevel
);
3062 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3063 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3070 * If single input and a src.ctf.fs component, provide the
3071 * relative path from the path passed on the command line to the
3074 if (bt_value_array_get_length(res
->inputs
) == 1 &&
3075 strcmp(res
->plugin_name
, "ctf") == 0 &&
3076 strcmp(res
->source_cc_name
, "fs") == 0) {
3077 const bt_value
*orig_idx_value
=
3078 bt_value_array_borrow_element_by_index(
3079 res
->original_input_indices
, 0);
3080 uint64_t orig_idx
= bt_value_integer_unsigned_get(orig_idx_value
);
3081 const bt_value
*non_opt_value
=
3082 bt_value_array_borrow_element_by_index_const(
3083 non_opts
, orig_idx
);
3084 const char *non_opt
= bt_value_string_get(non_opt_value
);
3085 const bt_value
*input_value
=
3086 bt_value_array_borrow_element_by_index_const(
3088 const char *input
= bt_value_string_get(input_value
);
3090 BT_ASSERT(orig_indices_count
== 1);
3091 BT_ASSERT(g_str_has_prefix(input
, non_opt
));
3093 input
+= strlen(non_opt
);
3095 while (G_IS_DIR_SEPARATOR(*input
)) {
3099 if (strlen(input
) > 0) {
3100 append_string_parameter_to_args(comp
->extra_params
,
3101 "trace-name", input
);
3105 status
= append_parameter_to_args(comp
->extra_params
, "inputs", res
->inputs
);
3110 g_ptr_array_add(component_args
, comp
);
3124 destroy_implicit_component_args(comp
);
3131 * As we iterate the arguments to the convert command, this tracks what is the
3132 * type of the current item, to which some contextual options (e.g. --params)
3135 enum convert_current_item_type
{
3136 /* There is no current item. */
3137 CONVERT_CURRENT_ITEM_TYPE_NONE
,
3139 /* Current item is a component. */
3140 CONVERT_CURRENT_ITEM_TYPE_COMPONENT
,
3142 /* Current item is a non-option argument. */
3143 CONVERT_CURRENT_ITEM_TYPE_NON_OPT
,
3147 * Creates a Babeltrace config object from the arguments of a convert
3150 * *retcode is set to the appropriate exit code to use.
3153 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3154 int *retcode
, const bt_value
*plugin_paths
,
3155 int *default_log_level
, const bt_interrupter
*interrupter
)
3157 enum convert_current_item_type current_item_type
=
3158 CONVERT_CURRENT_ITEM_TYPE_NONE
;
3160 struct bt_config
*cfg
= NULL
;
3161 bool got_input_format_opt
= false;
3162 bool got_output_format_opt
= false;
3163 bool trimmer_has_begin
= false;
3164 bool trimmer_has_end
= false;
3165 bool stream_intersection_mode
= false;
3166 bool print_run_args
= false;
3167 bool print_run_args_0
= false;
3168 bool print_ctf_metadata
= false;
3169 bt_value
*run_args
= NULL
;
3170 bt_value
*all_names
= NULL
;
3171 GList
*source_names
= NULL
;
3172 GList
*filter_names
= NULL
;
3173 GList
*sink_names
= NULL
;
3174 bt_value
*non_opts
= NULL
;
3175 bt_value
*non_opt_params
= NULL
;
3176 bt_value
*non_opt_loglevels
= NULL
;
3177 struct implicit_component_args implicit_ctf_output_args
= { 0 };
3178 struct implicit_component_args implicit_lttng_live_args
= { 0 };
3179 struct implicit_component_args implicit_dummy_args
= { 0 };
3180 struct implicit_component_args implicit_text_args
= { 0 };
3181 struct implicit_component_args implicit_debug_info_args
= { 0 };
3182 struct implicit_component_args implicit_muxer_args
= { 0 };
3183 struct implicit_component_args implicit_trimmer_args
= { 0 };
3184 char error_buf
[256] = { 0 };
3186 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
3187 char *output
= NULL
;
3188 struct auto_source_discovery auto_disc
= { NULL
};
3189 GString
*auto_disc_comp_name
= NULL
;
3190 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
3191 GString
*name_gstr
= NULL
;
3192 GString
*component_arg_for_run
= NULL
;
3193 bt_value
*live_inputs_array_val
= NULL
;
3196 * Array of `struct implicit_component_args *` created for the sources
3197 * we have auto-discovered.
3199 GPtrArray
*discovered_source_args
= NULL
;
3202 * If set, restrict automatic source discovery to this component class
3205 const char *auto_source_discovery_restrict_plugin_name
= NULL
;
3206 const char *auto_source_discovery_restrict_component_class_name
= NULL
;
3208 bool ctf_fs_source_force_clock_class_unix_epoch_origin
= false;
3209 gchar
*ctf_fs_source_clock_class_offset_arg
= NULL
;
3210 gchar
*ctf_fs_source_clock_class_offset_ns_arg
= NULL
;
3214 print_convert_usage(stdout
);
3219 if (init_implicit_component_args(&implicit_ctf_output_args
,
3220 "sink.ctf.fs", false)) {
3224 if (init_implicit_component_args(&implicit_lttng_live_args
,
3225 "source.ctf.lttng-live", false)) {
3229 if (init_implicit_component_args(&implicit_text_args
,
3230 "sink.text.pretty", false)) {
3234 if (init_implicit_component_args(&implicit_dummy_args
,
3235 "sink.utils.dummy", false)) {
3239 if (init_implicit_component_args(&implicit_debug_info_args
,
3240 "filter.lttng-utils.debug-info", false)) {
3244 if (init_implicit_component_args(&implicit_muxer_args
,
3245 "filter.utils.muxer", true)) {
3249 if (init_implicit_component_args(&implicit_trimmer_args
,
3250 "filter.utils.trimmer", false)) {
3254 all_names
= bt_value_map_create();
3256 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3260 run_args
= bt_value_array_create();
3262 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3266 component_arg_for_run
= g_string_new(NULL
);
3267 if (!component_arg_for_run
) {
3268 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3272 non_opts
= bt_value_array_create();
3274 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3278 non_opt_params
= bt_value_array_create();
3279 if (!non_opt_params
) {
3280 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3284 non_opt_loglevels
= bt_value_array_create();
3285 if (!non_opt_loglevels
) {
3286 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3290 if (auto_source_discovery_init(&auto_disc
) != 0) {
3294 discovered_source_args
=
3295 g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_implicit_component_args
);
3296 if (!discovered_source_args
) {
3297 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3301 auto_disc_comp_name
= g_string_new(NULL
);
3302 if (!auto_disc_comp_name
) {
3303 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3308 * First pass: collect all arguments which need to be passed
3309 * as is to the run command. This pass can also add --name
3310 * arguments if needed to automatically name unnamed component
3313 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, convert_options
, true);
3314 if (argpar_parse_ret
.error
) {
3315 BT_CLI_LOGE_APPEND_CAUSE(
3316 "While parsing `convert` command's command-line arguments: %s",
3317 argpar_parse_ret
.error
->str
);
3321 if (help_option_is_specified(&argpar_parse_ret
)) {
3322 print_convert_usage(stdout
);
3324 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
3328 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
3329 struct bt_argpar_item
*argpar_item
=
3330 g_ptr_array_index(argpar_parse_ret
.items
, i
);
3331 struct bt_argpar_item_opt
*argpar_item_opt
;
3333 char *plugin_name
= NULL
;
3334 char *comp_cls_name
= NULL
;
3337 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
3338 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
3339 arg
= argpar_item_opt
->arg
;
3341 switch (argpar_item_opt
->descr
->id
) {
3344 bt_component_class_type type
;
3346 current_item_type
= CONVERT_CURRENT_ITEM_TYPE_COMPONENT
;
3348 /* Parse the argument */
3349 plugin_comp_cls_names(arg
, &name
, &plugin_name
,
3350 &comp_cls_name
, &type
);
3351 if (!plugin_name
|| !comp_cls_name
) {
3352 BT_CLI_LOGE_APPEND_CAUSE(
3353 "Invalid format for --component option's argument:\n %s",
3360 * Name was given by the user, verify it isn't
3363 if (bt_value_map_has_entry(all_names
, name
)) {
3364 BT_CLI_LOGE_APPEND_CAUSE(
3365 "Duplicate component instance name:\n %s",
3370 name_gstr
= g_string_new(name
);
3372 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3376 g_string_assign(component_arg_for_run
, arg
);
3378 /* Name not given by user, generate one. */
3379 name_gstr
= get_component_auto_name(arg
, all_names
);
3384 g_string_printf(component_arg_for_run
, "%s:%s",
3385 name_gstr
->str
, arg
);
3388 if (bt_value_array_append_string_element(run_args
,
3390 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3394 if (bt_value_array_append_string_element(run_args
,
3395 component_arg_for_run
->str
)) {
3396 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3401 * Remember this name globally, for the uniqueness of
3402 * all component names.
3404 if (bt_value_map_insert_entry(all_names
,
3405 name_gstr
->str
, bt_value_null
)) {
3406 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3411 * Remember this name specifically for the type of the
3412 * component. This is to create connection arguments.
3414 * The list takes ownership of `name_gstr`.
3417 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
3418 source_names
= g_list_append(source_names
, name_gstr
);
3420 case BT_COMPONENT_CLASS_TYPE_FILTER
:
3421 filter_names
= g_list_append(filter_names
, name_gstr
);
3423 case BT_COMPONENT_CLASS_TYPE_SINK
:
3424 sink_names
= g_list_append(sink_names
, name_gstr
);
3433 free(comp_cls_name
);
3436 comp_cls_name
= NULL
;
3440 if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_COMPONENT
) {
3442 * The current item is a component (--component option),
3443 * pass it directly to the run args.
3445 if (bt_value_array_append_string_element(run_args
,
3447 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3451 if (bt_value_array_append_string_element(run_args
, arg
)) {
3452 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3455 } else if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_NON_OPT
) {
3457 * The current item is a
3458 * non-option argument, record
3459 * it in `non_opt_params`.
3462 bt_value_array_append_element_status append_element_status
;
3463 uint64_t idx
= bt_value_array_get_length(non_opt_params
) - 1;
3465 array
= bt_value_array_borrow_element_by_index(non_opt_params
, idx
);
3467 append_element_status
= bt_value_array_append_string_element(array
, arg
);
3468 if (append_element_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3469 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3473 BT_CLI_LOGE_APPEND_CAUSE(
3474 "No current component (--component option) or non-option argument of which to set parameters:\n %s",
3480 if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_COMPONENT
) {
3481 if (bt_value_array_append_string_element(run_args
, "--log-level")) {
3482 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3486 if (bt_value_array_append_string_element(run_args
, arg
)) {
3487 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3490 } else if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_NON_OPT
) {
3491 uint64_t idx
= bt_value_array_get_length(non_opt_loglevels
) - 1;
3492 bt_value
*log_level_str_value
;
3494 log_level_str_value
= bt_value_string_create_init(arg
);
3495 if (!log_level_str_value
) {
3496 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3500 if (bt_value_array_set_element_by_index(non_opt_loglevels
, idx
,
3501 log_level_str_value
)) {
3502 bt_value_put_ref(log_level_str_value
);
3503 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3507 BT_CLI_LOGE_APPEND_CAUSE(
3508 "No current component (--component option) or non-option argument to assign a log level to:\n %s",
3514 case OPT_RETRY_DURATION
:
3515 if (bt_value_array_append_string_element(run_args
,
3516 "--retry-duration")) {
3517 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3521 if (bt_value_array_append_string_element(run_args
, arg
)) {
3522 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3527 case OPT_CLOCK_CYCLES
:
3528 case OPT_CLOCK_DATE
:
3529 case OPT_CLOCK_FORCE_CORRELATE
:
3531 case OPT_CLOCK_OFFSET
:
3532 case OPT_CLOCK_OFFSET_NS
:
3533 case OPT_CLOCK_SECONDS
:
3536 case OPT_DEBUG_INFO
:
3537 case OPT_DEBUG_INFO_DIR
:
3538 case OPT_DEBUG_INFO_FULL_PATH
:
3539 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3542 case OPT_INPUT_FORMAT
:
3545 case OPT_OUTPUT_FORMAT
:
3548 case OPT_RUN_ARGS_0
:
3549 case OPT_STREAM_INTERSECTION
:
3552 /* Ignore in this pass */
3555 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
3556 argpar_item_opt
->descr
->id
);
3559 } else if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
3560 struct bt_argpar_item_non_opt
*argpar_item_non_opt
;
3561 bt_value_array_append_element_status append_status
;
3563 current_item_type
= CONVERT_CURRENT_ITEM_TYPE_NON_OPT
;
3565 argpar_item_non_opt
= (struct bt_argpar_item_non_opt
*) argpar_item
;
3567 append_status
= bt_value_array_append_string_element(non_opts
,
3568 argpar_item_non_opt
->arg
);
3569 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3570 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3574 append_status
= bt_value_array_append_empty_array_element(
3575 non_opt_params
, NULL
);
3576 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3577 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3581 append_status
= bt_value_array_append_element(non_opt_loglevels
, bt_value_null
);
3582 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3583 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3592 * Second pass: transform the convert-specific options and
3593 * arguments into implicit component instances for the run
3596 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
3597 struct bt_argpar_item
*argpar_item
=
3598 g_ptr_array_index(argpar_parse_ret
.items
, i
);
3599 struct bt_argpar_item_opt
*argpar_item_opt
;
3602 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
3606 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
3607 arg
= argpar_item_opt
->arg
;
3609 switch (argpar_item_opt
->descr
->id
) {
3611 if (trimmer_has_begin
) {
3612 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
3617 trimmer_has_begin
= true;
3618 ret
= append_implicit_component_extra_param(
3619 &implicit_trimmer_args
, "begin", arg
);
3620 implicit_trimmer_args
.exists
= true;
3626 if (trimmer_has_end
) {
3627 printf("At --end option: --end or --timerange option already specified\n %s\n",
3632 trimmer_has_end
= true;
3633 ret
= append_implicit_component_extra_param(
3634 &implicit_trimmer_args
, "end", arg
);
3635 implicit_trimmer_args
.exists
= true;
3645 if (trimmer_has_begin
|| trimmer_has_end
) {
3646 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
3651 ret
= split_timerange(arg
, &begin
, &end
);
3653 BT_CLI_LOGE_APPEND_CAUSE("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s",
3658 ret
= append_implicit_component_extra_param(
3659 &implicit_trimmer_args
, "begin", begin
);
3660 ret
|= append_implicit_component_extra_param(
3661 &implicit_trimmer_args
, "end", end
);
3662 implicit_trimmer_args
.exists
= true;
3670 case OPT_CLOCK_CYCLES
:
3671 append_implicit_component_param(
3672 &implicit_text_args
, "clock-cycles", "yes");
3673 implicit_text_args
.exists
= true;
3675 case OPT_CLOCK_DATE
:
3676 append_implicit_component_param(
3677 &implicit_text_args
, "clock-date", "yes");
3678 implicit_text_args
.exists
= true;
3680 case OPT_CLOCK_FORCE_CORRELATE
:
3681 ctf_fs_source_force_clock_class_unix_epoch_origin
= true;
3684 append_implicit_component_param(
3685 &implicit_text_args
, "clock-gmt", "yes");
3686 append_implicit_component_param(
3687 &implicit_trimmer_args
, "gmt", "yes");
3688 implicit_text_args
.exists
= true;
3690 case OPT_CLOCK_OFFSET
:
3691 if (ctf_fs_source_clock_class_offset_arg
) {
3692 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset option\n");
3696 ctf_fs_source_clock_class_offset_arg
= g_strdup(arg
);
3697 if (!ctf_fs_source_clock_class_offset_arg
) {
3698 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3702 case OPT_CLOCK_OFFSET_NS
:
3703 if (ctf_fs_source_clock_class_offset_ns_arg
) {
3704 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset-ns option\n");
3708 ctf_fs_source_clock_class_offset_ns_arg
= g_strdup(arg
);
3709 if (!ctf_fs_source_clock_class_offset_ns_arg
) {
3710 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3714 case OPT_CLOCK_SECONDS
:
3715 append_implicit_component_param(
3716 &implicit_text_args
, "clock-seconds", "yes");
3717 implicit_text_args
.exists
= true;
3720 implicit_text_args
.exists
= true;
3721 ret
= append_implicit_component_extra_param(
3722 &implicit_text_args
, "color", arg
);
3727 case OPT_DEBUG_INFO
:
3728 implicit_debug_info_args
.exists
= true;
3730 case OPT_DEBUG_INFO_DIR
:
3731 implicit_debug_info_args
.exists
= true;
3732 ret
= append_implicit_component_extra_param(
3733 &implicit_debug_info_args
, "debug-info-dir", arg
);
3738 case OPT_DEBUG_INFO_FULL_PATH
:
3739 implicit_debug_info_args
.exists
= true;
3740 append_implicit_component_param(
3741 &implicit_debug_info_args
, "full-path", "yes");
3743 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3744 implicit_debug_info_args
.exists
= true;
3745 ret
= append_implicit_component_extra_param(
3746 &implicit_debug_info_args
,
3747 "target-prefix", arg
);
3754 bt_value
*fields
= fields_from_arg(arg
);
3760 implicit_text_args
.exists
= true;
3761 ret
= insert_flat_params_from_array(
3762 implicit_text_args
.params_arg
,
3764 bt_value_put_ref(fields
);
3772 bt_value
*names
= names_from_arg(arg
);
3778 implicit_text_args
.exists
= true;
3779 ret
= insert_flat_params_from_array(
3780 implicit_text_args
.params_arg
,
3782 bt_value_put_ref(names
);
3789 append_implicit_component_param(
3790 &implicit_text_args
, "no-delta", "yes");
3791 implicit_text_args
.exists
= true;
3793 case OPT_INPUT_FORMAT
:
3794 if (got_input_format_opt
) {
3795 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --input-format option.");
3799 got_input_format_opt
= true;
3801 if (strcmp(arg
, "ctf") == 0) {
3802 auto_source_discovery_restrict_plugin_name
= "ctf";
3803 auto_source_discovery_restrict_component_class_name
= "fs";
3804 } else if (strcmp(arg
, "lttng-live") == 0) {
3805 auto_source_discovery_restrict_plugin_name
= "ctf";
3806 auto_source_discovery_restrict_component_class_name
= "lttng-live";
3807 implicit_lttng_live_args
.exists
= true;
3809 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy input format:\n %s",
3814 case OPT_OUTPUT_FORMAT
:
3815 if (got_output_format_opt
) {
3816 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output-format option.");
3820 got_output_format_opt
= true;
3822 if (strcmp(arg
, "text") == 0) {
3823 implicit_text_args
.exists
= true;
3824 } else if (strcmp(arg
, "ctf") == 0) {
3825 implicit_ctf_output_args
.exists
= true;
3826 } else if (strcmp(arg
, "dummy") == 0) {
3827 implicit_dummy_args
.exists
= true;
3828 } else if (strcmp(arg
, "ctf-metadata") == 0) {
3829 print_ctf_metadata
= true;
3831 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy output format:\n %s",
3838 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output option");
3842 output
= strdup(arg
);
3844 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3849 if (print_run_args_0
) {
3850 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
3854 print_run_args
= true;
3856 case OPT_RUN_ARGS_0
:
3857 if (print_run_args
) {
3858 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
3862 print_run_args_0
= true;
3864 case OPT_STREAM_INTERSECTION
:
3866 * Applies to all traces implementing the
3867 * babeltrace.trace-infos query.
3869 stream_intersection_mode
= true;
3872 *default_log_level
=
3873 logging_level_min(*default_log_level
, BT_LOG_INFO
);
3876 *default_log_level
=
3877 logging_level_min(*default_log_level
, BT_LOG_TRACE
);
3884 set_auto_log_levels(default_log_level
);
3887 * Legacy behaviour: --verbose used to make the `text` output
3888 * format print more information. --verbose is now equivalent to
3889 * the INFO log level, which is why we compare to `BT_LOG_INFO`
3892 if (*default_log_level
== BT_LOG_INFO
) {
3893 append_implicit_component_param(&implicit_text_args
,
3897 /* Print CTF metadata or print LTTng live sessions */
3898 if (print_ctf_metadata
) {
3899 const bt_value
*bt_val_non_opt
;
3901 if (bt_value_array_is_empty(non_opts
)) {
3902 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf-metadata specified without a path.");
3906 if (bt_value_array_get_length(non_opts
) > 1) {
3907 BT_CLI_LOGE_APPEND_CAUSE("Too many paths specified for --output-format=ctf-metadata.");
3911 cfg
= bt_config_print_ctf_metadata_create(plugin_paths
);
3916 bt_val_non_opt
= bt_value_array_borrow_element_by_index_const(non_opts
, 0);
3917 g_string_assign(cfg
->cmd_data
.print_ctf_metadata
.path
,
3918 bt_value_string_get(bt_val_non_opt
));
3922 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
3930 * If -o ctf was specified, make sure an output path (--output)
3931 * was also specified. --output does not imply -o ctf because
3932 * it's also used for the default, implicit -o text if -o ctf
3935 if (implicit_ctf_output_args
.exists
) {
3937 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf specified without --output (trace output path).");
3942 * At this point we know that -o ctf AND --output were
3943 * specified. Make sure that no options were specified
3944 * which would imply -o text because --output would be
3945 * ambiguous in this case. For example, this is wrong:
3947 * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace
3949 * because --names=all implies -o text, and --output
3950 * could apply to both the sink.text.pretty and
3951 * sink.ctf.fs implicit components.
3953 if (implicit_text_args
.exists
) {
3954 BT_CLI_LOGE_APPEND_CAUSE("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text.");
3960 * If -o dummy and -o ctf were not specified, and if there are
3961 * no explicit sink components, then use an implicit
3962 * `sink.text.pretty` component.
3964 if (!implicit_dummy_args
.exists
&& !implicit_ctf_output_args
.exists
&&
3966 implicit_text_args
.exists
= true;
3970 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
3971 * `path` parameter if --output was specified.
3974 if (implicit_text_args
.exists
) {
3975 append_implicit_component_extra_param(&implicit_text_args
,
3977 } else if (implicit_ctf_output_args
.exists
) {
3978 append_implicit_component_extra_param(&implicit_ctf_output_args
,
3983 /* Decide where the non-option argument(s) go */
3984 if (bt_value_array_get_length(non_opts
) > 0) {
3985 if (implicit_lttng_live_args
.exists
) {
3986 const bt_value
*bt_val_non_opt
;
3988 if (bt_value_array_get_length(non_opts
) > 1) {
3989 BT_CLI_LOGE_APPEND_CAUSE("Too many URLs specified for --input-format=lttng-live.");
3993 bt_val_non_opt
= bt_value_array_borrow_element_by_index_const(non_opts
, 0);
3994 lttng_live_url_parts
=
3995 bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_non_opt
),
3996 error_buf
, sizeof(error_buf
));
3997 if (!lttng_live_url_parts
.proto
) {
3998 BT_CLI_LOGE_APPEND_CAUSE("Invalid LTTng live URL format: %s.",
4003 if (!lttng_live_url_parts
.session_name
) {
4004 /* Print LTTng live sessions */
4005 cfg
= bt_config_print_lttng_live_sessions_create(
4011 g_string_assign(cfg
->cmd_data
.print_lttng_live_sessions
.url
,
4012 bt_value_string_get(bt_val_non_opt
));
4016 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
4023 live_inputs_array_val
= bt_value_array_create();
4024 if (!live_inputs_array_val
) {
4025 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4029 if (bt_value_array_append_string_element(
4030 live_inputs_array_val
,
4031 bt_value_string_get(bt_val_non_opt
))) {
4032 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4036 ret
= append_parameter_to_args(
4037 implicit_lttng_live_args
.extra_params
,
4038 "inputs", live_inputs_array_val
);
4043 ret
= append_implicit_component_extra_param(
4044 &implicit_lttng_live_args
,
4045 "session-not-found-action", "end");
4051 size_t plugin_count
;
4052 const bt_plugin
**plugins
;
4053 const bt_plugin
*plugin
;
4055 status
= require_loaded_plugins(plugin_paths
);
4060 if (auto_source_discovery_restrict_plugin_name
) {
4062 plugin
= find_loaded_plugin(auto_source_discovery_restrict_plugin_name
);
4065 plugin_count
= get_loaded_plugins_count();
4066 plugins
= borrow_loaded_plugins();
4069 status
= auto_discover_source_components(non_opts
, plugins
, plugin_count
,
4070 auto_source_discovery_restrict_component_class_name
,
4071 *default_log_level
, &auto_disc
, interrupter
);
4074 if (status
== AUTO_SOURCE_DISCOVERY_STATUS_INTERRUPTED
) {
4075 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
4076 "Babeltrace CLI", "Automatic source discovery interrupted by the user");
4081 status
= create_implicit_component_args_from_auto_discovered_sources(
4082 &auto_disc
, non_opts
, non_opt_params
, non_opt_loglevels
,
4083 discovered_source_args
);
4092 * If --clock-force-correlated was given, apply it to any src.ctf.fs
4095 if (ctf_fs_source_force_clock_class_unix_epoch_origin
) {
4098 n
= append_multiple_implicit_components_param(
4099 discovered_source_args
, "source.ctf.fs", "force-clock-class-origin-unix-epoch",
4102 BT_CLI_LOGE_APPEND_CAUSE("--clock-force-correlate specified, but no source.ctf.fs component instantiated.");
4107 /* If --clock-offset was given, apply it to any src.ctf.fs component. */
4108 if (ctf_fs_source_clock_class_offset_arg
) {
4111 n
= append_multiple_implicit_components_param(
4112 discovered_source_args
, "source.ctf.fs", "clock-class-offset-s",
4113 ctf_fs_source_clock_class_offset_arg
);
4116 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset specified, but no source.ctf.fs component instantiated.");
4121 /* If --clock-offset-ns was given, apply it to any src.ctf.fs component. */
4122 if (ctf_fs_source_clock_class_offset_ns_arg
) {
4125 n
= append_multiple_implicit_components_param(
4126 discovered_source_args
, "source.ctf.fs", "clock-class-offset-ns",
4127 ctf_fs_source_clock_class_offset_ns_arg
);
4130 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset-ns specified, but no source.ctf.fs component instantiated.");
4136 * If the implicit `source.ctf.lttng-live` component exists,
4137 * make sure there's at least one non-option argument (which is
4140 if (implicit_lttng_live_args
.exists
&& bt_value_array_is_empty(non_opts
)) {
4141 BT_CLI_LOGE_APPEND_CAUSE("Missing URL for implicit `%s` component.",
4142 implicit_lttng_live_args
.comp_arg
->str
);
4146 /* Assign names to implicit components */
4147 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4148 struct implicit_component_args
*args
;
4151 args
= discovered_source_args
->pdata
[i
];
4153 g_string_printf(auto_disc_comp_name
, "auto-disc-%s", args
->comp_arg
->str
);
4155 /* Give it a name like `auto-disc-src-ctf-fs`. */
4156 for (j
= 0; j
< auto_disc_comp_name
->len
; j
++) {
4157 if (auto_disc_comp_name
->str
[j
] == '.') {
4158 auto_disc_comp_name
->str
[j
] = '-';
4162 ret
= assign_name_to_implicit_component(args
,
4163 auto_disc_comp_name
->str
, all_names
, &source_names
, true);
4169 ret
= assign_name_to_implicit_component(&implicit_lttng_live_args
,
4170 "lttng-live", all_names
, &source_names
, true);
4175 ret
= assign_name_to_implicit_component(&implicit_text_args
,
4176 "pretty", all_names
, &sink_names
, true);
4181 ret
= assign_name_to_implicit_component(&implicit_ctf_output_args
,
4182 "sink-ctf-fs", all_names
, &sink_names
, true);
4187 ret
= assign_name_to_implicit_component(&implicit_dummy_args
,
4188 "dummy", all_names
, &sink_names
, true);
4193 ret
= assign_name_to_implicit_component(&implicit_muxer_args
,
4194 "muxer", all_names
, NULL
, false);
4199 ret
= assign_name_to_implicit_component(&implicit_trimmer_args
,
4200 "trimmer", all_names
, NULL
, false);
4205 ret
= assign_name_to_implicit_component(&implicit_debug_info_args
,
4206 "debug-info", all_names
, NULL
, false);
4211 /* Make sure there's at least one source and one sink */
4212 if (!source_names
) {
4213 BT_CLI_LOGE_APPEND_CAUSE("No source component.");
4218 BT_CLI_LOGE_APPEND_CAUSE("No sink component.");
4222 /* Make sure there's a single sink component */
4223 if (g_list_length(sink_names
) != 1) {
4224 BT_CLI_LOGE_APPEND_CAUSE(
4225 "More than one sink component specified.");
4230 * Prepend the muxer, the trimmer, and the debug info to the
4231 * filter chain so that we have:
4233 * sources -> muxer -> [trimmer] -> [debug info] ->
4234 * [user filters] -> sinks
4236 if (implicit_debug_info_args
.exists
) {
4237 if (g_list_prepend_gstring(&filter_names
,
4238 implicit_debug_info_args
.name_arg
->str
)) {
4243 if (implicit_trimmer_args
.exists
) {
4244 if (g_list_prepend_gstring(&filter_names
,
4245 implicit_trimmer_args
.name_arg
->str
)) {
4250 if (g_list_prepend_gstring(&filter_names
,
4251 implicit_muxer_args
.name_arg
->str
)) {
4256 * Append the equivalent run arguments for the implicit
4259 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4260 struct implicit_component_args
*args
=
4261 discovered_source_args
->pdata
[i
];
4263 ret
= append_run_args_for_implicit_component(args
, run_args
);
4269 ret
= append_run_args_for_implicit_component(&implicit_lttng_live_args
,
4275 ret
= append_run_args_for_implicit_component(&implicit_text_args
,
4281 ret
= append_run_args_for_implicit_component(&implicit_ctf_output_args
,
4287 ret
= append_run_args_for_implicit_component(&implicit_dummy_args
,
4293 ret
= append_run_args_for_implicit_component(&implicit_muxer_args
,
4299 ret
= append_run_args_for_implicit_component(&implicit_trimmer_args
,
4305 ret
= append_run_args_for_implicit_component(&implicit_debug_info_args
,
4311 /* Auto-connect components */
4312 ret
= convert_auto_connect(run_args
, source_names
, filter_names
,
4315 BT_CLI_LOGE_APPEND_CAUSE("Cannot auto-connect components.");
4320 * We have all the run command arguments now. Depending on
4321 * --run-args, we pass this to the run command or print them
4324 if (print_run_args
|| print_run_args_0
) {
4325 uint64_t args_idx
, args_len
;
4326 if (stream_intersection_mode
) {
4327 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --stream-intersection with --run-args or --run-args-0.");
4331 args_len
= bt_value_array_get_length(run_args
);
4332 for (args_idx
= 0; args_idx
< args_len
; args_idx
++) {
4333 const bt_value
*arg_value
=
4334 bt_value_array_borrow_element_by_index(run_args
,
4337 GString
*quoted
= NULL
;
4338 const char *arg_to_print
;
4340 arg
= bt_value_string_get(arg_value
);
4342 if (print_run_args
) {
4343 quoted
= bt_common_shell_quote(arg
, true);
4348 arg_to_print
= quoted
->str
;
4353 printf("%s", arg_to_print
);
4356 g_string_free(quoted
, TRUE
);
4359 if (args_idx
< args_len
- 1) {
4360 if (print_run_args
) {
4369 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4373 cfg
= bt_config_run_from_args_array(run_args
, retcode
,
4374 plugin_paths
, *default_log_level
);
4379 cfg
->cmd_data
.run
.stream_intersection_mode
= stream_intersection_mode
;
4384 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4387 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
4391 if (component_arg_for_run
) {
4392 g_string_free(component_arg_for_run
, TRUE
);
4396 g_string_free(name_gstr
, TRUE
);
4399 bt_value_put_ref(live_inputs_array_val
);
4400 bt_value_put_ref(run_args
);
4401 bt_value_put_ref(all_names
);
4402 destroy_glist_of_gstring(source_names
);
4403 destroy_glist_of_gstring(filter_names
);
4404 destroy_glist_of_gstring(sink_names
);
4405 bt_value_put_ref(non_opt_params
);
4406 bt_value_put_ref(non_opt_loglevels
);
4407 bt_value_put_ref(non_opts
);
4408 finalize_implicit_component_args(&implicit_ctf_output_args
);
4409 finalize_implicit_component_args(&implicit_lttng_live_args
);
4410 finalize_implicit_component_args(&implicit_dummy_args
);
4411 finalize_implicit_component_args(&implicit_text_args
);
4412 finalize_implicit_component_args(&implicit_debug_info_args
);
4413 finalize_implicit_component_args(&implicit_muxer_args
);
4414 finalize_implicit_component_args(&implicit_trimmer_args
);
4415 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
4416 auto_source_discovery_fini(&auto_disc
);
4418 if (discovered_source_args
) {
4419 g_ptr_array_free(discovered_source_args
, TRUE
);
4422 g_free(ctf_fs_source_clock_class_offset_arg
);
4423 g_free(ctf_fs_source_clock_class_offset_ns_arg
);
4425 if (auto_disc_comp_name
) {
4426 g_string_free(auto_disc_comp_name
, TRUE
);
4433 * Prints the Babeltrace 2.x general usage.
4436 void print_gen_usage(FILE *fp
)
4438 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4440 fprintf(fp
, "General options:\n");
4442 fprintf(fp
, " -d, --debug Enable debug mode (same as --log-level=T)\n");
4443 fprintf(fp
, " -h, --help Show this help and quit\n");
4444 fprintf(fp
, " -l, --log-level=LVL Set the default log level to LVL (`N`, `T`, `D`,\n");
4445 fprintf(fp
, " `I`, `W` (default), `E`, or `F`)\n");
4446 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
4447 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
4448 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
4449 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
4450 fprintf(fp
, " dynamic plugins can be loaded\n");
4451 fprintf(fp
, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4452 fprintf(fp
, " -V, --version Show version and quit\n");
4454 fprintf(fp
, "Available commands:\n");
4456 fprintf(fp
, " convert Convert and trim traces (default)\n");
4457 fprintf(fp
, " help Get help for a plugin or a component class\n");
4458 fprintf(fp
, " list-plugins List available plugins and their content\n");
4459 fprintf(fp
, " query Query objects from a component class\n");
4460 fprintf(fp
, " run Build a processing graph and run it\n");
4462 fprintf(fp
, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n");
4465 struct bt_config
*bt_config_cli_args_create(int argc
, const char *argv
[],
4466 int *retcode
, bool omit_system_plugin_path
,
4467 bool omit_home_plugin_path
,
4468 const bt_value
*initial_plugin_paths
,
4469 const bt_interrupter
*interrupter
)
4471 struct bt_config
*config
= NULL
;
4474 const char **top_level_argv
;
4475 int command_argc
= -1;
4476 const char **command_argv
= NULL
;
4477 const char *command_name
= NULL
;
4478 int default_log_level
= -1;
4479 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
4480 bt_value
*plugin_paths
= NULL
;
4482 /* Top-level option descriptions. */
4483 static const struct bt_argpar_opt_descr descrs
[] = {
4484 { OPT_DEBUG
, 'd', "debug", false },
4485 { OPT_HELP
, 'h', "help", false },
4486 { OPT_LOG_LEVEL
, 'l', "log-level", true },
4487 { OPT_VERBOSE
, 'v', "verbose", false },
4488 { OPT_VERSION
, 'V', "version", false},
4489 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
4490 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
4491 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
4492 BT_ARGPAR_OPT_DESCR_SENTINEL
4496 COMMAND_TYPE_NONE
= -1,
4497 COMMAND_TYPE_RUN
= 0,
4498 COMMAND_TYPE_CONVERT
,
4499 COMMAND_TYPE_LIST_PLUGINS
,
4502 } command_type
= COMMAND_TYPE_NONE
;
4506 if (!initial_plugin_paths
) {
4507 plugin_paths
= bt_value_array_create();
4508 if (!plugin_paths
) {
4512 bt_value_copy_status copy_status
= bt_value_copy(
4513 initial_plugin_paths
, &plugin_paths
);
4519 BT_ASSERT(plugin_paths
);
4522 * The `BABELTRACE_PLUGIN_PATH` paths take precedence over the
4523 * `--plugin-path` option's paths, so append it now before
4524 * parsing the general options.
4526 if (append_env_var_plugin_paths(plugin_paths
)) {
4533 print_gen_usage(stdout
);
4537 /* Skip first argument, the name of the program. */
4538 top_level_argc
= argc
- 1;
4539 top_level_argv
= argv
+ 1;
4540 argpar_parse_ret
= bt_argpar_parse(top_level_argc
, top_level_argv
,
4543 if (argpar_parse_ret
.error
) {
4544 BT_CLI_LOGE_APPEND_CAUSE(
4545 "While parsing command-line arguments: %s",
4546 argpar_parse_ret
.error
->str
);
4550 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
4551 struct bt_argpar_item
*item
;
4553 item
= g_ptr_array_index(argpar_parse_ret
.items
, i
);
4555 if (item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
4556 struct bt_argpar_item_opt
*item_opt
=
4557 (struct bt_argpar_item_opt
*) item
;
4559 switch (item_opt
->descr
->id
) {
4562 logging_level_min(default_log_level
, BT_LOG_TRACE
);
4566 logging_level_min(default_log_level
, BT_LOG_INFO
);
4570 int level
= bt_log_get_level_from_string(item_opt
->arg
);
4573 BT_CLI_LOGE_APPEND_CAUSE(
4574 "Invalid argument for --log-level option:\n %s",
4580 logging_level_min(default_log_level
, level
);
4583 case OPT_PLUGIN_PATH
:
4584 if (bt_config_append_plugin_paths_check_setuid_setgid(
4585 plugin_paths
, item_opt
->arg
)) {
4589 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
4590 omit_system_plugin_path
= true;
4592 case OPT_OMIT_HOME_PLUGIN_PATH
:
4593 omit_home_plugin_path
= true;
4599 print_gen_usage(stdout
);
4602 } else if (item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
4603 struct bt_argpar_item_non_opt
*item_non_opt
=
4604 (struct bt_argpar_item_non_opt
*) item
;
4606 * First unknown argument: is it a known command
4610 top_level_argc
- item_non_opt
->orig_index
- 1;
4612 &top_level_argv
[item_non_opt
->orig_index
+ 1];
4614 if (strcmp(item_non_opt
->arg
, "convert") == 0) {
4615 command_type
= COMMAND_TYPE_CONVERT
;
4616 command_name
= "convert";
4617 } else if (strcmp(item_non_opt
->arg
, "list-plugins") == 0) {
4618 command_type
= COMMAND_TYPE_LIST_PLUGINS
;
4619 command_name
= "list-plugins";
4620 } else if (strcmp(item_non_opt
->arg
, "help") == 0) {
4621 command_type
= COMMAND_TYPE_HELP
;
4622 command_name
= "help";
4623 } else if (strcmp(item_non_opt
->arg
, "query") == 0) {
4624 command_type
= COMMAND_TYPE_QUERY
;
4625 command_name
= "query";
4626 } else if (strcmp(item_non_opt
->arg
, "run") == 0) {
4627 command_type
= COMMAND_TYPE_RUN
;
4628 command_name
= "run";
4631 * Non-option argument, but not a known
4632 * command name: assume the default
4633 * `convert` command.
4635 command_type
= COMMAND_TYPE_CONVERT
;
4636 command_name
= "convert";
4644 if (command_type
== COMMAND_TYPE_NONE
) {
4645 if (argpar_parse_ret
.ingested_orig_args
== top_level_argc
) {
4647 * We only got non-help, non-version general options
4648 * like --verbose and --debug, without any other
4649 * arguments, so we can't do anything useful: print the
4652 print_gen_usage(stdout
);
4657 * We stopped on an unknown option argument (and therefore
4658 * didn't see a command name). Assume `convert` command.
4660 command_type
= COMMAND_TYPE_CONVERT
;
4661 command_name
= "convert";
4663 top_level_argc
- argpar_parse_ret
.ingested_orig_args
;
4665 &top_level_argv
[argpar_parse_ret
.ingested_orig_args
];
4668 BT_ASSERT(command_argv
);
4669 BT_ASSERT(command_argc
>= 0);
4672 * For all commands other than `convert`, we now know the log level to
4673 * use, so we can apply it with `set_auto_log_levels`.
4675 * The convert command has `--debug` and `--verbose` arguments that are
4676 * equivalent to the top-level arguments of the same name. So after it
4677 * has parsed its arguments, `bt_config_convert_from_args` calls
4678 * `set_auto_log_levels` itself.
4680 if (command_type
!= COMMAND_TYPE_CONVERT
) {
4681 set_auto_log_levels(&default_log_level
);
4685 * At this point, `plugin_paths` contains the initial plugin
4686 * paths, the paths from the `BABELTRACE_PLUGIN_PATH` paths, and
4687 * the paths from the `--plugin-path` option.
4689 * Now append the user and system plugin paths.
4691 if (append_home_and_system_plugin_paths(plugin_paths
,
4692 omit_system_plugin_path
, omit_home_plugin_path
)) {
4696 switch (command_type
) {
4697 case COMMAND_TYPE_RUN
:
4698 config
= bt_config_run_from_args(command_argc
, command_argv
,
4699 retcode
, plugin_paths
,
4702 case COMMAND_TYPE_CONVERT
:
4703 config
= bt_config_convert_from_args(command_argc
, command_argv
,
4704 retcode
, plugin_paths
, &default_log_level
, interrupter
);
4706 case COMMAND_TYPE_LIST_PLUGINS
:
4707 config
= bt_config_list_plugins_from_args(command_argc
,
4708 command_argv
, retcode
, plugin_paths
);
4710 case COMMAND_TYPE_HELP
:
4711 config
= bt_config_help_from_args(command_argc
,
4712 command_argv
, retcode
, plugin_paths
,
4715 case COMMAND_TYPE_QUERY
:
4716 config
= bt_config_query_from_args(command_argc
,
4717 command_argv
, retcode
, plugin_paths
,
4725 BT_ASSERT(default_log_level
>= BT_LOG_TRACE
);
4726 config
->log_level
= default_log_level
;
4727 config
->command_name
= command_name
;
4736 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
4737 bt_value_put_ref(plugin_paths
);