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"
39 #include <sys/types.h>
40 #include "babeltrace2-cfg.h"
41 #include "babeltrace2-cfg-cli-args.h"
42 #include "babeltrace2-cfg-cli-args-connect.h"
43 #include "babeltrace2-cfg-cli-params-arg.h"
44 #include "common/version.h"
46 static const int cli_default_log_level
= BT_LOG_WARNING
;
48 /* INI-style parsing FSM states */
49 enum ini_parsing_fsm_state
{
50 /* Expect a map key (identifier) */
53 /* Expect an equal character ('=') */
59 /* Expect a comma character (',') */
63 /* INI-style parsing state variables */
64 struct ini_parsing_state
{
65 /* Lexical scanner (owned by this) */
68 /* Output map value object being filled (owned by this) */
71 /* Next expected FSM state */
72 enum ini_parsing_fsm_state expecting
;
74 /* Last decoded map key (owned by this) */
77 /* Complete INI-style string to parse (not owned by this) */
80 /* Error buffer (not owned by this) */
84 /* Offset option with "is set" boolean */
90 /* Legacy "ctf"/"lttng-live" format options */
91 struct ctf_legacy_opts
{
92 struct offset_opt offset_s
;
93 struct offset_opt offset_ns
;
94 bool stream_intersection
;
97 /* Legacy "text" format options */
98 struct text_legacy_opts
{
100 * output, dbg_info_dir, dbg_info_target_prefix, names,
101 * and fields are owned by this.
104 GString
*dbg_info_dir
;
105 GString
*dbg_info_target_prefix
;
106 const bt_value
*names
;
107 const bt_value
*fields
;
115 bool dbg_info_full_path
;
119 /* Legacy input format format */
120 enum legacy_input_format
{
121 LEGACY_INPUT_FORMAT_NONE
= 0,
122 LEGACY_INPUT_FORMAT_CTF
,
123 LEGACY_INPUT_FORMAT_LTTNG_LIVE
,
126 /* Legacy output format format */
127 enum legacy_output_format
{
128 LEGACY_OUTPUT_FORMAT_NONE
= 0,
129 LEGACY_OUTPUT_FORMAT_TEXT
,
130 LEGACY_OUTPUT_FORMAT_DUMMY
,
133 #define BT_CLI_LOGE_APPEND_CAUSE_OOM() BT_CLI_LOGE_APPEND_CAUSE("Out of memory.")
136 * Returns the plugin name, component class name, component class type,
137 * and component name from a command-line --component option's argument.
138 * arg must have the following format:
140 * [NAME:]TYPE.PLUGIN.CLS
142 * where NAME is the optional component name, TYPE is either `source`,
143 * `filter`, or `sink`, PLUGIN is the plugin name, and CLS is the
144 * component class name.
146 * On success, both *plugin and *component are not NULL. *plugin
147 * and *comp_cls are owned by the caller. On success, *name can be NULL
148 * if no component class name was found, and *comp_cls_type is set.
151 void plugin_comp_cls_names(const char *arg
, char **name
, char **plugin
,
152 char **comp_cls
, bt_component_class_type
*comp_cls_type
)
154 const char *at
= arg
;
155 GString
*gs_name
= NULL
;
156 GString
*gs_comp_cls_type
= NULL
;
157 GString
*gs_plugin
= NULL
;
158 GString
*gs_comp_cls
= NULL
;
164 BT_ASSERT(comp_cls_type
);
166 if (!bt_common_string_is_printable(arg
)) {
167 BT_CLI_LOGE_APPEND_CAUSE("Argument contains a non-printable character.");
171 /* Parse the component name */
172 gs_name
= bt_common_string_until(at
, ".:\\", ":", &end_pos
);
177 if (arg
[end_pos
] == ':') {
181 g_string_assign(gs_name
, "");
184 /* Parse the component class type */
185 gs_comp_cls_type
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
186 if (!gs_comp_cls_type
|| at
[end_pos
] == '\0') {
187 BT_CLI_LOGE_APPEND_CAUSE("Missing component class type (`source`, `filter`, or `sink`).");
191 if (strcmp(gs_comp_cls_type
->str
, "source") == 0 ||
192 strcmp(gs_comp_cls_type
->str
, "src") == 0) {
193 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SOURCE
;
194 } else if (strcmp(gs_comp_cls_type
->str
, "filter") == 0 ||
195 strcmp(gs_comp_cls_type
->str
, "flt") == 0) {
196 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_FILTER
;
197 } else if (strcmp(gs_comp_cls_type
->str
, "sink") == 0) {
198 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SINK
;
200 BT_CLI_LOGE_APPEND_CAUSE("Unknown component class type: `%s`.",
201 gs_comp_cls_type
->str
);
207 /* Parse the plugin name */
208 gs_plugin
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
209 if (!gs_plugin
|| gs_plugin
->len
== 0 || at
[end_pos
] == '\0') {
210 BT_CLI_LOGE_APPEND_CAUSE("Missing plugin or component class name.");
216 /* Parse the component class name */
217 gs_comp_cls
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
218 if (!gs_comp_cls
|| gs_comp_cls
->len
== 0) {
219 BT_CLI_LOGE_APPEND_CAUSE("Missing component class name.");
223 if (at
[end_pos
] != '\0') {
224 /* Found a non-escaped `.` */
229 if (gs_name
->len
== 0) {
231 g_string_free(gs_name
, TRUE
);
233 *name
= gs_name
->str
;
234 g_string_free(gs_name
, FALSE
);
237 g_string_free(gs_name
, TRUE
);
240 *plugin
= gs_plugin
->str
;
241 *comp_cls
= gs_comp_cls
->str
;
242 g_string_free(gs_plugin
, FALSE
);
243 g_string_free(gs_comp_cls
, FALSE
);
259 g_string_free(gs_name
, TRUE
);
263 g_string_free(gs_plugin
, TRUE
);
267 g_string_free(gs_comp_cls
, TRUE
);
270 if (gs_comp_cls_type
) {
271 g_string_free(gs_comp_cls_type
, TRUE
);
278 * Prints the Babeltrace version.
281 void print_version(void)
283 if (GIT_VERSION
[0] == '\0') {
284 puts("Babeltrace " VERSION
);
286 puts("Babeltrace " VERSION
" - " GIT_VERSION
);
291 * Destroys a component configuration.
294 void bt_config_component_destroy(bt_object
*obj
)
296 struct bt_config_component
*bt_config_component
=
297 container_of(obj
, struct bt_config_component
, base
);
303 if (bt_config_component
->plugin_name
) {
304 g_string_free(bt_config_component
->plugin_name
, TRUE
);
307 if (bt_config_component
->comp_cls_name
) {
308 g_string_free(bt_config_component
->comp_cls_name
, TRUE
);
311 if (bt_config_component
->instance_name
) {
312 g_string_free(bt_config_component
->instance_name
, TRUE
);
315 BT_VALUE_PUT_REF_AND_RESET(bt_config_component
->params
);
316 g_free(bt_config_component
);
323 * Creates a component configuration using the given plugin name and
324 * component name. `plugin_name` and `comp_cls_name` are copied (belong
325 * to the return value).
327 * Return value is owned by the caller.
330 struct bt_config_component
*bt_config_component_create(
331 bt_component_class_type type
,
332 const char *plugin_name
, const char *comp_cls_name
,
335 struct bt_config_component
*cfg_component
= NULL
;
337 cfg_component
= g_new0(struct bt_config_component
, 1);
338 if (!cfg_component
) {
339 BT_CLI_LOGE_APPEND_CAUSE_OOM();
343 bt_object_init_shared(&cfg_component
->base
,
344 bt_config_component_destroy
);
345 cfg_component
->type
= type
;
346 cfg_component
->plugin_name
= g_string_new(plugin_name
);
347 if (!cfg_component
->plugin_name
) {
348 BT_CLI_LOGE_APPEND_CAUSE_OOM();
352 cfg_component
->comp_cls_name
= g_string_new(comp_cls_name
);
353 if (!cfg_component
->comp_cls_name
) {
354 BT_CLI_LOGE_APPEND_CAUSE_OOM();
358 cfg_component
->instance_name
= g_string_new(NULL
);
359 if (!cfg_component
->instance_name
) {
360 BT_CLI_LOGE_APPEND_CAUSE_OOM();
364 cfg_component
->log_level
= init_log_level
;
366 /* Start with empty parameters */
367 cfg_component
->params
= bt_value_map_create();
368 if (!cfg_component
->params
) {
369 BT_CLI_LOGE_APPEND_CAUSE_OOM();
376 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
379 return cfg_component
;
383 * Creates a component configuration from a command-line --component
387 struct bt_config_component
*bt_config_component_from_arg(const char *arg
,
390 struct bt_config_component
*cfg_comp
= NULL
;
392 char *plugin_name
= NULL
;
393 char *comp_cls_name
= NULL
;
394 bt_component_class_type type
;
396 plugin_comp_cls_names(arg
, &name
, &plugin_name
, &comp_cls_name
, &type
);
397 if (!plugin_name
|| !comp_cls_name
) {
401 cfg_comp
= bt_config_component_create(type
, plugin_name
, comp_cls_name
,
408 g_string_assign(cfg_comp
->instance_name
, name
);
414 BT_OBJECT_PUT_REF_AND_RESET(cfg_comp
);
419 g_free(comp_cls_name
);
424 * Destroys a configuration.
427 void bt_config_destroy(bt_object
*obj
)
429 struct bt_config
*cfg
=
430 container_of(obj
, struct bt_config
, base
);
436 BT_VALUE_PUT_REF_AND_RESET(cfg
->plugin_paths
);
438 switch (cfg
->command
) {
439 case BT_CONFIG_COMMAND_RUN
:
440 if (cfg
->cmd_data
.run
.sources
) {
441 g_ptr_array_free(cfg
->cmd_data
.run
.sources
, TRUE
);
444 if (cfg
->cmd_data
.run
.filters
) {
445 g_ptr_array_free(cfg
->cmd_data
.run
.filters
, TRUE
);
448 if (cfg
->cmd_data
.run
.sinks
) {
449 g_ptr_array_free(cfg
->cmd_data
.run
.sinks
, TRUE
);
452 if (cfg
->cmd_data
.run
.connections
) {
453 g_ptr_array_free(cfg
->cmd_data
.run
.connections
,
457 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
459 case BT_CONFIG_COMMAND_HELP
:
460 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.help
.cfg_component
);
462 case BT_CONFIG_COMMAND_QUERY
:
463 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.query
.cfg_component
);
465 if (cfg
->cmd_data
.query
.object
) {
466 g_string_free(cfg
->cmd_data
.query
.object
, TRUE
);
469 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
470 if (cfg
->cmd_data
.print_ctf_metadata
.path
) {
471 g_string_free(cfg
->cmd_data
.print_ctf_metadata
.path
,
474 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
478 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
479 if (cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
481 cfg
->cmd_data
.print_lttng_live_sessions
.url
,
484 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
499 void destroy_glist_of_gstring(GList
*list
)
507 for (at
= list
; at
!= NULL
; at
= g_list_next(at
)) {
508 g_string_free(at
->data
, TRUE
);
515 * Creates a simple lexical scanner for parsing comma-delimited names
518 * Return value is owned by the caller.
521 GScanner
*create_csv_identifiers_scanner(void)
524 GScannerConfig scanner_config
= {
525 .cset_skip_characters
= " \t\n",
526 .cset_identifier_first
= G_CSET_a_2_z G_CSET_A_2_Z
"_",
527 .cset_identifier_nth
= G_CSET_a_2_z G_CSET_A_2_Z
":_-",
528 .case_sensitive
= TRUE
,
529 .cpair_comment_single
= NULL
,
530 .skip_comment_multi
= TRUE
,
531 .skip_comment_single
= TRUE
,
532 .scan_comment_multi
= FALSE
,
533 .scan_identifier
= TRUE
,
534 .scan_identifier_1char
= TRUE
,
535 .scan_identifier_NULL
= FALSE
,
536 .scan_symbols
= FALSE
,
537 .symbol_2_token
= FALSE
,
538 .scope_0_fallback
= FALSE
,
539 .scan_binary
= FALSE
,
543 .scan_hex_dollar
= FALSE
,
544 .numbers_2_int
= FALSE
,
545 .int_2_float
= FALSE
,
546 .store_int64
= FALSE
,
547 .scan_string_sq
= FALSE
,
548 .scan_string_dq
= FALSE
,
549 .identifier_2_string
= FALSE
,
550 .char_2_token
= TRUE
,
553 scanner
= g_scanner_new(&scanner_config
);
555 BT_CLI_LOGE_APPEND_CAUSE_OOM();
562 * Converts a comma-delimited list of known names (--names option) to
563 * an array value object containing those names as string value objects.
565 * Return value is owned by the caller.
568 bt_value
*names_from_arg(const char *arg
)
570 GScanner
*scanner
= NULL
;
571 bt_value
*names
= NULL
;
572 bool found_all
= false, found_none
= false, found_item
= false;
574 names
= bt_value_array_create();
576 BT_CLI_LOGE_APPEND_CAUSE_OOM();
580 scanner
= create_csv_identifiers_scanner();
585 g_scanner_input_text(scanner
, arg
, strlen(arg
));
588 GTokenType token_type
= g_scanner_get_next_token(scanner
);
590 switch (token_type
) {
591 case G_TOKEN_IDENTIFIER
:
593 const char *identifier
= scanner
->value
.v_identifier
;
595 if (strcmp(identifier
, "payload") == 0 ||
596 strcmp(identifier
, "args") == 0 ||
597 strcmp(identifier
, "arg") == 0) {
599 if (bt_value_array_append_string_element(names
,
603 } else if (strcmp(identifier
, "context") == 0 ||
604 strcmp(identifier
, "ctx") == 0) {
606 if (bt_value_array_append_string_element(names
,
610 } else if (strcmp(identifier
, "scope") == 0 ||
611 strcmp(identifier
, "header") == 0) {
613 if (bt_value_array_append_string_element(names
,
617 } else if (strcmp(identifier
, "all") == 0) {
619 if (bt_value_array_append_string_element(names
,
623 } else if (strcmp(identifier
, "none") == 0) {
625 if (bt_value_array_append_string_element(names
,
630 BT_CLI_LOGE_APPEND_CAUSE("Unknown name: `%s`.",
646 if (found_none
&& found_all
) {
647 BT_CLI_LOGE_APPEND_CAUSE("Only either `all` or `none` can be specified in the list given to the --names option, but not both.");
651 * Legacy behavior is to clear the defaults (show none) when at
652 * least one item is specified.
654 if (found_item
&& !found_none
&& !found_all
) {
655 if (bt_value_array_append_string_element(names
, "none")) {
660 g_scanner_destroy(scanner
);
665 BT_VALUE_PUT_REF_AND_RESET(names
);
667 g_scanner_destroy(scanner
);
673 * Converts a comma-delimited list of known fields (--fields option) to
674 * an array value object containing those fields as string
677 * Return value is owned by the caller.
680 bt_value
*fields_from_arg(const char *arg
)
682 GScanner
*scanner
= NULL
;
685 fields
= bt_value_array_create();
687 BT_CLI_LOGE_APPEND_CAUSE_OOM();
691 scanner
= create_csv_identifiers_scanner();
696 g_scanner_input_text(scanner
, arg
, strlen(arg
));
699 GTokenType token_type
= g_scanner_get_next_token(scanner
);
701 switch (token_type
) {
702 case G_TOKEN_IDENTIFIER
:
704 const char *identifier
= scanner
->value
.v_identifier
;
706 if (strcmp(identifier
, "trace") == 0 ||
707 strcmp(identifier
, "trace:hostname") == 0 ||
708 strcmp(identifier
, "trace:domain") == 0 ||
709 strcmp(identifier
, "trace:procname") == 0 ||
710 strcmp(identifier
, "trace:vpid") == 0 ||
711 strcmp(identifier
, "loglevel") == 0 ||
712 strcmp(identifier
, "emf") == 0 ||
713 strcmp(identifier
, "callsite") == 0 ||
714 strcmp(identifier
, "all") == 0) {
715 if (bt_value_array_append_string_element(fields
,
720 BT_CLI_LOGE_APPEND_CAUSE("Unknown field: `%s`.",
738 BT_VALUE_PUT_REF_AND_RESET(fields
);
742 g_scanner_destroy(scanner
);
748 void append_param_arg(GString
*params_arg
, const char *key
, const char *value
)
750 BT_ASSERT(params_arg
);
754 if (params_arg
->len
!= 0) {
755 g_string_append_c(params_arg
, ',');
758 g_string_append(params_arg
, key
);
759 g_string_append_c(params_arg
, '=');
760 g_string_append(params_arg
, value
);
764 * Inserts the equivalent "prefix-NAME=yes" strings into params_arg
765 * where the names are in names_array.
768 int insert_flat_params_from_array(GString
*params_arg
,
769 const bt_value
*names_array
, const char *prefix
)
773 GString
*tmpstr
= NULL
, *default_value
= NULL
;
774 bool default_set
= false, non_default_set
= false;
777 * names_array may be NULL if no CLI options were specified to
778 * trigger its creation.
784 tmpstr
= g_string_new(NULL
);
786 BT_CLI_LOGE_APPEND_CAUSE_OOM();
791 default_value
= g_string_new(NULL
);
792 if (!default_value
) {
793 BT_CLI_LOGE_APPEND_CAUSE_OOM();
798 for (i
= 0; i
< bt_value_array_get_size(names_array
); i
++) {
799 const bt_value
*str_obj
=
800 bt_value_array_borrow_element_by_index_const(names_array
,
803 bool is_default
= false;
806 BT_CLI_LOGE_APPEND_CAUSE("Unexpected error.");
811 suffix
= bt_value_string_get(str_obj
);
813 g_string_assign(tmpstr
, prefix
);
814 g_string_append(tmpstr
, "-");
816 /* Special-case for "all" and "none". */
817 if (strcmp(suffix
, "all") == 0) {
819 g_string_assign(default_value
, "show");
820 } else if (strcmp(suffix
, "none") == 0) {
822 g_string_assign(default_value
, "hide");
826 g_string_append(tmpstr
, "default");
827 append_param_arg(params_arg
, tmpstr
->str
,
830 non_default_set
= true;
831 g_string_append(tmpstr
, suffix
);
832 append_param_arg(params_arg
, tmpstr
->str
, "yes");
836 /* Implicit field-default=hide if any non-default option is set. */
837 if (non_default_set
&& !default_set
) {
838 g_string_assign(tmpstr
, prefix
);
839 g_string_append(tmpstr
, "-default");
840 g_string_assign(default_value
, "hide");
841 append_param_arg(params_arg
, tmpstr
->str
, default_value
->str
);
846 g_string_free(default_value
, TRUE
);
850 g_string_free(tmpstr
, TRUE
);
863 OPT_CLOCK_FORCE_CORRELATE
,
874 OPT_DEBUG_INFO_FULL_PATH
,
875 OPT_DEBUG_INFO_TARGET_PREFIX
,
885 OPT_OMIT_HOME_PLUGIN_PATH
,
886 OPT_OMIT_SYSTEM_PLUGIN_PATH
,
892 OPT_RESET_BASE_PARAMS
,
896 OPT_STREAM_INTERSECTION
,
902 enum bt_config_component_dest
{
903 BT_CONFIG_COMPONENT_DEST_UNKNOWN
= -1,
904 BT_CONFIG_COMPONENT_DEST_SOURCE
,
905 BT_CONFIG_COMPONENT_DEST_FILTER
,
906 BT_CONFIG_COMPONENT_DEST_SINK
,
910 * Adds a configuration component to the appropriate configuration
911 * array depending on the destination.
914 void add_run_cfg_comp(struct bt_config
*cfg
,
915 struct bt_config_component
*cfg_comp
,
916 enum bt_config_component_dest dest
)
918 bt_object_get_ref(cfg_comp
);
921 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
922 g_ptr_array_add(cfg
->cmd_data
.run
.sources
, cfg_comp
);
924 case BT_CONFIG_COMPONENT_DEST_FILTER
:
925 g_ptr_array_add(cfg
->cmd_data
.run
.filters
, cfg_comp
);
927 case BT_CONFIG_COMPONENT_DEST_SINK
:
928 g_ptr_array_add(cfg
->cmd_data
.run
.sinks
, cfg_comp
);
936 int add_run_cfg_comp_check_name(struct bt_config
*cfg
,
937 struct bt_config_component
*cfg_comp
,
938 enum bt_config_component_dest dest
,
939 bt_value
*instance_names
)
943 if (cfg_comp
->instance_name
->len
== 0) {
944 BT_CLI_LOGE_APPEND_CAUSE("Found an unnamed component.");
949 if (bt_value_map_has_entry(instance_names
,
950 cfg_comp
->instance_name
->str
)) {
951 BT_CLI_LOGE_APPEND_CAUSE("Duplicate component instance name:\n %s",
952 cfg_comp
->instance_name
->str
);
957 if (bt_value_map_insert_entry(instance_names
,
958 cfg_comp
->instance_name
->str
, bt_value_null
)) {
959 BT_CLI_LOGE_APPEND_CAUSE_OOM();
964 add_run_cfg_comp(cfg
, cfg_comp
, dest
);
971 int append_env_var_plugin_paths(bt_value
*plugin_paths
)
976 if (bt_common_is_setuid_setgid()) {
977 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
981 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
986 ret
= bt_config_append_plugin_paths(plugin_paths
, envvar
);
990 BT_CLI_LOGE_APPEND_CAUSE("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH.");
997 int append_home_and_system_plugin_paths(bt_value
*plugin_paths
,
998 bool omit_system_plugin_path
, bool omit_home_plugin_path
)
1002 if (!omit_home_plugin_path
) {
1003 if (bt_common_is_setuid_setgid()) {
1004 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1006 char *home_plugin_dir
= bt_common_get_home_plugin_path(
1007 BT_LOG_OUTPUT_LEVEL
);
1009 if (home_plugin_dir
) {
1010 ret
= bt_config_append_plugin_paths(
1011 plugin_paths
, home_plugin_dir
);
1012 free(home_plugin_dir
);
1015 BT_CLI_LOGE_APPEND_CAUSE("Invalid home plugin path.");
1022 if (!omit_system_plugin_path
) {
1023 if (bt_config_append_plugin_paths(plugin_paths
,
1024 bt_common_get_system_plugin_path())) {
1025 BT_CLI_LOGE_APPEND_CAUSE("Invalid system plugin path.");
1031 BT_CLI_LOGE_APPEND_CAUSE("Cannot append home and system plugin paths.");
1036 int append_home_and_system_plugin_paths_cfg(struct bt_config
*cfg
)
1038 return append_home_and_system_plugin_paths(cfg
->plugin_paths
,
1039 cfg
->omit_system_plugin_path
, cfg
->omit_home_plugin_path
);
1043 struct bt_config
*bt_config_base_create(enum bt_config_command command
,
1044 const bt_value
*initial_plugin_paths
,
1047 struct bt_config
*cfg
;
1050 cfg
= g_new0(struct bt_config
, 1);
1052 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1056 bt_object_init_shared(&cfg
->base
, bt_config_destroy
);
1057 cfg
->command
= command
;
1058 cfg
->command_needs_plugins
= needs_plugins
;
1060 if (initial_plugin_paths
) {
1061 bt_value
*initial_plugin_paths_copy
;
1063 (void) bt_value_copy(initial_plugin_paths
,
1064 &initial_plugin_paths_copy
);
1065 cfg
->plugin_paths
= initial_plugin_paths_copy
;
1067 cfg
->plugin_paths
= bt_value_array_create();
1068 if (!cfg
->plugin_paths
) {
1069 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1077 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1084 struct bt_config
*bt_config_run_create(
1085 const bt_value
*initial_plugin_paths
)
1087 struct bt_config
*cfg
;
1090 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_RUN
,
1091 initial_plugin_paths
, true);
1096 cfg
->cmd_data
.run
.sources
= g_ptr_array_new_with_free_func(
1097 (GDestroyNotify
) bt_object_put_ref
);
1098 if (!cfg
->cmd_data
.run
.sources
) {
1099 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1103 cfg
->cmd_data
.run
.filters
= g_ptr_array_new_with_free_func(
1104 (GDestroyNotify
) bt_object_put_ref
);
1105 if (!cfg
->cmd_data
.run
.filters
) {
1106 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1110 cfg
->cmd_data
.run
.sinks
= g_ptr_array_new_with_free_func(
1111 (GDestroyNotify
) bt_object_put_ref
);
1112 if (!cfg
->cmd_data
.run
.sinks
) {
1113 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1117 cfg
->cmd_data
.run
.connections
= g_ptr_array_new_with_free_func(
1118 (GDestroyNotify
) bt_config_connection_destroy
);
1119 if (!cfg
->cmd_data
.run
.connections
) {
1120 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1127 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1134 struct bt_config
*bt_config_list_plugins_create(
1135 const bt_value
*initial_plugin_paths
)
1137 struct bt_config
*cfg
;
1140 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS
,
1141 initial_plugin_paths
, true);
1149 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1156 struct bt_config
*bt_config_help_create(
1157 const bt_value
*initial_plugin_paths
,
1158 int default_log_level
)
1160 struct bt_config
*cfg
;
1163 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_HELP
,
1164 initial_plugin_paths
, true);
1169 cfg
->cmd_data
.help
.cfg_component
=
1170 bt_config_component_create(-1, NULL
, NULL
, default_log_level
);
1171 if (!cfg
->cmd_data
.help
.cfg_component
) {
1178 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1185 struct bt_config
*bt_config_query_create(
1186 const bt_value
*initial_plugin_paths
)
1188 struct bt_config
*cfg
;
1191 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_QUERY
,
1192 initial_plugin_paths
, true);
1197 cfg
->cmd_data
.query
.object
= g_string_new(NULL
);
1198 if (!cfg
->cmd_data
.query
.object
) {
1199 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1206 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1213 struct bt_config
*bt_config_print_ctf_metadata_create(
1214 const bt_value
*initial_plugin_paths
)
1216 struct bt_config
*cfg
;
1219 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_CTF_METADATA
,
1220 initial_plugin_paths
, true);
1225 cfg
->cmd_data
.print_ctf_metadata
.path
= g_string_new(NULL
);
1226 if (!cfg
->cmd_data
.print_ctf_metadata
.path
) {
1227 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1231 cfg
->cmd_data
.print_ctf_metadata
.output_path
= g_string_new(NULL
);
1232 if (!cfg
->cmd_data
.print_ctf_metadata
.output_path
) {
1233 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1240 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1247 struct bt_config
*bt_config_print_lttng_live_sessions_create(
1248 const bt_value
*initial_plugin_paths
)
1250 struct bt_config
*cfg
;
1253 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
,
1254 initial_plugin_paths
, true);
1259 cfg
->cmd_data
.print_lttng_live_sessions
.url
= g_string_new(NULL
);
1260 if (!cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
1261 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1265 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
=
1267 if (!cfg
->cmd_data
.print_lttng_live_sessions
.output_path
) {
1268 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1275 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1282 int bt_config_append_plugin_paths_check_setuid_setgid(
1283 bt_value
*plugin_paths
, const char *arg
)
1287 if (bt_common_is_setuid_setgid()) {
1288 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1292 if (bt_config_append_plugin_paths(plugin_paths
, arg
)) {
1293 BT_CLI_LOGE_APPEND_CAUSE("Invalid --plugin-path option's argument:\n %s",
1304 * Prints the expected format for a --params option.
1307 void print_expected_params_format(FILE *fp
)
1309 fprintf(fp
, "Expected format of PARAMS\n");
1310 fprintf(fp
, "-------------------------\n");
1312 fprintf(fp
, " PARAM=VALUE[,PARAM=VALUE]...\n");
1314 fprintf(fp
, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
1315 fprintf(fp
, "where PARAM is the parameter name (C identifier plus the [:.-] characters),\n");
1316 fprintf(fp
, "and VALUE can be one of:\n");
1318 fprintf(fp
, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
1319 fprintf(fp
, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
1320 fprintf(fp
, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
1321 fprintf(fp
, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
1322 fprintf(fp
, " (`0x` prefix) unsigned (with `+` prefix) or signed 64-bit integer.\n");
1323 fprintf(fp
, "* Double precision floating point number (scientific notation is accepted).\n");
1324 fprintf(fp
, "* Unquoted string with no special characters, and not matching any of\n");
1325 fprintf(fp
, " the null and boolean value symbols above.\n");
1326 fprintf(fp
, "* Double-quoted string (accepts escape characters).\n");
1327 fprintf(fp
, "* Array, formatted as an opening `[`, a list of comma-separated values\n");
1328 fprintf(fp
, " (as described by the current list) and a closing `]`.\n");
1330 fprintf(fp
, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1332 fprintf(fp
, "Example:\n");
1334 fprintf(fp
, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
1335 fprintf(fp
, " play=+23, observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
1336 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\",\n");
1337 fprintf(fp
, " things=[1, \"2\", 3]\n");
1339 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1340 fprintf(fp
, "babeltrace2 from a shell.\n");
1345 * Prints the help command usage.
1348 void print_help_usage(FILE *fp
)
1350 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1351 fprintf(fp
, " babeltrace2 [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
1353 fprintf(fp
, "Options:\n");
1355 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1356 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1357 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1358 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1359 fprintf(fp
, " dynamic plugins can be loaded\n");
1360 fprintf(fp
, " -h, --help Show this help and quit\n");
1362 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1364 fprintf(fp
, "Use `babeltrace2 list-plugins` to show the list of available plugins.\n");
1368 struct poptOption help_long_options
[] = {
1369 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
1370 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
1371 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
1372 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
1373 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
1374 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
1378 * Creates a Babeltrace config object from the arguments of a help
1381 * *retcode is set to the appropriate exit code to use.
1384 struct bt_config
*bt_config_help_from_args(int argc
, const char *argv
[],
1385 int *retcode
, bool force_omit_system_plugin_path
,
1386 bool force_omit_home_plugin_path
,
1387 const bt_value
*initial_plugin_paths
, int default_log_level
)
1389 poptContext pc
= NULL
;
1393 struct bt_config
*cfg
= NULL
;
1394 const char *leftover
;
1395 char *plugin_name
= NULL
, *comp_cls_name
= NULL
;
1398 cfg
= bt_config_help_create(initial_plugin_paths
, default_log_level
);
1403 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1404 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1405 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1411 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
1412 help_long_options
, 0);
1414 BT_CLI_LOGE_APPEND_CAUSE("Cannot get popt context.");
1418 poptReadDefaultConfig(pc
, 0);
1420 while ((opt
= poptGetNextOpt(pc
)) > 0) {
1421 arg
= poptGetOptArg(pc
);
1424 case OPT_PLUGIN_PATH
:
1425 if (bt_config_append_plugin_paths_check_setuid_setgid(
1426 cfg
->plugin_paths
, arg
)) {
1430 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1431 cfg
->omit_system_plugin_path
= true;
1433 case OPT_OMIT_HOME_PLUGIN_PATH
:
1434 cfg
->omit_home_plugin_path
= true;
1437 print_help_usage(stdout
);
1439 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1442 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1451 /* Check for option parsing error */
1453 BT_CLI_LOGE_APPEND_CAUSE("While parsing command-line options, at option %s: `%s`.",
1454 poptBadOption(pc
, 0), poptStrerror(opt
));
1458 leftover
= poptGetArg(pc
);
1460 plugin_comp_cls_names(leftover
, NULL
,
1461 &plugin_name
, &comp_cls_name
,
1462 &cfg
->cmd_data
.help
.cfg_component
->type
);
1463 if (plugin_name
&& comp_cls_name
) {
1464 /* Component class help */
1466 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1469 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
,
1472 /* Fall back to plugin help */
1474 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1478 print_help_usage(stdout
);
1480 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1484 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1492 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1495 g_free(plugin_name
);
1496 g_free(comp_cls_name
);
1499 poptFreeContext(pc
);
1507 * Prints the help command usage.
1510 void print_query_usage(FILE *fp
)
1512 fprintf(fp
, "Usage: babeltrace2 [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
1514 fprintf(fp
, "Options:\n");
1516 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1517 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1518 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1519 fprintf(fp
, " -p, --params=PARAMS Set the query parameters to PARAMS\n");
1520 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1521 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1522 fprintf(fp
, " dynamic plugins can be loaded\n");
1523 fprintf(fp
, " -h, --help Show this help and quit\n");
1524 fprintf(fp
, "\n\n");
1525 print_expected_params_format(fp
);
1529 struct poptOption query_long_options
[] = {
1530 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
1531 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
1532 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
1533 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
1534 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
1535 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
1536 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
1540 * Creates a Babeltrace config object from the arguments of a query
1543 * *retcode is set to the appropriate exit code to use.
1546 struct bt_config
*bt_config_query_from_args(int argc
, const char *argv
[],
1547 int *retcode
, bool force_omit_system_plugin_path
,
1548 bool force_omit_home_plugin_path
,
1549 const bt_value
*initial_plugin_paths
,
1550 int default_log_level
)
1552 poptContext pc
= NULL
;
1556 struct bt_config
*cfg
= NULL
;
1557 const char *leftover
;
1559 GString
*error_str
= NULL
;
1561 params
= bt_value_null
;
1562 bt_value_get_ref(bt_value_null
);
1565 cfg
= bt_config_query_create(initial_plugin_paths
);
1570 error_str
= g_string_new(NULL
);
1572 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1576 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1577 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1578 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1584 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
1585 query_long_options
, 0);
1587 BT_CLI_LOGE_APPEND_CAUSE("Cannot get popt context.");
1591 poptReadDefaultConfig(pc
, 0);
1593 while ((opt
= poptGetNextOpt(pc
)) > 0) {
1594 arg
= poptGetOptArg(pc
);
1597 case OPT_PLUGIN_PATH
:
1598 if (bt_config_append_plugin_paths_check_setuid_setgid(
1599 cfg
->plugin_paths
, arg
)) {
1603 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1604 cfg
->omit_system_plugin_path
= true;
1606 case OPT_OMIT_HOME_PLUGIN_PATH
:
1607 cfg
->omit_home_plugin_path
= true;
1611 bt_value_put_ref(params
);
1612 params
= cli_value_from_arg(arg
, error_str
);
1614 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1621 print_query_usage(stdout
);
1623 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1626 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1635 /* Check for option parsing error */
1637 BT_CLI_LOGE_APPEND_CAUSE("While parsing command-line options, at option %s: `%s`.",
1638 poptBadOption(pc
, 0), poptStrerror(opt
));
1643 * We need exactly two leftover arguments which are the
1644 * mandatory component class specification and query object.
1646 leftover
= poptGetArg(pc
);
1648 cfg
->cmd_data
.query
.cfg_component
=
1649 bt_config_component_from_arg(leftover
,
1651 if (!cfg
->cmd_data
.query
.cfg_component
) {
1652 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for component class specification:\n %s",
1658 BT_OBJECT_MOVE_REF(cfg
->cmd_data
.query
.cfg_component
->params
,
1661 print_query_usage(stdout
);
1663 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1667 leftover
= poptGetArg(pc
);
1669 if (strlen(leftover
) == 0) {
1670 BT_CLI_LOGE_APPEND_CAUSE("Invalid empty object.");
1674 g_string_assign(cfg
->cmd_data
.query
.object
, leftover
);
1676 print_query_usage(stdout
);
1678 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1682 leftover
= poptGetArg(pc
);
1684 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`.", leftover
);
1688 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1696 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1700 poptFreeContext(pc
);
1704 g_string_free(error_str
, TRUE
);
1707 bt_value_put_ref(params
);
1713 * Prints the list-plugins command usage.
1716 void print_list_plugins_usage(FILE *fp
)
1718 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
1720 fprintf(fp
, "Options:\n");
1722 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1723 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1724 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1725 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1726 fprintf(fp
, " dynamic plugins can be loaded\n");
1727 fprintf(fp
, " -h, --help Show this help and quit\n");
1729 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1731 fprintf(fp
, "Use `babeltrace2 help` to get help for a specific plugin or component class.\n");
1735 struct poptOption list_plugins_long_options
[] = {
1736 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
1737 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
1738 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
1739 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
1740 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
1741 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
1745 * Creates a Babeltrace config object from the arguments of a
1746 * list-plugins command.
1748 * *retcode is set to the appropriate exit code to use.
1751 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
1752 int *retcode
, bool force_omit_system_plugin_path
,
1753 bool force_omit_home_plugin_path
,
1754 const bt_value
*initial_plugin_paths
)
1756 poptContext pc
= NULL
;
1760 struct bt_config
*cfg
= NULL
;
1761 const char *leftover
;
1764 cfg
= bt_config_list_plugins_create(initial_plugin_paths
);
1769 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1770 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1771 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1777 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
1778 list_plugins_long_options
, 0);
1780 BT_CLI_LOGE_APPEND_CAUSE("Cannot get popt context.");
1784 poptReadDefaultConfig(pc
, 0);
1786 while ((opt
= poptGetNextOpt(pc
)) > 0) {
1787 arg
= poptGetOptArg(pc
);
1790 case OPT_PLUGIN_PATH
:
1791 if (bt_config_append_plugin_paths_check_setuid_setgid(
1792 cfg
->plugin_paths
, arg
)) {
1796 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1797 cfg
->omit_system_plugin_path
= true;
1799 case OPT_OMIT_HOME_PLUGIN_PATH
:
1800 cfg
->omit_home_plugin_path
= true;
1803 print_list_plugins_usage(stdout
);
1805 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1808 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1817 /* Check for option parsing error */
1819 BT_CLI_LOGE_APPEND_CAUSE("While parsing command-line options, at option %s: %s",
1820 poptBadOption(pc
, 0), poptStrerror(opt
));
1824 leftover
= poptGetArg(pc
);
1826 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`.", leftover
);
1830 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1838 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1842 poptFreeContext(pc
);
1850 * Prints the run command usage.
1853 void print_run_usage(FILE *fp
)
1855 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n");
1857 fprintf(fp
, "Options:\n");
1859 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
1860 fprintf(fp
, " for all the following components until\n");
1861 fprintf(fp
, " --reset-base-params is encountered\n");
1862 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1863 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
1864 fprintf(fp
, " Instantiate the component class CLS of type\n");
1865 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
1866 fprintf(fp
, " in the plugin PLUGIN, add it to the graph,\n");
1867 fprintf(fp
, " and optionally name it NAME (you can also\n");
1868 fprintf(fp
, " specify the name with --name)\n");
1869 fprintf(fp
, " -x, --connect=CONNECTION Connect two created components (see the\n");
1870 fprintf(fp
, " expected format of CONNECTION below)\n");
1871 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
1872 fprintf(fp
, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
1873 fprintf(fp
, " -n, --name=NAME Set the name of the current component\n");
1874 fprintf(fp
, " to NAME (must be unique amongst all the\n");
1875 fprintf(fp
, " names of the created components)\n");
1876 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1877 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1878 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1879 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
1880 fprintf(fp
, " current component (see the expected format\n");
1881 fprintf(fp
, " of PARAMS below)\n");
1882 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1883 fprintf(fp
, " dynamic plugins can be loaded\n");
1884 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters to an\n");
1885 fprintf(fp
, " empty map\n");
1886 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
1887 fprintf(fp
, " the graph later, retry in DUR µs\n");
1888 fprintf(fp
, " (default: 100000)\n");
1889 fprintf(fp
, " -h, --help Show this help and quit\n");
1891 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1892 fprintf(fp
, "\n\n");
1893 fprintf(fp
, "Expected format of CONNECTION\n");
1894 fprintf(fp
, "-----------------------------\n");
1896 fprintf(fp
, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
1898 fprintf(fp
, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
1899 fprintf(fp
, "components to connect together. You must escape the following characters\n\n");
1900 fprintf(fp
, "with `\\`: `\\`, `.`, and `:`. You can set the name of the current\n");
1901 fprintf(fp
, "component with the --name option.\n");
1903 fprintf(fp
, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
1904 fprintf(fp
, "identify the upstream and downstream ports to use for the connection.\n");
1905 fprintf(fp
, "When the port is not specified, `*` is used.\n");
1907 fprintf(fp
, "When a component named UPSTREAM has an available port which matches the\n");
1908 fprintf(fp
, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
1909 fprintf(fp
, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
1910 fprintf(fp
, "DOWNSTREAM.\n");
1912 fprintf(fp
, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
1913 fprintf(fp
, "which matches anything. You must escape the following characters\n");
1914 fprintf(fp
, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
1916 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
1917 fprintf(fp
, "can connect a filter component to a sink component.\n");
1919 fprintf(fp
, "Examples:\n");
1921 fprintf(fp
, " my-src:my-sink\n");
1922 fprintf(fp
, " ctf-fs.*stream*:utils-muxer:*\n");
1924 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1925 fprintf(fp
, "babeltrace2 from a shell.\n");
1926 fprintf(fp
, "\n\n");
1927 print_expected_params_format(fp
);
1931 * Creates a Babeltrace config object from the arguments of a run
1934 * *retcode is set to the appropriate exit code to use.
1937 struct bt_config
*bt_config_run_from_args(int argc
, const char *argv
[],
1938 int *retcode
, bool force_omit_system_plugin_path
,
1939 bool force_omit_home_plugin_path
,
1940 const bt_value
*initial_plugin_paths
, int default_log_level
)
1942 poptContext pc
= NULL
;
1944 struct bt_config_component
*cur_cfg_comp
= NULL
;
1945 enum bt_config_component_dest cur_cfg_comp_dest
=
1946 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
1947 bt_value
*cur_base_params
= NULL
;
1949 struct bt_config
*cfg
= NULL
;
1950 bt_value
*instance_names
= NULL
;
1951 bt_value
*connection_args
= NULL
;
1952 char error_buf
[256] = { 0 };
1953 long retry_duration
= -1;
1954 bt_value_map_extend_status extend_status
;
1955 GString
*error_str
= NULL
;
1956 struct poptOption run_long_options
[] = {
1957 { "base-params", 'b', POPT_ARG_STRING
, NULL
, OPT_BASE_PARAMS
, NULL
, NULL
},
1958 { "component", 'c', POPT_ARG_STRING
, NULL
, OPT_COMPONENT
, NULL
, NULL
},
1959 { "connect", 'x', POPT_ARG_STRING
, NULL
, OPT_CONNECT
, NULL
, NULL
},
1960 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
1961 { "log-level", 'l', POPT_ARG_STRING
, NULL
, OPT_LOG_LEVEL
, NULL
, NULL
},
1962 { "name", 'n', POPT_ARG_STRING
, NULL
, OPT_NAME
, NULL
, NULL
},
1963 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
1964 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
1965 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
1966 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
1967 { "reset-base-params", 'r', POPT_ARG_NONE
, NULL
, OPT_RESET_BASE_PARAMS
, NULL
, NULL
},
1968 { "retry-duration", '\0', POPT_ARG_LONG
, &retry_duration
, OPT_RETRY_DURATION
, NULL
, NULL
},
1969 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
1974 error_str
= g_string_new(NULL
);
1976 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1981 print_run_usage(stdout
);
1986 cfg
= bt_config_run_create(initial_plugin_paths
);
1991 cfg
->cmd_data
.run
.retry_duration_us
= 100000;
1992 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1993 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1994 cur_base_params
= bt_value_map_create();
1995 if (!cur_base_params
) {
1996 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2000 instance_names
= bt_value_map_create();
2001 if (!instance_names
) {
2002 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2006 connection_args
= bt_value_array_create();
2007 if (!connection_args
) {
2008 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2012 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2018 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
2019 run_long_options
, 0);
2021 BT_CLI_LOGE_APPEND_CAUSE("Cannot get popt context.");
2025 poptReadDefaultConfig(pc
, 0);
2027 while ((opt
= poptGetNextOpt(pc
)) > 0) {
2028 arg
= poptGetOptArg(pc
);
2031 case OPT_PLUGIN_PATH
:
2032 if (bt_config_append_plugin_paths_check_setuid_setgid(
2033 cfg
->plugin_paths
, arg
)) {
2037 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2038 cfg
->omit_system_plugin_path
= true;
2040 case OPT_OMIT_HOME_PLUGIN_PATH
:
2041 cfg
->omit_home_plugin_path
= true;
2045 enum bt_config_component_dest new_dest
;
2048 ret
= add_run_cfg_comp_check_name(cfg
,
2049 cur_cfg_comp
, cur_cfg_comp_dest
,
2051 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2057 cur_cfg_comp
= bt_config_component_from_arg(arg
,
2059 if (!cur_cfg_comp
) {
2060 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --component option's argument:\n %s",
2065 switch (cur_cfg_comp
->type
) {
2066 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2067 new_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
2069 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2070 new_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
2072 case BT_COMPONENT_CLASS_TYPE_SINK
:
2073 new_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
2079 BT_ASSERT(cur_base_params
);
2080 bt_value_put_ref(cur_cfg_comp
->params
);
2081 if (bt_value_copy(cur_base_params
,
2082 &cur_cfg_comp
->params
) < 0) {
2083 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2087 cur_cfg_comp_dest
= new_dest
;
2093 bt_value
*params_to_set
;
2095 if (!cur_cfg_comp
) {
2096 BT_CLI_LOGE_APPEND_CAUSE("Cannot add parameters to unavailable component:\n %s",
2101 params
= cli_value_from_arg(arg
, error_str
);
2103 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
2108 extend_status
= bt_value_map_extend(
2109 cur_cfg_comp
->params
, params
, ¶ms_to_set
);
2110 BT_VALUE_PUT_REF_AND_RESET(params
);
2111 if (extend_status
!= BT_VALUE_MAP_EXTEND_STATUS_OK
) {
2112 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current component parameters with --params option's argument:\n %s",
2117 BT_OBJECT_MOVE_REF(cur_cfg_comp
->params
, params_to_set
);
2121 if (!cur_cfg_comp
) {
2122 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the name of unavailable component:\n %s",
2127 g_string_assign(cur_cfg_comp
->instance_name
, arg
);
2130 if (!cur_cfg_comp
) {
2131 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the log level of unavailable component:\n %s",
2136 cur_cfg_comp
->log_level
=
2137 bt_log_get_level_from_string(arg
);
2138 if (cur_cfg_comp
->log_level
< 0) {
2139 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
2144 case OPT_BASE_PARAMS
:
2146 bt_value
*params
= cli_value_from_arg(arg
, error_str
);
2149 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --base-params option's argument:\n %s",
2154 BT_OBJECT_MOVE_REF(cur_base_params
, params
);
2157 case OPT_RESET_BASE_PARAMS
:
2158 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2159 cur_base_params
= bt_value_map_create();
2160 if (!cur_base_params
) {
2161 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2166 if (bt_value_array_append_string_element(
2167 connection_args
, arg
)) {
2168 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2172 case OPT_RETRY_DURATION
:
2173 if (retry_duration
< 0) {
2174 BT_CLI_LOGE_APPEND_CAUSE("--retry-duration option's argument must be positive or 0: %ld",
2179 cfg
->cmd_data
.run
.retry_duration_us
=
2180 (uint64_t) retry_duration
;
2183 print_run_usage(stdout
);
2185 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2188 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
2197 /* Check for option parsing error */
2199 BT_CLI_LOGE_APPEND_CAUSE("While parsing command-line options, at option %s: %s",
2200 poptBadOption(pc
, 0), poptStrerror(opt
));
2204 /* This command does not accept leftover arguments */
2205 if (poptPeekArg(pc
)) {
2206 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: %s", poptPeekArg(pc
));
2210 /* Add current component */
2212 ret
= add_run_cfg_comp_check_name(cfg
, cur_cfg_comp
,
2213 cur_cfg_comp_dest
, instance_names
);
2214 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2220 if (cfg
->cmd_data
.run
.sources
->len
== 0) {
2221 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no source component.");
2225 if (cfg
->cmd_data
.run
.sinks
->len
== 0) {
2226 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no sink component.");
2230 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2234 ret
= bt_config_cli_args_create_connections(cfg
,
2238 BT_CLI_LOGE_APPEND_CAUSE("Cannot creation connections:\n%s", error_buf
);
2246 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2250 poptFreeContext(pc
);
2254 g_string_free(error_str
, TRUE
);
2258 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2259 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2260 BT_VALUE_PUT_REF_AND_RESET(instance_names
);
2261 BT_VALUE_PUT_REF_AND_RESET(connection_args
);
2266 struct bt_config
*bt_config_run_from_args_array(const bt_value
*run_args
,
2267 int *retcode
, bool force_omit_system_plugin_path
,
2268 bool force_omit_home_plugin_path
,
2269 const bt_value
*initial_plugin_paths
, int default_log_level
)
2271 struct bt_config
*cfg
= NULL
;
2274 const size_t argc
= bt_value_array_get_size(run_args
) + 1;
2276 argv
= calloc(argc
, sizeof(*argv
));
2278 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2284 len
= bt_value_array_get_size(run_args
);
2286 BT_CLI_LOGE_APPEND_CAUSE("Invalid executable arguments.");
2289 for (i
= 0; i
< len
; i
++) {
2290 const bt_value
*arg_value
=
2291 bt_value_array_borrow_element_by_index_const(run_args
,
2295 BT_ASSERT(arg_value
);
2296 arg
= bt_value_string_get(arg_value
);
2301 cfg
= bt_config_run_from_args(argc
, argv
, retcode
,
2302 force_omit_system_plugin_path
, force_omit_home_plugin_path
,
2303 initial_plugin_paths
, default_log_level
);
2311 * Prints the convert command usage.
2314 void print_convert_usage(FILE *fp
)
2316 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2318 fprintf(fp
, "Options:\n");
2320 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2321 fprintf(fp
, " Instantiate the component class CLS of type\n");
2322 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2323 fprintf(fp
, " in the plugin PLUGIN, add it to the\n");
2324 fprintf(fp
, " conversion graph, and optionally name it\n");
2325 fprintf(fp
, " NAME (you can also specify the name with\n");
2326 fprintf(fp
, " --name)\n");
2327 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
2328 fprintf(fp
, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
2329 fprintf(fp
, " --name=NAME Set the name of the current component\n");
2330 fprintf(fp
, " to NAME (must be unique amongst all the\n");
2331 fprintf(fp
, " names of the created components)\n");
2332 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2333 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
2334 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2335 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2336 fprintf(fp
, " current component (see the expected format\n");
2337 fprintf(fp
, " of PARAMS below)\n");
2338 fprintf(fp
, " -P, --path=PATH Set the `path` string parameter of the\n");
2339 fprintf(fp
, " current component to PATH\n");
2340 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2341 fprintf(fp
, " dynamic plugins can be loaded\n");
2342 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
2343 fprintf(fp
, " the graph later, retry in DUR µs\n");
2344 fprintf(fp
, " (default: 100000)\n");
2345 fprintf(fp
, " dynamic plugins can be loaded\n");
2346 fprintf(fp
, " --run-args Print the equivalent arguments for the\n");
2347 fprintf(fp
, " `run` command to the standard output,\n");
2348 fprintf(fp
, " formatted for a shell, and quit\n");
2349 fprintf(fp
, " --run-args-0 Print the equivalent arguments for the\n");
2350 fprintf(fp
, " `run` command to the standard output,\n");
2351 fprintf(fp
, " formatted for `xargs -0`, and quit\n");
2352 fprintf(fp
, " --stream-intersection Only process events when all streams\n");
2353 fprintf(fp
, " are active\n");
2354 fprintf(fp
, " -u, --url=URL Set the `url` string parameter of the\n");
2355 fprintf(fp
, " current component to URL\n");
2356 fprintf(fp
, " -h, --help Show this help and quit\n");
2358 fprintf(fp
, "Implicit `source.ctf.fs` component options:\n");
2360 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2361 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2363 fprintf(fp
, "Implicit `sink.text.pretty` component options:\n");
2365 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
2366 fprintf(fp
, " --clock-date Print timestamp dates\n");
2367 fprintf(fp
, " --clock-gmt Print and parse timestamps in the GMT\n");
2368 fprintf(fp
, " time zone instead of the local time zone\n");
2369 fprintf(fp
, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2370 fprintf(fp
, " of `hh:mm:ss.nnnnnnnnn`\n");
2371 fprintf(fp
, " --color=(never | auto | always)\n");
2372 fprintf(fp
, " Never, automatically, or always emit\n");
2373 fprintf(fp
, " console color codes\n");
2374 fprintf(fp
, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2375 fprintf(fp
, " `all`, `trace`, `trace:hostname`,\n");
2376 fprintf(fp
, " `trace:domain`, `trace:procname`,\n");
2377 fprintf(fp
, " `trace:vpid`, `loglevel`, `emf`\n");
2378 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2379 fprintf(fp
, " `payload` (or `arg` or `args`), `none`,\n");
2380 fprintf(fp
, " `all`, `scope`, `header`, `context`\n");
2381 fprintf(fp
, " (or `ctx`)\n");
2382 fprintf(fp
, " --no-delta Do not print time delta between\n");
2383 fprintf(fp
, " consecutive events\n");
2384 fprintf(fp
, " -w, --output=PATH Write output text to PATH instead of\n");
2385 fprintf(fp
, " the standard output\n");
2387 fprintf(fp
, "Implicit `filter.utils.muxer` component options:\n");
2389 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently\n");
2390 fprintf(fp
, " correlated across traces\n");
2392 fprintf(fp
, "Implicit `filter.utils.trimmer` component options:\n");
2394 fprintf(fp
, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2395 fprintf(fp
, " time range to BEGIN (see the format of\n");
2396 fprintf(fp
, " BEGIN below)\n");
2397 fprintf(fp
, " -e, --end=END Set the end time of the conversion time\n");
2398 fprintf(fp
, " range to END (see the format of END below)\n");
2399 fprintf(fp
, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2400 fprintf(fp
, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2401 fprintf(fp
, " `]`) (see the format of BEGIN/END below)\n");
2403 fprintf(fp
, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2405 fprintf(fp
, " --debug-info Create an implicit\n");
2406 fprintf(fp
, " `filter.lttng-utils.debug-info` component\n");
2407 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2408 fprintf(fp
, " instead of `/usr/lib/debug`\n");
2409 fprintf(fp
, " --debug-info-full-path Show full debug info source and\n");
2410 fprintf(fp
, " binary paths instead of just names\n");
2411 fprintf(fp
, " --debug-info-target-prefix=DIR\n");
2412 fprintf(fp
, " Use directory DIR as a prefix when\n");
2413 fprintf(fp
, " looking up executables during debug\n");
2414 fprintf(fp
, " info analysis\n");
2416 fprintf(fp
, "Legacy options that still work:\n");
2418 fprintf(fp
, " -i, --input-format=(ctf | lttng-live)\n");
2419 fprintf(fp
, " `ctf`:\n");
2420 fprintf(fp
, " Create an implicit `source.ctf.fs`\n");
2421 fprintf(fp
, " component\n");
2422 fprintf(fp
, " `lttng-live`:\n");
2423 fprintf(fp
, " Create an implicit `source.ctf.lttng-live`\n");
2424 fprintf(fp
, " component\n");
2425 fprintf(fp
, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2426 fprintf(fp
, " `text`:\n");
2427 fprintf(fp
, " Create an implicit `sink.text.pretty`\n");
2428 fprintf(fp
, " component\n");
2429 fprintf(fp
, " `ctf`:\n");
2430 fprintf(fp
, " Create an implicit `sink.ctf.fs`\n");
2431 fprintf(fp
, " component\n");
2432 fprintf(fp
, " `dummy`:\n");
2433 fprintf(fp
, " Create an implicit `sink.utils.dummy`\n");
2434 fprintf(fp
, " component\n");
2435 fprintf(fp
, " `ctf-metadata`:\n");
2436 fprintf(fp
, " Query the `source.ctf.fs` component class\n");
2437 fprintf(fp
, " for metadata text and quit\n");
2439 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
2440 fprintf(fp
, "\n\n");
2441 fprintf(fp
, "Format of BEGIN and END\n");
2442 fprintf(fp
, "-----------------------\n");
2444 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2445 fprintf(fp
, "\n\n");
2446 print_expected_params_format(fp
);
2450 struct poptOption convert_long_options
[] = {
2451 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
2452 { "begin", 'b', POPT_ARG_STRING
, NULL
, OPT_BEGIN
, NULL
, NULL
},
2453 { "clock-cycles", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_CYCLES
, NULL
, NULL
},
2454 { "clock-date", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_DATE
, NULL
, NULL
},
2455 { "clock-force-correlate", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_FORCE_CORRELATE
, NULL
, NULL
},
2456 { "clock-gmt", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_GMT
, NULL
, NULL
},
2457 { "clock-offset", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET
, NULL
, NULL
},
2458 { "clock-offset-ns", '\0', POPT_ARG_STRING
, NULL
, OPT_CLOCK_OFFSET_NS
, NULL
, NULL
},
2459 { "clock-seconds", '\0', POPT_ARG_NONE
, NULL
, OPT_CLOCK_SECONDS
, NULL
, NULL
},
2460 { "color", '\0', POPT_ARG_STRING
, NULL
, OPT_COLOR
, NULL
, NULL
},
2461 { "component", 'c', POPT_ARG_STRING
, NULL
, OPT_COMPONENT
, NULL
, NULL
},
2462 { "debug", 'd', POPT_ARG_NONE
, NULL
, OPT_DEBUG
, NULL
, NULL
},
2463 { "debug-info-dir", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_DIR
, NULL
, NULL
},
2464 { "debug-info-full-path", 0, POPT_ARG_NONE
, NULL
, OPT_DEBUG_INFO_FULL_PATH
, NULL
, NULL
},
2465 { "debug-info-target-prefix", 0, POPT_ARG_STRING
, NULL
, OPT_DEBUG_INFO_TARGET_PREFIX
, NULL
, NULL
},
2466 { "end", 'e', POPT_ARG_STRING
, NULL
, OPT_END
, NULL
, NULL
},
2467 { "fields", 'f', POPT_ARG_STRING
, NULL
, OPT_FIELDS
, NULL
, NULL
},
2468 { "help", 'h', POPT_ARG_NONE
, NULL
, OPT_HELP
, NULL
, NULL
},
2469 { "input-format", 'i', POPT_ARG_STRING
, NULL
, OPT_INPUT_FORMAT
, NULL
, NULL
},
2470 { "log-level", 'l', POPT_ARG_STRING
, NULL
, OPT_LOG_LEVEL
, NULL
, NULL
},
2471 { "name", '\0', POPT_ARG_STRING
, NULL
, OPT_NAME
, NULL
, NULL
},
2472 { "names", 'n', POPT_ARG_STRING
, NULL
, OPT_NAMES
, NULL
, NULL
},
2473 { "debug-info", '\0', POPT_ARG_NONE
, NULL
, OPT_DEBUG_INFO
, NULL
, NULL
},
2474 { "no-delta", '\0', POPT_ARG_NONE
, NULL
, OPT_NO_DELTA
, NULL
, NULL
},
2475 { "omit-home-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_HOME_PLUGIN_PATH
, NULL
, NULL
},
2476 { "omit-system-plugin-path", '\0', POPT_ARG_NONE
, NULL
, OPT_OMIT_SYSTEM_PLUGIN_PATH
, NULL
, NULL
},
2477 { "output", 'w', POPT_ARG_STRING
, NULL
, OPT_OUTPUT
, NULL
, NULL
},
2478 { "output-format", 'o', POPT_ARG_STRING
, NULL
, OPT_OUTPUT_FORMAT
, NULL
, NULL
},
2479 { "params", 'p', POPT_ARG_STRING
, NULL
, OPT_PARAMS
, NULL
, NULL
},
2480 { "path", 'P', POPT_ARG_STRING
, NULL
, OPT_PATH
, NULL
, NULL
},
2481 { "plugin-path", '\0', POPT_ARG_STRING
, NULL
, OPT_PLUGIN_PATH
, NULL
, NULL
},
2482 { "retry-duration", '\0', POPT_ARG_STRING
, NULL
, OPT_RETRY_DURATION
, NULL
, NULL
},
2483 { "run-args", '\0', POPT_ARG_NONE
, NULL
, OPT_RUN_ARGS
, NULL
, NULL
},
2484 { "run-args-0", '\0', POPT_ARG_NONE
, NULL
, OPT_RUN_ARGS_0
, NULL
, NULL
},
2485 { "stream-intersection", '\0', POPT_ARG_NONE
, NULL
, OPT_STREAM_INTERSECTION
, NULL
, NULL
},
2486 { "timerange", '\0', POPT_ARG_STRING
, NULL
, OPT_TIMERANGE
, NULL
, NULL
},
2487 { "url", 'u', POPT_ARG_STRING
, NULL
, OPT_URL
, NULL
, NULL
},
2488 { "verbose", 'v', POPT_ARG_NONE
, NULL
, OPT_VERBOSE
, NULL
, NULL
},
2489 { NULL
, 0, '\0', NULL
, 0, NULL
, NULL
},
2493 GString
*get_component_auto_name(const char *prefix
,
2494 const bt_value
*existing_names
)
2497 GString
*auto_name
= g_string_new(NULL
);
2500 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2504 if (!bt_value_map_has_entry(existing_names
, prefix
)) {
2505 g_string_assign(auto_name
, prefix
);
2510 g_string_printf(auto_name
, "%s-%d", prefix
, i
);
2512 } while (bt_value_map_has_entry(existing_names
, auto_name
->str
));
2518 struct implicit_component_args
{
2522 GString
*params_arg
;
2523 bt_value
*extra_params
;
2527 int assign_name_to_implicit_component(struct implicit_component_args
*args
,
2528 const char *prefix
, bt_value
*existing_names
,
2529 GList
**comp_names
, bool append_to_comp_names
)
2532 GString
*name
= NULL
;
2534 if (!args
->exists
) {
2538 name
= get_component_auto_name(prefix
,
2546 g_string_assign(args
->name_arg
, name
->str
);
2548 if (bt_value_map_insert_entry(existing_names
, name
->str
,
2550 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2555 if (append_to_comp_names
) {
2556 *comp_names
= g_list_append(*comp_names
, name
);
2562 g_string_free(name
, TRUE
);
2569 int append_run_args_for_implicit_component(
2570 struct implicit_component_args
*impl_args
,
2576 if (!impl_args
->exists
) {
2580 if (bt_value_array_append_string_element(run_args
, "--component")) {
2581 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2585 if (bt_value_array_append_string_element(run_args
, impl_args
->comp_arg
->str
)) {
2586 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2590 if (bt_value_array_append_string_element(run_args
, "--name")) {
2591 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2595 if (bt_value_array_append_string_element(run_args
, impl_args
->name_arg
->str
)) {
2596 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2600 if (impl_args
->params_arg
->len
> 0) {
2601 if (bt_value_array_append_string_element(run_args
, "--params")) {
2602 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2606 if (bt_value_array_append_string_element(run_args
,
2607 impl_args
->params_arg
->str
)) {
2608 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2613 for (i
= 0; i
< bt_value_array_get_size(impl_args
->extra_params
);
2615 const bt_value
*elem
;
2618 elem
= bt_value_array_borrow_element_by_index(impl_args
->extra_params
,
2624 BT_ASSERT(bt_value_is_string(elem
));
2625 arg
= bt_value_string_get(elem
);
2626 ret
= bt_value_array_append_string_element(run_args
, arg
);
2628 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2643 void finalize_implicit_component_args(struct implicit_component_args
*args
)
2647 if (args
->comp_arg
) {
2648 g_string_free(args
->comp_arg
, TRUE
);
2651 if (args
->name_arg
) {
2652 g_string_free(args
->name_arg
, TRUE
);
2655 if (args
->params_arg
) {
2656 g_string_free(args
->params_arg
, TRUE
);
2659 bt_value_put_ref(args
->extra_params
);
2663 int init_implicit_component_args(struct implicit_component_args
*args
,
2664 const char *comp_arg
, bool exists
)
2668 args
->exists
= exists
;
2669 args
->comp_arg
= g_string_new(comp_arg
);
2670 args
->name_arg
= g_string_new(NULL
);
2671 args
->params_arg
= g_string_new(NULL
);
2672 args
->extra_params
= bt_value_array_create();
2674 if (!args
->comp_arg
|| !args
->name_arg
||
2675 !args
->params_arg
|| !args
->extra_params
) {
2677 finalize_implicit_component_args(args
);
2678 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2687 void append_implicit_component_param(struct implicit_component_args
*args
,
2688 const char *key
, const char *value
)
2693 append_param_arg(args
->params_arg
, key
, value
);
2696 /* Escape value to make it suitable to use as a string parameter value. */
2698 gchar
*escape_string_value(const char *value
)
2703 ret
= g_string_new(NULL
);
2705 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2714 g_string_append_c(ret
, '\\');
2718 g_string_append_c(ret
, *in
);
2724 return g_string_free(ret
, FALSE
);
2728 int bt_value_to_cli_param_value_append(const bt_value
*value
, GString
*buf
)
2734 switch (bt_value_get_type(value
)) {
2735 case BT_VALUE_TYPE_STRING
:
2737 const char *str_value
= bt_value_string_get(value
);
2738 gchar
*escaped_str_value
;
2740 escaped_str_value
= escape_string_value(str_value
);
2741 if (!escaped_str_value
) {
2745 g_string_append_printf(buf
, "\"%s\"", escaped_str_value
);
2747 g_free(escaped_str_value
);
2750 case BT_VALUE_TYPE_ARRAY
: {
2751 g_string_append_c(buf
, '[');
2752 uint64_t sz
= bt_value_array_get_size(value
);
2753 for (uint64_t i
= 0; i
< sz
; i
++) {
2754 const bt_value
*item
;
2758 g_string_append(buf
, ", ");
2761 item
= bt_value_array_borrow_element_by_index_const(
2763 ret
= bt_value_to_cli_param_value_append(item
, buf
);
2769 g_string_append_c(buf
, ']');
2783 * Convert `value` to its equivalent representation as a command line parameter
2788 gchar
*bt_value_to_cli_param_value(bt_value
*value
)
2791 gchar
*result
= NULL
;
2793 buf
= g_string_new(NULL
);
2795 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2799 if (bt_value_to_cli_param_value_append(value
, buf
)) {
2803 result
= g_string_free(buf
, FALSE
);
2810 g_string_free(buf
, TRUE
);
2818 int append_parameter_to_args(bt_value
*args
, const char *key
, bt_value
*value
)
2821 BT_ASSERT(bt_value_get_type(args
) == BT_VALUE_TYPE_ARRAY
);
2826 gchar
*str_value
= NULL
;
2827 GString
*parameter
= NULL
;
2829 if (bt_value_array_append_string_element(args
, "--params")) {
2830 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2835 str_value
= bt_value_to_cli_param_value(value
);
2841 parameter
= g_string_new(NULL
);
2843 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2848 g_string_printf(parameter
, "%s=%s", key
, str_value
);
2850 if (bt_value_array_append_string_element(args
, parameter
->str
)) {
2851 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2858 g_string_free(parameter
, TRUE
);
2871 int append_string_parameter_to_args(bt_value
*args
, const char *key
, const char *value
)
2873 bt_value
*str_value
;
2876 str_value
= bt_value_string_create_init(value
);
2879 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2884 ret
= append_parameter_to_args(args
, key
, str_value
);
2887 BT_VALUE_PUT_REF_AND_RESET(str_value
);
2892 int append_implicit_component_extra_param(struct implicit_component_args
*args
,
2893 const char *key
, const char *value
)
2895 return append_string_parameter_to_args(args
->extra_params
, key
, value
);
2899 int convert_append_name_param(enum bt_config_component_dest dest
,
2900 GString
*cur_name
, GString
*cur_name_prefix
,
2902 bt_value
*all_names
,
2903 GList
**source_names
, GList
**filter_names
,
2908 if (cur_name_prefix
->len
> 0) {
2909 /* We're after a --component option */
2910 GString
*name
= NULL
;
2911 bool append_name_opt
= false;
2913 if (cur_name
->len
== 0) {
2915 * No explicit name was provided for the user
2918 name
= get_component_auto_name(cur_name_prefix
->str
,
2920 append_name_opt
= true;
2923 * An explicit name was provided for the user
2926 if (bt_value_map_has_entry(all_names
,
2928 BT_CLI_LOGE_APPEND_CAUSE("Duplicate component instance name:\n %s",
2933 name
= g_string_new(cur_name
->str
);
2937 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2942 * Remember this name globally, for the uniqueness of
2943 * all component names.
2945 if (bt_value_map_insert_entry(all_names
, name
->str
, bt_value_null
)) {
2946 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2951 * Append the --name option if necessary.
2953 if (append_name_opt
) {
2954 if (bt_value_array_append_string_element(run_args
, "--name")) {
2955 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2959 if (bt_value_array_append_string_element(run_args
, name
->str
)) {
2960 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2966 * Remember this name specifically for the type of the
2967 * component. This is to create connection arguments.
2970 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
2971 *source_names
= g_list_append(*source_names
, name
);
2973 case BT_CONFIG_COMPONENT_DEST_FILTER
:
2974 *filter_names
= g_list_append(*filter_names
, name
);
2976 case BT_CONFIG_COMPONENT_DEST_SINK
:
2977 *sink_names
= g_list_append(*sink_names
, name
);
2983 g_string_assign(cur_name_prefix
, "");
2996 * Escapes `.`, `:`, and `\` of `input` with `\`.
2999 GString
*escape_dot_colon(const char *input
)
3001 GString
*output
= g_string_new(NULL
);
3005 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3009 for (ch
= input
; *ch
!= '\0'; ch
++) {
3010 if (*ch
== '\\' || *ch
== '.' || *ch
== ':') {
3011 g_string_append_c(output
, '\\');
3014 g_string_append_c(output
, *ch
);
3022 * Appends a --connect option to a list of arguments. `upstream_name`
3023 * and `downstream_name` are escaped with escape_dot_colon() in this
3027 int append_connect_arg(bt_value
*run_args
,
3028 const char *upstream_name
, const char *downstream_name
)
3031 GString
*e_upstream_name
= escape_dot_colon(upstream_name
);
3032 GString
*e_downstream_name
= escape_dot_colon(downstream_name
);
3033 GString
*arg
= g_string_new(NULL
);
3035 if (!e_upstream_name
|| !e_downstream_name
|| !arg
) {
3036 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3041 ret
= bt_value_array_append_string_element(run_args
, "--connect");
3043 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3048 g_string_append(arg
, e_upstream_name
->str
);
3049 g_string_append_c(arg
, ':');
3050 g_string_append(arg
, e_downstream_name
->str
);
3051 ret
= bt_value_array_append_string_element(run_args
, arg
->str
);
3053 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3060 g_string_free(arg
, TRUE
);
3063 if (e_upstream_name
) {
3064 g_string_free(e_upstream_name
, TRUE
);
3067 if (e_downstream_name
) {
3068 g_string_free(e_downstream_name
, TRUE
);
3075 * Appends the run command's --connect options for the convert command.
3078 int convert_auto_connect(bt_value
*run_args
,
3079 GList
*source_names
, GList
*filter_names
,
3083 GList
*source_at
= source_names
;
3084 GList
*filter_at
= filter_names
;
3086 GList
*sink_at
= sink_names
;
3088 BT_ASSERT(source_names
);
3089 BT_ASSERT(filter_names
);
3090 BT_ASSERT(sink_names
);
3092 /* Connect all sources to the first filter */
3093 for (source_at
= source_names
; source_at
!= NULL
; source_at
= g_list_next(source_at
)) {
3094 GString
*source_name
= source_at
->data
;
3095 GString
*filter_name
= filter_at
->data
;
3097 ret
= append_connect_arg(run_args
, source_name
->str
,
3104 filter_prev
= filter_at
;
3105 filter_at
= g_list_next(filter_at
);
3107 /* Connect remaining filters */
3108 for (; filter_at
!= NULL
; filter_prev
= filter_at
, filter_at
= g_list_next(filter_at
)) {
3109 GString
*filter_name
= filter_at
->data
;
3110 GString
*filter_prev_name
= filter_prev
->data
;
3112 ret
= append_connect_arg(run_args
, filter_prev_name
->str
,
3119 /* Connect last filter to all sinks */
3120 for (sink_at
= sink_names
; sink_at
!= NULL
; sink_at
= g_list_next(sink_at
)) {
3121 GString
*filter_name
= filter_prev
->data
;
3122 GString
*sink_name
= sink_at
->data
;
3124 ret
= append_connect_arg(run_args
, filter_name
->str
,
3141 int split_timerange(const char *arg
, char **begin
, char **end
)
3144 const char *ch
= arg
;
3146 GString
*g_begin
= NULL
;
3147 GString
*g_end
= NULL
;
3155 g_begin
= bt_common_string_until(ch
, "", ",", &end_pos
);
3156 if (!g_begin
|| ch
[end_pos
] != ',' || g_begin
->len
== 0) {
3162 g_end
= bt_common_string_until(ch
, "", "]", &end_pos
);
3163 if (!g_end
|| g_end
->len
== 0) {
3169 *begin
= g_begin
->str
;
3171 g_string_free(g_begin
, FALSE
);
3172 g_string_free(g_end
, FALSE
);
3182 g_string_free(g_begin
, TRUE
);
3186 g_string_free(g_end
, TRUE
);
3193 int g_list_prepend_gstring(GList
**list
, const char *string
)
3196 GString
*gs
= g_string_new(string
);
3201 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3205 *list
= g_list_prepend(*list
, gs
);
3212 * Creates a Babeltrace config object from the arguments of a convert
3215 * *retcode is set to the appropriate exit code to use.
3218 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3219 int *retcode
, bool force_omit_system_plugin_path
,
3220 bool force_omit_home_plugin_path
,
3221 const bt_value
*initial_plugin_paths
, int *default_log_level
)
3223 poptContext pc
= NULL
;
3225 enum bt_config_component_dest cur_comp_dest
=
3226 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
3228 struct bt_config
*cfg
= NULL
;
3229 bool got_input_format_opt
= false;
3230 bool got_output_format_opt
= false;
3231 bool trimmer_has_begin
= false;
3232 bool trimmer_has_end
= false;
3233 bool stream_intersection_mode
= false;
3234 GString
*cur_name
= NULL
;
3235 GString
*cur_name_prefix
= NULL
;
3236 const char *leftover
= NULL
;
3237 bool print_run_args
= false;
3238 bool print_run_args_0
= false;
3239 bool print_ctf_metadata
= false;
3240 bt_value
*run_args
= NULL
;
3241 bt_value
*all_names
= NULL
;
3242 GList
*source_names
= NULL
;
3243 GList
*filter_names
= NULL
;
3244 GList
*sink_names
= NULL
;
3245 bt_value
*leftovers
= NULL
;
3246 struct implicit_component_args implicit_ctf_input_args
= { 0 };
3247 struct implicit_component_args implicit_ctf_output_args
= { 0 };
3248 struct implicit_component_args implicit_lttng_live_args
= { 0 };
3249 struct implicit_component_args implicit_dummy_args
= { 0 };
3250 struct implicit_component_args implicit_text_args
= { 0 };
3251 struct implicit_component_args implicit_debug_info_args
= { 0 };
3252 struct implicit_component_args implicit_muxer_args
= { 0 };
3253 struct implicit_component_args implicit_trimmer_args
= { 0 };
3254 bt_value
*plugin_paths
;
3255 char error_buf
[256] = { 0 };
3257 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
3258 char *output
= NULL
;
3260 (void) bt_value_copy(initial_plugin_paths
, &plugin_paths
);
3265 print_convert_usage(stdout
);
3270 if (init_implicit_component_args(&implicit_ctf_input_args
,
3271 "source.ctf.fs", false)) {
3275 if (init_implicit_component_args(&implicit_ctf_output_args
,
3276 "sink.ctf.fs", false)) {
3280 if (init_implicit_component_args(&implicit_lttng_live_args
,
3281 "source.ctf.lttng-live", false)) {
3285 if (init_implicit_component_args(&implicit_text_args
,
3286 "sink.text.pretty", false)) {
3290 if (init_implicit_component_args(&implicit_dummy_args
,
3291 "sink.utils.dummy", false)) {
3295 if (init_implicit_component_args(&implicit_debug_info_args
,
3296 "filter.lttng-utils.debug-info", false)) {
3300 if (init_implicit_component_args(&implicit_muxer_args
,
3301 "filter.utils.muxer", true)) {
3305 if (init_implicit_component_args(&implicit_trimmer_args
,
3306 "filter.utils.trimmer", false)) {
3310 all_names
= bt_value_map_create();
3312 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3316 run_args
= bt_value_array_create();
3318 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3322 cur_name
= g_string_new(NULL
);
3324 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3328 cur_name_prefix
= g_string_new(NULL
);
3329 if (!cur_name_prefix
) {
3330 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3334 ret
= append_env_var_plugin_paths(plugin_paths
);
3339 leftovers
= bt_value_array_create();
3341 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3346 * First pass: collect all arguments which need to be passed
3347 * as is to the run command. This pass can also add --name
3348 * arguments if needed to automatically name unnamed component
3349 * instances. Also it does the following transformations:
3351 * --path=PATH -> --params=path="PATH"
3352 * --url=URL -> --params=url="URL"
3354 * Also it appends the plugin paths of --plugin-path to
3357 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
3358 convert_long_options
, 0);
3360 BT_CLI_LOGE_APPEND_CAUSE("Cannot get popt context.");
3364 poptReadDefaultConfig(pc
, 0);
3366 while ((opt
= poptGetNextOpt(pc
)) > 0) {
3368 char *plugin_name
= NULL
;
3369 char *comp_cls_name
= NULL
;
3371 arg
= poptGetOptArg(pc
);
3376 bt_component_class_type type
;
3377 const char *type_prefix
;
3379 /* Append current component's name if needed */
3380 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
3381 cur_name_prefix
, run_args
, all_names
,
3382 &source_names
, &filter_names
, &sink_names
);
3387 /* Parse the argument */
3388 plugin_comp_cls_names(arg
, &name
, &plugin_name
,
3389 &comp_cls_name
, &type
);
3390 if (!plugin_name
|| !comp_cls_name
) {
3391 BT_CLI_LOGE_APPEND_CAUSE(
3392 "Invalid format for --component option's argument:\n %s",
3398 g_string_assign(cur_name
, name
);
3400 g_string_assign(cur_name
, "");
3404 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
3405 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
3406 type_prefix
= "source";
3408 case BT_COMPONENT_CLASS_TYPE_FILTER
:
3409 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
3410 type_prefix
= "filter";
3412 case BT_COMPONENT_CLASS_TYPE_SINK
:
3413 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
3414 type_prefix
= "sink";
3420 if (bt_value_array_append_string_element(run_args
,
3422 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3426 if (bt_value_array_append_string_element(run_args
, arg
)) {
3427 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3431 g_string_assign(cur_name_prefix
, "");
3432 g_string_append_printf(cur_name_prefix
, "%s.%s.%s",
3433 type_prefix
, plugin_name
, comp_cls_name
);
3436 free(comp_cls_name
);
3439 comp_cls_name
= NULL
;
3443 if (cur_name_prefix
->len
== 0) {
3444 BT_CLI_LOGE_APPEND_CAUSE("No current component of which to set parameters:\n %s",
3449 if (bt_value_array_append_string_element(run_args
,
3451 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3455 if (bt_value_array_append_string_element(run_args
, arg
)) {
3456 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3461 if (cur_name_prefix
->len
== 0) {
3462 BT_CLI_LOGE_APPEND_CAUSE("No current component of which to set `path` parameter:\n %s",
3467 if (append_string_parameter_to_args(run_args
, "path", arg
)) {
3472 if (cur_name_prefix
->len
== 0) {
3473 BT_CLI_LOGE_APPEND_CAUSE("No current component of which to set `url` parameter:\n %s",
3479 if (append_string_parameter_to_args(run_args
, "url", arg
)) {
3484 if (cur_name_prefix
->len
== 0) {
3485 BT_CLI_LOGE_APPEND_CAUSE("No current component to name:\n %s",
3490 if (bt_value_array_append_string_element(run_args
, "--name")) {
3491 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3495 if (bt_value_array_append_string_element(run_args
, arg
)) {
3496 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3500 g_string_assign(cur_name
, arg
);
3503 if (cur_name_prefix
->len
== 0) {
3504 BT_CLI_LOGE_APPEND_CAUSE("No current component to assign a log level to:\n %s",
3509 if (bt_value_array_append_string_element(run_args
, "--log-level")) {
3510 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3514 if (bt_value_array_append_string_element(run_args
, arg
)) {
3515 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3520 case OPT_OMIT_HOME_PLUGIN_PATH
:
3521 force_omit_home_plugin_path
= true;
3523 if (bt_value_array_append_string_element(run_args
,
3524 "--omit-home-plugin-path")) {
3525 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3529 case OPT_RETRY_DURATION
:
3530 if (bt_value_array_append_string_element(run_args
,
3531 "--retry-duration")) {
3532 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3536 if (bt_value_array_append_string_element(run_args
, arg
)) {
3537 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3541 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
3542 force_omit_system_plugin_path
= true;
3544 if (bt_value_array_append_string_element(run_args
,
3545 "--omit-system-plugin-path")) {
3546 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3550 case OPT_PLUGIN_PATH
:
3551 if (bt_config_append_plugin_paths_check_setuid_setgid(
3552 plugin_paths
, arg
)) {
3556 if (bt_value_array_append_string_element(run_args
,
3558 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3562 if (bt_value_array_append_string_element(run_args
, arg
)) {
3563 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3568 print_convert_usage(stdout
);
3570 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
3573 case OPT_CLOCK_CYCLES
:
3574 case OPT_CLOCK_DATE
:
3575 case OPT_CLOCK_FORCE_CORRELATE
:
3577 case OPT_CLOCK_OFFSET
:
3578 case OPT_CLOCK_OFFSET_NS
:
3579 case OPT_CLOCK_SECONDS
:
3582 case OPT_DEBUG_INFO
:
3583 case OPT_DEBUG_INFO_DIR
:
3584 case OPT_DEBUG_INFO_FULL_PATH
:
3585 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3588 case OPT_INPUT_FORMAT
:
3591 case OPT_OUTPUT_FORMAT
:
3594 case OPT_RUN_ARGS_0
:
3595 case OPT_STREAM_INTERSECTION
:
3598 /* Ignore in this pass */
3601 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
3610 /* Append current component's name if needed */
3611 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
3612 cur_name_prefix
, run_args
, all_names
, &source_names
,
3613 &filter_names
, &sink_names
);
3618 /* Check for option parsing error */
3620 BT_CLI_LOGE_APPEND_CAUSE("While parsing command-line options, at option `%s`: %s.",
3621 poptBadOption(pc
, 0), poptStrerror(opt
));
3625 poptFreeContext(pc
);
3630 * Second pass: transform the convert-specific options and
3631 * arguments into implicit component instances for the run
3634 pc
= poptGetContext(NULL
, argc
, (const char **) argv
,
3635 convert_long_options
, 0);
3637 BT_CLI_LOGE_APPEND_CAUSE("Cannot get popt context.");
3641 poptReadDefaultConfig(pc
, 0);
3643 while ((opt
= poptGetNextOpt(pc
)) > 0) {
3644 arg
= poptGetOptArg(pc
);
3648 if (trimmer_has_begin
) {
3649 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
3654 trimmer_has_begin
= true;
3655 ret
= append_implicit_component_extra_param(
3656 &implicit_trimmer_args
, "begin", arg
);
3657 implicit_trimmer_args
.exists
= true;
3663 if (trimmer_has_end
) {
3664 printf("At --end option: --end or --timerange option already specified\n %s\n",
3669 trimmer_has_end
= true;
3670 ret
= append_implicit_component_extra_param(
3671 &implicit_trimmer_args
, "end", arg
);
3672 implicit_trimmer_args
.exists
= true;
3682 if (trimmer_has_begin
|| trimmer_has_end
) {
3683 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
3688 ret
= split_timerange(arg
, &begin
, &end
);
3690 BT_CLI_LOGE_APPEND_CAUSE("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s",
3695 ret
= append_implicit_component_extra_param(
3696 &implicit_trimmer_args
, "begin", begin
);
3697 ret
|= append_implicit_component_extra_param(
3698 &implicit_trimmer_args
, "end", end
);
3699 implicit_trimmer_args
.exists
= true;
3707 case OPT_CLOCK_CYCLES
:
3708 append_implicit_component_param(
3709 &implicit_text_args
, "clock-cycles", "yes");
3710 implicit_text_args
.exists
= true;
3712 case OPT_CLOCK_DATE
:
3713 append_implicit_component_param(
3714 &implicit_text_args
, "clock-date", "yes");
3715 implicit_text_args
.exists
= true;
3717 case OPT_CLOCK_FORCE_CORRELATE
:
3718 append_implicit_component_param(
3719 &implicit_muxer_args
,
3720 "assume-absolute-clock-classes", "yes");
3723 append_implicit_component_param(
3724 &implicit_text_args
, "clock-gmt", "yes");
3725 append_implicit_component_param(
3726 &implicit_trimmer_args
, "gmt", "yes");
3727 implicit_text_args
.exists
= true;
3729 case OPT_CLOCK_OFFSET
:
3730 implicit_ctf_input_args
.exists
= true;
3731 append_implicit_component_param(
3732 &implicit_ctf_input_args
,
3733 "clock-class-offset-s", arg
);
3735 case OPT_CLOCK_OFFSET_NS
:
3736 implicit_ctf_input_args
.exists
= true;
3737 append_implicit_component_param(
3738 &implicit_ctf_input_args
,
3739 "clock-class-offset-ns", arg
);
3741 case OPT_CLOCK_SECONDS
:
3742 append_implicit_component_param(
3743 &implicit_text_args
, "clock-seconds", "yes");
3744 implicit_text_args
.exists
= true;
3747 implicit_text_args
.exists
= true;
3748 ret
= append_implicit_component_extra_param(
3749 &implicit_text_args
, "color", arg
);
3754 case OPT_DEBUG_INFO
:
3755 implicit_debug_info_args
.exists
= true;
3757 case OPT_DEBUG_INFO_DIR
:
3758 implicit_debug_info_args
.exists
= true;
3759 ret
= append_implicit_component_extra_param(
3760 &implicit_debug_info_args
, "debug-info-dir", arg
);
3765 case OPT_DEBUG_INFO_FULL_PATH
:
3766 implicit_debug_info_args
.exists
= true;
3767 append_implicit_component_param(
3768 &implicit_debug_info_args
, "full-path", "yes");
3770 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3771 implicit_debug_info_args
.exists
= true;
3772 ret
= append_implicit_component_extra_param(
3773 &implicit_debug_info_args
,
3774 "target-prefix", arg
);
3781 bt_value
*fields
= fields_from_arg(arg
);
3787 implicit_text_args
.exists
= true;
3788 ret
= insert_flat_params_from_array(
3789 implicit_text_args
.params_arg
,
3791 bt_value_put_ref(fields
);
3799 bt_value
*names
= names_from_arg(arg
);
3805 implicit_text_args
.exists
= true;
3806 ret
= insert_flat_params_from_array(
3807 implicit_text_args
.params_arg
,
3809 bt_value_put_ref(names
);
3816 append_implicit_component_param(
3817 &implicit_text_args
, "no-delta", "yes");
3818 implicit_text_args
.exists
= true;
3820 case OPT_INPUT_FORMAT
:
3821 if (got_input_format_opt
) {
3822 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --input-format option.");
3826 got_input_format_opt
= true;
3828 if (strcmp(arg
, "ctf") == 0) {
3829 implicit_ctf_input_args
.exists
= true;
3830 } else if (strcmp(arg
, "lttng-live") == 0) {
3831 implicit_lttng_live_args
.exists
= true;
3833 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy input format:\n %s",
3838 case OPT_OUTPUT_FORMAT
:
3839 if (got_output_format_opt
) {
3840 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output-format option.");
3844 got_output_format_opt
= true;
3846 if (strcmp(arg
, "text") == 0) {
3847 implicit_text_args
.exists
= true;
3848 } else if (strcmp(arg
, "ctf") == 0) {
3849 implicit_ctf_output_args
.exists
= true;
3850 } else if (strcmp(arg
, "dummy") == 0) {
3851 implicit_dummy_args
.exists
= true;
3852 } else if (strcmp(arg
, "ctf-metadata") == 0) {
3853 print_ctf_metadata
= true;
3855 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy output format:\n %s",
3862 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output option");
3866 output
= strdup(arg
);
3868 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3873 if (print_run_args_0
) {
3874 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
3878 print_run_args
= true;
3880 case OPT_RUN_ARGS_0
:
3881 if (print_run_args
) {
3882 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
3886 print_run_args_0
= true;
3888 case OPT_STREAM_INTERSECTION
:
3890 * Applies to all traces implementing the trace-info
3893 stream_intersection_mode
= true;
3896 if (*default_log_level
!= BT_LOG_TRACE
&&
3897 *default_log_level
!= BT_LOG_DEBUG
) {
3898 *default_log_level
= BT_LOG_INFO
;
3902 *default_log_level
= BT_LOG_TRACE
;
3912 /* Check for option parsing error */
3914 BT_CLI_LOGE_APPEND_CAUSE("While parsing command-line options, at option %s: %s",
3915 poptBadOption(pc
, 0), poptStrerror(opt
));
3920 * Legacy behaviour: --verbose used to make the `text` output
3921 * format print more information. --verbose is now equivalent to
3922 * the INFO log level, which is why we compare to `BT_LOG_INFO`
3925 if (*default_log_level
== BT_LOG_INFO
) {
3926 append_implicit_component_param(&implicit_text_args
,
3931 * Append home and system plugin paths now that we possibly got
3934 if (append_home_and_system_plugin_paths(plugin_paths
,
3935 force_omit_system_plugin_path
,
3936 force_omit_home_plugin_path
)) {
3940 /* Consume and keep leftover arguments */
3941 while ((leftover
= poptGetArg(pc
))) {
3942 if (bt_value_array_append_string_element(leftovers
, leftover
) !=
3943 BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3944 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3949 /* Print CTF metadata or print LTTng live sessions */
3950 if (print_ctf_metadata
) {
3951 const bt_value
*bt_val_leftover
;
3953 if (bt_value_array_is_empty(leftovers
)) {
3954 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf-metadata specified without a path.");
3958 if (bt_value_array_get_size(leftovers
) > 1) {
3959 BT_CLI_LOGE_APPEND_CAUSE("Too many paths specified for --output-format=ctf-metadata.");
3963 cfg
= bt_config_print_ctf_metadata_create(plugin_paths
);
3968 bt_val_leftover
= bt_value_array_borrow_element_by_index_const(leftovers
, 0);
3969 g_string_assign(cfg
->cmd_data
.print_ctf_metadata
.path
,
3970 bt_value_string_get(bt_val_leftover
));
3974 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
3982 * If -o ctf was specified, make sure an output path (--output)
3983 * was also specified. --output does not imply -o ctf because
3984 * it's also used for the default, implicit -o text if -o ctf
3987 if (implicit_ctf_output_args
.exists
) {
3989 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf specified without --output (trace output path).");
3994 * At this point we know that -o ctf AND --output were
3995 * specified. Make sure that no options were specified
3996 * which would imply -o text because --output would be
3997 * ambiguous in this case. For example, this is wrong:
3999 * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace
4001 * because --names=all implies -o text, and --output
4002 * could apply to both the sink.text.pretty and
4003 * sink.ctf.fs implicit components.
4005 if (implicit_text_args
.exists
) {
4006 BT_CLI_LOGE_APPEND_CAUSE("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text.");
4012 * If -o dummy and -o ctf were not specified, and if there are
4013 * no explicit sink components, then use an implicit
4014 * `sink.text.pretty` component.
4016 if (!implicit_dummy_args
.exists
&& !implicit_ctf_output_args
.exists
&&
4018 implicit_text_args
.exists
= true;
4022 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4023 * `path` parameter if --output was specified.
4026 if (implicit_text_args
.exists
) {
4027 append_implicit_component_extra_param(&implicit_text_args
,
4029 } else if (implicit_ctf_output_args
.exists
) {
4030 append_implicit_component_extra_param(&implicit_ctf_output_args
,
4035 /* Decide where the leftover argument(s) go */
4036 if (bt_value_array_get_size(leftovers
) > 0) {
4037 if (implicit_lttng_live_args
.exists
) {
4038 const bt_value
*bt_val_leftover
;
4040 if (bt_value_array_get_size(leftovers
) > 1) {
4041 BT_CLI_LOGE_APPEND_CAUSE("Too many URLs specified for --input-format=lttng-live.");
4045 bt_val_leftover
= bt_value_array_borrow_element_by_index_const(leftovers
, 0);
4046 lttng_live_url_parts
=
4047 bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_leftover
),
4048 error_buf
, sizeof(error_buf
));
4049 if (!lttng_live_url_parts
.proto
) {
4050 BT_CLI_LOGE_APPEND_CAUSE("Invalid LTTng live URL format: %s.",
4055 if (!lttng_live_url_parts
.session_name
) {
4056 /* Print LTTng live sessions */
4057 cfg
= bt_config_print_lttng_live_sessions_create(
4063 g_string_assign(cfg
->cmd_data
.print_lttng_live_sessions
.url
,
4064 bt_value_string_get(bt_val_leftover
));
4068 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
4075 ret
= append_implicit_component_extra_param(
4076 &implicit_lttng_live_args
, "url",
4077 bt_value_string_get(bt_val_leftover
));
4082 ret
= append_implicit_component_extra_param(
4083 &implicit_lttng_live_args
,
4084 "session-not-found-action", "end");
4090 * Create one source.ctf.fs component, pass it an array
4091 * with the leftovers.
4092 * Note that it still has to be named later.
4094 implicit_ctf_input_args
.exists
= true;
4095 ret
= append_parameter_to_args(implicit_ctf_input_args
.extra_params
,
4096 "paths", leftovers
);
4104 * Ensure mutual exclusion between implicit `source.ctf.fs` and
4105 * `source.ctf.lttng-live` components.
4107 if (implicit_ctf_input_args
.exists
&& implicit_lttng_live_args
.exists
) {
4108 BT_CLI_LOGE_APPEND_CAUSE("Cannot create both implicit `%s` and `%s` components.",
4109 implicit_ctf_input_args
.comp_arg
->str
,
4110 implicit_lttng_live_args
.comp_arg
->str
);
4115 * If the implicit `source.ctf.fs` or `source.ctf.lttng-live`
4116 * components exists, make sure there's at least one leftover
4117 * (which is the path or URL).
4119 if (implicit_ctf_input_args
.exists
&& bt_value_array_is_empty(leftovers
)) {
4120 BT_CLI_LOGE_APPEND_CAUSE("Missing path for implicit `%s` component.",
4121 implicit_ctf_input_args
.comp_arg
->str
);
4125 if (implicit_lttng_live_args
.exists
&& bt_value_array_is_empty(leftovers
)) {
4126 BT_CLI_LOGE_APPEND_CAUSE("Missing URL for implicit `%s` component.",
4127 implicit_lttng_live_args
.comp_arg
->str
);
4131 /* Assign names to implicit components */
4132 ret
= assign_name_to_implicit_component(&implicit_ctf_input_args
,
4133 "source-ctf-fs", all_names
, &source_names
, true);
4138 ret
= assign_name_to_implicit_component(&implicit_lttng_live_args
,
4139 "lttng-live", all_names
, &source_names
, true);
4144 ret
= assign_name_to_implicit_component(&implicit_text_args
,
4145 "pretty", all_names
, &sink_names
, true);
4150 ret
= assign_name_to_implicit_component(&implicit_ctf_output_args
,
4151 "sink-ctf-fs", all_names
, &sink_names
, true);
4156 ret
= assign_name_to_implicit_component(&implicit_dummy_args
,
4157 "dummy", all_names
, &sink_names
, true);
4162 ret
= assign_name_to_implicit_component(&implicit_muxer_args
,
4163 "muxer", all_names
, NULL
, false);
4168 ret
= assign_name_to_implicit_component(&implicit_trimmer_args
,
4169 "trimmer", all_names
, NULL
, false);
4174 ret
= assign_name_to_implicit_component(&implicit_debug_info_args
,
4175 "debug-info", all_names
, NULL
, false);
4180 /* Make sure there's at least one source and one sink */
4181 if (!source_names
) {
4182 BT_CLI_LOGE_APPEND_CAUSE("No source component.");
4187 BT_CLI_LOGE_APPEND_CAUSE("No sink component.");
4192 * Prepend the muxer, the trimmer, and the debug info to the
4193 * filter chain so that we have:
4195 * sources -> muxer -> [trimmer] -> [debug info] ->
4196 * [user filters] -> sinks
4198 if (implicit_debug_info_args
.exists
) {
4199 if (g_list_prepend_gstring(&filter_names
,
4200 implicit_debug_info_args
.name_arg
->str
)) {
4205 if (implicit_trimmer_args
.exists
) {
4206 if (g_list_prepend_gstring(&filter_names
,
4207 implicit_trimmer_args
.name_arg
->str
)) {
4212 if (g_list_prepend_gstring(&filter_names
,
4213 implicit_muxer_args
.name_arg
->str
)) {
4218 * Append the equivalent run arguments for the implicit
4221 ret
= append_run_args_for_implicit_component(&implicit_ctf_input_args
, run_args
);
4226 ret
= append_run_args_for_implicit_component(&implicit_lttng_live_args
,
4232 ret
= append_run_args_for_implicit_component(&implicit_text_args
,
4238 ret
= append_run_args_for_implicit_component(&implicit_ctf_output_args
,
4244 ret
= append_run_args_for_implicit_component(&implicit_dummy_args
,
4250 ret
= append_run_args_for_implicit_component(&implicit_muxer_args
,
4256 ret
= append_run_args_for_implicit_component(&implicit_trimmer_args
,
4262 ret
= append_run_args_for_implicit_component(&implicit_debug_info_args
,
4268 /* Auto-connect components */
4269 ret
= convert_auto_connect(run_args
, source_names
, filter_names
,
4272 BT_CLI_LOGE_APPEND_CAUSE("Cannot auto-connect components.");
4277 * We have all the run command arguments now. Depending on
4278 * --run-args, we pass this to the run command or print them
4281 if (print_run_args
|| print_run_args_0
) {
4282 if (stream_intersection_mode
) {
4283 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --stream-intersection with --run-args or --run-args-0.");
4287 for (i
= 0; i
< bt_value_array_get_size(run_args
); i
++) {
4288 const bt_value
*arg_value
=
4289 bt_value_array_borrow_element_by_index(run_args
,
4292 GString
*quoted
= NULL
;
4293 const char *arg_to_print
;
4295 BT_ASSERT(arg_value
);
4296 arg
= bt_value_string_get(arg_value
);
4298 if (print_run_args
) {
4299 quoted
= bt_common_shell_quote(arg
, true);
4304 arg_to_print
= quoted
->str
;
4309 printf("%s", arg_to_print
);
4312 g_string_free(quoted
, TRUE
);
4315 if (i
< bt_value_array_get_size(run_args
) - 1) {
4316 if (print_run_args
) {
4325 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4330 * If the log level is still unset at this point, set it to
4331 * the program's default.
4333 if (*default_log_level
< 0) {
4334 *default_log_level
= cli_default_log_level
;
4337 cfg
= bt_config_run_from_args_array(run_args
, retcode
,
4338 force_omit_system_plugin_path
,
4339 force_omit_home_plugin_path
,
4340 initial_plugin_paths
, *default_log_level
);
4345 cfg
->cmd_data
.run
.stream_intersection_mode
= stream_intersection_mode
;
4350 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4354 * If the log level is still unset at this point, set it to
4355 * the program's default.
4357 if (*default_log_level
< 0) {
4358 *default_log_level
= cli_default_log_level
;
4362 poptFreeContext(pc
);
4369 g_string_free(cur_name
, TRUE
);
4372 if (cur_name_prefix
) {
4373 g_string_free(cur_name_prefix
, TRUE
);
4376 bt_value_put_ref(run_args
);
4377 bt_value_put_ref(all_names
);
4378 destroy_glist_of_gstring(source_names
);
4379 destroy_glist_of_gstring(filter_names
);
4380 destroy_glist_of_gstring(sink_names
);
4381 bt_value_put_ref(leftovers
);
4382 finalize_implicit_component_args(&implicit_ctf_input_args
);
4383 finalize_implicit_component_args(&implicit_ctf_output_args
);
4384 finalize_implicit_component_args(&implicit_lttng_live_args
);
4385 finalize_implicit_component_args(&implicit_dummy_args
);
4386 finalize_implicit_component_args(&implicit_text_args
);
4387 finalize_implicit_component_args(&implicit_debug_info_args
);
4388 finalize_implicit_component_args(&implicit_muxer_args
);
4389 finalize_implicit_component_args(&implicit_trimmer_args
);
4390 bt_value_put_ref(plugin_paths
);
4391 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
4396 * Prints the Babeltrace 2.x general usage.
4399 void print_gen_usage(FILE *fp
)
4401 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4403 fprintf(fp
, "General options:\n");
4405 fprintf(fp
, " -d, --debug Enable debug mode (same as --log-level=V)\n");
4406 fprintf(fp
, " -h, --help Show this help and quit\n");
4407 fprintf(fp
, " -l, --log-level=LVL Set the default log level to LVL (`N`, `V`, `D`,\n");
4408 fprintf(fp
, " `I`, `W` (default), `E`, or `F`)\n");
4409 fprintf(fp
, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4410 fprintf(fp
, " -V, --version Show version and quit\n");
4412 fprintf(fp
, "Available commands:\n");
4414 fprintf(fp
, " convert Convert and trim traces (default)\n");
4415 fprintf(fp
, " help Get help for a plugin or a component class\n");
4416 fprintf(fp
, " list-plugins List available plugins and their content\n");
4417 fprintf(fp
, " query Query objects from a component class\n");
4418 fprintf(fp
, " run Build a processing graph and run it\n");
4420 fprintf(fp
, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n");
4423 struct bt_config
*bt_config_cli_args_create(int argc
, const char *argv
[],
4424 int *retcode
, bool force_omit_system_plugin_path
,
4425 bool force_omit_home_plugin_path
,
4426 const bt_value
*initial_plugin_paths
)
4428 struct bt_config
*config
= NULL
;
4430 const char **command_argv
= NULL
;
4431 int command_argc
= -1;
4432 const char *command_name
= NULL
;
4433 int default_log_level
= -1;
4436 COMMAND_TYPE_NONE
= -1,
4437 COMMAND_TYPE_RUN
= 0,
4438 COMMAND_TYPE_CONVERT
,
4439 COMMAND_TYPE_LIST_PLUGINS
,
4442 } command_type
= COMMAND_TYPE_NONE
;
4446 if (!initial_plugin_paths
) {
4447 initial_plugin_paths
= bt_value_array_create();
4448 if (!initial_plugin_paths
) {
4453 bt_value_get_ref(initial_plugin_paths
);
4459 print_gen_usage(stdout
);
4463 for (i
= 1; i
< argc
; i
++) {
4464 const char *cur_arg
= argv
[i
];
4465 const char *next_arg
= i
== (argc
- 1) ? NULL
: argv
[i
+ 1];
4467 if (strcmp(cur_arg
, "-d") == 0 ||
4468 strcmp(cur_arg
, "--debug") == 0) {
4469 default_log_level
= BT_LOG_TRACE
;
4470 } else if (strcmp(cur_arg
, "-v") == 0 ||
4471 strcmp(cur_arg
, "--verbose") == 0) {
4472 if (default_log_level
!= BT_LOG_TRACE
&&
4473 default_log_level
!= BT_LOG_DEBUG
) {
4475 * Legacy: do not override a previous
4476 * --debug because --verbose and --debug
4477 * can be specified together (in this
4478 * case we want the lowest log level to
4481 default_log_level
= BT_LOG_INFO
;
4483 } else if (strcmp(cur_arg
, "--log-level") == 0 ||
4484 strcmp(cur_arg
, "-l") == 0) {
4486 BT_CLI_LOGE_APPEND_CAUSE("Missing log level value for --log-level option.");
4492 bt_log_get_level_from_string(next_arg
);
4493 if (default_log_level
< 0) {
4494 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
4501 } else if (strncmp(cur_arg
, "--log-level=", 12) == 0) {
4502 const char *arg
= &cur_arg
[12];
4504 default_log_level
= bt_log_get_level_from_string(arg
);
4505 if (default_log_level
< 0) {
4506 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
4511 } else if (strncmp(cur_arg
, "-l", 2) == 0) {
4512 const char *arg
= &cur_arg
[2];
4514 default_log_level
= bt_log_get_level_from_string(arg
);
4515 if (default_log_level
< 0) {
4516 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
4521 } else if (strcmp(cur_arg
, "-V") == 0 ||
4522 strcmp(cur_arg
, "--version") == 0) {
4525 } else if (strcmp(cur_arg
, "-h") == 0 ||
4526 strcmp(cur_arg
, "--help") == 0) {
4527 print_gen_usage(stdout
);
4531 * First unknown argument: is it a known command
4534 command_argv
= &argv
[i
];
4535 command_argc
= argc
- i
;
4537 if (strcmp(cur_arg
, "convert") == 0) {
4538 command_type
= COMMAND_TYPE_CONVERT
;
4539 } else if (strcmp(cur_arg
, "list-plugins") == 0) {
4540 command_type
= COMMAND_TYPE_LIST_PLUGINS
;
4541 } else if (strcmp(cur_arg
, "help") == 0) {
4542 command_type
= COMMAND_TYPE_HELP
;
4543 } else if (strcmp(cur_arg
, "query") == 0) {
4544 command_type
= COMMAND_TYPE_QUERY
;
4545 } else if (strcmp(cur_arg
, "run") == 0) {
4546 command_type
= COMMAND_TYPE_RUN
;
4549 * Unknown argument, but not a known
4550 * command name: assume the default
4551 * `convert` command.
4553 command_type
= COMMAND_TYPE_CONVERT
;
4554 command_name
= "convert";
4555 command_argv
= &argv
[i
- 1];
4556 command_argc
= argc
- i
+ 1;
4562 if (command_type
== COMMAND_TYPE_NONE
) {
4564 * We only got non-help, non-version general options
4565 * like --verbose and --debug, without any other
4566 * arguments, so we can't do anything useful: print the
4569 print_gen_usage(stdout
);
4573 BT_ASSERT(command_argv
);
4574 BT_ASSERT(command_argc
>= 0);
4577 * The convert command can set its own default log level for
4578 * backward compatibility reasons. It only does so if there's no
4579 * log level yet, so do not force one for this command.
4581 if (command_type
!= COMMAND_TYPE_CONVERT
&& default_log_level
< 0) {
4582 /* Default log level */
4583 default_log_level
= cli_default_log_level
;
4586 switch (command_type
) {
4587 case COMMAND_TYPE_RUN
:
4588 config
= bt_config_run_from_args(command_argc
, command_argv
,
4589 retcode
, force_omit_system_plugin_path
,
4590 force_omit_home_plugin_path
, initial_plugin_paths
,
4593 case COMMAND_TYPE_CONVERT
:
4594 config
= bt_config_convert_from_args(command_argc
, command_argv
,
4595 retcode
, force_omit_system_plugin_path
,
4596 force_omit_home_plugin_path
,
4597 initial_plugin_paths
, &default_log_level
);
4599 case COMMAND_TYPE_LIST_PLUGINS
:
4600 config
= bt_config_list_plugins_from_args(command_argc
,
4601 command_argv
, retcode
, force_omit_system_plugin_path
,
4602 force_omit_home_plugin_path
, initial_plugin_paths
);
4604 case COMMAND_TYPE_HELP
:
4605 config
= bt_config_help_from_args(command_argc
,
4606 command_argv
, retcode
, force_omit_system_plugin_path
,
4607 force_omit_home_plugin_path
, initial_plugin_paths
,
4610 case COMMAND_TYPE_QUERY
:
4611 config
= bt_config_query_from_args(command_argc
,
4612 command_argv
, retcode
, force_omit_system_plugin_path
,
4613 force_omit_home_plugin_path
, initial_plugin_paths
,
4621 BT_ASSERT(default_log_level
>= BT_LOG_TRACE
);
4622 config
->log_level
= default_log_level
;
4623 config
->command_name
= command_name
;
4627 bt_value_put_ref(initial_plugin_paths
);