2 * Babeltrace trace converter - parameter parsing
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "CLI/CFG-CLI-ARGS"
31 #include "common/assert.h"
35 #include <babeltrace2/babeltrace.h>
36 #include "common/common.h"
38 #include <sys/types.h>
39 #include "argpar/argpar.h"
40 #include "babeltrace2-cfg.h"
41 #include "babeltrace2-cfg-cli-args.h"
42 #include "babeltrace2-cfg-cli-args-connect.h"
43 #include "param-parse/param-parse.h"
44 #include "babeltrace2-log-level.h"
45 #include "babeltrace2-plugins.h"
46 #include "babeltrace2-query.h"
47 #include "autodisc/autodisc.h"
48 #include "common/version.h"
50 #define BT_CLI_LOGE_APPEND_CAUSE_OOM() BT_CLI_LOGE_APPEND_CAUSE("Out of memory.")
53 * Returns the plugin name, component class name, component class type,
54 * and component name from a command-line --component option's argument.
55 * arg must have the following format:
57 * [NAME:]TYPE.PLUGIN.CLS
59 * where NAME is the optional component name, TYPE is either `source`,
60 * `filter`, or `sink`, PLUGIN is the plugin name, and CLS is the
61 * component class name.
63 * On success, both *plugin and *component are not NULL. *plugin
64 * and *comp_cls are owned by the caller. On success, *name can be NULL
65 * if no component class name was found, and *comp_cls_type is set.
68 void plugin_comp_cls_names(const char *arg
, char **name
, char **plugin
,
69 char **comp_cls
, bt_component_class_type
*comp_cls_type
)
72 GString
*gs_name
= NULL
;
73 GString
*gs_comp_cls_type
= NULL
;
74 GString
*gs_plugin
= NULL
;
75 GString
*gs_comp_cls
= NULL
;
81 BT_ASSERT(comp_cls_type
);
83 if (!bt_common_string_is_printable(arg
)) {
84 BT_CLI_LOGE_APPEND_CAUSE("Argument contains a non-printable character.");
88 /* Parse the component name */
89 gs_name
= bt_common_string_until(at
, ".:\\", ":", &end_pos
);
94 if (arg
[end_pos
] == ':') {
98 g_string_assign(gs_name
, "");
101 /* Parse the component class type */
102 gs_comp_cls_type
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
103 if (!gs_comp_cls_type
|| at
[end_pos
] == '\0') {
104 BT_CLI_LOGE_APPEND_CAUSE("Missing component class type (`source`, `filter`, or `sink`).");
108 if (strcmp(gs_comp_cls_type
->str
, "source") == 0 ||
109 strcmp(gs_comp_cls_type
->str
, "src") == 0) {
110 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SOURCE
;
111 } else if (strcmp(gs_comp_cls_type
->str
, "filter") == 0 ||
112 strcmp(gs_comp_cls_type
->str
, "flt") == 0) {
113 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_FILTER
;
114 } else if (strcmp(gs_comp_cls_type
->str
, "sink") == 0) {
115 *comp_cls_type
= BT_COMPONENT_CLASS_TYPE_SINK
;
117 BT_CLI_LOGE_APPEND_CAUSE("Unknown component class type: `%s`.",
118 gs_comp_cls_type
->str
);
124 /* Parse the plugin name */
125 gs_plugin
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
126 if (!gs_plugin
|| gs_plugin
->len
== 0 || at
[end_pos
] == '\0') {
127 BT_CLI_LOGE_APPEND_CAUSE("Missing plugin or component class name.");
133 /* Parse the component class name */
134 gs_comp_cls
= bt_common_string_until(at
, ".:\\", ".", &end_pos
);
135 if (!gs_comp_cls
|| gs_comp_cls
->len
== 0) {
136 BT_CLI_LOGE_APPEND_CAUSE("Missing component class name.");
140 if (at
[end_pos
] != '\0') {
141 /* Found a non-escaped `.` */
146 if (gs_name
->len
== 0) {
148 g_string_free(gs_name
, TRUE
);
150 *name
= gs_name
->str
;
151 g_string_free(gs_name
, FALSE
);
154 g_string_free(gs_name
, TRUE
);
157 *plugin
= gs_plugin
->str
;
158 *comp_cls
= gs_comp_cls
->str
;
159 g_string_free(gs_plugin
, FALSE
);
160 g_string_free(gs_comp_cls
, FALSE
);
176 g_string_free(gs_name
, TRUE
);
180 g_string_free(gs_plugin
, TRUE
);
184 g_string_free(gs_comp_cls
, TRUE
);
187 if (gs_comp_cls_type
) {
188 g_string_free(gs_comp_cls_type
, TRUE
);
195 * Prints the Babeltrace version.
198 void print_version(void)
200 if (BT_VERSION_GIT
[0] == '\0') {
201 puts("Babeltrace " VERSION
);
203 puts("Babeltrace " VERSION
" - " BT_VERSION_GIT
);
208 * Destroys a component configuration.
211 void bt_config_component_destroy(bt_object
*obj
)
213 struct bt_config_component
*bt_config_component
=
214 container_of(obj
, struct bt_config_component
, base
);
220 if (bt_config_component
->plugin_name
) {
221 g_string_free(bt_config_component
->plugin_name
, TRUE
);
224 if (bt_config_component
->comp_cls_name
) {
225 g_string_free(bt_config_component
->comp_cls_name
, TRUE
);
228 if (bt_config_component
->instance_name
) {
229 g_string_free(bt_config_component
->instance_name
, TRUE
);
232 BT_VALUE_PUT_REF_AND_RESET(bt_config_component
->params
);
233 g_free(bt_config_component
);
240 * Creates a component configuration using the given plugin name and
241 * component name. `plugin_name` and `comp_cls_name` are copied (belong
242 * to the return value).
244 * Return value is owned by the caller.
247 struct bt_config_component
*bt_config_component_create(
248 bt_component_class_type type
,
249 const char *plugin_name
, const char *comp_cls_name
,
252 struct bt_config_component
*cfg_component
= NULL
;
254 cfg_component
= g_new0(struct bt_config_component
, 1);
255 if (!cfg_component
) {
256 BT_CLI_LOGE_APPEND_CAUSE_OOM();
260 bt_object_init_shared(&cfg_component
->base
,
261 bt_config_component_destroy
);
262 cfg_component
->type
= type
;
263 cfg_component
->plugin_name
= g_string_new(plugin_name
);
264 if (!cfg_component
->plugin_name
) {
265 BT_CLI_LOGE_APPEND_CAUSE_OOM();
269 cfg_component
->comp_cls_name
= g_string_new(comp_cls_name
);
270 if (!cfg_component
->comp_cls_name
) {
271 BT_CLI_LOGE_APPEND_CAUSE_OOM();
275 cfg_component
->instance_name
= g_string_new(NULL
);
276 if (!cfg_component
->instance_name
) {
277 BT_CLI_LOGE_APPEND_CAUSE_OOM();
281 cfg_component
->log_level
= init_log_level
;
283 /* Start with empty parameters */
284 cfg_component
->params
= bt_value_map_create();
285 if (!cfg_component
->params
) {
286 BT_CLI_LOGE_APPEND_CAUSE_OOM();
293 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
296 return cfg_component
;
300 * Creates a component configuration from a command-line --component
304 struct bt_config_component
*bt_config_component_from_arg(const char *arg
,
307 struct bt_config_component
*cfg_comp
= NULL
;
309 char *plugin_name
= NULL
;
310 char *comp_cls_name
= NULL
;
311 bt_component_class_type type
;
313 plugin_comp_cls_names(arg
, &name
, &plugin_name
, &comp_cls_name
, &type
);
314 if (!plugin_name
|| !comp_cls_name
) {
318 cfg_comp
= bt_config_component_create(type
, plugin_name
, comp_cls_name
,
325 g_string_assign(cfg_comp
->instance_name
, name
);
331 BT_OBJECT_PUT_REF_AND_RESET(cfg_comp
);
336 g_free(comp_cls_name
);
341 * Destroys a configuration.
344 void bt_config_destroy(bt_object
*obj
)
346 struct bt_config
*cfg
=
347 container_of(obj
, struct bt_config
, base
);
353 BT_VALUE_PUT_REF_AND_RESET(cfg
->plugin_paths
);
355 switch (cfg
->command
) {
356 case BT_CONFIG_COMMAND_RUN
:
357 if (cfg
->cmd_data
.run
.sources
) {
358 g_ptr_array_free(cfg
->cmd_data
.run
.sources
, TRUE
);
361 if (cfg
->cmd_data
.run
.filters
) {
362 g_ptr_array_free(cfg
->cmd_data
.run
.filters
, TRUE
);
365 if (cfg
->cmd_data
.run
.sinks
) {
366 g_ptr_array_free(cfg
->cmd_data
.run
.sinks
, TRUE
);
369 if (cfg
->cmd_data
.run
.connections
) {
370 g_ptr_array_free(cfg
->cmd_data
.run
.connections
,
374 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
376 case BT_CONFIG_COMMAND_HELP
:
377 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.help
.cfg_component
);
379 case BT_CONFIG_COMMAND_QUERY
:
380 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.query
.cfg_component
);
382 if (cfg
->cmd_data
.query
.object
) {
383 g_string_free(cfg
->cmd_data
.query
.object
, TRUE
);
386 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
387 if (cfg
->cmd_data
.print_ctf_metadata
.path
) {
388 g_string_free(cfg
->cmd_data
.print_ctf_metadata
.path
,
391 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
395 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
396 if (cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
398 cfg
->cmd_data
.print_lttng_live_sessions
.url
,
401 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
416 void destroy_glist_of_gstring(GList
*list
)
424 for (at
= list
; at
; at
= g_list_next(at
)) {
425 g_string_free(at
->data
, TRUE
);
432 * Creates a simple lexical scanner for parsing comma-delimited names
435 * Return value is owned by the caller.
438 GScanner
*create_csv_identifiers_scanner(void)
441 GScannerConfig scanner_config
= {
442 .cset_skip_characters
= (gchar
*) " \t\n",
443 .cset_identifier_first
= (gchar
*) G_CSET_a_2_z G_CSET_A_2_Z
"_",
444 .cset_identifier_nth
= (gchar
*) G_CSET_a_2_z G_CSET_A_2_Z
":_-",
445 .case_sensitive
= TRUE
,
446 .cpair_comment_single
= NULL
,
447 .skip_comment_multi
= TRUE
,
448 .skip_comment_single
= TRUE
,
449 .scan_comment_multi
= FALSE
,
450 .scan_identifier
= TRUE
,
451 .scan_identifier_1char
= TRUE
,
452 .scan_identifier_NULL
= FALSE
,
453 .scan_symbols
= FALSE
,
454 .symbol_2_token
= FALSE
,
455 .scope_0_fallback
= FALSE
,
456 .scan_binary
= FALSE
,
460 .scan_hex_dollar
= FALSE
,
461 .numbers_2_int
= FALSE
,
462 .int_2_float
= FALSE
,
463 .store_int64
= FALSE
,
464 .scan_string_sq
= FALSE
,
465 .scan_string_dq
= FALSE
,
466 .identifier_2_string
= FALSE
,
467 .char_2_token
= TRUE
,
470 scanner
= g_scanner_new(&scanner_config
);
472 BT_CLI_LOGE_APPEND_CAUSE_OOM();
479 * Converts a comma-delimited list of known names (--names option) to
480 * an array value object containing those names as string value objects.
482 * Return value is owned by the caller.
485 bt_value
*names_from_arg(const char *arg
)
487 GScanner
*scanner
= NULL
;
488 bt_value
*names
= NULL
;
489 bool found_all
= false, found_none
= false, found_item
= false;
491 names
= bt_value_array_create();
493 BT_CLI_LOGE_APPEND_CAUSE_OOM();
497 scanner
= create_csv_identifiers_scanner();
502 g_scanner_input_text(scanner
, arg
, strlen(arg
));
505 GTokenType token_type
= g_scanner_get_next_token(scanner
);
507 switch (token_type
) {
508 case G_TOKEN_IDENTIFIER
:
510 const char *identifier
= scanner
->value
.v_identifier
;
512 if (strcmp(identifier
, "payload") == 0 ||
513 strcmp(identifier
, "args") == 0 ||
514 strcmp(identifier
, "arg") == 0) {
516 if (bt_value_array_append_string_element(names
,
520 } else if (strcmp(identifier
, "context") == 0 ||
521 strcmp(identifier
, "ctx") == 0) {
523 if (bt_value_array_append_string_element(names
,
527 } else if (strcmp(identifier
, "scope") == 0 ||
528 strcmp(identifier
, "header") == 0) {
530 if (bt_value_array_append_string_element(names
,
534 } else if (strcmp(identifier
, "all") == 0) {
536 if (bt_value_array_append_string_element(names
,
540 } else if (strcmp(identifier
, "none") == 0) {
542 if (bt_value_array_append_string_element(names
,
547 BT_CLI_LOGE_APPEND_CAUSE("Unknown name: `%s`.",
563 if (found_none
&& found_all
) {
564 BT_CLI_LOGE_APPEND_CAUSE("Only either `all` or `none` can be specified in the list given to the --names option, but not both.");
568 * Legacy behavior is to clear the defaults (show none) when at
569 * least one item is specified.
571 if (found_item
&& !found_none
&& !found_all
) {
572 if (bt_value_array_append_string_element(names
, "none")) {
577 g_scanner_destroy(scanner
);
582 BT_VALUE_PUT_REF_AND_RESET(names
);
584 g_scanner_destroy(scanner
);
590 * Converts a comma-delimited list of known fields (--fields option) to
591 * an array value object containing those fields as string
594 * Return value is owned by the caller.
597 bt_value
*fields_from_arg(const char *arg
)
599 GScanner
*scanner
= NULL
;
602 fields
= bt_value_array_create();
604 BT_CLI_LOGE_APPEND_CAUSE_OOM();
608 scanner
= create_csv_identifiers_scanner();
613 g_scanner_input_text(scanner
, arg
, strlen(arg
));
616 GTokenType token_type
= g_scanner_get_next_token(scanner
);
618 switch (token_type
) {
619 case G_TOKEN_IDENTIFIER
:
621 const char *identifier
= scanner
->value
.v_identifier
;
623 if (strcmp(identifier
, "trace") == 0 ||
624 strcmp(identifier
, "trace:hostname") == 0 ||
625 strcmp(identifier
, "trace:domain") == 0 ||
626 strcmp(identifier
, "trace:procname") == 0 ||
627 strcmp(identifier
, "trace:vpid") == 0 ||
628 strcmp(identifier
, "loglevel") == 0 ||
629 strcmp(identifier
, "emf") == 0 ||
630 strcmp(identifier
, "callsite") == 0 ||
631 strcmp(identifier
, "all") == 0) {
632 if (bt_value_array_append_string_element(fields
,
637 BT_CLI_LOGE_APPEND_CAUSE("Unknown field: `%s`.",
655 BT_VALUE_PUT_REF_AND_RESET(fields
);
659 g_scanner_destroy(scanner
);
665 void append_param_arg(GString
*params_arg
, const char *key
, const char *value
)
667 BT_ASSERT(params_arg
);
671 if (params_arg
->len
!= 0) {
672 g_string_append_c(params_arg
, ',');
675 g_string_append(params_arg
, key
);
676 g_string_append_c(params_arg
, '=');
677 g_string_append(params_arg
, value
);
681 * Inserts the equivalent "prefix-NAME=yes" strings into params_arg
682 * where the names are in names_array.
685 int insert_flat_params_from_array(GString
*params_arg
,
686 const bt_value
*names_array
, const char *prefix
)
690 GString
*tmpstr
= NULL
, *default_value
= NULL
;
691 bool default_set
= false, non_default_set
= false;
694 * names_array may be NULL if no CLI options were specified to
695 * trigger its creation.
701 tmpstr
= g_string_new(NULL
);
703 BT_CLI_LOGE_APPEND_CAUSE_OOM();
708 default_value
= g_string_new(NULL
);
709 if (!default_value
) {
710 BT_CLI_LOGE_APPEND_CAUSE_OOM();
715 for (i
= 0; i
< bt_value_array_get_length(names_array
); i
++) {
716 const bt_value
*str_obj
=
717 bt_value_array_borrow_element_by_index_const(names_array
,
720 bool is_default
= false;
722 suffix
= bt_value_string_get(str_obj
);
724 g_string_assign(tmpstr
, prefix
);
725 g_string_append(tmpstr
, "-");
727 /* Special-case for "all" and "none". */
728 if (strcmp(suffix
, "all") == 0) {
730 g_string_assign(default_value
, "show");
731 } else if (strcmp(suffix
, "none") == 0) {
733 g_string_assign(default_value
, "hide");
737 g_string_append(tmpstr
, "default");
738 append_param_arg(params_arg
, tmpstr
->str
,
741 non_default_set
= true;
742 g_string_append(tmpstr
, suffix
);
743 append_param_arg(params_arg
, tmpstr
->str
, "yes");
747 /* Implicit field-default=hide if any non-default option is set. */
748 if (non_default_set
&& !default_set
) {
749 g_string_assign(tmpstr
, prefix
);
750 g_string_append(tmpstr
, "-default");
751 g_string_assign(default_value
, "hide");
752 append_param_arg(params_arg
, tmpstr
->str
, default_value
->str
);
757 g_string_free(default_value
, TRUE
);
761 g_string_free(tmpstr
, TRUE
);
774 OPT_CLOCK_FORCE_CORRELATE
,
785 OPT_DEBUG_INFO_FULL_PATH
,
786 OPT_DEBUG_INFO_TARGET_PREFIX
,
795 OPT_OMIT_HOME_PLUGIN_PATH
,
796 OPT_OMIT_SYSTEM_PLUGIN_PATH
,
801 OPT_RESET_BASE_PARAMS
,
805 OPT_STREAM_INTERSECTION
,
811 enum bt_config_component_dest
{
812 BT_CONFIG_COMPONENT_DEST_UNKNOWN
= -1,
813 BT_CONFIG_COMPONENT_DEST_SOURCE
,
814 BT_CONFIG_COMPONENT_DEST_FILTER
,
815 BT_CONFIG_COMPONENT_DEST_SINK
,
819 * Adds a configuration component to the appropriate configuration
820 * array depending on the destination.
823 void add_run_cfg_comp(struct bt_config
*cfg
,
824 struct bt_config_component
*cfg_comp
,
825 enum bt_config_component_dest dest
)
827 bt_object_get_ref(cfg_comp
);
830 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
831 g_ptr_array_add(cfg
->cmd_data
.run
.sources
, cfg_comp
);
833 case BT_CONFIG_COMPONENT_DEST_FILTER
:
834 g_ptr_array_add(cfg
->cmd_data
.run
.filters
, cfg_comp
);
836 case BT_CONFIG_COMPONENT_DEST_SINK
:
837 g_ptr_array_add(cfg
->cmd_data
.run
.sinks
, cfg_comp
);
845 int add_run_cfg_comp_check_name(struct bt_config
*cfg
,
846 struct bt_config_component
*cfg_comp
,
847 enum bt_config_component_dest dest
,
848 bt_value
*instance_names
)
852 if (cfg_comp
->instance_name
->len
== 0) {
853 BT_CLI_LOGE_APPEND_CAUSE("Found an unnamed component.");
858 if (bt_value_map_has_entry(instance_names
,
859 cfg_comp
->instance_name
->str
)) {
860 BT_CLI_LOGE_APPEND_CAUSE("Duplicate component instance name:\n %s",
861 cfg_comp
->instance_name
->str
);
866 if (bt_value_map_insert_entry(instance_names
,
867 cfg_comp
->instance_name
->str
, bt_value_null
)) {
868 BT_CLI_LOGE_APPEND_CAUSE_OOM();
873 add_run_cfg_comp(cfg
, cfg_comp
, dest
);
880 int append_env_var_plugin_paths(bt_value
*plugin_paths
)
885 if (bt_common_is_setuid_setgid()) {
886 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
890 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
895 ret
= bt_config_append_plugin_paths(plugin_paths
, envvar
);
899 BT_CLI_LOGE_APPEND_CAUSE("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH.");
906 int append_home_and_system_plugin_paths(bt_value
*plugin_paths
,
907 bool omit_system_plugin_path
, bool omit_home_plugin_path
)
911 if (!omit_home_plugin_path
) {
912 if (bt_common_is_setuid_setgid()) {
913 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
915 char *home_plugin_dir
= bt_common_get_home_plugin_path(
916 BT_LOG_OUTPUT_LEVEL
);
918 if (home_plugin_dir
) {
919 ret
= bt_config_append_plugin_paths(
920 plugin_paths
, home_plugin_dir
);
921 free(home_plugin_dir
);
924 BT_CLI_LOGE_APPEND_CAUSE("Invalid home plugin path.");
931 if (!omit_system_plugin_path
) {
932 if (bt_config_append_plugin_paths(plugin_paths
,
933 bt_common_get_system_plugin_path())) {
934 BT_CLI_LOGE_APPEND_CAUSE("Invalid system plugin path.");
940 BT_CLI_LOGE_APPEND_CAUSE("Cannot append home and system plugin paths.");
945 struct bt_config
*bt_config_base_create(enum bt_config_command command
,
946 const bt_value
*plugin_paths
, bool needs_plugins
)
948 struct bt_config
*cfg
;
951 cfg
= g_new0(struct bt_config
, 1);
953 BT_CLI_LOGE_APPEND_CAUSE_OOM();
957 bt_object_init_shared(&cfg
->base
, bt_config_destroy
);
958 cfg
->command
= command
;
959 cfg
->command_needs_plugins
= needs_plugins
;
962 bt_value
*plugin_paths_copy
;
964 (void) bt_value_copy(plugin_paths
,
966 cfg
->plugin_paths
= plugin_paths_copy
;
968 cfg
->plugin_paths
= bt_value_array_create();
969 if (!cfg
->plugin_paths
) {
970 BT_CLI_LOGE_APPEND_CAUSE_OOM();
978 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
985 struct bt_config
*bt_config_run_create(const bt_value
*plugin_paths
)
987 struct bt_config
*cfg
;
990 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_RUN
,
996 cfg
->cmd_data
.run
.sources
= g_ptr_array_new_with_free_func(
997 (GDestroyNotify
) bt_object_put_ref
);
998 if (!cfg
->cmd_data
.run
.sources
) {
999 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1003 cfg
->cmd_data
.run
.filters
= g_ptr_array_new_with_free_func(
1004 (GDestroyNotify
) bt_object_put_ref
);
1005 if (!cfg
->cmd_data
.run
.filters
) {
1006 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1010 cfg
->cmd_data
.run
.sinks
= g_ptr_array_new_with_free_func(
1011 (GDestroyNotify
) bt_object_put_ref
);
1012 if (!cfg
->cmd_data
.run
.sinks
) {
1013 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1017 cfg
->cmd_data
.run
.connections
= g_ptr_array_new_with_free_func(
1018 (GDestroyNotify
) bt_config_connection_destroy
);
1019 if (!cfg
->cmd_data
.run
.connections
) {
1020 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1027 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1034 struct bt_config
*bt_config_list_plugins_create(const bt_value
*plugin_paths
)
1036 return bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS
,
1037 plugin_paths
, true);
1041 struct bt_config
*bt_config_help_create(const bt_value
*plugin_paths
,
1042 int default_log_level
)
1044 struct bt_config
*cfg
;
1047 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_HELP
,
1048 plugin_paths
, true);
1053 cfg
->cmd_data
.help
.cfg_component
=
1054 bt_config_component_create(-1, NULL
, NULL
, default_log_level
);
1055 if (!cfg
->cmd_data
.help
.cfg_component
) {
1062 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1069 struct bt_config
*bt_config_query_create(const bt_value
*plugin_paths
)
1071 struct bt_config
*cfg
;
1074 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_QUERY
,
1075 plugin_paths
, true);
1080 cfg
->cmd_data
.query
.object
= g_string_new(NULL
);
1081 if (!cfg
->cmd_data
.query
.object
) {
1082 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1089 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1096 struct bt_config
*bt_config_print_ctf_metadata_create(
1097 const bt_value
*plugin_paths
)
1099 struct bt_config
*cfg
;
1102 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_CTF_METADATA
,
1103 plugin_paths
, true);
1108 cfg
->cmd_data
.print_ctf_metadata
.path
= g_string_new(NULL
);
1109 if (!cfg
->cmd_data
.print_ctf_metadata
.path
) {
1110 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1114 cfg
->cmd_data
.print_ctf_metadata
.output_path
= g_string_new(NULL
);
1115 if (!cfg
->cmd_data
.print_ctf_metadata
.output_path
) {
1116 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1123 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1130 struct bt_config
*bt_config_print_lttng_live_sessions_create(
1131 const bt_value
*plugin_paths
)
1133 struct bt_config
*cfg
;
1136 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
,
1137 plugin_paths
, true);
1142 cfg
->cmd_data
.print_lttng_live_sessions
.url
= g_string_new(NULL
);
1143 if (!cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
1144 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1148 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
=
1150 if (!cfg
->cmd_data
.print_lttng_live_sessions
.output_path
) {
1151 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1158 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1165 int bt_config_append_plugin_paths_check_setuid_setgid(
1166 bt_value
*plugin_paths
, const char *arg
)
1170 if (bt_common_is_setuid_setgid()) {
1171 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1175 if (bt_config_append_plugin_paths(plugin_paths
, arg
)) {
1176 BT_CLI_LOGE_APPEND_CAUSE("Invalid --plugin-path option's argument:\n %s",
1187 * Prints the expected format for a --params option.
1190 void print_expected_params_format(FILE *fp
)
1192 fprintf(fp
, "Expected format of PARAMS\n");
1193 fprintf(fp
, "-------------------------\n");
1195 fprintf(fp
, " PARAM=VALUE[,PARAM=VALUE]...\n");
1197 fprintf(fp
, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
1198 fprintf(fp
, "where PARAM is the parameter name (C identifier plus the [:.-] characters),\n");
1199 fprintf(fp
, "and VALUE can be one of:\n");
1201 fprintf(fp
, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
1202 fprintf(fp
, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
1203 fprintf(fp
, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
1204 fprintf(fp
, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
1205 fprintf(fp
, " (`0x` prefix) unsigned (with `+` prefix) or signed 64-bit integer.\n");
1206 fprintf(fp
, "* Double precision floating point number (scientific notation is accepted).\n");
1207 fprintf(fp
, "* Unquoted string with no special characters, and not matching any of\n");
1208 fprintf(fp
, " the null and boolean value symbols above.\n");
1209 fprintf(fp
, "* Double-quoted string (accepts escape characters).\n");
1210 fprintf(fp
, "* Array, formatted as an opening `[`, a list of comma-separated values\n");
1211 fprintf(fp
, " (as described by the current list) and a closing `]`.\n");
1212 fprintf(fp
, "* Map, formatted as an opening `{`, a comma-separated list of PARAM=VALUE\n");
1213 fprintf(fp
, " assignments and a closing `}`.\n");
1215 fprintf(fp
, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1217 fprintf(fp
, "Example:\n");
1219 fprintf(fp
, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
1220 fprintf(fp
, " play=+23, observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
1221 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\",\n");
1222 fprintf(fp
, " things=[1, \"2\", 3]\n");
1224 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1225 fprintf(fp
, "babeltrace2 from a shell.\n");
1229 bool help_option_is_specified(
1230 const struct argpar_parse_ret
*argpar_parse_ret
)
1233 bool specified
= false;
1235 for (i
= 0; i
< argpar_parse_ret
->items
->n_items
; i
++) {
1236 struct argpar_item
*argpar_item
=
1237 argpar_parse_ret
->items
->items
[i
];
1238 struct argpar_item_opt
*argpar_item_opt
;
1240 if (argpar_item
->type
!= ARGPAR_ITEM_TYPE_OPT
) {
1244 argpar_item_opt
= (struct argpar_item_opt
*) argpar_item
;
1245 if (argpar_item_opt
->descr
->id
== OPT_HELP
) {
1255 * Prints the help command usage.
1258 void print_help_usage(FILE *fp
)
1260 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1261 fprintf(fp
, " babeltrace2 [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
1263 fprintf(fp
, "Options:\n");
1265 fprintf(fp
, " -h, --help Show this help and quit\n");
1267 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1269 fprintf(fp
, "Use `babeltrace2 list-plugins` to show the list of available plugins.\n");
1273 const struct argpar_opt_descr help_options
[] = {
1274 /* id, short_name, long_name, with_arg */
1275 { OPT_HELP
, 'h', "help", false },
1276 ARGPAR_OPT_DESCR_SENTINEL
1280 * Creates a Babeltrace config object from the arguments of a help
1283 * *retcode is set to the appropriate exit code to use.
1286 struct bt_config
*bt_config_help_from_args(int argc
, const char *argv
[],
1287 int *retcode
, const bt_value
*plugin_paths
,
1288 int default_log_level
)
1290 struct bt_config
*cfg
= NULL
;
1291 char *plugin_name
= NULL
, *comp_cls_name
= NULL
;
1292 struct argpar_parse_ret argpar_parse_ret
= { 0 };
1293 struct argpar_item_non_opt
*non_opt
;
1294 GString
*substring
= NULL
;
1298 cfg
= bt_config_help_create(plugin_paths
, default_log_level
);
1304 argpar_parse_ret
= argpar_parse(argc
, argv
, help_options
, true);
1305 if (argpar_parse_ret
.error
) {
1306 BT_CLI_LOGE_APPEND_CAUSE(
1307 "While parsing `help` command's command-line arguments: %s",
1308 argpar_parse_ret
.error
);
1312 if (help_option_is_specified(&argpar_parse_ret
)) {
1313 print_help_usage(stdout
);
1315 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1319 if (argpar_parse_ret
.items
->n_items
== 0) {
1320 BT_CLI_LOGE_APPEND_CAUSE(
1321 "Missing plugin name or component class descriptor.");
1323 } else if (argpar_parse_ret
.items
->n_items
> 1) {
1325 * At this point we know there are least two non-option
1326 * arguments because we don't reach here with `--help`,
1329 non_opt
= (struct argpar_item_non_opt
*) argpar_parse_ret
.items
->items
[1];
1330 BT_CLI_LOGE_APPEND_CAUSE(
1331 "Extraneous command-line argument specified to `help` command: `%s`.",
1336 non_opt
= (struct argpar_item_non_opt
*) argpar_parse_ret
.items
->items
[0];
1338 /* Look for unescaped dots in the argument. */
1339 substring
= bt_common_string_until(non_opt
->arg
, ".\\", ".", &end_pos
);
1341 BT_CLI_LOGE_APPEND_CAUSE("Could not consume argument: arg=%s",
1346 if (end_pos
== strlen(non_opt
->arg
)) {
1347 /* Didn't find an unescaped dot, treat it as a plugin name. */
1348 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1352 * Found an unescaped dot, treat it as a component class name.
1354 plugin_comp_cls_names(non_opt
->arg
, NULL
, &plugin_name
, &comp_cls_name
,
1355 &cfg
->cmd_data
.help
.cfg_component
->type
);
1356 if (!plugin_name
|| !comp_cls_name
) {
1357 BT_CLI_LOGE_APPEND_CAUSE(
1358 "Could not parse argument as a component class name: arg=%s",
1363 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1365 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
,
1373 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1376 g_free(plugin_name
);
1377 g_free(comp_cls_name
);
1380 g_string_free(substring
, TRUE
);
1383 argpar_parse_ret_fini(&argpar_parse_ret
);
1389 * Prints the help command usage.
1392 void print_query_usage(FILE *fp
)
1394 fprintf(fp
, "Usage: babeltrace2 [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
1396 fprintf(fp
, "Options:\n");
1398 fprintf(fp
, " -p, --params=PARAMS Set the query parameters to PARAMS (see the expected\n");
1399 fprintf(fp
, " format of PARAMS below)\n");
1400 fprintf(fp
, " -h, --help Show this help and quit\n");
1401 fprintf(fp
, "\n\n");
1402 print_expected_params_format(fp
);
1406 const struct argpar_opt_descr query_options
[] = {
1407 /* id, short_name, long_name, with_arg */
1408 { OPT_HELP
, 'h', "help", false },
1409 { OPT_PARAMS
, 'p', "params", true },
1410 ARGPAR_OPT_DESCR_SENTINEL
1414 * Creates a Babeltrace config object from the arguments of a query
1417 * *retcode is set to the appropriate exit code to use.
1420 struct bt_config
*bt_config_query_from_args(int argc
, const char *argv
[],
1421 int *retcode
, const bt_value
*plugin_paths
,
1422 int default_log_level
)
1425 struct bt_config
*cfg
= NULL
;
1426 const char *component_class_spec
= NULL
;
1427 const char *query_object
= NULL
;
1428 GString
*error_str
= NULL
;
1429 struct argpar_parse_ret argpar_parse_ret
= { 0 };
1431 bt_value
*params
= bt_value_map_create();
1433 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1438 cfg
= bt_config_query_create(plugin_paths
);
1443 error_str
= g_string_new(NULL
);
1445 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1450 argpar_parse_ret
= argpar_parse(argc
, argv
, query_options
, true);
1451 if (argpar_parse_ret
.error
) {
1452 BT_CLI_LOGE_APPEND_CAUSE(
1453 "While parsing `query` command's command-line arguments: %s",
1454 argpar_parse_ret
.error
);
1458 if (help_option_is_specified(&argpar_parse_ret
)) {
1459 print_query_usage(stdout
);
1461 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1465 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
1466 struct argpar_item
*argpar_item
=
1467 argpar_parse_ret
.items
->items
[i
];
1469 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1470 struct argpar_item_opt
*argpar_item_opt
=
1471 (struct argpar_item_opt
*) argpar_item
;
1472 const char *arg
= argpar_item_opt
->arg
;
1474 switch (argpar_item_opt
->descr
->id
) {
1477 bt_value
*parsed_params
= bt_param_parse(arg
, error_str
);
1478 bt_value_map_extend_status extend_status
;
1479 if (!parsed_params
) {
1480 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1485 extend_status
= bt_value_map_extend(params
, parsed_params
);
1486 BT_VALUE_PUT_REF_AND_RESET(parsed_params
);
1487 if (extend_status
) {
1488 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current parameters with --params option's argument:\n %s",
1495 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1496 argpar_item_opt
->descr
->id
);
1500 struct argpar_item_non_opt
*argpar_item_non_opt
1501 = (struct argpar_item_non_opt
*) argpar_item
;
1504 * We need exactly two non-option arguments
1505 * which are the mandatory component class
1506 * specification and query object.
1508 if (!component_class_spec
) {
1509 component_class_spec
= argpar_item_non_opt
->arg
;
1510 } else if (!query_object
) {
1511 query_object
= argpar_item_non_opt
->arg
;
1513 BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `query` command: `%s`.",
1514 argpar_item_non_opt
->arg
);
1520 if (!component_class_spec
|| !query_object
) {
1521 print_query_usage(stdout
);
1523 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1527 cfg
->cmd_data
.query
.cfg_component
=
1528 bt_config_component_from_arg(component_class_spec
,
1530 if (!cfg
->cmd_data
.query
.cfg_component
) {
1531 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for component class specification:\n %s",
1532 component_class_spec
);
1537 BT_OBJECT_MOVE_REF(cfg
->cmd_data
.query
.cfg_component
->params
, params
);
1539 if (strlen(query_object
) == 0) {
1540 BT_CLI_LOGE_APPEND_CAUSE("Invalid empty object.");
1544 g_string_assign(cfg
->cmd_data
.query
.object
, query_object
);
1549 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1552 argpar_parse_ret_fini(&argpar_parse_ret
);
1555 g_string_free(error_str
, TRUE
);
1558 bt_value_put_ref(params
);
1563 * Prints the list-plugins command usage.
1566 void print_list_plugins_usage(FILE *fp
)
1568 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
1570 fprintf(fp
, "Options:\n");
1572 fprintf(fp
, " -h, --help Show this help and quit\n");
1574 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1576 fprintf(fp
, "Use `babeltrace2 help` to get help for a specific plugin or component class.\n");
1580 const struct argpar_opt_descr list_plugins_options
[] = {
1581 /* id, short_name, long_name, with_arg */
1582 { OPT_HELP
, 'h', "help", false },
1583 ARGPAR_OPT_DESCR_SENTINEL
1587 * Creates a Babeltrace config object from the arguments of a
1588 * list-plugins command.
1590 * *retcode is set to the appropriate exit code to use.
1593 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
1594 int *retcode
, const bt_value
*plugin_paths
)
1596 struct bt_config
*cfg
= NULL
;
1597 struct argpar_parse_ret argpar_parse_ret
= { 0 };
1600 cfg
= bt_config_list_plugins_create(plugin_paths
);
1606 argpar_parse_ret
= argpar_parse(argc
, argv
, list_plugins_options
, true);
1607 if (argpar_parse_ret
.error
) {
1608 BT_CLI_LOGE_APPEND_CAUSE(
1609 "While parsing `list-plugins` command's command-line arguments: %s",
1610 argpar_parse_ret
.error
);
1614 if (help_option_is_specified(&argpar_parse_ret
)) {
1615 print_list_plugins_usage(stdout
);
1617 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1621 if (argpar_parse_ret
.items
->n_items
> 0) {
1623 * At this point we know there's at least one non-option
1624 * argument because we don't reach here with `--help`,
1627 struct argpar_item_non_opt
*non_opt
=
1628 (struct argpar_item_non_opt
*) argpar_parse_ret
.items
->items
[0];
1630 BT_CLI_LOGE_APPEND_CAUSE(
1631 "Extraneous command-line argument specified to `list-plugins` command: `%s`.",
1640 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1643 argpar_parse_ret_fini(&argpar_parse_ret
);
1649 * Prints the run command usage.
1652 void print_run_usage(FILE *fp
)
1654 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n");
1656 fprintf(fp
, "Options:\n");
1658 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
1659 fprintf(fp
, " for all the following components until\n");
1660 fprintf(fp
, " --reset-base-params is encountered\n");
1661 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1662 fprintf(fp
, " -c, --component=NAME:TYPE.PLUGIN.CLS\n");
1663 fprintf(fp
, " Instantiate the component class CLS of type\n");
1664 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
1665 fprintf(fp
, " in the plugin PLUGIN, add it to the graph,\n");
1666 fprintf(fp
, " and name it NAME");
1667 fprintf(fp
, " -x, --connect=CONNECTION Connect two created components (see the\n");
1668 fprintf(fp
, " expected format of CONNECTION below)\n");
1669 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
1670 fprintf(fp
, " (`N`, `T`, `D`, `I`, `W`, `E`, or `F`)\n");
1671 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
1672 fprintf(fp
, " current component (see the expected format\n");
1673 fprintf(fp
, " of PARAMS below)\n");
1674 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters to an\n");
1675 fprintf(fp
, " empty map\n");
1676 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
1677 fprintf(fp
, " the graph later, retry in DUR µs\n");
1678 fprintf(fp
, " (default: 100000)\n");
1679 fprintf(fp
, " -h, --help Show this help and quit\n");
1681 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1682 fprintf(fp
, "\n\n");
1683 fprintf(fp
, "Expected format of CONNECTION\n");
1684 fprintf(fp
, "-----------------------------\n");
1686 fprintf(fp
, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
1688 fprintf(fp
, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
1689 fprintf(fp
, "components to connect together. You must escape the following characters\n\n");
1690 fprintf(fp
, "with `\\`: `\\`, `.`, and `:`. You must set the name of the current\n");
1691 fprintf(fp
, "component using the NAME prefix of the --component option.\n");
1693 fprintf(fp
, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
1694 fprintf(fp
, "identify the upstream and downstream ports to use for the connection.\n");
1695 fprintf(fp
, "When the port is not specified, `*` is used.\n");
1697 fprintf(fp
, "When a component named UPSTREAM has an available port which matches the\n");
1698 fprintf(fp
, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
1699 fprintf(fp
, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
1700 fprintf(fp
, "DOWNSTREAM.\n");
1702 fprintf(fp
, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
1703 fprintf(fp
, "which matches anything. You must escape the following characters\n");
1704 fprintf(fp
, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
1706 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
1707 fprintf(fp
, "can connect a filter component to a sink component.\n");
1709 fprintf(fp
, "Examples:\n");
1711 fprintf(fp
, " my-src:my-sink\n");
1712 fprintf(fp
, " ctf-fs.*stream*:utils-muxer:*\n");
1714 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1715 fprintf(fp
, "babeltrace2 from a shell.\n");
1716 fprintf(fp
, "\n\n");
1717 print_expected_params_format(fp
);
1721 * Creates a Babeltrace config object from the arguments of a run
1724 * *retcode is set to the appropriate exit code to use.
1727 struct bt_config
*bt_config_run_from_args(int argc
, const char *argv
[],
1728 int *retcode
, const bt_value
*plugin_paths
,
1729 int default_log_level
)
1731 struct bt_config_component
*cur_cfg_comp
= NULL
;
1732 bt_value
*cur_base_params
= NULL
;
1734 struct bt_config
*cfg
= NULL
;
1735 bt_value
*instance_names
= NULL
;
1736 bt_value
*connection_args
= NULL
;
1737 char error_buf
[256] = { 0 };
1738 long retry_duration
= -1;
1739 bt_value_map_extend_status extend_status
;
1740 GString
*error_str
= NULL
;
1741 struct argpar_parse_ret argpar_parse_ret
= { 0 };
1744 static const struct argpar_opt_descr run_options
[] = {
1745 { OPT_BASE_PARAMS
, 'b', "base-params", true },
1746 { OPT_COMPONENT
, 'c', "component", true },
1747 { OPT_CONNECT
, 'x', "connect", true },
1748 { OPT_HELP
, 'h', "help", false },
1749 { OPT_LOG_LEVEL
, 'l', "log-level", true },
1750 { OPT_PARAMS
, 'p', "params", true },
1751 { OPT_RESET_BASE_PARAMS
, 'r', "reset-base-params", false },
1752 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
1753 ARGPAR_OPT_DESCR_SENTINEL
1758 error_str
= g_string_new(NULL
);
1760 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1765 print_run_usage(stdout
);
1770 cfg
= bt_config_run_create(plugin_paths
);
1775 cfg
->cmd_data
.run
.retry_duration_us
= 100000;
1776 cur_base_params
= bt_value_map_create();
1777 if (!cur_base_params
) {
1778 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1782 instance_names
= bt_value_map_create();
1783 if (!instance_names
) {
1784 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1788 connection_args
= bt_value_array_create();
1789 if (!connection_args
) {
1790 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1795 argpar_parse_ret
= argpar_parse(argc
, argv
, run_options
, true);
1796 if (argpar_parse_ret
.error
) {
1797 BT_CLI_LOGE_APPEND_CAUSE(
1798 "While parsing `run` command's command-line arguments: %s",
1799 argpar_parse_ret
.error
);
1803 if (help_option_is_specified(&argpar_parse_ret
)) {
1804 print_run_usage(stdout
);
1806 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1810 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
1811 struct argpar_item
*argpar_item
=
1812 argpar_parse_ret
.items
->items
[i
];
1813 struct argpar_item_opt
*argpar_item_opt
;
1816 /* This command does not accept non-option arguments.*/
1817 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
1818 struct argpar_item_non_opt
*argpar_nonopt_item
=
1819 (struct argpar_item_non_opt
*) argpar_item
;
1821 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`",
1822 argpar_nonopt_item
->arg
);
1826 argpar_item_opt
= (struct argpar_item_opt
*) argpar_item
;
1827 arg
= argpar_item_opt
->arg
;
1829 switch (argpar_item_opt
->descr
->id
) {
1832 enum bt_config_component_dest dest
;
1834 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
1835 cur_cfg_comp
= bt_config_component_from_arg(arg
,
1837 if (!cur_cfg_comp
) {
1838 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --component option's argument:\n %s",
1843 switch (cur_cfg_comp
->type
) {
1844 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1845 dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
1847 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1848 dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
1850 case BT_COMPONENT_CLASS_TYPE_SINK
:
1851 dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
1857 BT_ASSERT(cur_base_params
);
1858 bt_value_put_ref(cur_cfg_comp
->params
);
1859 if (bt_value_copy(cur_base_params
,
1860 &cur_cfg_comp
->params
) < 0) {
1861 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1865 ret
= add_run_cfg_comp_check_name(cfg
,
1878 if (!cur_cfg_comp
) {
1879 BT_CLI_LOGE_APPEND_CAUSE("Cannot add parameters to unavailable component:\n %s",
1884 params
= bt_param_parse(arg
, error_str
);
1886 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1891 extend_status
= bt_value_map_extend(cur_cfg_comp
->params
,
1893 BT_VALUE_PUT_REF_AND_RESET(params
);
1894 if (extend_status
!= BT_VALUE_MAP_EXTEND_STATUS_OK
) {
1895 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current component parameters with --params option's argument:\n %s",
1903 if (!cur_cfg_comp
) {
1904 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the log level of unavailable component:\n %s",
1909 cur_cfg_comp
->log_level
=
1910 bt_log_get_level_from_string(arg
);
1911 if (cur_cfg_comp
->log_level
< 0) {
1912 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
1917 case OPT_BASE_PARAMS
:
1919 bt_value
*params
= bt_param_parse(arg
, error_str
);
1922 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --base-params option's argument:\n %s",
1927 BT_OBJECT_MOVE_REF(cur_base_params
, params
);
1930 case OPT_RESET_BASE_PARAMS
:
1931 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
1932 cur_base_params
= bt_value_map_create();
1933 if (!cur_base_params
) {
1934 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1939 if (bt_value_array_append_string_element(
1940 connection_args
, arg
)) {
1941 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1945 case OPT_RETRY_DURATION
: {
1947 size_t arg_len
= strlen(argpar_item_opt
->arg
);
1949 retry_duration
= g_ascii_strtoll(argpar_item_opt
->arg
, &end
, 10);
1951 if (arg_len
== 0 || end
!= (argpar_item_opt
->arg
+ arg_len
)) {
1952 BT_CLI_LOGE_APPEND_CAUSE(
1953 "Could not parse --retry-duration option's argument as an unsigned integer: `%s`",
1954 argpar_item_opt
->arg
);
1958 if (retry_duration
< 0) {
1959 BT_CLI_LOGE_APPEND_CAUSE("--retry-duration option's argument must be positive or 0: %ld",
1964 cfg
->cmd_data
.run
.retry_duration_us
=
1965 (uint64_t) retry_duration
;
1969 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1970 argpar_item_opt
->descr
->id
);
1975 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
1977 if (cfg
->cmd_data
.run
.sources
->len
== 0) {
1978 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no source component.");
1982 if (cfg
->cmd_data
.run
.sinks
->len
== 0) {
1983 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no sink component.");
1987 ret
= bt_config_cli_args_create_connections(cfg
,
1991 BT_CLI_LOGE_APPEND_CAUSE("Cannot creation connections:\n%s", error_buf
);
1999 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2003 g_string_free(error_str
, TRUE
);
2006 argpar_parse_ret_fini(&argpar_parse_ret
);
2007 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2008 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2009 BT_VALUE_PUT_REF_AND_RESET(instance_names
);
2010 BT_VALUE_PUT_REF_AND_RESET(connection_args
);
2015 struct bt_config
*bt_config_run_from_args_array(const bt_value
*run_args
,
2016 int *retcode
, const bt_value
*plugin_paths
,
2017 int default_log_level
)
2019 struct bt_config
*cfg
= NULL
;
2021 uint64_t i
, len
= bt_value_array_get_length(run_args
);
2023 BT_ASSERT(len
<= SIZE_MAX
);
2024 argv
= calloc((size_t) len
, sizeof(*argv
));
2026 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2030 for (i
= 0; i
< len
; i
++) {
2031 const bt_value
*arg_value
=
2032 bt_value_array_borrow_element_by_index_const(run_args
,
2036 arg
= bt_value_string_get(arg_value
);
2041 cfg
= bt_config_run_from_args((int) len
, argv
, retcode
,
2042 plugin_paths
, default_log_level
);
2050 * Prints the convert command usage.
2053 void print_convert_usage(FILE *fp
)
2055 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2057 fprintf(fp
, "Options:\n");
2059 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2060 fprintf(fp
, " Instantiate the component class CLS of type\n");
2061 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2062 fprintf(fp
, " in the plugin PLUGIN, add it to the\n");
2063 fprintf(fp
, " conversion graph, and optionally name it\n");
2064 fprintf(fp
, " NAME\n");
2065 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
2066 fprintf(fp
, " (`N`, `T`, `D`, `I`, `W`, `E`, or `F`)\n");
2067 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2068 fprintf(fp
, " current component (see the expected format\n");
2069 fprintf(fp
, " of PARAMS below)\n");
2070 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
2071 fprintf(fp
, " the graph later, retry in DUR µs\n");
2072 fprintf(fp
, " (default: 100000)\n");
2073 fprintf(fp
, " dynamic plugins can be loaded\n");
2074 fprintf(fp
, " --run-args Print the equivalent arguments for the\n");
2075 fprintf(fp
, " `run` command to the standard output,\n");
2076 fprintf(fp
, " formatted for a shell, and quit\n");
2077 fprintf(fp
, " --run-args-0 Print the equivalent arguments for the\n");
2078 fprintf(fp
, " `run` command to the standard output,\n");
2079 fprintf(fp
, " formatted for `xargs -0`, and quit\n");
2080 fprintf(fp
, " --stream-intersection Only process events when all streams\n");
2081 fprintf(fp
, " are active\n");
2082 fprintf(fp
, " -h, --help Show this help and quit\n");
2084 fprintf(fp
, "Implicit `source.ctf.fs` component options:\n");
2086 fprintf(fp
, " --clock-force-correlate Force the origin of all clocks\n");
2087 fprintf(fp
, " to the Unix epoch\n");
2088 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2089 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2091 fprintf(fp
, "Implicit `sink.text.pretty` component options:\n");
2093 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
2094 fprintf(fp
, " --clock-date Print timestamp dates\n");
2095 fprintf(fp
, " --clock-gmt Print and parse timestamps in the GMT\n");
2096 fprintf(fp
, " time zone instead of the local time zone\n");
2097 fprintf(fp
, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2098 fprintf(fp
, " of `hh:mm:ss.nnnnnnnnn`\n");
2099 fprintf(fp
, " --color=(never | auto | always)\n");
2100 fprintf(fp
, " Never, automatically, or always emit\n");
2101 fprintf(fp
, " console color codes\n");
2102 fprintf(fp
, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2103 fprintf(fp
, " `all`, `trace`, `trace:hostname`,\n");
2104 fprintf(fp
, " `trace:domain`, `trace:procname`,\n");
2105 fprintf(fp
, " `trace:vpid`, `loglevel`, `emf`\n");
2106 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2107 fprintf(fp
, " `payload` (or `arg` or `args`), `none`,\n");
2108 fprintf(fp
, " `all`, `scope`, `header`, `context`\n");
2109 fprintf(fp
, " (or `ctx`)\n");
2110 fprintf(fp
, " --no-delta Do not print time delta between\n");
2111 fprintf(fp
, " consecutive events\n");
2112 fprintf(fp
, " -w, --output=PATH Write output text to PATH instead of\n");
2113 fprintf(fp
, " the standard output\n");
2115 fprintf(fp
, "Implicit `filter.utils.trimmer` component options:\n");
2117 fprintf(fp
, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2118 fprintf(fp
, " time range to BEGIN (see the format of\n");
2119 fprintf(fp
, " BEGIN below)\n");
2120 fprintf(fp
, " -e, --end=END Set the end time of the conversion time\n");
2121 fprintf(fp
, " range to END (see the format of END below)\n");
2122 fprintf(fp
, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2123 fprintf(fp
, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2124 fprintf(fp
, " `]`) (see the format of BEGIN/END below)\n");
2126 fprintf(fp
, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2128 fprintf(fp
, " --debug-info Create an implicit\n");
2129 fprintf(fp
, " `filter.lttng-utils.debug-info` component\n");
2130 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2131 fprintf(fp
, " instead of `/usr/lib/debug`\n");
2132 fprintf(fp
, " --debug-info-full-path Show full debug info source and\n");
2133 fprintf(fp
, " binary paths instead of just names\n");
2134 fprintf(fp
, " --debug-info-target-prefix=DIR\n");
2135 fprintf(fp
, " Use directory DIR as a prefix when\n");
2136 fprintf(fp
, " looking up executables during debug\n");
2137 fprintf(fp
, " info analysis\n");
2139 fprintf(fp
, "Legacy options that still work:\n");
2141 fprintf(fp
, " -i, --input-format=(ctf | lttng-live)\n");
2142 fprintf(fp
, " `ctf`:\n");
2143 fprintf(fp
, " Create an implicit `source.ctf.fs`\n");
2144 fprintf(fp
, " component\n");
2145 fprintf(fp
, " `lttng-live`:\n");
2146 fprintf(fp
, " Create an implicit `source.ctf.lttng-live`\n");
2147 fprintf(fp
, " component\n");
2148 fprintf(fp
, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2149 fprintf(fp
, " `text`:\n");
2150 fprintf(fp
, " Create an implicit `sink.text.pretty`\n");
2151 fprintf(fp
, " component\n");
2152 fprintf(fp
, " `ctf`:\n");
2153 fprintf(fp
, " Create an implicit `sink.ctf.fs`\n");
2154 fprintf(fp
, " component\n");
2155 fprintf(fp
, " `dummy`:\n");
2156 fprintf(fp
, " Create an implicit `sink.utils.dummy`\n");
2157 fprintf(fp
, " component\n");
2158 fprintf(fp
, " `ctf-metadata`:\n");
2159 fprintf(fp
, " Query the `source.ctf.fs` component class\n");
2160 fprintf(fp
, " for metadata text and quit\n");
2162 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
2163 fprintf(fp
, "\n\n");
2164 fprintf(fp
, "Format of BEGIN and END\n");
2165 fprintf(fp
, "-----------------------\n");
2167 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2168 fprintf(fp
, "\n\n");
2169 print_expected_params_format(fp
);
2173 const struct argpar_opt_descr convert_options
[] = {
2174 /* id, short_name, long_name, with_arg */
2175 { OPT_BEGIN
, 'b', "begin", true },
2176 { OPT_CLOCK_CYCLES
, '\0', "clock-cycles", false },
2177 { OPT_CLOCK_DATE
, '\0', "clock-date", false },
2178 { OPT_CLOCK_FORCE_CORRELATE
, '\0', "clock-force-correlate", false },
2179 { OPT_CLOCK_GMT
, '\0', "clock-gmt", false },
2180 { OPT_CLOCK_OFFSET
, '\0', "clock-offset", true },
2181 { OPT_CLOCK_OFFSET_NS
, '\0', "clock-offset-ns", true },
2182 { OPT_CLOCK_SECONDS
, '\0', "clock-seconds", false },
2183 { OPT_COLOR
, '\0', "color", true },
2184 { OPT_COMPONENT
, 'c', "component", true },
2185 { OPT_DEBUG
, 'd', "debug", false },
2186 { OPT_DEBUG_INFO_DIR
, '\0', "debug-info-dir", true },
2187 { OPT_DEBUG_INFO_FULL_PATH
, '\0', "debug-info-full-path", false },
2188 { OPT_DEBUG_INFO_TARGET_PREFIX
, '\0', "debug-info-target-prefix", true },
2189 { OPT_END
, 'e', "end", true },
2190 { OPT_FIELDS
, 'f', "fields", true },
2191 { OPT_HELP
, 'h', "help", false },
2192 { OPT_INPUT_FORMAT
, 'i', "input-format", true },
2193 { OPT_LOG_LEVEL
, 'l', "log-level", true },
2194 { OPT_NAMES
, 'n', "names", true },
2195 { OPT_DEBUG_INFO
, '\0', "debug-info", false },
2196 { OPT_NO_DELTA
, '\0', "no-delta", false },
2197 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
2198 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
2199 { OPT_OUTPUT
, 'w', "output", true },
2200 { OPT_OUTPUT_FORMAT
, 'o', "output-format", true },
2201 { OPT_PARAMS
, 'p', "params", true },
2202 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
2203 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
2204 { OPT_RUN_ARGS
, '\0', "run-args", false },
2205 { OPT_RUN_ARGS_0
, '\0', "run-args-0", false },
2206 { OPT_STREAM_INTERSECTION
, '\0', "stream-intersection", false },
2207 { OPT_TIMERANGE
, '\0', "timerange", true },
2208 { OPT_VERBOSE
, 'v', "verbose", false },
2209 ARGPAR_OPT_DESCR_SENTINEL
2213 GString
*get_component_auto_name(const char *prefix
,
2214 const bt_value
*existing_names
)
2217 GString
*auto_name
= g_string_new(NULL
);
2220 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2224 if (!bt_value_map_has_entry(existing_names
, prefix
)) {
2225 g_string_assign(auto_name
, prefix
);
2230 g_string_printf(auto_name
, "%s-%d", prefix
, i
);
2232 } while (bt_value_map_has_entry(existing_names
, auto_name
->str
));
2238 struct implicit_component_args
{
2241 /* The component class name (e.g. src.ctf.fs). */
2244 /* The component instance name. */
2247 GString
*params_arg
;
2248 bt_value
*extra_params
;
2252 int assign_name_to_implicit_component(struct implicit_component_args
*args
,
2253 const char *prefix
, bt_value
*existing_names
,
2254 GList
**comp_names
, bool append_to_comp_names
)
2257 GString
*name
= NULL
;
2259 if (!args
->exists
) {
2263 name
= get_component_auto_name(prefix
,
2271 g_string_assign(args
->name_arg
, name
->str
);
2273 if (bt_value_map_insert_entry(existing_names
, name
->str
,
2275 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2280 if (append_to_comp_names
) {
2281 *comp_names
= g_list_append(*comp_names
, name
);
2287 g_string_free(name
, TRUE
);
2294 int append_run_args_for_implicit_component(
2295 struct implicit_component_args
*impl_args
,
2300 GString
*component_arg_for_run
= NULL
;
2302 if (!impl_args
->exists
) {
2306 component_arg_for_run
= g_string_new(NULL
);
2307 if (!component_arg_for_run
) {
2308 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2312 /* Build the full `name:type.plugin.cls`. */
2313 BT_ASSERT(!strchr(impl_args
->name_arg
->str
, '\\'));
2314 BT_ASSERT(!strchr(impl_args
->name_arg
->str
, ':'));
2315 g_string_printf(component_arg_for_run
, "%s:%s",
2316 impl_args
->name_arg
->str
, impl_args
->comp_arg
->str
);
2318 if (bt_value_array_append_string_element(run_args
, "--component")) {
2319 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2323 if (bt_value_array_append_string_element(run_args
,
2324 component_arg_for_run
->str
)) {
2325 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2329 if (impl_args
->params_arg
->len
> 0) {
2330 if (bt_value_array_append_string_element(run_args
, "--params")) {
2331 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2335 if (bt_value_array_append_string_element(run_args
,
2336 impl_args
->params_arg
->str
)) {
2337 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2342 for (i
= 0; i
< bt_value_array_get_length(impl_args
->extra_params
); i
++) {
2343 const bt_value
*elem
;
2346 elem
= bt_value_array_borrow_element_by_index(
2347 impl_args
->extra_params
, i
);
2349 BT_ASSERT(bt_value_is_string(elem
));
2350 arg
= bt_value_string_get(elem
);
2351 ret
= bt_value_array_append_string_element(run_args
, arg
);
2353 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2364 if (component_arg_for_run
) {
2365 g_string_free(component_arg_for_run
, TRUE
);
2371 /* Free the fields of a `struct implicit_component_args`. */
2374 void finalize_implicit_component_args(struct implicit_component_args
*args
)
2378 if (args
->comp_arg
) {
2379 g_string_free(args
->comp_arg
, TRUE
);
2382 if (args
->name_arg
) {
2383 g_string_free(args
->name_arg
, TRUE
);
2386 if (args
->params_arg
) {
2387 g_string_free(args
->params_arg
, TRUE
);
2390 bt_value_put_ref(args
->extra_params
);
2393 /* Destroy a dynamically-allocated `struct implicit_component_args`. */
2396 void destroy_implicit_component_args(struct implicit_component_args
*args
)
2398 finalize_implicit_component_args(args
);
2402 /* Initialize the fields of an already allocated `struct implicit_component_args`. */
2405 int init_implicit_component_args(struct implicit_component_args
*args
,
2406 const char *comp_arg
, bool exists
)
2410 args
->exists
= exists
;
2411 args
->comp_arg
= g_string_new(comp_arg
);
2412 args
->name_arg
= g_string_new(NULL
);
2413 args
->params_arg
= g_string_new(NULL
);
2414 args
->extra_params
= bt_value_array_create();
2416 if (!args
->comp_arg
|| !args
->name_arg
||
2417 !args
->params_arg
|| !args
->extra_params
) {
2419 finalize_implicit_component_args(args
);
2420 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2428 /* Dynamically allocate and initialize a `struct implicit_component_args`. */
2431 struct implicit_component_args
*create_implicit_component_args(
2432 const char *comp_arg
)
2434 struct implicit_component_args
*args
;
2437 args
= g_new(struct implicit_component_args
, 1);
2439 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2443 status
= init_implicit_component_args(args
, comp_arg
, true);
2454 void append_implicit_component_param(struct implicit_component_args
*args
,
2455 const char *key
, const char *value
)
2460 append_param_arg(args
->params_arg
, key
, value
);
2464 * Append the given parameter (`key=value`) to all component specifications
2465 * in `implicit_comp_args` (an array of `struct implicit_component_args *`)
2466 * which match `comp_arg`.
2468 * Return the number of matching components.
2472 int append_multiple_implicit_components_param(GPtrArray
*implicit_comp_args
,
2473 const char *comp_arg
, const char *key
, const char *value
)
2478 for (i
= 0; i
< implicit_comp_args
->len
; i
++) {
2479 struct implicit_component_args
*args
= implicit_comp_args
->pdata
[i
];
2481 if (strcmp(args
->comp_arg
->str
, comp_arg
) == 0) {
2482 append_implicit_component_param(args
, key
, value
);
2490 /* Escape value to make it suitable to use as a string parameter value. */
2492 gchar
*escape_string_value(const char *value
)
2497 ret
= g_string_new(NULL
);
2499 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2508 g_string_append_c(ret
, '\\');
2512 g_string_append_c(ret
, *in
);
2518 return g_string_free(ret
, FALSE
);
2522 int bt_value_to_cli_param_value_append(const bt_value
*value
, GString
*buf
)
2528 switch (bt_value_get_type(value
)) {
2529 case BT_VALUE_TYPE_STRING
:
2531 const char *str_value
= bt_value_string_get(value
);
2532 gchar
*escaped_str_value
;
2534 escaped_str_value
= escape_string_value(str_value
);
2535 if (!escaped_str_value
) {
2539 g_string_append_printf(buf
, "\"%s\"", escaped_str_value
);
2541 g_free(escaped_str_value
);
2544 case BT_VALUE_TYPE_ARRAY
: {
2545 g_string_append_c(buf
, '[');
2546 uint64_t sz
= bt_value_array_get_length(value
);
2547 for (uint64_t i
= 0; i
< sz
; i
++) {
2548 const bt_value
*item
;
2551 g_string_append(buf
, ", ");
2554 item
= bt_value_array_borrow_element_by_index_const(
2556 ret
= bt_value_to_cli_param_value_append(item
, buf
);
2562 g_string_append_c(buf
, ']');
2576 * Convert `value` to its equivalent representation as a command line parameter
2581 gchar
*bt_value_to_cli_param_value(bt_value
*value
)
2584 gchar
*result
= NULL
;
2586 buf
= g_string_new(NULL
);
2588 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2592 if (bt_value_to_cli_param_value_append(value
, buf
)) {
2596 result
= g_string_free(buf
, FALSE
);
2603 g_string_free(buf
, TRUE
);
2611 int append_parameter_to_args(bt_value
*args
, const char *key
, bt_value
*value
)
2614 BT_ASSERT(bt_value_get_type(args
) == BT_VALUE_TYPE_ARRAY
);
2619 gchar
*str_value
= NULL
;
2620 GString
*parameter
= NULL
;
2622 if (bt_value_array_append_string_element(args
, "--params")) {
2623 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2628 str_value
= bt_value_to_cli_param_value(value
);
2634 parameter
= g_string_new(NULL
);
2636 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2641 g_string_printf(parameter
, "%s=%s", key
, str_value
);
2643 if (bt_value_array_append_string_element(args
, parameter
->str
)) {
2644 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2651 g_string_free(parameter
, TRUE
);
2664 int append_string_parameter_to_args(bt_value
*args
, const char *key
, const char *value
)
2666 bt_value
*str_value
;
2669 str_value
= bt_value_string_create_init(value
);
2672 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2677 ret
= append_parameter_to_args(args
, key
, str_value
);
2680 BT_VALUE_PUT_REF_AND_RESET(str_value
);
2685 int append_implicit_component_extra_param(struct implicit_component_args
*args
,
2686 const char *key
, const char *value
)
2688 return append_string_parameter_to_args(args
->extra_params
, key
, value
);
2692 * Escapes `.`, `:`, and `\` of `input` with `\`.
2695 GString
*escape_dot_colon(const char *input
)
2697 GString
*output
= g_string_new(NULL
);
2701 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2705 for (ch
= input
; *ch
!= '\0'; ch
++) {
2706 if (*ch
== '\\' || *ch
== '.' || *ch
== ':') {
2707 g_string_append_c(output
, '\\');
2710 g_string_append_c(output
, *ch
);
2718 * Appends a --connect option to a list of arguments. `upstream_name`
2719 * and `downstream_name` are escaped with escape_dot_colon() in this
2723 int append_connect_arg(bt_value
*run_args
,
2724 const char *upstream_name
, const char *downstream_name
)
2727 GString
*e_upstream_name
= escape_dot_colon(upstream_name
);
2728 GString
*e_downstream_name
= escape_dot_colon(downstream_name
);
2729 GString
*arg
= g_string_new(NULL
);
2731 if (!e_upstream_name
|| !e_downstream_name
|| !arg
) {
2732 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2737 ret
= bt_value_array_append_string_element(run_args
, "--connect");
2739 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2744 g_string_append(arg
, e_upstream_name
->str
);
2745 g_string_append_c(arg
, ':');
2746 g_string_append(arg
, e_downstream_name
->str
);
2747 ret
= bt_value_array_append_string_element(run_args
, arg
->str
);
2749 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2756 g_string_free(arg
, TRUE
);
2759 if (e_upstream_name
) {
2760 g_string_free(e_upstream_name
, TRUE
);
2763 if (e_downstream_name
) {
2764 g_string_free(e_downstream_name
, TRUE
);
2771 * Appends the run command's --connect options for the convert command.
2774 int convert_auto_connect(bt_value
*run_args
,
2775 GList
*source_names
, GList
*filter_names
,
2779 GList
*source_at
= source_names
;
2780 GList
*filter_at
= filter_names
;
2782 GList
*sink_at
= sink_names
;
2784 BT_ASSERT(source_names
);
2785 BT_ASSERT(filter_names
);
2786 BT_ASSERT(sink_names
);
2788 /* Connect all sources to the first filter */
2789 for (source_at
= source_names
; source_at
; source_at
= g_list_next(source_at
)) {
2790 GString
*source_name
= source_at
->data
;
2791 GString
*filter_name
= filter_at
->data
;
2793 ret
= append_connect_arg(run_args
, source_name
->str
,
2800 filter_prev
= filter_at
;
2801 filter_at
= g_list_next(filter_at
);
2803 /* Connect remaining filters */
2804 for (; filter_at
; filter_prev
= filter_at
, filter_at
= g_list_next(filter_at
)) {
2805 GString
*filter_name
= filter_at
->data
;
2806 GString
*filter_prev_name
= filter_prev
->data
;
2808 ret
= append_connect_arg(run_args
, filter_prev_name
->str
,
2815 /* Connect last filter to all sinks */
2816 for (sink_at
= sink_names
; sink_at
; sink_at
= g_list_next(sink_at
)) {
2817 GString
*filter_name
= filter_prev
->data
;
2818 GString
*sink_name
= sink_at
->data
;
2820 ret
= append_connect_arg(run_args
, filter_name
->str
,
2837 int split_timerange(const char *arg
, char **begin
, char **end
)
2840 const char *ch
= arg
;
2842 GString
*g_begin
= NULL
;
2843 GString
*g_end
= NULL
;
2851 g_begin
= bt_common_string_until(ch
, "", ",", &end_pos
);
2852 if (!g_begin
|| ch
[end_pos
] != ',' || g_begin
->len
== 0) {
2858 g_end
= bt_common_string_until(ch
, "", "]", &end_pos
);
2859 if (!g_end
|| g_end
->len
== 0) {
2865 *begin
= g_begin
->str
;
2867 g_string_free(g_begin
, FALSE
);
2868 g_string_free(g_end
, FALSE
);
2878 g_string_free(g_begin
, TRUE
);
2882 g_string_free(g_end
, TRUE
);
2889 int g_list_prepend_gstring(GList
**list
, const char *string
)
2892 GString
*gs
= g_string_new(string
);
2897 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2901 *list
= g_list_prepend(*list
, gs
);
2908 * Create `struct implicit_component_args` structures for each of the
2909 * source components we identified. Add them to `component_args`.
2911 * `non_opts` is an array of the non-option arguments passed on the command
2914 * `non_opt_params` is an array where each element is an array of
2915 * strings containing all the arguments to `--params` that apply to the
2916 * non-option argument at the same index. For example, if, for a
2917 * non-option argument, the following `--params` options applied:
2919 * --params=a=2 --params=b=3,c=4
2921 * its entry in `non_opt_params` would contain
2923 * ["a=2", "b=3,c=4"]
2927 int create_implicit_component_args_from_auto_discovered_sources(
2928 const struct auto_source_discovery
*auto_disc
,
2929 const bt_value
*non_opts
,
2930 const bt_value
*non_opt_params
,
2931 const bt_value
*non_opt_loglevels
,
2932 GPtrArray
*component_args
)
2934 gchar
*cc_name
= NULL
;
2935 struct implicit_component_args
*comp
= NULL
;
2939 len
= auto_disc
->results
->len
;
2941 for (i
= 0; i
< len
; i
++) {
2942 struct auto_source_discovery_result
*res
=
2943 g_ptr_array_index(auto_disc
->results
, i
);
2944 uint64_t orig_indices_i
, orig_indices_count
;
2947 cc_name
= g_strdup_printf("source.%s.%s", res
->plugin_name
, res
->source_cc_name
);
2949 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2953 comp
= create_implicit_component_args(cc_name
);
2959 * Append parameters and log levels of all the
2960 * non-option arguments that contributed to this
2961 * component instance coming into existence.
2963 orig_indices_count
= bt_value_array_get_length(res
->original_input_indices
);
2964 for (orig_indices_i
= 0; orig_indices_i
< orig_indices_count
; orig_indices_i
++) {
2965 const bt_value
*orig_idx_value
=
2966 bt_value_array_borrow_element_by_index(
2967 res
->original_input_indices
, orig_indices_i
);
2968 uint64_t orig_idx
= bt_value_integer_unsigned_get(orig_idx_value
);
2969 const bt_value
*params_array
=
2970 bt_value_array_borrow_element_by_index_const(
2971 non_opt_params
, orig_idx
);
2972 uint64_t params_i
, params_count
;
2973 const bt_value
*loglevel_value
;
2975 params_count
= bt_value_array_get_length(params_array
);
2976 for (params_i
= 0; params_i
< params_count
; params_i
++) {
2977 const bt_value
*params_value
=
2978 bt_value_array_borrow_element_by_index_const(
2979 params_array
, params_i
);
2980 const char *params
= bt_value_string_get(params_value
);
2981 bt_value_array_append_element_status append_status
;
2983 append_status
= bt_value_array_append_string_element(
2984 comp
->extra_params
, "--params");
2985 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
2986 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
2990 append_status
= bt_value_array_append_string_element(
2991 comp
->extra_params
, params
);
2992 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
2993 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
2998 loglevel_value
= bt_value_array_borrow_element_by_index_const(
2999 non_opt_loglevels
, orig_idx
);
3000 if (bt_value_get_type(loglevel_value
) == BT_VALUE_TYPE_STRING
) {
3001 const char *loglevel
= bt_value_string_get(loglevel_value
);
3002 bt_value_array_append_element_status append_status
;
3004 append_status
= bt_value_array_append_string_element(
3005 comp
->extra_params
, "--log-level");
3006 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3007 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3011 append_status
= bt_value_array_append_string_element(
3012 comp
->extra_params
, loglevel
);
3013 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3014 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3021 * If single input and a src.ctf.fs component, provide the
3022 * relative path from the path passed on the command line to the
3025 if (bt_value_array_get_length(res
->inputs
) == 1 &&
3026 strcmp(res
->plugin_name
, "ctf") == 0 &&
3027 strcmp(res
->source_cc_name
, "fs") == 0) {
3028 const bt_value
*orig_idx_value
=
3029 bt_value_array_borrow_element_by_index(
3030 res
->original_input_indices
, 0);
3031 uint64_t orig_idx
= bt_value_integer_unsigned_get(orig_idx_value
);
3032 const bt_value
*non_opt_value
=
3033 bt_value_array_borrow_element_by_index_const(
3034 non_opts
, orig_idx
);
3035 const char *non_opt
= bt_value_string_get(non_opt_value
);
3036 const bt_value
*input_value
=
3037 bt_value_array_borrow_element_by_index_const(
3039 const char *input
= bt_value_string_get(input_value
);
3041 BT_ASSERT(orig_indices_count
== 1);
3042 BT_ASSERT(g_str_has_prefix(input
, non_opt
));
3044 input
+= strlen(non_opt
);
3046 while (G_IS_DIR_SEPARATOR(*input
)) {
3050 if (strlen(input
) > 0) {
3051 append_string_parameter_to_args(comp
->extra_params
,
3052 "trace-name", input
);
3056 status
= append_parameter_to_args(comp
->extra_params
, "inputs", res
->inputs
);
3061 g_ptr_array_add(component_args
, comp
);
3075 destroy_implicit_component_args(comp
);
3082 * As we iterate the arguments to the convert command, this tracks what is the
3083 * type of the current item, to which some contextual options (e.g. --params)
3086 enum convert_current_item_type
{
3087 /* There is no current item. */
3088 CONVERT_CURRENT_ITEM_TYPE_NONE
,
3090 /* Current item is a component. */
3091 CONVERT_CURRENT_ITEM_TYPE_COMPONENT
,
3093 /* Current item is a non-option argument. */
3094 CONVERT_CURRENT_ITEM_TYPE_NON_OPT
,
3098 * Creates a Babeltrace config object from the arguments of a convert
3101 * *retcode is set to the appropriate exit code to use.
3104 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3105 int *retcode
, const bt_value
*plugin_paths
,
3106 int *default_log_level
, const bt_interrupter
*interrupter
)
3108 enum convert_current_item_type current_item_type
=
3109 CONVERT_CURRENT_ITEM_TYPE_NONE
;
3111 struct bt_config
*cfg
= NULL
;
3112 bool got_input_format_opt
= false;
3113 bool got_output_format_opt
= false;
3114 bool trimmer_has_begin
= false;
3115 bool trimmer_has_end
= false;
3116 bool stream_intersection_mode
= false;
3117 bool print_run_args
= false;
3118 bool print_run_args_0
= false;
3119 bool print_ctf_metadata
= false;
3120 bt_value
*run_args
= NULL
;
3121 bt_value
*all_names
= NULL
;
3122 GList
*source_names
= NULL
;
3123 GList
*filter_names
= NULL
;
3124 GList
*sink_names
= NULL
;
3125 bt_value
*non_opts
= NULL
;
3126 bt_value
*non_opt_params
= NULL
;
3127 bt_value
*non_opt_loglevels
= NULL
;
3128 struct implicit_component_args implicit_ctf_output_args
= { 0 };
3129 struct implicit_component_args implicit_lttng_live_args
= { 0 };
3130 struct implicit_component_args implicit_dummy_args
= { 0 };
3131 struct implicit_component_args implicit_text_args
= { 0 };
3132 struct implicit_component_args implicit_debug_info_args
= { 0 };
3133 struct implicit_component_args implicit_muxer_args
= { 0 };
3134 struct implicit_component_args implicit_trimmer_args
= { 0 };
3135 char error_buf
[256] = { 0 };
3137 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
3138 char *output
= NULL
;
3139 struct auto_source_discovery auto_disc
= { NULL
};
3140 GString
*auto_disc_comp_name
= NULL
;
3141 struct argpar_parse_ret argpar_parse_ret
= { 0 };
3142 GString
*name_gstr
= NULL
;
3143 GString
*component_arg_for_run
= NULL
;
3144 bt_value
*live_inputs_array_val
= NULL
;
3147 * Array of `struct implicit_component_args *` created for the sources
3148 * we have auto-discovered.
3150 GPtrArray
*discovered_source_args
= NULL
;
3153 * If set, restrict automatic source discovery to this component class
3156 const char *auto_source_discovery_restrict_plugin_name
= NULL
;
3157 const char *auto_source_discovery_restrict_component_class_name
= NULL
;
3159 bool ctf_fs_source_force_clock_class_unix_epoch_origin
= false;
3160 gchar
*ctf_fs_source_clock_class_offset_arg
= NULL
;
3161 gchar
*ctf_fs_source_clock_class_offset_ns_arg
= NULL
;
3165 print_convert_usage(stdout
);
3170 if (init_implicit_component_args(&implicit_ctf_output_args
,
3171 "sink.ctf.fs", false)) {
3175 if (init_implicit_component_args(&implicit_lttng_live_args
,
3176 "source.ctf.lttng-live", false)) {
3180 if (init_implicit_component_args(&implicit_text_args
,
3181 "sink.text.pretty", false)) {
3185 if (init_implicit_component_args(&implicit_dummy_args
,
3186 "sink.utils.dummy", false)) {
3190 if (init_implicit_component_args(&implicit_debug_info_args
,
3191 "filter.lttng-utils.debug-info", false)) {
3195 if (init_implicit_component_args(&implicit_muxer_args
,
3196 "filter.utils.muxer", true)) {
3200 if (init_implicit_component_args(&implicit_trimmer_args
,
3201 "filter.utils.trimmer", false)) {
3205 all_names
= bt_value_map_create();
3207 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3211 run_args
= bt_value_array_create();
3213 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3217 component_arg_for_run
= g_string_new(NULL
);
3218 if (!component_arg_for_run
) {
3219 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3223 non_opts
= bt_value_array_create();
3225 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3229 non_opt_params
= bt_value_array_create();
3230 if (!non_opt_params
) {
3231 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3235 non_opt_loglevels
= bt_value_array_create();
3236 if (!non_opt_loglevels
) {
3237 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3241 if (auto_source_discovery_init(&auto_disc
) != 0) {
3245 discovered_source_args
=
3246 g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_implicit_component_args
);
3247 if (!discovered_source_args
) {
3248 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3252 auto_disc_comp_name
= g_string_new(NULL
);
3253 if (!auto_disc_comp_name
) {
3254 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3259 * First pass: collect all arguments which need to be passed
3260 * as is to the run command. This pass can also add --name
3261 * arguments if needed to automatically name unnamed component
3264 argpar_parse_ret
= argpar_parse(argc
, argv
, convert_options
, true);
3265 if (argpar_parse_ret
.error
) {
3266 BT_CLI_LOGE_APPEND_CAUSE(
3267 "While parsing `convert` command's command-line arguments: %s",
3268 argpar_parse_ret
.error
);
3272 if (help_option_is_specified(&argpar_parse_ret
)) {
3273 print_convert_usage(stdout
);
3275 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
3279 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
3280 struct argpar_item
*argpar_item
=
3281 argpar_parse_ret
.items
->items
[i
];
3282 struct argpar_item_opt
*argpar_item_opt
;
3284 char *plugin_name
= NULL
;
3285 char *comp_cls_name
= NULL
;
3288 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
3289 argpar_item_opt
= (struct argpar_item_opt
*) argpar_item
;
3290 arg
= argpar_item_opt
->arg
;
3292 switch (argpar_item_opt
->descr
->id
) {
3295 bt_component_class_type type
;
3297 current_item_type
= CONVERT_CURRENT_ITEM_TYPE_COMPONENT
;
3299 /* Parse the argument */
3300 plugin_comp_cls_names(arg
, &name
, &plugin_name
,
3301 &comp_cls_name
, &type
);
3302 if (!plugin_name
|| !comp_cls_name
) {
3303 BT_CLI_LOGE_APPEND_CAUSE(
3304 "Invalid format for --component option's argument:\n %s",
3311 * Name was given by the user, verify it isn't
3314 if (bt_value_map_has_entry(all_names
, name
)) {
3315 BT_CLI_LOGE_APPEND_CAUSE(
3316 "Duplicate component instance name:\n %s",
3321 name_gstr
= g_string_new(name
);
3323 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3327 g_string_assign(component_arg_for_run
, arg
);
3329 /* Name not given by user, generate one. */
3330 name_gstr
= get_component_auto_name(arg
, all_names
);
3335 g_string_printf(component_arg_for_run
, "%s:%s",
3336 name_gstr
->str
, arg
);
3339 if (bt_value_array_append_string_element(run_args
,
3341 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3345 if (bt_value_array_append_string_element(run_args
,
3346 component_arg_for_run
->str
)) {
3347 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3352 * Remember this name globally, for the uniqueness of
3353 * all component names.
3355 if (bt_value_map_insert_entry(all_names
,
3356 name_gstr
->str
, bt_value_null
)) {
3357 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3362 * Remember this name specifically for the type of the
3363 * component. This is to create connection arguments.
3365 * The list takes ownership of `name_gstr`.
3368 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
3369 source_names
= g_list_append(source_names
, name_gstr
);
3371 case BT_COMPONENT_CLASS_TYPE_FILTER
:
3372 filter_names
= g_list_append(filter_names
, name_gstr
);
3374 case BT_COMPONENT_CLASS_TYPE_SINK
:
3375 sink_names
= g_list_append(sink_names
, name_gstr
);
3384 free(comp_cls_name
);
3387 comp_cls_name
= NULL
;
3391 if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_COMPONENT
) {
3393 * The current item is a component (--component option),
3394 * pass it directly to the run args.
3396 if (bt_value_array_append_string_element(run_args
,
3398 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3402 if (bt_value_array_append_string_element(run_args
, arg
)) {
3403 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3406 } else if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_NON_OPT
) {
3408 * The current item is a
3409 * non-option argument, record
3410 * it in `non_opt_params`.
3413 bt_value_array_append_element_status append_element_status
;
3414 uint64_t idx
= bt_value_array_get_length(non_opt_params
) - 1;
3416 array
= bt_value_array_borrow_element_by_index(non_opt_params
, idx
);
3418 append_element_status
= bt_value_array_append_string_element(array
, arg
);
3419 if (append_element_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3420 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3424 BT_CLI_LOGE_APPEND_CAUSE(
3425 "No current component (--component option) or non-option argument of which to set parameters:\n %s",
3431 if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_COMPONENT
) {
3432 if (bt_value_array_append_string_element(run_args
, "--log-level")) {
3433 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3437 if (bt_value_array_append_string_element(run_args
, arg
)) {
3438 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3441 } else if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_NON_OPT
) {
3442 uint64_t idx
= bt_value_array_get_length(non_opt_loglevels
) - 1;
3443 enum bt_value_array_set_element_by_index_status set_element_status
;
3444 bt_value
*log_level_str_value
;
3446 log_level_str_value
= bt_value_string_create_init(arg
);
3447 if (!log_level_str_value
) {
3448 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3452 set_element_status
=
3453 bt_value_array_set_element_by_index(non_opt_loglevels
,
3454 idx
, log_level_str_value
);
3455 bt_value_put_ref(log_level_str_value
);
3456 if (set_element_status
!= BT_VALUE_ARRAY_SET_ELEMENT_BY_INDEX_STATUS_OK
) {
3457 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3461 BT_CLI_LOGE_APPEND_CAUSE(
3462 "No current component (--component option) or non-option argument to assign a log level to:\n %s",
3468 case OPT_RETRY_DURATION
:
3469 if (bt_value_array_append_string_element(run_args
,
3470 "--retry-duration")) {
3471 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3475 if (bt_value_array_append_string_element(run_args
, arg
)) {
3476 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3481 case OPT_CLOCK_CYCLES
:
3482 case OPT_CLOCK_DATE
:
3483 case OPT_CLOCK_FORCE_CORRELATE
:
3485 case OPT_CLOCK_OFFSET
:
3486 case OPT_CLOCK_OFFSET_NS
:
3487 case OPT_CLOCK_SECONDS
:
3490 case OPT_DEBUG_INFO
:
3491 case OPT_DEBUG_INFO_DIR
:
3492 case OPT_DEBUG_INFO_FULL_PATH
:
3493 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3496 case OPT_INPUT_FORMAT
:
3499 case OPT_OUTPUT_FORMAT
:
3502 case OPT_RUN_ARGS_0
:
3503 case OPT_STREAM_INTERSECTION
:
3506 /* Ignore in this pass */
3509 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
3510 argpar_item_opt
->descr
->id
);
3513 } else if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
3514 struct argpar_item_non_opt
*argpar_item_non_opt
;
3515 bt_value_array_append_element_status append_status
;
3517 current_item_type
= CONVERT_CURRENT_ITEM_TYPE_NON_OPT
;
3519 argpar_item_non_opt
= (struct argpar_item_non_opt
*) argpar_item
;
3521 append_status
= bt_value_array_append_string_element(non_opts
,
3522 argpar_item_non_opt
->arg
);
3523 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3524 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3528 append_status
= bt_value_array_append_empty_array_element(
3529 non_opt_params
, NULL
);
3530 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3531 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3535 append_status
= bt_value_array_append_element(non_opt_loglevels
, bt_value_null
);
3536 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3537 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3546 * Second pass: transform the convert-specific options and
3547 * arguments into implicit component instances for the run
3550 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
3551 struct argpar_item
*argpar_item
=
3552 argpar_parse_ret
.items
->items
[i
];
3553 struct argpar_item_opt
*argpar_item_opt
;
3556 if (argpar_item
->type
!= ARGPAR_ITEM_TYPE_OPT
) {
3560 argpar_item_opt
= (struct argpar_item_opt
*) argpar_item
;
3561 arg
= argpar_item_opt
->arg
;
3563 switch (argpar_item_opt
->descr
->id
) {
3565 if (trimmer_has_begin
) {
3566 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
3571 trimmer_has_begin
= true;
3572 ret
= append_implicit_component_extra_param(
3573 &implicit_trimmer_args
, "begin", arg
);
3574 implicit_trimmer_args
.exists
= true;
3580 if (trimmer_has_end
) {
3581 printf("At --end option: --end or --timerange option already specified\n %s\n",
3586 trimmer_has_end
= true;
3587 ret
= append_implicit_component_extra_param(
3588 &implicit_trimmer_args
, "end", arg
);
3589 implicit_trimmer_args
.exists
= true;
3599 if (trimmer_has_begin
|| trimmer_has_end
) {
3600 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
3605 ret
= split_timerange(arg
, &begin
, &end
);
3607 BT_CLI_LOGE_APPEND_CAUSE("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s",
3612 ret
= append_implicit_component_extra_param(
3613 &implicit_trimmer_args
, "begin", begin
);
3614 ret
|= append_implicit_component_extra_param(
3615 &implicit_trimmer_args
, "end", end
);
3616 implicit_trimmer_args
.exists
= true;
3624 case OPT_CLOCK_CYCLES
:
3625 append_implicit_component_param(
3626 &implicit_text_args
, "clock-cycles", "yes");
3627 implicit_text_args
.exists
= true;
3629 case OPT_CLOCK_DATE
:
3630 append_implicit_component_param(
3631 &implicit_text_args
, "clock-date", "yes");
3632 implicit_text_args
.exists
= true;
3634 case OPT_CLOCK_FORCE_CORRELATE
:
3635 ctf_fs_source_force_clock_class_unix_epoch_origin
= true;
3638 append_implicit_component_param(
3639 &implicit_text_args
, "clock-gmt", "yes");
3640 append_implicit_component_param(
3641 &implicit_trimmer_args
, "gmt", "yes");
3642 implicit_text_args
.exists
= true;
3644 case OPT_CLOCK_OFFSET
:
3645 if (ctf_fs_source_clock_class_offset_arg
) {
3646 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset option\n");
3650 ctf_fs_source_clock_class_offset_arg
= g_strdup(arg
);
3651 if (!ctf_fs_source_clock_class_offset_arg
) {
3652 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3656 case OPT_CLOCK_OFFSET_NS
:
3657 if (ctf_fs_source_clock_class_offset_ns_arg
) {
3658 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset-ns option\n");
3662 ctf_fs_source_clock_class_offset_ns_arg
= g_strdup(arg
);
3663 if (!ctf_fs_source_clock_class_offset_ns_arg
) {
3664 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3668 case OPT_CLOCK_SECONDS
:
3669 append_implicit_component_param(
3670 &implicit_text_args
, "clock-seconds", "yes");
3671 implicit_text_args
.exists
= true;
3674 implicit_text_args
.exists
= true;
3675 ret
= append_implicit_component_extra_param(
3676 &implicit_text_args
, "color", arg
);
3681 case OPT_DEBUG_INFO
:
3682 implicit_debug_info_args
.exists
= true;
3684 case OPT_DEBUG_INFO_DIR
:
3685 implicit_debug_info_args
.exists
= true;
3686 ret
= append_implicit_component_extra_param(
3687 &implicit_debug_info_args
, "debug-info-dir", arg
);
3692 case OPT_DEBUG_INFO_FULL_PATH
:
3693 implicit_debug_info_args
.exists
= true;
3694 append_implicit_component_param(
3695 &implicit_debug_info_args
, "full-path", "yes");
3697 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3698 implicit_debug_info_args
.exists
= true;
3699 ret
= append_implicit_component_extra_param(
3700 &implicit_debug_info_args
,
3701 "target-prefix", arg
);
3708 bt_value
*fields
= fields_from_arg(arg
);
3714 implicit_text_args
.exists
= true;
3715 ret
= insert_flat_params_from_array(
3716 implicit_text_args
.params_arg
,
3718 bt_value_put_ref(fields
);
3726 bt_value
*names
= names_from_arg(arg
);
3732 implicit_text_args
.exists
= true;
3733 ret
= insert_flat_params_from_array(
3734 implicit_text_args
.params_arg
,
3736 bt_value_put_ref(names
);
3743 append_implicit_component_param(
3744 &implicit_text_args
, "no-delta", "yes");
3745 implicit_text_args
.exists
= true;
3747 case OPT_INPUT_FORMAT
:
3748 if (got_input_format_opt
) {
3749 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --input-format option.");
3753 got_input_format_opt
= true;
3755 if (strcmp(arg
, "ctf") == 0) {
3756 auto_source_discovery_restrict_plugin_name
= "ctf";
3757 auto_source_discovery_restrict_component_class_name
= "fs";
3758 } else if (strcmp(arg
, "lttng-live") == 0) {
3759 auto_source_discovery_restrict_plugin_name
= "ctf";
3760 auto_source_discovery_restrict_component_class_name
= "lttng-live";
3761 implicit_lttng_live_args
.exists
= true;
3763 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy input format:\n %s",
3768 case OPT_OUTPUT_FORMAT
:
3769 if (got_output_format_opt
) {
3770 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output-format option.");
3774 got_output_format_opt
= true;
3776 if (strcmp(arg
, "text") == 0) {
3777 implicit_text_args
.exists
= true;
3778 } else if (strcmp(arg
, "ctf") == 0) {
3779 implicit_ctf_output_args
.exists
= true;
3780 } else if (strcmp(arg
, "dummy") == 0) {
3781 implicit_dummy_args
.exists
= true;
3782 } else if (strcmp(arg
, "ctf-metadata") == 0) {
3783 print_ctf_metadata
= true;
3785 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy output format:\n %s",
3792 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output option");
3796 output
= strdup(arg
);
3798 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3803 if (print_run_args_0
) {
3804 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
3808 print_run_args
= true;
3810 case OPT_RUN_ARGS_0
:
3811 if (print_run_args
) {
3812 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
3816 print_run_args_0
= true;
3818 case OPT_STREAM_INTERSECTION
:
3820 * Applies to all traces implementing the
3821 * babeltrace.trace-infos query.
3823 stream_intersection_mode
= true;
3826 *default_log_level
=
3827 logging_level_min(*default_log_level
, BT_LOG_INFO
);
3830 *default_log_level
=
3831 logging_level_min(*default_log_level
, BT_LOG_TRACE
);
3838 set_auto_log_levels(default_log_level
);
3841 * Legacy behaviour: --verbose used to make the `text` output
3842 * format print more information. --verbose is now equivalent to
3843 * the INFO log level, which is why we compare to `BT_LOG_INFO`
3846 if (*default_log_level
== BT_LOG_INFO
) {
3847 append_implicit_component_param(&implicit_text_args
,
3851 /* Print CTF metadata or print LTTng live sessions */
3852 if (print_ctf_metadata
) {
3853 const bt_value
*bt_val_non_opt
;
3855 if (bt_value_array_is_empty(non_opts
)) {
3856 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf-metadata specified without a path.");
3860 if (bt_value_array_get_length(non_opts
) > 1) {
3861 BT_CLI_LOGE_APPEND_CAUSE("Too many paths specified for --output-format=ctf-metadata.");
3865 cfg
= bt_config_print_ctf_metadata_create(plugin_paths
);
3870 bt_val_non_opt
= bt_value_array_borrow_element_by_index_const(non_opts
, 0);
3871 g_string_assign(cfg
->cmd_data
.print_ctf_metadata
.path
,
3872 bt_value_string_get(bt_val_non_opt
));
3876 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
3884 * If -o ctf was specified, make sure an output path (--output)
3885 * was also specified. --output does not imply -o ctf because
3886 * it's also used for the default, implicit -o text if -o ctf
3889 if (implicit_ctf_output_args
.exists
) {
3891 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf specified without --output (trace output path).");
3896 * At this point we know that -o ctf AND --output were
3897 * specified. Make sure that no options were specified
3898 * which would imply -o text because --output would be
3899 * ambiguous in this case. For example, this is wrong:
3901 * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace
3903 * because --names=all implies -o text, and --output
3904 * could apply to both the sink.text.pretty and
3905 * sink.ctf.fs implicit components.
3907 if (implicit_text_args
.exists
) {
3908 BT_CLI_LOGE_APPEND_CAUSE("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text.");
3914 * If -o dummy and -o ctf were not specified, and if there are
3915 * no explicit sink components, then use an implicit
3916 * `sink.text.pretty` component.
3918 if (!implicit_dummy_args
.exists
&& !implicit_ctf_output_args
.exists
&&
3920 implicit_text_args
.exists
= true;
3924 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
3925 * `path` parameter if --output was specified.
3928 if (implicit_text_args
.exists
) {
3929 append_implicit_component_extra_param(&implicit_text_args
,
3931 } else if (implicit_ctf_output_args
.exists
) {
3932 append_implicit_component_extra_param(&implicit_ctf_output_args
,
3937 /* Decide where the non-option argument(s) go */
3938 if (bt_value_array_get_length(non_opts
) > 0) {
3939 if (implicit_lttng_live_args
.exists
) {
3940 const bt_value
*bt_val_non_opt
;
3942 if (bt_value_array_get_length(non_opts
) > 1) {
3943 BT_CLI_LOGE_APPEND_CAUSE("Too many URLs specified for --input-format=lttng-live.");
3947 bt_val_non_opt
= bt_value_array_borrow_element_by_index_const(non_opts
, 0);
3948 lttng_live_url_parts
=
3949 bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_non_opt
),
3950 error_buf
, sizeof(error_buf
));
3951 if (!lttng_live_url_parts
.proto
) {
3952 BT_CLI_LOGE_APPEND_CAUSE("Invalid LTTng live URL format: %s.",
3957 if (!lttng_live_url_parts
.session_name
) {
3958 /* Print LTTng live sessions */
3959 cfg
= bt_config_print_lttng_live_sessions_create(
3965 g_string_assign(cfg
->cmd_data
.print_lttng_live_sessions
.url
,
3966 bt_value_string_get(bt_val_non_opt
));
3970 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
3977 live_inputs_array_val
= bt_value_array_create();
3978 if (!live_inputs_array_val
) {
3979 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3983 if (bt_value_array_append_string_element(
3984 live_inputs_array_val
,
3985 bt_value_string_get(bt_val_non_opt
))) {
3986 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3990 ret
= append_parameter_to_args(
3991 implicit_lttng_live_args
.extra_params
,
3992 "inputs", live_inputs_array_val
);
3997 ret
= append_implicit_component_extra_param(
3998 &implicit_lttng_live_args
,
3999 "session-not-found-action", "end");
4005 size_t plugin_count
;
4006 const bt_plugin
**plugins
;
4007 const bt_plugin
*plugin
;
4009 status
= require_loaded_plugins(plugin_paths
);
4014 if (auto_source_discovery_restrict_plugin_name
) {
4016 plugin
= borrow_loaded_plugin_by_name(auto_source_discovery_restrict_plugin_name
);
4019 plugin_count
= get_loaded_plugins_count();
4020 plugins
= borrow_loaded_plugins();
4023 status
= auto_discover_source_components(non_opts
, plugins
, plugin_count
,
4024 auto_source_discovery_restrict_component_class_name
,
4025 *default_log_level
, &auto_disc
, interrupter
);
4028 if (status
== AUTO_SOURCE_DISCOVERY_STATUS_INTERRUPTED
) {
4029 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
4030 "Babeltrace CLI", "Automatic source discovery interrupted by the user");
4035 status
= create_implicit_component_args_from_auto_discovered_sources(
4036 &auto_disc
, non_opts
, non_opt_params
, non_opt_loglevels
,
4037 discovered_source_args
);
4046 * If --clock-force-correlated was given, apply it to any src.ctf.fs
4049 if (ctf_fs_source_force_clock_class_unix_epoch_origin
) {
4052 n
= append_multiple_implicit_components_param(
4053 discovered_source_args
, "source.ctf.fs", "force-clock-class-origin-unix-epoch",
4056 BT_CLI_LOGE_APPEND_CAUSE("--clock-force-correlate specified, but no source.ctf.fs component instantiated.");
4061 /* If --clock-offset was given, apply it to any src.ctf.fs component. */
4062 if (ctf_fs_source_clock_class_offset_arg
) {
4065 n
= append_multiple_implicit_components_param(
4066 discovered_source_args
, "source.ctf.fs", "clock-class-offset-s",
4067 ctf_fs_source_clock_class_offset_arg
);
4070 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset specified, but no source.ctf.fs component instantiated.");
4075 /* If --clock-offset-ns was given, apply it to any src.ctf.fs component. */
4076 if (ctf_fs_source_clock_class_offset_ns_arg
) {
4079 n
= append_multiple_implicit_components_param(
4080 discovered_source_args
, "source.ctf.fs", "clock-class-offset-ns",
4081 ctf_fs_source_clock_class_offset_ns_arg
);
4084 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset-ns specified, but no source.ctf.fs component instantiated.");
4090 * If the implicit `source.ctf.lttng-live` component exists,
4091 * make sure there's at least one non-option argument (which is
4094 if (implicit_lttng_live_args
.exists
&& bt_value_array_is_empty(non_opts
)) {
4095 BT_CLI_LOGE_APPEND_CAUSE("Missing URL for implicit `%s` component.",
4096 implicit_lttng_live_args
.comp_arg
->str
);
4100 /* Assign names to implicit components */
4101 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4102 struct implicit_component_args
*args
;
4105 args
= discovered_source_args
->pdata
[i
];
4107 g_string_printf(auto_disc_comp_name
, "auto-disc-%s", args
->comp_arg
->str
);
4109 /* Give it a name like `auto-disc-src-ctf-fs`. */
4110 for (j
= 0; j
< auto_disc_comp_name
->len
; j
++) {
4111 if (auto_disc_comp_name
->str
[j
] == '.') {
4112 auto_disc_comp_name
->str
[j
] = '-';
4116 ret
= assign_name_to_implicit_component(args
,
4117 auto_disc_comp_name
->str
, all_names
, &source_names
, true);
4123 ret
= assign_name_to_implicit_component(&implicit_lttng_live_args
,
4124 "lttng-live", all_names
, &source_names
, true);
4129 ret
= assign_name_to_implicit_component(&implicit_text_args
,
4130 "pretty", all_names
, &sink_names
, true);
4135 ret
= assign_name_to_implicit_component(&implicit_ctf_output_args
,
4136 "sink-ctf-fs", all_names
, &sink_names
, true);
4141 ret
= assign_name_to_implicit_component(&implicit_dummy_args
,
4142 "dummy", all_names
, &sink_names
, true);
4147 ret
= assign_name_to_implicit_component(&implicit_muxer_args
,
4148 "muxer", all_names
, NULL
, false);
4153 ret
= assign_name_to_implicit_component(&implicit_trimmer_args
,
4154 "trimmer", all_names
, NULL
, false);
4159 ret
= assign_name_to_implicit_component(&implicit_debug_info_args
,
4160 "debug-info", all_names
, NULL
, false);
4165 /* Make sure there's at least one source and one sink */
4166 if (!source_names
) {
4167 BT_CLI_LOGE_APPEND_CAUSE("No source component.");
4172 BT_CLI_LOGE_APPEND_CAUSE("No sink component.");
4176 /* Make sure there's a single sink component */
4177 if (g_list_length(sink_names
) != 1) {
4178 BT_CLI_LOGE_APPEND_CAUSE(
4179 "More than one sink component specified.");
4184 * Prepend the muxer, the trimmer, and the debug info to the
4185 * filter chain so that we have:
4187 * sources -> muxer -> [trimmer] -> [debug info] ->
4188 * [user filters] -> sinks
4190 if (implicit_debug_info_args
.exists
) {
4191 if (g_list_prepend_gstring(&filter_names
,
4192 implicit_debug_info_args
.name_arg
->str
)) {
4197 if (implicit_trimmer_args
.exists
) {
4198 if (g_list_prepend_gstring(&filter_names
,
4199 implicit_trimmer_args
.name_arg
->str
)) {
4204 if (g_list_prepend_gstring(&filter_names
,
4205 implicit_muxer_args
.name_arg
->str
)) {
4210 * Append the equivalent run arguments for the implicit
4213 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4214 struct implicit_component_args
*args
=
4215 discovered_source_args
->pdata
[i
];
4217 ret
= append_run_args_for_implicit_component(args
, run_args
);
4223 ret
= append_run_args_for_implicit_component(&implicit_lttng_live_args
,
4229 ret
= append_run_args_for_implicit_component(&implicit_text_args
,
4235 ret
= append_run_args_for_implicit_component(&implicit_ctf_output_args
,
4241 ret
= append_run_args_for_implicit_component(&implicit_dummy_args
,
4247 ret
= append_run_args_for_implicit_component(&implicit_muxer_args
,
4253 ret
= append_run_args_for_implicit_component(&implicit_trimmer_args
,
4259 ret
= append_run_args_for_implicit_component(&implicit_debug_info_args
,
4265 /* Auto-connect components */
4266 ret
= convert_auto_connect(run_args
, source_names
, filter_names
,
4269 BT_CLI_LOGE_APPEND_CAUSE("Cannot auto-connect components.");
4274 * We have all the run command arguments now. Depending on
4275 * --run-args, we pass this to the run command or print them
4278 if (print_run_args
|| print_run_args_0
) {
4279 uint64_t args_idx
, args_len
;
4280 if (stream_intersection_mode
) {
4281 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --stream-intersection with --run-args or --run-args-0.");
4285 args_len
= bt_value_array_get_length(run_args
);
4286 for (args_idx
= 0; args_idx
< args_len
; args_idx
++) {
4287 const bt_value
*arg_value
=
4288 bt_value_array_borrow_element_by_index(run_args
,
4291 GString
*quoted
= NULL
;
4292 const char *arg_to_print
;
4294 arg
= bt_value_string_get(arg_value
);
4296 if (print_run_args
) {
4297 quoted
= bt_common_shell_quote(arg
, true);
4302 arg_to_print
= quoted
->str
;
4307 printf("%s", arg_to_print
);
4310 g_string_free(quoted
, TRUE
);
4313 if (args_idx
< args_len
- 1) {
4314 if (print_run_args
) {
4323 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4327 cfg
= bt_config_run_from_args_array(run_args
, retcode
,
4328 plugin_paths
, *default_log_level
);
4333 cfg
->cmd_data
.run
.stream_intersection_mode
= stream_intersection_mode
;
4338 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4341 argpar_parse_ret_fini(&argpar_parse_ret
);
4345 if (component_arg_for_run
) {
4346 g_string_free(component_arg_for_run
, TRUE
);
4350 g_string_free(name_gstr
, TRUE
);
4353 bt_value_put_ref(live_inputs_array_val
);
4354 bt_value_put_ref(run_args
);
4355 bt_value_put_ref(all_names
);
4356 destroy_glist_of_gstring(source_names
);
4357 destroy_glist_of_gstring(filter_names
);
4358 destroy_glist_of_gstring(sink_names
);
4359 bt_value_put_ref(non_opt_params
);
4360 bt_value_put_ref(non_opt_loglevels
);
4361 bt_value_put_ref(non_opts
);
4362 finalize_implicit_component_args(&implicit_ctf_output_args
);
4363 finalize_implicit_component_args(&implicit_lttng_live_args
);
4364 finalize_implicit_component_args(&implicit_dummy_args
);
4365 finalize_implicit_component_args(&implicit_text_args
);
4366 finalize_implicit_component_args(&implicit_debug_info_args
);
4367 finalize_implicit_component_args(&implicit_muxer_args
);
4368 finalize_implicit_component_args(&implicit_trimmer_args
);
4369 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
4370 auto_source_discovery_fini(&auto_disc
);
4372 if (discovered_source_args
) {
4373 g_ptr_array_free(discovered_source_args
, TRUE
);
4376 g_free(ctf_fs_source_clock_class_offset_arg
);
4377 g_free(ctf_fs_source_clock_class_offset_ns_arg
);
4379 if (auto_disc_comp_name
) {
4380 g_string_free(auto_disc_comp_name
, TRUE
);
4387 * Prints the Babeltrace 2.x general usage.
4390 void print_gen_usage(FILE *fp
)
4392 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4394 fprintf(fp
, "General options:\n");
4396 fprintf(fp
, " -d, --debug Enable debug mode (same as --log-level=T)\n");
4397 fprintf(fp
, " -h, --help Show this help and quit\n");
4398 fprintf(fp
, " -l, --log-level=LVL Set the default log level to LVL (`N`, `T`, `D`,\n");
4399 fprintf(fp
, " `I`, `W` (default), `E`, or `F`)\n");
4400 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
4401 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
4402 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
4403 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
4404 fprintf(fp
, " dynamic plugins can be loaded\n");
4405 fprintf(fp
, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4406 fprintf(fp
, " -V, --version Show version and quit\n");
4408 fprintf(fp
, "Available commands:\n");
4410 fprintf(fp
, " convert Convert and trim traces (default)\n");
4411 fprintf(fp
, " help Get help for a plugin or a component class\n");
4412 fprintf(fp
, " list-plugins List available plugins and their content\n");
4413 fprintf(fp
, " query Query objects from a component class\n");
4414 fprintf(fp
, " run Build a processing graph and run it\n");
4416 fprintf(fp
, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n");
4419 struct bt_config
*bt_config_cli_args_create(int argc
, const char *argv
[],
4420 int *retcode
, bool omit_system_plugin_path
,
4421 bool omit_home_plugin_path
,
4422 const bt_value
*initial_plugin_paths
,
4423 const bt_interrupter
*interrupter
)
4425 struct bt_config
*config
= NULL
;
4428 const char **top_level_argv
;
4429 int command_argc
= -1;
4430 const char **command_argv
= NULL
;
4431 const char *command_name
= NULL
;
4432 int default_log_level
= -1;
4433 struct argpar_parse_ret argpar_parse_ret
= { 0 };
4434 bt_value
*plugin_paths
= NULL
;
4436 /* Top-level option descriptions. */
4437 static const struct argpar_opt_descr descrs
[] = {
4438 { OPT_DEBUG
, 'd', "debug", false },
4439 { OPT_HELP
, 'h', "help", false },
4440 { OPT_LOG_LEVEL
, 'l', "log-level", true },
4441 { OPT_VERBOSE
, 'v', "verbose", false },
4442 { OPT_VERSION
, 'V', "version", false},
4443 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
4444 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
4445 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
4446 ARGPAR_OPT_DESCR_SENTINEL
4450 COMMAND_TYPE_NONE
= -1,
4451 COMMAND_TYPE_RUN
= 0,
4452 COMMAND_TYPE_CONVERT
,
4453 COMMAND_TYPE_LIST_PLUGINS
,
4456 } command_type
= COMMAND_TYPE_NONE
;
4460 if (!initial_plugin_paths
) {
4461 plugin_paths
= bt_value_array_create();
4462 if (!plugin_paths
) {
4466 bt_value_copy_status copy_status
= bt_value_copy(
4467 initial_plugin_paths
, &plugin_paths
);
4473 BT_ASSERT(plugin_paths
);
4476 * The `BABELTRACE_PLUGIN_PATH` paths take precedence over the
4477 * `--plugin-path` option's paths, so append it now before
4478 * parsing the general options.
4480 if (append_env_var_plugin_paths(plugin_paths
)) {
4487 print_gen_usage(stdout
);
4491 /* Skip first argument, the name of the program. */
4492 top_level_argc
= argc
- 1;
4493 top_level_argv
= argv
+ 1;
4494 argpar_parse_ret
= argpar_parse(top_level_argc
, top_level_argv
,
4497 if (argpar_parse_ret
.error
) {
4498 BT_CLI_LOGE_APPEND_CAUSE(
4499 "While parsing command-line arguments: %s",
4500 argpar_parse_ret
.error
);
4504 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
4505 struct argpar_item
*item
;
4507 item
= argpar_parse_ret
.items
->items
[i
];
4509 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
4510 struct argpar_item_opt
*item_opt
=
4511 (struct argpar_item_opt
*) item
;
4513 switch (item_opt
->descr
->id
) {
4516 logging_level_min(default_log_level
, BT_LOG_TRACE
);
4520 logging_level_min(default_log_level
, BT_LOG_INFO
);
4524 int level
= bt_log_get_level_from_string(item_opt
->arg
);
4527 BT_CLI_LOGE_APPEND_CAUSE(
4528 "Invalid argument for --log-level option:\n %s",
4534 logging_level_min(default_log_level
, level
);
4537 case OPT_PLUGIN_PATH
:
4538 if (bt_config_append_plugin_paths_check_setuid_setgid(
4539 plugin_paths
, item_opt
->arg
)) {
4543 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
4544 omit_system_plugin_path
= true;
4546 case OPT_OMIT_HOME_PLUGIN_PATH
:
4547 omit_home_plugin_path
= true;
4553 print_gen_usage(stdout
);
4556 } else if (item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
4557 struct argpar_item_non_opt
*item_non_opt
=
4558 (struct argpar_item_non_opt
*) item
;
4560 * First unknown argument: is it a known command
4564 top_level_argc
- item_non_opt
->orig_index
- 1;
4566 &top_level_argv
[item_non_opt
->orig_index
+ 1];
4568 if (strcmp(item_non_opt
->arg
, "convert") == 0) {
4569 command_type
= COMMAND_TYPE_CONVERT
;
4570 command_name
= "convert";
4571 } else if (strcmp(item_non_opt
->arg
, "list-plugins") == 0) {
4572 command_type
= COMMAND_TYPE_LIST_PLUGINS
;
4573 command_name
= "list-plugins";
4574 } else if (strcmp(item_non_opt
->arg
, "help") == 0) {
4575 command_type
= COMMAND_TYPE_HELP
;
4576 command_name
= "help";
4577 } else if (strcmp(item_non_opt
->arg
, "query") == 0) {
4578 command_type
= COMMAND_TYPE_QUERY
;
4579 command_name
= "query";
4580 } else if (strcmp(item_non_opt
->arg
, "run") == 0) {
4581 command_type
= COMMAND_TYPE_RUN
;
4582 command_name
= "run";
4585 * Non-option argument, but not a known
4586 * command name: assume the default
4587 * `convert` command.
4589 command_type
= COMMAND_TYPE_CONVERT
;
4590 command_name
= "convert";
4598 if (command_type
== COMMAND_TYPE_NONE
) {
4599 if (argpar_parse_ret
.ingested_orig_args
== top_level_argc
) {
4601 * We only got non-help, non-version general options
4602 * like --verbose and --debug, without any other
4603 * arguments, so we can't do anything useful: print the
4606 print_gen_usage(stdout
);
4611 * We stopped on an unknown option argument (and therefore
4612 * didn't see a command name). Assume `convert` command.
4614 command_type
= COMMAND_TYPE_CONVERT
;
4615 command_name
= "convert";
4617 top_level_argc
- argpar_parse_ret
.ingested_orig_args
;
4619 &top_level_argv
[argpar_parse_ret
.ingested_orig_args
];
4622 BT_ASSERT(command_argv
);
4623 BT_ASSERT(command_argc
>= 0);
4626 * For all commands other than `convert`, we now know the log level to
4627 * use, so we can apply it with `set_auto_log_levels`.
4629 * The convert command has `--debug` and `--verbose` arguments that are
4630 * equivalent to the top-level arguments of the same name. So after it
4631 * has parsed its arguments, `bt_config_convert_from_args` calls
4632 * `set_auto_log_levels` itself.
4634 if (command_type
!= COMMAND_TYPE_CONVERT
) {
4635 set_auto_log_levels(&default_log_level
);
4639 * At this point, `plugin_paths` contains the initial plugin
4640 * paths, the paths from the `BABELTRACE_PLUGIN_PATH` paths, and
4641 * the paths from the `--plugin-path` option.
4643 * Now append the user and system plugin paths.
4645 if (append_home_and_system_plugin_paths(plugin_paths
,
4646 omit_system_plugin_path
, omit_home_plugin_path
)) {
4650 switch (command_type
) {
4651 case COMMAND_TYPE_RUN
:
4652 config
= bt_config_run_from_args(command_argc
, command_argv
,
4653 retcode
, plugin_paths
,
4656 case COMMAND_TYPE_CONVERT
:
4657 config
= bt_config_convert_from_args(command_argc
, command_argv
,
4658 retcode
, plugin_paths
, &default_log_level
, interrupter
);
4660 case COMMAND_TYPE_LIST_PLUGINS
:
4661 config
= bt_config_list_plugins_from_args(command_argc
,
4662 command_argv
, retcode
, plugin_paths
);
4664 case COMMAND_TYPE_HELP
:
4665 config
= bt_config_help_from_args(command_argc
,
4666 command_argv
, retcode
, plugin_paths
,
4669 case COMMAND_TYPE_QUERY
:
4670 config
= bt_config_query_from_args(command_argc
,
4671 command_argv
, retcode
, plugin_paths
,
4679 BT_ASSERT(default_log_level
>= BT_LOG_TRACE
);
4680 config
->log_level
= default_log_level
;
4681 config
->command_name
= command_name
;
4690 argpar_parse_ret_fini(&argpar_parse_ret
);
4691 bt_value_put_ref(plugin_paths
);