2 * Babeltrace trace converter - parameter parsing
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "CLI/CFG-CLI-ARGS"
31 #include "common/assert.h"
35 #include <babeltrace2/babeltrace.h>
36 #include "common/common.h"
38 #include <sys/types.h>
39 #include "argpar/argpar.h"
40 #include "babeltrace2-cfg.h"
41 #include "babeltrace2-cfg-cli-args.h"
42 #include "babeltrace2-cfg-cli-args-connect.h"
43 #include "babeltrace2-cfg-cli-params-arg.h"
44 #include "babeltrace2-plugins.h"
45 #include "babeltrace2-query.h"
46 #include "autodisc/autodisc.h"
47 #include "common/version.h"
49 static const int cli_default_log_level
= BT_LOG_WARNING
;
51 /* INI-style parsing FSM states */
52 enum ini_parsing_fsm_state
{
53 /* Expect a map key (identifier) */
56 /* Expect an equal character ('=') */
62 /* Expect a comma character (',') */
66 /* INI-style parsing state variables */
67 struct ini_parsing_state
{
68 /* Lexical scanner (owned by this) */
71 /* Output map value object being filled (owned by this) */
74 /* Next expected FSM state */
75 enum ini_parsing_fsm_state expecting
;
77 /* Last decoded map key (owned by this) */
80 /* Complete INI-style string to parse (not owned by this) */
83 /* Error buffer (not owned by this) */
87 /* Offset option with "is set" boolean */
93 /* Legacy "ctf"/"lttng-live" format options */
94 struct ctf_legacy_opts
{
95 struct offset_opt offset_s
;
96 struct offset_opt offset_ns
;
97 bool stream_intersection
;
100 /* Legacy "text" format options */
101 struct text_legacy_opts
{
103 * output, dbg_info_dir, dbg_info_target_prefix, names,
104 * and fields are owned by this.
107 GString
*dbg_info_dir
;
108 GString
*dbg_info_target_prefix
;
109 const bt_value
*names
;
110 const bt_value
*fields
;
118 bool dbg_info_full_path
;
122 /* Legacy input format format */
123 enum legacy_input_format
{
124 LEGACY_INPUT_FORMAT_NONE
= 0,
125 LEGACY_INPUT_FORMAT_CTF
,
126 LEGACY_INPUT_FORMAT_LTTNG_LIVE
,
129 /* Legacy output format format */
130 enum legacy_output_format
{
131 LEGACY_OUTPUT_FORMAT_NONE
= 0,
132 LEGACY_OUTPUT_FORMAT_TEXT
,
133 LEGACY_OUTPUT_FORMAT_DUMMY
,
136 #define BT_CLI_LOGE_APPEND_CAUSE_OOM() BT_CLI_LOGE_APPEND_CAUSE("Out of memory.")
139 * Returns the plugin name, component class name, component class type,
140 * and component name from a command-line --component option's argument.
141 * arg must have the following format:
143 * [NAME:]TYPE.PLUGIN.CLS
145 * where NAME is the optional component name, TYPE is either `source`,
146 * `filter`, or `sink`, PLUGIN is the plugin name, and CLS is the
147 * component class name.
149 * On success, both *plugin and *component are not NULL. *plugin
150 * and *comp_cls are owned by the caller. On success, *name can be NULL
151 * if no component class name was found, and *comp_cls_type is set.
154 void plugin_comp_cls_names(const char *arg
, char **name
, char **plugin
,
155 char **comp_cls
, bt_component_class_type
*comp_cls_type
)
157 const char *at
= arg
;
158 GString
*gs_name
= NULL
;
159 GString
*gs_comp_cls_type
= NULL
;
160 GString
*gs_plugin
= NULL
;
161 GString
*gs_comp_cls
= NULL
;
167 BT_ASSERT(comp_cls_type
);
169 if (!bt_common_string_is_printable(arg
)) {
170 BT_CLI_LOGE_APPEND_CAUSE("Argument contains a non-printable character.");
174 /* Parse the component name */
175 gs_name
= bt_common_string_until(at
, ".:\\", ":", &end_pos
);
180 if (arg
[end_pos
] == ':') {
184 g_string_assign(gs_name
, "");
187 /* Parse the component class type */
188 gs_comp_cls_type
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
189 if (!gs_comp_cls_type
|| at
[end_pos
] == '\0') {
190 BT_CLI_LOGE_APPEND_CAUSE("Missing component class type (`source`, `filter`, or `sink`).");
194 if (strcmp(gs_comp_cls_type
->str
, "source") == 0 ||
195 strcmp(gs_comp_cls_type
->str
, "src") == 0) {
196 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SOURCE
;
197 } else if (strcmp(gs_comp_cls_type
->str
, "filter") == 0 ||
198 strcmp(gs_comp_cls_type
->str
, "flt") == 0) {
199 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_FILTER
;
200 } else if (strcmp(gs_comp_cls_type
->str
, "sink") == 0) {
201 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SINK
;
203 BT_CLI_LOGE_APPEND_CAUSE("Unknown component class type: `%s`.",
204 gs_comp_cls_type
->str
);
210 /* Parse the plugin name */
211 gs_plugin
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
212 if (!gs_plugin
|| gs_plugin
->len
== 0 || at
[end_pos
] == '\0') {
213 BT_CLI_LOGE_APPEND_CAUSE("Missing plugin or component class name.");
219 /* Parse the component class name */
220 gs_comp_cls
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
221 if (!gs_comp_cls
|| gs_comp_cls
->len
== 0) {
222 BT_CLI_LOGE_APPEND_CAUSE("Missing component class name.");
226 if (at
[end_pos
] != '\0') {
227 /* Found a non-escaped `.` */
232 if (gs_name
->len
== 0) {
234 g_string_free(gs_name
, TRUE
);
236 *name
= gs_name
->str
;
237 g_string_free(gs_name
, FALSE
);
240 g_string_free(gs_name
, TRUE
);
243 *plugin
= gs_plugin
->str
;
244 *comp_cls
= gs_comp_cls
->str
;
245 g_string_free(gs_plugin
, FALSE
);
246 g_string_free(gs_comp_cls
, FALSE
);
262 g_string_free(gs_name
, TRUE
);
266 g_string_free(gs_plugin
, TRUE
);
270 g_string_free(gs_comp_cls
, TRUE
);
273 if (gs_comp_cls_type
) {
274 g_string_free(gs_comp_cls_type
, TRUE
);
281 * Prints the Babeltrace version.
284 void print_version(void)
286 if (GIT_VERSION
[0] == '\0') {
287 puts("Babeltrace " VERSION
);
289 puts("Babeltrace " VERSION
" - " GIT_VERSION
);
294 * Destroys a component configuration.
297 void bt_config_component_destroy(bt_object
*obj
)
299 struct bt_config_component
*bt_config_component
=
300 container_of(obj
, struct bt_config_component
, base
);
306 if (bt_config_component
->plugin_name
) {
307 g_string_free(bt_config_component
->plugin_name
, TRUE
);
310 if (bt_config_component
->comp_cls_name
) {
311 g_string_free(bt_config_component
->comp_cls_name
, TRUE
);
314 if (bt_config_component
->instance_name
) {
315 g_string_free(bt_config_component
->instance_name
, TRUE
);
318 BT_VALUE_PUT_REF_AND_RESET(bt_config_component
->params
);
319 g_free(bt_config_component
);
326 * Creates a component configuration using the given plugin name and
327 * component name. `plugin_name` and `comp_cls_name` are copied (belong
328 * to the return value).
330 * Return value is owned by the caller.
333 struct bt_config_component
*bt_config_component_create(
334 bt_component_class_type type
,
335 const char *plugin_name
, const char *comp_cls_name
,
338 struct bt_config_component
*cfg_component
= NULL
;
340 cfg_component
= g_new0(struct bt_config_component
, 1);
341 if (!cfg_component
) {
342 BT_CLI_LOGE_APPEND_CAUSE_OOM();
346 bt_object_init_shared(&cfg_component
->base
,
347 bt_config_component_destroy
);
348 cfg_component
->type
= type
;
349 cfg_component
->plugin_name
= g_string_new(plugin_name
);
350 if (!cfg_component
->plugin_name
) {
351 BT_CLI_LOGE_APPEND_CAUSE_OOM();
355 cfg_component
->comp_cls_name
= g_string_new(comp_cls_name
);
356 if (!cfg_component
->comp_cls_name
) {
357 BT_CLI_LOGE_APPEND_CAUSE_OOM();
361 cfg_component
->instance_name
= g_string_new(NULL
);
362 if (!cfg_component
->instance_name
) {
363 BT_CLI_LOGE_APPEND_CAUSE_OOM();
367 cfg_component
->log_level
= init_log_level
;
369 /* Start with empty parameters */
370 cfg_component
->params
= bt_value_map_create();
371 if (!cfg_component
->params
) {
372 BT_CLI_LOGE_APPEND_CAUSE_OOM();
379 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
382 return cfg_component
;
386 * Creates a component configuration from a command-line --component
390 struct bt_config_component
*bt_config_component_from_arg(const char *arg
,
393 struct bt_config_component
*cfg_comp
= NULL
;
395 char *plugin_name
= NULL
;
396 char *comp_cls_name
= NULL
;
397 bt_component_class_type type
;
399 plugin_comp_cls_names(arg
, &name
, &plugin_name
, &comp_cls_name
, &type
);
400 if (!plugin_name
|| !comp_cls_name
) {
404 cfg_comp
= bt_config_component_create(type
, plugin_name
, comp_cls_name
,
411 g_string_assign(cfg_comp
->instance_name
, name
);
417 BT_OBJECT_PUT_REF_AND_RESET(cfg_comp
);
422 g_free(comp_cls_name
);
427 * Destroys a configuration.
430 void bt_config_destroy(bt_object
*obj
)
432 struct bt_config
*cfg
=
433 container_of(obj
, struct bt_config
, base
);
439 BT_VALUE_PUT_REF_AND_RESET(cfg
->plugin_paths
);
441 switch (cfg
->command
) {
442 case BT_CONFIG_COMMAND_RUN
:
443 if (cfg
->cmd_data
.run
.sources
) {
444 g_ptr_array_free(cfg
->cmd_data
.run
.sources
, TRUE
);
447 if (cfg
->cmd_data
.run
.filters
) {
448 g_ptr_array_free(cfg
->cmd_data
.run
.filters
, TRUE
);
451 if (cfg
->cmd_data
.run
.sinks
) {
452 g_ptr_array_free(cfg
->cmd_data
.run
.sinks
, TRUE
);
455 if (cfg
->cmd_data
.run
.connections
) {
456 g_ptr_array_free(cfg
->cmd_data
.run
.connections
,
460 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
462 case BT_CONFIG_COMMAND_HELP
:
463 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.help
.cfg_component
);
465 case BT_CONFIG_COMMAND_QUERY
:
466 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.query
.cfg_component
);
468 if (cfg
->cmd_data
.query
.object
) {
469 g_string_free(cfg
->cmd_data
.query
.object
, TRUE
);
472 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
473 if (cfg
->cmd_data
.print_ctf_metadata
.path
) {
474 g_string_free(cfg
->cmd_data
.print_ctf_metadata
.path
,
477 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
481 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
482 if (cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
484 cfg
->cmd_data
.print_lttng_live_sessions
.url
,
487 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
502 void destroy_glist_of_gstring(GList
*list
)
510 for (at
= list
; at
; at
= g_list_next(at
)) {
511 g_string_free(at
->data
, TRUE
);
518 * Creates a simple lexical scanner for parsing comma-delimited names
521 * Return value is owned by the caller.
524 GScanner
*create_csv_identifiers_scanner(void)
527 GScannerConfig scanner_config
= {
528 .cset_skip_characters
= " \t\n",
529 .cset_identifier_first
= G_CSET_a_2_z G_CSET_A_2_Z
"_",
530 .cset_identifier_nth
= G_CSET_a_2_z G_CSET_A_2_Z
":_-",
531 .case_sensitive
= TRUE
,
532 .cpair_comment_single
= NULL
,
533 .skip_comment_multi
= TRUE
,
534 .skip_comment_single
= TRUE
,
535 .scan_comment_multi
= FALSE
,
536 .scan_identifier
= TRUE
,
537 .scan_identifier_1char
= TRUE
,
538 .scan_identifier_NULL
= FALSE
,
539 .scan_symbols
= FALSE
,
540 .symbol_2_token
= FALSE
,
541 .scope_0_fallback
= FALSE
,
542 .scan_binary
= FALSE
,
546 .scan_hex_dollar
= FALSE
,
547 .numbers_2_int
= FALSE
,
548 .int_2_float
= FALSE
,
549 .store_int64
= FALSE
,
550 .scan_string_sq
= FALSE
,
551 .scan_string_dq
= FALSE
,
552 .identifier_2_string
= FALSE
,
553 .char_2_token
= TRUE
,
556 scanner
= g_scanner_new(&scanner_config
);
558 BT_CLI_LOGE_APPEND_CAUSE_OOM();
565 * Converts a comma-delimited list of known names (--names option) to
566 * an array value object containing those names as string value objects.
568 * Return value is owned by the caller.
571 bt_value
*names_from_arg(const char *arg
)
573 GScanner
*scanner
= NULL
;
574 bt_value
*names
= NULL
;
575 bool found_all
= false, found_none
= false, found_item
= false;
577 names
= bt_value_array_create();
579 BT_CLI_LOGE_APPEND_CAUSE_OOM();
583 scanner
= create_csv_identifiers_scanner();
588 g_scanner_input_text(scanner
, arg
, strlen(arg
));
591 GTokenType token_type
= g_scanner_get_next_token(scanner
);
593 switch (token_type
) {
594 case G_TOKEN_IDENTIFIER
:
596 const char *identifier
= scanner
->value
.v_identifier
;
598 if (strcmp(identifier
, "payload") == 0 ||
599 strcmp(identifier
, "args") == 0 ||
600 strcmp(identifier
, "arg") == 0) {
602 if (bt_value_array_append_string_element(names
,
606 } else if (strcmp(identifier
, "context") == 0 ||
607 strcmp(identifier
, "ctx") == 0) {
609 if (bt_value_array_append_string_element(names
,
613 } else if (strcmp(identifier
, "scope") == 0 ||
614 strcmp(identifier
, "header") == 0) {
616 if (bt_value_array_append_string_element(names
,
620 } else if (strcmp(identifier
, "all") == 0) {
622 if (bt_value_array_append_string_element(names
,
626 } else if (strcmp(identifier
, "none") == 0) {
628 if (bt_value_array_append_string_element(names
,
633 BT_CLI_LOGE_APPEND_CAUSE("Unknown name: `%s`.",
649 if (found_none
&& found_all
) {
650 BT_CLI_LOGE_APPEND_CAUSE("Only either `all` or `none` can be specified in the list given to the --names option, but not both.");
654 * Legacy behavior is to clear the defaults (show none) when at
655 * least one item is specified.
657 if (found_item
&& !found_none
&& !found_all
) {
658 if (bt_value_array_append_string_element(names
, "none")) {
663 g_scanner_destroy(scanner
);
668 BT_VALUE_PUT_REF_AND_RESET(names
);
670 g_scanner_destroy(scanner
);
676 * Converts a comma-delimited list of known fields (--fields option) to
677 * an array value object containing those fields as string
680 * Return value is owned by the caller.
683 bt_value
*fields_from_arg(const char *arg
)
685 GScanner
*scanner
= NULL
;
688 fields
= bt_value_array_create();
690 BT_CLI_LOGE_APPEND_CAUSE_OOM();
694 scanner
= create_csv_identifiers_scanner();
699 g_scanner_input_text(scanner
, arg
, strlen(arg
));
702 GTokenType token_type
= g_scanner_get_next_token(scanner
);
704 switch (token_type
) {
705 case G_TOKEN_IDENTIFIER
:
707 const char *identifier
= scanner
->value
.v_identifier
;
709 if (strcmp(identifier
, "trace") == 0 ||
710 strcmp(identifier
, "trace:hostname") == 0 ||
711 strcmp(identifier
, "trace:domain") == 0 ||
712 strcmp(identifier
, "trace:procname") == 0 ||
713 strcmp(identifier
, "trace:vpid") == 0 ||
714 strcmp(identifier
, "loglevel") == 0 ||
715 strcmp(identifier
, "emf") == 0 ||
716 strcmp(identifier
, "callsite") == 0 ||
717 strcmp(identifier
, "all") == 0) {
718 if (bt_value_array_append_string_element(fields
,
723 BT_CLI_LOGE_APPEND_CAUSE("Unknown field: `%s`.",
741 BT_VALUE_PUT_REF_AND_RESET(fields
);
745 g_scanner_destroy(scanner
);
751 void append_param_arg(GString
*params_arg
, const char *key
, const char *value
)
753 BT_ASSERT(params_arg
);
757 if (params_arg
->len
!= 0) {
758 g_string_append_c(params_arg
, ',');
761 g_string_append(params_arg
, key
);
762 g_string_append_c(params_arg
, '=');
763 g_string_append(params_arg
, value
);
767 * Inserts the equivalent "prefix-NAME=yes" strings into params_arg
768 * where the names are in names_array.
771 int insert_flat_params_from_array(GString
*params_arg
,
772 const bt_value
*names_array
, const char *prefix
)
776 GString
*tmpstr
= NULL
, *default_value
= NULL
;
777 bool default_set
= false, non_default_set
= false;
780 * names_array may be NULL if no CLI options were specified to
781 * trigger its creation.
787 tmpstr
= g_string_new(NULL
);
789 BT_CLI_LOGE_APPEND_CAUSE_OOM();
794 default_value
= g_string_new(NULL
);
795 if (!default_value
) {
796 BT_CLI_LOGE_APPEND_CAUSE_OOM();
801 for (i
= 0; i
< bt_value_array_get_size(names_array
); i
++) {
802 const bt_value
*str_obj
=
803 bt_value_array_borrow_element_by_index_const(names_array
,
806 bool is_default
= false;
809 BT_CLI_LOGE_APPEND_CAUSE("Unexpected error.");
814 suffix
= bt_value_string_get(str_obj
);
816 g_string_assign(tmpstr
, prefix
);
817 g_string_append(tmpstr
, "-");
819 /* Special-case for "all" and "none". */
820 if (strcmp(suffix
, "all") == 0) {
822 g_string_assign(default_value
, "show");
823 } else if (strcmp(suffix
, "none") == 0) {
825 g_string_assign(default_value
, "hide");
829 g_string_append(tmpstr
, "default");
830 append_param_arg(params_arg
, tmpstr
->str
,
833 non_default_set
= true;
834 g_string_append(tmpstr
, suffix
);
835 append_param_arg(params_arg
, tmpstr
->str
, "yes");
839 /* Implicit field-default=hide if any non-default option is set. */
840 if (non_default_set
&& !default_set
) {
841 g_string_assign(tmpstr
, prefix
);
842 g_string_append(tmpstr
, "-default");
843 g_string_assign(default_value
, "hide");
844 append_param_arg(params_arg
, tmpstr
->str
, default_value
->str
);
849 g_string_free(default_value
, TRUE
);
853 g_string_free(tmpstr
, TRUE
);
866 OPT_CLOCK_FORCE_CORRELATE
,
877 OPT_DEBUG_INFO_FULL_PATH
,
878 OPT_DEBUG_INFO_TARGET_PREFIX
,
887 OPT_OMIT_HOME_PLUGIN_PATH
,
888 OPT_OMIT_SYSTEM_PLUGIN_PATH
,
893 OPT_RESET_BASE_PARAMS
,
897 OPT_STREAM_INTERSECTION
,
903 enum bt_config_component_dest
{
904 BT_CONFIG_COMPONENT_DEST_UNKNOWN
= -1,
905 BT_CONFIG_COMPONENT_DEST_SOURCE
,
906 BT_CONFIG_COMPONENT_DEST_FILTER
,
907 BT_CONFIG_COMPONENT_DEST_SINK
,
911 * Adds a configuration component to the appropriate configuration
912 * array depending on the destination.
915 void add_run_cfg_comp(struct bt_config
*cfg
,
916 struct bt_config_component
*cfg_comp
,
917 enum bt_config_component_dest dest
)
919 bt_object_get_ref(cfg_comp
);
922 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
923 g_ptr_array_add(cfg
->cmd_data
.run
.sources
, cfg_comp
);
925 case BT_CONFIG_COMPONENT_DEST_FILTER
:
926 g_ptr_array_add(cfg
->cmd_data
.run
.filters
, cfg_comp
);
928 case BT_CONFIG_COMPONENT_DEST_SINK
:
929 g_ptr_array_add(cfg
->cmd_data
.run
.sinks
, cfg_comp
);
937 int add_run_cfg_comp_check_name(struct bt_config
*cfg
,
938 struct bt_config_component
*cfg_comp
,
939 enum bt_config_component_dest dest
,
940 bt_value
*instance_names
)
944 if (cfg_comp
->instance_name
->len
== 0) {
945 BT_CLI_LOGE_APPEND_CAUSE("Found an unnamed component.");
950 if (bt_value_map_has_entry(instance_names
,
951 cfg_comp
->instance_name
->str
)) {
952 BT_CLI_LOGE_APPEND_CAUSE("Duplicate component instance name:\n %s",
953 cfg_comp
->instance_name
->str
);
958 if (bt_value_map_insert_entry(instance_names
,
959 cfg_comp
->instance_name
->str
, bt_value_null
)) {
960 BT_CLI_LOGE_APPEND_CAUSE_OOM();
965 add_run_cfg_comp(cfg
, cfg_comp
, dest
);
972 int append_env_var_plugin_paths(bt_value
*plugin_paths
)
977 if (bt_common_is_setuid_setgid()) {
978 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
982 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
987 ret
= bt_config_append_plugin_paths(plugin_paths
, envvar
);
991 BT_CLI_LOGE_APPEND_CAUSE("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH.");
998 int append_home_and_system_plugin_paths(bt_value
*plugin_paths
,
999 bool omit_system_plugin_path
, bool omit_home_plugin_path
)
1003 if (!omit_home_plugin_path
) {
1004 if (bt_common_is_setuid_setgid()) {
1005 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1007 char *home_plugin_dir
= bt_common_get_home_plugin_path(
1008 BT_LOG_OUTPUT_LEVEL
);
1010 if (home_plugin_dir
) {
1011 ret
= bt_config_append_plugin_paths(
1012 plugin_paths
, home_plugin_dir
);
1013 free(home_plugin_dir
);
1016 BT_CLI_LOGE_APPEND_CAUSE("Invalid home plugin path.");
1023 if (!omit_system_plugin_path
) {
1024 if (bt_config_append_plugin_paths(plugin_paths
,
1025 bt_common_get_system_plugin_path())) {
1026 BT_CLI_LOGE_APPEND_CAUSE("Invalid system plugin path.");
1032 BT_CLI_LOGE_APPEND_CAUSE("Cannot append home and system plugin paths.");
1037 int append_home_and_system_plugin_paths_cfg(struct bt_config
*cfg
)
1039 return append_home_and_system_plugin_paths(cfg
->plugin_paths
,
1040 cfg
->omit_system_plugin_path
, cfg
->omit_home_plugin_path
);
1044 struct bt_config
*bt_config_base_create(enum bt_config_command command
,
1045 const bt_value
*initial_plugin_paths
,
1048 struct bt_config
*cfg
;
1051 cfg
= g_new0(struct bt_config
, 1);
1053 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1057 bt_object_init_shared(&cfg
->base
, bt_config_destroy
);
1058 cfg
->command
= command
;
1059 cfg
->command_needs_plugins
= needs_plugins
;
1061 if (initial_plugin_paths
) {
1062 bt_value
*initial_plugin_paths_copy
;
1064 (void) bt_value_copy(initial_plugin_paths
,
1065 &initial_plugin_paths_copy
);
1066 cfg
->plugin_paths
= initial_plugin_paths_copy
;
1068 cfg
->plugin_paths
= bt_value_array_create();
1069 if (!cfg
->plugin_paths
) {
1070 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1078 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1085 struct bt_config
*bt_config_run_create(
1086 const bt_value
*initial_plugin_paths
)
1088 struct bt_config
*cfg
;
1091 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_RUN
,
1092 initial_plugin_paths
, true);
1097 cfg
->cmd_data
.run
.sources
= g_ptr_array_new_with_free_func(
1098 (GDestroyNotify
) bt_object_put_ref
);
1099 if (!cfg
->cmd_data
.run
.sources
) {
1100 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1104 cfg
->cmd_data
.run
.filters
= g_ptr_array_new_with_free_func(
1105 (GDestroyNotify
) bt_object_put_ref
);
1106 if (!cfg
->cmd_data
.run
.filters
) {
1107 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1111 cfg
->cmd_data
.run
.sinks
= g_ptr_array_new_with_free_func(
1112 (GDestroyNotify
) bt_object_put_ref
);
1113 if (!cfg
->cmd_data
.run
.sinks
) {
1114 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1118 cfg
->cmd_data
.run
.connections
= g_ptr_array_new_with_free_func(
1119 (GDestroyNotify
) bt_config_connection_destroy
);
1120 if (!cfg
->cmd_data
.run
.connections
) {
1121 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1128 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1135 struct bt_config
*bt_config_list_plugins_create(
1136 const bt_value
*initial_plugin_paths
)
1138 struct bt_config
*cfg
;
1141 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS
,
1142 initial_plugin_paths
, true);
1150 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1157 struct bt_config
*bt_config_help_create(
1158 const bt_value
*initial_plugin_paths
,
1159 int default_log_level
)
1161 struct bt_config
*cfg
;
1164 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_HELP
,
1165 initial_plugin_paths
, true);
1170 cfg
->cmd_data
.help
.cfg_component
=
1171 bt_config_component_create(-1, NULL
, NULL
, default_log_level
);
1172 if (!cfg
->cmd_data
.help
.cfg_component
) {
1179 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1186 struct bt_config
*bt_config_query_create(
1187 const bt_value
*initial_plugin_paths
)
1189 struct bt_config
*cfg
;
1192 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_QUERY
,
1193 initial_plugin_paths
, true);
1198 cfg
->cmd_data
.query
.object
= g_string_new(NULL
);
1199 if (!cfg
->cmd_data
.query
.object
) {
1200 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1207 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1214 struct bt_config
*bt_config_print_ctf_metadata_create(
1215 const bt_value
*initial_plugin_paths
)
1217 struct bt_config
*cfg
;
1220 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_CTF_METADATA
,
1221 initial_plugin_paths
, true);
1226 cfg
->cmd_data
.print_ctf_metadata
.path
= g_string_new(NULL
);
1227 if (!cfg
->cmd_data
.print_ctf_metadata
.path
) {
1228 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1232 cfg
->cmd_data
.print_ctf_metadata
.output_path
= g_string_new(NULL
);
1233 if (!cfg
->cmd_data
.print_ctf_metadata
.output_path
) {
1234 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1241 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1248 struct bt_config
*bt_config_print_lttng_live_sessions_create(
1249 const bt_value
*initial_plugin_paths
)
1251 struct bt_config
*cfg
;
1254 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
,
1255 initial_plugin_paths
, true);
1260 cfg
->cmd_data
.print_lttng_live_sessions
.url
= g_string_new(NULL
);
1261 if (!cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
1262 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1266 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
=
1268 if (!cfg
->cmd_data
.print_lttng_live_sessions
.output_path
) {
1269 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1276 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1283 int bt_config_append_plugin_paths_check_setuid_setgid(
1284 bt_value
*plugin_paths
, const char *arg
)
1288 if (bt_common_is_setuid_setgid()) {
1289 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1293 if (bt_config_append_plugin_paths(plugin_paths
, arg
)) {
1294 BT_CLI_LOGE_APPEND_CAUSE("Invalid --plugin-path option's argument:\n %s",
1305 * Prints the expected format for a --params option.
1308 void print_expected_params_format(FILE *fp
)
1310 fprintf(fp
, "Expected format of PARAMS\n");
1311 fprintf(fp
, "-------------------------\n");
1313 fprintf(fp
, " PARAM=VALUE[,PARAM=VALUE]...\n");
1315 fprintf(fp
, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
1316 fprintf(fp
, "where PARAM is the parameter name (C identifier plus the [:.-] characters),\n");
1317 fprintf(fp
, "and VALUE can be one of:\n");
1319 fprintf(fp
, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
1320 fprintf(fp
, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
1321 fprintf(fp
, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
1322 fprintf(fp
, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
1323 fprintf(fp
, " (`0x` prefix) unsigned (with `+` prefix) or signed 64-bit integer.\n");
1324 fprintf(fp
, "* Double precision floating point number (scientific notation is accepted).\n");
1325 fprintf(fp
, "* Unquoted string with no special characters, and not matching any of\n");
1326 fprintf(fp
, " the null and boolean value symbols above.\n");
1327 fprintf(fp
, "* Double-quoted string (accepts escape characters).\n");
1328 fprintf(fp
, "* Array, formatted as an opening `[`, a list of comma-separated values\n");
1329 fprintf(fp
, " (as described by the current list) and a closing `]`.\n");
1331 fprintf(fp
, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1333 fprintf(fp
, "Example:\n");
1335 fprintf(fp
, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
1336 fprintf(fp
, " play=+23, observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
1337 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\",\n");
1338 fprintf(fp
, " things=[1, \"2\", 3]\n");
1340 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1341 fprintf(fp
, "babeltrace2 from a shell.\n");
1345 bool help_option_is_specified(
1346 const struct bt_argpar_parse_ret
*argpar_parse_ret
)
1349 bool specified
= false;
1351 for (i
= 0; i
< argpar_parse_ret
->items
->len
; i
++) {
1352 struct bt_argpar_item
*argpar_item
=
1353 g_ptr_array_index(argpar_parse_ret
->items
, i
);
1354 struct bt_argpar_item_opt
*argpar_item_opt
;
1356 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
1360 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
1361 if (argpar_item_opt
->descr
->id
== OPT_HELP
) {
1371 * Prints the help command usage.
1374 void print_help_usage(FILE *fp
)
1376 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1377 fprintf(fp
, " babeltrace2 [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
1379 fprintf(fp
, "Options:\n");
1381 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1382 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1383 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1384 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1385 fprintf(fp
, " dynamic plugins can be loaded\n");
1386 fprintf(fp
, " -h, --help Show this help and quit\n");
1388 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1390 fprintf(fp
, "Use `babeltrace2 list-plugins` to show the list of available plugins.\n");
1394 const struct bt_argpar_opt_descr help_options
[] = {
1395 /* id, short_name, long_name, with_arg */
1396 { OPT_HELP
, 'h', "help", false },
1397 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1398 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1399 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1400 BT_ARGPAR_OPT_DESCR_SENTINEL
1404 * Creates a Babeltrace config object from the arguments of a help
1407 * *retcode is set to the appropriate exit code to use.
1410 struct bt_config
*bt_config_help_from_args(int argc
, const char *argv
[],
1411 int *retcode
, bool force_omit_system_plugin_path
,
1412 bool force_omit_home_plugin_path
,
1413 const bt_value
*initial_plugin_paths
, int default_log_level
)
1416 struct bt_config
*cfg
= NULL
;
1417 const char *non_opt
= NULL
;
1418 char *plugin_name
= NULL
, *comp_cls_name
= NULL
;
1419 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1422 cfg
= bt_config_help_create(initial_plugin_paths
, default_log_level
);
1427 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1428 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1429 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1435 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, help_options
, true);
1436 if (argpar_parse_ret
.error
) {
1437 BT_CLI_LOGE_APPEND_CAUSE(
1438 "While parsing `help` command's command-line arguments: %s",
1439 argpar_parse_ret
.error
->str
);
1443 if (help_option_is_specified(&argpar_parse_ret
)) {
1444 print_help_usage(stdout
);
1446 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1450 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
1451 struct bt_argpar_item
*argpar_item
=
1452 g_ptr_array_index(argpar_parse_ret
.items
, i
);
1454 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
1455 struct bt_argpar_item_opt
*argpar_item_opt
;
1457 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
1458 arg
= argpar_item_opt
->arg
;
1460 switch (argpar_item_opt
->descr
->id
) {
1461 case OPT_PLUGIN_PATH
:
1462 if (bt_config_append_plugin_paths_check_setuid_setgid(
1463 cfg
->plugin_paths
, arg
)) {
1467 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1468 cfg
->omit_system_plugin_path
= true;
1470 case OPT_OMIT_HOME_PLUGIN_PATH
:
1471 cfg
->omit_home_plugin_path
= true;
1474 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1475 argpar_item_opt
->descr
->id
);
1479 struct bt_argpar_item_non_opt
*argpar_item_non_opt
1480 = (struct bt_argpar_item_non_opt
*) argpar_item
;
1483 BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `help` command: `%s`.",
1484 argpar_item_non_opt
->arg
);
1488 non_opt
= argpar_item_non_opt
->arg
;
1493 plugin_comp_cls_names(non_opt
, NULL
,
1494 &plugin_name
, &comp_cls_name
,
1495 &cfg
->cmd_data
.help
.cfg_component
->type
);
1496 if (plugin_name
&& comp_cls_name
) {
1497 /* Component class help */
1499 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1502 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
,
1505 /* Fall back to plugin help */
1507 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1511 print_help_usage(stdout
);
1513 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1517 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1525 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1528 g_free(plugin_name
);
1529 g_free(comp_cls_name
);
1531 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1537 * Prints the help command usage.
1540 void print_query_usage(FILE *fp
)
1542 fprintf(fp
, "Usage: babeltrace2 [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
1544 fprintf(fp
, "Options:\n");
1546 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1547 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1548 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1549 fprintf(fp
, " -p, --params=PARAMS Set the query parameters to PARAMS\n");
1550 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1551 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1552 fprintf(fp
, " dynamic plugins can be loaded\n");
1553 fprintf(fp
, " -h, --help Show this help and quit\n");
1554 fprintf(fp
, "\n\n");
1555 print_expected_params_format(fp
);
1559 const struct bt_argpar_opt_descr query_options
[] = {
1560 /* id, short_name, long_name, with_arg */
1561 { OPT_HELP
, 'h', "help", false },
1562 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1563 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1564 { OPT_PARAMS
, 'p', "params", true },
1565 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1566 BT_ARGPAR_OPT_DESCR_SENTINEL
1570 * Creates a Babeltrace config object from the arguments of a query
1573 * *retcode is set to the appropriate exit code to use.
1576 struct bt_config
*bt_config_query_from_args(int argc
, const char *argv
[],
1577 int *retcode
, bool force_omit_system_plugin_path
,
1578 bool force_omit_home_plugin_path
,
1579 const bt_value
*initial_plugin_paths
,
1580 int default_log_level
)
1583 struct bt_config
*cfg
= NULL
;
1584 const char *component_class_spec
= NULL
;
1585 const char *query_object
= NULL
;
1587 GString
*error_str
= NULL
;
1588 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1590 params
= bt_value_null
;
1591 bt_value_get_ref(bt_value_null
);
1594 cfg
= bt_config_query_create(initial_plugin_paths
);
1599 error_str
= g_string_new(NULL
);
1601 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1605 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1606 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1607 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1613 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, query_options
, true);
1614 if (argpar_parse_ret
.error
) {
1615 BT_CLI_LOGE_APPEND_CAUSE(
1616 "While parsing `query` command's command-line arguments: %s",
1617 argpar_parse_ret
.error
->str
);
1621 if (help_option_is_specified(&argpar_parse_ret
)) {
1622 print_query_usage(stdout
);
1624 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1628 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
1629 struct bt_argpar_item
*argpar_item
=
1630 g_ptr_array_index(argpar_parse_ret
.items
, i
);
1632 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
1633 struct bt_argpar_item_opt
*argpar_item_opt
=
1634 (struct bt_argpar_item_opt
*) argpar_item
;
1635 const char *arg
= argpar_item_opt
->arg
;
1637 switch (argpar_item_opt
->descr
->id
) {
1638 case OPT_PLUGIN_PATH
:
1639 if (bt_config_append_plugin_paths_check_setuid_setgid(
1640 cfg
->plugin_paths
, arg
)) {
1644 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1645 cfg
->omit_system_plugin_path
= true;
1647 case OPT_OMIT_HOME_PLUGIN_PATH
:
1648 cfg
->omit_home_plugin_path
= true;
1652 bt_value_put_ref(params
);
1653 params
= cli_value_from_arg(arg
, error_str
);
1655 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1662 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1663 argpar_item_opt
->descr
->id
);
1667 struct bt_argpar_item_non_opt
*argpar_item_non_opt
1668 = (struct bt_argpar_item_non_opt
*) argpar_item
;
1671 * We need exactly two non-option arguments
1672 * which are the mandatory component class
1673 * specification and query object.
1675 if (!component_class_spec
) {
1676 component_class_spec
= argpar_item_non_opt
->arg
;
1677 } else if (!query_object
) {
1678 query_object
= argpar_item_non_opt
->arg
;
1680 BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `query` command: `%s`.",
1681 argpar_item_non_opt
->arg
);
1687 if (!component_class_spec
|| !query_object
) {
1688 print_query_usage(stdout
);
1690 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1694 cfg
->cmd_data
.query
.cfg_component
=
1695 bt_config_component_from_arg(component_class_spec
,
1697 if (!cfg
->cmd_data
.query
.cfg_component
) {
1698 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for component class specification:\n %s",
1699 component_class_spec
);
1704 BT_OBJECT_MOVE_REF(cfg
->cmd_data
.query
.cfg_component
->params
, params
);
1706 if (strlen(query_object
) == 0) {
1707 BT_CLI_LOGE_APPEND_CAUSE("Invalid empty object.");
1711 g_string_assign(cfg
->cmd_data
.query
.object
, query_object
);
1713 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1721 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1724 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1727 g_string_free(error_str
, TRUE
);
1730 bt_value_put_ref(params
);
1735 * Prints the list-plugins command usage.
1738 void print_list_plugins_usage(FILE *fp
)
1740 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
1742 fprintf(fp
, "Options:\n");
1744 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1745 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1746 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1747 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1748 fprintf(fp
, " dynamic plugins can be loaded\n");
1749 fprintf(fp
, " -h, --help Show this help and quit\n");
1751 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1753 fprintf(fp
, "Use `babeltrace2 help` to get help for a specific plugin or component class.\n");
1757 const struct bt_argpar_opt_descr list_plugins_options
[] = {
1758 /* id, short_name, long_name, with_arg */
1759 { OPT_HELP
, 'h', "help", false },
1760 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1761 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1762 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1763 BT_ARGPAR_OPT_DESCR_SENTINEL
1767 * Creates a Babeltrace config object from the arguments of a
1768 * list-plugins command.
1770 * *retcode is set to the appropriate exit code to use.
1773 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
1774 int *retcode
, bool force_omit_system_plugin_path
,
1775 bool force_omit_home_plugin_path
,
1776 const bt_value
*initial_plugin_paths
)
1779 struct bt_config
*cfg
= NULL
;
1780 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1783 cfg
= bt_config_list_plugins_create(initial_plugin_paths
);
1788 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1789 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1790 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1796 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, list_plugins_options
, true);
1797 if (argpar_parse_ret
.error
) {
1798 BT_CLI_LOGE_APPEND_CAUSE(
1799 "While parsing `list-plugins` command's command-line arguments: %s",
1800 argpar_parse_ret
.error
->str
);
1804 if (help_option_is_specified(&argpar_parse_ret
)) {
1805 print_list_plugins_usage(stdout
);
1807 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1811 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
1812 struct bt_argpar_item
*argpar_item
=
1813 g_ptr_array_index(argpar_parse_ret
.items
, i
);
1814 struct bt_argpar_item_opt
*argpar_item_opt
;
1817 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
1818 struct bt_argpar_item_non_opt
*argpar_item_non_opt
1819 = (struct bt_argpar_item_non_opt
*) argpar_item
;
1821 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`.",
1822 argpar_item_non_opt
->arg
);
1826 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
1827 arg
= argpar_item_opt
->arg
;
1829 switch (argpar_item_opt
->descr
->id
) {
1830 case OPT_PLUGIN_PATH
:
1831 if (bt_config_append_plugin_paths_check_setuid_setgid(
1832 cfg
->plugin_paths
, arg
)) {
1836 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1837 cfg
->omit_system_plugin_path
= true;
1839 case OPT_OMIT_HOME_PLUGIN_PATH
:
1840 cfg
->omit_home_plugin_path
= true;
1843 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1844 argpar_item_opt
->descr
->id
);
1849 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1857 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1860 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1866 * Prints the run command usage.
1869 void print_run_usage(FILE *fp
)
1871 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n");
1873 fprintf(fp
, "Options:\n");
1875 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
1876 fprintf(fp
, " for all the following components until\n");
1877 fprintf(fp
, " --reset-base-params is encountered\n");
1878 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1879 fprintf(fp
, " -c, --component=NAME:TYPE.PLUGIN.CLS\n");
1880 fprintf(fp
, " Instantiate the component class CLS of type\n");
1881 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
1882 fprintf(fp
, " in the plugin PLUGIN, add it to the graph,\n");
1883 fprintf(fp
, " and name it NAME");
1884 fprintf(fp
, " -x, --connect=CONNECTION Connect two created components (see the\n");
1885 fprintf(fp
, " expected format of CONNECTION below)\n");
1886 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
1887 fprintf(fp
, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
1888 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1889 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1890 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1891 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
1892 fprintf(fp
, " current component (see the expected format\n");
1893 fprintf(fp
, " of PARAMS below)\n");
1894 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1895 fprintf(fp
, " dynamic plugins can be loaded\n");
1896 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters to an\n");
1897 fprintf(fp
, " empty map\n");
1898 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
1899 fprintf(fp
, " the graph later, retry in DUR µs\n");
1900 fprintf(fp
, " (default: 100000)\n");
1901 fprintf(fp
, " -h, --help Show this help and quit\n");
1903 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1904 fprintf(fp
, "\n\n");
1905 fprintf(fp
, "Expected format of CONNECTION\n");
1906 fprintf(fp
, "-----------------------------\n");
1908 fprintf(fp
, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
1910 fprintf(fp
, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
1911 fprintf(fp
, "components to connect together. You must escape the following characters\n\n");
1912 fprintf(fp
, "with `\\`: `\\`, `.`, and `:`. You must set the name of the current\n");
1913 fprintf(fp
, "component using the NAME prefix of the --component option.\n");
1915 fprintf(fp
, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
1916 fprintf(fp
, "identify the upstream and downstream ports to use for the connection.\n");
1917 fprintf(fp
, "When the port is not specified, `*` is used.\n");
1919 fprintf(fp
, "When a component named UPSTREAM has an available port which matches the\n");
1920 fprintf(fp
, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
1921 fprintf(fp
, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
1922 fprintf(fp
, "DOWNSTREAM.\n");
1924 fprintf(fp
, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
1925 fprintf(fp
, "which matches anything. You must escape the following characters\n");
1926 fprintf(fp
, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
1928 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
1929 fprintf(fp
, "can connect a filter component to a sink component.\n");
1931 fprintf(fp
, "Examples:\n");
1933 fprintf(fp
, " my-src:my-sink\n");
1934 fprintf(fp
, " ctf-fs.*stream*:utils-muxer:*\n");
1936 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1937 fprintf(fp
, "babeltrace2 from a shell.\n");
1938 fprintf(fp
, "\n\n");
1939 print_expected_params_format(fp
);
1943 * Creates a Babeltrace config object from the arguments of a run
1946 * *retcode is set to the appropriate exit code to use.
1949 struct bt_config
*bt_config_run_from_args(int argc
, const char *argv
[],
1950 int *retcode
, bool force_omit_system_plugin_path
,
1951 bool force_omit_home_plugin_path
,
1952 const bt_value
*initial_plugin_paths
, int default_log_level
)
1954 struct bt_config_component
*cur_cfg_comp
= NULL
;
1955 bt_value
*cur_base_params
= NULL
;
1957 struct bt_config
*cfg
= NULL
;
1958 bt_value
*instance_names
= NULL
;
1959 bt_value
*connection_args
= NULL
;
1960 char error_buf
[256] = { 0 };
1961 long retry_duration
= -1;
1962 bt_value_map_extend_status extend_status
;
1963 GString
*error_str
= NULL
;
1964 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1967 static const struct bt_argpar_opt_descr run_options
[] = {
1968 { OPT_BASE_PARAMS
, 'b', "base-params", true },
1969 { OPT_COMPONENT
, 'c', "component", true },
1970 { OPT_CONNECT
, 'x', "connect", true },
1971 { OPT_HELP
, 'h', "help", false },
1972 { OPT_LOG_LEVEL
, 'l', "log-level", true },
1973 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1974 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1975 { OPT_PARAMS
, 'p', "params", true },
1976 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1977 { OPT_RESET_BASE_PARAMS
, 'r', "reset-base-params", false },
1978 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
1979 BT_ARGPAR_OPT_DESCR_SENTINEL
1984 error_str
= g_string_new(NULL
);
1986 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1991 print_run_usage(stdout
);
1996 cfg
= bt_config_run_create(initial_plugin_paths
);
2001 cfg
->cmd_data
.run
.retry_duration_us
= 100000;
2002 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2003 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2004 cur_base_params
= bt_value_map_create();
2005 if (!cur_base_params
) {
2006 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2010 instance_names
= bt_value_map_create();
2011 if (!instance_names
) {
2012 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2016 connection_args
= bt_value_array_create();
2017 if (!connection_args
) {
2018 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2022 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2028 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, run_options
, true);
2029 if (argpar_parse_ret
.error
) {
2030 BT_CLI_LOGE_APPEND_CAUSE(
2031 "While parsing `run` command's command-line arguments: %s",
2032 argpar_parse_ret
.error
->str
);
2036 if (help_option_is_specified(&argpar_parse_ret
)) {
2037 print_run_usage(stdout
);
2039 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2043 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
2044 struct bt_argpar_item
*argpar_item
=
2045 g_ptr_array_index(argpar_parse_ret
.items
, i
);
2046 struct bt_argpar_item_opt
*argpar_item_opt
;
2049 /* This command does not accept non-option arguments.*/
2050 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
2051 struct bt_argpar_item_non_opt
*argpar_nonopt_item
=
2052 (struct bt_argpar_item_non_opt
*) argpar_item
;
2054 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`",
2055 argpar_nonopt_item
->arg
);
2059 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
2060 arg
= argpar_item_opt
->arg
;
2062 switch (argpar_item_opt
->descr
->id
) {
2063 case OPT_PLUGIN_PATH
:
2064 if (bt_config_append_plugin_paths_check_setuid_setgid(
2065 cfg
->plugin_paths
, arg
)) {
2069 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2070 cfg
->omit_system_plugin_path
= true;
2072 case OPT_OMIT_HOME_PLUGIN_PATH
:
2073 cfg
->omit_home_plugin_path
= true;
2077 enum bt_config_component_dest dest
;
2079 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2080 cur_cfg_comp
= bt_config_component_from_arg(arg
,
2082 if (!cur_cfg_comp
) {
2083 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --component option's argument:\n %s",
2088 switch (cur_cfg_comp
->type
) {
2089 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2090 dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
2092 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2093 dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
2095 case BT_COMPONENT_CLASS_TYPE_SINK
:
2096 dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
2102 BT_ASSERT(cur_base_params
);
2103 bt_value_put_ref(cur_cfg_comp
->params
);
2104 if (bt_value_copy(cur_base_params
,
2105 &cur_cfg_comp
->params
) < 0) {
2106 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2110 ret
= add_run_cfg_comp_check_name(cfg
,
2122 bt_value
*params_to_set
;
2124 if (!cur_cfg_comp
) {
2125 BT_CLI_LOGE_APPEND_CAUSE("Cannot add parameters to unavailable component:\n %s",
2130 params
= cli_value_from_arg(arg
, error_str
);
2132 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
2137 extend_status
= bt_value_map_extend(
2138 cur_cfg_comp
->params
, params
, ¶ms_to_set
);
2139 BT_VALUE_PUT_REF_AND_RESET(params
);
2140 if (extend_status
!= BT_VALUE_MAP_EXTEND_STATUS_OK
) {
2141 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current component parameters with --params option's argument:\n %s",
2146 BT_OBJECT_MOVE_REF(cur_cfg_comp
->params
, params_to_set
);
2150 if (!cur_cfg_comp
) {
2151 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the log level of unavailable component:\n %s",
2156 cur_cfg_comp
->log_level
=
2157 bt_log_get_level_from_string(arg
);
2158 if (cur_cfg_comp
->log_level
< 0) {
2159 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
2164 case OPT_BASE_PARAMS
:
2166 bt_value
*params
= cli_value_from_arg(arg
, error_str
);
2169 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --base-params option's argument:\n %s",
2174 BT_OBJECT_MOVE_REF(cur_base_params
, params
);
2177 case OPT_RESET_BASE_PARAMS
:
2178 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2179 cur_base_params
= bt_value_map_create();
2180 if (!cur_base_params
) {
2181 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2186 if (bt_value_array_append_string_element(
2187 connection_args
, arg
)) {
2188 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2192 case OPT_RETRY_DURATION
: {
2194 size_t arg_len
= strlen(argpar_item_opt
->arg
);
2196 retry_duration
= g_ascii_strtoll(argpar_item_opt
->arg
, &end
, 10);
2198 if (arg_len
== 0 || end
!= (argpar_item_opt
->arg
+ arg_len
)) {
2199 BT_CLI_LOGE_APPEND_CAUSE(
2200 "Could not parse --retry-duration option's argument as an unsigned integer: `%s`",
2201 argpar_item_opt
->arg
);
2205 if (retry_duration
< 0) {
2206 BT_CLI_LOGE_APPEND_CAUSE("--retry-duration option's argument must be positive or 0: %ld",
2211 cfg
->cmd_data
.run
.retry_duration_us
=
2212 (uint64_t) retry_duration
;
2216 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
2217 argpar_item_opt
->descr
->id
);
2222 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2224 if (cfg
->cmd_data
.run
.sources
->len
== 0) {
2225 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no source component.");
2229 if (cfg
->cmd_data
.run
.sinks
->len
== 0) {
2230 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no sink component.");
2234 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2238 ret
= bt_config_cli_args_create_connections(cfg
,
2242 BT_CLI_LOGE_APPEND_CAUSE("Cannot creation connections:\n%s", error_buf
);
2250 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2254 g_string_free(error_str
, TRUE
);
2257 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
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
);
2276 argv
= calloc(argc
, sizeof(*argv
));
2278 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2282 len
= bt_value_array_get_size(run_args
);
2284 BT_CLI_LOGE_APPEND_CAUSE("Invalid executable arguments.");
2287 for (i
= 0; i
< len
; i
++) {
2288 const bt_value
*arg_value
=
2289 bt_value_array_borrow_element_by_index_const(run_args
,
2293 BT_ASSERT(arg_value
);
2294 arg
= bt_value_string_get(arg_value
);
2299 cfg
= bt_config_run_from_args(argc
, argv
, retcode
,
2300 force_omit_system_plugin_path
, force_omit_home_plugin_path
,
2301 initial_plugin_paths
, default_log_level
);
2309 * Prints the convert command usage.
2312 void print_convert_usage(FILE *fp
)
2314 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2316 fprintf(fp
, "Options:\n");
2318 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2319 fprintf(fp
, " Instantiate the component class CLS of type\n");
2320 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2321 fprintf(fp
, " in the plugin PLUGIN, add it to the\n");
2322 fprintf(fp
, " conversion graph, and optionally name it\n");
2323 fprintf(fp
, " NAME\n");
2324 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
2325 fprintf(fp
, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
2326 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2327 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
2328 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2329 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2330 fprintf(fp
, " current component (see the expected format\n");
2331 fprintf(fp
, " of PARAMS below)\n");
2332 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2333 fprintf(fp
, " dynamic plugins can be loaded\n");
2334 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
2335 fprintf(fp
, " the graph later, retry in DUR µs\n");
2336 fprintf(fp
, " (default: 100000)\n");
2337 fprintf(fp
, " dynamic plugins can be loaded\n");
2338 fprintf(fp
, " --run-args Print the equivalent arguments for the\n");
2339 fprintf(fp
, " `run` command to the standard output,\n");
2340 fprintf(fp
, " formatted for a shell, and quit\n");
2341 fprintf(fp
, " --run-args-0 Print the equivalent arguments for the\n");
2342 fprintf(fp
, " `run` command to the standard output,\n");
2343 fprintf(fp
, " formatted for `xargs -0`, and quit\n");
2344 fprintf(fp
, " --stream-intersection Only process events when all streams\n");
2345 fprintf(fp
, " are active\n");
2346 fprintf(fp
, " -h, --help Show this help and quit\n");
2348 fprintf(fp
, "Implicit `source.ctf.fs` component options:\n");
2350 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2351 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2353 fprintf(fp
, "Implicit `sink.text.pretty` component options:\n");
2355 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
2356 fprintf(fp
, " --clock-date Print timestamp dates\n");
2357 fprintf(fp
, " --clock-gmt Print and parse timestamps in the GMT\n");
2358 fprintf(fp
, " time zone instead of the local time zone\n");
2359 fprintf(fp
, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2360 fprintf(fp
, " of `hh:mm:ss.nnnnnnnnn`\n");
2361 fprintf(fp
, " --color=(never | auto | always)\n");
2362 fprintf(fp
, " Never, automatically, or always emit\n");
2363 fprintf(fp
, " console color codes\n");
2364 fprintf(fp
, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2365 fprintf(fp
, " `all`, `trace`, `trace:hostname`,\n");
2366 fprintf(fp
, " `trace:domain`, `trace:procname`,\n");
2367 fprintf(fp
, " `trace:vpid`, `loglevel`, `emf`\n");
2368 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2369 fprintf(fp
, " `payload` (or `arg` or `args`), `none`,\n");
2370 fprintf(fp
, " `all`, `scope`, `header`, `context`\n");
2371 fprintf(fp
, " (or `ctx`)\n");
2372 fprintf(fp
, " --no-delta Do not print time delta between\n");
2373 fprintf(fp
, " consecutive events\n");
2374 fprintf(fp
, " -w, --output=PATH Write output text to PATH instead of\n");
2375 fprintf(fp
, " the standard output\n");
2377 fprintf(fp
, "Implicit `filter.utils.muxer` component options:\n");
2379 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently\n");
2380 fprintf(fp
, " correlated across traces\n");
2382 fprintf(fp
, "Implicit `filter.utils.trimmer` component options:\n");
2384 fprintf(fp
, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2385 fprintf(fp
, " time range to BEGIN (see the format of\n");
2386 fprintf(fp
, " BEGIN below)\n");
2387 fprintf(fp
, " -e, --end=END Set the end time of the conversion time\n");
2388 fprintf(fp
, " range to END (see the format of END below)\n");
2389 fprintf(fp
, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2390 fprintf(fp
, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2391 fprintf(fp
, " `]`) (see the format of BEGIN/END below)\n");
2393 fprintf(fp
, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2395 fprintf(fp
, " --debug-info Create an implicit\n");
2396 fprintf(fp
, " `filter.lttng-utils.debug-info` component\n");
2397 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2398 fprintf(fp
, " instead of `/usr/lib/debug`\n");
2399 fprintf(fp
, " --debug-info-full-path Show full debug info source and\n");
2400 fprintf(fp
, " binary paths instead of just names\n");
2401 fprintf(fp
, " --debug-info-target-prefix=DIR\n");
2402 fprintf(fp
, " Use directory DIR as a prefix when\n");
2403 fprintf(fp
, " looking up executables during debug\n");
2404 fprintf(fp
, " info analysis\n");
2406 fprintf(fp
, "Legacy options that still work:\n");
2408 fprintf(fp
, " -i, --input-format=(ctf | lttng-live)\n");
2409 fprintf(fp
, " `ctf`:\n");
2410 fprintf(fp
, " Create an implicit `source.ctf.fs`\n");
2411 fprintf(fp
, " component\n");
2412 fprintf(fp
, " `lttng-live`:\n");
2413 fprintf(fp
, " Create an implicit `source.ctf.lttng-live`\n");
2414 fprintf(fp
, " component\n");
2415 fprintf(fp
, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2416 fprintf(fp
, " `text`:\n");
2417 fprintf(fp
, " Create an implicit `sink.text.pretty`\n");
2418 fprintf(fp
, " component\n");
2419 fprintf(fp
, " `ctf`:\n");
2420 fprintf(fp
, " Create an implicit `sink.ctf.fs`\n");
2421 fprintf(fp
, " component\n");
2422 fprintf(fp
, " `dummy`:\n");
2423 fprintf(fp
, " Create an implicit `sink.utils.dummy`\n");
2424 fprintf(fp
, " component\n");
2425 fprintf(fp
, " `ctf-metadata`:\n");
2426 fprintf(fp
, " Query the `source.ctf.fs` component class\n");
2427 fprintf(fp
, " for metadata text and quit\n");
2429 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
2430 fprintf(fp
, "\n\n");
2431 fprintf(fp
, "Format of BEGIN and END\n");
2432 fprintf(fp
, "-----------------------\n");
2434 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2435 fprintf(fp
, "\n\n");
2436 print_expected_params_format(fp
);
2440 const struct bt_argpar_opt_descr convert_options
[] = {
2441 /* id, short_name, long_name, with_arg */
2442 { OPT_BEGIN
, 'b', "begin", true },
2443 { OPT_CLOCK_CYCLES
, '\0', "clock-cycles", false },
2444 { OPT_CLOCK_DATE
, '\0', "clock-date", false },
2445 { OPT_CLOCK_FORCE_CORRELATE
, '\0', "clock-force-correlate", false },
2446 { OPT_CLOCK_GMT
, '\0', "clock-gmt", false },
2447 { OPT_CLOCK_OFFSET
, '\0', "clock-offset", true },
2448 { OPT_CLOCK_OFFSET_NS
, '\0', "clock-offset-ns", true },
2449 { OPT_CLOCK_SECONDS
, '\0', "clock-seconds", false },
2450 { OPT_COLOR
, '\0', "color", true },
2451 { OPT_COMPONENT
, 'c', "component", true },
2452 { OPT_DEBUG
, 'd', "debug", false },
2453 { OPT_DEBUG_INFO_DIR
, '\0', "debug-info-dir", true },
2454 { OPT_DEBUG_INFO_FULL_PATH
, '\0', "debug-info-full-path", false },
2455 { OPT_DEBUG_INFO_TARGET_PREFIX
, '\0', "debug-info-target-prefix", true },
2456 { OPT_END
, 'e', "end", true },
2457 { OPT_FIELDS
, 'f', "fields", true },
2458 { OPT_HELP
, 'h', "help", false },
2459 { OPT_INPUT_FORMAT
, 'i', "input-format", true },
2460 { OPT_LOG_LEVEL
, 'l', "log-level", true },
2461 { OPT_NAMES
, 'n', "names", true },
2462 { OPT_DEBUG_INFO
, '\0', "debug-info", false },
2463 { OPT_NO_DELTA
, '\0', "no-delta", false },
2464 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
2465 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
2466 { OPT_OUTPUT
, 'w', "output", true },
2467 { OPT_OUTPUT_FORMAT
, 'o', "output-format", true },
2468 { OPT_PARAMS
, 'p', "params", true },
2469 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
2470 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
2471 { OPT_RUN_ARGS
, '\0', "run-args", false },
2472 { OPT_RUN_ARGS_0
, '\0', "run-args-0", false },
2473 { OPT_STREAM_INTERSECTION
, '\0', "stream-intersection", false },
2474 { OPT_TIMERANGE
, '\0', "timerange", true },
2475 { OPT_VERBOSE
, 'v', "verbose", false },
2476 BT_ARGPAR_OPT_DESCR_SENTINEL
2480 GString
*get_component_auto_name(const char *prefix
,
2481 const bt_value
*existing_names
)
2484 GString
*auto_name
= g_string_new(NULL
);
2487 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2491 if (!bt_value_map_has_entry(existing_names
, prefix
)) {
2492 g_string_assign(auto_name
, prefix
);
2497 g_string_printf(auto_name
, "%s-%d", prefix
, i
);
2499 } while (bt_value_map_has_entry(existing_names
, auto_name
->str
));
2505 struct implicit_component_args
{
2508 /* The component class name (e.g. src.ctf.fs). */
2511 /* The component instance name. */
2514 GString
*params_arg
;
2515 bt_value
*extra_params
;
2519 int assign_name_to_implicit_component(struct implicit_component_args
*args
,
2520 const char *prefix
, bt_value
*existing_names
,
2521 GList
**comp_names
, bool append_to_comp_names
)
2524 GString
*name
= NULL
;
2526 if (!args
->exists
) {
2530 name
= get_component_auto_name(prefix
,
2538 g_string_assign(args
->name_arg
, name
->str
);
2540 if (bt_value_map_insert_entry(existing_names
, name
->str
,
2542 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2547 if (append_to_comp_names
) {
2548 *comp_names
= g_list_append(*comp_names
, name
);
2554 g_string_free(name
, TRUE
);
2561 int append_run_args_for_implicit_component(
2562 struct implicit_component_args
*impl_args
,
2567 GString
*component_arg_for_run
= NULL
;
2569 if (!impl_args
->exists
) {
2573 component_arg_for_run
= g_string_new(NULL
);
2574 if (!component_arg_for_run
) {
2575 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2579 /* Build the full `name:type.plugin.cls`. */
2580 BT_ASSERT(!strchr(impl_args
->name_arg
->str
, '\\'));
2581 BT_ASSERT(!strchr(impl_args
->name_arg
->str
, ':'));
2582 g_string_printf(component_arg_for_run
, "%s:%s",
2583 impl_args
->name_arg
->str
, impl_args
->comp_arg
->str
);
2585 if (bt_value_array_append_string_element(run_args
, "--component")) {
2586 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2590 if (bt_value_array_append_string_element(run_args
,
2591 component_arg_for_run
->str
)) {
2592 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2596 if (impl_args
->params_arg
->len
> 0) {
2597 if (bt_value_array_append_string_element(run_args
, "--params")) {
2598 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2602 if (bt_value_array_append_string_element(run_args
,
2603 impl_args
->params_arg
->str
)) {
2604 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2609 for (i
= 0; i
< bt_value_array_get_size(impl_args
->extra_params
);
2611 const bt_value
*elem
;
2614 elem
= bt_value_array_borrow_element_by_index(impl_args
->extra_params
,
2620 BT_ASSERT(bt_value_is_string(elem
));
2621 arg
= bt_value_string_get(elem
);
2622 ret
= bt_value_array_append_string_element(run_args
, arg
);
2624 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2635 if (component_arg_for_run
) {
2636 g_string_free(component_arg_for_run
, TRUE
);
2642 /* Free the fields of a `struct implicit_component_args`. */
2645 void finalize_implicit_component_args(struct implicit_component_args
*args
)
2649 if (args
->comp_arg
) {
2650 g_string_free(args
->comp_arg
, TRUE
);
2653 if (args
->name_arg
) {
2654 g_string_free(args
->name_arg
, TRUE
);
2657 if (args
->params_arg
) {
2658 g_string_free(args
->params_arg
, TRUE
);
2661 bt_value_put_ref(args
->extra_params
);
2664 /* Destroy a dynamically-allocated `struct implicit_component_args`. */
2667 void destroy_implicit_component_args(struct implicit_component_args
*args
)
2669 finalize_implicit_component_args(args
);
2673 /* Initialize the fields of an already allocated `struct implicit_component_args`. */
2676 int init_implicit_component_args(struct implicit_component_args
*args
,
2677 const char *comp_arg
, bool exists
)
2681 args
->exists
= exists
;
2682 args
->comp_arg
= g_string_new(comp_arg
);
2683 args
->name_arg
= g_string_new(NULL
);
2684 args
->params_arg
= g_string_new(NULL
);
2685 args
->extra_params
= bt_value_array_create();
2687 if (!args
->comp_arg
|| !args
->name_arg
||
2688 !args
->params_arg
|| !args
->extra_params
) {
2690 finalize_implicit_component_args(args
);
2691 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2699 /* Dynamically allocate and initialize a `struct implicit_component_args`. */
2702 struct implicit_component_args
*create_implicit_component_args(
2703 const char *comp_arg
)
2705 struct implicit_component_args
*args
;
2708 args
= g_new(struct implicit_component_args
, 1);
2710 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2714 status
= init_implicit_component_args(args
, comp_arg
, true);
2725 void append_implicit_component_param(struct implicit_component_args
*args
,
2726 const char *key
, const char *value
)
2731 append_param_arg(args
->params_arg
, key
, value
);
2735 * Append the given parameter (`key=value`) to all component specifications
2736 * in `implicit_comp_args` (an array of `struct implicit_component_args *`)
2737 * which match `comp_arg`.
2739 * Return the number of matching components.
2743 int append_multiple_implicit_components_param(GPtrArray
*implicit_comp_args
,
2744 const char *comp_arg
, const char *key
, const char *value
)
2749 for (i
= 0; i
< implicit_comp_args
->len
; i
++) {
2750 struct implicit_component_args
*args
= implicit_comp_args
->pdata
[i
];
2752 if (strcmp(args
->comp_arg
->str
, comp_arg
) == 0) {
2753 append_implicit_component_param(args
, key
, value
);
2761 /* Escape value to make it suitable to use as a string parameter value. */
2763 gchar
*escape_string_value(const char *value
)
2768 ret
= g_string_new(NULL
);
2770 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2779 g_string_append_c(ret
, '\\');
2783 g_string_append_c(ret
, *in
);
2789 return g_string_free(ret
, FALSE
);
2793 int bt_value_to_cli_param_value_append(const bt_value
*value
, GString
*buf
)
2799 switch (bt_value_get_type(value
)) {
2800 case BT_VALUE_TYPE_STRING
:
2802 const char *str_value
= bt_value_string_get(value
);
2803 gchar
*escaped_str_value
;
2805 escaped_str_value
= escape_string_value(str_value
);
2806 if (!escaped_str_value
) {
2810 g_string_append_printf(buf
, "\"%s\"", escaped_str_value
);
2812 g_free(escaped_str_value
);
2815 case BT_VALUE_TYPE_ARRAY
: {
2816 g_string_append_c(buf
, '[');
2817 uint64_t sz
= bt_value_array_get_size(value
);
2818 for (uint64_t i
= 0; i
< sz
; i
++) {
2819 const bt_value
*item
;
2823 g_string_append(buf
, ", ");
2826 item
= bt_value_array_borrow_element_by_index_const(
2828 ret
= bt_value_to_cli_param_value_append(item
, buf
);
2834 g_string_append_c(buf
, ']');
2848 * Convert `value` to its equivalent representation as a command line parameter
2853 gchar
*bt_value_to_cli_param_value(bt_value
*value
)
2856 gchar
*result
= NULL
;
2858 buf
= g_string_new(NULL
);
2860 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2864 if (bt_value_to_cli_param_value_append(value
, buf
)) {
2868 result
= g_string_free(buf
, FALSE
);
2875 g_string_free(buf
, TRUE
);
2883 int append_parameter_to_args(bt_value
*args
, const char *key
, bt_value
*value
)
2886 BT_ASSERT(bt_value_get_type(args
) == BT_VALUE_TYPE_ARRAY
);
2891 gchar
*str_value
= NULL
;
2892 GString
*parameter
= NULL
;
2894 if (bt_value_array_append_string_element(args
, "--params")) {
2895 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2900 str_value
= bt_value_to_cli_param_value(value
);
2906 parameter
= g_string_new(NULL
);
2908 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2913 g_string_printf(parameter
, "%s=%s", key
, str_value
);
2915 if (bt_value_array_append_string_element(args
, parameter
->str
)) {
2916 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2923 g_string_free(parameter
, TRUE
);
2936 int append_string_parameter_to_args(bt_value
*args
, const char *key
, const char *value
)
2938 bt_value
*str_value
;
2941 str_value
= bt_value_string_create_init(value
);
2944 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2949 ret
= append_parameter_to_args(args
, key
, str_value
);
2952 BT_VALUE_PUT_REF_AND_RESET(str_value
);
2957 int append_implicit_component_extra_param(struct implicit_component_args
*args
,
2958 const char *key
, const char *value
)
2960 return append_string_parameter_to_args(args
->extra_params
, key
, value
);
2964 * Escapes `.`, `:`, and `\` of `input` with `\`.
2967 GString
*escape_dot_colon(const char *input
)
2969 GString
*output
= g_string_new(NULL
);
2973 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2977 for (ch
= input
; *ch
!= '\0'; ch
++) {
2978 if (*ch
== '\\' || *ch
== '.' || *ch
== ':') {
2979 g_string_append_c(output
, '\\');
2982 g_string_append_c(output
, *ch
);
2990 * Appends a --connect option to a list of arguments. `upstream_name`
2991 * and `downstream_name` are escaped with escape_dot_colon() in this
2995 int append_connect_arg(bt_value
*run_args
,
2996 const char *upstream_name
, const char *downstream_name
)
2999 GString
*e_upstream_name
= escape_dot_colon(upstream_name
);
3000 GString
*e_downstream_name
= escape_dot_colon(downstream_name
);
3001 GString
*arg
= g_string_new(NULL
);
3003 if (!e_upstream_name
|| !e_downstream_name
|| !arg
) {
3004 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3009 ret
= bt_value_array_append_string_element(run_args
, "--connect");
3011 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3016 g_string_append(arg
, e_upstream_name
->str
);
3017 g_string_append_c(arg
, ':');
3018 g_string_append(arg
, e_downstream_name
->str
);
3019 ret
= bt_value_array_append_string_element(run_args
, arg
->str
);
3021 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3028 g_string_free(arg
, TRUE
);
3031 if (e_upstream_name
) {
3032 g_string_free(e_upstream_name
, TRUE
);
3035 if (e_downstream_name
) {
3036 g_string_free(e_downstream_name
, TRUE
);
3043 * Appends the run command's --connect options for the convert command.
3046 int convert_auto_connect(bt_value
*run_args
,
3047 GList
*source_names
, GList
*filter_names
,
3051 GList
*source_at
= source_names
;
3052 GList
*filter_at
= filter_names
;
3054 GList
*sink_at
= sink_names
;
3056 BT_ASSERT(source_names
);
3057 BT_ASSERT(filter_names
);
3058 BT_ASSERT(sink_names
);
3060 /* Connect all sources to the first filter */
3061 for (source_at
= source_names
; source_at
; source_at
= g_list_next(source_at
)) {
3062 GString
*source_name
= source_at
->data
;
3063 GString
*filter_name
= filter_at
->data
;
3065 ret
= append_connect_arg(run_args
, source_name
->str
,
3072 filter_prev
= filter_at
;
3073 filter_at
= g_list_next(filter_at
);
3075 /* Connect remaining filters */
3076 for (; filter_at
; filter_prev
= filter_at
, filter_at
= g_list_next(filter_at
)) {
3077 GString
*filter_name
= filter_at
->data
;
3078 GString
*filter_prev_name
= filter_prev
->data
;
3080 ret
= append_connect_arg(run_args
, filter_prev_name
->str
,
3087 /* Connect last filter to all sinks */
3088 for (sink_at
= sink_names
; sink_at
; sink_at
= g_list_next(sink_at
)) {
3089 GString
*filter_name
= filter_prev
->data
;
3090 GString
*sink_name
= sink_at
->data
;
3092 ret
= append_connect_arg(run_args
, filter_name
->str
,
3109 int split_timerange(const char *arg
, char **begin
, char **end
)
3112 const char *ch
= arg
;
3114 GString
*g_begin
= NULL
;
3115 GString
*g_end
= NULL
;
3123 g_begin
= bt_common_string_until(ch
, "", ",", &end_pos
);
3124 if (!g_begin
|| ch
[end_pos
] != ',' || g_begin
->len
== 0) {
3130 g_end
= bt_common_string_until(ch
, "", "]", &end_pos
);
3131 if (!g_end
|| g_end
->len
== 0) {
3137 *begin
= g_begin
->str
;
3139 g_string_free(g_begin
, FALSE
);
3140 g_string_free(g_end
, FALSE
);
3150 g_string_free(g_begin
, TRUE
);
3154 g_string_free(g_end
, TRUE
);
3161 int g_list_prepend_gstring(GList
**list
, const char *string
)
3164 GString
*gs
= g_string_new(string
);
3169 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3173 *list
= g_list_prepend(*list
, gs
);
3180 * Create `struct implicit_component_args` structures for each of the
3181 * source components we identified. Add them to `component_args`.
3183 * `non_opt_params` is an array where each element is an array of
3184 * strings containing all the arguments to `--params` that apply to the
3185 * non-option argument at the same index. For example, if, for a
3186 * non-option argument, the following `--params` options applied:
3188 * --params=a=2 --params=b=3,c=4
3190 * its entry in `non_opt_params` would contain
3192 * ["a=2", "b=3,c=4"]
3196 int create_implicit_component_args_from_auto_discovered_sources(
3197 const struct auto_source_discovery
*auto_disc
,
3198 const bt_value
*non_opt_params
,
3199 const bt_value
*non_opt_loglevels
,
3200 GPtrArray
*component_args
)
3202 gchar
*cc_name
= NULL
;
3203 struct implicit_component_args
*comp
= NULL
;
3207 len
= auto_disc
->results
->len
;
3209 for (i
= 0; i
< len
; i
++) {
3210 struct auto_source_discovery_result
*res
=
3211 g_ptr_array_index(auto_disc
->results
, i
);
3212 uint64_t orig_indices_i
, orig_indices_count
;
3215 cc_name
= g_strdup_printf("source.%s.%s", res
->plugin_name
, res
->source_cc_name
);
3217 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3221 comp
= create_implicit_component_args(cc_name
);
3227 * Append parameters and log levels of all the
3228 * non-option arguments that contributed to this
3229 * component instance coming into existence.
3231 orig_indices_count
= bt_value_array_get_size(res
->original_input_indices
);
3232 for (orig_indices_i
= 0; orig_indices_i
< orig_indices_count
; orig_indices_i
++) {
3233 const bt_value
*orig_idx_value
=
3234 bt_value_array_borrow_element_by_index(
3235 res
->original_input_indices
, orig_indices_i
);
3236 uint64_t orig_idx
= bt_value_integer_unsigned_get(orig_idx_value
);
3237 const bt_value
*params_array
=
3238 bt_value_array_borrow_element_by_index_const(
3239 non_opt_params
, orig_idx
);
3240 uint64_t params_i
, params_count
;
3241 const bt_value
*loglevel_value
;
3243 params_count
= bt_value_array_get_size(params_array
);
3244 for (params_i
= 0; params_i
< params_count
; params_i
++) {
3245 const bt_value
*params_value
=
3246 bt_value_array_borrow_element_by_index_const(
3247 params_array
, params_i
);
3248 const char *params
= bt_value_string_get(params_value
);
3249 bt_value_array_append_element_status append_status
;
3251 append_status
= bt_value_array_append_string_element(
3252 comp
->extra_params
, "--params");
3253 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3254 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3258 append_status
= bt_value_array_append_string_element(
3259 comp
->extra_params
, params
);
3260 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3261 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3266 loglevel_value
= bt_value_array_borrow_element_by_index_const(
3267 non_opt_loglevels
, orig_idx
);
3268 if (bt_value_get_type(loglevel_value
) == BT_VALUE_TYPE_STRING
) {
3269 const char *loglevel
= bt_value_string_get(loglevel_value
);
3270 bt_value_array_append_element_status append_status
;
3272 append_status
= bt_value_array_append_string_element(
3273 comp
->extra_params
, "--log-level");
3274 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3275 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3279 append_status
= bt_value_array_append_string_element(
3280 comp
->extra_params
, loglevel
);
3281 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3282 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3288 status
= append_parameter_to_args(comp
->extra_params
, "inputs", res
->inputs
);
3293 g_ptr_array_add(component_args
, comp
);
3307 destroy_implicit_component_args(comp
);
3314 * As we iterate the arguments to the convert command, this tracks what is the
3315 * type of the current item, to which some contextual options (e.g. --params)
3318 enum convert_current_item_type
{
3319 /* There is no current item. */
3320 CONVERT_CURRENT_ITEM_TYPE_NONE
,
3322 /* Current item is a component. */
3323 CONVERT_CURRENT_ITEM_TYPE_COMPONENT
,
3325 /* Current item is a non-option argument. */
3326 CONVERT_CURRENT_ITEM_TYPE_NON_OPT
,
3330 * Creates a Babeltrace config object from the arguments of a convert
3333 * *retcode is set to the appropriate exit code to use.
3336 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3337 int *retcode
, bool force_omit_system_plugin_path
,
3338 bool force_omit_home_plugin_path
,
3339 const bt_value
*initial_plugin_paths
, int *default_log_level
)
3341 enum convert_current_item_type current_item_type
=
3342 CONVERT_CURRENT_ITEM_TYPE_NONE
;
3344 struct bt_config
*cfg
= NULL
;
3345 bool got_input_format_opt
= false;
3346 bool got_output_format_opt
= false;
3347 bool trimmer_has_begin
= false;
3348 bool trimmer_has_end
= false;
3349 bool stream_intersection_mode
= false;
3350 bool print_run_args
= false;
3351 bool print_run_args_0
= false;
3352 bool print_ctf_metadata
= false;
3353 bt_value
*run_args
= NULL
;
3354 bt_value
*all_names
= NULL
;
3355 GList
*source_names
= NULL
;
3356 GList
*filter_names
= NULL
;
3357 GList
*sink_names
= NULL
;
3358 bt_value
*non_opts
= NULL
;
3359 bt_value
*non_opt_params
= NULL
;
3360 bt_value
*non_opt_loglevels
= NULL
;
3361 struct implicit_component_args implicit_ctf_output_args
= { 0 };
3362 struct implicit_component_args implicit_lttng_live_args
= { 0 };
3363 struct implicit_component_args implicit_dummy_args
= { 0 };
3364 struct implicit_component_args implicit_text_args
= { 0 };
3365 struct implicit_component_args implicit_debug_info_args
= { 0 };
3366 struct implicit_component_args implicit_muxer_args
= { 0 };
3367 struct implicit_component_args implicit_trimmer_args
= { 0 };
3368 bt_value
*plugin_paths
;
3369 char error_buf
[256] = { 0 };
3371 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
3372 char *output
= NULL
;
3373 struct auto_source_discovery auto_disc
= { NULL
};
3374 GString
*auto_disc_comp_name
= NULL
;
3375 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
3376 GString
*name_gstr
= NULL
;
3377 GString
*component_arg_for_run
= NULL
;
3380 * Array of `struct implicit_component_args *` created for the sources
3381 * we have auto-discovered.
3383 GPtrArray
*discovered_source_args
= NULL
;
3386 * If set, restrict automatic source discovery to this component class
3389 const char *auto_source_discovery_restrict_plugin_name
= NULL
;
3390 const char *auto_source_discovery_restrict_component_class_name
= NULL
;
3392 gchar
*ctf_fs_source_clock_class_offset_arg
= NULL
;
3393 gchar
*ctf_fs_source_clock_class_offset_ns_arg
= NULL
;
3395 (void) bt_value_copy(initial_plugin_paths
, &plugin_paths
);
3400 print_convert_usage(stdout
);
3405 if (init_implicit_component_args(&implicit_ctf_output_args
,
3406 "sink.ctf.fs", false)) {
3410 if (init_implicit_component_args(&implicit_lttng_live_args
,
3411 "source.ctf.lttng-live", false)) {
3415 if (init_implicit_component_args(&implicit_text_args
,
3416 "sink.text.pretty", false)) {
3420 if (init_implicit_component_args(&implicit_dummy_args
,
3421 "sink.utils.dummy", false)) {
3425 if (init_implicit_component_args(&implicit_debug_info_args
,
3426 "filter.lttng-utils.debug-info", false)) {
3430 if (init_implicit_component_args(&implicit_muxer_args
,
3431 "filter.utils.muxer", true)) {
3435 if (init_implicit_component_args(&implicit_trimmer_args
,
3436 "filter.utils.trimmer", false)) {
3440 all_names
= bt_value_map_create();
3442 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3446 run_args
= bt_value_array_create();
3448 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3452 component_arg_for_run
= g_string_new(NULL
);
3453 if (!component_arg_for_run
) {
3454 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3458 ret
= append_env_var_plugin_paths(plugin_paths
);
3463 non_opts
= bt_value_array_create();
3465 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3469 non_opt_params
= bt_value_array_create();
3470 if (!non_opt_params
) {
3471 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3475 non_opt_loglevels
= bt_value_array_create();
3476 if (!non_opt_loglevels
) {
3477 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3481 if (auto_source_discovery_init(&auto_disc
) != 0) {
3485 discovered_source_args
=
3486 g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_implicit_component_args
);
3487 if (!discovered_source_args
) {
3488 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3492 auto_disc_comp_name
= g_string_new(NULL
);
3493 if (!auto_disc_comp_name
) {
3494 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3499 * First pass: collect all arguments which need to be passed
3500 * as is to the run command. This pass can also add --name
3501 * arguments if needed to automatically name unnamed component
3504 * Also it appends the plugin paths of --plugin-path to
3507 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, convert_options
, true);
3508 if (argpar_parse_ret
.error
) {
3509 BT_CLI_LOGE_APPEND_CAUSE(
3510 "While parsing `convert` command's command-line arguments: %s",
3511 argpar_parse_ret
.error
->str
);
3515 if (help_option_is_specified(&argpar_parse_ret
)) {
3516 print_convert_usage(stdout
);
3518 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
3522 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
3523 struct bt_argpar_item
*argpar_item
=
3524 g_ptr_array_index(argpar_parse_ret
.items
, i
);
3525 struct bt_argpar_item_opt
*argpar_item_opt
;
3527 char *plugin_name
= NULL
;
3528 char *comp_cls_name
= NULL
;
3531 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
3532 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
3533 arg
= argpar_item_opt
->arg
;
3535 switch (argpar_item_opt
->descr
->id
) {
3538 bt_component_class_type type
;
3540 current_item_type
= CONVERT_CURRENT_ITEM_TYPE_COMPONENT
;
3542 /* Parse the argument */
3543 plugin_comp_cls_names(arg
, &name
, &plugin_name
,
3544 &comp_cls_name
, &type
);
3545 if (!plugin_name
|| !comp_cls_name
) {
3546 BT_CLI_LOGE_APPEND_CAUSE(
3547 "Invalid format for --component option's argument:\n %s",
3554 * Name was given by the user, verify it isn't
3557 if (bt_value_map_has_entry(all_names
, name
)) {
3558 BT_CLI_LOGE_APPEND_CAUSE(
3559 "Duplicate component instance name:\n %s",
3564 name_gstr
= g_string_new(name
);
3566 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3570 g_string_assign(component_arg_for_run
, arg
);
3572 /* Name not given by user, generate one. */
3573 name_gstr
= get_component_auto_name(arg
, all_names
);
3578 g_string_printf(component_arg_for_run
, "%s:%s",
3579 name_gstr
->str
, arg
);
3582 if (bt_value_array_append_string_element(run_args
,
3584 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3588 if (bt_value_array_append_string_element(run_args
,
3589 component_arg_for_run
->str
)) {
3590 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3595 * Remember this name globally, for the uniqueness of
3596 * all component names.
3598 if (bt_value_map_insert_entry(all_names
,
3599 name_gstr
->str
, bt_value_null
)) {
3600 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3605 * Remember this name specifically for the type of the
3606 * component. This is to create connection arguments.
3608 * The list takes ownership of `name_gstr`.
3611 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
3612 source_names
= g_list_append(source_names
, name_gstr
);
3614 case BT_COMPONENT_CLASS_TYPE_FILTER
:
3615 filter_names
= g_list_append(filter_names
, name_gstr
);
3617 case BT_COMPONENT_CLASS_TYPE_SINK
:
3618 sink_names
= g_list_append(sink_names
, name_gstr
);
3627 free(comp_cls_name
);
3630 comp_cls_name
= NULL
;
3634 if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_COMPONENT
) {
3636 * The current item is a component (--component option),
3637 * pass it directly to the run args.
3639 if (bt_value_array_append_string_element(run_args
,
3641 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3645 if (bt_value_array_append_string_element(run_args
, arg
)) {
3646 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3649 } else if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_NON_OPT
) {
3651 * The current item is a
3652 * non-option argument, record
3653 * it in `non_opt_params`.
3656 uint64_t idx
= bt_value_array_get_size(non_opt_params
) - 1;
3658 array
= bt_value_array_borrow_element_by_index(non_opt_params
, idx
);
3659 bt_value_array_append_string_element(array
, arg
);
3661 BT_CLI_LOGE_APPEND_CAUSE(
3662 "No current component (--component option) or non-option argument of which to set parameters:\n %s",
3668 if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_COMPONENT
) {
3669 if (bt_value_array_append_string_element(run_args
, "--log-level")) {
3670 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3674 if (bt_value_array_append_string_element(run_args
, arg
)) {
3675 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3678 } else if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_NON_OPT
) {
3679 uint64_t idx
= bt_value_array_get_size(non_opt_loglevels
) - 1;
3680 bt_value
*log_level_str_value
;
3682 log_level_str_value
= bt_value_string_create_init(arg
);
3683 if (!log_level_str_value
) {
3684 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3688 if (bt_value_array_set_element_by_index(non_opt_loglevels
, idx
,
3689 log_level_str_value
)) {
3690 bt_value_put_ref(log_level_str_value
);
3691 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3695 BT_CLI_LOGE_APPEND_CAUSE(
3696 "No current component (--component option) or non-option argument to assign a log level to:\n %s",
3702 case OPT_OMIT_HOME_PLUGIN_PATH
:
3703 force_omit_home_plugin_path
= true;
3705 if (bt_value_array_append_string_element(run_args
,
3706 "--omit-home-plugin-path")) {
3707 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3711 case OPT_RETRY_DURATION
:
3712 if (bt_value_array_append_string_element(run_args
,
3713 "--retry-duration")) {
3714 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3718 if (bt_value_array_append_string_element(run_args
, arg
)) {
3719 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3723 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
3724 force_omit_system_plugin_path
= true;
3726 if (bt_value_array_append_string_element(run_args
,
3727 "--omit-system-plugin-path")) {
3728 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3732 case OPT_PLUGIN_PATH
:
3733 if (bt_config_append_plugin_paths_check_setuid_setgid(
3734 plugin_paths
, arg
)) {
3738 if (bt_value_array_append_string_element(run_args
,
3740 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3744 if (bt_value_array_append_string_element(run_args
, arg
)) {
3745 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3750 case OPT_CLOCK_CYCLES
:
3751 case OPT_CLOCK_DATE
:
3752 case OPT_CLOCK_FORCE_CORRELATE
:
3754 case OPT_CLOCK_OFFSET
:
3755 case OPT_CLOCK_OFFSET_NS
:
3756 case OPT_CLOCK_SECONDS
:
3759 case OPT_DEBUG_INFO
:
3760 case OPT_DEBUG_INFO_DIR
:
3761 case OPT_DEBUG_INFO_FULL_PATH
:
3762 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3765 case OPT_INPUT_FORMAT
:
3768 case OPT_OUTPUT_FORMAT
:
3771 case OPT_RUN_ARGS_0
:
3772 case OPT_STREAM_INTERSECTION
:
3775 /* Ignore in this pass */
3778 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
3779 argpar_item_opt
->descr
->id
);
3782 } else if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
3783 struct bt_argpar_item_non_opt
*argpar_item_non_opt
;
3784 bt_value_array_append_element_status append_status
;
3786 current_item_type
= CONVERT_CURRENT_ITEM_TYPE_NON_OPT
;
3788 argpar_item_non_opt
= (struct bt_argpar_item_non_opt
*) argpar_item
;
3790 append_status
= bt_value_array_append_string_element(non_opts
,
3791 argpar_item_non_opt
->arg
);
3792 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3793 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3797 append_status
= bt_value_array_append_empty_array_element(non_opt_params
);
3798 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3799 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3803 append_status
= bt_value_array_append_element(non_opt_loglevels
, bt_value_null
);
3804 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3805 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3814 * Second pass: transform the convert-specific options and
3815 * arguments into implicit component instances for the run
3818 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
3819 struct bt_argpar_item
*argpar_item
=
3820 g_ptr_array_index(argpar_parse_ret
.items
, i
);
3821 struct bt_argpar_item_opt
*argpar_item_opt
;
3824 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
3828 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
3829 arg
= argpar_item_opt
->arg
;
3831 switch (argpar_item_opt
->descr
->id
) {
3833 if (trimmer_has_begin
) {
3834 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
3839 trimmer_has_begin
= true;
3840 ret
= append_implicit_component_extra_param(
3841 &implicit_trimmer_args
, "begin", arg
);
3842 implicit_trimmer_args
.exists
= true;
3848 if (trimmer_has_end
) {
3849 printf("At --end option: --end or --timerange option already specified\n %s\n",
3854 trimmer_has_end
= true;
3855 ret
= append_implicit_component_extra_param(
3856 &implicit_trimmer_args
, "end", arg
);
3857 implicit_trimmer_args
.exists
= true;
3867 if (trimmer_has_begin
|| trimmer_has_end
) {
3868 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
3873 ret
= split_timerange(arg
, &begin
, &end
);
3875 BT_CLI_LOGE_APPEND_CAUSE("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s",
3880 ret
= append_implicit_component_extra_param(
3881 &implicit_trimmer_args
, "begin", begin
);
3882 ret
|= append_implicit_component_extra_param(
3883 &implicit_trimmer_args
, "end", end
);
3884 implicit_trimmer_args
.exists
= true;
3892 case OPT_CLOCK_CYCLES
:
3893 append_implicit_component_param(
3894 &implicit_text_args
, "clock-cycles", "yes");
3895 implicit_text_args
.exists
= true;
3897 case OPT_CLOCK_DATE
:
3898 append_implicit_component_param(
3899 &implicit_text_args
, "clock-date", "yes");
3900 implicit_text_args
.exists
= true;
3902 case OPT_CLOCK_FORCE_CORRELATE
:
3903 append_implicit_component_param(
3904 &implicit_muxer_args
,
3905 "assume-absolute-clock-classes", "yes");
3908 append_implicit_component_param(
3909 &implicit_text_args
, "clock-gmt", "yes");
3910 append_implicit_component_param(
3911 &implicit_trimmer_args
, "gmt", "yes");
3912 implicit_text_args
.exists
= true;
3914 case OPT_CLOCK_OFFSET
:
3915 if (ctf_fs_source_clock_class_offset_arg
) {
3916 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset option\n");
3920 ctf_fs_source_clock_class_offset_arg
= g_strdup(arg
);
3921 if (!ctf_fs_source_clock_class_offset_arg
) {
3922 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3926 case OPT_CLOCK_OFFSET_NS
:
3927 if (ctf_fs_source_clock_class_offset_ns_arg
) {
3928 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset-ns option\n");
3932 ctf_fs_source_clock_class_offset_ns_arg
= g_strdup(arg
);
3933 if (!ctf_fs_source_clock_class_offset_ns_arg
) {
3934 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3938 case OPT_CLOCK_SECONDS
:
3939 append_implicit_component_param(
3940 &implicit_text_args
, "clock-seconds", "yes");
3941 implicit_text_args
.exists
= true;
3944 implicit_text_args
.exists
= true;
3945 ret
= append_implicit_component_extra_param(
3946 &implicit_text_args
, "color", arg
);
3951 case OPT_DEBUG_INFO
:
3952 implicit_debug_info_args
.exists
= true;
3954 case OPT_DEBUG_INFO_DIR
:
3955 implicit_debug_info_args
.exists
= true;
3956 ret
= append_implicit_component_extra_param(
3957 &implicit_debug_info_args
, "debug-info-dir", arg
);
3962 case OPT_DEBUG_INFO_FULL_PATH
:
3963 implicit_debug_info_args
.exists
= true;
3964 append_implicit_component_param(
3965 &implicit_debug_info_args
, "full-path", "yes");
3967 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3968 implicit_debug_info_args
.exists
= true;
3969 ret
= append_implicit_component_extra_param(
3970 &implicit_debug_info_args
,
3971 "target-prefix", arg
);
3978 bt_value
*fields
= fields_from_arg(arg
);
3984 implicit_text_args
.exists
= true;
3985 ret
= insert_flat_params_from_array(
3986 implicit_text_args
.params_arg
,
3988 bt_value_put_ref(fields
);
3996 bt_value
*names
= names_from_arg(arg
);
4002 implicit_text_args
.exists
= true;
4003 ret
= insert_flat_params_from_array(
4004 implicit_text_args
.params_arg
,
4006 bt_value_put_ref(names
);
4013 append_implicit_component_param(
4014 &implicit_text_args
, "no-delta", "yes");
4015 implicit_text_args
.exists
= true;
4017 case OPT_INPUT_FORMAT
:
4018 if (got_input_format_opt
) {
4019 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --input-format option.");
4023 got_input_format_opt
= true;
4025 if (strcmp(arg
, "ctf") == 0) {
4026 auto_source_discovery_restrict_plugin_name
= "ctf";
4027 auto_source_discovery_restrict_component_class_name
= "fs";
4028 } else if (strcmp(arg
, "lttng-live") == 0) {
4029 auto_source_discovery_restrict_plugin_name
= "ctf";
4030 auto_source_discovery_restrict_component_class_name
= "lttng-live";
4031 implicit_lttng_live_args
.exists
= true;
4033 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy input format:\n %s",
4038 case OPT_OUTPUT_FORMAT
:
4039 if (got_output_format_opt
) {
4040 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output-format option.");
4044 got_output_format_opt
= true;
4046 if (strcmp(arg
, "text") == 0) {
4047 implicit_text_args
.exists
= true;
4048 } else if (strcmp(arg
, "ctf") == 0) {
4049 implicit_ctf_output_args
.exists
= true;
4050 } else if (strcmp(arg
, "dummy") == 0) {
4051 implicit_dummy_args
.exists
= true;
4052 } else if (strcmp(arg
, "ctf-metadata") == 0) {
4053 print_ctf_metadata
= true;
4055 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy output format:\n %s",
4062 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output option");
4066 output
= strdup(arg
);
4068 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4073 if (print_run_args_0
) {
4074 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
4078 print_run_args
= true;
4080 case OPT_RUN_ARGS_0
:
4081 if (print_run_args
) {
4082 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
4086 print_run_args_0
= true;
4088 case OPT_STREAM_INTERSECTION
:
4090 * Applies to all traces implementing the
4091 * babeltrace.trace-info query.
4093 stream_intersection_mode
= true;
4096 if (*default_log_level
!= BT_LOG_TRACE
&&
4097 *default_log_level
!= BT_LOG_DEBUG
) {
4098 *default_log_level
= BT_LOG_INFO
;
4102 *default_log_level
= BT_LOG_TRACE
;
4110 * Legacy behaviour: --verbose used to make the `text` output
4111 * format print more information. --verbose is now equivalent to
4112 * the INFO log level, which is why we compare to `BT_LOG_INFO`
4115 if (*default_log_level
== BT_LOG_INFO
) {
4116 append_implicit_component_param(&implicit_text_args
,
4121 * Append home and system plugin paths now that we possibly got
4124 if (append_home_and_system_plugin_paths(plugin_paths
,
4125 force_omit_system_plugin_path
,
4126 force_omit_home_plugin_path
)) {
4130 /* Print CTF metadata or print LTTng live sessions */
4131 if (print_ctf_metadata
) {
4132 const bt_value
*bt_val_non_opt
;
4134 if (bt_value_array_is_empty(non_opts
)) {
4135 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf-metadata specified without a path.");
4139 if (bt_value_array_get_size(non_opts
) > 1) {
4140 BT_CLI_LOGE_APPEND_CAUSE("Too many paths specified for --output-format=ctf-metadata.");
4144 cfg
= bt_config_print_ctf_metadata_create(plugin_paths
);
4149 bt_val_non_opt
= bt_value_array_borrow_element_by_index_const(non_opts
, 0);
4150 g_string_assign(cfg
->cmd_data
.print_ctf_metadata
.path
,
4151 bt_value_string_get(bt_val_non_opt
));
4155 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
4163 * If -o ctf was specified, make sure an output path (--output)
4164 * was also specified. --output does not imply -o ctf because
4165 * it's also used for the default, implicit -o text if -o ctf
4168 if (implicit_ctf_output_args
.exists
) {
4170 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf specified without --output (trace output path).");
4175 * At this point we know that -o ctf AND --output were
4176 * specified. Make sure that no options were specified
4177 * which would imply -o text because --output would be
4178 * ambiguous in this case. For example, this is wrong:
4180 * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace
4182 * because --names=all implies -o text, and --output
4183 * could apply to both the sink.text.pretty and
4184 * sink.ctf.fs implicit components.
4186 if (implicit_text_args
.exists
) {
4187 BT_CLI_LOGE_APPEND_CAUSE("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text.");
4193 * If -o dummy and -o ctf were not specified, and if there are
4194 * no explicit sink components, then use an implicit
4195 * `sink.text.pretty` component.
4197 if (!implicit_dummy_args
.exists
&& !implicit_ctf_output_args
.exists
&&
4199 implicit_text_args
.exists
= true;
4203 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4204 * `path` parameter if --output was specified.
4207 if (implicit_text_args
.exists
) {
4208 append_implicit_component_extra_param(&implicit_text_args
,
4210 } else if (implicit_ctf_output_args
.exists
) {
4211 append_implicit_component_extra_param(&implicit_ctf_output_args
,
4216 /* Decide where the non-option argument(s) go */
4217 if (bt_value_array_get_size(non_opts
) > 0) {
4218 if (implicit_lttng_live_args
.exists
) {
4219 const bt_value
*bt_val_non_opt
;
4221 if (bt_value_array_get_size(non_opts
) > 1) {
4222 BT_CLI_LOGE_APPEND_CAUSE("Too many URLs specified for --input-format=lttng-live.");
4226 bt_val_non_opt
= bt_value_array_borrow_element_by_index_const(non_opts
, 0);
4227 lttng_live_url_parts
=
4228 bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_non_opt
),
4229 error_buf
, sizeof(error_buf
));
4230 if (!lttng_live_url_parts
.proto
) {
4231 BT_CLI_LOGE_APPEND_CAUSE("Invalid LTTng live URL format: %s.",
4236 if (!lttng_live_url_parts
.session_name
) {
4237 /* Print LTTng live sessions */
4238 cfg
= bt_config_print_lttng_live_sessions_create(
4244 g_string_assign(cfg
->cmd_data
.print_lttng_live_sessions
.url
,
4245 bt_value_string_get(bt_val_non_opt
));
4249 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
4256 ret
= append_implicit_component_extra_param(
4257 &implicit_lttng_live_args
, "url",
4258 bt_value_string_get(bt_val_non_opt
));
4263 ret
= append_implicit_component_extra_param(
4264 &implicit_lttng_live_args
,
4265 "session-not-found-action", "end");
4271 size_t plugin_count
;
4272 const bt_plugin
**plugins
;
4273 const bt_plugin
*plugin
;
4275 status
= require_loaded_plugins(plugin_paths
);
4280 if (auto_source_discovery_restrict_plugin_name
) {
4282 plugin
= find_loaded_plugin(auto_source_discovery_restrict_plugin_name
);
4285 plugin_count
= get_loaded_plugins_count();
4286 plugins
= borrow_loaded_plugins();
4289 status
= auto_discover_source_components(non_opts
, plugins
, plugin_count
,
4290 auto_source_discovery_restrict_component_class_name
,
4291 *default_log_level
>= 0 ? *default_log_level
: cli_default_log_level
,
4298 status
= create_implicit_component_args_from_auto_discovered_sources(
4299 &auto_disc
, non_opt_params
, non_opt_loglevels
,
4300 discovered_source_args
);
4307 /* If --clock-offset was given, apply it to any src.ctf.fs component. */
4308 if (ctf_fs_source_clock_class_offset_arg
) {
4311 n
= append_multiple_implicit_components_param(
4312 discovered_source_args
, "source.ctf.fs", "clock-class-offset-s",
4313 ctf_fs_source_clock_class_offset_arg
);
4316 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset specified, but no source.ctf.fs component instantiated.");
4321 /* If --clock-offset-ns was given, apply it to any src.ctf.fs component. */
4322 if (ctf_fs_source_clock_class_offset_ns_arg
) {
4325 n
= append_multiple_implicit_components_param(
4326 discovered_source_args
, "source.ctf.fs", "clock-class-offset-ns",
4327 ctf_fs_source_clock_class_offset_ns_arg
);
4330 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset-ns specified, but no source.ctf.fs component instantiated.");
4336 * If the implicit `source.ctf.lttng-live` component exists,
4337 * make sure there's at least one non-option argument (which is
4340 if (implicit_lttng_live_args
.exists
&& bt_value_array_is_empty(non_opts
)) {
4341 BT_CLI_LOGE_APPEND_CAUSE("Missing URL for implicit `%s` component.",
4342 implicit_lttng_live_args
.comp_arg
->str
);
4346 /* Assign names to implicit components */
4347 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4348 struct implicit_component_args
*args
;
4351 args
= discovered_source_args
->pdata
[i
];
4353 g_string_printf(auto_disc_comp_name
, "auto-disc-%s", args
->comp_arg
->str
);
4355 /* Give it a name like `auto-disc-src-ctf-fs`. */
4356 for (j
= 0; j
< auto_disc_comp_name
->len
; j
++) {
4357 if (auto_disc_comp_name
->str
[j
] == '.') {
4358 auto_disc_comp_name
->str
[j
] = '-';
4362 ret
= assign_name_to_implicit_component(args
,
4363 auto_disc_comp_name
->str
, all_names
, &source_names
, true);
4369 ret
= assign_name_to_implicit_component(&implicit_lttng_live_args
,
4370 "lttng-live", all_names
, &source_names
, true);
4375 ret
= assign_name_to_implicit_component(&implicit_text_args
,
4376 "pretty", all_names
, &sink_names
, true);
4381 ret
= assign_name_to_implicit_component(&implicit_ctf_output_args
,
4382 "sink-ctf-fs", all_names
, &sink_names
, true);
4387 ret
= assign_name_to_implicit_component(&implicit_dummy_args
,
4388 "dummy", all_names
, &sink_names
, true);
4393 ret
= assign_name_to_implicit_component(&implicit_muxer_args
,
4394 "muxer", all_names
, NULL
, false);
4399 ret
= assign_name_to_implicit_component(&implicit_trimmer_args
,
4400 "trimmer", all_names
, NULL
, false);
4405 ret
= assign_name_to_implicit_component(&implicit_debug_info_args
,
4406 "debug-info", all_names
, NULL
, false);
4411 /* Make sure there's at least one source and one sink */
4412 if (!source_names
) {
4413 BT_CLI_LOGE_APPEND_CAUSE("No source component.");
4418 BT_CLI_LOGE_APPEND_CAUSE("No sink component.");
4423 * Prepend the muxer, the trimmer, and the debug info to the
4424 * filter chain so that we have:
4426 * sources -> muxer -> [trimmer] -> [debug info] ->
4427 * [user filters] -> sinks
4429 if (implicit_debug_info_args
.exists
) {
4430 if (g_list_prepend_gstring(&filter_names
,
4431 implicit_debug_info_args
.name_arg
->str
)) {
4436 if (implicit_trimmer_args
.exists
) {
4437 if (g_list_prepend_gstring(&filter_names
,
4438 implicit_trimmer_args
.name_arg
->str
)) {
4443 if (g_list_prepend_gstring(&filter_names
,
4444 implicit_muxer_args
.name_arg
->str
)) {
4449 * Append the equivalent run arguments for the implicit
4452 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4453 struct implicit_component_args
*args
=
4454 discovered_source_args
->pdata
[i
];
4456 ret
= append_run_args_for_implicit_component(args
, run_args
);
4462 ret
= append_run_args_for_implicit_component(&implicit_lttng_live_args
,
4468 ret
= append_run_args_for_implicit_component(&implicit_text_args
,
4474 ret
= append_run_args_for_implicit_component(&implicit_ctf_output_args
,
4480 ret
= append_run_args_for_implicit_component(&implicit_dummy_args
,
4486 ret
= append_run_args_for_implicit_component(&implicit_muxer_args
,
4492 ret
= append_run_args_for_implicit_component(&implicit_trimmer_args
,
4498 ret
= append_run_args_for_implicit_component(&implicit_debug_info_args
,
4504 /* Auto-connect components */
4505 ret
= convert_auto_connect(run_args
, source_names
, filter_names
,
4508 BT_CLI_LOGE_APPEND_CAUSE("Cannot auto-connect components.");
4513 * We have all the run command arguments now. Depending on
4514 * --run-args, we pass this to the run command or print them
4517 if (print_run_args
|| print_run_args_0
) {
4518 if (stream_intersection_mode
) {
4519 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --stream-intersection with --run-args or --run-args-0.");
4523 for (i
= 0; i
< bt_value_array_get_size(run_args
); i
++) {
4524 const bt_value
*arg_value
=
4525 bt_value_array_borrow_element_by_index(run_args
,
4528 GString
*quoted
= NULL
;
4529 const char *arg_to_print
;
4531 BT_ASSERT(arg_value
);
4532 arg
= bt_value_string_get(arg_value
);
4534 if (print_run_args
) {
4535 quoted
= bt_common_shell_quote(arg
, true);
4540 arg_to_print
= quoted
->str
;
4545 printf("%s", arg_to_print
);
4548 g_string_free(quoted
, TRUE
);
4551 if (i
< bt_value_array_get_size(run_args
) - 1) {
4552 if (print_run_args
) {
4561 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4566 * If the log level is still unset at this point, set it to
4567 * the program's default.
4569 if (*default_log_level
< 0) {
4570 *default_log_level
= cli_default_log_level
;
4573 cfg
= bt_config_run_from_args_array(run_args
, retcode
,
4574 force_omit_system_plugin_path
,
4575 force_omit_home_plugin_path
,
4576 initial_plugin_paths
, *default_log_level
);
4581 cfg
->cmd_data
.run
.stream_intersection_mode
= stream_intersection_mode
;
4586 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4590 * If the log level is still unset at this point, set it to
4591 * the program's default.
4593 if (*default_log_level
< 0) {
4594 *default_log_level
= cli_default_log_level
;
4597 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
4601 if (component_arg_for_run
) {
4602 g_string_free(component_arg_for_run
, TRUE
);
4606 g_string_free(name_gstr
, TRUE
);
4609 bt_value_put_ref(run_args
);
4610 bt_value_put_ref(all_names
);
4611 destroy_glist_of_gstring(source_names
);
4612 destroy_glist_of_gstring(filter_names
);
4613 destroy_glist_of_gstring(sink_names
);
4614 bt_value_put_ref(non_opt_params
);
4615 bt_value_put_ref(non_opt_loglevels
);
4616 bt_value_put_ref(non_opts
);
4617 finalize_implicit_component_args(&implicit_ctf_output_args
);
4618 finalize_implicit_component_args(&implicit_lttng_live_args
);
4619 finalize_implicit_component_args(&implicit_dummy_args
);
4620 finalize_implicit_component_args(&implicit_text_args
);
4621 finalize_implicit_component_args(&implicit_debug_info_args
);
4622 finalize_implicit_component_args(&implicit_muxer_args
);
4623 finalize_implicit_component_args(&implicit_trimmer_args
);
4624 bt_value_put_ref(plugin_paths
);
4625 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
4626 auto_source_discovery_fini(&auto_disc
);
4628 if (discovered_source_args
) {
4629 g_ptr_array_free(discovered_source_args
, TRUE
);
4632 g_free(ctf_fs_source_clock_class_offset_arg
);
4633 g_free(ctf_fs_source_clock_class_offset_ns_arg
);
4635 if (auto_disc_comp_name
) {
4636 g_string_free(auto_disc_comp_name
, TRUE
);
4643 * Prints the Babeltrace 2.x general usage.
4646 void print_gen_usage(FILE *fp
)
4648 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4650 fprintf(fp
, "General options:\n");
4652 fprintf(fp
, " -d, --debug Enable debug mode (same as --log-level=V)\n");
4653 fprintf(fp
, " -h, --help Show this help and quit\n");
4654 fprintf(fp
, " -l, --log-level=LVL Set the default log level to LVL (`N`, `V`, `D`,\n");
4655 fprintf(fp
, " `I`, `W` (default), `E`, or `F`)\n");
4656 fprintf(fp
, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4657 fprintf(fp
, " -V, --version Show version and quit\n");
4659 fprintf(fp
, "Available commands:\n");
4661 fprintf(fp
, " convert Convert and trim traces (default)\n");
4662 fprintf(fp
, " help Get help for a plugin or a component class\n");
4663 fprintf(fp
, " list-plugins List available plugins and their content\n");
4664 fprintf(fp
, " query Query objects from a component class\n");
4665 fprintf(fp
, " run Build a processing graph and run it\n");
4667 fprintf(fp
, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n");
4670 struct bt_config
*bt_config_cli_args_create(int argc
, const char *argv
[],
4671 int *retcode
, bool force_omit_system_plugin_path
,
4672 bool force_omit_home_plugin_path
,
4673 const bt_value
*initial_plugin_paths
)
4675 struct bt_config
*config
= NULL
;
4678 const char **top_level_argv
;
4679 int command_argc
= -1;
4680 const char **command_argv
= NULL
;
4681 const char *command_name
= NULL
;
4682 int default_log_level
= -1;
4683 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
4685 /* Top-level option descriptions. */
4686 static const struct bt_argpar_opt_descr descrs
[] = {
4687 { OPT_DEBUG
, 'd', "debug", false },
4688 { OPT_HELP
, 'h', "help", false },
4689 { OPT_LOG_LEVEL
, 'l', "log-level", true },
4690 { OPT_VERBOSE
, 'v', "verbose", false },
4691 { OPT_VERSION
, 'V', "version", false},
4692 BT_ARGPAR_OPT_DESCR_SENTINEL
4696 COMMAND_TYPE_NONE
= -1,
4697 COMMAND_TYPE_RUN
= 0,
4698 COMMAND_TYPE_CONVERT
,
4699 COMMAND_TYPE_LIST_PLUGINS
,
4702 } command_type
= COMMAND_TYPE_NONE
;
4706 if (!initial_plugin_paths
) {
4707 initial_plugin_paths
= bt_value_array_create();
4708 if (!initial_plugin_paths
) {
4713 bt_value_get_ref(initial_plugin_paths
);
4719 print_gen_usage(stdout
);
4723 /* Skip first argument, the name of the program. */
4724 top_level_argc
= argc
- 1;
4725 top_level_argv
= argv
+ 1;
4726 argpar_parse_ret
= bt_argpar_parse(top_level_argc
, top_level_argv
,
4729 if (argpar_parse_ret
.error
) {
4730 BT_CLI_LOGE_APPEND_CAUSE(
4731 "While parsing command-line arguments: %s",
4732 argpar_parse_ret
.error
->str
);
4736 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
4737 struct bt_argpar_item
*item
;
4739 item
= g_ptr_array_index(argpar_parse_ret
.items
, i
);
4741 if (item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
4742 struct bt_argpar_item_opt
*item_opt
=
4743 (struct bt_argpar_item_opt
*) item
;
4745 switch (item_opt
->descr
->id
) {
4747 default_log_level
= BT_LOG_TRACE
;
4751 * Legacy: do not override a previous
4752 * --debug because --verbose and --debug
4753 * can be specified together (in this
4754 * case we want the lowest log level to
4757 default_log_level
= BT_LOG_INFO
;
4761 bt_log_get_level_from_string(item_opt
->arg
);
4762 if (default_log_level
< 0) {
4763 BT_CLI_LOGE_APPEND_CAUSE(
4764 "Invalid argument for --log-level option:\n %s",
4773 print_gen_usage(stdout
);
4776 } else if (item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
4777 struct bt_argpar_item_non_opt
*item_non_opt
=
4778 (struct bt_argpar_item_non_opt
*) item
;
4780 * First unknown argument: is it a known command
4784 top_level_argc
- item_non_opt
->orig_index
- 1;
4786 &top_level_argv
[item_non_opt
->orig_index
+ 1];
4788 if (strcmp(item_non_opt
->arg
, "convert") == 0) {
4789 command_type
= COMMAND_TYPE_CONVERT
;
4790 } else if (strcmp(item_non_opt
->arg
, "list-plugins") == 0) {
4791 command_type
= COMMAND_TYPE_LIST_PLUGINS
;
4792 } else if (strcmp(item_non_opt
->arg
, "help") == 0) {
4793 command_type
= COMMAND_TYPE_HELP
;
4794 } else if (strcmp(item_non_opt
->arg
, "query") == 0) {
4795 command_type
= COMMAND_TYPE_QUERY
;
4796 } else if (strcmp(item_non_opt
->arg
, "run") == 0) {
4797 command_type
= COMMAND_TYPE_RUN
;
4800 * Non-option argument, but not a known
4801 * command name: assume the default
4802 * `convert` command.
4804 command_type
= COMMAND_TYPE_CONVERT
;
4805 command_name
= "convert";
4813 if (command_type
== COMMAND_TYPE_NONE
) {
4814 if (argpar_parse_ret
.ingested_orig_args
== top_level_argc
) {
4816 * We only got non-help, non-version general options
4817 * like --verbose and --debug, without any other
4818 * arguments, so we can't do anything useful: print the
4821 print_gen_usage(stdout
);
4826 * We stopped on an unknown option argument (and therefore
4827 * didn't see a command name). Assume `convert` command.
4829 command_type
= COMMAND_TYPE_CONVERT
;
4830 command_name
= "convert";
4832 top_level_argc
- argpar_parse_ret
.ingested_orig_args
;
4834 &top_level_argv
[argpar_parse_ret
.ingested_orig_args
];
4837 BT_ASSERT(command_argv
);
4838 BT_ASSERT(command_argc
>= 0);
4841 * The convert command can set its own default log level for
4842 * backward compatibility reasons. It only does so if there's no
4843 * log level yet, so do not force one for this command.
4845 if (command_type
!= COMMAND_TYPE_CONVERT
&& default_log_level
< 0) {
4846 /* Default log level */
4847 default_log_level
= cli_default_log_level
;
4850 switch (command_type
) {
4851 case COMMAND_TYPE_RUN
:
4852 config
= bt_config_run_from_args(command_argc
, command_argv
,
4853 retcode
, force_omit_system_plugin_path
,
4854 force_omit_home_plugin_path
, initial_plugin_paths
,
4857 case COMMAND_TYPE_CONVERT
:
4858 config
= bt_config_convert_from_args(command_argc
, command_argv
,
4859 retcode
, force_omit_system_plugin_path
,
4860 force_omit_home_plugin_path
,
4861 initial_plugin_paths
, &default_log_level
);
4863 case COMMAND_TYPE_LIST_PLUGINS
:
4864 config
= bt_config_list_plugins_from_args(command_argc
,
4865 command_argv
, retcode
, force_omit_system_plugin_path
,
4866 force_omit_home_plugin_path
, initial_plugin_paths
);
4868 case COMMAND_TYPE_HELP
:
4869 config
= bt_config_help_from_args(command_argc
,
4870 command_argv
, retcode
, force_omit_system_plugin_path
,
4871 force_omit_home_plugin_path
, initial_plugin_paths
,
4874 case COMMAND_TYPE_QUERY
:
4875 config
= bt_config_query_from_args(command_argc
,
4876 command_argv
, retcode
, force_omit_system_plugin_path
,
4877 force_omit_home_plugin_path
, initial_plugin_paths
,
4885 BT_ASSERT(default_log_level
>= BT_LOG_TRACE
);
4886 config
->log_level
= default_log_level
;
4887 config
->command_name
= command_name
;
4896 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
4897 bt_value_put_ref(initial_plugin_paths
);