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
,
887 OPT_OMIT_HOME_PLUGIN_PATH
,
888 OPT_OMIT_SYSTEM_PLUGIN_PATH
,
893 OPT_RESET_BASE_PARAMS
,
897 OPT_STREAM_INTERSECTION
,
903 enum bt_config_component_dest
{
904 BT_CONFIG_COMPONENT_DEST_UNKNOWN
= -1,
905 BT_CONFIG_COMPONENT_DEST_SOURCE
,
906 BT_CONFIG_COMPONENT_DEST_FILTER
,
907 BT_CONFIG_COMPONENT_DEST_SINK
,
911 * Adds a configuration component to the appropriate configuration
912 * array depending on the destination.
915 void add_run_cfg_comp(struct bt_config
*cfg
,
916 struct bt_config_component
*cfg_comp
,
917 enum bt_config_component_dest dest
)
919 bt_object_get_ref(cfg_comp
);
922 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
923 g_ptr_array_add(cfg
->cmd_data
.run
.sources
, cfg_comp
);
925 case BT_CONFIG_COMPONENT_DEST_FILTER
:
926 g_ptr_array_add(cfg
->cmd_data
.run
.filters
, cfg_comp
);
928 case BT_CONFIG_COMPONENT_DEST_SINK
:
929 g_ptr_array_add(cfg
->cmd_data
.run
.sinks
, cfg_comp
);
937 int add_run_cfg_comp_check_name(struct bt_config
*cfg
,
938 struct bt_config_component
*cfg_comp
,
939 enum bt_config_component_dest dest
,
940 bt_value
*instance_names
)
944 if (cfg_comp
->instance_name
->len
== 0) {
945 BT_CLI_LOGE_APPEND_CAUSE("Found an unnamed component.");
950 if (bt_value_map_has_entry(instance_names
,
951 cfg_comp
->instance_name
->str
)) {
952 BT_CLI_LOGE_APPEND_CAUSE("Duplicate component instance name:\n %s",
953 cfg_comp
->instance_name
->str
);
958 if (bt_value_map_insert_entry(instance_names
,
959 cfg_comp
->instance_name
->str
, bt_value_null
)) {
960 BT_CLI_LOGE_APPEND_CAUSE_OOM();
965 add_run_cfg_comp(cfg
, cfg_comp
, dest
);
972 int append_env_var_plugin_paths(bt_value
*plugin_paths
)
977 if (bt_common_is_setuid_setgid()) {
978 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
982 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
987 ret
= bt_config_append_plugin_paths(plugin_paths
, envvar
);
991 BT_CLI_LOGE_APPEND_CAUSE("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH.");
998 int append_home_and_system_plugin_paths(bt_value
*plugin_paths
,
999 bool omit_system_plugin_path
, bool omit_home_plugin_path
)
1003 if (!omit_home_plugin_path
) {
1004 if (bt_common_is_setuid_setgid()) {
1005 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1007 char *home_plugin_dir
= bt_common_get_home_plugin_path(
1008 BT_LOG_OUTPUT_LEVEL
);
1010 if (home_plugin_dir
) {
1011 ret
= bt_config_append_plugin_paths(
1012 plugin_paths
, home_plugin_dir
);
1013 free(home_plugin_dir
);
1016 BT_CLI_LOGE_APPEND_CAUSE("Invalid home plugin path.");
1023 if (!omit_system_plugin_path
) {
1024 if (bt_config_append_plugin_paths(plugin_paths
,
1025 bt_common_get_system_plugin_path())) {
1026 BT_CLI_LOGE_APPEND_CAUSE("Invalid system plugin path.");
1032 BT_CLI_LOGE_APPEND_CAUSE("Cannot append home and system plugin paths.");
1037 int append_home_and_system_plugin_paths_cfg(struct bt_config
*cfg
)
1039 return append_home_and_system_plugin_paths(cfg
->plugin_paths
,
1040 cfg
->omit_system_plugin_path
, cfg
->omit_home_plugin_path
);
1044 struct bt_config
*bt_config_base_create(enum bt_config_command command
,
1045 const bt_value
*initial_plugin_paths
,
1048 struct bt_config
*cfg
;
1051 cfg
= g_new0(struct bt_config
, 1);
1053 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1057 bt_object_init_shared(&cfg
->base
, bt_config_destroy
);
1058 cfg
->command
= command
;
1059 cfg
->command_needs_plugins
= needs_plugins
;
1061 if (initial_plugin_paths
) {
1062 bt_value
*initial_plugin_paths_copy
;
1064 (void) bt_value_copy(initial_plugin_paths
,
1065 &initial_plugin_paths_copy
);
1066 cfg
->plugin_paths
= initial_plugin_paths_copy
;
1068 cfg
->plugin_paths
= bt_value_array_create();
1069 if (!cfg
->plugin_paths
) {
1070 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1078 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1085 struct bt_config
*bt_config_run_create(
1086 const bt_value
*initial_plugin_paths
)
1088 struct bt_config
*cfg
;
1091 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_RUN
,
1092 initial_plugin_paths
, true);
1097 cfg
->cmd_data
.run
.sources
= g_ptr_array_new_with_free_func(
1098 (GDestroyNotify
) bt_object_put_ref
);
1099 if (!cfg
->cmd_data
.run
.sources
) {
1100 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1104 cfg
->cmd_data
.run
.filters
= g_ptr_array_new_with_free_func(
1105 (GDestroyNotify
) bt_object_put_ref
);
1106 if (!cfg
->cmd_data
.run
.filters
) {
1107 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1111 cfg
->cmd_data
.run
.sinks
= g_ptr_array_new_with_free_func(
1112 (GDestroyNotify
) bt_object_put_ref
);
1113 if (!cfg
->cmd_data
.run
.sinks
) {
1114 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1118 cfg
->cmd_data
.run
.connections
= g_ptr_array_new_with_free_func(
1119 (GDestroyNotify
) bt_config_connection_destroy
);
1120 if (!cfg
->cmd_data
.run
.connections
) {
1121 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1128 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1135 struct bt_config
*bt_config_list_plugins_create(
1136 const bt_value
*initial_plugin_paths
)
1138 struct bt_config
*cfg
;
1141 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS
,
1142 initial_plugin_paths
, true);
1150 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1157 struct bt_config
*bt_config_help_create(
1158 const bt_value
*initial_plugin_paths
,
1159 int default_log_level
)
1161 struct bt_config
*cfg
;
1164 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_HELP
,
1165 initial_plugin_paths
, true);
1170 cfg
->cmd_data
.help
.cfg_component
=
1171 bt_config_component_create(-1, NULL
, NULL
, default_log_level
);
1172 if (!cfg
->cmd_data
.help
.cfg_component
) {
1179 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1186 struct bt_config
*bt_config_query_create(
1187 const bt_value
*initial_plugin_paths
)
1189 struct bt_config
*cfg
;
1192 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_QUERY
,
1193 initial_plugin_paths
, true);
1198 cfg
->cmd_data
.query
.object
= g_string_new(NULL
);
1199 if (!cfg
->cmd_data
.query
.object
) {
1200 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1207 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1214 struct bt_config
*bt_config_print_ctf_metadata_create(
1215 const bt_value
*initial_plugin_paths
)
1217 struct bt_config
*cfg
;
1220 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_CTF_METADATA
,
1221 initial_plugin_paths
, true);
1226 cfg
->cmd_data
.print_ctf_metadata
.path
= g_string_new(NULL
);
1227 if (!cfg
->cmd_data
.print_ctf_metadata
.path
) {
1228 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1232 cfg
->cmd_data
.print_ctf_metadata
.output_path
= g_string_new(NULL
);
1233 if (!cfg
->cmd_data
.print_ctf_metadata
.output_path
) {
1234 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1241 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1248 struct bt_config
*bt_config_print_lttng_live_sessions_create(
1249 const bt_value
*initial_plugin_paths
)
1251 struct bt_config
*cfg
;
1254 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
,
1255 initial_plugin_paths
, true);
1260 cfg
->cmd_data
.print_lttng_live_sessions
.url
= g_string_new(NULL
);
1261 if (!cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
1262 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1266 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
=
1268 if (!cfg
->cmd_data
.print_lttng_live_sessions
.output_path
) {
1269 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1276 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1283 int bt_config_append_plugin_paths_check_setuid_setgid(
1284 bt_value
*plugin_paths
, const char *arg
)
1288 if (bt_common_is_setuid_setgid()) {
1289 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1293 if (bt_config_append_plugin_paths(plugin_paths
, arg
)) {
1294 BT_CLI_LOGE_APPEND_CAUSE("Invalid --plugin-path option's argument:\n %s",
1305 * Prints the expected format for a --params option.
1308 void print_expected_params_format(FILE *fp
)
1310 fprintf(fp
, "Expected format of PARAMS\n");
1311 fprintf(fp
, "-------------------------\n");
1313 fprintf(fp
, " PARAM=VALUE[,PARAM=VALUE]...\n");
1315 fprintf(fp
, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
1316 fprintf(fp
, "where PARAM is the parameter name (C identifier plus the [:.-] characters),\n");
1317 fprintf(fp
, "and VALUE can be one of:\n");
1319 fprintf(fp
, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
1320 fprintf(fp
, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
1321 fprintf(fp
, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
1322 fprintf(fp
, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
1323 fprintf(fp
, " (`0x` prefix) unsigned (with `+` prefix) or signed 64-bit integer.\n");
1324 fprintf(fp
, "* Double precision floating point number (scientific notation is accepted).\n");
1325 fprintf(fp
, "* Unquoted string with no special characters, and not matching any of\n");
1326 fprintf(fp
, " the null and boolean value symbols above.\n");
1327 fprintf(fp
, "* Double-quoted string (accepts escape characters).\n");
1328 fprintf(fp
, "* Array, formatted as an opening `[`, a list of comma-separated values\n");
1329 fprintf(fp
, " (as described by the current list) and a closing `]`.\n");
1331 fprintf(fp
, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1333 fprintf(fp
, "Example:\n");
1335 fprintf(fp
, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
1336 fprintf(fp
, " play=+23, observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
1337 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\",\n");
1338 fprintf(fp
, " things=[1, \"2\", 3]\n");
1340 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1341 fprintf(fp
, "babeltrace2 from a shell.\n");
1345 bool help_option_is_specified(
1346 const struct bt_argpar_parse_ret
*argpar_parse_ret
)
1349 bool specified
= false;
1351 for (i
= 0; i
< argpar_parse_ret
->items
->len
; i
++) {
1352 struct bt_argpar_item
*argpar_item
=
1353 g_ptr_array_index(argpar_parse_ret
->items
, i
);
1354 struct bt_argpar_item_opt
*argpar_item_opt
;
1356 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
1360 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
1361 if (argpar_item_opt
->descr
->id
== OPT_HELP
) {
1371 * Prints the help command usage.
1374 void print_help_usage(FILE *fp
)
1376 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1377 fprintf(fp
, " babeltrace2 [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
1379 fprintf(fp
, "Options:\n");
1381 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1382 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1383 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1384 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1385 fprintf(fp
, " dynamic plugins can be loaded\n");
1386 fprintf(fp
, " -h, --help Show this help and quit\n");
1388 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1390 fprintf(fp
, "Use `babeltrace2 list-plugins` to show the list of available plugins.\n");
1394 const struct bt_argpar_opt_descr help_options
[] = {
1395 /* id, short_name, long_name, with_arg */
1396 { OPT_HELP
, 'h', "help", false },
1397 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1398 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1399 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1400 BT_ARGPAR_OPT_DESCR_SENTINEL
1404 * Creates a Babeltrace config object from the arguments of a help
1407 * *retcode is set to the appropriate exit code to use.
1410 struct bt_config
*bt_config_help_from_args(int argc
, const char *argv
[],
1411 int *retcode
, bool force_omit_system_plugin_path
,
1412 bool force_omit_home_plugin_path
,
1413 const bt_value
*initial_plugin_paths
, int default_log_level
)
1416 struct bt_config
*cfg
= NULL
;
1417 const char *leftover
= NULL
;
1418 char *plugin_name
= NULL
, *comp_cls_name
= NULL
;
1419 struct bt_argpar_parse_ret argpar_parse_ret
;
1422 cfg
= bt_config_help_create(initial_plugin_paths
, default_log_level
);
1427 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1428 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1429 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1435 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, help_options
, true);
1436 if (argpar_parse_ret
.error
) {
1437 BT_CLI_LOGE_APPEND_CAUSE(
1438 "While parsing `help` command's command-line arguments: %s",
1439 argpar_parse_ret
.error
->str
);
1443 if (help_option_is_specified(&argpar_parse_ret
)) {
1444 print_help_usage(stdout
);
1446 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1450 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
1451 struct bt_argpar_item
*argpar_item
=
1452 g_ptr_array_index(argpar_parse_ret
.items
, i
);
1454 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
1455 struct bt_argpar_item_opt
*argpar_item_opt
;
1457 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
1458 arg
= argpar_item_opt
->arg
;
1460 switch (argpar_item_opt
->descr
->id
) {
1461 case OPT_PLUGIN_PATH
:
1462 if (bt_config_append_plugin_paths_check_setuid_setgid(
1463 cfg
->plugin_paths
, arg
)) {
1467 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1468 cfg
->omit_system_plugin_path
= true;
1470 case OPT_OMIT_HOME_PLUGIN_PATH
:
1471 cfg
->omit_home_plugin_path
= true;
1474 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1475 argpar_item_opt
->descr
->id
);
1479 struct bt_argpar_item_non_opt
*argpar_item_non_opt
1480 = (struct bt_argpar_item_non_opt
*) argpar_item
;
1483 BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `help` command: `%s`.",
1484 argpar_item_non_opt
->arg
);
1488 leftover
= argpar_item_non_opt
->arg
;
1493 plugin_comp_cls_names(leftover
, NULL
,
1494 &plugin_name
, &comp_cls_name
,
1495 &cfg
->cmd_data
.help
.cfg_component
->type
);
1496 if (plugin_name
&& comp_cls_name
) {
1497 /* Component class help */
1499 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1502 cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
,
1505 /* Fall back to plugin help */
1507 cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1511 print_help_usage(stdout
);
1513 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1517 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1525 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1528 g_free(plugin_name
);
1529 g_free(comp_cls_name
);
1531 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1537 * Prints the help command usage.
1540 void print_query_usage(FILE *fp
)
1542 fprintf(fp
, "Usage: babeltrace2 [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
1544 fprintf(fp
, "Options:\n");
1546 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1547 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1548 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1549 fprintf(fp
, " -p, --params=PARAMS Set the query parameters to PARAMS\n");
1550 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1551 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1552 fprintf(fp
, " dynamic plugins can be loaded\n");
1553 fprintf(fp
, " -h, --help Show this help and quit\n");
1554 fprintf(fp
, "\n\n");
1555 print_expected_params_format(fp
);
1559 const struct bt_argpar_opt_descr query_options
[] = {
1560 /* id, short_name, long_name, with_arg */
1561 { OPT_HELP
, 'h', "help", false },
1562 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1563 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1564 { OPT_PARAMS
, 'p', "params", true },
1565 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1566 BT_ARGPAR_OPT_DESCR_SENTINEL
1570 * Creates a Babeltrace config object from the arguments of a query
1573 * *retcode is set to the appropriate exit code to use.
1576 struct bt_config
*bt_config_query_from_args(int argc
, const char *argv
[],
1577 int *retcode
, bool force_omit_system_plugin_path
,
1578 bool force_omit_home_plugin_path
,
1579 const bt_value
*initial_plugin_paths
,
1580 int default_log_level
)
1583 struct bt_config
*cfg
= NULL
;
1584 const char *component_class_spec
= NULL
;
1585 const char *query_object
= NULL
;
1587 GString
*error_str
= NULL
;
1588 struct bt_argpar_parse_ret argpar_parse_ret
;
1590 params
= bt_value_null
;
1591 bt_value_get_ref(bt_value_null
);
1594 cfg
= bt_config_query_create(initial_plugin_paths
);
1599 error_str
= g_string_new(NULL
);
1601 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1605 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1606 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1607 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1613 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, query_options
, true);
1614 if (argpar_parse_ret
.error
) {
1615 BT_CLI_LOGE_APPEND_CAUSE(
1616 "While parsing `query` command's command-line arguments: %s",
1617 argpar_parse_ret
.error
->str
);
1621 if (help_option_is_specified(&argpar_parse_ret
)) {
1622 print_query_usage(stdout
);
1624 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1628 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
1629 struct bt_argpar_item
*argpar_item
=
1630 g_ptr_array_index(argpar_parse_ret
.items
, i
);
1632 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
1633 struct bt_argpar_item_opt
*argpar_item_opt
=
1634 (struct bt_argpar_item_opt
*) argpar_item
;
1635 const char *arg
= argpar_item_opt
->arg
;
1637 switch (argpar_item_opt
->descr
->id
) {
1638 case OPT_PLUGIN_PATH
:
1639 if (bt_config_append_plugin_paths_check_setuid_setgid(
1640 cfg
->plugin_paths
, arg
)) {
1644 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1645 cfg
->omit_system_plugin_path
= true;
1647 case OPT_OMIT_HOME_PLUGIN_PATH
:
1648 cfg
->omit_home_plugin_path
= true;
1652 bt_value_put_ref(params
);
1653 params
= cli_value_from_arg(arg
, error_str
);
1655 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1662 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1663 argpar_item_opt
->descr
->id
);
1667 struct bt_argpar_item_non_opt
*argpar_item_non_opt
1668 = (struct bt_argpar_item_non_opt
*) argpar_item
;
1671 * We need exactly two leftover arguments which are the
1672 * mandatory component class specification and query object.
1674 if (!component_class_spec
) {
1675 component_class_spec
= argpar_item_non_opt
->arg
;
1676 } else if (!query_object
) {
1677 query_object
= argpar_item_non_opt
->arg
;
1679 BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `query` command: `%s`.",
1680 argpar_item_non_opt
->arg
);
1686 if (!component_class_spec
|| !query_object
) {
1687 print_query_usage(stdout
);
1689 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1693 cfg
->cmd_data
.query
.cfg_component
=
1694 bt_config_component_from_arg(component_class_spec
,
1696 if (!cfg
->cmd_data
.query
.cfg_component
) {
1697 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for component class specification:\n %s",
1698 component_class_spec
);
1703 BT_OBJECT_MOVE_REF(cfg
->cmd_data
.query
.cfg_component
->params
, params
);
1705 if (strlen(query_object
) == 0) {
1706 BT_CLI_LOGE_APPEND_CAUSE("Invalid empty object.");
1710 g_string_assign(cfg
->cmd_data
.query
.object
, query_object
);
1712 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1720 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1723 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1726 g_string_free(error_str
, TRUE
);
1729 bt_value_put_ref(params
);
1734 * Prints the list-plugins command usage.
1737 void print_list_plugins_usage(FILE *fp
)
1739 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
1741 fprintf(fp
, "Options:\n");
1743 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1744 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1745 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1746 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1747 fprintf(fp
, " dynamic plugins can be loaded\n");
1748 fprintf(fp
, " -h, --help Show this help and quit\n");
1750 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1752 fprintf(fp
, "Use `babeltrace2 help` to get help for a specific plugin or component class.\n");
1756 const struct bt_argpar_opt_descr list_plugins_options
[] = {
1757 /* id, short_name, long_name, with_arg */
1758 { OPT_HELP
, 'h', "help", false },
1759 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1760 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1761 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1762 BT_ARGPAR_OPT_DESCR_SENTINEL
1766 * Creates a Babeltrace config object from the arguments of a
1767 * list-plugins command.
1769 * *retcode is set to the appropriate exit code to use.
1772 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
1773 int *retcode
, bool force_omit_system_plugin_path
,
1774 bool force_omit_home_plugin_path
,
1775 const bt_value
*initial_plugin_paths
)
1778 struct bt_config
*cfg
= NULL
;
1779 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1782 cfg
= bt_config_list_plugins_create(initial_plugin_paths
);
1787 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
1788 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
1789 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
1795 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, list_plugins_options
, true);
1796 if (argpar_parse_ret
.error
) {
1797 BT_CLI_LOGE_APPEND_CAUSE(
1798 "While parsing `list-plugins` command's command-line arguments: %s",
1799 argpar_parse_ret
.error
->str
);
1803 if (help_option_is_specified(&argpar_parse_ret
)) {
1804 print_list_plugins_usage(stdout
);
1806 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1810 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
1811 struct bt_argpar_item
*argpar_item
=
1812 g_ptr_array_index(argpar_parse_ret
.items
, i
);
1813 struct bt_argpar_item_opt
*argpar_item_opt
;
1816 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
1817 struct bt_argpar_item_non_opt
*argpar_item_non_opt
1818 = (struct bt_argpar_item_non_opt
*) argpar_item
;
1820 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`.",
1821 argpar_item_non_opt
->arg
);
1825 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
1826 arg
= argpar_item_opt
->arg
;
1828 switch (argpar_item_opt
->descr
->id
) {
1829 case OPT_PLUGIN_PATH
:
1830 if (bt_config_append_plugin_paths_check_setuid_setgid(
1831 cfg
->plugin_paths
, arg
)) {
1835 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
1836 cfg
->omit_system_plugin_path
= true;
1838 case OPT_OMIT_HOME_PLUGIN_PATH
:
1839 cfg
->omit_home_plugin_path
= true;
1842 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1843 argpar_item_opt
->descr
->id
);
1848 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
1856 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1859 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
1865 * Prints the run command usage.
1868 void print_run_usage(FILE *fp
)
1870 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n");
1872 fprintf(fp
, "Options:\n");
1874 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
1875 fprintf(fp
, " for all the following components until\n");
1876 fprintf(fp
, " --reset-base-params is encountered\n");
1877 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1878 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
1879 fprintf(fp
, " Instantiate the component class CLS of type\n");
1880 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
1881 fprintf(fp
, " in the plugin PLUGIN, add it to the graph,\n");
1882 fprintf(fp
, " and optionally name it NAME (you can also\n");
1883 fprintf(fp
, " specify the name with --name)\n");
1884 fprintf(fp
, " -x, --connect=CONNECTION Connect two created components (see the\n");
1885 fprintf(fp
, " expected format of CONNECTION below)\n");
1886 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
1887 fprintf(fp
, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
1888 fprintf(fp
, " -n, --name=NAME Set the name of the current component\n");
1889 fprintf(fp
, " to NAME (must be unique amongst all the\n");
1890 fprintf(fp
, " names of the created components)\n");
1891 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
1892 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
1893 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
1894 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
1895 fprintf(fp
, " current component (see the expected format\n");
1896 fprintf(fp
, " of PARAMS below)\n");
1897 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
1898 fprintf(fp
, " dynamic plugins can be loaded\n");
1899 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters to an\n");
1900 fprintf(fp
, " empty map\n");
1901 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
1902 fprintf(fp
, " the graph later, retry in DUR µs\n");
1903 fprintf(fp
, " (default: 100000)\n");
1904 fprintf(fp
, " -h, --help Show this help and quit\n");
1906 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1907 fprintf(fp
, "\n\n");
1908 fprintf(fp
, "Expected format of CONNECTION\n");
1909 fprintf(fp
, "-----------------------------\n");
1911 fprintf(fp
, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
1913 fprintf(fp
, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
1914 fprintf(fp
, "components to connect together. You must escape the following characters\n\n");
1915 fprintf(fp
, "with `\\`: `\\`, `.`, and `:`. You can set the name of the current\n");
1916 fprintf(fp
, "component with the --name option.\n");
1918 fprintf(fp
, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
1919 fprintf(fp
, "identify the upstream and downstream ports to use for the connection.\n");
1920 fprintf(fp
, "When the port is not specified, `*` is used.\n");
1922 fprintf(fp
, "When a component named UPSTREAM has an available port which matches the\n");
1923 fprintf(fp
, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
1924 fprintf(fp
, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
1925 fprintf(fp
, "DOWNSTREAM.\n");
1927 fprintf(fp
, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
1928 fprintf(fp
, "which matches anything. You must escape the following characters\n");
1929 fprintf(fp
, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
1931 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
1932 fprintf(fp
, "can connect a filter component to a sink component.\n");
1934 fprintf(fp
, "Examples:\n");
1936 fprintf(fp
, " my-src:my-sink\n");
1937 fprintf(fp
, " ctf-fs.*stream*:utils-muxer:*\n");
1939 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1940 fprintf(fp
, "babeltrace2 from a shell.\n");
1941 fprintf(fp
, "\n\n");
1942 print_expected_params_format(fp
);
1946 * Creates a Babeltrace config object from the arguments of a run
1949 * *retcode is set to the appropriate exit code to use.
1952 struct bt_config
*bt_config_run_from_args(int argc
, const char *argv
[],
1953 int *retcode
, bool force_omit_system_plugin_path
,
1954 bool force_omit_home_plugin_path
,
1955 const bt_value
*initial_plugin_paths
, int default_log_level
)
1957 struct bt_config_component
*cur_cfg_comp
= NULL
;
1958 enum bt_config_component_dest cur_cfg_comp_dest
=
1959 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
1960 bt_value
*cur_base_params
= NULL
;
1962 struct bt_config
*cfg
= NULL
;
1963 bt_value
*instance_names
= NULL
;
1964 bt_value
*connection_args
= NULL
;
1965 char error_buf
[256] = { 0 };
1966 long retry_duration
= -1;
1967 bt_value_map_extend_status extend_status
;
1968 GString
*error_str
= NULL
;
1969 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
1972 static const struct bt_argpar_opt_descr run_options
[] = {
1973 { OPT_BASE_PARAMS
, 'b', "base-params", true },
1974 { OPT_COMPONENT
, 'c', "component", true },
1975 { OPT_CONNECT
, 'x', "connect", true },
1976 { OPT_HELP
, 'h', "help", false },
1977 { OPT_LOG_LEVEL
, 'l', "log-level", true },
1978 { OPT_NAME
, 'n', "name", true },
1979 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
1980 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
1981 { OPT_PARAMS
, 'p', "params", true },
1982 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
1983 { OPT_RESET_BASE_PARAMS
, 'r', "reset-base-params", false },
1984 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
1985 BT_ARGPAR_OPT_DESCR_SENTINEL
1990 error_str
= g_string_new(NULL
);
1992 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1997 print_run_usage(stdout
);
2002 cfg
= bt_config_run_create(initial_plugin_paths
);
2007 cfg
->cmd_data
.run
.retry_duration_us
= 100000;
2008 cfg
->omit_system_plugin_path
= force_omit_system_plugin_path
;
2009 cfg
->omit_home_plugin_path
= force_omit_home_plugin_path
;
2010 cur_base_params
= bt_value_map_create();
2011 if (!cur_base_params
) {
2012 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2016 instance_names
= bt_value_map_create();
2017 if (!instance_names
) {
2018 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2022 connection_args
= bt_value_array_create();
2023 if (!connection_args
) {
2024 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2028 ret
= append_env_var_plugin_paths(cfg
->plugin_paths
);
2034 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, run_options
, true);
2035 if (argpar_parse_ret
.error
) {
2036 BT_CLI_LOGE_APPEND_CAUSE(
2037 "While parsing `run` command's command-line arguments: %s",
2038 argpar_parse_ret
.error
->str
);
2042 if (help_option_is_specified(&argpar_parse_ret
)) {
2043 print_run_usage(stdout
);
2045 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2049 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
2050 struct bt_argpar_item
*argpar_item
=
2051 g_ptr_array_index(argpar_parse_ret
.items
, i
);
2052 struct bt_argpar_item_opt
*argpar_item_opt
;
2055 /* This command does not accept leftover arguments. */
2056 if (argpar_item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
2057 struct bt_argpar_item_non_opt
*argpar_nonopt_item
=
2058 (struct bt_argpar_item_non_opt
*) argpar_item
;
2060 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`",
2061 argpar_nonopt_item
->arg
);
2065 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
2066 arg
= argpar_item_opt
->arg
;
2068 switch (argpar_item_opt
->descr
->id
) {
2069 case OPT_PLUGIN_PATH
:
2070 if (bt_config_append_plugin_paths_check_setuid_setgid(
2071 cfg
->plugin_paths
, arg
)) {
2075 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
2076 cfg
->omit_system_plugin_path
= true;
2078 case OPT_OMIT_HOME_PLUGIN_PATH
:
2079 cfg
->omit_home_plugin_path
= true;
2083 enum bt_config_component_dest new_dest
;
2086 ret
= add_run_cfg_comp_check_name(cfg
,
2087 cur_cfg_comp
, cur_cfg_comp_dest
,
2089 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2095 cur_cfg_comp
= bt_config_component_from_arg(arg
,
2097 if (!cur_cfg_comp
) {
2098 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --component option's argument:\n %s",
2103 switch (cur_cfg_comp
->type
) {
2104 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
2105 new_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
2107 case BT_COMPONENT_CLASS_TYPE_FILTER
:
2108 new_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
2110 case BT_COMPONENT_CLASS_TYPE_SINK
:
2111 new_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
2117 BT_ASSERT(cur_base_params
);
2118 bt_value_put_ref(cur_cfg_comp
->params
);
2119 if (bt_value_copy(cur_base_params
,
2120 &cur_cfg_comp
->params
) < 0) {
2121 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2125 cur_cfg_comp_dest
= new_dest
;
2131 bt_value
*params_to_set
;
2133 if (!cur_cfg_comp
) {
2134 BT_CLI_LOGE_APPEND_CAUSE("Cannot add parameters to unavailable component:\n %s",
2139 params
= cli_value_from_arg(arg
, error_str
);
2141 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
2146 extend_status
= bt_value_map_extend(
2147 cur_cfg_comp
->params
, params
, ¶ms_to_set
);
2148 BT_VALUE_PUT_REF_AND_RESET(params
);
2149 if (extend_status
!= BT_VALUE_MAP_EXTEND_STATUS_OK
) {
2150 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current component parameters with --params option's argument:\n %s",
2155 BT_OBJECT_MOVE_REF(cur_cfg_comp
->params
, params_to_set
);
2159 if (!cur_cfg_comp
) {
2160 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the name of unavailable component:\n %s",
2165 g_string_assign(cur_cfg_comp
->instance_name
, arg
);
2168 if (!cur_cfg_comp
) {
2169 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the log level of unavailable component:\n %s",
2174 cur_cfg_comp
->log_level
=
2175 bt_log_get_level_from_string(arg
);
2176 if (cur_cfg_comp
->log_level
< 0) {
2177 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
2182 case OPT_BASE_PARAMS
:
2184 bt_value
*params
= cli_value_from_arg(arg
, error_str
);
2187 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --base-params option's argument:\n %s",
2192 BT_OBJECT_MOVE_REF(cur_base_params
, params
);
2195 case OPT_RESET_BASE_PARAMS
:
2196 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2197 cur_base_params
= bt_value_map_create();
2198 if (!cur_base_params
) {
2199 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2204 if (bt_value_array_append_string_element(
2205 connection_args
, arg
)) {
2206 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2210 case OPT_RETRY_DURATION
: {
2212 size_t arg_len
= strlen(argpar_item_opt
->arg
);
2214 retry_duration
= g_ascii_strtoll(argpar_item_opt
->arg
, &end
, 10);
2216 if (arg_len
== 0 || end
!= (argpar_item_opt
->arg
+ arg_len
)) {
2217 BT_CLI_LOGE_APPEND_CAUSE(
2218 "Could not parse --retry-duration option's argument as an unsigned integer: `%s`",
2219 argpar_item_opt
->arg
);
2223 if (retry_duration
< 0) {
2224 BT_CLI_LOGE_APPEND_CAUSE("--retry-duration option's argument must be positive or 0: %ld",
2229 cfg
->cmd_data
.run
.retry_duration_us
=
2230 (uint64_t) retry_duration
;
2234 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
2235 argpar_item_opt
->descr
->id
);
2240 /* Add current component */
2242 ret
= add_run_cfg_comp_check_name(cfg
, cur_cfg_comp
,
2243 cur_cfg_comp_dest
, instance_names
);
2244 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2250 if (cfg
->cmd_data
.run
.sources
->len
== 0) {
2251 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no source component.");
2255 if (cfg
->cmd_data
.run
.sinks
->len
== 0) {
2256 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no sink component.");
2260 if (append_home_and_system_plugin_paths_cfg(cfg
)) {
2264 ret
= bt_config_cli_args_create_connections(cfg
,
2268 BT_CLI_LOGE_APPEND_CAUSE("Cannot creation connections:\n%s", error_buf
);
2276 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2280 g_string_free(error_str
, TRUE
);
2283 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
2284 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2285 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2286 BT_VALUE_PUT_REF_AND_RESET(instance_names
);
2287 BT_VALUE_PUT_REF_AND_RESET(connection_args
);
2292 struct bt_config
*bt_config_run_from_args_array(const bt_value
*run_args
,
2293 int *retcode
, bool force_omit_system_plugin_path
,
2294 bool force_omit_home_plugin_path
,
2295 const bt_value
*initial_plugin_paths
, int default_log_level
)
2297 struct bt_config
*cfg
= NULL
;
2300 const size_t argc
= bt_value_array_get_size(run_args
);
2302 argv
= calloc(argc
, sizeof(*argv
));
2304 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2308 len
= bt_value_array_get_size(run_args
);
2310 BT_CLI_LOGE_APPEND_CAUSE("Invalid executable arguments.");
2313 for (i
= 0; i
< len
; i
++) {
2314 const bt_value
*arg_value
=
2315 bt_value_array_borrow_element_by_index_const(run_args
,
2319 BT_ASSERT(arg_value
);
2320 arg
= bt_value_string_get(arg_value
);
2325 cfg
= bt_config_run_from_args(argc
, argv
, retcode
,
2326 force_omit_system_plugin_path
, force_omit_home_plugin_path
,
2327 initial_plugin_paths
, default_log_level
);
2335 * Prints the convert command usage.
2338 void print_convert_usage(FILE *fp
)
2340 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2342 fprintf(fp
, "Options:\n");
2344 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2345 fprintf(fp
, " Instantiate the component class CLS of type\n");
2346 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2347 fprintf(fp
, " in the plugin PLUGIN, add it to the\n");
2348 fprintf(fp
, " conversion graph, and optionally name it\n");
2349 fprintf(fp
, " NAME (you can also specify the name with\n");
2350 fprintf(fp
, " --name)\n");
2351 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
2352 fprintf(fp
, " (`N`, `V`, `D`, `I`, `W`, `E`, or `F`)\n");
2353 fprintf(fp
, " --name=NAME Set the name of the current component\n");
2354 fprintf(fp
, " to NAME (must be unique amongst all the\n");
2355 fprintf(fp
, " names of the created components)\n");
2356 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
2357 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
2358 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
2359 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2360 fprintf(fp
, " current component (see the expected format\n");
2361 fprintf(fp
, " of PARAMS below)\n");
2362 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
2363 fprintf(fp
, " dynamic plugins can be loaded\n");
2364 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
2365 fprintf(fp
, " the graph later, retry in DUR µs\n");
2366 fprintf(fp
, " (default: 100000)\n");
2367 fprintf(fp
, " dynamic plugins can be loaded\n");
2368 fprintf(fp
, " --run-args Print the equivalent arguments for the\n");
2369 fprintf(fp
, " `run` command to the standard output,\n");
2370 fprintf(fp
, " formatted for a shell, and quit\n");
2371 fprintf(fp
, " --run-args-0 Print the equivalent arguments for the\n");
2372 fprintf(fp
, " `run` command to the standard output,\n");
2373 fprintf(fp
, " formatted for `xargs -0`, and quit\n");
2374 fprintf(fp
, " --stream-intersection Only process events when all streams\n");
2375 fprintf(fp
, " are active\n");
2376 fprintf(fp
, " -h, --help Show this help and quit\n");
2378 fprintf(fp
, "Implicit `source.ctf.fs` component options:\n");
2380 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2381 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2383 fprintf(fp
, "Implicit `sink.text.pretty` component options:\n");
2385 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
2386 fprintf(fp
, " --clock-date Print timestamp dates\n");
2387 fprintf(fp
, " --clock-gmt Print and parse timestamps in the GMT\n");
2388 fprintf(fp
, " time zone instead of the local time zone\n");
2389 fprintf(fp
, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2390 fprintf(fp
, " of `hh:mm:ss.nnnnnnnnn`\n");
2391 fprintf(fp
, " --color=(never | auto | always)\n");
2392 fprintf(fp
, " Never, automatically, or always emit\n");
2393 fprintf(fp
, " console color codes\n");
2394 fprintf(fp
, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2395 fprintf(fp
, " `all`, `trace`, `trace:hostname`,\n");
2396 fprintf(fp
, " `trace:domain`, `trace:procname`,\n");
2397 fprintf(fp
, " `trace:vpid`, `loglevel`, `emf`\n");
2398 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2399 fprintf(fp
, " `payload` (or `arg` or `args`), `none`,\n");
2400 fprintf(fp
, " `all`, `scope`, `header`, `context`\n");
2401 fprintf(fp
, " (or `ctx`)\n");
2402 fprintf(fp
, " --no-delta Do not print time delta between\n");
2403 fprintf(fp
, " consecutive events\n");
2404 fprintf(fp
, " -w, --output=PATH Write output text to PATH instead of\n");
2405 fprintf(fp
, " the standard output\n");
2407 fprintf(fp
, "Implicit `filter.utils.muxer` component options:\n");
2409 fprintf(fp
, " --clock-force-correlate Assume that clocks are inherently\n");
2410 fprintf(fp
, " correlated across traces\n");
2412 fprintf(fp
, "Implicit `filter.utils.trimmer` component options:\n");
2414 fprintf(fp
, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2415 fprintf(fp
, " time range to BEGIN (see the format of\n");
2416 fprintf(fp
, " BEGIN below)\n");
2417 fprintf(fp
, " -e, --end=END Set the end time of the conversion time\n");
2418 fprintf(fp
, " range to END (see the format of END below)\n");
2419 fprintf(fp
, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2420 fprintf(fp
, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2421 fprintf(fp
, " `]`) (see the format of BEGIN/END below)\n");
2423 fprintf(fp
, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2425 fprintf(fp
, " --debug-info Create an implicit\n");
2426 fprintf(fp
, " `filter.lttng-utils.debug-info` component\n");
2427 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2428 fprintf(fp
, " instead of `/usr/lib/debug`\n");
2429 fprintf(fp
, " --debug-info-full-path Show full debug info source and\n");
2430 fprintf(fp
, " binary paths instead of just names\n");
2431 fprintf(fp
, " --debug-info-target-prefix=DIR\n");
2432 fprintf(fp
, " Use directory DIR as a prefix when\n");
2433 fprintf(fp
, " looking up executables during debug\n");
2434 fprintf(fp
, " info analysis\n");
2436 fprintf(fp
, "Legacy options that still work:\n");
2438 fprintf(fp
, " -i, --input-format=(ctf | lttng-live)\n");
2439 fprintf(fp
, " `ctf`:\n");
2440 fprintf(fp
, " Create an implicit `source.ctf.fs`\n");
2441 fprintf(fp
, " component\n");
2442 fprintf(fp
, " `lttng-live`:\n");
2443 fprintf(fp
, " Create an implicit `source.ctf.lttng-live`\n");
2444 fprintf(fp
, " component\n");
2445 fprintf(fp
, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2446 fprintf(fp
, " `text`:\n");
2447 fprintf(fp
, " Create an implicit `sink.text.pretty`\n");
2448 fprintf(fp
, " component\n");
2449 fprintf(fp
, " `ctf`:\n");
2450 fprintf(fp
, " Create an implicit `sink.ctf.fs`\n");
2451 fprintf(fp
, " component\n");
2452 fprintf(fp
, " `dummy`:\n");
2453 fprintf(fp
, " Create an implicit `sink.utils.dummy`\n");
2454 fprintf(fp
, " component\n");
2455 fprintf(fp
, " `ctf-metadata`:\n");
2456 fprintf(fp
, " Query the `source.ctf.fs` component class\n");
2457 fprintf(fp
, " for metadata text and quit\n");
2459 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
2460 fprintf(fp
, "\n\n");
2461 fprintf(fp
, "Format of BEGIN and END\n");
2462 fprintf(fp
, "-----------------------\n");
2464 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2465 fprintf(fp
, "\n\n");
2466 print_expected_params_format(fp
);
2470 const struct bt_argpar_opt_descr convert_options
[] = {
2471 /* id, short_name, long_name, with_arg */
2472 { OPT_BEGIN
, 'b', "begin", true },
2473 { OPT_CLOCK_CYCLES
, '\0', "clock-cycles", false },
2474 { OPT_CLOCK_DATE
, '\0', "clock-date", false },
2475 { OPT_CLOCK_FORCE_CORRELATE
, '\0', "clock-force-correlate", false },
2476 { OPT_CLOCK_GMT
, '\0', "clock-gmt", false },
2477 { OPT_CLOCK_OFFSET
, '\0', "clock-offset", true },
2478 { OPT_CLOCK_OFFSET_NS
, '\0', "clock-offset-ns", true },
2479 { OPT_CLOCK_SECONDS
, '\0', "clock-seconds", false },
2480 { OPT_COLOR
, '\0', "color", true },
2481 { OPT_COMPONENT
, 'c', "component", true },
2482 { OPT_DEBUG
, 'd', "debug", false },
2483 { OPT_DEBUG_INFO_DIR
, '\0', "debug-info-dir", true },
2484 { OPT_DEBUG_INFO_FULL_PATH
, '\0', "debug-info-full-path", false },
2485 { OPT_DEBUG_INFO_TARGET_PREFIX
, '\0', "debug-info-target-prefix", true },
2486 { OPT_END
, 'e', "end", true },
2487 { OPT_FIELDS
, 'f', "fields", true },
2488 { OPT_HELP
, 'h', "help", false },
2489 { OPT_INPUT_FORMAT
, 'i', "input-format", true },
2490 { OPT_LOG_LEVEL
, 'l', "log-level", true },
2491 { OPT_NAME
, '\0', "name", true },
2492 { OPT_NAMES
, 'n', "names", true },
2493 { OPT_DEBUG_INFO
, '\0', "debug-info", false },
2494 { OPT_NO_DELTA
, '\0', "no-delta", false },
2495 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
2496 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
2497 { OPT_OUTPUT
, 'w', "output", true },
2498 { OPT_OUTPUT_FORMAT
, 'o', "output-format", true },
2499 { OPT_PARAMS
, 'p', "params", true },
2500 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
2501 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
2502 { OPT_RUN_ARGS
, '\0', "run-args", false },
2503 { OPT_RUN_ARGS_0
, '\0', "run-args-0", false },
2504 { OPT_STREAM_INTERSECTION
, '\0', "stream-intersection", false },
2505 { OPT_TIMERANGE
, '\0', "timerange", true },
2506 { OPT_VERBOSE
, 'v', "verbose", false },
2507 BT_ARGPAR_OPT_DESCR_SENTINEL
2511 GString
*get_component_auto_name(const char *prefix
,
2512 const bt_value
*existing_names
)
2515 GString
*auto_name
= g_string_new(NULL
);
2518 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2522 if (!bt_value_map_has_entry(existing_names
, prefix
)) {
2523 g_string_assign(auto_name
, prefix
);
2528 g_string_printf(auto_name
, "%s-%d", prefix
, i
);
2530 } while (bt_value_map_has_entry(existing_names
, auto_name
->str
));
2536 struct implicit_component_args
{
2539 /* The component class name (e.g. src.ctf.fs). */
2542 /* The component instance name. */
2545 GString
*params_arg
;
2546 bt_value
*extra_params
;
2550 int assign_name_to_implicit_component(struct implicit_component_args
*args
,
2551 const char *prefix
, bt_value
*existing_names
,
2552 GList
**comp_names
, bool append_to_comp_names
)
2555 GString
*name
= NULL
;
2557 if (!args
->exists
) {
2561 name
= get_component_auto_name(prefix
,
2569 g_string_assign(args
->name_arg
, name
->str
);
2571 if (bt_value_map_insert_entry(existing_names
, name
->str
,
2573 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2578 if (append_to_comp_names
) {
2579 *comp_names
= g_list_append(*comp_names
, name
);
2585 g_string_free(name
, TRUE
);
2592 int append_run_args_for_implicit_component(
2593 struct implicit_component_args
*impl_args
,
2599 if (!impl_args
->exists
) {
2603 if (bt_value_array_append_string_element(run_args
, "--component")) {
2604 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2608 if (bt_value_array_append_string_element(run_args
, impl_args
->comp_arg
->str
)) {
2609 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2613 if (bt_value_array_append_string_element(run_args
, "--name")) {
2614 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2618 if (bt_value_array_append_string_element(run_args
, impl_args
->name_arg
->str
)) {
2619 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2623 if (impl_args
->params_arg
->len
> 0) {
2624 if (bt_value_array_append_string_element(run_args
, "--params")) {
2625 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2629 if (bt_value_array_append_string_element(run_args
,
2630 impl_args
->params_arg
->str
)) {
2631 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2636 for (i
= 0; i
< bt_value_array_get_size(impl_args
->extra_params
);
2638 const bt_value
*elem
;
2641 elem
= bt_value_array_borrow_element_by_index(impl_args
->extra_params
,
2647 BT_ASSERT(bt_value_is_string(elem
));
2648 arg
= bt_value_string_get(elem
);
2649 ret
= bt_value_array_append_string_element(run_args
, arg
);
2651 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2665 /* Free the fields of a `struct implicit_component_args`. */
2668 void finalize_implicit_component_args(struct implicit_component_args
*args
)
2672 if (args
->comp_arg
) {
2673 g_string_free(args
->comp_arg
, TRUE
);
2676 if (args
->name_arg
) {
2677 g_string_free(args
->name_arg
, TRUE
);
2680 if (args
->params_arg
) {
2681 g_string_free(args
->params_arg
, TRUE
);
2684 bt_value_put_ref(args
->extra_params
);
2687 /* Destroy a dynamically-allocated `struct implicit_component_args`. */
2690 void destroy_implicit_component_args(struct implicit_component_args
*args
)
2692 finalize_implicit_component_args(args
);
2696 /* Initialize the fields of an already allocated `struct implicit_component_args`. */
2699 int init_implicit_component_args(struct implicit_component_args
*args
,
2700 const char *comp_arg
, bool exists
)
2704 args
->exists
= exists
;
2705 args
->comp_arg
= g_string_new(comp_arg
);
2706 args
->name_arg
= g_string_new(NULL
);
2707 args
->params_arg
= g_string_new(NULL
);
2708 args
->extra_params
= bt_value_array_create();
2710 if (!args
->comp_arg
|| !args
->name_arg
||
2711 !args
->params_arg
|| !args
->extra_params
) {
2713 finalize_implicit_component_args(args
);
2714 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2722 /* Dynamically allocate and initialize a `struct implicit_component_args`. */
2725 struct implicit_component_args
*create_implicit_component_args(
2726 const char *comp_arg
)
2728 struct implicit_component_args
*args
;
2731 args
= g_new(struct implicit_component_args
, 1);
2733 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2737 status
= init_implicit_component_args(args
, comp_arg
, true);
2748 void append_implicit_component_param(struct implicit_component_args
*args
,
2749 const char *key
, const char *value
)
2754 append_param_arg(args
->params_arg
, key
, value
);
2758 * Append the given parameter (`key=value`) to all component specifications
2759 * in `implicit_comp_args` (an array of `struct implicit_component_args *`)
2760 * which match `comp_arg`.
2762 * Return the number of matching components.
2766 int append_multiple_implicit_components_param(GPtrArray
*implicit_comp_args
,
2767 const char *comp_arg
, const char *key
, const char *value
)
2772 for (i
= 0; i
< implicit_comp_args
->len
; i
++) {
2773 struct implicit_component_args
*args
= implicit_comp_args
->pdata
[i
];
2775 if (strcmp(args
->comp_arg
->str
, comp_arg
) == 0) {
2776 append_implicit_component_param(args
, key
, value
);
2784 /* Escape value to make it suitable to use as a string parameter value. */
2786 gchar
*escape_string_value(const char *value
)
2791 ret
= g_string_new(NULL
);
2793 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2802 g_string_append_c(ret
, '\\');
2806 g_string_append_c(ret
, *in
);
2812 return g_string_free(ret
, FALSE
);
2816 int bt_value_to_cli_param_value_append(const bt_value
*value
, GString
*buf
)
2822 switch (bt_value_get_type(value
)) {
2823 case BT_VALUE_TYPE_STRING
:
2825 const char *str_value
= bt_value_string_get(value
);
2826 gchar
*escaped_str_value
;
2828 escaped_str_value
= escape_string_value(str_value
);
2829 if (!escaped_str_value
) {
2833 g_string_append_printf(buf
, "\"%s\"", escaped_str_value
);
2835 g_free(escaped_str_value
);
2838 case BT_VALUE_TYPE_ARRAY
: {
2839 g_string_append_c(buf
, '[');
2840 uint64_t sz
= bt_value_array_get_size(value
);
2841 for (uint64_t i
= 0; i
< sz
; i
++) {
2842 const bt_value
*item
;
2846 g_string_append(buf
, ", ");
2849 item
= bt_value_array_borrow_element_by_index_const(
2851 ret
= bt_value_to_cli_param_value_append(item
, buf
);
2857 g_string_append_c(buf
, ']');
2871 * Convert `value` to its equivalent representation as a command line parameter
2876 gchar
*bt_value_to_cli_param_value(bt_value
*value
)
2879 gchar
*result
= NULL
;
2881 buf
= g_string_new(NULL
);
2883 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2887 if (bt_value_to_cli_param_value_append(value
, buf
)) {
2891 result
= g_string_free(buf
, FALSE
);
2898 g_string_free(buf
, TRUE
);
2906 int append_parameter_to_args(bt_value
*args
, const char *key
, bt_value
*value
)
2909 BT_ASSERT(bt_value_get_type(args
) == BT_VALUE_TYPE_ARRAY
);
2914 gchar
*str_value
= NULL
;
2915 GString
*parameter
= NULL
;
2917 if (bt_value_array_append_string_element(args
, "--params")) {
2918 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2923 str_value
= bt_value_to_cli_param_value(value
);
2929 parameter
= g_string_new(NULL
);
2931 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2936 g_string_printf(parameter
, "%s=%s", key
, str_value
);
2938 if (bt_value_array_append_string_element(args
, parameter
->str
)) {
2939 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2946 g_string_free(parameter
, TRUE
);
2959 int append_string_parameter_to_args(bt_value
*args
, const char *key
, const char *value
)
2961 bt_value
*str_value
;
2964 str_value
= bt_value_string_create_init(value
);
2967 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2972 ret
= append_parameter_to_args(args
, key
, str_value
);
2975 BT_VALUE_PUT_REF_AND_RESET(str_value
);
2980 int append_implicit_component_extra_param(struct implicit_component_args
*args
,
2981 const char *key
, const char *value
)
2983 return append_string_parameter_to_args(args
->extra_params
, key
, value
);
2987 int convert_append_name_param(enum bt_config_component_dest dest
,
2988 GString
*cur_name
, GString
*cur_name_prefix
,
2990 bt_value
*all_names
,
2991 GList
**source_names
, GList
**filter_names
,
2996 if (cur_name_prefix
->len
> 0) {
2997 /* We're after a --component option */
2998 GString
*name
= NULL
;
2999 bool append_name_opt
= false;
3001 if (cur_name
->len
== 0) {
3003 * No explicit name was provided for the user
3006 name
= get_component_auto_name(cur_name_prefix
->str
,
3008 append_name_opt
= true;
3011 * An explicit name was provided for the user
3014 if (bt_value_map_has_entry(all_names
,
3016 BT_CLI_LOGE_APPEND_CAUSE("Duplicate component instance name:\n %s",
3021 name
= g_string_new(cur_name
->str
);
3025 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3030 * Remember this name globally, for the uniqueness of
3031 * all component names.
3033 if (bt_value_map_insert_entry(all_names
, name
->str
, bt_value_null
)) {
3034 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3039 * Append the --name option if necessary.
3041 if (append_name_opt
) {
3042 if (bt_value_array_append_string_element(run_args
, "--name")) {
3043 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3047 if (bt_value_array_append_string_element(run_args
, name
->str
)) {
3048 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3054 * Remember this name specifically for the type of the
3055 * component. This is to create connection arguments.
3058 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
3059 *source_names
= g_list_append(*source_names
, name
);
3061 case BT_CONFIG_COMPONENT_DEST_FILTER
:
3062 *filter_names
= g_list_append(*filter_names
, name
);
3064 case BT_CONFIG_COMPONENT_DEST_SINK
:
3065 *sink_names
= g_list_append(*sink_names
, name
);
3071 g_string_assign(cur_name_prefix
, "");
3084 * Escapes `.`, `:`, and `\` of `input` with `\`.
3087 GString
*escape_dot_colon(const char *input
)
3089 GString
*output
= g_string_new(NULL
);
3093 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3097 for (ch
= input
; *ch
!= '\0'; ch
++) {
3098 if (*ch
== '\\' || *ch
== '.' || *ch
== ':') {
3099 g_string_append_c(output
, '\\');
3102 g_string_append_c(output
, *ch
);
3110 * Appends a --connect option to a list of arguments. `upstream_name`
3111 * and `downstream_name` are escaped with escape_dot_colon() in this
3115 int append_connect_arg(bt_value
*run_args
,
3116 const char *upstream_name
, const char *downstream_name
)
3119 GString
*e_upstream_name
= escape_dot_colon(upstream_name
);
3120 GString
*e_downstream_name
= escape_dot_colon(downstream_name
);
3121 GString
*arg
= g_string_new(NULL
);
3123 if (!e_upstream_name
|| !e_downstream_name
|| !arg
) {
3124 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3129 ret
= bt_value_array_append_string_element(run_args
, "--connect");
3131 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3136 g_string_append(arg
, e_upstream_name
->str
);
3137 g_string_append_c(arg
, ':');
3138 g_string_append(arg
, e_downstream_name
->str
);
3139 ret
= bt_value_array_append_string_element(run_args
, arg
->str
);
3141 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3148 g_string_free(arg
, TRUE
);
3151 if (e_upstream_name
) {
3152 g_string_free(e_upstream_name
, TRUE
);
3155 if (e_downstream_name
) {
3156 g_string_free(e_downstream_name
, TRUE
);
3163 * Appends the run command's --connect options for the convert command.
3166 int convert_auto_connect(bt_value
*run_args
,
3167 GList
*source_names
, GList
*filter_names
,
3171 GList
*source_at
= source_names
;
3172 GList
*filter_at
= filter_names
;
3174 GList
*sink_at
= sink_names
;
3176 BT_ASSERT(source_names
);
3177 BT_ASSERT(filter_names
);
3178 BT_ASSERT(sink_names
);
3180 /* Connect all sources to the first filter */
3181 for (source_at
= source_names
; source_at
; source_at
= g_list_next(source_at
)) {
3182 GString
*source_name
= source_at
->data
;
3183 GString
*filter_name
= filter_at
->data
;
3185 ret
= append_connect_arg(run_args
, source_name
->str
,
3192 filter_prev
= filter_at
;
3193 filter_at
= g_list_next(filter_at
);
3195 /* Connect remaining filters */
3196 for (; filter_at
; filter_prev
= filter_at
, filter_at
= g_list_next(filter_at
)) {
3197 GString
*filter_name
= filter_at
->data
;
3198 GString
*filter_prev_name
= filter_prev
->data
;
3200 ret
= append_connect_arg(run_args
, filter_prev_name
->str
,
3207 /* Connect last filter to all sinks */
3208 for (sink_at
= sink_names
; sink_at
; sink_at
= g_list_next(sink_at
)) {
3209 GString
*filter_name
= filter_prev
->data
;
3210 GString
*sink_name
= sink_at
->data
;
3212 ret
= append_connect_arg(run_args
, filter_name
->str
,
3229 int split_timerange(const char *arg
, char **begin
, char **end
)
3232 const char *ch
= arg
;
3234 GString
*g_begin
= NULL
;
3235 GString
*g_end
= NULL
;
3243 g_begin
= bt_common_string_until(ch
, "", ",", &end_pos
);
3244 if (!g_begin
|| ch
[end_pos
] != ',' || g_begin
->len
== 0) {
3250 g_end
= bt_common_string_until(ch
, "", "]", &end_pos
);
3251 if (!g_end
|| g_end
->len
== 0) {
3257 *begin
= g_begin
->str
;
3259 g_string_free(g_begin
, FALSE
);
3260 g_string_free(g_end
, FALSE
);
3270 g_string_free(g_begin
, TRUE
);
3274 g_string_free(g_end
, TRUE
);
3281 int g_list_prepend_gstring(GList
**list
, const char *string
)
3284 GString
*gs
= g_string_new(string
);
3289 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3293 *list
= g_list_prepend(*list
, gs
);
3300 * Create `struct implicit_component_args` structures for each of the source
3301 * components we identified. Add them to `component_args`.
3305 void create_implicit_component_args_from_auto_discovered_sources(
3306 const struct auto_source_discovery
*auto_disc
, GPtrArray
*component_args
)
3308 gchar
*cc_name
= NULL
;
3309 struct implicit_component_args
*comp
= NULL
;
3313 len
= auto_disc
->results
->len
;
3315 for (i
= 0; i
< len
; i
++) {
3316 struct auto_source_discovery_result
*res
=
3317 g_ptr_array_index(auto_disc
->results
, i
);
3320 cc_name
= g_strdup_printf("source.%s.%s", res
->plugin_name
, res
->source_cc_name
);
3322 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3326 comp
= create_implicit_component_args(cc_name
);
3331 status
= append_parameter_to_args(comp
->extra_params
, "inputs", res
->inputs
);
3336 g_ptr_array_add(component_args
, comp
);
3344 destroy_implicit_component_args(comp
);
3349 * Creates a Babeltrace config object from the arguments of a convert
3352 * *retcode is set to the appropriate exit code to use.
3355 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3356 int *retcode
, bool force_omit_system_plugin_path
,
3357 bool force_omit_home_plugin_path
,
3358 const bt_value
*initial_plugin_paths
, int *default_log_level
)
3360 enum bt_config_component_dest cur_comp_dest
=
3361 BT_CONFIG_COMPONENT_DEST_UNKNOWN
;
3363 struct bt_config
*cfg
= NULL
;
3364 bool got_input_format_opt
= false;
3365 bool got_output_format_opt
= false;
3366 bool trimmer_has_begin
= false;
3367 bool trimmer_has_end
= false;
3368 bool stream_intersection_mode
= false;
3369 GString
*cur_name
= NULL
;
3370 GString
*cur_name_prefix
= NULL
;
3371 bool print_run_args
= false;
3372 bool print_run_args_0
= false;
3373 bool print_ctf_metadata
= false;
3374 bt_value
*run_args
= NULL
;
3375 bt_value
*all_names
= NULL
;
3376 GList
*source_names
= NULL
;
3377 GList
*filter_names
= NULL
;
3378 GList
*sink_names
= NULL
;
3379 bt_value
*leftovers
= NULL
;
3380 struct implicit_component_args implicit_ctf_output_args
= { 0 };
3381 struct implicit_component_args implicit_lttng_live_args
= { 0 };
3382 struct implicit_component_args implicit_dummy_args
= { 0 };
3383 struct implicit_component_args implicit_text_args
= { 0 };
3384 struct implicit_component_args implicit_debug_info_args
= { 0 };
3385 struct implicit_component_args implicit_muxer_args
= { 0 };
3386 struct implicit_component_args implicit_trimmer_args
= { 0 };
3387 bt_value
*plugin_paths
;
3388 char error_buf
[256] = { 0 };
3390 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
3391 char *output
= NULL
;
3392 struct auto_source_discovery auto_disc
= { NULL
};
3393 GString
*auto_disc_comp_name
= NULL
;
3394 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
3397 * Array of `struct implicit_component_args *` created for the sources
3398 * we have auto-discovered.
3400 GPtrArray
*discovered_source_args
= NULL
;
3403 * If set, restrict automatic source discovery to this component class
3406 const char *auto_source_discovery_restrict_plugin_name
= NULL
;
3407 const char *auto_source_discovery_restrict_component_class_name
= NULL
;
3409 gchar
*ctf_fs_source_clock_class_offset_arg
= NULL
;
3410 gchar
*ctf_fs_source_clock_class_offset_ns_arg
= NULL
;
3412 (void) bt_value_copy(initial_plugin_paths
, &plugin_paths
);
3417 print_convert_usage(stdout
);
3422 if (init_implicit_component_args(&implicit_ctf_output_args
,
3423 "sink.ctf.fs", false)) {
3427 if (init_implicit_component_args(&implicit_lttng_live_args
,
3428 "source.ctf.lttng-live", false)) {
3432 if (init_implicit_component_args(&implicit_text_args
,
3433 "sink.text.pretty", false)) {
3437 if (init_implicit_component_args(&implicit_dummy_args
,
3438 "sink.utils.dummy", false)) {
3442 if (init_implicit_component_args(&implicit_debug_info_args
,
3443 "filter.lttng-utils.debug-info", false)) {
3447 if (init_implicit_component_args(&implicit_muxer_args
,
3448 "filter.utils.muxer", true)) {
3452 if (init_implicit_component_args(&implicit_trimmer_args
,
3453 "filter.utils.trimmer", false)) {
3457 all_names
= bt_value_map_create();
3459 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3463 run_args
= bt_value_array_create();
3465 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3469 cur_name
= g_string_new(NULL
);
3471 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3475 cur_name_prefix
= g_string_new(NULL
);
3476 if (!cur_name_prefix
) {
3477 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3481 ret
= append_env_var_plugin_paths(plugin_paths
);
3486 leftovers
= bt_value_array_create();
3488 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3492 if (auto_source_discovery_init(&auto_disc
) != 0) {
3496 discovered_source_args
=
3497 g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_implicit_component_args
);
3498 if (!discovered_source_args
) {
3499 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3503 auto_disc_comp_name
= g_string_new(NULL
);
3504 if (!auto_disc_comp_name
) {
3505 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3510 * First pass: collect all arguments which need to be passed
3511 * as is to the run command. This pass can also add --name
3512 * arguments if needed to automatically name unnamed component
3515 * Also it appends the plugin paths of --plugin-path to
3518 argpar_parse_ret
= bt_argpar_parse(argc
, argv
, convert_options
, true);
3519 if (argpar_parse_ret
.error
) {
3520 BT_CLI_LOGE_APPEND_CAUSE(
3521 "While parsing `convert` command's command-line arguments: %s",
3522 argpar_parse_ret
.error
->str
);
3526 if (help_option_is_specified(&argpar_parse_ret
)) {
3527 print_convert_usage(stdout
);
3529 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
3533 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
3534 struct bt_argpar_item
*argpar_item
=
3535 g_ptr_array_index(argpar_parse_ret
.items
, i
);
3536 struct bt_argpar_item_opt
*argpar_item_opt
;
3538 char *plugin_name
= NULL
;
3539 char *comp_cls_name
= NULL
;
3542 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
3546 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
3547 arg
= argpar_item_opt
->arg
;
3549 switch (argpar_item_opt
->descr
->id
) {
3552 bt_component_class_type type
;
3553 const char *type_prefix
;
3555 /* Append current component's name if needed */
3556 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
3557 cur_name_prefix
, run_args
, all_names
,
3558 &source_names
, &filter_names
, &sink_names
);
3563 /* Parse the argument */
3564 plugin_comp_cls_names(arg
, &name
, &plugin_name
,
3565 &comp_cls_name
, &type
);
3566 if (!plugin_name
|| !comp_cls_name
) {
3567 BT_CLI_LOGE_APPEND_CAUSE(
3568 "Invalid format for --component option's argument:\n %s",
3574 g_string_assign(cur_name
, name
);
3576 g_string_assign(cur_name
, "");
3580 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
3581 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
3582 type_prefix
= "source";
3584 case BT_COMPONENT_CLASS_TYPE_FILTER
:
3585 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
3586 type_prefix
= "filter";
3588 case BT_COMPONENT_CLASS_TYPE_SINK
:
3589 cur_comp_dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
3590 type_prefix
= "sink";
3596 if (bt_value_array_append_string_element(run_args
,
3598 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3602 if (bt_value_array_append_string_element(run_args
, arg
)) {
3603 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3607 g_string_assign(cur_name_prefix
, "");
3608 g_string_append_printf(cur_name_prefix
, "%s.%s.%s",
3609 type_prefix
, plugin_name
, comp_cls_name
);
3612 free(comp_cls_name
);
3615 comp_cls_name
= NULL
;
3619 if (cur_name_prefix
->len
== 0) {
3620 BT_CLI_LOGE_APPEND_CAUSE("No current component of which to set parameters:\n %s",
3625 if (bt_value_array_append_string_element(run_args
,
3627 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3631 if (bt_value_array_append_string_element(run_args
, arg
)) {
3632 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3637 if (cur_name_prefix
->len
== 0) {
3638 BT_CLI_LOGE_APPEND_CAUSE("No current component to name:\n %s",
3643 if (bt_value_array_append_string_element(run_args
, "--name")) {
3644 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3648 if (bt_value_array_append_string_element(run_args
, arg
)) {
3649 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3653 g_string_assign(cur_name
, arg
);
3656 if (cur_name_prefix
->len
== 0) {
3657 BT_CLI_LOGE_APPEND_CAUSE("No current component to assign a log level to:\n %s",
3662 if (bt_value_array_append_string_element(run_args
, "--log-level")) {
3663 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3667 if (bt_value_array_append_string_element(run_args
, arg
)) {
3668 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3673 case OPT_OMIT_HOME_PLUGIN_PATH
:
3674 force_omit_home_plugin_path
= true;
3676 if (bt_value_array_append_string_element(run_args
,
3677 "--omit-home-plugin-path")) {
3678 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3682 case OPT_RETRY_DURATION
:
3683 if (bt_value_array_append_string_element(run_args
,
3684 "--retry-duration")) {
3685 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3689 if (bt_value_array_append_string_element(run_args
, arg
)) {
3690 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3694 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
3695 force_omit_system_plugin_path
= true;
3697 if (bt_value_array_append_string_element(run_args
,
3698 "--omit-system-plugin-path")) {
3699 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3703 case OPT_PLUGIN_PATH
:
3704 if (bt_config_append_plugin_paths_check_setuid_setgid(
3705 plugin_paths
, arg
)) {
3709 if (bt_value_array_append_string_element(run_args
,
3711 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3715 if (bt_value_array_append_string_element(run_args
, arg
)) {
3716 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3721 case OPT_CLOCK_CYCLES
:
3722 case OPT_CLOCK_DATE
:
3723 case OPT_CLOCK_FORCE_CORRELATE
:
3725 case OPT_CLOCK_OFFSET
:
3726 case OPT_CLOCK_OFFSET_NS
:
3727 case OPT_CLOCK_SECONDS
:
3730 case OPT_DEBUG_INFO
:
3731 case OPT_DEBUG_INFO_DIR
:
3732 case OPT_DEBUG_INFO_FULL_PATH
:
3733 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3736 case OPT_INPUT_FORMAT
:
3739 case OPT_OUTPUT_FORMAT
:
3742 case OPT_RUN_ARGS_0
:
3743 case OPT_STREAM_INTERSECTION
:
3746 /* Ignore in this pass */
3749 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
3750 argpar_item_opt
->descr
->id
);
3755 /* Append current component's name if needed */
3756 ret
= convert_append_name_param(cur_comp_dest
, cur_name
,
3757 cur_name_prefix
, run_args
, all_names
, &source_names
,
3758 &filter_names
, &sink_names
);
3764 * Second pass: transform the convert-specific options and
3765 * arguments into implicit component instances for the run
3768 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
3769 struct bt_argpar_item
*argpar_item
=
3770 g_ptr_array_index(argpar_parse_ret
.items
, i
);
3771 struct bt_argpar_item_opt
*argpar_item_opt
;
3774 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_OPT
) {
3778 argpar_item_opt
= (struct bt_argpar_item_opt
*) argpar_item
;
3779 arg
= argpar_item_opt
->arg
;
3781 switch (argpar_item_opt
->descr
->id
) {
3783 if (trimmer_has_begin
) {
3784 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
3789 trimmer_has_begin
= true;
3790 ret
= append_implicit_component_extra_param(
3791 &implicit_trimmer_args
, "begin", arg
);
3792 implicit_trimmer_args
.exists
= true;
3798 if (trimmer_has_end
) {
3799 printf("At --end option: --end or --timerange option already specified\n %s\n",
3804 trimmer_has_end
= true;
3805 ret
= append_implicit_component_extra_param(
3806 &implicit_trimmer_args
, "end", arg
);
3807 implicit_trimmer_args
.exists
= true;
3817 if (trimmer_has_begin
|| trimmer_has_end
) {
3818 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
3823 ret
= split_timerange(arg
, &begin
, &end
);
3825 BT_CLI_LOGE_APPEND_CAUSE("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s",
3830 ret
= append_implicit_component_extra_param(
3831 &implicit_trimmer_args
, "begin", begin
);
3832 ret
|= append_implicit_component_extra_param(
3833 &implicit_trimmer_args
, "end", end
);
3834 implicit_trimmer_args
.exists
= true;
3842 case OPT_CLOCK_CYCLES
:
3843 append_implicit_component_param(
3844 &implicit_text_args
, "clock-cycles", "yes");
3845 implicit_text_args
.exists
= true;
3847 case OPT_CLOCK_DATE
:
3848 append_implicit_component_param(
3849 &implicit_text_args
, "clock-date", "yes");
3850 implicit_text_args
.exists
= true;
3852 case OPT_CLOCK_FORCE_CORRELATE
:
3853 append_implicit_component_param(
3854 &implicit_muxer_args
,
3855 "assume-absolute-clock-classes", "yes");
3858 append_implicit_component_param(
3859 &implicit_text_args
, "clock-gmt", "yes");
3860 append_implicit_component_param(
3861 &implicit_trimmer_args
, "gmt", "yes");
3862 implicit_text_args
.exists
= true;
3864 case OPT_CLOCK_OFFSET
:
3865 if (ctf_fs_source_clock_class_offset_arg
) {
3866 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset option\n");
3870 ctf_fs_source_clock_class_offset_arg
= g_strdup(arg
);
3871 if (!ctf_fs_source_clock_class_offset_arg
) {
3872 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3876 case OPT_CLOCK_OFFSET_NS
:
3877 if (ctf_fs_source_clock_class_offset_ns_arg
) {
3878 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset-ns option\n");
3882 ctf_fs_source_clock_class_offset_ns_arg
= g_strdup(arg
);
3883 if (!ctf_fs_source_clock_class_offset_ns_arg
) {
3884 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3888 case OPT_CLOCK_SECONDS
:
3889 append_implicit_component_param(
3890 &implicit_text_args
, "clock-seconds", "yes");
3891 implicit_text_args
.exists
= true;
3894 implicit_text_args
.exists
= true;
3895 ret
= append_implicit_component_extra_param(
3896 &implicit_text_args
, "color", arg
);
3901 case OPT_DEBUG_INFO
:
3902 implicit_debug_info_args
.exists
= true;
3904 case OPT_DEBUG_INFO_DIR
:
3905 implicit_debug_info_args
.exists
= true;
3906 ret
= append_implicit_component_extra_param(
3907 &implicit_debug_info_args
, "debug-info-dir", arg
);
3912 case OPT_DEBUG_INFO_FULL_PATH
:
3913 implicit_debug_info_args
.exists
= true;
3914 append_implicit_component_param(
3915 &implicit_debug_info_args
, "full-path", "yes");
3917 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3918 implicit_debug_info_args
.exists
= true;
3919 ret
= append_implicit_component_extra_param(
3920 &implicit_debug_info_args
,
3921 "target-prefix", arg
);
3928 bt_value
*fields
= fields_from_arg(arg
);
3934 implicit_text_args
.exists
= true;
3935 ret
= insert_flat_params_from_array(
3936 implicit_text_args
.params_arg
,
3938 bt_value_put_ref(fields
);
3946 bt_value
*names
= names_from_arg(arg
);
3952 implicit_text_args
.exists
= true;
3953 ret
= insert_flat_params_from_array(
3954 implicit_text_args
.params_arg
,
3956 bt_value_put_ref(names
);
3963 append_implicit_component_param(
3964 &implicit_text_args
, "no-delta", "yes");
3965 implicit_text_args
.exists
= true;
3967 case OPT_INPUT_FORMAT
:
3968 if (got_input_format_opt
) {
3969 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --input-format option.");
3973 got_input_format_opt
= true;
3975 if (strcmp(arg
, "ctf") == 0) {
3976 auto_source_discovery_restrict_plugin_name
= "ctf";
3977 auto_source_discovery_restrict_component_class_name
= "fs";
3978 } else if (strcmp(arg
, "lttng-live") == 0) {
3979 auto_source_discovery_restrict_plugin_name
= "ctf";
3980 auto_source_discovery_restrict_component_class_name
= "lttng-live";
3981 implicit_lttng_live_args
.exists
= true;
3983 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy input format:\n %s",
3988 case OPT_OUTPUT_FORMAT
:
3989 if (got_output_format_opt
) {
3990 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output-format option.");
3994 got_output_format_opt
= true;
3996 if (strcmp(arg
, "text") == 0) {
3997 implicit_text_args
.exists
= true;
3998 } else if (strcmp(arg
, "ctf") == 0) {
3999 implicit_ctf_output_args
.exists
= true;
4000 } else if (strcmp(arg
, "dummy") == 0) {
4001 implicit_dummy_args
.exists
= true;
4002 } else if (strcmp(arg
, "ctf-metadata") == 0) {
4003 print_ctf_metadata
= true;
4005 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy output format:\n %s",
4012 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output option");
4016 output
= strdup(arg
);
4018 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4023 if (print_run_args_0
) {
4024 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
4028 print_run_args
= true;
4030 case OPT_RUN_ARGS_0
:
4031 if (print_run_args
) {
4032 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
4036 print_run_args_0
= true;
4038 case OPT_STREAM_INTERSECTION
:
4040 * Applies to all traces implementing the
4041 * babeltrace.trace-info query.
4043 stream_intersection_mode
= true;
4046 if (*default_log_level
!= BT_LOG_TRACE
&&
4047 *default_log_level
!= BT_LOG_DEBUG
) {
4048 *default_log_level
= BT_LOG_INFO
;
4052 *default_log_level
= BT_LOG_TRACE
;
4060 * Legacy behaviour: --verbose used to make the `text` output
4061 * format print more information. --verbose is now equivalent to
4062 * the INFO log level, which is why we compare to `BT_LOG_INFO`
4065 if (*default_log_level
== BT_LOG_INFO
) {
4066 append_implicit_component_param(&implicit_text_args
,
4071 * Append home and system plugin paths now that we possibly got
4074 if (append_home_and_system_plugin_paths(plugin_paths
,
4075 force_omit_system_plugin_path
,
4076 force_omit_home_plugin_path
)) {
4080 /* Consume and keep leftover arguments */
4081 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
4082 struct bt_argpar_item
*argpar_item
=
4083 g_ptr_array_index(argpar_parse_ret
.items
, i
);
4084 struct bt_argpar_item_non_opt
*argpar_item_non_opt
;
4086 if (argpar_item
->type
!= BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
4090 argpar_item_non_opt
= (struct bt_argpar_item_non_opt
*) argpar_item
;
4092 if (bt_value_array_append_string_element(leftovers
, argpar_item_non_opt
->arg
) !=
4093 BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
4094 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4099 /* Print CTF metadata or print LTTng live sessions */
4100 if (print_ctf_metadata
) {
4101 const bt_value
*bt_val_leftover
;
4103 if (bt_value_array_is_empty(leftovers
)) {
4104 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf-metadata specified without a path.");
4108 if (bt_value_array_get_size(leftovers
) > 1) {
4109 BT_CLI_LOGE_APPEND_CAUSE("Too many paths specified for --output-format=ctf-metadata.");
4113 cfg
= bt_config_print_ctf_metadata_create(plugin_paths
);
4118 bt_val_leftover
= bt_value_array_borrow_element_by_index_const(leftovers
, 0);
4119 g_string_assign(cfg
->cmd_data
.print_ctf_metadata
.path
,
4120 bt_value_string_get(bt_val_leftover
));
4124 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
4132 * If -o ctf was specified, make sure an output path (--output)
4133 * was also specified. --output does not imply -o ctf because
4134 * it's also used for the default, implicit -o text if -o ctf
4137 if (implicit_ctf_output_args
.exists
) {
4139 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf specified without --output (trace output path).");
4144 * At this point we know that -o ctf AND --output were
4145 * specified. Make sure that no options were specified
4146 * which would imply -o text because --output would be
4147 * ambiguous in this case. For example, this is wrong:
4149 * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace
4151 * because --names=all implies -o text, and --output
4152 * could apply to both the sink.text.pretty and
4153 * sink.ctf.fs implicit components.
4155 if (implicit_text_args
.exists
) {
4156 BT_CLI_LOGE_APPEND_CAUSE("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text.");
4162 * If -o dummy and -o ctf were not specified, and if there are
4163 * no explicit sink components, then use an implicit
4164 * `sink.text.pretty` component.
4166 if (!implicit_dummy_args
.exists
&& !implicit_ctf_output_args
.exists
&&
4168 implicit_text_args
.exists
= true;
4172 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4173 * `path` parameter if --output was specified.
4176 if (implicit_text_args
.exists
) {
4177 append_implicit_component_extra_param(&implicit_text_args
,
4179 } else if (implicit_ctf_output_args
.exists
) {
4180 append_implicit_component_extra_param(&implicit_ctf_output_args
,
4185 /* Decide where the leftover argument(s) go */
4186 if (bt_value_array_get_size(leftovers
) > 0) {
4187 if (implicit_lttng_live_args
.exists
) {
4188 const bt_value
*bt_val_leftover
;
4190 if (bt_value_array_get_size(leftovers
) > 1) {
4191 BT_CLI_LOGE_APPEND_CAUSE("Too many URLs specified for --input-format=lttng-live.");
4195 bt_val_leftover
= bt_value_array_borrow_element_by_index_const(leftovers
, 0);
4196 lttng_live_url_parts
=
4197 bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_leftover
),
4198 error_buf
, sizeof(error_buf
));
4199 if (!lttng_live_url_parts
.proto
) {
4200 BT_CLI_LOGE_APPEND_CAUSE("Invalid LTTng live URL format: %s.",
4205 if (!lttng_live_url_parts
.session_name
) {
4206 /* Print LTTng live sessions */
4207 cfg
= bt_config_print_lttng_live_sessions_create(
4213 g_string_assign(cfg
->cmd_data
.print_lttng_live_sessions
.url
,
4214 bt_value_string_get(bt_val_leftover
));
4218 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
4225 ret
= append_implicit_component_extra_param(
4226 &implicit_lttng_live_args
, "url",
4227 bt_value_string_get(bt_val_leftover
));
4232 ret
= append_implicit_component_extra_param(
4233 &implicit_lttng_live_args
,
4234 "session-not-found-action", "end");
4241 status
= auto_discover_source_components(plugin_paths
, leftovers
,
4242 auto_source_discovery_restrict_plugin_name
,
4243 auto_source_discovery_restrict_component_class_name
,
4244 *default_log_level
>= 0 ? *default_log_level
: cli_default_log_level
,
4251 create_implicit_component_args_from_auto_discovered_sources(
4252 &auto_disc
, discovered_source_args
);
4256 /* If --clock-offset was given, apply it to any src.ctf.fs component. */
4257 if (ctf_fs_source_clock_class_offset_arg
) {
4260 n
= append_multiple_implicit_components_param(
4261 discovered_source_args
, "source.ctf.fs", "clock-class-offset-s",
4262 ctf_fs_source_clock_class_offset_arg
);
4265 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset specified, but no source.ctf.fs component instantiated.");
4270 /* If --clock-offset-ns was given, apply it to any src.ctf.fs component. */
4271 if (ctf_fs_source_clock_class_offset_ns_arg
) {
4274 n
= append_multiple_implicit_components_param(
4275 discovered_source_args
, "source.ctf.fs", "clock-class-offset-ns",
4276 ctf_fs_source_clock_class_offset_ns_arg
);
4279 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset-ns specified, but no source.ctf.fs component instantiated.");
4285 * If the implicit `source.ctf.lttng-live` component exists, make sure
4286 * there's at least one leftover (which is the URL).
4288 if (implicit_lttng_live_args
.exists
&& bt_value_array_is_empty(leftovers
)) {
4289 BT_CLI_LOGE_APPEND_CAUSE("Missing URL for implicit `%s` component.",
4290 implicit_lttng_live_args
.comp_arg
->str
);
4294 /* Assign names to implicit components */
4295 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4296 struct implicit_component_args
*args
;
4299 args
= discovered_source_args
->pdata
[i
];
4301 g_string_printf(auto_disc_comp_name
, "auto-disc-%s", args
->comp_arg
->str
);
4303 /* Give it a name like `auto-disc-src-ctf-fs`. */
4304 for (j
= 0; j
< auto_disc_comp_name
->len
; j
++) {
4305 if (auto_disc_comp_name
->str
[j
] == '.') {
4306 auto_disc_comp_name
->str
[j
] = '-';
4310 ret
= assign_name_to_implicit_component(args
,
4311 auto_disc_comp_name
->str
, all_names
, &source_names
, true);
4317 ret
= assign_name_to_implicit_component(&implicit_lttng_live_args
,
4318 "lttng-live", all_names
, &source_names
, true);
4323 ret
= assign_name_to_implicit_component(&implicit_text_args
,
4324 "pretty", all_names
, &sink_names
, true);
4329 ret
= assign_name_to_implicit_component(&implicit_ctf_output_args
,
4330 "sink-ctf-fs", all_names
, &sink_names
, true);
4335 ret
= assign_name_to_implicit_component(&implicit_dummy_args
,
4336 "dummy", all_names
, &sink_names
, true);
4341 ret
= assign_name_to_implicit_component(&implicit_muxer_args
,
4342 "muxer", all_names
, NULL
, false);
4347 ret
= assign_name_to_implicit_component(&implicit_trimmer_args
,
4348 "trimmer", all_names
, NULL
, false);
4353 ret
= assign_name_to_implicit_component(&implicit_debug_info_args
,
4354 "debug-info", all_names
, NULL
, false);
4359 /* Make sure there's at least one source and one sink */
4360 if (!source_names
) {
4361 BT_CLI_LOGE_APPEND_CAUSE("No source component.");
4366 BT_CLI_LOGE_APPEND_CAUSE("No sink component.");
4371 * Prepend the muxer, the trimmer, and the debug info to the
4372 * filter chain so that we have:
4374 * sources -> muxer -> [trimmer] -> [debug info] ->
4375 * [user filters] -> sinks
4377 if (implicit_debug_info_args
.exists
) {
4378 if (g_list_prepend_gstring(&filter_names
,
4379 implicit_debug_info_args
.name_arg
->str
)) {
4384 if (implicit_trimmer_args
.exists
) {
4385 if (g_list_prepend_gstring(&filter_names
,
4386 implicit_trimmer_args
.name_arg
->str
)) {
4391 if (g_list_prepend_gstring(&filter_names
,
4392 implicit_muxer_args
.name_arg
->str
)) {
4397 * Append the equivalent run arguments for the implicit
4400 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4401 struct implicit_component_args
*args
=
4402 discovered_source_args
->pdata
[i
];
4404 ret
= append_run_args_for_implicit_component(args
, run_args
);
4410 ret
= append_run_args_for_implicit_component(&implicit_lttng_live_args
,
4416 ret
= append_run_args_for_implicit_component(&implicit_text_args
,
4422 ret
= append_run_args_for_implicit_component(&implicit_ctf_output_args
,
4428 ret
= append_run_args_for_implicit_component(&implicit_dummy_args
,
4434 ret
= append_run_args_for_implicit_component(&implicit_muxer_args
,
4440 ret
= append_run_args_for_implicit_component(&implicit_trimmer_args
,
4446 ret
= append_run_args_for_implicit_component(&implicit_debug_info_args
,
4452 /* Auto-connect components */
4453 ret
= convert_auto_connect(run_args
, source_names
, filter_names
,
4456 BT_CLI_LOGE_APPEND_CAUSE("Cannot auto-connect components.");
4461 * We have all the run command arguments now. Depending on
4462 * --run-args, we pass this to the run command or print them
4465 if (print_run_args
|| print_run_args_0
) {
4466 if (stream_intersection_mode
) {
4467 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --stream-intersection with --run-args or --run-args-0.");
4471 for (i
= 0; i
< bt_value_array_get_size(run_args
); i
++) {
4472 const bt_value
*arg_value
=
4473 bt_value_array_borrow_element_by_index(run_args
,
4476 GString
*quoted
= NULL
;
4477 const char *arg_to_print
;
4479 BT_ASSERT(arg_value
);
4480 arg
= bt_value_string_get(arg_value
);
4482 if (print_run_args
) {
4483 quoted
= bt_common_shell_quote(arg
, true);
4488 arg_to_print
= quoted
->str
;
4493 printf("%s", arg_to_print
);
4496 g_string_free(quoted
, TRUE
);
4499 if (i
< bt_value_array_get_size(run_args
) - 1) {
4500 if (print_run_args
) {
4509 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4514 * If the log level is still unset at this point, set it to
4515 * the program's default.
4517 if (*default_log_level
< 0) {
4518 *default_log_level
= cli_default_log_level
;
4521 cfg
= bt_config_run_from_args_array(run_args
, retcode
,
4522 force_omit_system_plugin_path
,
4523 force_omit_home_plugin_path
,
4524 initial_plugin_paths
, *default_log_level
);
4529 cfg
->cmd_data
.run
.stream_intersection_mode
= stream_intersection_mode
;
4534 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4538 * If the log level is still unset at this point, set it to
4539 * the program's default.
4541 if (*default_log_level
< 0) {
4542 *default_log_level
= cli_default_log_level
;
4545 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
4550 g_string_free(cur_name
, TRUE
);
4553 if (cur_name_prefix
) {
4554 g_string_free(cur_name_prefix
, TRUE
);
4557 bt_value_put_ref(run_args
);
4558 bt_value_put_ref(all_names
);
4559 destroy_glist_of_gstring(source_names
);
4560 destroy_glist_of_gstring(filter_names
);
4561 destroy_glist_of_gstring(sink_names
);
4562 bt_value_put_ref(leftovers
);
4563 finalize_implicit_component_args(&implicit_ctf_output_args
);
4564 finalize_implicit_component_args(&implicit_lttng_live_args
);
4565 finalize_implicit_component_args(&implicit_dummy_args
);
4566 finalize_implicit_component_args(&implicit_text_args
);
4567 finalize_implicit_component_args(&implicit_debug_info_args
);
4568 finalize_implicit_component_args(&implicit_muxer_args
);
4569 finalize_implicit_component_args(&implicit_trimmer_args
);
4570 bt_value_put_ref(plugin_paths
);
4571 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
4572 auto_source_discovery_fini(&auto_disc
);
4574 if (discovered_source_args
) {
4575 g_ptr_array_free(discovered_source_args
, TRUE
);
4578 g_free(ctf_fs_source_clock_class_offset_arg
);
4579 g_free(ctf_fs_source_clock_class_offset_ns_arg
);
4581 if (auto_disc_comp_name
) {
4582 g_string_free(auto_disc_comp_name
, TRUE
);
4589 * Prints the Babeltrace 2.x general usage.
4592 void print_gen_usage(FILE *fp
)
4594 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4596 fprintf(fp
, "General options:\n");
4598 fprintf(fp
, " -d, --debug Enable debug mode (same as --log-level=V)\n");
4599 fprintf(fp
, " -h, --help Show this help and quit\n");
4600 fprintf(fp
, " -l, --log-level=LVL Set the default log level to LVL (`N`, `V`, `D`,\n");
4601 fprintf(fp
, " `I`, `W` (default), `E`, or `F`)\n");
4602 fprintf(fp
, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4603 fprintf(fp
, " -V, --version Show version and quit\n");
4605 fprintf(fp
, "Available commands:\n");
4607 fprintf(fp
, " convert Convert and trim traces (default)\n");
4608 fprintf(fp
, " help Get help for a plugin or a component class\n");
4609 fprintf(fp
, " list-plugins List available plugins and their content\n");
4610 fprintf(fp
, " query Query objects from a component class\n");
4611 fprintf(fp
, " run Build a processing graph and run it\n");
4613 fprintf(fp
, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n");
4616 struct bt_config
*bt_config_cli_args_create(int argc
, const char *argv
[],
4617 int *retcode
, bool force_omit_system_plugin_path
,
4618 bool force_omit_home_plugin_path
,
4619 const bt_value
*initial_plugin_paths
)
4621 struct bt_config
*config
= NULL
;
4624 const char **top_level_argv
;
4625 int command_argc
= -1;
4626 const char **command_argv
= NULL
;
4627 const char *command_name
= NULL
;
4628 int default_log_level
= -1;
4629 struct bt_argpar_parse_ret argpar_parse_ret
= { 0 };
4631 /* Top-level option descriptions. */
4632 static const struct bt_argpar_opt_descr descrs
[] = {
4633 { OPT_DEBUG
, 'd', "debug", false },
4634 { OPT_HELP
, 'h', "help", false },
4635 { OPT_LOG_LEVEL
, 'l', "log-level", true },
4636 { OPT_VERBOSE
, 'v', "verbose", false },
4637 { OPT_VERSION
, 'V', "version", false},
4638 BT_ARGPAR_OPT_DESCR_SENTINEL
4642 COMMAND_TYPE_NONE
= -1,
4643 COMMAND_TYPE_RUN
= 0,
4644 COMMAND_TYPE_CONVERT
,
4645 COMMAND_TYPE_LIST_PLUGINS
,
4648 } command_type
= COMMAND_TYPE_NONE
;
4652 if (!initial_plugin_paths
) {
4653 initial_plugin_paths
= bt_value_array_create();
4654 if (!initial_plugin_paths
) {
4659 bt_value_get_ref(initial_plugin_paths
);
4665 print_gen_usage(stdout
);
4669 /* Skip first argument, the name of the program. */
4670 top_level_argc
= argc
- 1;
4671 top_level_argv
= argv
+ 1;
4672 argpar_parse_ret
= bt_argpar_parse(top_level_argc
, top_level_argv
,
4675 if (argpar_parse_ret
.error
) {
4676 BT_CLI_LOGE_APPEND_CAUSE(
4677 "While parsing command-line arguments: %s",
4678 argpar_parse_ret
.error
->str
);
4682 for (i
= 0; i
< argpar_parse_ret
.items
->len
; i
++) {
4683 struct bt_argpar_item
*item
;
4685 item
= g_ptr_array_index(argpar_parse_ret
.items
, i
);
4687 if (item
->type
== BT_ARGPAR_ITEM_TYPE_OPT
) {
4688 struct bt_argpar_item_opt
*item_opt
=
4689 (struct bt_argpar_item_opt
*) item
;
4691 switch (item_opt
->descr
->id
) {
4693 default_log_level
= BT_LOG_TRACE
;
4697 * Legacy: do not override a previous
4698 * --debug because --verbose and --debug
4699 * can be specified together (in this
4700 * case we want the lowest log level to
4703 default_log_level
= BT_LOG_INFO
;
4707 bt_log_get_level_from_string(item_opt
->arg
);
4708 if (default_log_level
< 0) {
4709 BT_CLI_LOGE_APPEND_CAUSE(
4710 "Invalid argument for --log-level option:\n %s",
4719 print_gen_usage(stdout
);
4722 } else if (item
->type
== BT_ARGPAR_ITEM_TYPE_NON_OPT
) {
4723 struct bt_argpar_item_non_opt
*item_non_opt
=
4724 (struct bt_argpar_item_non_opt
*) item
;
4726 * First unknown argument: is it a known command
4730 top_level_argc
- item_non_opt
->orig_index
- 1;
4732 &top_level_argv
[item_non_opt
->orig_index
+ 1];
4734 if (strcmp(item_non_opt
->arg
, "convert") == 0) {
4735 command_type
= COMMAND_TYPE_CONVERT
;
4736 } else if (strcmp(item_non_opt
->arg
, "list-plugins") == 0) {
4737 command_type
= COMMAND_TYPE_LIST_PLUGINS
;
4738 } else if (strcmp(item_non_opt
->arg
, "help") == 0) {
4739 command_type
= COMMAND_TYPE_HELP
;
4740 } else if (strcmp(item_non_opt
->arg
, "query") == 0) {
4741 command_type
= COMMAND_TYPE_QUERY
;
4742 } else if (strcmp(item_non_opt
->arg
, "run") == 0) {
4743 command_type
= COMMAND_TYPE_RUN
;
4746 * Non-option argument, but not a known
4747 * command name: assume the default
4748 * `convert` command.
4750 command_type
= COMMAND_TYPE_CONVERT
;
4751 command_name
= "convert";
4759 if (command_type
== COMMAND_TYPE_NONE
) {
4760 if (argpar_parse_ret
.ingested_orig_args
== top_level_argc
) {
4762 * We only got non-help, non-version general options
4763 * like --verbose and --debug, without any other
4764 * arguments, so we can't do anything useful: print the
4767 print_gen_usage(stdout
);
4772 * We stopped on an unknown option argument (and therefore
4773 * didn't see a command name). Assume `convert` command.
4775 command_type
= COMMAND_TYPE_CONVERT
;
4776 command_name
= "convert";
4778 top_level_argc
- argpar_parse_ret
.ingested_orig_args
;
4780 &top_level_argv
[argpar_parse_ret
.ingested_orig_args
];
4783 BT_ASSERT(command_argv
);
4784 BT_ASSERT(command_argc
>= 0);
4787 * The convert command can set its own default log level for
4788 * backward compatibility reasons. It only does so if there's no
4789 * log level yet, so do not force one for this command.
4791 if (command_type
!= COMMAND_TYPE_CONVERT
&& default_log_level
< 0) {
4792 /* Default log level */
4793 default_log_level
= cli_default_log_level
;
4796 switch (command_type
) {
4797 case COMMAND_TYPE_RUN
:
4798 config
= bt_config_run_from_args(command_argc
, command_argv
,
4799 retcode
, force_omit_system_plugin_path
,
4800 force_omit_home_plugin_path
, initial_plugin_paths
,
4803 case COMMAND_TYPE_CONVERT
:
4804 config
= bt_config_convert_from_args(command_argc
, command_argv
,
4805 retcode
, force_omit_system_plugin_path
,
4806 force_omit_home_plugin_path
,
4807 initial_plugin_paths
, &default_log_level
);
4809 case COMMAND_TYPE_LIST_PLUGINS
:
4810 config
= bt_config_list_plugins_from_args(command_argc
,
4811 command_argv
, retcode
, force_omit_system_plugin_path
,
4812 force_omit_home_plugin_path
, initial_plugin_paths
);
4814 case COMMAND_TYPE_HELP
:
4815 config
= bt_config_help_from_args(command_argc
,
4816 command_argv
, retcode
, force_omit_system_plugin_path
,
4817 force_omit_home_plugin_path
, initial_plugin_paths
,
4820 case COMMAND_TYPE_QUERY
:
4821 config
= bt_config_query_from_args(command_argc
,
4822 command_argv
, retcode
, force_omit_system_plugin_path
,
4823 force_omit_home_plugin_path
, initial_plugin_paths
,
4831 BT_ASSERT(default_log_level
>= BT_LOG_TRACE
);
4832 config
->log_level
= default_log_level
;
4833 config
->command_name
= command_name
;
4842 bt_argpar_parse_ret_fini(&argpar_parse_ret
);
4843 bt_value_put_ref(initial_plugin_paths
);