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 void print_and_indent(const char *str
)
197 const char *ch
= &str
[0];
199 for (; *ch
!= '\0'; ch
++) {
213 * Prints the Babeltrace version.
216 void print_version(void)
218 bool has_extra_name
= strlen(BT_VERSION_EXTRA_NAME
) > 0;
219 bool has_extra_description
= strlen(BT_VERSION_EXTRA_DESCRIPTION
) > 0;
220 bool has_extra_patch_names
= strlen(BT_VERSION_EXTRA_PATCHES
) > 0;
221 bool has_extra
= has_extra_name
|| has_extra_description
||
222 has_extra_patch_names
;
224 printf("%sBabeltrace %s%s",
225 bt_common_color_bold(),
227 bt_common_color_reset());
229 if (strlen(BT_VERSION_NAME
) > 0) {
230 printf(" \"%s%s%s%s\"",
231 bt_common_color_fg_bright_blue(),
232 bt_common_color_bold(),
234 bt_common_color_reset());
237 if (strlen(BT_VERSION_GIT
) > 0) {
239 bt_common_color_fg_yellow(),
241 bt_common_color_reset());
246 if (strlen(BT_VERSION_DESCRIPTION
) > 0) {
247 unsigned int columns
;
250 if (bt_common_get_term_size(&columns
, NULL
) < 0) {
251 /* Width not found: default to 80 */
255 descr
= bt_common_fold(BT_VERSION_DESCRIPTION
, columns
, 0);
257 printf("\n%s\n", descr
->str
);
258 g_string_free(descr
, TRUE
);
264 if (has_extra_name
) {
265 printf("%sExtra name%s: %s\n",
266 bt_common_color_fg_cyan(),
267 bt_common_color_reset(),
268 BT_VERSION_EXTRA_NAME
);
271 if (has_extra_description
) {
272 printf("%sExtra description%s:\n ",
273 bt_common_color_fg_cyan(),
274 bt_common_color_reset());
275 print_and_indent(BT_VERSION_EXTRA_DESCRIPTION
);
278 if (has_extra_patch_names
) {
279 printf("%sExtra patch names%s:\n ",
280 bt_common_color_fg_cyan(),
281 bt_common_color_reset());
282 print_and_indent(BT_VERSION_EXTRA_PATCHES
);
288 * Destroys a component configuration.
291 void bt_config_component_destroy(bt_object
*obj
)
293 struct bt_config_component
*bt_config_component
=
294 container_of(obj
, struct bt_config_component
, base
);
300 if (bt_config_component
->plugin_name
) {
301 g_string_free(bt_config_component
->plugin_name
, TRUE
);
304 if (bt_config_component
->comp_cls_name
) {
305 g_string_free(bt_config_component
->comp_cls_name
, TRUE
);
308 if (bt_config_component
->instance_name
) {
309 g_string_free(bt_config_component
->instance_name
, TRUE
);
312 BT_VALUE_PUT_REF_AND_RESET(bt_config_component
->params
);
313 g_free(bt_config_component
);
320 * Creates a component configuration using the given plugin name and
321 * component name. `plugin_name` and `comp_cls_name` are copied (belong
322 * to the return value).
324 * Return value is owned by the caller.
327 struct bt_config_component
*bt_config_component_create(
328 bt_component_class_type type
,
329 const char *plugin_name
, const char *comp_cls_name
,
332 struct bt_config_component
*cfg_component
= NULL
;
334 cfg_component
= g_new0(struct bt_config_component
, 1);
335 if (!cfg_component
) {
336 BT_CLI_LOGE_APPEND_CAUSE_OOM();
340 bt_object_init_shared(&cfg_component
->base
,
341 bt_config_component_destroy
);
342 cfg_component
->type
= type
;
343 cfg_component
->plugin_name
= g_string_new(plugin_name
);
344 if (!cfg_component
->plugin_name
) {
345 BT_CLI_LOGE_APPEND_CAUSE_OOM();
349 cfg_component
->comp_cls_name
= g_string_new(comp_cls_name
);
350 if (!cfg_component
->comp_cls_name
) {
351 BT_CLI_LOGE_APPEND_CAUSE_OOM();
355 cfg_component
->instance_name
= g_string_new(NULL
);
356 if (!cfg_component
->instance_name
) {
357 BT_CLI_LOGE_APPEND_CAUSE_OOM();
361 cfg_component
->log_level
= init_log_level
;
363 /* Start with empty parameters */
364 cfg_component
->params
= bt_value_map_create();
365 if (!cfg_component
->params
) {
366 BT_CLI_LOGE_APPEND_CAUSE_OOM();
373 BT_OBJECT_PUT_REF_AND_RESET(cfg_component
);
376 return cfg_component
;
380 * Creates a component configuration from a command-line --component
384 struct bt_config_component
*bt_config_component_from_arg(const char *arg
,
387 struct bt_config_component
*cfg_comp
= NULL
;
389 char *plugin_name
= NULL
;
390 char *comp_cls_name
= NULL
;
391 bt_component_class_type type
;
393 plugin_comp_cls_names(arg
, &name
, &plugin_name
, &comp_cls_name
, &type
);
394 if (!plugin_name
|| !comp_cls_name
) {
398 cfg_comp
= bt_config_component_create(type
, plugin_name
, comp_cls_name
,
405 g_string_assign(cfg_comp
->instance_name
, name
);
411 BT_OBJECT_PUT_REF_AND_RESET(cfg_comp
);
416 g_free(comp_cls_name
);
421 * Destroys a configuration.
424 void bt_config_destroy(bt_object
*obj
)
426 struct bt_config
*cfg
=
427 container_of(obj
, struct bt_config
, base
);
433 BT_VALUE_PUT_REF_AND_RESET(cfg
->plugin_paths
);
435 switch (cfg
->command
) {
436 case BT_CONFIG_COMMAND_RUN
:
437 if (cfg
->cmd_data
.run
.sources
) {
438 g_ptr_array_free(cfg
->cmd_data
.run
.sources
, TRUE
);
441 if (cfg
->cmd_data
.run
.filters
) {
442 g_ptr_array_free(cfg
->cmd_data
.run
.filters
, TRUE
);
445 if (cfg
->cmd_data
.run
.sinks
) {
446 g_ptr_array_free(cfg
->cmd_data
.run
.sinks
, TRUE
);
449 if (cfg
->cmd_data
.run
.connections
) {
450 g_ptr_array_free(cfg
->cmd_data
.run
.connections
,
454 case BT_CONFIG_COMMAND_LIST_PLUGINS
:
456 case BT_CONFIG_COMMAND_HELP
:
457 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.help
.cfg_component
);
459 case BT_CONFIG_COMMAND_QUERY
:
460 BT_OBJECT_PUT_REF_AND_RESET(cfg
->cmd_data
.query
.cfg_component
);
462 if (cfg
->cmd_data
.query
.object
) {
463 g_string_free(cfg
->cmd_data
.query
.object
, TRUE
);
466 case BT_CONFIG_COMMAND_PRINT_CTF_METADATA
:
467 if (cfg
->cmd_data
.print_ctf_metadata
.path
) {
468 g_string_free(cfg
->cmd_data
.print_ctf_metadata
.path
,
471 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
475 case BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
:
476 if (cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
478 cfg
->cmd_data
.print_lttng_live_sessions
.url
,
481 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
496 void destroy_glist_of_gstring(GList
*list
)
504 for (at
= list
; at
; at
= g_list_next(at
)) {
505 g_string_free(at
->data
, TRUE
);
512 * Creates a simple lexical scanner for parsing comma-delimited names
515 * Return value is owned by the caller.
518 GScanner
*create_csv_identifiers_scanner(void)
521 GScannerConfig scanner_config
= {
522 .cset_skip_characters
= (gchar
*) " \t\n",
523 .cset_identifier_first
= (gchar
*) G_CSET_a_2_z G_CSET_A_2_Z
"_",
524 .cset_identifier_nth
= (gchar
*) G_CSET_a_2_z G_CSET_A_2_Z
":_-",
525 .case_sensitive
= TRUE
,
526 .cpair_comment_single
= NULL
,
527 .skip_comment_multi
= TRUE
,
528 .skip_comment_single
= TRUE
,
529 .scan_comment_multi
= FALSE
,
530 .scan_identifier
= TRUE
,
531 .scan_identifier_1char
= TRUE
,
532 .scan_identifier_NULL
= FALSE
,
533 .scan_symbols
= FALSE
,
534 .symbol_2_token
= FALSE
,
535 .scope_0_fallback
= FALSE
,
536 .scan_binary
= FALSE
,
540 .scan_hex_dollar
= FALSE
,
541 .numbers_2_int
= FALSE
,
542 .int_2_float
= FALSE
,
543 .store_int64
= FALSE
,
544 .scan_string_sq
= FALSE
,
545 .scan_string_dq
= FALSE
,
546 .identifier_2_string
= FALSE
,
547 .char_2_token
= TRUE
,
550 scanner
= g_scanner_new(&scanner_config
);
552 BT_CLI_LOGE_APPEND_CAUSE_OOM();
559 * Converts a comma-delimited list of known names (--names option) to
560 * an array value object containing those names as string value objects.
562 * Return value is owned by the caller.
565 bt_value
*names_from_arg(const char *arg
)
567 GScanner
*scanner
= NULL
;
568 bt_value
*names
= NULL
;
569 bool found_all
= false, found_none
= false, found_item
= false;
571 names
= bt_value_array_create();
573 BT_CLI_LOGE_APPEND_CAUSE_OOM();
577 scanner
= create_csv_identifiers_scanner();
582 g_scanner_input_text(scanner
, arg
, strlen(arg
));
585 GTokenType token_type
= g_scanner_get_next_token(scanner
);
587 switch (token_type
) {
588 case G_TOKEN_IDENTIFIER
:
590 const char *identifier
= scanner
->value
.v_identifier
;
592 if (strcmp(identifier
, "payload") == 0 ||
593 strcmp(identifier
, "args") == 0 ||
594 strcmp(identifier
, "arg") == 0) {
596 if (bt_value_array_append_string_element(names
,
600 } else if (strcmp(identifier
, "context") == 0 ||
601 strcmp(identifier
, "ctx") == 0) {
603 if (bt_value_array_append_string_element(names
,
607 } else if (strcmp(identifier
, "scope") == 0 ||
608 strcmp(identifier
, "header") == 0) {
610 if (bt_value_array_append_string_element(names
,
614 } else if (strcmp(identifier
, "all") == 0) {
616 if (bt_value_array_append_string_element(names
,
620 } else if (strcmp(identifier
, "none") == 0) {
622 if (bt_value_array_append_string_element(names
,
627 BT_CLI_LOGE_APPEND_CAUSE("Unknown name: `%s`.",
643 if (found_none
&& found_all
) {
644 BT_CLI_LOGE_APPEND_CAUSE("Only either `all` or `none` can be specified in the list given to the --names option, but not both.");
648 * Legacy behavior is to clear the defaults (show none) when at
649 * least one item is specified.
651 if (found_item
&& !found_none
&& !found_all
) {
652 if (bt_value_array_append_string_element(names
, "none")) {
657 g_scanner_destroy(scanner
);
662 BT_VALUE_PUT_REF_AND_RESET(names
);
664 g_scanner_destroy(scanner
);
670 * Converts a comma-delimited list of known fields (--fields option) to
671 * an array value object containing those fields as string
674 * Return value is owned by the caller.
677 bt_value
*fields_from_arg(const char *arg
)
679 GScanner
*scanner
= NULL
;
682 fields
= bt_value_array_create();
684 BT_CLI_LOGE_APPEND_CAUSE_OOM();
688 scanner
= create_csv_identifiers_scanner();
693 g_scanner_input_text(scanner
, arg
, strlen(arg
));
696 GTokenType token_type
= g_scanner_get_next_token(scanner
);
698 switch (token_type
) {
699 case G_TOKEN_IDENTIFIER
:
701 const char *identifier
= scanner
->value
.v_identifier
;
703 if (strcmp(identifier
, "trace") == 0 ||
704 strcmp(identifier
, "trace:hostname") == 0 ||
705 strcmp(identifier
, "trace:domain") == 0 ||
706 strcmp(identifier
, "trace:procname") == 0 ||
707 strcmp(identifier
, "trace:vpid") == 0 ||
708 strcmp(identifier
, "loglevel") == 0 ||
709 strcmp(identifier
, "emf") == 0 ||
710 strcmp(identifier
, "callsite") == 0 ||
711 strcmp(identifier
, "all") == 0) {
712 if (bt_value_array_append_string_element(fields
,
717 BT_CLI_LOGE_APPEND_CAUSE("Unknown field: `%s`.",
735 BT_VALUE_PUT_REF_AND_RESET(fields
);
739 g_scanner_destroy(scanner
);
745 void append_param_arg(GString
*params_arg
, const char *key
, const char *value
)
747 BT_ASSERT(params_arg
);
751 if (params_arg
->len
!= 0) {
752 g_string_append_c(params_arg
, ',');
755 g_string_append(params_arg
, key
);
756 g_string_append_c(params_arg
, '=');
757 g_string_append(params_arg
, value
);
761 * Inserts the equivalent "prefix-NAME=yes" strings into params_arg
762 * where the names are in names_array.
765 int insert_flat_params_from_array(GString
*params_arg
,
766 const bt_value
*names_array
, const char *prefix
)
770 GString
*tmpstr
= NULL
, *default_value
= NULL
;
771 bool default_set
= false, non_default_set
= false;
774 * names_array may be NULL if no CLI options were specified to
775 * trigger its creation.
781 tmpstr
= g_string_new(NULL
);
783 BT_CLI_LOGE_APPEND_CAUSE_OOM();
788 default_value
= g_string_new(NULL
);
789 if (!default_value
) {
790 BT_CLI_LOGE_APPEND_CAUSE_OOM();
795 for (i
= 0; i
< bt_value_array_get_length(names_array
); i
++) {
796 const bt_value
*str_obj
=
797 bt_value_array_borrow_element_by_index_const(names_array
,
800 bool is_default
= false;
802 suffix
= bt_value_string_get(str_obj
);
804 g_string_assign(tmpstr
, prefix
);
805 g_string_append(tmpstr
, "-");
807 /* Special-case for "all" and "none". */
808 if (strcmp(suffix
, "all") == 0) {
810 g_string_assign(default_value
, "show");
811 } else if (strcmp(suffix
, "none") == 0) {
813 g_string_assign(default_value
, "hide");
817 g_string_append(tmpstr
, "default");
818 append_param_arg(params_arg
, tmpstr
->str
,
821 non_default_set
= true;
822 g_string_append(tmpstr
, suffix
);
823 append_param_arg(params_arg
, tmpstr
->str
, "yes");
827 /* Implicit field-default=hide if any non-default option is set. */
828 if (non_default_set
&& !default_set
) {
829 g_string_assign(tmpstr
, prefix
);
830 g_string_append(tmpstr
, "-default");
831 g_string_assign(default_value
, "hide");
832 append_param_arg(params_arg
, tmpstr
->str
, default_value
->str
);
837 g_string_free(default_value
, TRUE
);
841 g_string_free(tmpstr
, TRUE
);
854 OPT_CLOCK_FORCE_CORRELATE
,
865 OPT_DEBUG_INFO_FULL_PATH
,
866 OPT_DEBUG_INFO_TARGET_PREFIX
,
875 OPT_OMIT_HOME_PLUGIN_PATH
,
876 OPT_OMIT_SYSTEM_PLUGIN_PATH
,
881 OPT_RESET_BASE_PARAMS
,
885 OPT_STREAM_INTERSECTION
,
891 enum bt_config_component_dest
{
892 BT_CONFIG_COMPONENT_DEST_UNKNOWN
= -1,
893 BT_CONFIG_COMPONENT_DEST_SOURCE
,
894 BT_CONFIG_COMPONENT_DEST_FILTER
,
895 BT_CONFIG_COMPONENT_DEST_SINK
,
899 * Adds a configuration component to the appropriate configuration
900 * array depending on the destination.
903 void add_run_cfg_comp(struct bt_config
*cfg
,
904 struct bt_config_component
*cfg_comp
,
905 enum bt_config_component_dest dest
)
907 bt_object_get_ref(cfg_comp
);
910 case BT_CONFIG_COMPONENT_DEST_SOURCE
:
911 g_ptr_array_add(cfg
->cmd_data
.run
.sources
, cfg_comp
);
913 case BT_CONFIG_COMPONENT_DEST_FILTER
:
914 g_ptr_array_add(cfg
->cmd_data
.run
.filters
, cfg_comp
);
916 case BT_CONFIG_COMPONENT_DEST_SINK
:
917 g_ptr_array_add(cfg
->cmd_data
.run
.sinks
, cfg_comp
);
925 int add_run_cfg_comp_check_name(struct bt_config
*cfg
,
926 struct bt_config_component
*cfg_comp
,
927 enum bt_config_component_dest dest
,
928 bt_value
*instance_names
)
932 if (cfg_comp
->instance_name
->len
== 0) {
933 BT_CLI_LOGE_APPEND_CAUSE("Found an unnamed component.");
938 if (bt_value_map_has_entry(instance_names
,
939 cfg_comp
->instance_name
->str
)) {
940 BT_CLI_LOGE_APPEND_CAUSE("Duplicate component instance name:\n %s",
941 cfg_comp
->instance_name
->str
);
946 if (bt_value_map_insert_entry(instance_names
,
947 cfg_comp
->instance_name
->str
, bt_value_null
)) {
948 BT_CLI_LOGE_APPEND_CAUSE_OOM();
953 add_run_cfg_comp(cfg
, cfg_comp
, dest
);
960 int append_env_var_plugin_paths(bt_value
*plugin_paths
)
965 if (bt_common_is_setuid_setgid()) {
966 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
970 envvar
= getenv("BABELTRACE_PLUGIN_PATH");
975 ret
= bt_config_append_plugin_paths(plugin_paths
, envvar
);
979 BT_CLI_LOGE_APPEND_CAUSE("Cannot append plugin paths from BABELTRACE_PLUGIN_PATH.");
986 int append_home_and_system_plugin_paths(bt_value
*plugin_paths
,
987 bool omit_system_plugin_path
, bool omit_home_plugin_path
)
991 if (!omit_home_plugin_path
) {
992 if (bt_common_is_setuid_setgid()) {
993 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
995 char *home_plugin_dir
= bt_common_get_home_plugin_path(
996 BT_LOG_OUTPUT_LEVEL
);
998 if (home_plugin_dir
) {
999 ret
= bt_config_append_plugin_paths(
1000 plugin_paths
, home_plugin_dir
);
1001 free(home_plugin_dir
);
1004 BT_CLI_LOGE_APPEND_CAUSE("Invalid home plugin path.");
1011 if (!omit_system_plugin_path
) {
1012 if (bt_config_append_plugin_paths(plugin_paths
,
1013 bt_common_get_system_plugin_path())) {
1014 BT_CLI_LOGE_APPEND_CAUSE("Invalid system plugin path.");
1020 BT_CLI_LOGE_APPEND_CAUSE("Cannot append home and system plugin paths.");
1025 struct bt_config
*bt_config_base_create(enum bt_config_command command
,
1026 const bt_value
*plugin_paths
, bool needs_plugins
)
1028 struct bt_config
*cfg
;
1031 cfg
= g_new0(struct bt_config
, 1);
1033 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1037 bt_object_init_shared(&cfg
->base
, bt_config_destroy
);
1038 cfg
->command
= command
;
1039 cfg
->command_needs_plugins
= needs_plugins
;
1042 bt_value
*plugin_paths_copy
;
1044 (void) bt_value_copy(plugin_paths
,
1045 &plugin_paths_copy
);
1046 cfg
->plugin_paths
= plugin_paths_copy
;
1048 cfg
->plugin_paths
= bt_value_array_create();
1049 if (!cfg
->plugin_paths
) {
1050 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1058 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1065 struct bt_config
*bt_config_run_create(const bt_value
*plugin_paths
)
1067 struct bt_config
*cfg
;
1070 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_RUN
,
1071 plugin_paths
, true);
1076 cfg
->cmd_data
.run
.sources
= g_ptr_array_new_with_free_func(
1077 (GDestroyNotify
) bt_object_put_ref
);
1078 if (!cfg
->cmd_data
.run
.sources
) {
1079 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1083 cfg
->cmd_data
.run
.filters
= g_ptr_array_new_with_free_func(
1084 (GDestroyNotify
) bt_object_put_ref
);
1085 if (!cfg
->cmd_data
.run
.filters
) {
1086 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1090 cfg
->cmd_data
.run
.sinks
= g_ptr_array_new_with_free_func(
1091 (GDestroyNotify
) bt_object_put_ref
);
1092 if (!cfg
->cmd_data
.run
.sinks
) {
1093 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1097 cfg
->cmd_data
.run
.connections
= g_ptr_array_new_with_free_func(
1098 (GDestroyNotify
) bt_config_connection_destroy
);
1099 if (!cfg
->cmd_data
.run
.connections
) {
1100 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1107 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1114 struct bt_config
*bt_config_list_plugins_create(const bt_value
*plugin_paths
)
1116 return bt_config_base_create(BT_CONFIG_COMMAND_LIST_PLUGINS
,
1117 plugin_paths
, true);
1121 struct bt_config
*bt_config_help_create(const bt_value
*plugin_paths
,
1122 int default_log_level
)
1124 struct bt_config
*cfg
;
1127 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_HELP
,
1128 plugin_paths
, true);
1133 cfg
->cmd_data
.help
.cfg_component
=
1134 bt_config_component_create(-1, NULL
, NULL
, default_log_level
);
1135 if (!cfg
->cmd_data
.help
.cfg_component
) {
1142 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1149 struct bt_config
*bt_config_query_create(const bt_value
*plugin_paths
)
1151 struct bt_config
*cfg
;
1154 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_QUERY
,
1155 plugin_paths
, true);
1160 cfg
->cmd_data
.query
.object
= g_string_new(NULL
);
1161 if (!cfg
->cmd_data
.query
.object
) {
1162 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1169 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1176 struct bt_config
*bt_config_print_ctf_metadata_create(
1177 const bt_value
*plugin_paths
)
1179 struct bt_config
*cfg
;
1182 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_CTF_METADATA
,
1183 plugin_paths
, true);
1188 cfg
->cmd_data
.print_ctf_metadata
.path
= g_string_new(NULL
);
1189 if (!cfg
->cmd_data
.print_ctf_metadata
.path
) {
1190 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1194 cfg
->cmd_data
.print_ctf_metadata
.output_path
= g_string_new(NULL
);
1195 if (!cfg
->cmd_data
.print_ctf_metadata
.output_path
) {
1196 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1203 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1210 struct bt_config
*bt_config_print_lttng_live_sessions_create(
1211 const bt_value
*plugin_paths
)
1213 struct bt_config
*cfg
;
1216 cfg
= bt_config_base_create(BT_CONFIG_COMMAND_PRINT_LTTNG_LIVE_SESSIONS
,
1217 plugin_paths
, true);
1222 cfg
->cmd_data
.print_lttng_live_sessions
.url
= g_string_new(NULL
);
1223 if (!cfg
->cmd_data
.print_lttng_live_sessions
.url
) {
1224 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1228 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
=
1230 if (!cfg
->cmd_data
.print_lttng_live_sessions
.output_path
) {
1231 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1238 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1245 int bt_config_append_plugin_paths_check_setuid_setgid(
1246 bt_value
*plugin_paths
, const char *arg
)
1250 if (bt_common_is_setuid_setgid()) {
1251 BT_LOGI_STR("Skipping non-system plugin paths for setuid/setgid binary.");
1255 if (bt_config_append_plugin_paths(plugin_paths
, arg
)) {
1256 BT_CLI_LOGE_APPEND_CAUSE("Invalid --plugin-path option's argument:\n %s",
1267 * Prints the expected format for a --params option.
1270 void print_expected_params_format(FILE *fp
)
1272 fprintf(fp
, "Expected format of PARAMS\n");
1273 fprintf(fp
, "-------------------------\n");
1275 fprintf(fp
, " PARAM=VALUE[,PARAM=VALUE]...\n");
1277 fprintf(fp
, "The parameter string is a comma-separated list of PARAM=VALUE assignments,\n");
1278 fprintf(fp
, "where PARAM is the parameter name (C identifier plus the [:.-] characters),\n");
1279 fprintf(fp
, "and VALUE can be one of:\n");
1281 fprintf(fp
, "* `null`, `nul`, `NULL`: null value (no backticks).\n");
1282 fprintf(fp
, "* `true`, `TRUE`, `yes`, `YES`: true boolean value (no backticks).\n");
1283 fprintf(fp
, "* `false`, `FALSE`, `no`, `NO`: false boolean value (no backticks).\n");
1284 fprintf(fp
, "* Binary (`0b` prefix), octal (`0` prefix), decimal, or hexadecimal\n");
1285 fprintf(fp
, " (`0x` prefix) unsigned (with `+` prefix) or signed 64-bit integer.\n");
1286 fprintf(fp
, "* Double precision floating point number (scientific notation is accepted).\n");
1287 fprintf(fp
, "* Unquoted string with no special characters, and not matching any of\n");
1288 fprintf(fp
, " the null and boolean value symbols above.\n");
1289 fprintf(fp
, "* Double-quoted string (accepts escape characters).\n");
1290 fprintf(fp
, "* Array, formatted as an opening `[`, a list of comma-separated values\n");
1291 fprintf(fp
, " (as described by the current list) and a closing `]`.\n");
1292 fprintf(fp
, "* Map, formatted as an opening `{`, a comma-separated list of PARAM=VALUE\n");
1293 fprintf(fp
, " assignments and a closing `}`.\n");
1295 fprintf(fp
, "You can put whitespaces allowed around individual `=` and `,` symbols.\n");
1297 fprintf(fp
, "Example:\n");
1299 fprintf(fp
, " many=null, fresh=yes, condition=false, squirrel=-782329,\n");
1300 fprintf(fp
, " play=+23, observe=3.14, simple=beef, needs-quotes=\"some string\",\n");
1301 fprintf(fp
, " escape.chars-are:allowed=\"this is a \\\" double quote\",\n");
1302 fprintf(fp
, " things=[1, \"2\", 3]\n");
1304 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1305 fprintf(fp
, "babeltrace2 from a shell.\n");
1309 bool help_option_is_specified(
1310 const struct argpar_parse_ret
*argpar_parse_ret
)
1313 bool specified
= false;
1315 for (i
= 0; i
< argpar_parse_ret
->items
->n_items
; i
++) {
1316 struct argpar_item
*argpar_item
=
1317 argpar_parse_ret
->items
->items
[i
];
1318 struct argpar_item_opt
*argpar_item_opt
;
1320 if (argpar_item
->type
!= ARGPAR_ITEM_TYPE_OPT
) {
1324 argpar_item_opt
= (struct argpar_item_opt
*) argpar_item
;
1325 if (argpar_item_opt
->descr
->id
== OPT_HELP
) {
1335 * Prints the help command usage.
1338 void print_help_usage(FILE *fp
)
1340 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] help [OPTIONS] PLUGIN\n");
1341 fprintf(fp
, " babeltrace2 [GENERAL OPTIONS] help [OPTIONS] TYPE.PLUGIN.CLS\n");
1343 fprintf(fp
, "Options:\n");
1345 fprintf(fp
, " -h, --help Show this help and quit\n");
1347 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1349 fprintf(fp
, "Use `babeltrace2 list-plugins` to show the list of available plugins.\n");
1353 const struct argpar_opt_descr help_options
[] = {
1354 /* id, short_name, long_name, with_arg */
1355 { OPT_HELP
, 'h', "help", false },
1356 ARGPAR_OPT_DESCR_SENTINEL
1360 * Creates a Babeltrace config object from the arguments of a help
1363 * *retcode is set to the appropriate exit code to use.
1366 struct bt_config
*bt_config_help_from_args(int argc
, const char *argv
[],
1367 int *retcode
, const bt_value
*plugin_paths
,
1368 int default_log_level
)
1370 struct bt_config
*cfg
= NULL
;
1371 char *plugin_name
= NULL
, *comp_cls_name
= NULL
;
1372 struct argpar_parse_ret argpar_parse_ret
= { 0 };
1373 struct argpar_item_non_opt
*non_opt
;
1374 GString
*substring
= NULL
;
1378 cfg
= bt_config_help_create(plugin_paths
, default_log_level
);
1384 argpar_parse_ret
= argpar_parse(argc
, argv
, help_options
, true);
1385 if (argpar_parse_ret
.error
) {
1386 BT_CLI_LOGE_APPEND_CAUSE(
1387 "While parsing `help` command's command-line arguments: %s",
1388 argpar_parse_ret
.error
);
1392 if (help_option_is_specified(&argpar_parse_ret
)) {
1393 print_help_usage(stdout
);
1395 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1399 if (argpar_parse_ret
.items
->n_items
== 0) {
1400 BT_CLI_LOGE_APPEND_CAUSE(
1401 "Missing plugin name or component class descriptor.");
1403 } else if (argpar_parse_ret
.items
->n_items
> 1) {
1405 * At this point we know there are least two non-option
1406 * arguments because we don't reach here with `--help`,
1409 non_opt
= (struct argpar_item_non_opt
*) argpar_parse_ret
.items
->items
[1];
1410 BT_CLI_LOGE_APPEND_CAUSE(
1411 "Extraneous command-line argument specified to `help` command: `%s`.",
1416 non_opt
= (struct argpar_item_non_opt
*) argpar_parse_ret
.items
->items
[0];
1418 /* Look for unescaped dots in the argument. */
1419 substring
= bt_common_string_until(non_opt
->arg
, ".\\", ".", &end_pos
);
1421 BT_CLI_LOGE_APPEND_CAUSE("Could not consume argument: arg=%s",
1426 if (end_pos
== strlen(non_opt
->arg
)) {
1427 /* Didn't find an unescaped dot, treat it as a plugin name. */
1428 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1432 * Found an unescaped dot, treat it as a component class name.
1434 plugin_comp_cls_names(non_opt
->arg
, NULL
, &plugin_name
, &comp_cls_name
,
1435 &cfg
->cmd_data
.help
.cfg_component
->type
);
1436 if (!plugin_name
|| !comp_cls_name
) {
1437 BT_CLI_LOGE_APPEND_CAUSE(
1438 "Could not parse argument as a component class name: arg=%s",
1443 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->plugin_name
,
1445 g_string_assign(cfg
->cmd_data
.help
.cfg_component
->comp_cls_name
,
1453 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1456 g_free(plugin_name
);
1457 g_free(comp_cls_name
);
1460 g_string_free(substring
, TRUE
);
1463 argpar_parse_ret_fini(&argpar_parse_ret
);
1469 * Prints the help command usage.
1472 void print_query_usage(FILE *fp
)
1474 fprintf(fp
, "Usage: babeltrace2 [GEN OPTS] query [OPTS] TYPE.PLUGIN.CLS OBJECT\n");
1476 fprintf(fp
, "Options:\n");
1478 fprintf(fp
, " -p, --params=PARAMS Set the query parameters to PARAMS (see the expected\n");
1479 fprintf(fp
, " format of PARAMS below)\n");
1480 fprintf(fp
, " -h, --help Show this help and quit\n");
1481 fprintf(fp
, "\n\n");
1482 print_expected_params_format(fp
);
1486 const struct argpar_opt_descr query_options
[] = {
1487 /* id, short_name, long_name, with_arg */
1488 { OPT_HELP
, 'h', "help", false },
1489 { OPT_PARAMS
, 'p', "params", true },
1490 ARGPAR_OPT_DESCR_SENTINEL
1494 * Creates a Babeltrace config object from the arguments of a query
1497 * *retcode is set to the appropriate exit code to use.
1500 struct bt_config
*bt_config_query_from_args(int argc
, const char *argv
[],
1501 int *retcode
, const bt_value
*plugin_paths
,
1502 int default_log_level
)
1505 struct bt_config
*cfg
= NULL
;
1506 const char *component_class_spec
= NULL
;
1507 const char *query_object
= NULL
;
1508 GString
*error_str
= NULL
;
1509 struct argpar_parse_ret argpar_parse_ret
= { 0 };
1511 bt_value
*params
= bt_value_map_create();
1513 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1518 cfg
= bt_config_query_create(plugin_paths
);
1523 error_str
= g_string_new(NULL
);
1525 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1530 argpar_parse_ret
= argpar_parse(argc
, argv
, query_options
, true);
1531 if (argpar_parse_ret
.error
) {
1532 BT_CLI_LOGE_APPEND_CAUSE(
1533 "While parsing `query` command's command-line arguments: %s",
1534 argpar_parse_ret
.error
);
1538 if (help_option_is_specified(&argpar_parse_ret
)) {
1539 print_query_usage(stdout
);
1541 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1545 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
1546 struct argpar_item
*argpar_item
=
1547 argpar_parse_ret
.items
->items
[i
];
1549 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
1550 struct argpar_item_opt
*argpar_item_opt
=
1551 (struct argpar_item_opt
*) argpar_item
;
1552 const char *arg
= argpar_item_opt
->arg
;
1554 switch (argpar_item_opt
->descr
->id
) {
1557 bt_value
*parsed_params
= bt_param_parse(arg
, error_str
);
1558 bt_value_map_extend_status extend_status
;
1559 if (!parsed_params
) {
1560 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1565 extend_status
= bt_value_map_extend(params
, parsed_params
);
1566 BT_VALUE_PUT_REF_AND_RESET(parsed_params
);
1567 if (extend_status
) {
1568 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current parameters with --params option's argument:\n %s",
1575 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
1576 argpar_item_opt
->descr
->id
);
1580 struct argpar_item_non_opt
*argpar_item_non_opt
1581 = (struct argpar_item_non_opt
*) argpar_item
;
1584 * We need exactly two non-option arguments
1585 * which are the mandatory component class
1586 * specification and query object.
1588 if (!component_class_spec
) {
1589 component_class_spec
= argpar_item_non_opt
->arg
;
1590 } else if (!query_object
) {
1591 query_object
= argpar_item_non_opt
->arg
;
1593 BT_CLI_LOGE_APPEND_CAUSE("Extraneous command-line argument specified to `query` command: `%s`.",
1594 argpar_item_non_opt
->arg
);
1600 if (!component_class_spec
|| !query_object
) {
1601 print_query_usage(stdout
);
1603 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1607 cfg
->cmd_data
.query
.cfg_component
=
1608 bt_config_component_from_arg(component_class_spec
,
1610 if (!cfg
->cmd_data
.query
.cfg_component
) {
1611 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for component class specification:\n %s",
1612 component_class_spec
);
1617 BT_OBJECT_MOVE_REF(cfg
->cmd_data
.query
.cfg_component
->params
, params
);
1619 if (strlen(query_object
) == 0) {
1620 BT_CLI_LOGE_APPEND_CAUSE("Invalid empty object.");
1624 g_string_assign(cfg
->cmd_data
.query
.object
, query_object
);
1629 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1632 argpar_parse_ret_fini(&argpar_parse_ret
);
1635 g_string_free(error_str
, TRUE
);
1638 bt_value_put_ref(params
);
1643 * Prints the list-plugins command usage.
1646 void print_list_plugins_usage(FILE *fp
)
1648 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] list-plugins [OPTIONS]\n");
1650 fprintf(fp
, "Options:\n");
1652 fprintf(fp
, " -h, --help Show this help and quit\n");
1654 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1656 fprintf(fp
, "Use `babeltrace2 help` to get help for a specific plugin or component class.\n");
1660 const struct argpar_opt_descr list_plugins_options
[] = {
1661 /* id, short_name, long_name, with_arg */
1662 { OPT_HELP
, 'h', "help", false },
1663 ARGPAR_OPT_DESCR_SENTINEL
1667 * Creates a Babeltrace config object from the arguments of a
1668 * list-plugins command.
1670 * *retcode is set to the appropriate exit code to use.
1673 struct bt_config
*bt_config_list_plugins_from_args(int argc
, const char *argv
[],
1674 int *retcode
, const bt_value
*plugin_paths
)
1676 struct bt_config
*cfg
= NULL
;
1677 struct argpar_parse_ret argpar_parse_ret
= { 0 };
1680 cfg
= bt_config_list_plugins_create(plugin_paths
);
1686 argpar_parse_ret
= argpar_parse(argc
, argv
, list_plugins_options
, true);
1687 if (argpar_parse_ret
.error
) {
1688 BT_CLI_LOGE_APPEND_CAUSE(
1689 "While parsing `list-plugins` command's command-line arguments: %s",
1690 argpar_parse_ret
.error
);
1694 if (help_option_is_specified(&argpar_parse_ret
)) {
1695 print_list_plugins_usage(stdout
);
1697 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1701 if (argpar_parse_ret
.items
->n_items
> 0) {
1703 * At this point we know there's at least one non-option
1704 * argument because we don't reach here with `--help`,
1707 struct argpar_item_non_opt
*non_opt
=
1708 (struct argpar_item_non_opt
*) argpar_parse_ret
.items
->items
[0];
1710 BT_CLI_LOGE_APPEND_CAUSE(
1711 "Extraneous command-line argument specified to `list-plugins` command: `%s`.",
1720 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1723 argpar_parse_ret_fini(&argpar_parse_ret
);
1729 * Prints the run command usage.
1732 void print_run_usage(FILE *fp
)
1734 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] run [OPTIONS]\n");
1736 fprintf(fp
, "Options:\n");
1738 fprintf(fp
, " -b, --base-params=PARAMS Set PARAMS as the current base parameters\n");
1739 fprintf(fp
, " for all the following components until\n");
1740 fprintf(fp
, " --reset-base-params is encountered\n");
1741 fprintf(fp
, " (see the expected format of PARAMS below)\n");
1742 fprintf(fp
, " -c, --component=NAME:TYPE.PLUGIN.CLS\n");
1743 fprintf(fp
, " Instantiate the component class CLS of type\n");
1744 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
1745 fprintf(fp
, " in the plugin PLUGIN, add it to the graph,\n");
1746 fprintf(fp
, " and name it NAME");
1747 fprintf(fp
, " -x, --connect=CONNECTION Connect two created components (see the\n");
1748 fprintf(fp
, " expected format of CONNECTION below)\n");
1749 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
1750 fprintf(fp
, " (`N`, `T`, `D`, `I`, `W`, `E`, or `F`)\n");
1751 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
1752 fprintf(fp
, " current component (see the expected format\n");
1753 fprintf(fp
, " of PARAMS below)\n");
1754 fprintf(fp
, " -r, --reset-base-params Reset the current base parameters to an\n");
1755 fprintf(fp
, " empty map\n");
1756 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
1757 fprintf(fp
, " the graph later, retry in DUR µs\n");
1758 fprintf(fp
, " (default: 100000)\n");
1759 fprintf(fp
, " -h, --help Show this help and quit\n");
1761 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
1762 fprintf(fp
, "\n\n");
1763 fprintf(fp
, "Expected format of CONNECTION\n");
1764 fprintf(fp
, "-----------------------------\n");
1766 fprintf(fp
, " UPSTREAM[.UPSTREAM-PORT]:DOWNSTREAM[.DOWNSTREAM-PORT]\n");
1768 fprintf(fp
, "UPSTREAM and DOWNSTREAM are names of the upstream and downstream\n");
1769 fprintf(fp
, "components to connect together. You must escape the following characters\n\n");
1770 fprintf(fp
, "with `\\`: `\\`, `.`, and `:`. You must set the name of the current\n");
1771 fprintf(fp
, "component using the NAME prefix of the --component option.\n");
1773 fprintf(fp
, "UPSTREAM-PORT and DOWNSTREAM-PORT are optional globbing patterns to\n");
1774 fprintf(fp
, "identify the upstream and downstream ports to use for the connection.\n");
1775 fprintf(fp
, "When the port is not specified, `*` is used.\n");
1777 fprintf(fp
, "When a component named UPSTREAM has an available port which matches the\n");
1778 fprintf(fp
, "UPSTREAM-PORT globbing pattern, it is connected to the first port which\n");
1779 fprintf(fp
, "matches the DOWNSTREAM-PORT globbing pattern of the component named\n");
1780 fprintf(fp
, "DOWNSTREAM.\n");
1782 fprintf(fp
, "The only special character in UPSTREAM-PORT and DOWNSTREAM-PORT is `*`\n");
1783 fprintf(fp
, "which matches anything. You must escape the following characters\n");
1784 fprintf(fp
, "with `\\`: `\\`, `*`, `?`, `[`, `.`, and `:`.\n");
1786 fprintf(fp
, "You can connect a source component to a filter or sink component. You\n");
1787 fprintf(fp
, "can connect a filter component to a sink component.\n");
1789 fprintf(fp
, "Examples:\n");
1791 fprintf(fp
, " my-src:my-sink\n");
1792 fprintf(fp
, " ctf-fs.*stream*:utils-muxer:*\n");
1794 fprintf(fp
, "IMPORTANT: Make sure to single-quote the whole argument when you run\n");
1795 fprintf(fp
, "babeltrace2 from a shell.\n");
1796 fprintf(fp
, "\n\n");
1797 print_expected_params_format(fp
);
1801 * Creates a Babeltrace config object from the arguments of a run
1804 * *retcode is set to the appropriate exit code to use.
1807 struct bt_config
*bt_config_run_from_args(int argc
, const char *argv
[],
1808 int *retcode
, const bt_value
*plugin_paths
,
1809 int default_log_level
)
1811 struct bt_config_component
*cur_cfg_comp
= NULL
;
1812 bt_value
*cur_base_params
= NULL
;
1814 struct bt_config
*cfg
= NULL
;
1815 bt_value
*instance_names
= NULL
;
1816 bt_value
*connection_args
= NULL
;
1817 char error_buf
[256] = { 0 };
1818 long retry_duration
= -1;
1819 bt_value_map_extend_status extend_status
;
1820 GString
*error_str
= NULL
;
1821 struct argpar_parse_ret argpar_parse_ret
= { 0 };
1824 static const struct argpar_opt_descr run_options
[] = {
1825 { OPT_BASE_PARAMS
, 'b', "base-params", true },
1826 { OPT_COMPONENT
, 'c', "component", true },
1827 { OPT_CONNECT
, 'x', "connect", true },
1828 { OPT_HELP
, 'h', "help", false },
1829 { OPT_LOG_LEVEL
, 'l', "log-level", true },
1830 { OPT_PARAMS
, 'p', "params", true },
1831 { OPT_RESET_BASE_PARAMS
, 'r', "reset-base-params", false },
1832 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
1833 ARGPAR_OPT_DESCR_SENTINEL
1838 error_str
= g_string_new(NULL
);
1840 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1845 print_run_usage(stdout
);
1850 cfg
= bt_config_run_create(plugin_paths
);
1855 cfg
->cmd_data
.run
.retry_duration_us
= 100000;
1856 cur_base_params
= bt_value_map_create();
1857 if (!cur_base_params
) {
1858 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1862 instance_names
= bt_value_map_create();
1863 if (!instance_names
) {
1864 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1868 connection_args
= bt_value_array_create();
1869 if (!connection_args
) {
1870 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1875 argpar_parse_ret
= argpar_parse(argc
, argv
, run_options
, true);
1876 if (argpar_parse_ret
.error
) {
1877 BT_CLI_LOGE_APPEND_CAUSE(
1878 "While parsing `run` command's command-line arguments: %s",
1879 argpar_parse_ret
.error
);
1883 if (help_option_is_specified(&argpar_parse_ret
)) {
1884 print_run_usage(stdout
);
1886 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
1890 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
1891 struct argpar_item
*argpar_item
=
1892 argpar_parse_ret
.items
->items
[i
];
1893 struct argpar_item_opt
*argpar_item_opt
;
1896 /* This command does not accept non-option arguments.*/
1897 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
1898 struct argpar_item_non_opt
*argpar_nonopt_item
=
1899 (struct argpar_item_non_opt
*) argpar_item
;
1901 BT_CLI_LOGE_APPEND_CAUSE("Unexpected argument: `%s`",
1902 argpar_nonopt_item
->arg
);
1906 argpar_item_opt
= (struct argpar_item_opt
*) argpar_item
;
1907 arg
= argpar_item_opt
->arg
;
1909 switch (argpar_item_opt
->descr
->id
) {
1912 enum bt_config_component_dest dest
;
1914 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
1915 cur_cfg_comp
= bt_config_component_from_arg(arg
,
1917 if (!cur_cfg_comp
) {
1918 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --component option's argument:\n %s",
1923 switch (cur_cfg_comp
->type
) {
1924 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
1925 dest
= BT_CONFIG_COMPONENT_DEST_SOURCE
;
1927 case BT_COMPONENT_CLASS_TYPE_FILTER
:
1928 dest
= BT_CONFIG_COMPONENT_DEST_FILTER
;
1930 case BT_COMPONENT_CLASS_TYPE_SINK
:
1931 dest
= BT_CONFIG_COMPONENT_DEST_SINK
;
1937 BT_ASSERT(cur_base_params
);
1938 bt_value_put_ref(cur_cfg_comp
->params
);
1939 if (bt_value_copy(cur_base_params
,
1940 &cur_cfg_comp
->params
) < 0) {
1941 BT_CLI_LOGE_APPEND_CAUSE_OOM();
1945 ret
= add_run_cfg_comp_check_name(cfg
,
1958 if (!cur_cfg_comp
) {
1959 BT_CLI_LOGE_APPEND_CAUSE("Cannot add parameters to unavailable component:\n %s",
1964 params
= bt_param_parse(arg
, error_str
);
1966 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --params option's argument:\n %s",
1971 extend_status
= bt_value_map_extend(cur_cfg_comp
->params
,
1973 BT_VALUE_PUT_REF_AND_RESET(params
);
1974 if (extend_status
!= BT_VALUE_MAP_EXTEND_STATUS_OK
) {
1975 BT_CLI_LOGE_APPEND_CAUSE("Cannot extend current component parameters with --params option's argument:\n %s",
1983 if (!cur_cfg_comp
) {
1984 BT_CLI_LOGE_APPEND_CAUSE("Cannot set the log level of unavailable component:\n %s",
1989 cur_cfg_comp
->log_level
=
1990 bt_log_get_level_from_string(arg
);
1991 if (cur_cfg_comp
->log_level
< 0) {
1992 BT_CLI_LOGE_APPEND_CAUSE("Invalid argument for --log-level option:\n %s",
1997 case OPT_BASE_PARAMS
:
1999 bt_value
*params
= bt_param_parse(arg
, error_str
);
2002 BT_CLI_LOGE_APPEND_CAUSE("Invalid format for --base-params option's argument:\n %s",
2007 BT_OBJECT_MOVE_REF(cur_base_params
, params
);
2010 case OPT_RESET_BASE_PARAMS
:
2011 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2012 cur_base_params
= bt_value_map_create();
2013 if (!cur_base_params
) {
2014 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2019 if (bt_value_array_append_string_element(
2020 connection_args
, arg
)) {
2021 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2025 case OPT_RETRY_DURATION
: {
2027 size_t arg_len
= strlen(argpar_item_opt
->arg
);
2029 retry_duration
= g_ascii_strtoll(argpar_item_opt
->arg
, &end
, 10);
2031 if (arg_len
== 0 || end
!= (argpar_item_opt
->arg
+ arg_len
)) {
2032 BT_CLI_LOGE_APPEND_CAUSE(
2033 "Could not parse --retry-duration option's argument as an unsigned integer: `%s`",
2034 argpar_item_opt
->arg
);
2038 if (retry_duration
< 0) {
2039 BT_CLI_LOGE_APPEND_CAUSE("--retry-duration option's argument must be positive or 0: %ld",
2044 cfg
->cmd_data
.run
.retry_duration_us
=
2045 (uint64_t) retry_duration
;
2049 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
2050 argpar_item_opt
->descr
->id
);
2055 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2057 if (cfg
->cmd_data
.run
.sources
->len
== 0) {
2058 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no source component.");
2062 if (cfg
->cmd_data
.run
.sinks
->len
== 0) {
2063 BT_CLI_LOGE_APPEND_CAUSE("Incomplete graph: no sink component.");
2067 ret
= bt_config_cli_args_create_connections(cfg
,
2071 BT_CLI_LOGE_APPEND_CAUSE("Cannot creation connections:\n%s", error_buf
);
2079 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
2083 g_string_free(error_str
, TRUE
);
2086 argpar_parse_ret_fini(&argpar_parse_ret
);
2087 BT_OBJECT_PUT_REF_AND_RESET(cur_cfg_comp
);
2088 BT_VALUE_PUT_REF_AND_RESET(cur_base_params
);
2089 BT_VALUE_PUT_REF_AND_RESET(instance_names
);
2090 BT_VALUE_PUT_REF_AND_RESET(connection_args
);
2095 struct bt_config
*bt_config_run_from_args_array(const bt_value
*run_args
,
2096 int *retcode
, const bt_value
*plugin_paths
,
2097 int default_log_level
)
2099 struct bt_config
*cfg
= NULL
;
2101 uint64_t i
, len
= bt_value_array_get_length(run_args
);
2103 BT_ASSERT(len
<= SIZE_MAX
);
2104 argv
= calloc((size_t) len
, sizeof(*argv
));
2106 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2110 for (i
= 0; i
< len
; i
++) {
2111 const bt_value
*arg_value
=
2112 bt_value_array_borrow_element_by_index_const(run_args
,
2116 arg
= bt_value_string_get(arg_value
);
2121 cfg
= bt_config_run_from_args((int) len
, argv
, retcode
,
2122 plugin_paths
, default_log_level
);
2130 * Prints the convert command usage.
2133 void print_convert_usage(FILE *fp
)
2135 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [convert] [OPTIONS] [PATH/URL]\n");
2137 fprintf(fp
, "Options:\n");
2139 fprintf(fp
, " -c, --component=[NAME:]TYPE.PLUGIN.CLS\n");
2140 fprintf(fp
, " Instantiate the component class CLS of type\n");
2141 fprintf(fp
, " TYPE (`source`, `filter`, or `sink`) found\n");
2142 fprintf(fp
, " in the plugin PLUGIN, add it to the\n");
2143 fprintf(fp
, " conversion graph, and optionally name it\n");
2144 fprintf(fp
, " NAME\n");
2145 fprintf(fp
, " -l, --log-level=LVL Set the log level of the current component to LVL\n");
2146 fprintf(fp
, " (`N`, `T`, `D`, `I`, `W`, `E`, or `F`)\n");
2147 fprintf(fp
, " -p, --params=PARAMS Add initialization parameters PARAMS to the\n");
2148 fprintf(fp
, " current component (see the expected format\n");
2149 fprintf(fp
, " of PARAMS below)\n");
2150 fprintf(fp
, " --retry-duration=DUR When babeltrace2(1) needs to retry to run\n");
2151 fprintf(fp
, " the graph later, retry in DUR µs\n");
2152 fprintf(fp
, " (default: 100000)\n");
2153 fprintf(fp
, " dynamic plugins can be loaded\n");
2154 fprintf(fp
, " --run-args Print the equivalent arguments for the\n");
2155 fprintf(fp
, " `run` command to the standard output,\n");
2156 fprintf(fp
, " formatted for a shell, and quit\n");
2157 fprintf(fp
, " --run-args-0 Print the equivalent arguments for the\n");
2158 fprintf(fp
, " `run` command to the standard output,\n");
2159 fprintf(fp
, " formatted for `xargs -0`, and quit\n");
2160 fprintf(fp
, " --stream-intersection Only process events when all streams\n");
2161 fprintf(fp
, " are active\n");
2162 fprintf(fp
, " -h, --help Show this help and quit\n");
2164 fprintf(fp
, "Implicit `source.ctf.fs` component options:\n");
2166 fprintf(fp
, " --clock-force-correlate Force the origin of all clocks\n");
2167 fprintf(fp
, " to the Unix epoch\n");
2168 fprintf(fp
, " --clock-offset=SEC Set clock offset to SEC seconds\n");
2169 fprintf(fp
, " --clock-offset-ns=NS Set clock offset to NS ns\n");
2171 fprintf(fp
, "Implicit `sink.text.pretty` component options:\n");
2173 fprintf(fp
, " --clock-cycles Print timestamps in clock cycles\n");
2174 fprintf(fp
, " --clock-date Print timestamp dates\n");
2175 fprintf(fp
, " --clock-gmt Print and parse timestamps in the GMT\n");
2176 fprintf(fp
, " time zone instead of the local time zone\n");
2177 fprintf(fp
, " --clock-seconds Print the timestamps as `SEC.NS` instead\n");
2178 fprintf(fp
, " of `hh:mm:ss.nnnnnnnnn`\n");
2179 fprintf(fp
, " --color=(never | auto | always)\n");
2180 fprintf(fp
, " Never, automatically, or always emit\n");
2181 fprintf(fp
, " console color codes\n");
2182 fprintf(fp
, " -f, --fields=FIELD[,FIELD]... Print additional fields; FIELD can be:\n");
2183 fprintf(fp
, " `all`, `trace`, `trace:hostname`,\n");
2184 fprintf(fp
, " `trace:domain`, `trace:procname`,\n");
2185 fprintf(fp
, " `trace:vpid`, `loglevel`, `emf`\n");
2186 fprintf(fp
, " -n, --names=NAME[,NAME]... Print field names; NAME can be:\n");
2187 fprintf(fp
, " `payload` (or `arg` or `args`), `none`,\n");
2188 fprintf(fp
, " `all`, `scope`, `header`, `context`\n");
2189 fprintf(fp
, " (or `ctx`)\n");
2190 fprintf(fp
, " --no-delta Do not print time delta between\n");
2191 fprintf(fp
, " consecutive events\n");
2192 fprintf(fp
, " -w, --output=PATH Write output text to PATH instead of\n");
2193 fprintf(fp
, " the standard output\n");
2195 fprintf(fp
, "Implicit `filter.utils.trimmer` component options:\n");
2197 fprintf(fp
, " -b, --begin=BEGIN Set the beginning time of the conversion\n");
2198 fprintf(fp
, " time range to BEGIN (see the format of\n");
2199 fprintf(fp
, " BEGIN below)\n");
2200 fprintf(fp
, " -e, --end=END Set the end time of the conversion time\n");
2201 fprintf(fp
, " range to END (see the format of END below)\n");
2202 fprintf(fp
, " -t, --timerange=TIMERANGE Set conversion time range to TIMERANGE:\n");
2203 fprintf(fp
, " BEGIN,END or [BEGIN,END] (literally `[` and\n");
2204 fprintf(fp
, " `]`) (see the format of BEGIN/END below)\n");
2206 fprintf(fp
, "Implicit `filter.lttng-utils.debug-info` component options:\n");
2208 fprintf(fp
, " --debug-info Create an implicit\n");
2209 fprintf(fp
, " `filter.lttng-utils.debug-info` component\n");
2210 fprintf(fp
, " --debug-info-dir=DIR Search for debug info in directory DIR\n");
2211 fprintf(fp
, " instead of `/usr/lib/debug`\n");
2212 fprintf(fp
, " --debug-info-full-path Show full debug info source and\n");
2213 fprintf(fp
, " binary paths instead of just names\n");
2214 fprintf(fp
, " --debug-info-target-prefix=DIR\n");
2215 fprintf(fp
, " Use directory DIR as a prefix when\n");
2216 fprintf(fp
, " looking up executables during debug\n");
2217 fprintf(fp
, " info analysis\n");
2219 fprintf(fp
, "Legacy options that still work:\n");
2221 fprintf(fp
, " -i, --input-format=(ctf | lttng-live)\n");
2222 fprintf(fp
, " `ctf`:\n");
2223 fprintf(fp
, " Create an implicit `source.ctf.fs`\n");
2224 fprintf(fp
, " component\n");
2225 fprintf(fp
, " `lttng-live`:\n");
2226 fprintf(fp
, " Create an implicit `source.ctf.lttng-live`\n");
2227 fprintf(fp
, " component\n");
2228 fprintf(fp
, " -o, --output-format=(text | ctf | dummy | ctf-metadata)\n");
2229 fprintf(fp
, " `text`:\n");
2230 fprintf(fp
, " Create an implicit `sink.text.pretty`\n");
2231 fprintf(fp
, " component\n");
2232 fprintf(fp
, " `ctf`:\n");
2233 fprintf(fp
, " Create an implicit `sink.ctf.fs`\n");
2234 fprintf(fp
, " component\n");
2235 fprintf(fp
, " `dummy`:\n");
2236 fprintf(fp
, " Create an implicit `sink.utils.dummy`\n");
2237 fprintf(fp
, " component\n");
2238 fprintf(fp
, " `ctf-metadata`:\n");
2239 fprintf(fp
, " Query the `source.ctf.fs` component class\n");
2240 fprintf(fp
, " for metadata text and quit\n");
2242 fprintf(fp
, "See `babeltrace2 --help` for the list of general options.\n");
2243 fprintf(fp
, "\n\n");
2244 fprintf(fp
, "Format of BEGIN and END\n");
2245 fprintf(fp
, "-----------------------\n");
2247 fprintf(fp
, " [YYYY-MM-DD [hh:mm:]]ss[.nnnnnnnnn]\n");
2248 fprintf(fp
, "\n\n");
2249 print_expected_params_format(fp
);
2253 const struct argpar_opt_descr convert_options
[] = {
2254 /* id, short_name, long_name, with_arg */
2255 { OPT_BEGIN
, 'b', "begin", true },
2256 { OPT_CLOCK_CYCLES
, '\0', "clock-cycles", false },
2257 { OPT_CLOCK_DATE
, '\0', "clock-date", false },
2258 { OPT_CLOCK_FORCE_CORRELATE
, '\0', "clock-force-correlate", false },
2259 { OPT_CLOCK_GMT
, '\0', "clock-gmt", false },
2260 { OPT_CLOCK_OFFSET
, '\0', "clock-offset", true },
2261 { OPT_CLOCK_OFFSET_NS
, '\0', "clock-offset-ns", true },
2262 { OPT_CLOCK_SECONDS
, '\0', "clock-seconds", false },
2263 { OPT_COLOR
, '\0', "color", true },
2264 { OPT_COMPONENT
, 'c', "component", true },
2265 { OPT_DEBUG
, 'd', "debug", false },
2266 { OPT_DEBUG_INFO_DIR
, '\0', "debug-info-dir", true },
2267 { OPT_DEBUG_INFO_FULL_PATH
, '\0', "debug-info-full-path", false },
2268 { OPT_DEBUG_INFO_TARGET_PREFIX
, '\0', "debug-info-target-prefix", true },
2269 { OPT_END
, 'e', "end", true },
2270 { OPT_FIELDS
, 'f', "fields", true },
2271 { OPT_HELP
, 'h', "help", false },
2272 { OPT_INPUT_FORMAT
, 'i', "input-format", true },
2273 { OPT_LOG_LEVEL
, 'l', "log-level", true },
2274 { OPT_NAMES
, 'n', "names", true },
2275 { OPT_DEBUG_INFO
, '\0', "debug-info", false },
2276 { OPT_NO_DELTA
, '\0', "no-delta", false },
2277 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
2278 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
2279 { OPT_OUTPUT
, 'w', "output", true },
2280 { OPT_OUTPUT_FORMAT
, 'o', "output-format", true },
2281 { OPT_PARAMS
, 'p', "params", true },
2282 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
2283 { OPT_RETRY_DURATION
, '\0', "retry-duration", true },
2284 { OPT_RUN_ARGS
, '\0', "run-args", false },
2285 { OPT_RUN_ARGS_0
, '\0', "run-args-0", false },
2286 { OPT_STREAM_INTERSECTION
, '\0', "stream-intersection", false },
2287 { OPT_TIMERANGE
, '\0', "timerange", true },
2288 { OPT_VERBOSE
, 'v', "verbose", false },
2289 ARGPAR_OPT_DESCR_SENTINEL
2293 GString
*get_component_auto_name(const char *prefix
,
2294 const bt_value
*existing_names
)
2297 GString
*auto_name
= g_string_new(NULL
);
2300 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2304 if (!bt_value_map_has_entry(existing_names
, prefix
)) {
2305 g_string_assign(auto_name
, prefix
);
2310 g_string_printf(auto_name
, "%s-%d", prefix
, i
);
2312 } while (bt_value_map_has_entry(existing_names
, auto_name
->str
));
2318 struct implicit_component_args
{
2321 /* The component class name (e.g. src.ctf.fs). */
2324 /* The component instance name. */
2327 GString
*params_arg
;
2328 bt_value
*extra_params
;
2332 int assign_name_to_implicit_component(struct implicit_component_args
*args
,
2333 const char *prefix
, bt_value
*existing_names
,
2334 GList
**comp_names
, bool append_to_comp_names
)
2337 GString
*name
= NULL
;
2339 if (!args
->exists
) {
2343 name
= get_component_auto_name(prefix
,
2351 g_string_assign(args
->name_arg
, name
->str
);
2353 if (bt_value_map_insert_entry(existing_names
, name
->str
,
2355 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2360 if (append_to_comp_names
) {
2361 *comp_names
= g_list_append(*comp_names
, name
);
2367 g_string_free(name
, TRUE
);
2374 int append_run_args_for_implicit_component(
2375 struct implicit_component_args
*impl_args
,
2380 GString
*component_arg_for_run
= NULL
;
2382 if (!impl_args
->exists
) {
2386 component_arg_for_run
= g_string_new(NULL
);
2387 if (!component_arg_for_run
) {
2388 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2392 /* Build the full `name:type.plugin.cls`. */
2393 BT_ASSERT(!strchr(impl_args
->name_arg
->str
, '\\'));
2394 BT_ASSERT(!strchr(impl_args
->name_arg
->str
, ':'));
2395 g_string_printf(component_arg_for_run
, "%s:%s",
2396 impl_args
->name_arg
->str
, impl_args
->comp_arg
->str
);
2398 if (bt_value_array_append_string_element(run_args
, "--component")) {
2399 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2403 if (bt_value_array_append_string_element(run_args
,
2404 component_arg_for_run
->str
)) {
2405 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2409 if (impl_args
->params_arg
->len
> 0) {
2410 if (bt_value_array_append_string_element(run_args
, "--params")) {
2411 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2415 if (bt_value_array_append_string_element(run_args
,
2416 impl_args
->params_arg
->str
)) {
2417 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2422 for (i
= 0; i
< bt_value_array_get_length(impl_args
->extra_params
); i
++) {
2423 const bt_value
*elem
;
2426 elem
= bt_value_array_borrow_element_by_index(
2427 impl_args
->extra_params
, i
);
2429 BT_ASSERT(bt_value_is_string(elem
));
2430 arg
= bt_value_string_get(elem
);
2431 ret
= bt_value_array_append_string_element(run_args
, arg
);
2433 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2444 if (component_arg_for_run
) {
2445 g_string_free(component_arg_for_run
, TRUE
);
2451 /* Free the fields of a `struct implicit_component_args`. */
2454 void finalize_implicit_component_args(struct implicit_component_args
*args
)
2458 if (args
->comp_arg
) {
2459 g_string_free(args
->comp_arg
, TRUE
);
2462 if (args
->name_arg
) {
2463 g_string_free(args
->name_arg
, TRUE
);
2466 if (args
->params_arg
) {
2467 g_string_free(args
->params_arg
, TRUE
);
2470 bt_value_put_ref(args
->extra_params
);
2473 /* Destroy a dynamically-allocated `struct implicit_component_args`. */
2476 void destroy_implicit_component_args(struct implicit_component_args
*args
)
2478 finalize_implicit_component_args(args
);
2482 /* Initialize the fields of an already allocated `struct implicit_component_args`. */
2485 int init_implicit_component_args(struct implicit_component_args
*args
,
2486 const char *comp_arg
, bool exists
)
2490 args
->exists
= exists
;
2491 args
->comp_arg
= g_string_new(comp_arg
);
2492 args
->name_arg
= g_string_new(NULL
);
2493 args
->params_arg
= g_string_new(NULL
);
2494 args
->extra_params
= bt_value_array_create();
2496 if (!args
->comp_arg
|| !args
->name_arg
||
2497 !args
->params_arg
|| !args
->extra_params
) {
2499 finalize_implicit_component_args(args
);
2500 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2508 /* Dynamically allocate and initialize a `struct implicit_component_args`. */
2511 struct implicit_component_args
*create_implicit_component_args(
2512 const char *comp_arg
)
2514 struct implicit_component_args
*args
;
2517 args
= g_new(struct implicit_component_args
, 1);
2519 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2523 status
= init_implicit_component_args(args
, comp_arg
, true);
2534 void append_implicit_component_param(struct implicit_component_args
*args
,
2535 const char *key
, const char *value
)
2540 append_param_arg(args
->params_arg
, key
, value
);
2544 * Append the given parameter (`key=value`) to all component specifications
2545 * in `implicit_comp_args` (an array of `struct implicit_component_args *`)
2546 * which match `comp_arg`.
2548 * Return the number of matching components.
2552 int append_multiple_implicit_components_param(GPtrArray
*implicit_comp_args
,
2553 const char *comp_arg
, const char *key
, const char *value
)
2558 for (i
= 0; i
< implicit_comp_args
->len
; i
++) {
2559 struct implicit_component_args
*args
= implicit_comp_args
->pdata
[i
];
2561 if (strcmp(args
->comp_arg
->str
, comp_arg
) == 0) {
2562 append_implicit_component_param(args
, key
, value
);
2570 /* Escape value to make it suitable to use as a string parameter value. */
2572 gchar
*escape_string_value(const char *value
)
2577 ret
= g_string_new(NULL
);
2579 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2588 g_string_append_c(ret
, '\\');
2592 g_string_append_c(ret
, *in
);
2598 return g_string_free(ret
, FALSE
);
2602 int bt_value_to_cli_param_value_append(const bt_value
*value
, GString
*buf
)
2608 switch (bt_value_get_type(value
)) {
2609 case BT_VALUE_TYPE_STRING
:
2611 const char *str_value
= bt_value_string_get(value
);
2612 gchar
*escaped_str_value
;
2614 escaped_str_value
= escape_string_value(str_value
);
2615 if (!escaped_str_value
) {
2619 g_string_append_printf(buf
, "\"%s\"", escaped_str_value
);
2621 g_free(escaped_str_value
);
2624 case BT_VALUE_TYPE_ARRAY
: {
2625 g_string_append_c(buf
, '[');
2626 uint64_t sz
= bt_value_array_get_length(value
);
2627 for (uint64_t i
= 0; i
< sz
; i
++) {
2628 const bt_value
*item
;
2631 g_string_append(buf
, ", ");
2634 item
= bt_value_array_borrow_element_by_index_const(
2636 ret
= bt_value_to_cli_param_value_append(item
, buf
);
2642 g_string_append_c(buf
, ']');
2656 * Convert `value` to its equivalent representation as a command line parameter
2661 gchar
*bt_value_to_cli_param_value(bt_value
*value
)
2664 gchar
*result
= NULL
;
2666 buf
= g_string_new(NULL
);
2668 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2672 if (bt_value_to_cli_param_value_append(value
, buf
)) {
2676 result
= g_string_free(buf
, FALSE
);
2683 g_string_free(buf
, TRUE
);
2691 int append_parameter_to_args(bt_value
*args
, const char *key
, bt_value
*value
)
2694 BT_ASSERT(bt_value_get_type(args
) == BT_VALUE_TYPE_ARRAY
);
2699 gchar
*str_value
= NULL
;
2700 GString
*parameter
= NULL
;
2702 if (bt_value_array_append_string_element(args
, "--params")) {
2703 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2708 str_value
= bt_value_to_cli_param_value(value
);
2714 parameter
= g_string_new(NULL
);
2716 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2721 g_string_printf(parameter
, "%s=%s", key
, str_value
);
2723 if (bt_value_array_append_string_element(args
, parameter
->str
)) {
2724 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2731 g_string_free(parameter
, TRUE
);
2744 int append_string_parameter_to_args(bt_value
*args
, const char *key
, const char *value
)
2746 bt_value
*str_value
;
2749 str_value
= bt_value_string_create_init(value
);
2752 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2757 ret
= append_parameter_to_args(args
, key
, str_value
);
2760 BT_VALUE_PUT_REF_AND_RESET(str_value
);
2765 int append_implicit_component_extra_param(struct implicit_component_args
*args
,
2766 const char *key
, const char *value
)
2768 return append_string_parameter_to_args(args
->extra_params
, key
, value
);
2772 * Escapes `.`, `:`, and `\` of `input` with `\`.
2775 GString
*escape_dot_colon(const char *input
)
2777 GString
*output
= g_string_new(NULL
);
2781 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2785 for (ch
= input
; *ch
!= '\0'; ch
++) {
2786 if (*ch
== '\\' || *ch
== '.' || *ch
== ':') {
2787 g_string_append_c(output
, '\\');
2790 g_string_append_c(output
, *ch
);
2798 * Appends a --connect option to a list of arguments. `upstream_name`
2799 * and `downstream_name` are escaped with escape_dot_colon() in this
2803 int append_connect_arg(bt_value
*run_args
,
2804 const char *upstream_name
, const char *downstream_name
)
2807 GString
*e_upstream_name
= escape_dot_colon(upstream_name
);
2808 GString
*e_downstream_name
= escape_dot_colon(downstream_name
);
2809 GString
*arg
= g_string_new(NULL
);
2811 if (!e_upstream_name
|| !e_downstream_name
|| !arg
) {
2812 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2817 ret
= bt_value_array_append_string_element(run_args
, "--connect");
2819 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2824 g_string_append(arg
, e_upstream_name
->str
);
2825 g_string_append_c(arg
, ':');
2826 g_string_append(arg
, e_downstream_name
->str
);
2827 ret
= bt_value_array_append_string_element(run_args
, arg
->str
);
2829 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2836 g_string_free(arg
, TRUE
);
2839 if (e_upstream_name
) {
2840 g_string_free(e_upstream_name
, TRUE
);
2843 if (e_downstream_name
) {
2844 g_string_free(e_downstream_name
, TRUE
);
2851 * Appends the run command's --connect options for the convert command.
2854 int convert_auto_connect(bt_value
*run_args
,
2855 GList
*source_names
, GList
*filter_names
,
2859 GList
*source_at
= source_names
;
2860 GList
*filter_at
= filter_names
;
2862 GList
*sink_at
= sink_names
;
2864 BT_ASSERT(source_names
);
2865 BT_ASSERT(filter_names
);
2866 BT_ASSERT(sink_names
);
2868 /* Connect all sources to the first filter */
2869 for (source_at
= source_names
; source_at
; source_at
= g_list_next(source_at
)) {
2870 GString
*source_name
= source_at
->data
;
2871 GString
*filter_name
= filter_at
->data
;
2873 ret
= append_connect_arg(run_args
, source_name
->str
,
2880 filter_prev
= filter_at
;
2881 filter_at
= g_list_next(filter_at
);
2883 /* Connect remaining filters */
2884 for (; filter_at
; filter_prev
= filter_at
, filter_at
= g_list_next(filter_at
)) {
2885 GString
*filter_name
= filter_at
->data
;
2886 GString
*filter_prev_name
= filter_prev
->data
;
2888 ret
= append_connect_arg(run_args
, filter_prev_name
->str
,
2895 /* Connect last filter to all sinks */
2896 for (sink_at
= sink_names
; sink_at
; sink_at
= g_list_next(sink_at
)) {
2897 GString
*filter_name
= filter_prev
->data
;
2898 GString
*sink_name
= sink_at
->data
;
2900 ret
= append_connect_arg(run_args
, filter_name
->str
,
2917 int split_timerange(const char *arg
, char **begin
, char **end
)
2920 const char *ch
= arg
;
2922 GString
*g_begin
= NULL
;
2923 GString
*g_end
= NULL
;
2931 g_begin
= bt_common_string_until(ch
, "", ",", &end_pos
);
2932 if (!g_begin
|| ch
[end_pos
] != ',' || g_begin
->len
== 0) {
2938 g_end
= bt_common_string_until(ch
, "", "]", &end_pos
);
2939 if (!g_end
|| g_end
->len
== 0) {
2945 *begin
= g_begin
->str
;
2947 g_string_free(g_begin
, FALSE
);
2948 g_string_free(g_end
, FALSE
);
2958 g_string_free(g_begin
, TRUE
);
2962 g_string_free(g_end
, TRUE
);
2969 int g_list_prepend_gstring(GList
**list
, const char *string
)
2972 GString
*gs
= g_string_new(string
);
2977 BT_CLI_LOGE_APPEND_CAUSE_OOM();
2981 *list
= g_list_prepend(*list
, gs
);
2988 * Create `struct implicit_component_args` structures for each of the
2989 * source components we identified. Add them to `component_args`.
2991 * `non_opts` is an array of the non-option arguments passed on the command
2994 * `non_opt_params` is an array where each element is an array of
2995 * strings containing all the arguments to `--params` that apply to the
2996 * non-option argument at the same index. For example, if, for a
2997 * non-option argument, the following `--params` options applied:
2999 * --params=a=2 --params=b=3,c=4
3001 * its entry in `non_opt_params` would contain
3003 * ["a=2", "b=3,c=4"]
3007 int create_implicit_component_args_from_auto_discovered_sources(
3008 const struct auto_source_discovery
*auto_disc
,
3009 const bt_value
*non_opts
,
3010 const bt_value
*non_opt_params
,
3011 const bt_value
*non_opt_loglevels
,
3012 GPtrArray
*component_args
)
3014 gchar
*cc_name
= NULL
;
3015 struct implicit_component_args
*comp
= NULL
;
3019 len
= auto_disc
->results
->len
;
3021 for (i
= 0; i
< len
; i
++) {
3022 struct auto_source_discovery_result
*res
=
3023 g_ptr_array_index(auto_disc
->results
, i
);
3024 uint64_t orig_indices_i
, orig_indices_count
;
3027 cc_name
= g_strdup_printf("source.%s.%s", res
->plugin_name
, res
->source_cc_name
);
3029 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3033 comp
= create_implicit_component_args(cc_name
);
3039 * Append parameters and log levels of all the
3040 * non-option arguments that contributed to this
3041 * component instance coming into existence.
3043 orig_indices_count
= bt_value_array_get_length(res
->original_input_indices
);
3044 for (orig_indices_i
= 0; orig_indices_i
< orig_indices_count
; orig_indices_i
++) {
3045 const bt_value
*orig_idx_value
=
3046 bt_value_array_borrow_element_by_index(
3047 res
->original_input_indices
, orig_indices_i
);
3048 uint64_t orig_idx
= bt_value_integer_unsigned_get(orig_idx_value
);
3049 const bt_value
*params_array
=
3050 bt_value_array_borrow_element_by_index_const(
3051 non_opt_params
, orig_idx
);
3052 uint64_t params_i
, params_count
;
3053 const bt_value
*loglevel_value
;
3055 params_count
= bt_value_array_get_length(params_array
);
3056 for (params_i
= 0; params_i
< params_count
; params_i
++) {
3057 const bt_value
*params_value
=
3058 bt_value_array_borrow_element_by_index_const(
3059 params_array
, params_i
);
3060 const char *params
= bt_value_string_get(params_value
);
3061 bt_value_array_append_element_status append_status
;
3063 append_status
= bt_value_array_append_string_element(
3064 comp
->extra_params
, "--params");
3065 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3066 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3070 append_status
= bt_value_array_append_string_element(
3071 comp
->extra_params
, params
);
3072 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3073 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3078 loglevel_value
= bt_value_array_borrow_element_by_index_const(
3079 non_opt_loglevels
, orig_idx
);
3080 if (bt_value_get_type(loglevel_value
) == BT_VALUE_TYPE_STRING
) {
3081 const char *loglevel
= bt_value_string_get(loglevel_value
);
3082 bt_value_array_append_element_status append_status
;
3084 append_status
= bt_value_array_append_string_element(
3085 comp
->extra_params
, "--log-level");
3086 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3087 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3091 append_status
= bt_value_array_append_string_element(
3092 comp
->extra_params
, loglevel
);
3093 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3094 BT_CLI_LOGE_APPEND_CAUSE("Failed to append array element.");
3101 * If single input and a src.ctf.fs component, provide the
3102 * relative path from the path passed on the command line to the
3105 if (bt_value_array_get_length(res
->inputs
) == 1 &&
3106 strcmp(res
->plugin_name
, "ctf") == 0 &&
3107 strcmp(res
->source_cc_name
, "fs") == 0) {
3108 const bt_value
*orig_idx_value
=
3109 bt_value_array_borrow_element_by_index(
3110 res
->original_input_indices
, 0);
3111 uint64_t orig_idx
= bt_value_integer_unsigned_get(orig_idx_value
);
3112 const bt_value
*non_opt_value
=
3113 bt_value_array_borrow_element_by_index_const(
3114 non_opts
, orig_idx
);
3115 const char *non_opt
= bt_value_string_get(non_opt_value
);
3116 const bt_value
*input_value
=
3117 bt_value_array_borrow_element_by_index_const(
3119 const char *input
= bt_value_string_get(input_value
);
3121 BT_ASSERT(orig_indices_count
== 1);
3122 BT_ASSERT(g_str_has_prefix(input
, non_opt
));
3124 input
+= strlen(non_opt
);
3126 while (G_IS_DIR_SEPARATOR(*input
)) {
3130 if (strlen(input
) > 0) {
3131 append_string_parameter_to_args(comp
->extra_params
,
3132 "trace-name", input
);
3136 status
= append_parameter_to_args(comp
->extra_params
, "inputs", res
->inputs
);
3141 g_ptr_array_add(component_args
, comp
);
3155 destroy_implicit_component_args(comp
);
3162 * As we iterate the arguments to the convert command, this tracks what is the
3163 * type of the current item, to which some contextual options (e.g. --params)
3166 enum convert_current_item_type
{
3167 /* There is no current item. */
3168 CONVERT_CURRENT_ITEM_TYPE_NONE
,
3170 /* Current item is a component. */
3171 CONVERT_CURRENT_ITEM_TYPE_COMPONENT
,
3173 /* Current item is a non-option argument. */
3174 CONVERT_CURRENT_ITEM_TYPE_NON_OPT
,
3178 * Creates a Babeltrace config object from the arguments of a convert
3181 * *retcode is set to the appropriate exit code to use.
3184 struct bt_config
*bt_config_convert_from_args(int argc
, const char *argv
[],
3185 int *retcode
, const bt_value
*plugin_paths
,
3186 int *default_log_level
, const bt_interrupter
*interrupter
)
3188 enum convert_current_item_type current_item_type
=
3189 CONVERT_CURRENT_ITEM_TYPE_NONE
;
3191 struct bt_config
*cfg
= NULL
;
3192 bool got_input_format_opt
= false;
3193 bool got_output_format_opt
= false;
3194 bool trimmer_has_begin
= false;
3195 bool trimmer_has_end
= false;
3196 bool stream_intersection_mode
= false;
3197 bool print_run_args
= false;
3198 bool print_run_args_0
= false;
3199 bool print_ctf_metadata
= false;
3200 bt_value
*run_args
= NULL
;
3201 bt_value
*all_names
= NULL
;
3202 GList
*source_names
= NULL
;
3203 GList
*filter_names
= NULL
;
3204 GList
*sink_names
= NULL
;
3205 bt_value
*non_opts
= NULL
;
3206 bt_value
*non_opt_params
= NULL
;
3207 bt_value
*non_opt_loglevels
= NULL
;
3208 struct implicit_component_args implicit_ctf_output_args
= { 0 };
3209 struct implicit_component_args implicit_lttng_live_args
= { 0 };
3210 struct implicit_component_args implicit_dummy_args
= { 0 };
3211 struct implicit_component_args implicit_text_args
= { 0 };
3212 struct implicit_component_args implicit_debug_info_args
= { 0 };
3213 struct implicit_component_args implicit_muxer_args
= { 0 };
3214 struct implicit_component_args implicit_trimmer_args
= { 0 };
3215 char error_buf
[256] = { 0 };
3217 struct bt_common_lttng_live_url_parts lttng_live_url_parts
= { 0 };
3218 char *output
= NULL
;
3219 struct auto_source_discovery auto_disc
= { NULL
};
3220 GString
*auto_disc_comp_name
= NULL
;
3221 struct argpar_parse_ret argpar_parse_ret
= { 0 };
3222 GString
*name_gstr
= NULL
;
3223 GString
*component_arg_for_run
= NULL
;
3224 bt_value
*live_inputs_array_val
= NULL
;
3227 * Array of `struct implicit_component_args *` created for the sources
3228 * we have auto-discovered.
3230 GPtrArray
*discovered_source_args
= NULL
;
3233 * If set, restrict automatic source discovery to this component class
3236 const char *auto_source_discovery_restrict_plugin_name
= NULL
;
3237 const char *auto_source_discovery_restrict_component_class_name
= NULL
;
3239 bool ctf_fs_source_force_clock_class_unix_epoch_origin
= false;
3240 gchar
*ctf_fs_source_clock_class_offset_arg
= NULL
;
3241 gchar
*ctf_fs_source_clock_class_offset_ns_arg
= NULL
;
3245 print_convert_usage(stdout
);
3250 if (init_implicit_component_args(&implicit_ctf_output_args
,
3251 "sink.ctf.fs", false)) {
3255 if (init_implicit_component_args(&implicit_lttng_live_args
,
3256 "source.ctf.lttng-live", false)) {
3260 if (init_implicit_component_args(&implicit_text_args
,
3261 "sink.text.pretty", false)) {
3265 if (init_implicit_component_args(&implicit_dummy_args
,
3266 "sink.utils.dummy", false)) {
3270 if (init_implicit_component_args(&implicit_debug_info_args
,
3271 "filter.lttng-utils.debug-info", false)) {
3275 if (init_implicit_component_args(&implicit_muxer_args
,
3276 "filter.utils.muxer", true)) {
3280 if (init_implicit_component_args(&implicit_trimmer_args
,
3281 "filter.utils.trimmer", false)) {
3285 all_names
= bt_value_map_create();
3287 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3291 run_args
= bt_value_array_create();
3293 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3297 component_arg_for_run
= g_string_new(NULL
);
3298 if (!component_arg_for_run
) {
3299 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3303 non_opts
= bt_value_array_create();
3305 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3309 non_opt_params
= bt_value_array_create();
3310 if (!non_opt_params
) {
3311 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3315 non_opt_loglevels
= bt_value_array_create();
3316 if (!non_opt_loglevels
) {
3317 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3321 if (auto_source_discovery_init(&auto_disc
) != 0) {
3325 discovered_source_args
=
3326 g_ptr_array_new_with_free_func((GDestroyNotify
) destroy_implicit_component_args
);
3327 if (!discovered_source_args
) {
3328 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3332 auto_disc_comp_name
= g_string_new(NULL
);
3333 if (!auto_disc_comp_name
) {
3334 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3339 * First pass: collect all arguments which need to be passed
3340 * as is to the run command. This pass can also add --name
3341 * arguments if needed to automatically name unnamed component
3344 argpar_parse_ret
= argpar_parse(argc
, argv
, convert_options
, true);
3345 if (argpar_parse_ret
.error
) {
3346 BT_CLI_LOGE_APPEND_CAUSE(
3347 "While parsing `convert` command's command-line arguments: %s",
3348 argpar_parse_ret
.error
);
3352 if (help_option_is_specified(&argpar_parse_ret
)) {
3353 print_convert_usage(stdout
);
3355 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
3359 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
3360 struct argpar_item
*argpar_item
=
3361 argpar_parse_ret
.items
->items
[i
];
3362 struct argpar_item_opt
*argpar_item_opt
;
3364 char *plugin_name
= NULL
;
3365 char *comp_cls_name
= NULL
;
3368 if (argpar_item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
3369 argpar_item_opt
= (struct argpar_item_opt
*) argpar_item
;
3370 arg
= argpar_item_opt
->arg
;
3372 switch (argpar_item_opt
->descr
->id
) {
3375 bt_component_class_type type
;
3377 current_item_type
= CONVERT_CURRENT_ITEM_TYPE_COMPONENT
;
3379 /* Parse the argument */
3380 plugin_comp_cls_names(arg
, &name
, &plugin_name
,
3381 &comp_cls_name
, &type
);
3382 if (!plugin_name
|| !comp_cls_name
) {
3383 BT_CLI_LOGE_APPEND_CAUSE(
3384 "Invalid format for --component option's argument:\n %s",
3391 * Name was given by the user, verify it isn't
3394 if (bt_value_map_has_entry(all_names
, name
)) {
3395 BT_CLI_LOGE_APPEND_CAUSE(
3396 "Duplicate component instance name:\n %s",
3401 name_gstr
= g_string_new(name
);
3403 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3407 g_string_assign(component_arg_for_run
, arg
);
3409 /* Name not given by user, generate one. */
3410 name_gstr
= get_component_auto_name(arg
, all_names
);
3415 g_string_printf(component_arg_for_run
, "%s:%s",
3416 name_gstr
->str
, arg
);
3419 if (bt_value_array_append_string_element(run_args
,
3421 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3425 if (bt_value_array_append_string_element(run_args
,
3426 component_arg_for_run
->str
)) {
3427 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3432 * Remember this name globally, for the uniqueness of
3433 * all component names.
3435 if (bt_value_map_insert_entry(all_names
,
3436 name_gstr
->str
, bt_value_null
)) {
3437 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3442 * Remember this name specifically for the type of the
3443 * component. This is to create connection arguments.
3445 * The list takes ownership of `name_gstr`.
3448 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
3449 source_names
= g_list_append(source_names
, name_gstr
);
3451 case BT_COMPONENT_CLASS_TYPE_FILTER
:
3452 filter_names
= g_list_append(filter_names
, name_gstr
);
3454 case BT_COMPONENT_CLASS_TYPE_SINK
:
3455 sink_names
= g_list_append(sink_names
, name_gstr
);
3464 free(comp_cls_name
);
3467 comp_cls_name
= NULL
;
3471 if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_COMPONENT
) {
3473 * The current item is a component (--component option),
3474 * pass it directly to the run args.
3476 if (bt_value_array_append_string_element(run_args
,
3478 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3482 if (bt_value_array_append_string_element(run_args
, arg
)) {
3483 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3486 } else if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_NON_OPT
) {
3488 * The current item is a
3489 * non-option argument, record
3490 * it in `non_opt_params`.
3493 bt_value_array_append_element_status append_element_status
;
3494 uint64_t idx
= bt_value_array_get_length(non_opt_params
) - 1;
3496 array
= bt_value_array_borrow_element_by_index(non_opt_params
, idx
);
3498 append_element_status
= bt_value_array_append_string_element(array
, arg
);
3499 if (append_element_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3500 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3504 BT_CLI_LOGE_APPEND_CAUSE(
3505 "No current component (--component option) or non-option argument of which to set parameters:\n %s",
3511 if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_COMPONENT
) {
3512 if (bt_value_array_append_string_element(run_args
, "--log-level")) {
3513 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3517 if (bt_value_array_append_string_element(run_args
, arg
)) {
3518 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3521 } else if (current_item_type
== CONVERT_CURRENT_ITEM_TYPE_NON_OPT
) {
3522 uint64_t idx
= bt_value_array_get_length(non_opt_loglevels
) - 1;
3523 enum bt_value_array_set_element_by_index_status set_element_status
;
3524 bt_value
*log_level_str_value
;
3526 log_level_str_value
= bt_value_string_create_init(arg
);
3527 if (!log_level_str_value
) {
3528 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3532 set_element_status
=
3533 bt_value_array_set_element_by_index(non_opt_loglevels
,
3534 idx
, log_level_str_value
);
3535 bt_value_put_ref(log_level_str_value
);
3536 if (set_element_status
!= BT_VALUE_ARRAY_SET_ELEMENT_BY_INDEX_STATUS_OK
) {
3537 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3541 BT_CLI_LOGE_APPEND_CAUSE(
3542 "No current component (--component option) or non-option argument to assign a log level to:\n %s",
3548 case OPT_RETRY_DURATION
:
3549 if (bt_value_array_append_string_element(run_args
,
3550 "--retry-duration")) {
3551 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3555 if (bt_value_array_append_string_element(run_args
, arg
)) {
3556 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3561 case OPT_CLOCK_CYCLES
:
3562 case OPT_CLOCK_DATE
:
3563 case OPT_CLOCK_FORCE_CORRELATE
:
3565 case OPT_CLOCK_OFFSET
:
3566 case OPT_CLOCK_OFFSET_NS
:
3567 case OPT_CLOCK_SECONDS
:
3570 case OPT_DEBUG_INFO
:
3571 case OPT_DEBUG_INFO_DIR
:
3572 case OPT_DEBUG_INFO_FULL_PATH
:
3573 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3576 case OPT_INPUT_FORMAT
:
3579 case OPT_OUTPUT_FORMAT
:
3582 case OPT_RUN_ARGS_0
:
3583 case OPT_STREAM_INTERSECTION
:
3586 /* Ignore in this pass */
3589 BT_CLI_LOGE_APPEND_CAUSE("Unknown command-line option specified (option code %d).",
3590 argpar_item_opt
->descr
->id
);
3593 } else if (argpar_item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
3594 struct argpar_item_non_opt
*argpar_item_non_opt
;
3595 bt_value_array_append_element_status append_status
;
3597 current_item_type
= CONVERT_CURRENT_ITEM_TYPE_NON_OPT
;
3599 argpar_item_non_opt
= (struct argpar_item_non_opt
*) argpar_item
;
3601 append_status
= bt_value_array_append_string_element(non_opts
,
3602 argpar_item_non_opt
->arg
);
3603 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3604 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3608 append_status
= bt_value_array_append_empty_array_element(
3609 non_opt_params
, NULL
);
3610 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3611 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3615 append_status
= bt_value_array_append_element(non_opt_loglevels
, bt_value_null
);
3616 if (append_status
!= BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK
) {
3617 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3626 * Second pass: transform the convert-specific options and
3627 * arguments into implicit component instances for the run
3630 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
3631 struct argpar_item
*argpar_item
=
3632 argpar_parse_ret
.items
->items
[i
];
3633 struct argpar_item_opt
*argpar_item_opt
;
3636 if (argpar_item
->type
!= ARGPAR_ITEM_TYPE_OPT
) {
3640 argpar_item_opt
= (struct argpar_item_opt
*) argpar_item
;
3641 arg
= argpar_item_opt
->arg
;
3643 switch (argpar_item_opt
->descr
->id
) {
3645 if (trimmer_has_begin
) {
3646 printf("At --begin option: --begin or --timerange option already specified\n %s\n",
3651 trimmer_has_begin
= true;
3652 ret
= append_implicit_component_extra_param(
3653 &implicit_trimmer_args
, "begin", arg
);
3654 implicit_trimmer_args
.exists
= true;
3660 if (trimmer_has_end
) {
3661 printf("At --end option: --end or --timerange option already specified\n %s\n",
3666 trimmer_has_end
= true;
3667 ret
= append_implicit_component_extra_param(
3668 &implicit_trimmer_args
, "end", arg
);
3669 implicit_trimmer_args
.exists
= true;
3679 if (trimmer_has_begin
|| trimmer_has_end
) {
3680 printf("At --timerange option: --begin, --end, or --timerange option already specified\n %s\n",
3685 ret
= split_timerange(arg
, &begin
, &end
);
3687 BT_CLI_LOGE_APPEND_CAUSE("Invalid --timerange option's argument: expecting BEGIN,END or [BEGIN,END]:\n %s",
3692 ret
= append_implicit_component_extra_param(
3693 &implicit_trimmer_args
, "begin", begin
);
3694 ret
|= append_implicit_component_extra_param(
3695 &implicit_trimmer_args
, "end", end
);
3696 implicit_trimmer_args
.exists
= true;
3704 case OPT_CLOCK_CYCLES
:
3705 append_implicit_component_param(
3706 &implicit_text_args
, "clock-cycles", "yes");
3707 implicit_text_args
.exists
= true;
3709 case OPT_CLOCK_DATE
:
3710 append_implicit_component_param(
3711 &implicit_text_args
, "clock-date", "yes");
3712 implicit_text_args
.exists
= true;
3714 case OPT_CLOCK_FORCE_CORRELATE
:
3715 ctf_fs_source_force_clock_class_unix_epoch_origin
= true;
3718 append_implicit_component_param(
3719 &implicit_text_args
, "clock-gmt", "yes");
3720 append_implicit_component_param(
3721 &implicit_trimmer_args
, "gmt", "yes");
3722 implicit_text_args
.exists
= true;
3724 case OPT_CLOCK_OFFSET
:
3725 if (ctf_fs_source_clock_class_offset_arg
) {
3726 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset option\n");
3730 ctf_fs_source_clock_class_offset_arg
= g_strdup(arg
);
3731 if (!ctf_fs_source_clock_class_offset_arg
) {
3732 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3736 case OPT_CLOCK_OFFSET_NS
:
3737 if (ctf_fs_source_clock_class_offset_ns_arg
) {
3738 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --clock-offset-ns option\n");
3742 ctf_fs_source_clock_class_offset_ns_arg
= g_strdup(arg
);
3743 if (!ctf_fs_source_clock_class_offset_ns_arg
) {
3744 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3748 case OPT_CLOCK_SECONDS
:
3749 append_implicit_component_param(
3750 &implicit_text_args
, "clock-seconds", "yes");
3751 implicit_text_args
.exists
= true;
3754 implicit_text_args
.exists
= true;
3755 ret
= append_implicit_component_extra_param(
3756 &implicit_text_args
, "color", arg
);
3761 case OPT_DEBUG_INFO
:
3762 implicit_debug_info_args
.exists
= true;
3764 case OPT_DEBUG_INFO_DIR
:
3765 implicit_debug_info_args
.exists
= true;
3766 ret
= append_implicit_component_extra_param(
3767 &implicit_debug_info_args
, "debug-info-dir", arg
);
3772 case OPT_DEBUG_INFO_FULL_PATH
:
3773 implicit_debug_info_args
.exists
= true;
3774 append_implicit_component_param(
3775 &implicit_debug_info_args
, "full-path", "yes");
3777 case OPT_DEBUG_INFO_TARGET_PREFIX
:
3778 implicit_debug_info_args
.exists
= true;
3779 ret
= append_implicit_component_extra_param(
3780 &implicit_debug_info_args
,
3781 "target-prefix", arg
);
3788 bt_value
*fields
= fields_from_arg(arg
);
3794 implicit_text_args
.exists
= true;
3795 ret
= insert_flat_params_from_array(
3796 implicit_text_args
.params_arg
,
3798 bt_value_put_ref(fields
);
3806 bt_value
*names
= names_from_arg(arg
);
3812 implicit_text_args
.exists
= true;
3813 ret
= insert_flat_params_from_array(
3814 implicit_text_args
.params_arg
,
3816 bt_value_put_ref(names
);
3823 append_implicit_component_param(
3824 &implicit_text_args
, "no-delta", "yes");
3825 implicit_text_args
.exists
= true;
3827 case OPT_INPUT_FORMAT
:
3828 if (got_input_format_opt
) {
3829 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --input-format option.");
3833 got_input_format_opt
= true;
3835 if (strcmp(arg
, "ctf") == 0) {
3836 auto_source_discovery_restrict_plugin_name
= "ctf";
3837 auto_source_discovery_restrict_component_class_name
= "fs";
3838 } else if (strcmp(arg
, "lttng-live") == 0) {
3839 auto_source_discovery_restrict_plugin_name
= "ctf";
3840 auto_source_discovery_restrict_component_class_name
= "lttng-live";
3841 implicit_lttng_live_args
.exists
= true;
3843 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy input format:\n %s",
3848 case OPT_OUTPUT_FORMAT
:
3849 if (got_output_format_opt
) {
3850 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output-format option.");
3854 got_output_format_opt
= true;
3856 if (strcmp(arg
, "text") == 0) {
3857 implicit_text_args
.exists
= true;
3858 } else if (strcmp(arg
, "ctf") == 0) {
3859 implicit_ctf_output_args
.exists
= true;
3860 } else if (strcmp(arg
, "dummy") == 0) {
3861 implicit_dummy_args
.exists
= true;
3862 } else if (strcmp(arg
, "ctf-metadata") == 0) {
3863 print_ctf_metadata
= true;
3865 BT_CLI_LOGE_APPEND_CAUSE("Unknown legacy output format:\n %s",
3872 BT_CLI_LOGE_APPEND_CAUSE("Duplicate --output option");
3876 output
= strdup(arg
);
3878 BT_CLI_LOGE_APPEND_CAUSE_OOM();
3883 if (print_run_args_0
) {
3884 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
3888 print_run_args
= true;
3890 case OPT_RUN_ARGS_0
:
3891 if (print_run_args
) {
3892 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --run-args and --run-args-0.");
3896 print_run_args_0
= true;
3898 case OPT_STREAM_INTERSECTION
:
3900 * Applies to all traces implementing the
3901 * babeltrace.trace-infos query.
3903 stream_intersection_mode
= true;
3906 *default_log_level
=
3907 logging_level_min(*default_log_level
, BT_LOG_INFO
);
3910 *default_log_level
=
3911 logging_level_min(*default_log_level
, BT_LOG_TRACE
);
3918 set_auto_log_levels(default_log_level
);
3921 * Legacy behaviour: --verbose used to make the `text` output
3922 * format print more information. --verbose is now equivalent to
3923 * the INFO log level, which is why we compare to `BT_LOG_INFO`
3926 if (*default_log_level
== BT_LOG_INFO
) {
3927 append_implicit_component_param(&implicit_text_args
,
3931 /* Print CTF metadata or print LTTng live sessions */
3932 if (print_ctf_metadata
) {
3933 const bt_value
*bt_val_non_opt
;
3935 if (bt_value_array_is_empty(non_opts
)) {
3936 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf-metadata specified without a path.");
3940 if (bt_value_array_get_length(non_opts
) > 1) {
3941 BT_CLI_LOGE_APPEND_CAUSE("Too many paths specified for --output-format=ctf-metadata.");
3945 cfg
= bt_config_print_ctf_metadata_create(plugin_paths
);
3950 bt_val_non_opt
= bt_value_array_borrow_element_by_index_const(non_opts
, 0);
3951 g_string_assign(cfg
->cmd_data
.print_ctf_metadata
.path
,
3952 bt_value_string_get(bt_val_non_opt
));
3956 cfg
->cmd_data
.print_ctf_metadata
.output_path
,
3964 * If -o ctf was specified, make sure an output path (--output)
3965 * was also specified. --output does not imply -o ctf because
3966 * it's also used for the default, implicit -o text if -o ctf
3969 if (implicit_ctf_output_args
.exists
) {
3971 BT_CLI_LOGE_APPEND_CAUSE("--output-format=ctf specified without --output (trace output path).");
3976 * At this point we know that -o ctf AND --output were
3977 * specified. Make sure that no options were specified
3978 * which would imply -o text because --output would be
3979 * ambiguous in this case. For example, this is wrong:
3981 * babeltrace2 --names=all -o ctf --output=/tmp/path my-trace
3983 * because --names=all implies -o text, and --output
3984 * could apply to both the sink.text.pretty and
3985 * sink.ctf.fs implicit components.
3987 if (implicit_text_args
.exists
) {
3988 BT_CLI_LOGE_APPEND_CAUSE("Ambiguous --output option: --output-format=ctf specified but another option implies --output-format=text.");
3994 * If -o dummy and -o ctf were not specified, and if there are
3995 * no explicit sink components, then use an implicit
3996 * `sink.text.pretty` component.
3998 if (!implicit_dummy_args
.exists
&& !implicit_ctf_output_args
.exists
&&
4000 implicit_text_args
.exists
= true;
4004 * Set implicit `sink.text.pretty` or `sink.ctf.fs` component's
4005 * `path` parameter if --output was specified.
4008 if (implicit_text_args
.exists
) {
4009 append_implicit_component_extra_param(&implicit_text_args
,
4011 } else if (implicit_ctf_output_args
.exists
) {
4012 append_implicit_component_extra_param(&implicit_ctf_output_args
,
4017 /* Decide where the non-option argument(s) go */
4018 if (bt_value_array_get_length(non_opts
) > 0) {
4019 if (implicit_lttng_live_args
.exists
) {
4020 const bt_value
*bt_val_non_opt
;
4022 if (bt_value_array_get_length(non_opts
) > 1) {
4023 BT_CLI_LOGE_APPEND_CAUSE("Too many URLs specified for --input-format=lttng-live.");
4027 bt_val_non_opt
= bt_value_array_borrow_element_by_index_const(non_opts
, 0);
4028 lttng_live_url_parts
=
4029 bt_common_parse_lttng_live_url(bt_value_string_get(bt_val_non_opt
),
4030 error_buf
, sizeof(error_buf
));
4031 if (!lttng_live_url_parts
.proto
) {
4032 BT_CLI_LOGE_APPEND_CAUSE("Invalid LTTng live URL format: %s.",
4037 if (!lttng_live_url_parts
.session_name
) {
4038 /* Print LTTng live sessions */
4039 cfg
= bt_config_print_lttng_live_sessions_create(
4045 g_string_assign(cfg
->cmd_data
.print_lttng_live_sessions
.url
,
4046 bt_value_string_get(bt_val_non_opt
));
4050 cfg
->cmd_data
.print_lttng_live_sessions
.output_path
,
4057 live_inputs_array_val
= bt_value_array_create();
4058 if (!live_inputs_array_val
) {
4059 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4063 if (bt_value_array_append_string_element(
4064 live_inputs_array_val
,
4065 bt_value_string_get(bt_val_non_opt
))) {
4066 BT_CLI_LOGE_APPEND_CAUSE_OOM();
4070 ret
= append_parameter_to_args(
4071 implicit_lttng_live_args
.extra_params
,
4072 "inputs", live_inputs_array_val
);
4077 ret
= append_implicit_component_extra_param(
4078 &implicit_lttng_live_args
,
4079 "session-not-found-action", "end");
4085 size_t plugin_count
;
4086 const bt_plugin
**plugins
;
4087 const bt_plugin
*plugin
;
4089 status
= require_loaded_plugins(plugin_paths
);
4094 if (auto_source_discovery_restrict_plugin_name
) {
4096 plugin
= borrow_loaded_plugin_by_name(auto_source_discovery_restrict_plugin_name
);
4099 plugin_count
= get_loaded_plugins_count();
4100 plugins
= borrow_loaded_plugins();
4103 status
= auto_discover_source_components(non_opts
, plugins
, plugin_count
,
4104 auto_source_discovery_restrict_component_class_name
,
4105 *default_log_level
, &auto_disc
, interrupter
);
4108 if (status
== AUTO_SOURCE_DISCOVERY_STATUS_INTERRUPTED
) {
4109 BT_CURRENT_THREAD_ERROR_APPEND_CAUSE_FROM_UNKNOWN(
4110 "Babeltrace CLI", "Automatic source discovery interrupted by the user");
4115 status
= create_implicit_component_args_from_auto_discovered_sources(
4116 &auto_disc
, non_opts
, non_opt_params
, non_opt_loglevels
,
4117 discovered_source_args
);
4126 * If --clock-force-correlated was given, apply it to any src.ctf.fs
4129 if (ctf_fs_source_force_clock_class_unix_epoch_origin
) {
4132 n
= append_multiple_implicit_components_param(
4133 discovered_source_args
, "source.ctf.fs", "force-clock-class-origin-unix-epoch",
4136 BT_CLI_LOGE_APPEND_CAUSE("--clock-force-correlate specified, but no source.ctf.fs component instantiated.");
4141 /* If --clock-offset was given, apply it to any src.ctf.fs component. */
4142 if (ctf_fs_source_clock_class_offset_arg
) {
4145 n
= append_multiple_implicit_components_param(
4146 discovered_source_args
, "source.ctf.fs", "clock-class-offset-s",
4147 ctf_fs_source_clock_class_offset_arg
);
4150 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset specified, but no source.ctf.fs component instantiated.");
4155 /* If --clock-offset-ns was given, apply it to any src.ctf.fs component. */
4156 if (ctf_fs_source_clock_class_offset_ns_arg
) {
4159 n
= append_multiple_implicit_components_param(
4160 discovered_source_args
, "source.ctf.fs", "clock-class-offset-ns",
4161 ctf_fs_source_clock_class_offset_ns_arg
);
4164 BT_CLI_LOGE_APPEND_CAUSE("--clock-offset-ns specified, but no source.ctf.fs component instantiated.");
4170 * If the implicit `source.ctf.lttng-live` component exists,
4171 * make sure there's at least one non-option argument (which is
4174 if (implicit_lttng_live_args
.exists
&& bt_value_array_is_empty(non_opts
)) {
4175 BT_CLI_LOGE_APPEND_CAUSE("Missing URL for implicit `%s` component.",
4176 implicit_lttng_live_args
.comp_arg
->str
);
4180 /* Assign names to implicit components */
4181 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4182 struct implicit_component_args
*args
;
4185 args
= discovered_source_args
->pdata
[i
];
4187 g_string_printf(auto_disc_comp_name
, "auto-disc-%s", args
->comp_arg
->str
);
4189 /* Give it a name like `auto-disc-src-ctf-fs`. */
4190 for (j
= 0; j
< auto_disc_comp_name
->len
; j
++) {
4191 if (auto_disc_comp_name
->str
[j
] == '.') {
4192 auto_disc_comp_name
->str
[j
] = '-';
4196 ret
= assign_name_to_implicit_component(args
,
4197 auto_disc_comp_name
->str
, all_names
, &source_names
, true);
4203 ret
= assign_name_to_implicit_component(&implicit_lttng_live_args
,
4204 "lttng-live", all_names
, &source_names
, true);
4209 ret
= assign_name_to_implicit_component(&implicit_text_args
,
4210 "pretty", all_names
, &sink_names
, true);
4215 ret
= assign_name_to_implicit_component(&implicit_ctf_output_args
,
4216 "sink-ctf-fs", all_names
, &sink_names
, true);
4221 ret
= assign_name_to_implicit_component(&implicit_dummy_args
,
4222 "dummy", all_names
, &sink_names
, true);
4227 ret
= assign_name_to_implicit_component(&implicit_muxer_args
,
4228 "muxer", all_names
, NULL
, false);
4233 ret
= assign_name_to_implicit_component(&implicit_trimmer_args
,
4234 "trimmer", all_names
, NULL
, false);
4239 ret
= assign_name_to_implicit_component(&implicit_debug_info_args
,
4240 "debug-info", all_names
, NULL
, false);
4245 /* Make sure there's at least one source and one sink */
4246 if (!source_names
) {
4247 BT_CLI_LOGE_APPEND_CAUSE("No source component.");
4252 BT_CLI_LOGE_APPEND_CAUSE("No sink component.");
4256 /* Make sure there's a single sink component */
4257 if (g_list_length(sink_names
) != 1) {
4258 BT_CLI_LOGE_APPEND_CAUSE(
4259 "More than one sink component specified.");
4264 * Prepend the muxer, the trimmer, and the debug info to the
4265 * filter chain so that we have:
4267 * sources -> muxer -> [trimmer] -> [debug info] ->
4268 * [user filters] -> sinks
4270 if (implicit_debug_info_args
.exists
) {
4271 if (g_list_prepend_gstring(&filter_names
,
4272 implicit_debug_info_args
.name_arg
->str
)) {
4277 if (implicit_trimmer_args
.exists
) {
4278 if (g_list_prepend_gstring(&filter_names
,
4279 implicit_trimmer_args
.name_arg
->str
)) {
4284 if (g_list_prepend_gstring(&filter_names
,
4285 implicit_muxer_args
.name_arg
->str
)) {
4290 * Append the equivalent run arguments for the implicit
4293 for (i
= 0; i
< discovered_source_args
->len
; i
++) {
4294 struct implicit_component_args
*args
=
4295 discovered_source_args
->pdata
[i
];
4297 ret
= append_run_args_for_implicit_component(args
, run_args
);
4303 ret
= append_run_args_for_implicit_component(&implicit_lttng_live_args
,
4309 ret
= append_run_args_for_implicit_component(&implicit_text_args
,
4315 ret
= append_run_args_for_implicit_component(&implicit_ctf_output_args
,
4321 ret
= append_run_args_for_implicit_component(&implicit_dummy_args
,
4327 ret
= append_run_args_for_implicit_component(&implicit_muxer_args
,
4333 ret
= append_run_args_for_implicit_component(&implicit_trimmer_args
,
4339 ret
= append_run_args_for_implicit_component(&implicit_debug_info_args
,
4345 /* Auto-connect components */
4346 ret
= convert_auto_connect(run_args
, source_names
, filter_names
,
4349 BT_CLI_LOGE_APPEND_CAUSE("Cannot auto-connect components.");
4354 * We have all the run command arguments now. Depending on
4355 * --run-args, we pass this to the run command or print them
4358 if (print_run_args
|| print_run_args_0
) {
4359 uint64_t args_idx
, args_len
;
4360 if (stream_intersection_mode
) {
4361 BT_CLI_LOGE_APPEND_CAUSE("Cannot specify --stream-intersection with --run-args or --run-args-0.");
4365 args_len
= bt_value_array_get_length(run_args
);
4366 for (args_idx
= 0; args_idx
< args_len
; args_idx
++) {
4367 const bt_value
*arg_value
=
4368 bt_value_array_borrow_element_by_index(run_args
,
4371 GString
*quoted
= NULL
;
4372 const char *arg_to_print
;
4374 arg
= bt_value_string_get(arg_value
);
4376 if (print_run_args
) {
4377 quoted
= bt_common_shell_quote(arg
, true);
4382 arg_to_print
= quoted
->str
;
4387 printf("%s", arg_to_print
);
4390 g_string_free(quoted
, TRUE
);
4393 if (args_idx
< args_len
- 1) {
4394 if (print_run_args
) {
4403 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4407 cfg
= bt_config_run_from_args_array(run_args
, retcode
,
4408 plugin_paths
, *default_log_level
);
4413 cfg
->cmd_data
.run
.stream_intersection_mode
= stream_intersection_mode
;
4418 BT_OBJECT_PUT_REF_AND_RESET(cfg
);
4421 argpar_parse_ret_fini(&argpar_parse_ret
);
4425 if (component_arg_for_run
) {
4426 g_string_free(component_arg_for_run
, TRUE
);
4430 g_string_free(name_gstr
, TRUE
);
4433 bt_value_put_ref(live_inputs_array_val
);
4434 bt_value_put_ref(run_args
);
4435 bt_value_put_ref(all_names
);
4436 destroy_glist_of_gstring(source_names
);
4437 destroy_glist_of_gstring(filter_names
);
4438 destroy_glist_of_gstring(sink_names
);
4439 bt_value_put_ref(non_opt_params
);
4440 bt_value_put_ref(non_opt_loglevels
);
4441 bt_value_put_ref(non_opts
);
4442 finalize_implicit_component_args(&implicit_ctf_output_args
);
4443 finalize_implicit_component_args(&implicit_lttng_live_args
);
4444 finalize_implicit_component_args(&implicit_dummy_args
);
4445 finalize_implicit_component_args(&implicit_text_args
);
4446 finalize_implicit_component_args(&implicit_debug_info_args
);
4447 finalize_implicit_component_args(&implicit_muxer_args
);
4448 finalize_implicit_component_args(&implicit_trimmer_args
);
4449 bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts
);
4450 auto_source_discovery_fini(&auto_disc
);
4452 if (discovered_source_args
) {
4453 g_ptr_array_free(discovered_source_args
, TRUE
);
4456 g_free(ctf_fs_source_clock_class_offset_arg
);
4457 g_free(ctf_fs_source_clock_class_offset_ns_arg
);
4459 if (auto_disc_comp_name
) {
4460 g_string_free(auto_disc_comp_name
, TRUE
);
4467 * Prints the Babeltrace 2.x general usage.
4470 void print_gen_usage(FILE *fp
)
4472 fprintf(fp
, "Usage: babeltrace2 [GENERAL OPTIONS] [COMMAND] [COMMAND ARGUMENTS]\n");
4474 fprintf(fp
, "General options:\n");
4476 fprintf(fp
, " -d, --debug Enable debug mode (same as --log-level=T)\n");
4477 fprintf(fp
, " -h, --help Show this help and quit\n");
4478 fprintf(fp
, " -l, --log-level=LVL Set the default log level to LVL (`N`, `T`, `D`,\n");
4479 fprintf(fp
, " `I`, `W` (default), `E`, or `F`)\n");
4480 fprintf(fp
, " --omit-home-plugin-path Omit home plugins from plugin search path\n");
4481 fprintf(fp
, " (~/.local/lib/babeltrace2/plugins)\n");
4482 fprintf(fp
, " --omit-system-plugin-path Omit system plugins from plugin search path\n");
4483 fprintf(fp
, " --plugin-path=PATH[:PATH]... Add PATH to the list of paths from which\n");
4484 fprintf(fp
, " dynamic plugins can be loaded\n");
4485 fprintf(fp
, " -v, --verbose Enable verbose mode (same as --log-level=I)\n");
4486 fprintf(fp
, " -V, --version Show version and quit\n");
4488 fprintf(fp
, "Available commands:\n");
4490 fprintf(fp
, " convert Convert and trim traces (default)\n");
4491 fprintf(fp
, " help Get help for a plugin or a component class\n");
4492 fprintf(fp
, " list-plugins List available plugins and their content\n");
4493 fprintf(fp
, " query Query objects from a component class\n");
4494 fprintf(fp
, " run Build a processing graph and run it\n");
4496 fprintf(fp
, "Use `babeltrace2 COMMAND --help` to show the help of COMMAND.\n");
4499 struct bt_config
*bt_config_cli_args_create(int argc
, const char *argv
[],
4500 int *retcode
, bool omit_system_plugin_path
,
4501 bool omit_home_plugin_path
,
4502 const bt_value
*initial_plugin_paths
,
4503 const bt_interrupter
*interrupter
)
4505 struct bt_config
*config
= NULL
;
4508 const char **top_level_argv
;
4509 int command_argc
= -1;
4510 const char **command_argv
= NULL
;
4511 const char *command_name
= NULL
;
4512 int default_log_level
= -1;
4513 struct argpar_parse_ret argpar_parse_ret
= { 0 };
4514 bt_value
*plugin_paths
= NULL
;
4516 /* Top-level option descriptions. */
4517 static const struct argpar_opt_descr descrs
[] = {
4518 { OPT_DEBUG
, 'd', "debug", false },
4519 { OPT_HELP
, 'h', "help", false },
4520 { OPT_LOG_LEVEL
, 'l', "log-level", true },
4521 { OPT_VERBOSE
, 'v', "verbose", false },
4522 { OPT_VERSION
, 'V', "version", false},
4523 { OPT_OMIT_HOME_PLUGIN_PATH
, '\0', "omit-home-plugin-path", false },
4524 { OPT_OMIT_SYSTEM_PLUGIN_PATH
, '\0', "omit-system-plugin-path", false },
4525 { OPT_PLUGIN_PATH
, '\0', "plugin-path", true },
4526 ARGPAR_OPT_DESCR_SENTINEL
4530 COMMAND_TYPE_NONE
= -1,
4531 COMMAND_TYPE_RUN
= 0,
4532 COMMAND_TYPE_CONVERT
,
4533 COMMAND_TYPE_LIST_PLUGINS
,
4536 } command_type
= COMMAND_TYPE_NONE
;
4540 if (!initial_plugin_paths
) {
4541 plugin_paths
= bt_value_array_create();
4542 if (!plugin_paths
) {
4546 bt_value_copy_status copy_status
= bt_value_copy(
4547 initial_plugin_paths
, &plugin_paths
);
4553 BT_ASSERT(plugin_paths
);
4556 * The `BABELTRACE_PLUGIN_PATH` paths take precedence over the
4557 * `--plugin-path` option's paths, so append it now before
4558 * parsing the general options.
4560 if (append_env_var_plugin_paths(plugin_paths
)) {
4567 print_gen_usage(stdout
);
4571 /* Skip first argument, the name of the program. */
4572 top_level_argc
= argc
- 1;
4573 top_level_argv
= argv
+ 1;
4574 argpar_parse_ret
= argpar_parse(top_level_argc
, top_level_argv
,
4577 if (argpar_parse_ret
.error
) {
4578 BT_CLI_LOGE_APPEND_CAUSE(
4579 "While parsing command-line arguments: %s",
4580 argpar_parse_ret
.error
);
4584 for (i
= 0; i
< argpar_parse_ret
.items
->n_items
; i
++) {
4585 struct argpar_item
*item
;
4587 item
= argpar_parse_ret
.items
->items
[i
];
4589 if (item
->type
== ARGPAR_ITEM_TYPE_OPT
) {
4590 struct argpar_item_opt
*item_opt
=
4591 (struct argpar_item_opt
*) item
;
4593 switch (item_opt
->descr
->id
) {
4596 logging_level_min(default_log_level
, BT_LOG_TRACE
);
4600 logging_level_min(default_log_level
, BT_LOG_INFO
);
4604 int level
= bt_log_get_level_from_string(item_opt
->arg
);
4607 BT_CLI_LOGE_APPEND_CAUSE(
4608 "Invalid argument for --log-level option:\n %s",
4614 logging_level_min(default_log_level
, level
);
4617 case OPT_PLUGIN_PATH
:
4618 if (bt_config_append_plugin_paths_check_setuid_setgid(
4619 plugin_paths
, item_opt
->arg
)) {
4623 case OPT_OMIT_SYSTEM_PLUGIN_PATH
:
4624 omit_system_plugin_path
= true;
4626 case OPT_OMIT_HOME_PLUGIN_PATH
:
4627 omit_home_plugin_path
= true;
4633 print_gen_usage(stdout
);
4636 } else if (item
->type
== ARGPAR_ITEM_TYPE_NON_OPT
) {
4637 struct argpar_item_non_opt
*item_non_opt
=
4638 (struct argpar_item_non_opt
*) item
;
4640 * First unknown argument: is it a known command
4644 top_level_argc
- item_non_opt
->orig_index
- 1;
4646 &top_level_argv
[item_non_opt
->orig_index
+ 1];
4648 if (strcmp(item_non_opt
->arg
, "convert") == 0) {
4649 command_type
= COMMAND_TYPE_CONVERT
;
4650 command_name
= "convert";
4651 } else if (strcmp(item_non_opt
->arg
, "list-plugins") == 0) {
4652 command_type
= COMMAND_TYPE_LIST_PLUGINS
;
4653 command_name
= "list-plugins";
4654 } else if (strcmp(item_non_opt
->arg
, "help") == 0) {
4655 command_type
= COMMAND_TYPE_HELP
;
4656 command_name
= "help";
4657 } else if (strcmp(item_non_opt
->arg
, "query") == 0) {
4658 command_type
= COMMAND_TYPE_QUERY
;
4659 command_name
= "query";
4660 } else if (strcmp(item_non_opt
->arg
, "run") == 0) {
4661 command_type
= COMMAND_TYPE_RUN
;
4662 command_name
= "run";
4665 * Non-option argument, but not a known
4666 * command name: assume the default
4667 * `convert` command.
4669 command_type
= COMMAND_TYPE_CONVERT
;
4670 command_name
= "convert";
4678 if (command_type
== COMMAND_TYPE_NONE
) {
4679 if (argpar_parse_ret
.ingested_orig_args
== top_level_argc
) {
4681 * We only got non-help, non-version general options
4682 * like --verbose and --debug, without any other
4683 * arguments, so we can't do anything useful: print the
4686 print_gen_usage(stdout
);
4691 * We stopped on an unknown option argument (and therefore
4692 * didn't see a command name). Assume `convert` command.
4694 command_type
= COMMAND_TYPE_CONVERT
;
4695 command_name
= "convert";
4697 top_level_argc
- argpar_parse_ret
.ingested_orig_args
;
4699 &top_level_argv
[argpar_parse_ret
.ingested_orig_args
];
4702 BT_ASSERT(command_argv
);
4703 BT_ASSERT(command_argc
>= 0);
4706 * For all commands other than `convert`, we now know the log level to
4707 * use, so we can apply it with `set_auto_log_levels`.
4709 * The convert command has `--debug` and `--verbose` arguments that are
4710 * equivalent to the top-level arguments of the same name. So after it
4711 * has parsed its arguments, `bt_config_convert_from_args` calls
4712 * `set_auto_log_levels` itself.
4714 if (command_type
!= COMMAND_TYPE_CONVERT
) {
4715 set_auto_log_levels(&default_log_level
);
4719 * At this point, `plugin_paths` contains the initial plugin
4720 * paths, the paths from the `BABELTRACE_PLUGIN_PATH` paths, and
4721 * the paths from the `--plugin-path` option.
4723 * Now append the user and system plugin paths.
4725 if (append_home_and_system_plugin_paths(plugin_paths
,
4726 omit_system_plugin_path
, omit_home_plugin_path
)) {
4730 switch (command_type
) {
4731 case COMMAND_TYPE_RUN
:
4732 config
= bt_config_run_from_args(command_argc
, command_argv
,
4733 retcode
, plugin_paths
,
4736 case COMMAND_TYPE_CONVERT
:
4737 config
= bt_config_convert_from_args(command_argc
, command_argv
,
4738 retcode
, plugin_paths
, &default_log_level
, interrupter
);
4740 case COMMAND_TYPE_LIST_PLUGINS
:
4741 config
= bt_config_list_plugins_from_args(command_argc
,
4742 command_argv
, retcode
, plugin_paths
);
4744 case COMMAND_TYPE_HELP
:
4745 config
= bt_config_help_from_args(command_argc
,
4746 command_argv
, retcode
, plugin_paths
,
4749 case COMMAND_TYPE_QUERY
:
4750 config
= bt_config_query_from_args(command_argc
,
4751 command_argv
, retcode
, plugin_paths
,
4759 BT_ASSERT(default_log_level
>= BT_LOG_TRACE
);
4760 config
->log_level
= default_log_level
;
4761 config
->command_name
= command_name
;
4770 argpar_parse_ret_fini(&argpar_parse_ret
);
4771 bt_value_put_ref(plugin_paths
);