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-cfg-src-auto-disc.h"
46 #include "common/version.h"
48 static const int cli_default_log_level
= BT_LOG_WARNING
;
50 /* INI-style parsing FSM states */
51 enum ini_parsing_fsm_state
{
52 /* Expect a map key (identifier) */
55 /* Expect an equal character ('=') */
61 /* Expect a comma character (',') */
65 /* INI-style parsing state variables */
66 struct ini_parsing_state
{
67 /* Lexical scanner (owned by this) */
70 /* Output map value object being filled (owned by this) */
73 /* Next expected FSM state */
74 enum ini_parsing_fsm_state expecting
;
76 /* Last decoded map key (owned by this) */
79 /* Complete INI-style string to parse (not owned by this) */
82 /* Error buffer (not owned by this) */
86 /* Offset option with "is set" boolean */
92 /* Legacy "ctf"/"lttng-live" format options */
93 struct ctf_legacy_opts
{
94 struct offset_opt offset_s
;
95 struct offset_opt offset_ns
;
96 bool stream_intersection
;
99 /* Legacy "text" format options */
100 struct text_legacy_opts
{
102 * output, dbg_info_dir, dbg_info_target_prefix, names,
103 * and fields are owned by this.
106 GString
*dbg_info_dir
;
107 GString
*dbg_info_target_prefix
;
108 const bt_value
*names
;
109 const bt_value
*fields
;
117 bool dbg_info_full_path
;
121 /* Legacy input format format */
122 enum legacy_input_format
{
123 LEGACY_INPUT_FORMAT_NONE
= 0,
124 LEGACY_INPUT_FORMAT_CTF
,
125 LEGACY_INPUT_FORMAT_LTTNG_LIVE
,
128 /* Legacy output format format */
129 enum legacy_output_format
{
130 LEGACY_OUTPUT_FORMAT_NONE
= 0,
131 LEGACY_OUTPUT_FORMAT_TEXT
,
132 LEGACY_OUTPUT_FORMAT_DUMMY
,
135 #define BT_CLI_LOGE_APPEND_CAUSE_OOM() BT_CLI_LOGE_APPEND_CAUSE("Out of memory.")
138 * Returns the plugin name, component class name, component class type,
139 * and component name from a command-line --component option's argument.
140 * arg must have the following format:
142 * [NAME:]TYPE.PLUGIN.CLS
144 * where NAME is the optional component name, TYPE is either `source`,
145 * `filter`, or `sink`, PLUGIN is the plugin name, and CLS is the
146 * component class name.
148 * On success, both *plugin and *component are not NULL. *plugin
149 * and *comp_cls are owned by the caller. On success, *name can be NULL
150 * if no component class name was found, and *comp_cls_type is set.
153 void plugin_comp_cls_names(const char *arg
, char **name
, char **plugin
,
154 char **comp_cls
, bt_component_class_type
*comp_cls_type
)
156 const char *at
= arg
;
157 GString
*gs_name
= NULL
;
158 GString
*gs_comp_cls_type
= NULL
;
159 GString
*gs_plugin
= NULL
;
160 GString
*gs_comp_cls
= NULL
;
166 BT_ASSERT(comp_cls_type
);
168 if (!bt_common_string_is_printable(arg
)) {
169 BT_CLI_LOGE_APPEND_CAUSE("Argument contains a non-printable character.");
173 /* Parse the component name */
174 gs_name
= bt_common_string_until(at
, ".:\\", ":", &end_pos
);
179 if (arg
[end_pos
] == ':') {
183 g_string_assign(gs_name
, "");
186 /* Parse the component class type */
187 gs_comp_cls_type
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
188 if (!gs_comp_cls_type
|| at
[end_pos
] == '\0') {
189 BT_CLI_LOGE_APPEND_CAUSE("Missing component class type (`source`, `filter`, or `sink`).");
193 if (strcmp(gs_comp_cls_type
->str
, "source") == 0 ||
194 strcmp(gs_comp_cls_type
->str
, "src") == 0) {
195 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SOURCE
;
196 } else if (strcmp(gs_comp_cls_type
->str
, "filter") == 0 ||
197 strcmp(gs_comp_cls_type
->str
, "flt") == 0) {
198 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_FILTER
;
199 } else if (strcmp(gs_comp_cls_type
->str
, "sink") == 0) {
200 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SINK
;
202 BT_CLI_LOGE_APPEND_CAUSE("Unknown component class type: `%s`.",
203 gs_comp_cls_type
->str
);
209 /* Parse the plugin name */
210 gs_plugin
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
211 if (!gs_plugin
|| gs_plugin
->len
== 0 || at
[end_pos
] == '\0') {
212 BT_CLI_LOGE_APPEND_CAUSE("Missing plugin or component class name.");
218 /* Parse the component class name */
219 gs_comp_cls
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
220 if (!gs_comp_cls
|| gs_comp_cls
->len
== 0) {
221 BT_CLI_LOGE_APPEND_CAUSE("Missing component class name.");
225 if (at
[end_pos
] != '\0') {
226 /* Found a non-escaped `.` */
231 if (gs_name
->len
== 0) {
233 g_string_free(gs_name
, TRUE
);
235 *name
= gs_name
->str
;
236 g_string_free(gs_name
, FALSE
);
239 g_string_free(gs_name
, TRUE
);
242 *plugin
= gs_plugin
->str
;
243 *comp_cls
= gs_comp_cls
->str
;
244 g_string_free(gs_plugin
, FALSE
);
245 g_string_free(gs_comp_cls
, FALSE
);
261 g_string_free(gs_name
, TRUE
);
265 g_string_free(gs_plugin
, TRUE
);
269 g_string_free(gs_comp_cls
, TRUE
);
272 if (gs_comp_cls_type
) {
273 g_string_free(gs_comp_cls_type
, TRUE
);
280 * Prints the Babeltrace version.
283 void print_version(void)
285 if (GIT_VERSION
[0] == '\0') {
286 puts("Babeltrace " VERSION
);
288 puts("Babeltrace " VERSION
" - " GIT_VERSION
);
293 * Destroys a component configuration.
296 void bt_config_component_destroy(bt_object
*obj
)
298 struct bt_config_component
*bt_config_component
=
299 container_of(obj
, struct bt_config_component
, base
);
305 if (bt_config_component
->plugin_name
) {
306 g_string_free(bt_config_component
->plugin_name
, TRUE
);
309 if (bt_config_component
->comp_cls_name
) {
310 g_string_free(bt_config_component
->comp_cls_name
, TRUE
);
313 if (bt_config_component
->instance_name
) {
314 g_string_free(bt_config_component
->instance_name
, TRUE
);
317 BT_VALUE_PUT_REF_AND_RESET(bt_config_component
->params
);
318 g_free(bt_config_component
);
325 * Creates a component configuration using the given plugin name and
326 * component name. `plugin_name` and `comp_cls_name` are copied (belong
327 * to the return value).
329 * Return value is owned by the caller.
332 struct bt_config_component
*bt_config_component_create(
333 bt_component_class_type type
,
334 const char *plugin_name
, const char *comp_cls_name
,
337 struct bt_config_component
*cfg_component
= NULL
;
339 cfg_component
= g_new0(struct bt_config_component
, 1);
340 if (!cfg_component
) {
341 BT_CLI_LOGE_APPEND_CAUSE_OOM();
345 bt_object_init_shared(&cfg_component
->base
,
346 bt_config_component_destroy
);
347 cfg_component
->type
= type
;
348 cfg_component
->plugin_name
= g_string_new(plugin_name
);
349 if (!cfg_component
->plugin_name
) {
350 BT_CLI_LOGE_APPEND_CAUSE_OOM();
354 cfg_component
->comp_cls_name
= g_string_new(comp_cls_name
);
355 if (!cfg_component
->comp_cls_name
) {
356 BT_CLI_LOGE_APPEND_CAUSE_OOM();
360 cfg_component
->instance_name
= g_string_new(NULL
);
361 if (!cfg_component
->instance_name
) {
362 BT_CLI_LOGE_APPEND_CAUSE_OOM();
366 cfg_component
->log_level
= init_log_level
;
368 /* Start with empty parameters */
369 cfg_component
->params
= bt_value_map_create();
370 if (!cfg_component
->params
) {
371 BT_CLI_LOGE_APPEND_CAUSE_OOM();
378 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
381 return cfg_component
;
385 * Creates a component configuration from a command-line --component
389 struct bt_config_component
*bt_config_component_from_arg(const char *arg
,
392 struct bt_config_component
*cfg_comp
= NULL
;
394 char *plugin_name
= NULL
;
395 char *comp_cls_name
= NULL
;
396 bt_component_class_type type
;
398 plugin_comp_cls_names(arg
, &name
, &plugin_name
, &comp_cls_name
, &type
);
399 if (!plugin_name
|| !comp_cls_name
) {
403 cfg_comp
= bt_config_component_create(type
, plugin_name
, comp_cls_name
,
410 g_string_assign(cfg_comp
->instance_name
, name
);
416 BT_OBJECT_PUT_REF_AND_RESET(cfg_comp
);
421 g_free(comp_cls_name
);
426 * Destroys a configuration.
429 void bt_config_destroy(bt_object
*obj
)
431 struct bt_config
*cfg
=
432 container_of(obj
, struct bt_config
, base
);
438 BT_VALUE_PUT_REF_AND_RESET(cfg
->plugin_paths
);
440 switch (cfg
->command
) {
441 case BT_CONFIG_COMMAND_RUN
:
442 if (cfg
->cmd_data
.run
.sources
) {
443 g_ptr_array_free(cfg
->cmd_data
.run
.sources
, TRUE
);
446 if (cfg
->cmd_data
.run
.filters
) {
447 g_ptr_array_free(cfg
->cmd_data
.run
.filters
, TRUE
);
450 if (cfg
->cmd_data
.run
.sinks
) {
451 g_ptr_array_free(cfg
->cmd_data
.run
.sinks
, TRUE
);
454 if (cfg
->cmd_data
.run
.connections
) {
455 g_ptr_array_free(cfg
->cmd_data
.run
.connections
,
459 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
461 case BT_CONFIG_COMMAND_HELP
:
462 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.help
.cfg_component
);
464 case BT_CONFIG_COMMAND_QUERY
:
465 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.query
.cfg_component
);
467 if (cfg
->cmd_data
.query
.object
) {
468 g_string_free(cfg
->cmd_data
.query
.object
, TRUE
);
471 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
472 if (cfg
->cmd_data
.print_ctf_metadata
.path
) {
473 g_string_free(cfg
->cmd_data
.print_ctf_metadata
.path
,
476 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
480 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
481 if (cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
483 cfg
->cmd_data
.print_lttng_live_sessions
.url
,
486 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
501 void destroy_glist_of_gstring(GList
*list
)
509 for (at
= list
; at
; at
= g_list_next(at
)) {
510 g_string_free(at
->data
, TRUE
);
517 * Creates a simple lexical scanner for parsing comma-delimited names
520 * Return value is owned by the caller.
523 GScanner
*create_csv_identifiers_scanner(void)
526 GScannerConfig scanner_config
= {
527 .cset_skip_characters
= " \t\n",
528 .cset_identifier_first
= G_CSET_a_2_z G_CSET_A_2_Z
"_",
529 .cset_identifier_nth
= G_CSET_a_2_z G_CSET_A_2_Z
":_-",
530 .case_sensitive
= TRUE
,
531 .cpair_comment_single
= NULL
,
532 .skip_comment_multi
= TRUE
,
533 .skip_comment_single
= TRUE
,
534 .scan_comment_multi
= FALSE
,
535 .scan_identifier
= TRUE
,
536 .scan_identifier_1char
= TRUE
,
537 .scan_identifier_NULL
= FALSE
,
538 .scan_symbols
= FALSE
,
539 .symbol_2_token
= FALSE
,
540 .scope_0_fallback
= FALSE
,
541 .scan_binary
= FALSE
,
545 .scan_hex_dollar
= FALSE
,
546 .numbers_2_int
= FALSE
,
547 .int_2_float
= FALSE
,
548 .store_int64
= FALSE
,
549 .scan_string_sq
= FALSE
,
550 .scan_string_dq
= FALSE
,
551 .identifier_2_string
= FALSE
,
552 .char_2_token
= TRUE
,
555 scanner
= g_scanner_new(&scanner_config
);
557 BT_CLI_LOGE_APPEND_CAUSE_OOM();
564 * Converts a comma-delimited list of known names (--names option) to
565 * an array value object containing those names as string value objects.
567 * Return value is owned by the caller.
570 bt_value
*names_from_arg(const char *arg
)
572 GScanner
*scanner
= NULL
;
573 bt_value
*names
= NULL
;
574 bool found_all
= false, found_none
= false, found_item
= false;
576 names
= bt_value_array_create();
578 BT_CLI_LOGE_APPEND_CAUSE_OOM();
582 scanner
= create_csv_identifiers_scanner();
587 g_scanner_input_text(scanner
, arg
, strlen(arg
));
590 GTokenType token_type
= g_scanner_get_next_token(scanner
);
592 switch (token_type
) {
593 case G_TOKEN_IDENTIFIER
:
595 const char *identifier
= scanner
->value
.v_identifier
;
597 if (strcmp(identifier
, "payload") == 0 ||
598 strcmp(identifier
, "args") == 0 ||
599 strcmp(identifier
, "arg") == 0) {
601 if (bt_value_array_append_string_element(names
,
605 } else if (strcmp(identifier
, "context") == 0 ||
606 strcmp(identifier
, "ctx") == 0) {
608 if (bt_value_array_append_string_element(names
,
612 } else if (strcmp(identifier
, "scope") == 0 ||
613 strcmp(identifier
, "header") == 0) {
615 if (bt_value_array_append_string_element(names
,
619 } else if (strcmp(identifier
, "all") == 0) {
621 if (bt_value_array_append_string_element(names
,
625 } else if (strcmp(identifier
, "none") == 0) {
627 if (bt_value_array_append_string_element(names
,
632 BT_CLI_LOGE_APPEND_CAUSE("Unknown name: `%s`.",
648 if (found_none
&& found_all
) {
649 BT_CLI_LOGE_APPEND_CAUSE("Only either `all` or `none` can be specified in the list given to the --names option, but not both.");
653 * Legacy behavior is to clear the defaults (show none) when at
654 * least one item is specified.
656 if (found_item
&& !found_none
&& !found_all
) {
657 if (bt_value_array_append_string_element(names
, "none")) {
662 g_scanner_destroy(scanner
);
667 BT_VALUE_PUT_REF_AND_RESET(names
);
669 g_scanner_destroy(scanner
);
675 * Converts a comma-delimited list of known fields (--fields option) to
676 * an array value object containing those fields as string
679 * Return value is owned by the caller.
682 bt_value
*fields_from_arg(const char *arg
)
684 GScanner
*scanner
= NULL
;
687 fields
= bt_value_array_create();
689 BT_CLI_LOGE_APPEND_CAUSE_OOM();
693 scanner
= create_csv_identifiers_scanner();
698 g_scanner_input_text(scanner
, arg
, strlen(arg
));
701 GTokenType token_type
= g_scanner_get_next_token(scanner
);
703 switch (token_type
) {
704 case G_TOKEN_IDENTIFIER
:
706 const char *identifier
= scanner
->value
.v_identifier
;
708 if (strcmp(identifier
, "trace") == 0 ||
709 strcmp(identifier
, "trace:hostname") == 0 ||
710 strcmp(identifier
, "trace:domain") == 0 ||
711 strcmp(identifier
, "trace:procname") == 0 ||
712 strcmp(identifier
, "trace:vpid") == 0 ||
713 strcmp(identifier
, "loglevel") == 0 ||
714 strcmp(identifier
, "emf") == 0 ||
715 strcmp(identifier
, "callsite") == 0 ||
716 strcmp(identifier
, "all") == 0) {
717 if (bt_value_array_append_string_element(fields
,
722 BT_CLI_LOGE_APPEND_CAUSE("Unknown field: `%s`.",
740 BT_VALUE_PUT_REF_AND_RESET(fields
);
744 g_scanner_destroy(scanner
);
750 void append_param_arg(GString
*params_arg
, const char *key
, const char *value
)
752 BT_ASSERT(params_arg
);
756 if (params_arg
->len
!= 0) {
757 g_string_append_c(params_arg
, ',');
760 g_string_append(params_arg
, key
);
761 g_string_append_c(params_arg
, '=');
762 g_string_append(params_arg
, value
);
766 * Inserts the equivalent "prefix-NAME=yes" strings into params_arg
767 * where the names are in names_array.
770 int insert_flat_params_from_array(GString
*params_arg
,
771 const bt_value
*names_array
, const char *prefix
)
775 GString
*tmpstr
= NULL
, *default_value
= NULL
;
776 bool default_set
= false, non_default_set
= false;
779 * names_array may be NULL if no CLI options were specified to
780 * trigger its creation.
786 tmpstr
= g_string_new(NULL
);
788 BT_CLI_LOGE_APPEND_CAUSE_OOM();
793 default_value
= g_string_new(NULL
);
794 if (!default_value
) {
795 BT_CLI_LOGE_APPEND_CAUSE_OOM();
800 for (i
= 0; i
< bt_value_array_get_size(names_array
); i
++) {
801 const bt_value
*str_obj
=
802 bt_value_array_borrow_element_by_index_const(names_array
,
805 bool is_default
= false;
808 BT_CLI_LOGE_APPEND_CAUSE("Unexpected error.");
813 suffix
= bt_value_string_get(str_obj
);
815 g_string_assign(tmpstr
, prefix
);
816 g_string_append(tmpstr
, "-");
818 /* Special-case for "all" and "none". */
819 if (strcmp(suffix
, "all") == 0) {
821 g_string_assign(default_value
, "show");
822 } else if (strcmp(suffix
, "none") == 0) {
824 g_string_assign(default_value
, "hide");
828 g_string_append(tmpstr
, "default");
829 append_param_arg(params_arg
, tmpstr
->str
,
832 non_default_set
= true;
833 g_string_append(tmpstr
, suffix
);
834 append_param_arg(params_arg
, tmpstr
->str
, "yes");
838 /* Implicit field-default=hide if any non-default option is set. */
839 if (non_default_set
&& !default_set
) {
840 g_string_assign(tmpstr
, prefix
);
841 g_string_append(tmpstr
, "-default");
842 g_string_assign(default_value
, "hide");
843 append_param_arg(params_arg
, tmpstr
->str
, default_value
->str
);
848 g_string_free(default_value
, TRUE
);
852 g_string_free(tmpstr
, TRUE
);
865 OPT_CLOCK_FORCE_CORRELATE
,
876 OPT_DEBUG_INFO_FULL_PATH
,
877 OPT_DEBUG_INFO_TARGET_PREFIX
,
886 OPT_OMIT_HOME_PLUGIN_PATH
,
887 OPT_OMIT_SYSTEM_PLUGIN_PATH
,
892 OPT_RESET_BASE_PARAMS
,
896 OPT_STREAM_INTERSECTION
,
902 enum bt_config_component_dest
{
903 BT_CONFIG_COMPONENT_DEST_UNKNOWN
= -1,
904 BT_CONFIG_COMPONENT_DEST_SOURCE
,
905 BT_CONFIG_COMPONENT_DEST_FILTER
,
906 BT_CONFIG_COMPONENT_DEST_SINK
,
910 * Adds a configuration component to the appropriate configuration
911 * array depending on the destination.
914 void add_run_cfg_comp(struct bt_config
*cfg
,
915 struct bt_config_component
*cfg_comp
,
916 enum bt_config_component_dest dest
)
918 bt_object_get_ref(cfg_comp
);
921 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
922 g_ptr_array_add(cfg
->cmd_data
.run
.sources
, cfg_comp
);
924 case BT_CONFIG_COMPONENT_DEST_FILTER
:
925 g_ptr_array_add(cfg
->cmd_data
.run
.filters
, cfg_comp
);
927 case BT_CONFIG_COMPONENT_DEST_SINK
:
928 g_ptr_array_add(cfg
->cmd_data
.run
.sinks
, cfg_comp
);
936 int add_run_cfg_comp_check_name(struct bt_config
*cfg
,
937 struct bt_config_component
*cfg_comp
,
938 enum bt_config_component_dest dest
,
939 bt_value
*instance_names
)
943 if (cfg_comp
->instance_name
->len
== 0) {
944 BT_CLI_LOGE_APPEND_CAUSE("Found an unnamed component.");
949 if (bt_value_map_has_entry(instance_names
,
950 cfg_comp
->instance_name
->str
)) {
951 BT_CLI_LOGE_APPEND_CAUSE("Duplicate component instance name:\n %s",
952 cfg_comp
->instance_name
->str
);
957 if (bt_value_map_insert_entry(instance_names
,
958 cfg_comp
->instance_name
->str
, bt_value_null
)) {
959 BT_CLI_LOGE_APPEND_CAUSE_OOM();
964 add_run_cfg_comp(cfg
, cfg_comp
, dest
);
971 int append_env_var_plugin_paths(bt_value
*plugin_paths
)
976 if (bt_common_is_setuid_setgid()) {
977 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
981 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
986 ret
= bt_config_append_plugin_paths(plugin_paths
, envvar
);
990 BT_CLI_LOGE_APPEND_CAUSE("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH.");
997 int append_home_and_system_plugin_paths(bt_value
*plugin_paths
,
998 bool omit_system_plugin_path
, bool omit_home_plugin_path
)
1002 if (!omit_home_plugin_path
) {
1003 if (bt_common_is_setuid_setgid()) {
1004 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1006 char *home_plugin_dir
= bt_common_get_home_plugin_path(
1007 BT_LOG_OUTPUT_LEVEL
);
1009 if (home_plugin_dir
) {
1010 ret
= bt_config_append_plugin_paths(
1011 plugin_paths
, home_plugin_dir
);
1012 free(home_plugin_dir
);
1015 BT_CLI_LOGE_APPEND_CAUSE("Invalid home plugin path.");
1022 if (!omit_system_plugin_path
) {
1023 if (bt_config_append_plugin_paths(plugin_paths
,
1024 bt_common_get_system_plugin_path())) {
1025 BT_CLI_LOGE_APPEND_CAUSE("Invalid system plugin path.");
1031 BT_CLI_LOGE_APPEND_CAUSE("Cannot append home and system plugin paths.");
1036 int append_home_and_system_plugin_paths_cfg(struct bt_config
*cfg
)
1038 return append_home_and_system_plugin_paths(cfg
->plugin_paths
,
1039 cfg
->omit_system_plugin_path
, cfg
->omit_home_plugin_path
);
1043 struct bt_config
*bt_config_base_create(enum bt_config_command command
,
1044 const bt_value
*initial_plugin_paths
,
1047 struct bt_config
*cfg
;
1050 cfg
= g_new0(struct bt_config
, 1);
1052 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1056 bt_object_init_shared(&cfg
->base
, bt_config_destroy
);
1057 cfg
->command
= command
;
1058 cfg
->command_needs_plugins
= needs_plugins
;
1060 if (initial_plugin_paths
) {
1061 bt_value
*initial_plugin_paths_copy
;
1063 (void) bt_value_copy(initial_plugin_paths
,
1064 &initial_plugin_paths_copy
);
1065 cfg
->plugin_paths
= initial_plugin_paths_copy
;
1067 cfg
->plugin_paths
= bt_value_array_create();
1068 if (!cfg
->plugin_paths
) {
1069 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1077 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1084 struct bt_config
*bt_config_run_create(
1085 const bt_value
*initial_plugin_paths
)
1087 struct bt_config
*cfg
;
1090 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_RUN
,
1091 initial_plugin_paths
, true);
1096 cfg
->cmd_data
.run
.sources
= g_ptr_array_new_with_free_func(
1097 (GDestroyNotify
) bt_object_put_ref
);
1098 if (!cfg
->cmd_data
.run
.sources
) {
1099 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1103 cfg
->cmd_data
.run
.filters
= g_ptr_array_new_with_free_func(
1104 (GDestroyNotify
) bt_object_put_ref
);
1105 if (!cfg
->cmd_data
.run
.filters
) {
1106 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1110 cfg
->cmd_data
.run
.sinks
= g_ptr_array_new_with_free_func(
1111 (GDestroyNotify
) bt_object_put_ref
);
1112 if (!cfg
->cmd_data
.run
.sinks
) {
1113 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1117 cfg
->cmd_data
.run
.connections
= g_ptr_array_new_with_free_func(
1118 (GDestroyNotify
) bt_config_connection_destroy
);
1119 if (!cfg
->cmd_data
.run
.connections
) {
1120 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1127 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1134 struct bt_config
*bt_config_list_plugins_create(
1135 const bt_value
*initial_plugin_paths
)
1137 struct bt_config
*cfg
;
1140 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS
,
1141 initial_plugin_paths
, true);
1149 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1156 struct bt_config
*bt_config_help_create(
1157 const bt_value
*initial_plugin_paths
,
1158 int default_log_level
)
1160 struct bt_config
*cfg
;
1163 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_HELP
,
1164 initial_plugin_paths
, true);
1169 cfg
->cmd_data
.help
.cfg_component
=
1170 bt_config_component_create(-1, NULL
, NULL
, default_log_level
);
1171 if (!cfg
->cmd_data
.help
.cfg_component
) {
1178 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1185 struct bt_config
*bt_config_query_create(
1186 const bt_value
*initial_plugin_paths
)
1188 struct bt_config
*cfg
;
1191 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_QUERY
,
1192 initial_plugin_paths
, true);
1197 cfg
->cmd_data
.query
.object
= g_string_new(NULL
);
1198 if (!cfg
->cmd_data
.query
.object
) {
1199 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1206 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1213 struct bt_config
*bt_config_print_ctf_metadata_create(
1214 const bt_value
*initial_plugin_paths
)
1216 struct bt_config
*cfg
;
1219 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_CTF_METADATA
,
1220 initial_plugin_paths
, true);
1225 cfg
->cmd_data
.print_ctf_metadata
.path
= g_string_new(NULL
);
1226 if (!cfg
->cmd_data
.print_ctf_metadata
.path
) {
1227 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1231 cfg
->cmd_data
.print_ctf_metadata
.output_path
= g_string_new(NULL
);
1232 if (!cfg
->cmd_data
.print_ctf_metadata
.output_path
) {
1233 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1240 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1247 struct bt_config
*bt_config_print_lttng_live_sessions_create(
1248 const bt_value
*initial_plugin_paths
)
1250 struct bt_config
*cfg
;
1253 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
,
1254 initial_plugin_paths
, true);
1259 cfg
->cmd_data
.print_lttng_live_sessions
.url
= g_string_new(NULL
);
1260 if (!cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
1261 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1265 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
=
1267 if (!cfg
->cmd_data
.print_lttng_live_sessions
.output_path
) {
1268 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1275 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1282 int bt_config_append_plugin_paths_check_setuid_setgid(
1283 bt_value
*plugin_paths
, const char *arg
)
1287 if (bt_common_is_setuid_setgid()) {
1288 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1292 if (bt_config_append_plugin_paths(plugin_paths
, arg
)) {
1293 BT_CLI_LOGE_APPEND_CAUSE("Invalid --plugin-path option's argument:\n %s",
1304 * Prints the expected format for a --params option.
1307 void print_expected_params_format(FILE *fp
)
1309 fprintf(fp
, "Expected format of PARAMS\n");
1310 fprintf(fp
, "-------------------------\n");
1312 fprintf(fp
, " PARAM=VALUE[,PARAM=VALUE]...\n");
1314 fprintf(fp
, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
1315 fprintf(fp
, "where PARAM is the parameter name (C identifier plus the [:.-] characters),\n");
1316 fprintf(fp
, "and VALUE can be one of:\n");
1318 fprintf(fp
, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
1319 fprintf(fp
, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
1320 fprintf(fp
, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
1321 fprintf(fp
, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
1322 fprintf(fp
, " (`0x` prefix) unsigned (with `+` prefix) or signed 64-bit integer.\n");
1323 fprintf(fp
, "* Double precision floating point number (scientific notation is accepted).\n");
1324 fprintf(fp
, "* Unquoted string with no special characters, and not matching any of\n");
1325 fprintf(fp
, " the null and boolean value symbols above.\n");
1326 fprintf(fp
, "* Double-quoted string (accepts escape characters).\n");
1327 fprintf(fp
, "* Array, formatted as an opening `[`, a list of comma-separated values\n");
1328 fprintf(fp
, " (as described by the current list) and a closing `]`.\n");
1330 fprintf(fp
, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1332 fprintf(fp
, "Example:\n");
1334 fprintf(fp
, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
1335 fprintf(fp
, " play=+23, observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
1336 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\",\n");
1337 fprintf(fp
, " things=[1, \"2\", 3]\n");
1339 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1340 fprintf(fp
, "babeltrace2 from a shell.\n");
1344 bool help_option_is_specified(
1345 const struct bt_argpar_parse_ret
*argpar_parse_ret
)
1348 bool specified
= false;
1350 for (i
= 0; i
< argpar_parse_ret
->items
->len
; i
++) {
1351 struct bt_argpar_item
*argpar_item
=
1352 g_ptr_array_index(argpar_parse_ret
->items
, i
);
1353 struct bt_argpar_item_opt
*argpar_item_opt
;
1355 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
1359 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
1360 if (argpar_item_opt
->descr
->id
== OPT_HELP
) {
1370 * Prints the help command usage.
1373 void print_help_usage(FILE *fp
)
1375 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1376 fprintf(fp
, " babeltrace2 [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
1378 fprintf(fp
, "Options:\n");
1380 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1381 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1382 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1383 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1384 fprintf(fp
, " dynamic plugins can be loaded\n");
1385 fprintf(fp
, " -h, --help Show this help and quit\n");
1387 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1389 fprintf(fp
, "Use `babeltrace2 list-plugins` to show the list of available plugins.\n");
1393 const struct bt_argpar_opt_descr help_options
[] = {
1394 /* id, short_name, long_name, with_arg */
1395 { OPT_HELP
, 'h', "help", false },
1396 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1397 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1398 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1399 BT_ARGPAR_OPT_DESCR_SENTINEL
1403 * Creates a Babeltrace config object from the arguments of a help
1406 * *retcode is set to the appropriate exit code to use.
1409 struct bt_config
*bt_config_help_from_args(int argc
, const char *argv
[],
1410 int *retcode
, bool force_omit_system_plugin_path
,
1411 bool force_omit_home_plugin_path
,
1412 const bt_value
*initial_plugin_paths
, int default_log_level
)
1415 struct bt_config
*cfg
= NULL
;
1416 const char *leftover
= NULL
;
1417 char *plugin_name
= NULL
, *comp_cls_name
= NULL
;
1418 struct bt_argpar_parse_ret argpar_parse_ret
;
1421 cfg
= bt_config_help_create(initial_plugin_paths
, default_log_level
);
1426 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1427 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1428 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1434 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, help_options
, true);
1435 if (argpar_parse_ret
.error
) {
1436 BT_CLI_LOGE_APPEND_CAUSE(
1437 "While parsing `help` command's command-line arguments: %s",
1438 argpar_parse_ret
.error
->str
);
1442 if (help_option_is_specified(&argpar_parse_ret
)) {
1443 print_help_usage(stdout
);
1445 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1449 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
1450 struct bt_argpar_item
*argpar_item
=
1451 g_ptr_array_index(argpar_parse_ret
.items
, i
);
1453 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
1454 struct bt_argpar_item_opt
*argpar_item_opt
;
1456 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
1457 arg
= argpar_item_opt
->arg
;
1459 switch (argpar_item_opt
->descr
->id
) {
1460 case OPT_PLUGIN_PATH
:
1461 if (bt_config_append_plugin_paths_check_setuid_setgid(
1462 cfg
->plugin_paths
, arg
)) {
1466 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1467 cfg
->omit_system_plugin_path
= true;
1469 case OPT_OMIT_HOME_PLUGIN_PATH
:
1470 cfg
->omit_home_plugin_path
= true;
1473 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1474 argpar_item_opt
->descr
->id
);
1478 struct bt_argpar_item_non_opt
*argpar_item_non_opt
1479 = (struct bt_argpar_item_non_opt
*) argpar_item
;
1482 BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `help` command: `%s`.",
1483 argpar_item_non_opt
->arg
);
1487 leftover
= argpar_item_non_opt
->arg
;
1492 plugin_comp_cls_names(leftover
, NULL
,
1493 &plugin_name
, &comp_cls_name
,
1494 &cfg
->cmd_data
.help
.cfg_component
->type
);
1495 if (plugin_name
&& comp_cls_name
) {
1496 /* Component class help */
1498 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1501 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
,
1504 /* Fall back to plugin help */
1506 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1510 print_help_usage(stdout
);
1512 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1516 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1524 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1527 g_free(plugin_name
);
1528 g_free(comp_cls_name
);
1530 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1536 * Prints the help command usage.
1539 void print_query_usage(FILE *fp
)
1541 fprintf(fp
, "Usage: babeltrace2 [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
1543 fprintf(fp
, "Options:\n");
1545 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1546 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1547 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1548 fprintf(fp
, " -p, --params=PARAMS Set the query parameters to PARAMS\n");
1549 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1550 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1551 fprintf(fp
, " dynamic plugins can be loaded\n");
1552 fprintf(fp
, " -h, --help Show this help and quit\n");
1553 fprintf(fp
, "\n\n");
1554 print_expected_params_format(fp
);
1558 const struct bt_argpar_opt_descr query_options
[] = {
1559 /* id, short_name, long_name, with_arg */
1560 { OPT_HELP
, 'h', "help", false },
1561 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1562 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1563 { OPT_PARAMS
, 'p', "params", true },
1564 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1565 BT_ARGPAR_OPT_DESCR_SENTINEL
1569 * Creates a Babeltrace config object from the arguments of a query
1572 * *retcode is set to the appropriate exit code to use.
1575 struct bt_config
*bt_config_query_from_args(int argc
, const char *argv
[],
1576 int *retcode
, bool force_omit_system_plugin_path
,
1577 bool force_omit_home_plugin_path
,
1578 const bt_value
*initial_plugin_paths
,
1579 int default_log_level
)
1582 struct bt_config
*cfg
= NULL
;
1583 const char *component_class_spec
= NULL
;
1584 const char *query_object
= NULL
;
1586 GString
*error_str
= NULL
;
1587 struct bt_argpar_parse_ret argpar_parse_ret
;
1589 params
= bt_value_null
;
1590 bt_value_get_ref(bt_value_null
);
1593 cfg
= bt_config_query_create(initial_plugin_paths
);
1598 error_str
= g_string_new(NULL
);
1600 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1604 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1605 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1606 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1612 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, query_options
, true);
1613 if (argpar_parse_ret
.error
) {
1614 BT_CLI_LOGE_APPEND_CAUSE(
1615 "While parsing `query` command's command-line arguments: %s",
1616 argpar_parse_ret
.error
->str
);
1620 if (help_option_is_specified(&argpar_parse_ret
)) {
1621 print_query_usage(stdout
);
1623 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1627 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
1628 struct bt_argpar_item
*argpar_item
=
1629 g_ptr_array_index(argpar_parse_ret
.items
, i
);
1631 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
1632 struct bt_argpar_item_opt
*argpar_item_opt
=
1633 (struct bt_argpar_item_opt
*) argpar_item
;
1634 const char *arg
= argpar_item_opt
->arg
;
1636 switch (argpar_item_opt
->descr
->id
) {
1637 case OPT_PLUGIN_PATH
:
1638 if (bt_config_append_plugin_paths_check_setuid_setgid(
1639 cfg
->plugin_paths
, arg
)) {
1643 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1644 cfg
->omit_system_plugin_path
= true;
1646 case OPT_OMIT_HOME_PLUGIN_PATH
:
1647 cfg
->omit_home_plugin_path
= true;
1651 bt_value_put_ref(params
);
1652 params
= cli_value_from_arg(arg
, error_str
);
1654 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1661 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1662 argpar_item_opt
->descr
->id
);
1666 struct bt_argpar_item_non_opt
*argpar_item_non_opt
1667 = (struct bt_argpar_item_non_opt
*) argpar_item
;
1670 * We need exactly two leftover arguments which are the
1671 * mandatory component class specification and query object.
1673 if (!component_class_spec
) {
1674 component_class_spec
= argpar_item_non_opt
->arg
;
1675 } else if (!query_object
) {
1676 query_object
= argpar_item_non_opt
->arg
;
1678 BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `query` command: `%s`.",
1679 argpar_item_non_opt
->arg
);
1685 if (!component_class_spec
|| !query_object
) {
1686 print_query_usage(stdout
);
1688 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1692 cfg
->cmd_data
.query
.cfg_component
=
1693 bt_config_component_from_arg(component_class_spec
,
1695 if (!cfg
->cmd_data
.query
.cfg_component
) {
1696 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for component class specification:\n %s",
1697 component_class_spec
);
1702 BT_OBJECT_MOVE_REF(cfg
->cmd_data
.query
.cfg_component
->params
, params
);
1704 if (strlen(query_object
) == 0) {
1705 BT_CLI_LOGE_APPEND_CAUSE("Invalid empty object.");
1709 g_string_assign(cfg
->cmd_data
.query
.object
, query_object
);
1711 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1719 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1722 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1725 g_string_free(error_str
, TRUE
);
1728 bt_value_put_ref(params
);
1733 * Prints the list-plugins command usage.
1736 void print_list_plugins_usage(FILE *fp
)
1738 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
1740 fprintf(fp
, "Options:\n");
1742 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1743 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1744 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1745 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1746 fprintf(fp
, " dynamic plugins can be loaded\n");
1747 fprintf(fp
, " -h, --help Show this help and quit\n");
1749 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1751 fprintf(fp
, "Use `babeltrace2 help` to get help for a specific plugin or component class.\n");
1755 const struct bt_argpar_opt_descr list_plugins_options
[] = {
1756 /* id, short_name, long_name, with_arg */
1757 { OPT_HELP
, 'h', "help", false },
1758 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1759 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1760 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1761 BT_ARGPAR_OPT_DESCR_SENTINEL
1765 * Creates a Babeltrace config object from the arguments of a
1766 * list-plugins command.
1768 * *retcode is set to the appropriate exit code to use.
1771 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
1772 int *retcode
, bool force_omit_system_plugin_path
,
1773 bool force_omit_home_plugin_path
,
1774 const bt_value
*initial_plugin_paths
)
1777 struct bt_config
*cfg
= NULL
;
1778 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1781 cfg
= bt_config_list_plugins_create(initial_plugin_paths
);
1786 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1787 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1788 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1794 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, list_plugins_options
, true);
1795 if (argpar_parse_ret
.error
) {
1796 BT_CLI_LOGE_APPEND_CAUSE(
1797 "While parsing `list-plugins` command's command-line arguments: %s",
1798 argpar_parse_ret
.error
->str
);
1802 if (help_option_is_specified(&argpar_parse_ret
)) {
1803 print_list_plugins_usage(stdout
);
1805 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1809 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
1810 struct bt_argpar_item
*argpar_item
=
1811 g_ptr_array_index(argpar_parse_ret
.items
, i
);
1812 struct bt_argpar_item_opt
*argpar_item_opt
;
1815 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
1816 struct bt_argpar_item_non_opt
*argpar_item_non_opt
1817 = (struct bt_argpar_item_non_opt
*) argpar_item
;
1819 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`.",
1820 argpar_item_non_opt
->arg
);
1824 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
1825 arg
= argpar_item_opt
->arg
;
1827 switch (argpar_item_opt
->descr
->id
) {
1828 case OPT_PLUGIN_PATH
:
1829 if (bt_config_append_plugin_paths_check_setuid_setgid(
1830 cfg
->plugin_paths
, arg
)) {
1834 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1835 cfg
->omit_system_plugin_path
= true;
1837 case OPT_OMIT_HOME_PLUGIN_PATH
:
1838 cfg
->omit_home_plugin_path
= true;
1841 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1842 argpar_item_opt
->descr
->id
);
1847 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1855 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1858 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1864 * Prints the run command usage.
1867 void print_run_usage(FILE *fp
)
1869 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n");
1871 fprintf(fp
, "Options:\n");
1873 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
1874 fprintf(fp
, " for all the following components until\n");
1875 fprintf(fp
, " --reset-base-params is encountered\n");
1876 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1877 fprintf(fp
, " -c, --component=NAME:TYPE.PLUGIN.CLS\n");
1878 fprintf(fp
, " Instantiate the component class CLS of type\n");
1879 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
1880 fprintf(fp
, " in the plugin PLUGIN, add it to the graph,\n");
1881 fprintf(fp
, " and name it NAME");
1882 fprintf(fp
, " -x, --connect=CONNECTION Connect two created components (see the\n");
1883 fprintf(fp
, " expected format of CONNECTION below)\n");
1884 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
1885 fprintf(fp
, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
1886 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1887 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1888 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1889 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
1890 fprintf(fp
, " current component (see the expected format\n");
1891 fprintf(fp
, " of PARAMS below)\n");
1892 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1893 fprintf(fp
, " dynamic plugins can be loaded\n");
1894 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters to an\n");
1895 fprintf(fp
, " empty map\n");
1896 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
1897 fprintf(fp
, " the graph later, retry in DUR µs\n");
1898 fprintf(fp
, " (default: 100000)\n");
1899 fprintf(fp
, " -h, --help Show this help and quit\n");
1901 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1902 fprintf(fp
, "\n\n");
1903 fprintf(fp
, "Expected format of CONNECTION\n");
1904 fprintf(fp
, "-----------------------------\n");
1906 fprintf(fp
, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
1908 fprintf(fp
, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
1909 fprintf(fp
, "components to connect together. You must escape the following characters\n\n");
1910 fprintf(fp
, "with `\\`: `\\`, `.`, and `:`. You must set the name of the current\n");
1911 fprintf(fp
, "component using the NAME prefix of the --component option.\n");
1913 fprintf(fp
, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
1914 fprintf(fp
, "identify the upstream and downstream ports to use for the connection.\n");
1915 fprintf(fp
, "When the port is not specified, `*` is used.\n");
1917 fprintf(fp
, "When a component named UPSTREAM has an available port which matches the\n");
1918 fprintf(fp
, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
1919 fprintf(fp
, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
1920 fprintf(fp
, "DOWNSTREAM.\n");
1922 fprintf(fp
, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
1923 fprintf(fp
, "which matches anything. You must escape the following characters\n");
1924 fprintf(fp
, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
1926 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
1927 fprintf(fp
, "can connect a filter component to a sink component.\n");
1929 fprintf(fp
, "Examples:\n");
1931 fprintf(fp
, " my-src:my-sink\n");
1932 fprintf(fp
, " ctf-fs.*stream*:utils-muxer:*\n");
1934 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1935 fprintf(fp
, "babeltrace2 from a shell.\n");
1936 fprintf(fp
, "\n\n");
1937 print_expected_params_format(fp
);
1941 * Creates a Babeltrace config object from the arguments of a run
1944 * *retcode is set to the appropriate exit code to use.
1947 struct bt_config
*bt_config_run_from_args(int argc
, const char *argv
[],
1948 int *retcode
, bool force_omit_system_plugin_path
,
1949 bool force_omit_home_plugin_path
,
1950 const bt_value
*initial_plugin_paths
, int default_log_level
)
1952 struct bt_config_component
*cur_cfg_comp
= NULL
;
1953 bt_value
*cur_base_params
= NULL
;
1955 struct bt_config
*cfg
= NULL
;
1956 bt_value
*instance_names
= NULL
;
1957 bt_value
*connection_args
= NULL
;
1958 char error_buf
[256] = { 0 };
1959 long retry_duration
= -1;
1960 bt_value_map_extend_status extend_status
;
1961 GString
*error_str
= NULL
;
1962 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1965 static const struct bt_argpar_opt_descr run_options
[] = {
1966 { OPT_BASE_PARAMS
, 'b', "base-params", true },
1967 { OPT_COMPONENT
, 'c', "component", true },
1968 { OPT_CONNECT
, 'x', "connect", true },
1969 { OPT_HELP
, 'h', "help", false },
1970 { OPT_LOG_LEVEL
, 'l', "log-level", true },
1971 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1972 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1973 { OPT_PARAMS
, 'p', "params", true },
1974 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1975 { OPT_RESET_BASE_PARAMS
, 'r', "reset-base-params", false },
1976 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
1977 BT_ARGPAR_OPT_DESCR_SENTINEL
1982 error_str
= g_string_new(NULL
);
1984 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1989 print_run_usage(stdout
);
1994 cfg
= bt_config_run_create(initial_plugin_paths
);
1999 cfg
->cmd_data
.run
.retry_duration_us
= 100000;
2000 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2001 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2002 cur_base_params
= bt_value_map_create();
2003 if (!cur_base_params
) {
2004 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2008 instance_names
= bt_value_map_create();
2009 if (!instance_names
) {
2010 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2014 connection_args
= bt_value_array_create();
2015 if (!connection_args
) {
2016 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2020 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2026 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, run_options
, true);
2027 if (argpar_parse_ret
.error
) {
2028 BT_CLI_LOGE_APPEND_CAUSE(
2029 "While parsing `run` command's command-line arguments: %s",
2030 argpar_parse_ret
.error
->str
);
2034 if (help_option_is_specified(&argpar_parse_ret
)) {
2035 print_run_usage(stdout
);
2037 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2041 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
2042 struct bt_argpar_item
*argpar_item
=
2043 g_ptr_array_index(argpar_parse_ret
.items
, i
);
2044 struct bt_argpar_item_opt
*argpar_item_opt
;
2047 /* This command does not accept leftover arguments. */
2048 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
2049 struct bt_argpar_item_non_opt
*argpar_nonopt_item
=
2050 (struct bt_argpar_item_non_opt
*) argpar_item
;
2052 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`",
2053 argpar_nonopt_item
->arg
);
2057 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
2058 arg
= argpar_item_opt
->arg
;
2060 switch (argpar_item_opt
->descr
->id
) {
2061 case OPT_PLUGIN_PATH
:
2062 if (bt_config_append_plugin_paths_check_setuid_setgid(
2063 cfg
->plugin_paths
, arg
)) {
2067 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2068 cfg
->omit_system_plugin_path
= true;
2070 case OPT_OMIT_HOME_PLUGIN_PATH
:
2071 cfg
->omit_home_plugin_path
= true;
2075 enum bt_config_component_dest dest
;
2077 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2078 cur_cfg_comp
= bt_config_component_from_arg(arg
,
2080 if (!cur_cfg_comp
) {
2081 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --component option's argument:\n %s",
2086 switch (cur_cfg_comp
->type
) {
2087 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2088 dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
2090 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2091 dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
2093 case BT_COMPONENT_CLASS_TYPE_SINK
:
2094 dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
2100 BT_ASSERT(cur_base_params
);
2101 bt_value_put_ref(cur_cfg_comp
->params
);
2102 if (bt_value_copy(cur_base_params
,
2103 &cur_cfg_comp
->params
) < 0) {
2104 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2108 ret
= add_run_cfg_comp_check_name(cfg
,
2120 bt_value
*params_to_set
;
2122 if (!cur_cfg_comp
) {
2123 BT_CLI_LOGE_APPEND_CAUSE("Cannot add parameters to unavailable component:\n %s",
2128 params
= cli_value_from_arg(arg
, error_str
);
2130 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
2135 extend_status
= bt_value_map_extend(
2136 cur_cfg_comp
->params
, params
, ¶ms_to_set
);
2137 BT_VALUE_PUT_REF_AND_RESET(params
);
2138 if (extend_status
!= BT_VALUE_MAP_EXTEND_STATUS_OK
) {
2139 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current component parameters with --params option's argument:\n %s",
2144 BT_OBJECT_MOVE_REF(cur_cfg_comp
->params
, params_to_set
);
2148 if (!cur_cfg_comp
) {
2149 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the log level of unavailable component:\n %s",
2154 cur_cfg_comp
->log_level
=
2155 bt_log_get_level_from_string(arg
);
2156 if (cur_cfg_comp
->log_level
< 0) {
2157 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
2162 case OPT_BASE_PARAMS
:
2164 bt_value
*params
= cli_value_from_arg(arg
, error_str
);
2167 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --base-params option's argument:\n %s",
2172 BT_OBJECT_MOVE_REF(cur_base_params
, params
);
2175 case OPT_RESET_BASE_PARAMS
:
2176 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2177 cur_base_params
= bt_value_map_create();
2178 if (!cur_base_params
) {
2179 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2184 if (bt_value_array_append_string_element(
2185 connection_args
, arg
)) {
2186 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2190 case OPT_RETRY_DURATION
: {
2192 size_t arg_len
= strlen(argpar_item_opt
->arg
);
2194 retry_duration
= g_ascii_strtoll(argpar_item_opt
->arg
, &end
, 10);
2196 if (arg_len
== 0 || end
!= (argpar_item_opt
->arg
+ arg_len
)) {
2197 BT_CLI_LOGE_APPEND_CAUSE(
2198 "Could not parse --retry-duration option's argument as an unsigned integer: `%s`",
2199 argpar_item_opt
->arg
);
2203 if (retry_duration
< 0) {
2204 BT_CLI_LOGE_APPEND_CAUSE("--retry-duration option's argument must be positive or 0: %ld",
2209 cfg
->cmd_data
.run
.retry_duration_us
=
2210 (uint64_t) retry_duration
;
2214 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
2215 argpar_item_opt
->descr
->id
);
2220 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2222 if (cfg
->cmd_data
.run
.sources
->len
== 0) {
2223 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no source component.");
2227 if (cfg
->cmd_data
.run
.sinks
->len
== 0) {
2228 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no sink component.");
2232 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2236 ret
= bt_config_cli_args_create_connections(cfg
,
2240 BT_CLI_LOGE_APPEND_CAUSE("Cannot creation connections:\n%s", error_buf
);
2248 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2252 g_string_free(error_str
, TRUE
);
2255 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
2256 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2257 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2258 BT_VALUE_PUT_REF_AND_RESET(instance_names
);
2259 BT_VALUE_PUT_REF_AND_RESET(connection_args
);
2264 struct bt_config
*bt_config_run_from_args_array(const bt_value
*run_args
,
2265 int *retcode
, bool force_omit_system_plugin_path
,
2266 bool force_omit_home_plugin_path
,
2267 const bt_value
*initial_plugin_paths
, int default_log_level
)
2269 struct bt_config
*cfg
= NULL
;
2272 const size_t argc
= bt_value_array_get_size(run_args
);
2274 argv
= calloc(argc
, sizeof(*argv
));
2276 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2280 len
= bt_value_array_get_size(run_args
);
2282 BT_CLI_LOGE_APPEND_CAUSE("Invalid executable arguments.");
2285 for (i
= 0; i
< len
; i
++) {
2286 const bt_value
*arg_value
=
2287 bt_value_array_borrow_element_by_index_const(run_args
,
2291 BT_ASSERT(arg_value
);
2292 arg
= bt_value_string_get(arg_value
);
2297 cfg
= bt_config_run_from_args(argc
, argv
, retcode
,
2298 force_omit_system_plugin_path
, force_omit_home_plugin_path
,
2299 initial_plugin_paths
, default_log_level
);
2307 * Prints the convert command usage.
2310 void print_convert_usage(FILE *fp
)
2312 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2314 fprintf(fp
, "Options:\n");
2316 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2317 fprintf(fp
, " Instantiate the component class CLS of type\n");
2318 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2319 fprintf(fp
, " in the plugin PLUGIN, add it to the\n");
2320 fprintf(fp
, " conversion graph, and optionally name it\n");
2321 fprintf(fp
, " NAME\n");
2322 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
2323 fprintf(fp
, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
2324 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2325 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
2326 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2327 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2328 fprintf(fp
, " current component (see the expected format\n");
2329 fprintf(fp
, " of PARAMS below)\n");
2330 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2331 fprintf(fp
, " dynamic plugins can be loaded\n");
2332 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
2333 fprintf(fp
, " the graph later, retry in DUR µs\n");
2334 fprintf(fp
, " (default: 100000)\n");
2335 fprintf(fp
, " dynamic plugins can be loaded\n");
2336 fprintf(fp
, " --run-args Print the equivalent arguments for the\n");
2337 fprintf(fp
, " `run` command to the standard output,\n");
2338 fprintf(fp
, " formatted for a shell, and quit\n");
2339 fprintf(fp
, " --run-args-0 Print the equivalent arguments for the\n");
2340 fprintf(fp
, " `run` command to the standard output,\n");
2341 fprintf(fp
, " formatted for `xargs -0`, and quit\n");
2342 fprintf(fp
, " --stream-intersection Only process events when all streams\n");
2343 fprintf(fp
, " are active\n");
2344 fprintf(fp
, " -h, --help Show this help and quit\n");
2346 fprintf(fp
, "Implicit `source.ctf.fs` component options:\n");
2348 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2349 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2351 fprintf(fp
, "Implicit `sink.text.pretty` component options:\n");
2353 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
2354 fprintf(fp
, " --clock-date Print timestamp dates\n");
2355 fprintf(fp
, " --clock-gmt Print and parse timestamps in the GMT\n");
2356 fprintf(fp
, " time zone instead of the local time zone\n");
2357 fprintf(fp
, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2358 fprintf(fp
, " of `hh:mm:ss.nnnnnnnnn`\n");
2359 fprintf(fp
, " --color=(never | auto | always)\n");
2360 fprintf(fp
, " Never, automatically, or always emit\n");
2361 fprintf(fp
, " console color codes\n");
2362 fprintf(fp
, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2363 fprintf(fp
, " `all`, `trace`, `trace:hostname`,\n");
2364 fprintf(fp
, " `trace:domain`, `trace:procname`,\n");
2365 fprintf(fp
, " `trace:vpid`, `loglevel`, `emf`\n");
2366 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2367 fprintf(fp
, " `payload` (or `arg` or `args`), `none`,\n");
2368 fprintf(fp
, " `all`, `scope`, `header`, `context`\n");
2369 fprintf(fp
, " (or `ctx`)\n");
2370 fprintf(fp
, " --no-delta Do not print time delta between\n");
2371 fprintf(fp
, " consecutive events\n");
2372 fprintf(fp
, " -w, --output=PATH Write output text to PATH instead of\n");
2373 fprintf(fp
, " the standard output\n");
2375 fprintf(fp
, "Implicit `filter.utils.muxer` component options:\n");
2377 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently\n");
2378 fprintf(fp
, " correlated across traces\n");
2380 fprintf(fp
, "Implicit `filter.utils.trimmer` component options:\n");
2382 fprintf(fp
, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2383 fprintf(fp
, " time range to BEGIN (see the format of\n");
2384 fprintf(fp
, " BEGIN below)\n");
2385 fprintf(fp
, " -e, --end=END Set the end time of the conversion time\n");
2386 fprintf(fp
, " range to END (see the format of END below)\n");
2387 fprintf(fp
, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2388 fprintf(fp
, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2389 fprintf(fp
, " `]`) (see the format of BEGIN/END below)\n");
2391 fprintf(fp
, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2393 fprintf(fp
, " --debug-info Create an implicit\n");
2394 fprintf(fp
, " `filter.lttng-utils.debug-info` component\n");
2395 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2396 fprintf(fp
, " instead of `/usr/lib/debug`\n");
2397 fprintf(fp
, " --debug-info-full-path Show full debug info source and\n");
2398 fprintf(fp
, " binary paths instead of just names\n");
2399 fprintf(fp
, " --debug-info-target-prefix=DIR\n");
2400 fprintf(fp
, " Use directory DIR as a prefix when\n");
2401 fprintf(fp
, " looking up executables during debug\n");
2402 fprintf(fp
, " info analysis\n");
2404 fprintf(fp
, "Legacy options that still work:\n");
2406 fprintf(fp
, " -i, --input-format=(ctf | lttng-live)\n");
2407 fprintf(fp
, " `ctf`:\n");
2408 fprintf(fp
, " Create an implicit `source.ctf.fs`\n");
2409 fprintf(fp
, " component\n");
2410 fprintf(fp
, " `lttng-live`:\n");
2411 fprintf(fp
, " Create an implicit `source.ctf.lttng-live`\n");
2412 fprintf(fp
, " component\n");
2413 fprintf(fp
, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2414 fprintf(fp
, " `text`:\n");
2415 fprintf(fp
, " Create an implicit `sink.text.pretty`\n");
2416 fprintf(fp
, " component\n");
2417 fprintf(fp
, " `ctf`:\n");
2418 fprintf(fp
, " Create an implicit `sink.ctf.fs`\n");
2419 fprintf(fp
, " component\n");
2420 fprintf(fp
, " `dummy`:\n");
2421 fprintf(fp
, " Create an implicit `sink.utils.dummy`\n");
2422 fprintf(fp
, " component\n");
2423 fprintf(fp
, " `ctf-metadata`:\n");
2424 fprintf(fp
, " Query the `source.ctf.fs` component class\n");
2425 fprintf(fp
, " for metadata text and quit\n");
2427 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
2428 fprintf(fp
, "\n\n");
2429 fprintf(fp
, "Format of BEGIN and END\n");
2430 fprintf(fp
, "-----------------------\n");
2432 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2433 fprintf(fp
, "\n\n");
2434 print_expected_params_format(fp
);
2438 const struct bt_argpar_opt_descr convert_options
[] = {
2439 /* id, short_name, long_name, with_arg */
2440 { OPT_BEGIN
, 'b', "begin", true },
2441 { OPT_CLOCK_CYCLES
, '\0', "clock-cycles", false },
2442 { OPT_CLOCK_DATE
, '\0', "clock-date", false },
2443 { OPT_CLOCK_FORCE_CORRELATE
, '\0', "clock-force-correlate", false },
2444 { OPT_CLOCK_GMT
, '\0', "clock-gmt", false },
2445 { OPT_CLOCK_OFFSET
, '\0', "clock-offset", true },
2446 { OPT_CLOCK_OFFSET_NS
, '\0', "clock-offset-ns", true },
2447 { OPT_CLOCK_SECONDS
, '\0', "clock-seconds", false },
2448 { OPT_COLOR
, '\0', "color", true },
2449 { OPT_COMPONENT
, 'c', "component", true },
2450 { OPT_DEBUG
, 'd', "debug", false },
2451 { OPT_DEBUG_INFO_DIR
, '\0', "debug-info-dir", true },
2452 { OPT_DEBUG_INFO_FULL_PATH
, '\0', "debug-info-full-path", false },
2453 { OPT_DEBUG_INFO_TARGET_PREFIX
, '\0', "debug-info-target-prefix", true },
2454 { OPT_END
, 'e', "end", true },
2455 { OPT_FIELDS
, 'f', "fields", true },
2456 { OPT_HELP
, 'h', "help", false },
2457 { OPT_INPUT_FORMAT
, 'i', "input-format", true },
2458 { OPT_LOG_LEVEL
, 'l', "log-level", true },
2459 { OPT_NAMES
, 'n', "names", true },
2460 { OPT_DEBUG_INFO
, '\0', "debug-info", false },
2461 { OPT_NO_DELTA
, '\0', "no-delta", false },
2462 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
2463 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
2464 { OPT_OUTPUT
, 'w', "output", true },
2465 { OPT_OUTPUT_FORMAT
, 'o', "output-format", true },
2466 { OPT_PARAMS
, 'p', "params", true },
2467 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
2468 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
2469 { OPT_RUN_ARGS
, '\0', "run-args", false },
2470 { OPT_RUN_ARGS_0
, '\0', "run-args-0", false },
2471 { OPT_STREAM_INTERSECTION
, '\0', "stream-intersection", false },
2472 { OPT_TIMERANGE
, '\0', "timerange", true },
2473 { OPT_VERBOSE
, 'v', "verbose", false },
2474 BT_ARGPAR_OPT_DESCR_SENTINEL
2478 GString
*get_component_auto_name(const char *prefix
,
2479 const bt_value
*existing_names
)
2482 GString
*auto_name
= g_string_new(NULL
);
2485 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2489 if (!bt_value_map_has_entry(existing_names
, prefix
)) {
2490 g_string_assign(auto_name
, prefix
);
2495 g_string_printf(auto_name
, "%s-%d", prefix
, i
);
2497 } while (bt_value_map_has_entry(existing_names
, auto_name
->str
));
2503 struct implicit_component_args
{
2506 /* The component class name (e.g. src.ctf.fs). */
2509 /* The component instance name. */
2512 GString
*params_arg
;
2513 bt_value
*extra_params
;
2517 int assign_name_to_implicit_component(struct implicit_component_args
*args
,
2518 const char *prefix
, bt_value
*existing_names
,
2519 GList
**comp_names
, bool append_to_comp_names
)
2522 GString
*name
= NULL
;
2524 if (!args
->exists
) {
2528 name
= get_component_auto_name(prefix
,
2536 g_string_assign(args
->name_arg
, name
->str
);
2538 if (bt_value_map_insert_entry(existing_names
, name
->str
,
2540 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2545 if (append_to_comp_names
) {
2546 *comp_names
= g_list_append(*comp_names
, name
);
2552 g_string_free(name
, TRUE
);
2559 int append_run_args_for_implicit_component(
2560 struct implicit_component_args
*impl_args
,
2565 GString
*component_arg_for_run
= NULL
;
2567 if (!impl_args
->exists
) {
2571 component_arg_for_run
= g_string_new(NULL
);
2572 if (!component_arg_for_run
) {
2573 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2577 /* Build the full `name:type.plugin.cls`. */
2578 BT_ASSERT(!strchr(impl_args
->name_arg
->str
, '\\'));
2579 BT_ASSERT(!strchr(impl_args
->name_arg
->str
, ':'));
2580 g_string_printf(component_arg_for_run
, "%s:%s",
2581 impl_args
->name_arg
->str
, impl_args
->comp_arg
->str
);
2583 if (bt_value_array_append_string_element(run_args
, "--component")) {
2584 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2588 if (bt_value_array_append_string_element(run_args
,
2589 component_arg_for_run
->str
)) {
2590 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2594 if (impl_args
->params_arg
->len
> 0) {
2595 if (bt_value_array_append_string_element(run_args
, "--params")) {
2596 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2600 if (bt_value_array_append_string_element(run_args
,
2601 impl_args
->params_arg
->str
)) {
2602 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2607 for (i
= 0; i
< bt_value_array_get_size(impl_args
->extra_params
);
2609 const bt_value
*elem
;
2612 elem
= bt_value_array_borrow_element_by_index(impl_args
->extra_params
,
2618 BT_ASSERT(bt_value_is_string(elem
));
2619 arg
= bt_value_string_get(elem
);
2620 ret
= bt_value_array_append_string_element(run_args
, arg
);
2622 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2633 if (component_arg_for_run
) {
2634 g_string_free(component_arg_for_run
, TRUE
);
2640 /* Free the fields of a `struct implicit_component_args`. */
2643 void finalize_implicit_component_args(struct implicit_component_args
*args
)
2647 if (args
->comp_arg
) {
2648 g_string_free(args
->comp_arg
, TRUE
);
2651 if (args
->name_arg
) {
2652 g_string_free(args
->name_arg
, TRUE
);
2655 if (args
->params_arg
) {
2656 g_string_free(args
->params_arg
, TRUE
);
2659 bt_value_put_ref(args
->extra_params
);
2662 /* Destroy a dynamically-allocated `struct implicit_component_args`. */
2665 void destroy_implicit_component_args(struct implicit_component_args
*args
)
2667 finalize_implicit_component_args(args
);
2671 /* Initialize the fields of an already allocated `struct implicit_component_args`. */
2674 int init_implicit_component_args(struct implicit_component_args
*args
,
2675 const char *comp_arg
, bool exists
)
2679 args
->exists
= exists
;
2680 args
->comp_arg
= g_string_new(comp_arg
);
2681 args
->name_arg
= g_string_new(NULL
);
2682 args
->params_arg
= g_string_new(NULL
);
2683 args
->extra_params
= bt_value_array_create();
2685 if (!args
->comp_arg
|| !args
->name_arg
||
2686 !args
->params_arg
|| !args
->extra_params
) {
2688 finalize_implicit_component_args(args
);
2689 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2697 /* Dynamically allocate and initialize a `struct implicit_component_args`. */
2700 struct implicit_component_args
*create_implicit_component_args(
2701 const char *comp_arg
)
2703 struct implicit_component_args
*args
;
2706 args
= g_new(struct implicit_component_args
, 1);
2708 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2712 status
= init_implicit_component_args(args
, comp_arg
, true);
2723 void append_implicit_component_param(struct implicit_component_args
*args
,
2724 const char *key
, const char *value
)
2729 append_param_arg(args
->params_arg
, key
, value
);
2733 * Append the given parameter (`key=value`) to all component specifications
2734 * in `implicit_comp_args` (an array of `struct implicit_component_args *`)
2735 * which match `comp_arg`.
2737 * Return the number of matching components.
2741 int append_multiple_implicit_components_param(GPtrArray
*implicit_comp_args
,
2742 const char *comp_arg
, const char *key
, const char *value
)
2747 for (i
= 0; i
< implicit_comp_args
->len
; i
++) {
2748 struct implicit_component_args
*args
= implicit_comp_args
->pdata
[i
];
2750 if (strcmp(args
->comp_arg
->str
, comp_arg
) == 0) {
2751 append_implicit_component_param(args
, key
, value
);
2759 /* Escape value to make it suitable to use as a string parameter value. */
2761 gchar
*escape_string_value(const char *value
)
2766 ret
= g_string_new(NULL
);
2768 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2777 g_string_append_c(ret
, '\\');
2781 g_string_append_c(ret
, *in
);
2787 return g_string_free(ret
, FALSE
);
2791 int bt_value_to_cli_param_value_append(const bt_value
*value
, GString
*buf
)
2797 switch (bt_value_get_type(value
)) {
2798 case BT_VALUE_TYPE_STRING
:
2800 const char *str_value
= bt_value_string_get(value
);
2801 gchar
*escaped_str_value
;
2803 escaped_str_value
= escape_string_value(str_value
);
2804 if (!escaped_str_value
) {
2808 g_string_append_printf(buf
, "\"%s\"", escaped_str_value
);
2810 g_free(escaped_str_value
);
2813 case BT_VALUE_TYPE_ARRAY
: {
2814 g_string_append_c(buf
, '[');
2815 uint64_t sz
= bt_value_array_get_size(value
);
2816 for (uint64_t i
= 0; i
< sz
; i
++) {
2817 const bt_value
*item
;
2821 g_string_append(buf
, ", ");
2824 item
= bt_value_array_borrow_element_by_index_const(
2826 ret
= bt_value_to_cli_param_value_append(item
, buf
);
2832 g_string_append_c(buf
, ']');
2846 * Convert `value` to its equivalent representation as a command line parameter
2851 gchar
*bt_value_to_cli_param_value(bt_value
*value
)
2854 gchar
*result
= NULL
;
2856 buf
= g_string_new(NULL
);
2858 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2862 if (bt_value_to_cli_param_value_append(value
, buf
)) {
2866 result
= g_string_free(buf
, FALSE
);
2873 g_string_free(buf
, TRUE
);
2881 int append_parameter_to_args(bt_value
*args
, const char *key
, bt_value
*value
)
2884 BT_ASSERT(bt_value_get_type(args
) == BT_VALUE_TYPE_ARRAY
);
2889 gchar
*str_value
= NULL
;
2890 GString
*parameter
= NULL
;
2892 if (bt_value_array_append_string_element(args
, "--params")) {
2893 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2898 str_value
= bt_value_to_cli_param_value(value
);
2904 parameter
= g_string_new(NULL
);
2906 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2911 g_string_printf(parameter
, "%s=%s", key
, str_value
);
2913 if (bt_value_array_append_string_element(args
, parameter
->str
)) {
2914 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2921 g_string_free(parameter
, TRUE
);
2934 int append_string_parameter_to_args(bt_value
*args
, const char *key
, const char *value
)
2936 bt_value
*str_value
;
2939 str_value
= bt_value_string_create_init(value
);
2942 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2947 ret
= append_parameter_to_args(args
, key
, str_value
);
2950 BT_VALUE_PUT_REF_AND_RESET(str_value
);
2955 int append_implicit_component_extra_param(struct implicit_component_args
*args
,
2956 const char *key
, const char *value
)
2958 return append_string_parameter_to_args(args
->extra_params
, key
, value
);
2962 * Escapes `.`, `:`, and `\` of `input` with `\`.
2965 GString
*escape_dot_colon(const char *input
)
2967 GString
*output
= g_string_new(NULL
);
2971 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2975 for (ch
= input
; *ch
!= '\0'; ch
++) {
2976 if (*ch
== '\\' || *ch
== '.' || *ch
== ':') {
2977 g_string_append_c(output
, '\\');
2980 g_string_append_c(output
, *ch
);
2988 * Appends a --connect option to a list of arguments. `upstream_name`
2989 * and `downstream_name` are escaped with escape_dot_colon() in this
2993 int append_connect_arg(bt_value
*run_args
,
2994 const char *upstream_name
, const char *downstream_name
)
2997 GString
*e_upstream_name
= escape_dot_colon(upstream_name
);
2998 GString
*e_downstream_name
= escape_dot_colon(downstream_name
);
2999 GString
*arg
= g_string_new(NULL
);
3001 if (!e_upstream_name
|| !e_downstream_name
|| !arg
) {
3002 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3007 ret
= bt_value_array_append_string_element(run_args
, "--connect");
3009 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3014 g_string_append(arg
, e_upstream_name
->str
);
3015 g_string_append_c(arg
, ':');
3016 g_string_append(arg
, e_downstream_name
->str
);
3017 ret
= bt_value_array_append_string_element(run_args
, arg
->str
);
3019 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3026 g_string_free(arg
, TRUE
);
3029 if (e_upstream_name
) {
3030 g_string_free(e_upstream_name
, TRUE
);
3033 if (e_downstream_name
) {
3034 g_string_free(e_downstream_name
, TRUE
);
3041 * Appends the run command's --connect options for the convert command.
3044 int convert_auto_connect(bt_value
*run_args
,
3045 GList
*source_names
, GList
*filter_names
,
3049 GList
*source_at
= source_names
;
3050 GList
*filter_at
= filter_names
;
3052 GList
*sink_at
= sink_names
;
3054 BT_ASSERT(source_names
);
3055 BT_ASSERT(filter_names
);
3056 BT_ASSERT(sink_names
);
3058 /* Connect all sources to the first filter */
3059 for (source_at
= source_names
; source_at
; source_at
= g_list_next(source_at
)) {
3060 GString
*source_name
= source_at
->data
;
3061 GString
*filter_name
= filter_at
->data
;
3063 ret
= append_connect_arg(run_args
, source_name
->str
,
3070 filter_prev
= filter_at
;
3071 filter_at
= g_list_next(filter_at
);
3073 /* Connect remaining filters */
3074 for (; filter_at
; filter_prev
= filter_at
, filter_at
= g_list_next(filter_at
)) {
3075 GString
*filter_name
= filter_at
->data
;
3076 GString
*filter_prev_name
= filter_prev
->data
;
3078 ret
= append_connect_arg(run_args
, filter_prev_name
->str
,
3085 /* Connect last filter to all sinks */
3086 for (sink_at
= sink_names
; sink_at
; sink_at
= g_list_next(sink_at
)) {
3087 GString
*filter_name
= filter_prev
->data
;
3088 GString
*sink_name
= sink_at
->data
;
3090 ret
= append_connect_arg(run_args
, filter_name
->str
,
3107 int split_timerange(const char *arg
, char **begin
, char **end
)
3110 const char *ch
= arg
;
3112 GString
*g_begin
= NULL
;
3113 GString
*g_end
= NULL
;
3121 g_begin
= bt_common_string_until(ch
, "", ",", &end_pos
);
3122 if (!g_begin
|| ch
[end_pos
] != ',' || g_begin
->len
== 0) {
3128 g_end
= bt_common_string_until(ch
, "", "]", &end_pos
);
3129 if (!g_end
|| g_end
->len
== 0) {
3135 *begin
= g_begin
->str
;
3137 g_string_free(g_begin
, FALSE
);
3138 g_string_free(g_end
, FALSE
);
3148 g_string_free(g_begin
, TRUE
);
3152 g_string_free(g_end
, TRUE
);
3159 int g_list_prepend_gstring(GList
**list
, const char *string
)
3162 GString
*gs
= g_string_new(string
);
3167 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3171 *list
= g_list_prepend(*list
, gs
);
3178 * Create `struct implicit_component_args` structures for each of the source
3179 * components we identified. Add them to `component_args`.
3181 * `leftover_params` is an array where each element is an array of strings
3182 * containing all the arguments to `--params` that apply to the leftover at the
3183 * same index. For example, if, for a leftover, the following `--params`
3186 * --params=a=2 --params=b=3,c=4
3188 * its entry in `leftover_params` would contain
3190 * ["a=2", "b=3,c=4"]
3194 int create_implicit_component_args_from_auto_discovered_sources(
3195 const struct auto_source_discovery
*auto_disc
,
3196 const bt_value
*leftover_params
,
3197 GPtrArray
*component_args
)
3199 gchar
*cc_name
= NULL
;
3200 struct implicit_component_args
*comp
= NULL
;
3204 len
= auto_disc
->results
->len
;
3206 for (i
= 0; i
< len
; i
++) {
3207 struct auto_source_discovery_result
*res
=
3208 g_ptr_array_index(auto_disc
->results
, i
);
3209 uint64_t orig_indices_i
, orig_indices_count
;
3212 cc_name
= g_strdup_printf("source.%s.%s", res
->plugin_name
, res
->source_cc_name
);
3214 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3218 comp
= create_implicit_component_args(cc_name
);
3224 * Append parameters of all the leftovers that contributed to
3225 * this component instance coming into existence.
3227 orig_indices_count
= bt_value_array_get_size(res
->original_input_indices
);
3228 for (orig_indices_i
= 0; orig_indices_i
< orig_indices_count
; orig_indices_i
++) {
3229 const bt_value
*orig_idx_value
=
3230 bt_value_array_borrow_element_by_index(
3231 res
->original_input_indices
, orig_indices_i
);
3232 uint64_t orig_idx
= bt_value_integer_unsigned_get(orig_idx_value
);
3233 const bt_value
*params_array
=
3234 bt_value_array_borrow_element_by_index_const(
3235 leftover_params
, orig_idx
);
3236 uint64_t params_i
, params_count
;
3238 params_count
= bt_value_array_get_size(params_array
);
3239 for (params_i
= 0; params_i
< params_count
; params_i
++) {
3240 const bt_value
*params_value
=
3241 bt_value_array_borrow_element_by_index_const(
3242 params_array
, params_i
);
3243 const char *params
= bt_value_string_get(params_value
);
3244 bt_value_array_append_element_status append_status
;
3246 append_status
= bt_value_array_append_string_element(
3247 comp
->extra_params
, "--params");
3248 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3249 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3253 append_status
= bt_value_array_append_string_element(
3254 comp
->extra_params
, params
);
3255 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3256 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3262 status
= append_parameter_to_args(comp
->extra_params
, "inputs", res
->inputs
);
3267 g_ptr_array_add(component_args
, comp
);
3281 destroy_implicit_component_args(comp
);
3288 * As we iterate the arguments to the convert command, this tracks what is the
3289 * type of the current item, to which some contextual options (e.g. --params)
3292 enum convert_current_item_type
{
3293 /* There is no current item. */
3294 CONVERT_CURRENT_ITEM_TYPE_NONE
,
3296 /* Current item is a component. */
3297 CONVERT_CURRENT_ITEM_TYPE_COMPONENT
,
3299 /* Current item is a leftover. */
3300 CONVERT_CURRENT_ITEM_TYPE_LEFTOVER
,
3304 * Creates a Babeltrace config object from the arguments of a convert
3307 * *retcode is set to the appropriate exit code to use.
3310 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3311 int *retcode
, bool force_omit_system_plugin_path
,
3312 bool force_omit_home_plugin_path
,
3313 const bt_value
*initial_plugin_paths
, int *default_log_level
)
3315 enum convert_current_item_type current_item_type
=
3316 CONVERT_CURRENT_ITEM_TYPE_NONE
;
3318 struct bt_config
*cfg
= NULL
;
3319 bool got_input_format_opt
= false;
3320 bool got_output_format_opt
= false;
3321 bool trimmer_has_begin
= false;
3322 bool trimmer_has_end
= false;
3323 bool stream_intersection_mode
= false;
3324 bool print_run_args
= false;
3325 bool print_run_args_0
= false;
3326 bool print_ctf_metadata
= false;
3327 bt_value
*run_args
= NULL
;
3328 bt_value
*all_names
= NULL
;
3329 GList
*source_names
= NULL
;
3330 GList
*filter_names
= NULL
;
3331 GList
*sink_names
= NULL
;
3332 bt_value
*leftovers
= NULL
;
3333 bt_value
*leftover_params
= NULL
;
3334 struct implicit_component_args implicit_ctf_output_args
= { 0 };
3335 struct implicit_component_args implicit_lttng_live_args
= { 0 };
3336 struct implicit_component_args implicit_dummy_args
= { 0 };
3337 struct implicit_component_args implicit_text_args
= { 0 };
3338 struct implicit_component_args implicit_debug_info_args
= { 0 };
3339 struct implicit_component_args implicit_muxer_args
= { 0 };
3340 struct implicit_component_args implicit_trimmer_args
= { 0 };
3341 bt_value
*plugin_paths
;
3342 char error_buf
[256] = { 0 };
3344 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
3345 char *output
= NULL
;
3346 struct auto_source_discovery auto_disc
= { NULL
};
3347 GString
*auto_disc_comp_name
= NULL
;
3348 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
3349 GString
*name_gstr
= NULL
;
3350 GString
*component_arg_for_run
= NULL
;
3353 * Array of `struct implicit_component_args *` created for the sources
3354 * we have auto-discovered.
3356 GPtrArray
*discovered_source_args
= NULL
;
3359 * If set, restrict automatic source discovery to this component class
3362 const char *auto_source_discovery_restrict_plugin_name
= NULL
;
3363 const char *auto_source_discovery_restrict_component_class_name
= NULL
;
3365 gchar
*ctf_fs_source_clock_class_offset_arg
= NULL
;
3366 gchar
*ctf_fs_source_clock_class_offset_ns_arg
= NULL
;
3368 (void) bt_value_copy(initial_plugin_paths
, &plugin_paths
);
3373 print_convert_usage(stdout
);
3378 if (init_implicit_component_args(&implicit_ctf_output_args
,
3379 "sink.ctf.fs", false)) {
3383 if (init_implicit_component_args(&implicit_lttng_live_args
,
3384 "source.ctf.lttng-live", false)) {
3388 if (init_implicit_component_args(&implicit_text_args
,
3389 "sink.text.pretty", false)) {
3393 if (init_implicit_component_args(&implicit_dummy_args
,
3394 "sink.utils.dummy", false)) {
3398 if (init_implicit_component_args(&implicit_debug_info_args
,
3399 "filter.lttng-utils.debug-info", false)) {
3403 if (init_implicit_component_args(&implicit_muxer_args
,
3404 "filter.utils.muxer", true)) {
3408 if (init_implicit_component_args(&implicit_trimmer_args
,
3409 "filter.utils.trimmer", false)) {
3413 all_names
= bt_value_map_create();
3415 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3419 run_args
= bt_value_array_create();
3421 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3425 component_arg_for_run
= g_string_new(NULL
);
3426 if (!component_arg_for_run
) {
3427 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3431 ret
= append_env_var_plugin_paths(plugin_paths
);
3436 leftovers
= bt_value_array_create();
3438 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3442 leftover_params
= bt_value_array_create();
3443 if (!leftover_params
) {
3444 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3448 if (auto_source_discovery_init(&auto_disc
) != 0) {
3452 discovered_source_args
=
3453 g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_implicit_component_args
);
3454 if (!discovered_source_args
) {
3455 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3459 auto_disc_comp_name
= g_string_new(NULL
);
3460 if (!auto_disc_comp_name
) {
3461 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3466 * First pass: collect all arguments which need to be passed
3467 * as is to the run command. This pass can also add --name
3468 * arguments if needed to automatically name unnamed component
3471 * Also it appends the plugin paths of --plugin-path to
3474 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, convert_options
, true);
3475 if (argpar_parse_ret
.error
) {
3476 BT_CLI_LOGE_APPEND_CAUSE(
3477 "While parsing `convert` command's command-line arguments: %s",
3478 argpar_parse_ret
.error
->str
);
3482 if (help_option_is_specified(&argpar_parse_ret
)) {
3483 print_convert_usage(stdout
);
3485 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
3489 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
3490 struct bt_argpar_item
*argpar_item
=
3491 g_ptr_array_index(argpar_parse_ret
.items
, i
);
3492 struct bt_argpar_item_opt
*argpar_item_opt
;
3494 char *plugin_name
= NULL
;
3495 char *comp_cls_name
= NULL
;
3498 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
3499 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
3500 arg
= argpar_item_opt
->arg
;
3502 switch (argpar_item_opt
->descr
->id
) {
3505 bt_component_class_type type
;
3507 current_item_type
= CONVERT_CURRENT_ITEM_TYPE_COMPONENT
;
3509 /* Parse the argument */
3510 plugin_comp_cls_names(arg
, &name
, &plugin_name
,
3511 &comp_cls_name
, &type
);
3512 if (!plugin_name
|| !comp_cls_name
) {
3513 BT_CLI_LOGE_APPEND_CAUSE(
3514 "Invalid format for --component option's argument:\n %s",
3521 * Name was given by the user, verify it isn't
3524 if (bt_value_map_has_entry(all_names
, name
)) {
3525 BT_CLI_LOGE_APPEND_CAUSE(
3526 "Duplicate component instance name:\n %s",
3531 name_gstr
= g_string_new(name
);
3533 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3537 g_string_assign(component_arg_for_run
, arg
);
3539 /* Name not given by user, generate one. */
3540 name_gstr
= get_component_auto_name(arg
, all_names
);
3545 g_string_printf(component_arg_for_run
, "%s:%s",
3546 name_gstr
->str
, arg
);
3549 if (bt_value_array_append_string_element(run_args
,
3551 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3555 if (bt_value_array_append_string_element(run_args
,
3556 component_arg_for_run
->str
)) {
3557 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3562 * Remember this name globally, for the uniqueness of
3563 * all component names.
3565 if (bt_value_map_insert_entry(all_names
,
3566 name_gstr
->str
, bt_value_null
)) {
3567 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3572 * Remember this name specifically for the type of the
3573 * component. This is to create connection arguments.
3575 * The list takes ownership of `name_gstr`.
3578 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
3579 source_names
= g_list_append(source_names
, name_gstr
);
3581 case BT_COMPONENT_CLASS_TYPE_FILTER
:
3582 filter_names
= g_list_append(filter_names
, name_gstr
);
3584 case BT_COMPONENT_CLASS_TYPE_SINK
:
3585 sink_names
= g_list_append(sink_names
, name_gstr
);
3594 free(comp_cls_name
);
3597 comp_cls_name
= NULL
;
3601 if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_COMPONENT
) {
3603 * The current item is a component (--component option),
3604 * pass it directly to the run args.
3606 if (bt_value_array_append_string_element(run_args
,
3608 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3612 if (bt_value_array_append_string_element(run_args
, arg
)) {
3613 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3616 } else if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_LEFTOVER
) {
3617 /* The current item is a leftover, record it in `leftover_params`. */
3619 uint64_t idx
= bt_value_array_get_size(leftover_params
) - 1;
3621 array
= bt_value_array_borrow_element_by_index(leftover_params
, idx
);
3622 bt_value_array_append_string_element(array
, arg
);
3624 BT_CLI_LOGE_APPEND_CAUSE(
3625 "No current component (--component option) or non-option argument of which to set parameters:\n %s",
3631 if (current_item_type
!= CONVERT_CURRENT_ITEM_TYPE_COMPONENT
) {
3632 BT_CLI_LOGE_APPEND_CAUSE(
3633 "No current component (--component option) to assign a log level to:\n %s",
3638 if (bt_value_array_append_string_element(run_args
, "--log-level")) {
3639 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3643 if (bt_value_array_append_string_element(run_args
, arg
)) {
3644 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3649 case OPT_OMIT_HOME_PLUGIN_PATH
:
3650 force_omit_home_plugin_path
= true;
3652 if (bt_value_array_append_string_element(run_args
,
3653 "--omit-home-plugin-path")) {
3654 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3658 case OPT_RETRY_DURATION
:
3659 if (bt_value_array_append_string_element(run_args
,
3660 "--retry-duration")) {
3661 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3665 if (bt_value_array_append_string_element(run_args
, arg
)) {
3666 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3670 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
3671 force_omit_system_plugin_path
= true;
3673 if (bt_value_array_append_string_element(run_args
,
3674 "--omit-system-plugin-path")) {
3675 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3679 case OPT_PLUGIN_PATH
:
3680 if (bt_config_append_plugin_paths_check_setuid_setgid(
3681 plugin_paths
, arg
)) {
3685 if (bt_value_array_append_string_element(run_args
,
3687 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3691 if (bt_value_array_append_string_element(run_args
, arg
)) {
3692 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3697 case OPT_CLOCK_CYCLES
:
3698 case OPT_CLOCK_DATE
:
3699 case OPT_CLOCK_FORCE_CORRELATE
:
3701 case OPT_CLOCK_OFFSET
:
3702 case OPT_CLOCK_OFFSET_NS
:
3703 case OPT_CLOCK_SECONDS
:
3706 case OPT_DEBUG_INFO
:
3707 case OPT_DEBUG_INFO_DIR
:
3708 case OPT_DEBUG_INFO_FULL_PATH
:
3709 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3712 case OPT_INPUT_FORMAT
:
3715 case OPT_OUTPUT_FORMAT
:
3718 case OPT_RUN_ARGS_0
:
3719 case OPT_STREAM_INTERSECTION
:
3722 /* Ignore in this pass */
3725 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
3726 argpar_item_opt
->descr
->id
);
3729 } else if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
3730 struct bt_argpar_item_non_opt
*argpar_item_non_opt
;
3731 bt_value_array_append_element_status append_status
;
3733 current_item_type
= CONVERT_CURRENT_ITEM_TYPE_LEFTOVER
;
3735 argpar_item_non_opt
= (struct bt_argpar_item_non_opt
*) argpar_item
;
3737 append_status
= bt_value_array_append_string_element(leftovers
,
3738 argpar_item_non_opt
->arg
);
3739 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3740 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3744 append_status
= bt_value_array_append_empty_array_element(leftover_params
);
3745 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3746 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3755 * Second pass: transform the convert-specific options and
3756 * arguments into implicit component instances for the run
3759 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
3760 struct bt_argpar_item
*argpar_item
=
3761 g_ptr_array_index(argpar_parse_ret
.items
, i
);
3762 struct bt_argpar_item_opt
*argpar_item_opt
;
3765 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
3769 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
3770 arg
= argpar_item_opt
->arg
;
3772 switch (argpar_item_opt
->descr
->id
) {
3774 if (trimmer_has_begin
) {
3775 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
3780 trimmer_has_begin
= true;
3781 ret
= append_implicit_component_extra_param(
3782 &implicit_trimmer_args
, "begin", arg
);
3783 implicit_trimmer_args
.exists
= true;
3789 if (trimmer_has_end
) {
3790 printf("At --end option: --end or --timerange option already specified\n %s\n",
3795 trimmer_has_end
= true;
3796 ret
= append_implicit_component_extra_param(
3797 &implicit_trimmer_args
, "end", arg
);
3798 implicit_trimmer_args
.exists
= true;
3808 if (trimmer_has_begin
|| trimmer_has_end
) {
3809 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
3814 ret
= split_timerange(arg
, &begin
, &end
);
3816 BT_CLI_LOGE_APPEND_CAUSE("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s",
3821 ret
= append_implicit_component_extra_param(
3822 &implicit_trimmer_args
, "begin", begin
);
3823 ret
|= append_implicit_component_extra_param(
3824 &implicit_trimmer_args
, "end", end
);
3825 implicit_trimmer_args
.exists
= true;
3833 case OPT_CLOCK_CYCLES
:
3834 append_implicit_component_param(
3835 &implicit_text_args
, "clock-cycles", "yes");
3836 implicit_text_args
.exists
= true;
3838 case OPT_CLOCK_DATE
:
3839 append_implicit_component_param(
3840 &implicit_text_args
, "clock-date", "yes");
3841 implicit_text_args
.exists
= true;
3843 case OPT_CLOCK_FORCE_CORRELATE
:
3844 append_implicit_component_param(
3845 &implicit_muxer_args
,
3846 "assume-absolute-clock-classes", "yes");
3849 append_implicit_component_param(
3850 &implicit_text_args
, "clock-gmt", "yes");
3851 append_implicit_component_param(
3852 &implicit_trimmer_args
, "gmt", "yes");
3853 implicit_text_args
.exists
= true;
3855 case OPT_CLOCK_OFFSET
:
3856 if (ctf_fs_source_clock_class_offset_arg
) {
3857 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset option\n");
3861 ctf_fs_source_clock_class_offset_arg
= g_strdup(arg
);
3862 if (!ctf_fs_source_clock_class_offset_arg
) {
3863 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3867 case OPT_CLOCK_OFFSET_NS
:
3868 if (ctf_fs_source_clock_class_offset_ns_arg
) {
3869 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset-ns option\n");
3873 ctf_fs_source_clock_class_offset_ns_arg
= g_strdup(arg
);
3874 if (!ctf_fs_source_clock_class_offset_ns_arg
) {
3875 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3879 case OPT_CLOCK_SECONDS
:
3880 append_implicit_component_param(
3881 &implicit_text_args
, "clock-seconds", "yes");
3882 implicit_text_args
.exists
= true;
3885 implicit_text_args
.exists
= true;
3886 ret
= append_implicit_component_extra_param(
3887 &implicit_text_args
, "color", arg
);
3892 case OPT_DEBUG_INFO
:
3893 implicit_debug_info_args
.exists
= true;
3895 case OPT_DEBUG_INFO_DIR
:
3896 implicit_debug_info_args
.exists
= true;
3897 ret
= append_implicit_component_extra_param(
3898 &implicit_debug_info_args
, "debug-info-dir", arg
);
3903 case OPT_DEBUG_INFO_FULL_PATH
:
3904 implicit_debug_info_args
.exists
= true;
3905 append_implicit_component_param(
3906 &implicit_debug_info_args
, "full-path", "yes");
3908 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3909 implicit_debug_info_args
.exists
= true;
3910 ret
= append_implicit_component_extra_param(
3911 &implicit_debug_info_args
,
3912 "target-prefix", arg
);
3919 bt_value
*fields
= fields_from_arg(arg
);
3925 implicit_text_args
.exists
= true;
3926 ret
= insert_flat_params_from_array(
3927 implicit_text_args
.params_arg
,
3929 bt_value_put_ref(fields
);
3937 bt_value
*names
= names_from_arg(arg
);
3943 implicit_text_args
.exists
= true;
3944 ret
= insert_flat_params_from_array(
3945 implicit_text_args
.params_arg
,
3947 bt_value_put_ref(names
);
3954 append_implicit_component_param(
3955 &implicit_text_args
, "no-delta", "yes");
3956 implicit_text_args
.exists
= true;
3958 case OPT_INPUT_FORMAT
:
3959 if (got_input_format_opt
) {
3960 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --input-format option.");
3964 got_input_format_opt
= true;
3966 if (strcmp(arg
, "ctf") == 0) {
3967 auto_source_discovery_restrict_plugin_name
= "ctf";
3968 auto_source_discovery_restrict_component_class_name
= "fs";
3969 } else if (strcmp(arg
, "lttng-live") == 0) {
3970 auto_source_discovery_restrict_plugin_name
= "ctf";
3971 auto_source_discovery_restrict_component_class_name
= "lttng-live";
3972 implicit_lttng_live_args
.exists
= true;
3974 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy input format:\n %s",
3979 case OPT_OUTPUT_FORMAT
:
3980 if (got_output_format_opt
) {
3981 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output-format option.");
3985 got_output_format_opt
= true;
3987 if (strcmp(arg
, "text") == 0) {
3988 implicit_text_args
.exists
= true;
3989 } else if (strcmp(arg
, "ctf") == 0) {
3990 implicit_ctf_output_args
.exists
= true;
3991 } else if (strcmp(arg
, "dummy") == 0) {
3992 implicit_dummy_args
.exists
= true;
3993 } else if (strcmp(arg
, "ctf-metadata") == 0) {
3994 print_ctf_metadata
= true;
3996 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy output format:\n %s",
4003 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output option");
4007 output
= strdup(arg
);
4009 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4014 if (print_run_args_0
) {
4015 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
4019 print_run_args
= true;
4021 case OPT_RUN_ARGS_0
:
4022 if (print_run_args
) {
4023 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
4027 print_run_args_0
= true;
4029 case OPT_STREAM_INTERSECTION
:
4031 * Applies to all traces implementing the
4032 * babeltrace.trace-info query.
4034 stream_intersection_mode
= true;
4037 if (*default_log_level
!= BT_LOG_TRACE
&&
4038 *default_log_level
!= BT_LOG_DEBUG
) {
4039 *default_log_level
= BT_LOG_INFO
;
4043 *default_log_level
= BT_LOG_TRACE
;
4051 * Legacy behaviour: --verbose used to make the `text` output
4052 * format print more information. --verbose is now equivalent to
4053 * the INFO log level, which is why we compare to `BT_LOG_INFO`
4056 if (*default_log_level
== BT_LOG_INFO
) {
4057 append_implicit_component_param(&implicit_text_args
,
4062 * Append home and system plugin paths now that we possibly got
4065 if (append_home_and_system_plugin_paths(plugin_paths
,
4066 force_omit_system_plugin_path
,
4067 force_omit_home_plugin_path
)) {
4071 /* Print CTF metadata or print LTTng live sessions */
4072 if (print_ctf_metadata
) {
4073 const bt_value
*bt_val_leftover
;
4075 if (bt_value_array_is_empty(leftovers
)) {
4076 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf-metadata specified without a path.");
4080 if (bt_value_array_get_size(leftovers
) > 1) {
4081 BT_CLI_LOGE_APPEND_CAUSE("Too many paths specified for --output-format=ctf-metadata.");
4085 cfg
= bt_config_print_ctf_metadata_create(plugin_paths
);
4090 bt_val_leftover
= bt_value_array_borrow_element_by_index_const(leftovers
, 0);
4091 g_string_assign(cfg
->cmd_data
.print_ctf_metadata
.path
,
4092 bt_value_string_get(bt_val_leftover
));
4096 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
4104 * If -o ctf was specified, make sure an output path (--output)
4105 * was also specified. --output does not imply -o ctf because
4106 * it's also used for the default, implicit -o text if -o ctf
4109 if (implicit_ctf_output_args
.exists
) {
4111 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf specified without --output (trace output path).");
4116 * At this point we know that -o ctf AND --output were
4117 * specified. Make sure that no options were specified
4118 * which would imply -o text because --output would be
4119 * ambiguous in this case. For example, this is wrong:
4121 * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace
4123 * because --names=all implies -o text, and --output
4124 * could apply to both the sink.text.pretty and
4125 * sink.ctf.fs implicit components.
4127 if (implicit_text_args
.exists
) {
4128 BT_CLI_LOGE_APPEND_CAUSE("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text.");
4134 * If -o dummy and -o ctf were not specified, and if there are
4135 * no explicit sink components, then use an implicit
4136 * `sink.text.pretty` component.
4138 if (!implicit_dummy_args
.exists
&& !implicit_ctf_output_args
.exists
&&
4140 implicit_text_args
.exists
= true;
4144 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4145 * `path` parameter if --output was specified.
4148 if (implicit_text_args
.exists
) {
4149 append_implicit_component_extra_param(&implicit_text_args
,
4151 } else if (implicit_ctf_output_args
.exists
) {
4152 append_implicit_component_extra_param(&implicit_ctf_output_args
,
4157 /* Decide where the leftover argument(s) go */
4158 if (bt_value_array_get_size(leftovers
) > 0) {
4159 if (implicit_lttng_live_args
.exists
) {
4160 const bt_value
*bt_val_leftover
;
4162 if (bt_value_array_get_size(leftovers
) > 1) {
4163 BT_CLI_LOGE_APPEND_CAUSE("Too many URLs specified for --input-format=lttng-live.");
4167 bt_val_leftover
= bt_value_array_borrow_element_by_index_const(leftovers
, 0);
4168 lttng_live_url_parts
=
4169 bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_leftover
),
4170 error_buf
, sizeof(error_buf
));
4171 if (!lttng_live_url_parts
.proto
) {
4172 BT_CLI_LOGE_APPEND_CAUSE("Invalid LTTng live URL format: %s.",
4177 if (!lttng_live_url_parts
.session_name
) {
4178 /* Print LTTng live sessions */
4179 cfg
= bt_config_print_lttng_live_sessions_create(
4185 g_string_assign(cfg
->cmd_data
.print_lttng_live_sessions
.url
,
4186 bt_value_string_get(bt_val_leftover
));
4190 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
4197 ret
= append_implicit_component_extra_param(
4198 &implicit_lttng_live_args
, "url",
4199 bt_value_string_get(bt_val_leftover
));
4204 ret
= append_implicit_component_extra_param(
4205 &implicit_lttng_live_args
,
4206 "session-not-found-action", "end");
4213 status
= auto_discover_source_components(plugin_paths
, leftovers
,
4214 auto_source_discovery_restrict_plugin_name
,
4215 auto_source_discovery_restrict_component_class_name
,
4216 *default_log_level
>= 0 ? *default_log_level
: cli_default_log_level
,
4223 status
= create_implicit_component_args_from_auto_discovered_sources(
4224 &auto_disc
, leftover_params
, discovered_source_args
);
4231 /* If --clock-offset was given, apply it to any src.ctf.fs component. */
4232 if (ctf_fs_source_clock_class_offset_arg
) {
4235 n
= append_multiple_implicit_components_param(
4236 discovered_source_args
, "source.ctf.fs", "clock-class-offset-s",
4237 ctf_fs_source_clock_class_offset_arg
);
4240 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset specified, but no source.ctf.fs component instantiated.");
4245 /* If --clock-offset-ns was given, apply it to any src.ctf.fs component. */
4246 if (ctf_fs_source_clock_class_offset_ns_arg
) {
4249 n
= append_multiple_implicit_components_param(
4250 discovered_source_args
, "source.ctf.fs", "clock-class-offset-ns",
4251 ctf_fs_source_clock_class_offset_ns_arg
);
4254 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset-ns specified, but no source.ctf.fs component instantiated.");
4260 * If the implicit `source.ctf.lttng-live` component exists, make sure
4261 * there's at least one leftover (which is the URL).
4263 if (implicit_lttng_live_args
.exists
&& bt_value_array_is_empty(leftovers
)) {
4264 BT_CLI_LOGE_APPEND_CAUSE("Missing URL for implicit `%s` component.",
4265 implicit_lttng_live_args
.comp_arg
->str
);
4269 /* Assign names to implicit components */
4270 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4271 struct implicit_component_args
*args
;
4274 args
= discovered_source_args
->pdata
[i
];
4276 g_string_printf(auto_disc_comp_name
, "auto-disc-%s", args
->comp_arg
->str
);
4278 /* Give it a name like `auto-disc-src-ctf-fs`. */
4279 for (j
= 0; j
< auto_disc_comp_name
->len
; j
++) {
4280 if (auto_disc_comp_name
->str
[j
] == '.') {
4281 auto_disc_comp_name
->str
[j
] = '-';
4285 ret
= assign_name_to_implicit_component(args
,
4286 auto_disc_comp_name
->str
, all_names
, &source_names
, true);
4292 ret
= assign_name_to_implicit_component(&implicit_lttng_live_args
,
4293 "lttng-live", all_names
, &source_names
, true);
4298 ret
= assign_name_to_implicit_component(&implicit_text_args
,
4299 "pretty", all_names
, &sink_names
, true);
4304 ret
= assign_name_to_implicit_component(&implicit_ctf_output_args
,
4305 "sink-ctf-fs", all_names
, &sink_names
, true);
4310 ret
= assign_name_to_implicit_component(&implicit_dummy_args
,
4311 "dummy", all_names
, &sink_names
, true);
4316 ret
= assign_name_to_implicit_component(&implicit_muxer_args
,
4317 "muxer", all_names
, NULL
, false);
4322 ret
= assign_name_to_implicit_component(&implicit_trimmer_args
,
4323 "trimmer", all_names
, NULL
, false);
4328 ret
= assign_name_to_implicit_component(&implicit_debug_info_args
,
4329 "debug-info", all_names
, NULL
, false);
4334 /* Make sure there's at least one source and one sink */
4335 if (!source_names
) {
4336 BT_CLI_LOGE_APPEND_CAUSE("No source component.");
4341 BT_CLI_LOGE_APPEND_CAUSE("No sink component.");
4346 * Prepend the muxer, the trimmer, and the debug info to the
4347 * filter chain so that we have:
4349 * sources -> muxer -> [trimmer] -> [debug info] ->
4350 * [user filters] -> sinks
4352 if (implicit_debug_info_args
.exists
) {
4353 if (g_list_prepend_gstring(&filter_names
,
4354 implicit_debug_info_args
.name_arg
->str
)) {
4359 if (implicit_trimmer_args
.exists
) {
4360 if (g_list_prepend_gstring(&filter_names
,
4361 implicit_trimmer_args
.name_arg
->str
)) {
4366 if (g_list_prepend_gstring(&filter_names
,
4367 implicit_muxer_args
.name_arg
->str
)) {
4372 * Append the equivalent run arguments for the implicit
4375 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4376 struct implicit_component_args
*args
=
4377 discovered_source_args
->pdata
[i
];
4379 ret
= append_run_args_for_implicit_component(args
, run_args
);
4385 ret
= append_run_args_for_implicit_component(&implicit_lttng_live_args
,
4391 ret
= append_run_args_for_implicit_component(&implicit_text_args
,
4397 ret
= append_run_args_for_implicit_component(&implicit_ctf_output_args
,
4403 ret
= append_run_args_for_implicit_component(&implicit_dummy_args
,
4409 ret
= append_run_args_for_implicit_component(&implicit_muxer_args
,
4415 ret
= append_run_args_for_implicit_component(&implicit_trimmer_args
,
4421 ret
= append_run_args_for_implicit_component(&implicit_debug_info_args
,
4427 /* Auto-connect components */
4428 ret
= convert_auto_connect(run_args
, source_names
, filter_names
,
4431 BT_CLI_LOGE_APPEND_CAUSE("Cannot auto-connect components.");
4436 * We have all the run command arguments now. Depending on
4437 * --run-args, we pass this to the run command or print them
4440 if (print_run_args
|| print_run_args_0
) {
4441 if (stream_intersection_mode
) {
4442 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --stream-intersection with --run-args or --run-args-0.");
4446 for (i
= 0; i
< bt_value_array_get_size(run_args
); i
++) {
4447 const bt_value
*arg_value
=
4448 bt_value_array_borrow_element_by_index(run_args
,
4451 GString
*quoted
= NULL
;
4452 const char *arg_to_print
;
4454 BT_ASSERT(arg_value
);
4455 arg
= bt_value_string_get(arg_value
);
4457 if (print_run_args
) {
4458 quoted
= bt_common_shell_quote(arg
, true);
4463 arg_to_print
= quoted
->str
;
4468 printf("%s", arg_to_print
);
4471 g_string_free(quoted
, TRUE
);
4474 if (i
< bt_value_array_get_size(run_args
) - 1) {
4475 if (print_run_args
) {
4484 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4489 * If the log level is still unset at this point, set it to
4490 * the program's default.
4492 if (*default_log_level
< 0) {
4493 *default_log_level
= cli_default_log_level
;
4496 cfg
= bt_config_run_from_args_array(run_args
, retcode
,
4497 force_omit_system_plugin_path
,
4498 force_omit_home_plugin_path
,
4499 initial_plugin_paths
, *default_log_level
);
4504 cfg
->cmd_data
.run
.stream_intersection_mode
= stream_intersection_mode
;
4509 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4513 * If the log level is still unset at this point, set it to
4514 * the program's default.
4516 if (*default_log_level
< 0) {
4517 *default_log_level
= cli_default_log_level
;
4520 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
4524 if (component_arg_for_run
) {
4525 g_string_free(component_arg_for_run
, TRUE
);
4529 g_string_free(name_gstr
, TRUE
);
4532 bt_value_put_ref(run_args
);
4533 bt_value_put_ref(all_names
);
4534 destroy_glist_of_gstring(source_names
);
4535 destroy_glist_of_gstring(filter_names
);
4536 destroy_glist_of_gstring(sink_names
);
4537 bt_value_put_ref(leftovers
);
4538 finalize_implicit_component_args(&implicit_ctf_output_args
);
4539 finalize_implicit_component_args(&implicit_lttng_live_args
);
4540 finalize_implicit_component_args(&implicit_dummy_args
);
4541 finalize_implicit_component_args(&implicit_text_args
);
4542 finalize_implicit_component_args(&implicit_debug_info_args
);
4543 finalize_implicit_component_args(&implicit_muxer_args
);
4544 finalize_implicit_component_args(&implicit_trimmer_args
);
4545 bt_value_put_ref(plugin_paths
);
4546 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
4547 auto_source_discovery_fini(&auto_disc
);
4549 if (discovered_source_args
) {
4550 g_ptr_array_free(discovered_source_args
, TRUE
);
4553 g_free(ctf_fs_source_clock_class_offset_arg
);
4554 g_free(ctf_fs_source_clock_class_offset_ns_arg
);
4556 if (auto_disc_comp_name
) {
4557 g_string_free(auto_disc_comp_name
, TRUE
);
4564 * Prints the Babeltrace 2.x general usage.
4567 void print_gen_usage(FILE *fp
)
4569 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4571 fprintf(fp
, "General options:\n");
4573 fprintf(fp
, " -d, --debug Enable debug mode (same as --log-level=V)\n");
4574 fprintf(fp
, " -h, --help Show this help and quit\n");
4575 fprintf(fp
, " -l, --log-level=LVL Set the default log level to LVL (`N`, `V`, `D`,\n");
4576 fprintf(fp
, " `I`, `W` (default), `E`, or `F`)\n");
4577 fprintf(fp
, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4578 fprintf(fp
, " -V, --version Show version and quit\n");
4580 fprintf(fp
, "Available commands:\n");
4582 fprintf(fp
, " convert Convert and trim traces (default)\n");
4583 fprintf(fp
, " help Get help for a plugin or a component class\n");
4584 fprintf(fp
, " list-plugins List available plugins and their content\n");
4585 fprintf(fp
, " query Query objects from a component class\n");
4586 fprintf(fp
, " run Build a processing graph and run it\n");
4588 fprintf(fp
, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n");
4591 struct bt_config
*bt_config_cli_args_create(int argc
, const char *argv
[],
4592 int *retcode
, bool force_omit_system_plugin_path
,
4593 bool force_omit_home_plugin_path
,
4594 const bt_value
*initial_plugin_paths
)
4596 struct bt_config
*config
= NULL
;
4599 const char **top_level_argv
;
4600 int command_argc
= -1;
4601 const char **command_argv
= NULL
;
4602 const char *command_name
= NULL
;
4603 int default_log_level
= -1;
4604 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
4606 /* Top-level option descriptions. */
4607 static const struct bt_argpar_opt_descr descrs
[] = {
4608 { OPT_DEBUG
, 'd', "debug", false },
4609 { OPT_HELP
, 'h', "help", false },
4610 { OPT_LOG_LEVEL
, 'l', "log-level", true },
4611 { OPT_VERBOSE
, 'v', "verbose", false },
4612 { OPT_VERSION
, 'V', "version", false},
4613 BT_ARGPAR_OPT_DESCR_SENTINEL
4617 COMMAND_TYPE_NONE
= -1,
4618 COMMAND_TYPE_RUN
= 0,
4619 COMMAND_TYPE_CONVERT
,
4620 COMMAND_TYPE_LIST_PLUGINS
,
4623 } command_type
= COMMAND_TYPE_NONE
;
4627 if (!initial_plugin_paths
) {
4628 initial_plugin_paths
= bt_value_array_create();
4629 if (!initial_plugin_paths
) {
4634 bt_value_get_ref(initial_plugin_paths
);
4640 print_gen_usage(stdout
);
4644 /* Skip first argument, the name of the program. */
4645 top_level_argc
= argc
- 1;
4646 top_level_argv
= argv
+ 1;
4647 argpar_parse_ret
= bt_argpar_parse(top_level_argc
, top_level_argv
,
4650 if (argpar_parse_ret
.error
) {
4651 BT_CLI_LOGE_APPEND_CAUSE(
4652 "While parsing command-line arguments: %s",
4653 argpar_parse_ret
.error
->str
);
4657 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
4658 struct bt_argpar_item
*item
;
4660 item
= g_ptr_array_index(argpar_parse_ret
.items
, i
);
4662 if (item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
4663 struct bt_argpar_item_opt
*item_opt
=
4664 (struct bt_argpar_item_opt
*) item
;
4666 switch (item_opt
->descr
->id
) {
4668 default_log_level
= BT_LOG_TRACE
;
4672 * Legacy: do not override a previous
4673 * --debug because --verbose and --debug
4674 * can be specified together (in this
4675 * case we want the lowest log level to
4678 default_log_level
= BT_LOG_INFO
;
4682 bt_log_get_level_from_string(item_opt
->arg
);
4683 if (default_log_level
< 0) {
4684 BT_CLI_LOGE_APPEND_CAUSE(
4685 "Invalid argument for --log-level option:\n %s",
4694 print_gen_usage(stdout
);
4697 } else if (item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
4698 struct bt_argpar_item_non_opt
*item_non_opt
=
4699 (struct bt_argpar_item_non_opt
*) item
;
4701 * First unknown argument: is it a known command
4705 top_level_argc
- item_non_opt
->orig_index
- 1;
4707 &top_level_argv
[item_non_opt
->orig_index
+ 1];
4709 if (strcmp(item_non_opt
->arg
, "convert") == 0) {
4710 command_type
= COMMAND_TYPE_CONVERT
;
4711 } else if (strcmp(item_non_opt
->arg
, "list-plugins") == 0) {
4712 command_type
= COMMAND_TYPE_LIST_PLUGINS
;
4713 } else if (strcmp(item_non_opt
->arg
, "help") == 0) {
4714 command_type
= COMMAND_TYPE_HELP
;
4715 } else if (strcmp(item_non_opt
->arg
, "query") == 0) {
4716 command_type
= COMMAND_TYPE_QUERY
;
4717 } else if (strcmp(item_non_opt
->arg
, "run") == 0) {
4718 command_type
= COMMAND_TYPE_RUN
;
4721 * Non-option argument, but not a known
4722 * command name: assume the default
4723 * `convert` command.
4725 command_type
= COMMAND_TYPE_CONVERT
;
4726 command_name
= "convert";
4734 if (command_type
== COMMAND_TYPE_NONE
) {
4735 if (argpar_parse_ret
.ingested_orig_args
== top_level_argc
) {
4737 * We only got non-help, non-version general options
4738 * like --verbose and --debug, without any other
4739 * arguments, so we can't do anything useful: print the
4742 print_gen_usage(stdout
);
4747 * We stopped on an unknown option argument (and therefore
4748 * didn't see a command name). Assume `convert` command.
4750 command_type
= COMMAND_TYPE_CONVERT
;
4751 command_name
= "convert";
4753 top_level_argc
- argpar_parse_ret
.ingested_orig_args
;
4755 &top_level_argv
[argpar_parse_ret
.ingested_orig_args
];
4758 BT_ASSERT(command_argv
);
4759 BT_ASSERT(command_argc
>= 0);
4762 * The convert command can set its own default log level for
4763 * backward compatibility reasons. It only does so if there's no
4764 * log level yet, so do not force one for this command.
4766 if (command_type
!= COMMAND_TYPE_CONVERT
&& default_log_level
< 0) {
4767 /* Default log level */
4768 default_log_level
= cli_default_log_level
;
4771 switch (command_type
) {
4772 case COMMAND_TYPE_RUN
:
4773 config
= bt_config_run_from_args(command_argc
, command_argv
,
4774 retcode
, force_omit_system_plugin_path
,
4775 force_omit_home_plugin_path
, initial_plugin_paths
,
4778 case COMMAND_TYPE_CONVERT
:
4779 config
= bt_config_convert_from_args(command_argc
, command_argv
,
4780 retcode
, force_omit_system_plugin_path
,
4781 force_omit_home_plugin_path
,
4782 initial_plugin_paths
, &default_log_level
);
4784 case COMMAND_TYPE_LIST_PLUGINS
:
4785 config
= bt_config_list_plugins_from_args(command_argc
,
4786 command_argv
, retcode
, force_omit_system_plugin_path
,
4787 force_omit_home_plugin_path
, initial_plugin_paths
);
4789 case COMMAND_TYPE_HELP
:
4790 config
= bt_config_help_from_args(command_argc
,
4791 command_argv
, retcode
, force_omit_system_plugin_path
,
4792 force_omit_home_plugin_path
, initial_plugin_paths
,
4795 case COMMAND_TYPE_QUERY
:
4796 config
= bt_config_query_from_args(command_argc
,
4797 command_argv
, retcode
, force_omit_system_plugin_path
,
4798 force_omit_home_plugin_path
, initial_plugin_paths
,
4806 BT_ASSERT(default_log_level
>= BT_LOG_TRACE
);
4807 config
->log_level
= default_log_level
;
4808 config
->command_name
= command_name
;
4817 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
4818 bt_value_put_ref(initial_plugin_paths
);